blob: a061d58e30d2255998598972f862394b7ee38fab [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;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070049import android.app.IStopUserCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.app.IThumbnailReceiver;
51import android.app.Instrumentation;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070052import android.app.Notification;
Dianne Hackborn860755f2010-06-03 18:47:52 -070053import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.app.PendingIntent;
Christopher Tate45281862010-03-05 15:46:30 -080055import android.app.backup.IBackupManager;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020056import android.content.ActivityNotFoundException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080057import android.content.BroadcastReceiver;
Dianne Hackborn21c241e2012-03-08 13:57:23 -080058import android.content.ClipData;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070059import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.ComponentName;
Jeff Sharkey110a6b62012-03-12 11:12:41 -070061import android.content.ContentProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062import android.content.ContentResolver;
63import android.content.Context;
Christian Mehlmauer7664e202010-07-20 08:46:17 +020064import android.content.DialogInterface;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070065import android.content.IContentProvider;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070066import android.content.IIntentReceiver;
67import android.content.IIntentSender;
Adam Powelldd8fab22012-03-22 17:47:27 -070068import android.content.Intent;
69import android.content.IntentFilter;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070070import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.pm.ActivityInfo;
72import android.content.pm.ApplicationInfo;
73import android.content.pm.ConfigurationInfo;
74import android.content.pm.IPackageDataObserver;
75import android.content.pm.IPackageManager;
76import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080077import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.content.pm.PackageManager;
Amith Yamasani258848d2012-08-10 17:06:33 -070079import android.content.pm.UserInfo;
Adam Powelldd8fab22012-03-22 17:47:27 -070080import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070081import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.content.pm.ProviderInfo;
83import android.content.pm.ResolveInfo;
84import android.content.pm.ServiceInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -040085import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.content.res.Configuration;
87import android.graphics.Bitmap;
Robert Greenwalt434203a2010-10-11 16:00:27 -070088import android.net.Proxy;
89import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.net.Uri;
91import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080092import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080093import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070094import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080095import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080097import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.os.FileUtils;
99import android.os.Handler;
100import android.os.IBinder;
101import android.os.IPermissionController;
102import android.os.Looper;
103import android.os.Message;
104import android.os.Parcel;
105import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700107import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.os.RemoteException;
rpcraigec7ed14c2012-07-25 13:10:37 -0400109import android.os.SELinux;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.os.ServiceManager;
Brad Fitzpatrick46d42382010-06-11 13:57:58 -0700111import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.os.SystemClock;
113import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700114import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -0700115import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.provider.Settings;
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700117import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118import android.util.EventLog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800119import android.util.Log;
Adam Powelldd8fab22012-03-22 17:47:27 -0700120import android.util.Pair;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121import android.util.PrintWriterPrinter;
Adam Powelldd8fab22012-03-22 17:47:27 -0700122import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import android.util.SparseArray;
Amith Yamasani742a6712011-05-04 14:49:28 -0700124import android.util.SparseIntArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700125import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import android.view.Gravity;
127import android.view.LayoutInflater;
128import android.view.View;
129import android.view.WindowManager;
130import android.view.WindowManagerPolicy;
131
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700132import java.io.BufferedInputStream;
133import java.io.BufferedOutputStream;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700134import java.io.BufferedReader;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700135import java.io.DataInputStream;
136import java.io.DataOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.io.File;
138import java.io.FileDescriptor;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700139import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import java.io.FileNotFoundException;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700141import java.io.FileOutputStream;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200142import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800143import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144import java.io.PrintWriter;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700145import java.io.StringWriter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146import java.lang.ref.WeakReference;
147import java.util.ArrayList;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700148import java.util.Collections;
149import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150import java.util.HashMap;
151import java.util.HashSet;
152import java.util.Iterator;
153import java.util.List;
154import java.util.Locale;
155import java.util.Map;
Amith Yamasani13593602012-03-22 16:16:17 -0700156import java.util.Map.Entry;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700157import java.util.Set;
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700158import java.util.concurrent.atomic.AtomicBoolean;
159import java.util.concurrent.atomic.AtomicLong;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700161public final class ActivityManagerService extends ActivityManagerNative
162 implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
Amith Yamasani742a6712011-05-04 14:49:28 -0700163 private static final String USER_DATA_DIR = "/data/user/";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 static final String TAG = "ActivityManager";
Amith Yamasani742a6712011-05-04 14:49:28 -0700165 static final String TAG_MU = "ActivityManagerServiceMU";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 static final boolean DEBUG = false;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400167 static final boolean localLOGV = DEBUG;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 static final boolean DEBUG_SWITCH = localLOGV || false;
169 static final boolean DEBUG_TASKS = localLOGV || false;
170 static final boolean DEBUG_PAUSE = localLOGV || false;
171 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
172 static final boolean DEBUG_TRANSITION = localLOGV || false;
173 static final boolean DEBUG_BROADCAST = localLOGV || false;
Christopher Tatef46723b2012-01-26 14:19:24 -0800174 static final boolean DEBUG_BACKGROUND_BROADCAST = DEBUG_BROADCAST || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700175 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 static final boolean DEBUG_SERVICE = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700177 static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 static final boolean DEBUG_VISBILITY = localLOGV || false;
179 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700180 static final boolean DEBUG_PROCESS_OBSERVERS = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700181 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800182 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700184 static final boolean DEBUG_RESULTS = localLOGV || false;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -0700185 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700186 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700187 static final boolean DEBUG_POWER = localLOGV || false;
188 static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
Amith Yamasani742a6712011-05-04 14:49:28 -0700189 static final boolean DEBUG_MU = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 static final boolean VALIDATE_TOKENS = false;
191 static final boolean SHOW_ACTIVITY_START_TIME = true;
192
193 // Control over CPU and battery monitoring.
194 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
195 static final boolean MONITOR_CPU_USAGE = true;
196 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
197 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
198 static final boolean MONITOR_THREAD_CPU_USAGE = false;
199
Dianne Hackborn1655be42009-05-08 14:29:01 -0700200 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700201 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700202
Dianne Hackbornf02e57b2011-03-01 22:21:04 -0800203 private static final String SYSTEM_DEBUGGABLE = "ro.debuggable";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -0700205 static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 // Maximum number of recent tasks that we can remember.
208 static final int MAX_RECENT_TASKS = 20;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700209
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700210 // Amount of time after a call to stopAppSwitches() during which we will
211 // prevent further untrusted switches from happening.
212 static final long APP_SWITCH_DELAY_TIME = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
214 // How long we wait for a launched process to attach to the activity manager
215 // before we decide it's never going to come up for real.
216 static final int PROC_START_TIMEOUT = 10*1000;
217
Jeff Brown3f9dd282011-07-08 20:02:19 -0700218 // How long we wait for a launched process to attach to the activity manager
219 // before we decide it's never going to come up for real, when the process was
220 // started with a wrapper for instrumentation (such as Valgrind) because it
221 // could take much longer than usual.
222 static final int PROC_START_TIMEOUT_WITH_WRAPPER = 300*1000;
223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 // How long to wait after going idle before forcing apps to GC.
225 static final int GC_TIMEOUT = 5*1000;
226
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700227 // The minimum amount of time between successive GC requests for a process.
228 static final int GC_MIN_INTERVAL = 60*1000;
229
Dianne Hackborn287952c2010-09-22 22:34:31 -0700230 // The rate at which we check for apps using excessive power -- 15 mins.
231 static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
232
233 // The minimum sample duration we will allow before deciding we have
234 // enough data on wake locks to start killing things.
235 static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
236
237 // The minimum sample duration we will allow before deciding we have
238 // enough data on CPU usage to start killing things.
239 static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 // How long we allow a receiver to run before giving up on it.
Christopher Tatef46723b2012-01-26 14:19:24 -0800242 static final int BROADCAST_FG_TIMEOUT = 10*1000;
243 static final int BROADCAST_BG_TIMEOUT = 60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 // How long we wait until we timeout on key dispatching.
246 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 // How long we wait until we timeout on key dispatching during instrumentation.
249 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
250
Dan Egnor42471dd2010-01-07 17:25:22 -0800251 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 static final String[] EMPTY_STRING_ARRAY = new String[0];
254
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700255 public ActivityStack mMainStack;
Mike Lockwood3a74bd32011-08-12 13:55:22 -0700256
257 private final boolean mHeadless;
258
Joe Onorato54a4a412011-11-02 20:50:08 -0700259 // Whether we should show our dialogs (ANR, crash, etc) or just perform their
260 // default actuion automatically. Important for devices without direct input
261 // devices.
262 private boolean mShowDialogs = true;
263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700265 * Description of a request to start a new activity, which has been held
266 * due to app switches being disabled.
267 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700268 static class PendingActivityLaunch {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700269 ActivityRecord r;
270 ActivityRecord sourceRecord;
Dianne Hackborna4972e92012-03-14 10:38:05 -0700271 int startFlags;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700272 }
273
274 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
275 = new ArrayList<PendingActivityLaunch>();
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800278 BroadcastQueue mFgBroadcastQueue;
279 BroadcastQueue mBgBroadcastQueue;
Christopher Tatef46723b2012-01-26 14:19:24 -0800280 // Convenient for easy iteration over the queues. Foreground is first
281 // so that dispatch of foreground broadcasts gets precedence.
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800282 final BroadcastQueue[] mBroadcastQueues = new BroadcastQueue[2];
Christopher Tatef46723b2012-01-26 14:19:24 -0800283
284 BroadcastQueue broadcastQueueForIntent(Intent intent) {
285 final boolean isFg = (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0;
286 if (DEBUG_BACKGROUND_BROADCAST) {
287 Slog.i(TAG, "Broadcast intent " + intent + " on "
288 + (isFg ? "foreground" : "background")
289 + " queue");
290 }
291 return (isFg) ? mFgBroadcastQueue : mBgBroadcastQueue;
292 }
293
294 BroadcastRecord broadcastRecordForReceiverLocked(IBinder receiver) {
295 for (BroadcastQueue queue : mBroadcastQueues) {
296 BroadcastRecord r = queue.getMatchingOrderedReceiver(receiver);
297 if (r != null) {
298 return r;
299 }
300 }
301 return null;
302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 * Activity we have told the window manager to have key focus.
306 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700307 ActivityRecord mFocusedActivity = null;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * List of intents that were used to start the most recent tasks.
310 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700311 final ArrayList<TaskRecord> mRecentTasks = new ArrayList<TaskRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
313 /**
Dianne Hackborn7d608422011-08-07 16:24:18 -0700314 * Process management.
315 */
316 final ProcessList mProcessList = new ProcessList();
317
318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * All of the applications we currently have running organized by name.
320 * The keys are strings of the application package name (as
321 * returned by the package manager), and the keys are ApplicationRecord
322 * objects.
323 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700324 final ProcessMap<ProcessRecord> mProcessNames = new ProcessMap<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
326 /**
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800327 * The currently running isolated processes.
328 */
329 final SparseArray<ProcessRecord> mIsolatedProcesses = new SparseArray<ProcessRecord>();
330
331 /**
332 * Counter for assigning isolated process uids, to avoid frequently reusing the
333 * same ones.
334 */
335 int mNextIsolatedProcessUid = 0;
336
337 /**
Dianne Hackborn860755f2010-06-03 18:47:52 -0700338 * The currently running heavy-weight process, if any.
339 */
340 ProcessRecord mHeavyWeightProcess = null;
341
342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 * The last time that various processes have crashed.
344 */
345 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
346
347 /**
348 * Set of applications that we consider to be bad, and will reject
349 * incoming broadcasts from (which the user has no control over).
350 * Processes are added to this set when they have crashed twice within
351 * a minimum amount of time; they are removed from it when they are
352 * later restarted (hopefully due to some user action). The value is the
353 * time it was added to the list.
354 */
355 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
356
357 /**
358 * All of the processes we currently have running organized by pid.
359 * The keys are the pid running the application.
360 *
361 * <p>NOTE: This object is protected by its own lock, NOT the global
362 * activity manager lock!
363 */
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700364 final SparseArray<ProcessRecord> mPidsSelfLocked = new SparseArray<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
366 /**
367 * All of the processes that have been forced to be foreground. The key
368 * is the pid of the caller who requested it (we hold a death
369 * link on it).
370 */
371 abstract class ForegroundToken implements IBinder.DeathRecipient {
372 int pid;
373 IBinder token;
374 }
375 final SparseArray<ForegroundToken> mForegroundProcesses
376 = new SparseArray<ForegroundToken>();
377
378 /**
379 * List of records for processes that someone had tried to start before the
380 * system was ready. We don't start them at that point, but ensure they
381 * are started by the time booting is complete.
382 */
383 final ArrayList<ProcessRecord> mProcessesOnHold
384 = new ArrayList<ProcessRecord>();
385
386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * List of persistent applications that are in the process
388 * of being started.
389 */
390 final ArrayList<ProcessRecord> mPersistentStartingProcesses
391 = new ArrayList<ProcessRecord>();
392
393 /**
394 * Processes that are being forcibly torn down.
395 */
396 final ArrayList<ProcessRecord> mRemovedProcesses
397 = new ArrayList<ProcessRecord>();
398
399 /**
400 * List of running applications, sorted by recent usage.
401 * The first entry in the list is the least recently used.
402 * It contains ApplicationRecord objects. This list does NOT include
403 * any persistent application records (since we never want to exit them).
404 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800405 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 = new ArrayList<ProcessRecord>();
407
408 /**
409 * List of processes that should gc as soon as things are idle.
410 */
411 final ArrayList<ProcessRecord> mProcessesToGc
412 = new ArrayList<ProcessRecord>();
413
414 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800415 * This is the process holding what we currently consider to be
416 * the "home" activity.
417 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700418 ProcessRecord mHomeProcess;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800419
420 /**
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700421 * This is the process holding the activity the user last visited that
422 * is in a different process from the one they are currently in.
423 */
424 ProcessRecord mPreviousProcess;
Dianne Hackborn50685602011-12-01 12:23:37 -0800425
426 /**
427 * The time at which the previous process was last visible.
428 */
429 long mPreviousProcessVisibleTime;
430
Dianne Hackbornf35fe232011-11-01 19:25:20 -0700431 /**
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700432 * Which uses have been started, so are allowed to run code.
433 */
434 final SparseArray<UserStartedState> mStartedUsers = new SparseArray<UserStartedState>();
435
436 /**
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400437 * Packages that the user has asked to have run in screen size
438 * compatibility mode instead of filling the screen.
439 */
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -0700440 final CompatModePackages mCompatModePackages;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400441
442 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 * Set of PendingResultRecord objects that are currently active.
444 */
445 final HashSet mPendingResultRecords = new HashSet();
446
447 /**
448 * Set of IntentSenderRecord objects that are currently active.
449 */
450 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
451 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
452
453 /**
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -0800454 * Fingerprints (hashCode()) of stack traces that we've
Brad Fitzpatrick143666f2010-06-14 12:40:21 -0700455 * already logged DropBox entries for. Guarded by itself. If
456 * something (rogue user app) forces this over
457 * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
458 */
459 private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
460 private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
461
462 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -0700463 * Strict Mode background batched logging state.
464 *
465 * The string buffer is guarded by itself, and its lock is also
466 * used to determine if another batched write is already
467 * in-flight.
468 */
469 private final StringBuilder mStrictModeBuffer = new StringBuilder();
470
471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 * Keeps track of all IIntentReceivers that have been registered for
473 * broadcasts. Hash keys are the receiver IBinder, hash value is
474 * a ReceiverList.
475 */
476 final HashMap mRegisteredReceivers = new HashMap();
477
478 /**
479 * Resolver for broadcast intents to registered receivers.
480 * Holds BroadcastFilter (subclass of IntentFilter).
481 */
482 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
483 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
484 @Override
485 protected boolean allowFilterResult(
486 BroadcastFilter filter, List<BroadcastFilter> dest) {
487 IBinder target = filter.receiverList.receiver.asBinder();
488 for (int i=dest.size()-1; i>=0; i--) {
489 if (dest.get(i).receiverList.receiver.asBinder() == target) {
490 return false;
491 }
492 }
493 return true;
494 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700495
496 @Override
Dianne Hackborn9ec6cdd2012-05-31 10:57:54 -0700497 protected BroadcastFilter[] newArray(int size) {
498 return new BroadcastFilter[size];
499 }
500
501 @Override
Dianne Hackborn6c418d52011-06-29 14:05:33 -0700502 protected String packageForFilter(BroadcastFilter filter) {
503 return filter.packageName;
504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 };
506
507 /**
508 * State of all active sticky broadcasts. Keys are the action of the
509 * sticky Intent, values are an ArrayList of all broadcasted intents with
510 * that action (which should usually be one).
511 */
512 final HashMap<String, ArrayList<Intent>> mStickyBroadcasts =
513 new HashMap<String, ArrayList<Intent>>();
514
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700515 final ActiveServices mServices;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
517 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700518 * Backup/restore process management
519 */
520 String mBackupAppName = null;
521 BackupRecord mBackupTarget = null;
522
523 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * List of PendingThumbnailsRecord objects of clients who are still
525 * waiting to receive all of the thumbnails for a task.
526 */
527 final ArrayList mPendingThumbnails = new ArrayList();
528
529 /**
530 * List of HistoryRecord objects that have been finished and must
531 * still report back to a pending thumbnail receiver.
532 */
533 final ArrayList mCancelledThumbnails = new ArrayList();
534
Amith Yamasani742a6712011-05-04 14:49:28 -0700535 final ProviderMap mProviderMap = new ProviderMap();
536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 /**
538 * List of content providers who have clients waiting for them. The
539 * application is currently being launched and the provider will be
540 * removed from this list once it is published.
541 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700542 final ArrayList<ContentProviderRecord> mLaunchingProviders
543 = new ArrayList<ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
545 /**
546 * Global set of specific Uri permissions that have been granted.
547 */
548 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
549 = new SparseArray<HashMap<Uri, UriPermission>>();
550
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800551 CoreSettingsObserver mCoreSettingsObserver;
552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 /**
554 * Thread-local storage used to carry caller permissions over through
555 * indirect content-provider access.
556 * @see #ActivityManagerService.openContentUri()
557 */
558 private class Identity {
559 public int pid;
560 public int uid;
561
562 Identity(int _pid, int _uid) {
563 pid = _pid;
564 uid = _uid;
565 }
566 }
Amith Yamasani742a6712011-05-04 14:49:28 -0700567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
569
570 /**
571 * All information we have collected about the runtime performance of
572 * any user id that can impact battery performance.
573 */
574 final BatteryStatsService mBatteryStatsService;
575
576 /**
577 * information about component usage
578 */
579 final UsageStatsService mUsageStatsService;
580
581 /**
582 * Current configuration information. HistoryRecord objects are given
583 * a reference to this object to indicate which configuration they are
584 * currently running in, so this object must be kept immutable.
585 */
586 Configuration mConfiguration = new Configuration();
587
588 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800589 * Current sequencing integer of the configuration, for skipping old
590 * configurations.
591 */
592 int mConfigurationSeq = 0;
593
594 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700595 * Hardware-reported OpenGLES version.
596 */
597 final int GL_ES_VERSION;
598
599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * List of initialization arguments to pass to all processes when binding applications to them.
601 * For example, references to the commonly used services.
602 */
603 HashMap<String, IBinder> mAppBindArgs;
604
605 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700606 * Temporary to avoid allocations. Protected by main lock.
607 */
608 final StringBuilder mStringBuilder = new StringBuilder(256);
609
610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * Used to control how we initialize the service.
612 */
613 boolean mStartRunning = false;
614 ComponentName mTopComponent;
615 String mTopAction;
616 String mTopData;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700617 boolean mProcessesReady = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 boolean mSystemReady = false;
619 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700620 boolean mWaitingUpdate = false;
621 boolean mDidUpdate = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700622 boolean mOnBattery = false;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700623 boolean mLaunchWarningShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624
625 Context mContext;
626
627 int mFactoryTest;
628
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700629 boolean mCheckedForSetup;
630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700632 * The time at which we will allow normal application switches again,
633 * after a call to {@link #stopAppSwitches()}.
634 */
635 long mAppSwitchesAllowedTime;
636
637 /**
638 * This is set to true after the first switch after mAppSwitchesAllowedTime
639 * is set; any switches after that will clear the time.
640 */
641 boolean mDidAppSwitch;
642
643 /**
Dianne Hackborn287952c2010-09-22 22:34:31 -0700644 * Last time (in realtime) at which we checked for power usage.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700645 */
Dianne Hackborn287952c2010-09-22 22:34:31 -0700646 long mLastPowerCheckRealtime;
647
648 /**
649 * Last time (in uptime) at which we checked for power usage.
650 */
651 long mLastPowerCheckUptime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700652
653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 * Set while we are wanting to sleep, to prevent any
655 * activities from being started/resumed.
656 */
657 boolean mSleeping = false;
658
659 /**
Dianne Hackbornff5b1582012-04-12 17:24:07 -0700660 * State of external calls telling us if the device is asleep.
661 */
662 boolean mWentToSleep = false;
663
664 /**
665 * State of external call telling us if the lock screen is shown.
666 */
667 boolean mLockScreenShown = false;
668
669 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700670 * Set if we are shutting down the system, similar to sleeping.
671 */
672 boolean mShuttingDown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673
674 /**
675 * Task identifier that activities are currently being started
676 * in. Incremented each time a new task is created.
677 * todo: Replace this with a TokenSpace class that generates non-repeating
678 * integers that won't wrap.
679 */
680 int mCurTask = 1;
681
682 /**
683 * Current sequence id for oom_adj computation traversal.
684 */
685 int mAdjSeq = 0;
686
687 /**
Dianne Hackborn906497c2010-05-10 15:57:38 -0700688 * Current sequence id for process LRU updating.
689 */
690 int mLruSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
692 /**
Dianne Hackbornee7621c2012-08-13 16:42:18 -0700693 * Keep track of the non-hidden/empty process we last found, to help
694 * determine how to distribute hidden/empty processes next time.
695 */
696 int mNumNonHiddenProcs = 0;
697
698 /**
699 * Keep track of the number of hidden procs, to balance oom adj
700 * distribution between those and empty procs.
701 */
702 int mNumHiddenProcs = 0;
703
704 /**
Dianne Hackborne02c88a2011-10-28 13:58:15 -0700705 * Keep track of the number of service processes we last found, to
706 * determine on the next iteration which should be B services.
707 */
708 int mNumServiceProcs = 0;
709 int mNewNumServiceProcs = 0;
710
711 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * System monitoring: number of processes that died since the last
713 * N procs were started.
714 */
715 int[] mProcDeaths = new int[20];
716
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700717 /**
718 * This is set if we had to do a delayed dexopt of an app before launching
719 * it, to increasing the ANR timeouts in that case.
720 */
721 boolean mDidDexOpt;
722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 String mDebugApp = null;
724 boolean mWaitForDebugger = false;
725 boolean mDebugTransient = false;
726 String mOrigDebugApp = null;
727 boolean mOrigWaitForDebugger = false;
728 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700729 IActivityController mController = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700730 String mProfileApp = null;
731 ProcessRecord mProfileProc = null;
732 String mProfileFile;
733 ParcelFileDescriptor mProfileFd;
734 int mProfileType = 0;
735 boolean mAutoStopProfiler = false;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800736 String mOpenGlTraceApp = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700738 static class ProcessChangeItem {
739 static final int CHANGE_ACTIVITIES = 1<<0;
740 static final int CHANGE_IMPORTANCE= 1<<1;
741 int changes;
742 int uid;
743 int pid;
744 int importance;
745 boolean foregroundActivities;
746 }
747
Jeff Sharkeya4620792011-05-20 15:29:23 -0700748 final RemoteCallbackList<IProcessObserver> mProcessObservers
749 = new RemoteCallbackList<IProcessObserver>();
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700750 ProcessChangeItem[] mActiveProcessChanges = new ProcessChangeItem[5];
751
752 final ArrayList<ProcessChangeItem> mPendingProcessChanges
753 = new ArrayList<ProcessChangeItem>();
754 final ArrayList<ProcessChangeItem> mAvailProcessChanges
755 = new ArrayList<ProcessChangeItem>();
Jeff Sharkeya4620792011-05-20 15:29:23 -0700756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 /**
758 * Callback of last caller to {@link #requestPss}.
759 */
760 Runnable mRequestPssCallback;
761
762 /**
763 * Remaining processes for which we are waiting results from the last
764 * call to {@link #requestPss}.
765 */
766 final ArrayList<ProcessRecord> mRequestPssList
767 = new ArrayList<ProcessRecord>();
768
769 /**
770 * Runtime statistics collection thread. This object's lock is used to
771 * protect all related state.
772 */
773 final Thread mProcessStatsThread;
774
775 /**
776 * Used to collect process stats when showing not responding dialog.
777 * Protected by mProcessStatsThread.
778 */
779 final ProcessStats mProcessStats = new ProcessStats(
780 MONITOR_THREAD_CPU_USAGE);
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700781 final AtomicLong mLastCpuTime = new AtomicLong(0);
782 final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 long mLastWriteTime = 0;
785
786 /**
787 * Set to true after the system has finished booting.
788 */
789 boolean mBooted = false;
790
Dianne Hackborn7d608422011-08-07 16:24:18 -0700791 int mProcessLimit = ProcessList.MAX_HIDDEN_APPS;
Dianne Hackbornce86ba82011-07-13 19:33:41 -0700792 int mProcessLimitOverride = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793
794 WindowManagerService mWindowManager;
795
796 static ActivityManagerService mSelf;
797 static ActivityThread mSystemThread;
798
Amith Yamasani258848d2012-08-10 17:06:33 -0700799 private int mCurrentUserId;
Amith Yamasani258848d2012-08-10 17:06:33 -0700800 private UserManager mUserManager;
801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 private final class AppDeathRecipient implements IBinder.DeathRecipient {
803 final ProcessRecord mApp;
804 final int mPid;
805 final IApplicationThread mAppThread;
806
807 AppDeathRecipient(ProcessRecord app, int pid,
808 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800809 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 TAG, "New death recipient " + this
811 + " for thread " + thread.asBinder());
812 mApp = app;
813 mPid = pid;
814 mAppThread = thread;
815 }
816
817 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800818 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 TAG, "Death received in " + this
820 + " for thread " + mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 synchronized(ActivityManagerService.this) {
822 appDiedLocked(mApp, mPid, mAppThread);
823 }
824 }
825 }
826
827 static final int SHOW_ERROR_MSG = 1;
828 static final int SHOW_NOT_RESPONDING_MSG = 2;
829 static final int SHOW_FACTORY_ERROR_MSG = 3;
830 static final int UPDATE_CONFIGURATION_MSG = 4;
831 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
832 static final int WAIT_FOR_DEBUGGER_MSG = 6;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 static final int SERVICE_TIMEOUT_MSG = 12;
834 static final int UPDATE_TIME_ZONE = 13;
835 static final int SHOW_UID_ERROR_MSG = 14;
836 static final int IM_FEELING_LUCKY_MSG = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700838 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700839 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800840 static final int FINALIZE_PENDING_INTENT_MSG = 23;
Dianne Hackborn860755f2010-06-03 18:47:52 -0700841 static final int POST_HEAVY_NOTIFICATION_MSG = 24;
842 static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700843 static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700844 static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700845 static final int CLEAR_DNS_CACHE = 28;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700846 static final int UPDATE_HTTP_PROXY = 29;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700847 static final int SHOW_COMPAT_MODE_DIALOG_MSG = 30;
Dianne Hackborna93c2c12012-05-31 15:29:36 -0700848 static final int DISPATCH_PROCESSES_CHANGED = 31;
Dianne Hackborn36f80f32011-05-31 18:26:45 -0700849 static final int DISPATCH_PROCESS_DIED = 32;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700850 static final int REPORT_MEM_USAGE = 33;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851
Dianne Hackborn40c8db52012-02-10 18:59:48 -0800852 static final int FIRST_ACTIVITY_STACK_MSG = 100;
853 static final int FIRST_BROADCAST_QUEUE_MSG = 200;
854 static final int FIRST_COMPAT_MODE_MSG = 300;
855
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 AlertDialog mUidAlert;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -0700857 CompatModeDialog mCompatModeDialog;
Dianne Hackborn04d6db32011-11-04 20:07:24 -0700858 long mLastMemUsageReportTime = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859
860 final Handler mHandler = new Handler() {
861 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800862 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 //}
864
865 public void handleMessage(Message msg) {
866 switch (msg.what) {
867 case SHOW_ERROR_MSG: {
868 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 synchronized (ActivityManagerService.this) {
870 ProcessRecord proc = (ProcessRecord)data.get("app");
871 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800872 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 return;
874 }
875 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700876 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -0800877 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 d.show();
879 proc.crashDialog = d;
880 } else {
881 // The device is asleep, so just pretend that the user
882 // saw a crash dialog and hit "force quit".
883 res.set(0);
884 }
885 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700886
887 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 } break;
889 case SHOW_NOT_RESPONDING_MSG: {
890 synchronized (ActivityManagerService.this) {
891 HashMap data = (HashMap) msg.obj;
892 ProcessRecord proc = (ProcessRecord)data.get("app");
893 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 return;
896 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800897
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700898 Intent intent = new Intent("android.intent.action.ANR");
899 if (!mProcessesReady) {
Christopher Tatef46723b2012-01-26 14:19:24 -0800900 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
901 | Intent.FLAG_RECEIVER_FOREGROUND);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700902 }
903 broadcastIntentLocked(null, null, intent,
The Android Open Source Project4df24232009-03-05 14:34:35 -0800904 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -0700905 false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
The Android Open Source Project4df24232009-03-05 14:34:35 -0800906
Justin Kohbc52ca22012-03-29 15:11:44 -0700907 if (mShowDialogs) {
908 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
Mike Lockwood69ccdbd2012-04-03 11:53:47 -0700909 mContext, proc, (ActivityRecord)data.get("activity"));
Justin Kohbc52ca22012-03-29 15:11:44 -0700910 d.show();
911 proc.anrDialog = d;
912 } else {
913 // Just kill the app if there is no dialog to be shown.
914 killAppAtUsersRequest(proc, null);
915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700917
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700918 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 } break;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700920 case SHOW_STRICT_MODE_VIOLATION_MSG: {
921 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
922 synchronized (ActivityManagerService.this) {
923 ProcessRecord proc = (ProcessRecord) data.get("app");
924 if (proc == null) {
925 Slog.e(TAG, "App not found when showing strict mode dialog.");
926 break;
927 }
928 if (proc.crashDialog != null) {
929 Slog.e(TAG, "App already has strict mode dialog: " + proc);
930 return;
931 }
932 AppErrorResult res = (AppErrorResult) data.get("result");
Joe Onorato54a4a412011-11-02 20:50:08 -0700933 if (mShowDialogs && !mSleeping && !mShuttingDown) {
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700934 Dialog d = new StrictModeViolationDialog(mContext, res, proc);
935 d.show();
936 proc.crashDialog = d;
937 } else {
938 // The device is asleep, so just pretend that the user
939 // saw a crash dialog and hit "force quit".
940 res.set(0);
941 }
942 }
943 ensureBootCompleted();
944 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 case SHOW_FACTORY_ERROR_MSG: {
946 Dialog d = new FactoryErrorDialog(
947 mContext, msg.getData().getCharSequence("msg"));
948 d.show();
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700949 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 } break;
951 case UPDATE_CONFIGURATION_MSG: {
952 final ContentResolver resolver = mContext.getContentResolver();
953 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
954 } break;
955 case GC_BACKGROUND_PROCESSES_MSG: {
956 synchronized (ActivityManagerService.this) {
957 performAppGcsIfAppropriateLocked();
958 }
959 } break;
960 case WAIT_FOR_DEBUGGER_MSG: {
961 synchronized (ActivityManagerService.this) {
962 ProcessRecord app = (ProcessRecord)msg.obj;
963 if (msg.arg1 != 0) {
964 if (!app.waitedForDebugger) {
965 Dialog d = new AppWaitingForDebuggerDialog(
966 ActivityManagerService.this,
967 mContext, app);
968 app.waitDialog = d;
969 app.waitedForDebugger = true;
970 d.show();
971 }
972 } else {
973 if (app.waitDialog != null) {
974 app.waitDialog.dismiss();
975 app.waitDialog = null;
976 }
977 }
978 }
979 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700981 if (mDidDexOpt) {
982 mDidDexOpt = false;
983 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
984 nmsg.obj = msg.obj;
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700985 mHandler.sendMessageDelayed(nmsg, ActiveServices.SERVICE_TIMEOUT);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700986 return;
987 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -0700988 mServices.serviceTimeout((ProcessRecord)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 } break;
990 case UPDATE_TIME_ZONE: {
991 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800992 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
993 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (r.thread != null) {
995 try {
996 r.thread.updateTimeZone();
997 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800998 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 }
1000 }
1001 }
1002 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001003 } break;
Robert Greenwalt03595d02010-11-02 14:08:23 -07001004 case CLEAR_DNS_CACHE: {
1005 synchronized (ActivityManagerService.this) {
1006 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1007 ProcessRecord r = mLruProcesses.get(i);
1008 if (r.thread != null) {
1009 try {
1010 r.thread.clearDnsCache();
1011 } catch (RemoteException ex) {
1012 Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
1013 }
1014 }
1015 }
1016 }
1017 } break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001018 case UPDATE_HTTP_PROXY: {
1019 ProxyProperties proxy = (ProxyProperties)msg.obj;
1020 String host = "";
1021 String port = "";
1022 String exclList = "";
1023 if (proxy != null) {
1024 host = proxy.getHost();
1025 port = Integer.toString(proxy.getPort());
1026 exclList = proxy.getExclusionList();
1027 }
1028 synchronized (ActivityManagerService.this) {
1029 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1030 ProcessRecord r = mLruProcesses.get(i);
1031 if (r.thread != null) {
1032 try {
1033 r.thread.setHttpProxy(host, port, exclList);
1034 } catch (RemoteException ex) {
1035 Slog.w(TAG, "Failed to update http proxy for: " +
1036 r.info.processName);
1037 }
1038 }
1039 }
1040 }
1041 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 case SHOW_UID_ERROR_MSG: {
Joe Onorato54a4a412011-11-02 20:50:08 -07001043 String title = "System UIDs Inconsistent";
1044 String text = "UIDs on the system are inconsistent, you need to wipe your"
1045 + " data partition or your device will be unstable.";
1046 Log.e(TAG, title + ": " + text);
1047 if (mShowDialogs) {
1048 // XXX This is a temporary dialog, no need to localize.
1049 AlertDialog d = new BaseErrorDialog(mContext);
1050 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1051 d.setCancelable(false);
1052 d.setTitle(title);
1053 d.setMessage(text);
1054 d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky",
1055 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1056 mUidAlert = d;
1057 d.show();
1058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 } break;
1060 case IM_FEELING_LUCKY_MSG: {
1061 if (mUidAlert != null) {
1062 mUidAlert.dismiss();
1063 mUidAlert = null;
1064 }
1065 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001067 if (mDidDexOpt) {
1068 mDidDexOpt = false;
1069 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1070 nmsg.obj = msg.obj;
1071 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1072 return;
1073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 ProcessRecord app = (ProcessRecord)msg.obj;
1075 synchronized (ActivityManagerService.this) {
1076 processStartTimedOutLocked(app);
1077 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001078 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001079 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1080 synchronized (ActivityManagerService.this) {
1081 doPendingActivityLaunchesLocked(true);
1082 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001083 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001084 case KILL_APPLICATION_MSG: {
1085 synchronized (ActivityManagerService.this) {
1086 int uid = msg.arg1;
1087 boolean restart = (msg.arg2 == 1);
1088 String pkg = (String) msg.obj;
Amith Yamasani483f3b02012-03-13 16:08:00 -07001089 forceStopPackageLocked(pkg, uid, restart, false, true, false,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001090 UserHandle.getUserId(uid));
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001091 }
1092 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001093 case FINALIZE_PENDING_INTENT_MSG: {
1094 ((PendingIntentRecord)msg.obj).completeFinalize();
1095 } break;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001096 case POST_HEAVY_NOTIFICATION_MSG: {
1097 INotificationManager inm = NotificationManager.getService();
1098 if (inm == null) {
1099 return;
1100 }
1101
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001102 ActivityRecord root = (ActivityRecord)msg.obj;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001103 ProcessRecord process = root.app;
1104 if (process == null) {
1105 return;
1106 }
1107
1108 try {
1109 Context context = mContext.createPackageContext(process.info.packageName, 0);
1110 String text = mContext.getString(R.string.heavy_weight_notification,
1111 context.getApplicationInfo().loadLabel(context.getPackageManager()));
1112 Notification notification = new Notification();
1113 notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1114 notification.when = 0;
1115 notification.flags = Notification.FLAG_ONGOING_EVENT;
1116 notification.tickerText = text;
1117 notification.defaults = 0; // please be quiet
1118 notification.sound = null;
1119 notification.vibrate = null;
1120 notification.setLatestEventInfo(context, text,
1121 mContext.getText(R.string.heavy_weight_notification_detail),
1122 PendingIntent.getActivity(mContext, 0, root.intent,
1123 PendingIntent.FLAG_CANCEL_CURRENT));
1124
1125 try {
1126 int[] outId = new int[1];
1127 inm.enqueueNotification("android", R.string.heavy_weight_notification,
1128 notification, outId);
1129 } catch (RuntimeException e) {
1130 Slog.w(ActivityManagerService.TAG,
1131 "Error showing notification for heavy-weight app", e);
1132 } catch (RemoteException e) {
1133 }
1134 } catch (NameNotFoundException e) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07001135 Slog.w(TAG, "Unable to create context for heavy notification", e);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001136 }
1137 } break;
1138 case CANCEL_HEAVY_NOTIFICATION_MSG: {
1139 INotificationManager inm = NotificationManager.getService();
1140 if (inm == null) {
1141 return;
1142 }
1143 try {
1144 inm.cancelNotification("android",
1145 R.string.heavy_weight_notification);
1146 } catch (RuntimeException e) {
1147 Slog.w(ActivityManagerService.TAG,
1148 "Error canceling notification for service", e);
1149 } catch (RemoteException e) {
1150 }
1151 } break;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001152 case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1153 synchronized (ActivityManagerService.this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001154 checkExcessivePowerUsageLocked(true);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001155 removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001156 Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1157 sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001158 }
1159 } break;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001160 case SHOW_COMPAT_MODE_DIALOG_MSG: {
1161 synchronized (ActivityManagerService.this) {
1162 ActivityRecord ar = (ActivityRecord)msg.obj;
1163 if (mCompatModeDialog != null) {
1164 if (mCompatModeDialog.mAppInfo.packageName.equals(
1165 ar.info.applicationInfo.packageName)) {
1166 return;
1167 }
1168 mCompatModeDialog.dismiss();
1169 mCompatModeDialog = null;
1170 }
Dianne Hackborn29478262011-06-07 15:44:22 -07001171 if (ar != null && false) {
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07001172 if (mCompatModePackages.getPackageAskCompatModeLocked(
1173 ar.packageName)) {
1174 int mode = mCompatModePackages.computeCompatModeLocked(
1175 ar.info.applicationInfo);
1176 if (mode == ActivityManager.COMPAT_MODE_DISABLED
1177 || mode == ActivityManager.COMPAT_MODE_ENABLED) {
1178 mCompatModeDialog = new CompatModeDialog(
1179 ActivityManagerService.this, mContext,
1180 ar.info.applicationInfo);
1181 mCompatModeDialog.show();
1182 }
1183 }
1184 }
1185 }
Dianne Hackborn36f80f32011-05-31 18:26:45 -07001186 break;
1187 }
Dianne Hackborna93c2c12012-05-31 15:29:36 -07001188 case DISPATCH_PROCESSES_CHANGED: {
1189 dispatchProcessesChanged();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001190 break;
1191 }
1192 case DISPATCH_PROCESS_DIED: {
Jeff Sharkey287bd832011-05-28 19:36:26 -07001193 final int pid = msg.arg1;
1194 final int uid = msg.arg2;
1195 dispatchProcessDied(pid, uid);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001196 break;
1197 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001198 case REPORT_MEM_USAGE: {
1199 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
1200 if (!isDebuggable) {
1201 return;
1202 }
1203 synchronized (ActivityManagerService.this) {
1204 long now = SystemClock.uptimeMillis();
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001205 if (now < (mLastMemUsageReportTime+5*60*1000)) {
1206 // Don't report more than every 5 minutes to somewhat
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001207 // avoid spamming.
1208 return;
1209 }
1210 mLastMemUsageReportTime = now;
1211 }
1212 Thread thread = new Thread() {
1213 @Override public void run() {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001214 StringBuilder dropBuilder = new StringBuilder(1024);
1215 StringBuilder logBuilder = new StringBuilder(1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -08001216 StringWriter oomSw = new StringWriter();
1217 PrintWriter oomPw = new PrintWriter(oomSw);
1218 StringWriter catSw = new StringWriter();
1219 PrintWriter catPw = new PrintWriter(catSw);
1220 String[] emptyArgs = new String[] { };
1221 StringBuilder tag = new StringBuilder(128);
1222 StringBuilder stack = new StringBuilder(128);
1223 tag.append("Low on memory -- ");
1224 dumpApplicationMemoryUsage(null, oomPw, " ", emptyArgs, true, catPw,
1225 tag, stack);
1226 dropBuilder.append(stack);
1227 dropBuilder.append('\n');
1228 dropBuilder.append('\n');
1229 String oomString = oomSw.toString();
1230 dropBuilder.append(oomString);
1231 dropBuilder.append('\n');
1232 logBuilder.append(oomString);
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001233 try {
1234 java.lang.Process proc = Runtime.getRuntime().exec(new String[] {
1235 "procrank", });
1236 final InputStreamReader converter = new InputStreamReader(
1237 proc.getInputStream());
1238 BufferedReader in = new BufferedReader(converter);
1239 String line;
1240 while (true) {
1241 line = in.readLine();
1242 if (line == null) {
1243 break;
1244 }
1245 if (line.length() > 0) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001246 logBuilder.append(line);
1247 logBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001248 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001249 dropBuilder.append(line);
1250 dropBuilder.append('\n');
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001251 }
1252 converter.close();
1253 } catch (IOException e) {
1254 }
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001255 synchronized (ActivityManagerService.this) {
Dianne Hackborn672342c2011-11-29 11:29:02 -08001256 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001257 dumpProcessesLocked(null, catPw, emptyArgs, 0, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001258 catPw.println();
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001259 mServices.dumpServicesLocked(null, catPw, emptyArgs, 0,
1260 false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001261 catPw.println();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001262 dumpActivitiesLocked(null, catPw, emptyArgs, 0, false, false, null);
Dianne Hackborn7aa6d312011-11-15 15:01:14 -08001263 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001264 dropBuilder.append(catSw.toString());
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001265 addErrorToDropBox("lowmem", null, "system_server", null,
1266 null, tag.toString(), dropBuilder.toString(), null, null);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08001267 Slog.i(TAG, logBuilder.toString());
Dianne Hackborn04d6db32011-11-04 20:07:24 -07001268 synchronized (ActivityManagerService.this) {
1269 long now = SystemClock.uptimeMillis();
1270 if (mLastMemUsageReportTime < now) {
1271 mLastMemUsageReportTime = now;
1272 }
1273 }
1274 }
1275 };
1276 thread.start();
1277 break;
1278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280 }
1281 };
1282
1283 public static void setSystemProcess() {
1284 try {
1285 ActivityManagerService m = mSelf;
1286
Dianne Hackborna573f6a2012-02-09 16:12:18 -08001287 ServiceManager.addService("activity", m, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 ServiceManager.addService("meminfo", new MemBinder(m));
Chet Haase9c1e23b2011-03-24 10:51:31 -07001289 ServiceManager.addService("gfxinfo", new GraphicsBinder(m));
Jeff Brown6754ba22011-12-14 20:20:01 -08001290 ServiceManager.addService("dbinfo", new DbBinder(m));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 if (MONITOR_CPU_USAGE) {
1292 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 ServiceManager.addService("permission", new PermissionController(m));
1295
1296 ApplicationInfo info =
1297 mSelf.mContext.getPackageManager().getApplicationInfo(
Amith Yamasani483f3b02012-03-13 16:08:00 -07001298 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001299 mSystemThread.installSystemApplicationInfo(info);
1300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 synchronized (mSelf) {
1302 ProcessRecord app = mSelf.newProcessRecordLocked(
1303 mSystemThread.getApplicationThread(), info,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001304 info.processName, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001306 app.pid = MY_PID;
Dianne Hackborn7d608422011-08-07 16:24:18 -07001307 app.maxAdj = ProcessList.SYSTEM_ADJ;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001308 mSelf.mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 synchronized (mSelf.mPidsSelfLocked) {
1310 mSelf.mPidsSelfLocked.put(app.pid, app);
1311 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001312 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
1314 } catch (PackageManager.NameNotFoundException e) {
1315 throw new RuntimeException(
1316 "Unable to find android system package", e);
1317 }
1318 }
1319
1320 public void setWindowManager(WindowManagerService wm) {
1321 mWindowManager = wm;
1322 }
1323
1324 public static final Context main(int factoryTest) {
1325 AThread thr = new AThread();
1326 thr.start();
1327
1328 synchronized (thr) {
1329 while (thr.mService == null) {
1330 try {
1331 thr.wait();
1332 } catch (InterruptedException e) {
1333 }
1334 }
1335 }
1336
1337 ActivityManagerService m = thr.mService;
1338 mSelf = m;
1339 ActivityThread at = ActivityThread.systemMain();
1340 mSystemThread = at;
1341 Context context = at.getSystemContext();
Dianne Hackborn247fe742011-01-08 17:25:57 -08001342 context.setTheme(android.R.style.Theme_Holo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 m.mContext = context;
1344 m.mFactoryTest = factoryTest;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001345 m.mMainStack = new ActivityStack(m, context, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346
1347 m.mBatteryStatsService.publish(context);
1348 m.mUsageStatsService.publish(context);
1349
1350 synchronized (thr) {
1351 thr.mReady = true;
1352 thr.notifyAll();
1353 }
1354
1355 m.startRunning(null, null, null, null);
1356
1357 return context;
1358 }
1359
1360 public static ActivityManagerService self() {
1361 return mSelf;
1362 }
1363
1364 static class AThread extends Thread {
1365 ActivityManagerService mService;
1366 boolean mReady = false;
1367
1368 public AThread() {
1369 super("ActivityManager");
1370 }
1371
1372 public void run() {
1373 Looper.prepare();
1374
1375 android.os.Process.setThreadPriority(
1376 android.os.Process.THREAD_PRIORITY_FOREGROUND);
Christopher Tate160edb32010-06-30 17:46:30 -07001377 android.os.Process.setCanSelfBackground(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378
1379 ActivityManagerService m = new ActivityManagerService();
1380
1381 synchronized (this) {
1382 mService = m;
1383 notifyAll();
1384 }
1385
1386 synchronized (this) {
1387 while (!mReady) {
1388 try {
1389 wait();
1390 } catch (InterruptedException e) {
1391 }
1392 }
1393 }
1394
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001395 // For debug builds, log event loop stalls to dropbox for analysis.
1396 if (StrictMode.conditionallyEnableDebugLogging()) {
1397 Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper");
1398 }
1399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 Looper.loop();
1401 }
1402 }
1403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 static class MemBinder extends Binder {
1405 ActivityManagerService mActivityManagerService;
1406 MemBinder(ActivityManagerService activityManagerService) {
1407 mActivityManagerService = activityManagerService;
1408 }
1409
1410 @Override
1411 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001412 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1413 != PackageManager.PERMISSION_GRANTED) {
1414 pw.println("Permission Denial: can't dump meminfo from from pid="
1415 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1416 + " without permission " + android.Manifest.permission.DUMP);
1417 return;
1418 }
1419
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -08001420 mActivityManagerService.dumpApplicationMemoryUsage(fd, pw, " ", args,
Dianne Hackborn672342c2011-11-29 11:29:02 -08001421 false, null, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
1423 }
1424
Chet Haase9c1e23b2011-03-24 10:51:31 -07001425 static class GraphicsBinder extends Binder {
1426 ActivityManagerService mActivityManagerService;
1427 GraphicsBinder(ActivityManagerService activityManagerService) {
1428 mActivityManagerService = activityManagerService;
1429 }
1430
1431 @Override
1432 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001433 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1434 != PackageManager.PERMISSION_GRANTED) {
1435 pw.println("Permission Denial: can't dump gfxinfo from from pid="
1436 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1437 + " without permission " + android.Manifest.permission.DUMP);
1438 return;
1439 }
1440
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001441 mActivityManagerService.dumpGraphicsHardwareUsage(fd, pw, args);
Chet Haase9c1e23b2011-03-24 10:51:31 -07001442 }
1443 }
1444
Jeff Brown6754ba22011-12-14 20:20:01 -08001445 static class DbBinder extends Binder {
1446 ActivityManagerService mActivityManagerService;
1447 DbBinder(ActivityManagerService activityManagerService) {
1448 mActivityManagerService = activityManagerService;
1449 }
1450
1451 @Override
1452 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1453 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1454 != PackageManager.PERMISSION_GRANTED) {
1455 pw.println("Permission Denial: can't dump dbinfo from from pid="
1456 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1457 + " without permission " + android.Manifest.permission.DUMP);
1458 return;
1459 }
1460
1461 mActivityManagerService.dumpDbInfo(fd, pw, args);
1462 }
1463 }
1464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 static class CpuBinder extends Binder {
1466 ActivityManagerService mActivityManagerService;
1467 CpuBinder(ActivityManagerService activityManagerService) {
1468 mActivityManagerService = activityManagerService;
1469 }
1470
1471 @Override
1472 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Kenny Root3abd75b2011-09-29 11:00:41 -07001473 if (mActivityManagerService.checkCallingPermission(android.Manifest.permission.DUMP)
1474 != PackageManager.PERMISSION_GRANTED) {
1475 pw.println("Permission Denial: can't dump cpuinfo from from pid="
1476 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
1477 + " without permission " + android.Manifest.permission.DUMP);
1478 return;
1479 }
1480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 synchronized (mActivityManagerService.mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07001482 pw.print(mActivityManagerService.mProcessStats.printCurrentLoad());
1483 pw.print(mActivityManagerService.mProcessStats.printCurrentState(
1484 SystemClock.uptimeMillis()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 }
1486 }
1487 }
1488
1489 private ActivityManagerService() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001491
Dianne Hackborn40c8db52012-02-10 18:59:48 -08001492 mFgBroadcastQueue = new BroadcastQueue(this, "foreground", BROADCAST_FG_TIMEOUT);
1493 mBgBroadcastQueue = new BroadcastQueue(this, "background", BROADCAST_BG_TIMEOUT);
1494 mBroadcastQueues[0] = mFgBroadcastQueue;
1495 mBroadcastQueues[1] = mBgBroadcastQueue;
1496
Dianne Hackborn599db5c2012-08-03 19:28:48 -07001497 mServices = new ActiveServices(this);
1498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 File dataDir = Environment.getDataDirectory();
1500 File systemDir = new File(dataDir, "system");
1501 systemDir.mkdirs();
1502 mBatteryStatsService = new BatteryStatsService(new File(
1503 systemDir, "batterystats.bin").toString());
1504 mBatteryStatsService.getActiveStatistics().readLocked();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001505 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
Dianne Hackborn287952c2010-09-22 22:34:31 -07001506 mOnBattery = DEBUG_POWER ? true
1507 : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001508 mBatteryStatsService.getActiveStatistics().setCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001510 mUsageStatsService = new UsageStatsService(new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001511 systemDir, "usagestats").toString());
Mike Lockwood3a74bd32011-08-12 13:55:22 -07001512 mHeadless = "1".equals(SystemProperties.get("ro.config.headless", "0"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001514 // User 0 is the first and only user that runs at boot.
1515 mStartedUsers.put(0, new UserStartedState(new UserHandle(0), true));
1516
Jack Palevichb90d28c2009-07-22 15:35:24 -07001517 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1518 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1519
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001520 mConfiguration.setToDefaults();
1521 mConfiguration.locale = Locale.getDefault();
Dianne Hackborn813075a62011-11-14 17:45:19 -08001522 mConfigurationSeq = mConfiguration.seq = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 mProcessStats.init();
1524
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07001525 mCompatModePackages = new CompatModePackages(this, systemDir);
1526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // Add ourself to the Watchdog monitors.
1528 Watchdog.getInstance().addMonitor(this);
1529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 mProcessStatsThread = new Thread("ProcessStats") {
1531 public void run() {
1532 while (true) {
1533 try {
1534 try {
1535 synchronized(this) {
1536 final long now = SystemClock.uptimeMillis();
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001537 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001539 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 // + ", write delay=" + nextWriteDelay);
1541 if (nextWriteDelay < nextCpuDelay) {
1542 nextCpuDelay = nextWriteDelay;
1543 }
1544 if (nextCpuDelay > 0) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001545 mProcessStatsMutexFree.set(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 this.wait(nextCpuDelay);
1547 }
1548 }
1549 } catch (InterruptedException e) {
1550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 updateCpuStatsNow();
1552 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001553 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 }
1555 }
1556 }
1557 };
1558 mProcessStatsThread.start();
1559 }
1560
1561 @Override
1562 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1563 throws RemoteException {
Dianne Hackborna53de062012-05-08 18:53:51 -07001564 if (code == SYSPROPS_TRANSACTION) {
1565 // We need to tell all apps about the system property change.
1566 ArrayList<IBinder> procs = new ArrayList<IBinder>();
1567 synchronized(this) {
1568 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
1569 final int NA = apps.size();
1570 for (int ia=0; ia<NA; ia++) {
1571 ProcessRecord app = apps.valueAt(ia);
1572 if (app.thread != null) {
1573 procs.add(app.thread.asBinder());
1574 }
1575 }
1576 }
1577 }
1578
1579 int N = procs.size();
1580 for (int i=0; i<N; i++) {
1581 Parcel data2 = Parcel.obtain();
1582 try {
1583 procs.get(i).transact(IBinder.SYSPROPS_TRANSACTION, data2, null, 0);
1584 } catch (RemoteException e) {
1585 }
1586 data2.recycle();
1587 }
1588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 try {
1590 return super.onTransact(code, data, reply, flags);
1591 } catch (RuntimeException e) {
1592 // The activity manager only throws security exceptions, so let's
1593 // log all others.
1594 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001595 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
1597 throw e;
1598 }
1599 }
1600
1601 void updateCpuStats() {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001602 final long now = SystemClock.uptimeMillis();
1603 if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
1604 return;
1605 }
1606 if (mProcessStatsMutexFree.compareAndSet(true, false)) {
1607 synchronized (mProcessStatsThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 mProcessStatsThread.notify();
1609 }
1610 }
1611 }
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 void updateCpuStatsNow() {
1614 synchronized (mProcessStatsThread) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001615 mProcessStatsMutexFree.set(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 final long now = SystemClock.uptimeMillis();
1617 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (MONITOR_CPU_USAGE &&
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001620 mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
1621 mLastCpuTime.set(now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 haveNewCpuStats = true;
1623 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 //Slog.i(TAG, mProcessStats.printCurrentState());
1625 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 // + mProcessStats.getTotalCpuPercent() + "%");
1627
Joe Onorato8a9b2202010-02-26 18:56:32 -08001628 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 if ("true".equals(SystemProperties.get("events.cpu"))) {
1630 int user = mProcessStats.getLastUserTime();
1631 int system = mProcessStats.getLastSystemTime();
1632 int iowait = mProcessStats.getLastIoWaitTime();
1633 int irq = mProcessStats.getLastIrqTime();
1634 int softIrq = mProcessStats.getLastSoftIrqTime();
1635 int idle = mProcessStats.getLastIdleTime();
1636
1637 int total = user + system + iowait + irq + softIrq + idle;
1638 if (total == 0) total = 1;
1639
Doug Zongker2bec3d42009-12-04 12:52:44 -08001640 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 ((user+system+iowait+irq+softIrq) * 100) / total,
1642 (user * 100) / total,
1643 (system * 100) / total,
1644 (iowait * 100) / total,
1645 (irq * 100) / total,
1646 (softIrq * 100) / total);
1647 }
1648 }
1649
Amith Yamasanie43530a2009-08-21 13:11:37 -07001650 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001651 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001652 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 synchronized(mPidsSelfLocked) {
1654 if (haveNewCpuStats) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001655 if (mOnBattery) {
1656 int perc = bstats.startAddingCpuLocked();
1657 int totalUTime = 0;
1658 int totalSTime = 0;
Dianne Hackborn287952c2010-09-22 22:34:31 -07001659 final int N = mProcessStats.countStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001661 ProcessStats.Stats st = mProcessStats.getStats(i);
1662 if (!st.working) {
1663 continue;
1664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001666 int otherUTime = (st.rel_utime*perc)/100;
1667 int otherSTime = (st.rel_stime*perc)/100;
1668 totalUTime += otherUTime;
1669 totalSTime += otherSTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 if (pr != null) {
1671 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
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);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001675 pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001676 } else {
1677 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001678 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001679 if (ps != null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001680 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1681 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001682 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001686 bstats.finishAddingCpuLocked(perc, totalUTime,
1687 totalSTime, cpuSpeedTimes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 }
1689 }
1690 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1693 mLastWriteTime = now;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001694 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 }
1696 }
1697 }
1698 }
1699
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001700 @Override
1701 public void batteryNeedsCpuUpdate() {
1702 updateCpuStatsNow();
1703 }
1704
1705 @Override
1706 public void batteryPowerChanged(boolean onBattery) {
1707 // When plugging in, update the CPU stats first before changing
1708 // the plug state.
1709 updateCpuStatsNow();
1710 synchronized (this) {
1711 synchronized(mPidsSelfLocked) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001712 mOnBattery = DEBUG_POWER ? true : onBattery;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001713 }
1714 }
1715 }
1716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 /**
1718 * Initialize the application bind args. These are passed to each
1719 * process when the bindApplication() IPC is sent to the process. They're
1720 * lazily setup to make sure the services are running when they're asked for.
1721 */
1722 private HashMap<String, IBinder> getCommonServicesLocked() {
1723 if (mAppBindArgs == null) {
1724 mAppBindArgs = new HashMap<String, IBinder>();
1725
1726 // Setup the application init args
1727 mAppBindArgs.put("package", ServiceManager.getService("package"));
1728 mAppBindArgs.put("window", ServiceManager.getService("window"));
1729 mAppBindArgs.put(Context.ALARM_SERVICE,
1730 ServiceManager.getService(Context.ALARM_SERVICE));
1731 }
1732 return mAppBindArgs;
1733 }
1734
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001735 final void setFocusedActivityLocked(ActivityRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 if (mFocusedActivity != r) {
1737 mFocusedActivity = r;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08001738 if (r != null) {
1739 mWindowManager.setFocusedApp(r.appToken, true);
1740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742 }
1743
Dianne Hackborn906497c2010-05-10 15:57:38 -07001744 private final void updateLruProcessInternalLocked(ProcessRecord app,
1745 boolean oomAdj, boolean updateActivityTime, int bestPos) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001747 int lrui = mLruProcesses.indexOf(app);
1748 if (lrui >= 0) mLruProcesses.remove(lrui);
1749
1750 int i = mLruProcesses.size()-1;
1751 int skipTop = 0;
1752
Dianne Hackborn906497c2010-05-10 15:57:38 -07001753 app.lruSeq = mLruSeq;
1754
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001755 // compute the new weight for this process.
1756 if (updateActivityTime) {
1757 app.lastActivityTime = SystemClock.uptimeMillis();
1758 }
1759 if (app.activities.size() > 0) {
1760 // If this process has activities, we more strongly want to keep
1761 // it around.
1762 app.lruWeight = app.lastActivityTime;
1763 } else if (app.pubProviders.size() > 0) {
1764 // If this process contains content providers, we want to keep
1765 // it a little more strongly.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001766 app.lruWeight = app.lastActivityTime - ProcessList.CONTENT_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001767 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001768 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001769 } else {
1770 // If this process doesn't have activities, we less strongly
1771 // want to keep it around, and generally want to avoid getting
1772 // in front of any very recently used activities.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001773 app.lruWeight = app.lastActivityTime - ProcessList.EMPTY_APP_IDLE_OFFSET;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001774 // Also don't let it kick out the first few "real" hidden processes.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001775 skipTop = ProcessList.MIN_HIDDEN_APPS;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001776 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001777
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001778 while (i >= 0) {
1779 ProcessRecord p = mLruProcesses.get(i);
1780 // If this app shouldn't be in front of the first N background
1781 // apps, then skip over that many that are currently hidden.
Dianne Hackborn7d608422011-08-07 16:24:18 -07001782 if (skipTop > 0 && p.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001783 skipTop--;
1784 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001785 if (p.lruWeight <= app.lruWeight || i < bestPos) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001786 mLruProcesses.add(i+1, app);
1787 break;
1788 }
1789 i--;
1790 }
1791 if (i < 0) {
1792 mLruProcesses.add(0, app);
1793 }
1794
Dianne Hackborn906497c2010-05-10 15:57:38 -07001795 // If the app is currently using a content provider or service,
1796 // bump those processes as well.
1797 if (app.connections.size() > 0) {
1798 for (ConnectionRecord cr : app.connections) {
1799 if (cr.binding != null && cr.binding.service != null
1800 && cr.binding.service.app != null
1801 && cr.binding.service.app.lruSeq != mLruSeq) {
Björn Davidsson90f9e312010-11-18 08:26:27 +01001802 updateLruProcessInternalLocked(cr.binding.service.app, false,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001803 updateActivityTime, i+1);
1804 }
1805 }
1806 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001807 for (int j=app.conProviders.size()-1; j>=0; j--) {
1808 ContentProviderRecord cpr = app.conProviders.get(j).provider;
1809 if (cpr.proc != null && cpr.proc.lruSeq != mLruSeq) {
Jean-Baptiste Querub8c64052012-06-11 11:01:19 -07001810 updateLruProcessInternalLocked(cpr.proc, false,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001811 updateActivityTime, i+1);
Dianne Hackborn906497c2010-05-10 15:57:38 -07001812 }
1813 }
1814
Joe Onorato8a9b2202010-02-26 18:56:32 -08001815 //Slog.i(TAG, "Putting proc to front: " + app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 if (oomAdj) {
1817 updateOomAdjLocked();
1818 }
1819 }
1820
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001821 final void updateLruProcessLocked(ProcessRecord app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001822 boolean oomAdj, boolean updateActivityTime) {
1823 mLruSeq++;
1824 updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0);
1825 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07001826
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001827 final ProcessRecord getProcessRecordLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 String processName, int uid) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07001829 if (uid == Process.SYSTEM_UID) {
Amith Yamasani0184ce92012-03-28 22:41:41 -07001830 // The system gets to run in any process. If there are multiple
1831 // processes with the same uid, just pick the first (this
1832 // should never happen).
1833 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1834 processName);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001835 if (procs == null) return null;
1836 final int N = procs.size();
1837 for (int i = 0; i < N; i++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001838 if (UserHandle.isSameUser(procs.keyAt(i), uid)) return procs.valueAt(i);
Amith Yamasania4a54e22012-04-16 15:44:19 -07001839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841 ProcessRecord proc = mProcessNames.get(processName, uid);
1842 return proc;
1843 }
1844
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001845 void ensurePackageDexOpt(String packageName) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001846 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001847 try {
1848 if (pm.performDexOpt(packageName)) {
1849 mDidDexOpt = true;
1850 }
1851 } catch (RemoteException e) {
1852 }
1853 }
1854
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001855 boolean isNextTransitionForward() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 int transit = mWindowManager.getPendingAppTransition();
1857 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1858 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1859 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1860 }
1861
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001862 final ProcessRecord startProcessLocked(String processName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001864 String hostingType, ComponentName hostingName, boolean allowWhileBooting,
1865 boolean isolated) {
1866 ProcessRecord app;
1867 if (!isolated) {
1868 app = getProcessRecordLocked(processName, info.uid);
1869 } else {
1870 // If this is an isolated process, it can't re-use an existing process.
1871 app = null;
1872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 // We don't have to do anything more if:
1874 // (1) There is an existing application record; and
1875 // (2) The caller doesn't think it is dead, OR there is no thread
1876 // object attached to it so we know it couldn't have crashed; and
1877 // (3) There is a pid assigned to it, so it is either starting or
1878 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001879 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 + " app=" + app + " knownToBeDead=" + knownToBeDead
1881 + " thread=" + (app != null ? app.thread : null)
1882 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001883 if (app != null && app.pid > 0) {
1884 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001885 // We already have the app running, or are waiting for it to
1886 // come up (we have a pid but not yet its thread), so keep it.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001887 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
Dianne Hackborn6c418d52011-06-29 14:05:33 -07001888 // If this is a new package in the process, add the package to the list
1889 app.addPackage(info.packageName);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001890 return app;
1891 } else {
1892 // An application record is attached to a previous process,
1893 // clean it up now.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001894 if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07001895 handleAppDiedLocked(app, true, true);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 String hostingNameStr = hostingName != null
1900 ? hostingName.flattenToShortString() : null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001901
1902 if (!isolated) {
1903 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1904 // If we are in the background, then check to see if this process
1905 // is bad. If so, we will just silently fail.
1906 if (mBadProcesses.get(info.processName, info.uid) != null) {
1907 if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
1908 + "/" + info.processName);
1909 return null;
1910 }
1911 } else {
1912 // When the user is explicitly starting a process, then clear its
1913 // crash count so that we won't make it bad until they see at
1914 // least one crash dialog again, and make the process good again
1915 // if it had been bad.
1916 if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001917 + "/" + info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001918 mProcessCrashTimes.remove(info.processName, info.uid);
1919 if (mBadProcesses.get(info.processName, info.uid) != null) {
1920 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
1921 info.processName);
1922 mBadProcesses.remove(info.processName, info.uid);
1923 if (app != null) {
1924 app.bad = false;
1925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 }
1927 }
1928 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001931 app = newProcessRecordLocked(null, info, processName, isolated);
1932 if (app == null) {
1933 Slog.w(TAG, "Failed making new process record for "
1934 + processName + "/" + info.uid + " isolated=" + isolated);
1935 return null;
1936 }
1937 mProcessNames.put(processName, app.uid, app);
1938 if (isolated) {
1939 mIsolatedProcesses.put(app.uid, app);
1940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 } else {
1942 // If this is a new package in the process, add the package to the list
1943 app.addPackage(info.packageName);
1944 }
1945
1946 // If the system is not ready yet, then hold off on starting this
1947 // process until it is.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001948 if (!mProcessesReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001949 && !isAllowedWhileBooting(info)
1950 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 if (!mProcessesOnHold.contains(app)) {
1952 mProcessesOnHold.add(app);
1953 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001954 if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 return app;
1956 }
1957
1958 startProcessLocked(app, hostingType, hostingNameStr);
1959 return (app.pid != 0) ? app : null;
1960 }
1961
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001962 boolean isAllowedWhileBooting(ApplicationInfo ai) {
1963 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
1964 }
1965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 private final void startProcessLocked(ProcessRecord app,
1967 String hostingType, String hostingNameStr) {
1968 if (app.pid > 0 && app.pid != MY_PID) {
1969 synchronized (mPidsSelfLocked) {
1970 mPidsSelfLocked.remove(app.pid);
1971 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
1972 }
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07001973 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 }
1975
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001976 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
1977 "startProcessLocked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 mProcessesOnHold.remove(app);
1979
1980 updateCpuStats();
1981
1982 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
1983 mProcDeaths[0] = 0;
1984
1985 try {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001986 int uid = app.uid;
Amith Yamasani742a6712011-05-04 14:49:28 -07001987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 int[] gids = null;
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07001989 int mountExternal = Zygote.MOUNT_EXTERNAL_NONE;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08001990 if (!app.isolated) {
1991 try {
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07001992 final PackageManager pm = mContext.getPackageManager();
1993 gids = pm.getPackageGids(app.info.packageName);
Jeff Sharkeye217ee42012-08-28 16:23:01 -07001994
1995 if (Environment.isExternalStorageEmulated()) {
1996 if (pm.checkPermission(
1997 android.Manifest.permission.ACCESS_ALL_EXTERNAL_STORAGE,
1998 app.info.packageName) == PERMISSION_GRANTED) {
1999 mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER_ALL;
2000 } else {
2001 mountExternal = Zygote.MOUNT_EXTERNAL_MULTIUSER;
2002 }
2003 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002004 } catch (PackageManager.NameNotFoundException e) {
2005 Slog.w(TAG, "Unable to retrieve gids", e);
2006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 }
2008 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
2009 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2010 && mTopComponent != null
2011 && app.processName.equals(mTopComponent.getPackageName())) {
2012 uid = 0;
2013 }
2014 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
2015 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
2016 uid = 0;
2017 }
2018 }
2019 int debugFlags = 0;
2020 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
2021 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
Elliott Hughesfa36aee2011-06-17 14:39:41 -07002022 // Also turn on CheckJNI for debuggable apps. It's quite
2023 // awkward to turn on otherwise.
2024 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08002026 // Run the app in safe mode if its manifest requests so or the
2027 // system is booted in safe mode.
2028 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
2029 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08002030 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
2031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
2033 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
2034 }
Elliott Hughesae07ecf2011-07-06 17:33:27 -07002035 if ("1".equals(SystemProperties.get("debug.jni.logging"))) {
2036 debugFlags |= Zygote.DEBUG_ENABLE_JNI_LOGGING;
2037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 if ("1".equals(SystemProperties.get("debug.assert"))) {
2039 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
2040 }
Jeff Brown3f9dd282011-07-08 20:02:19 -07002041
2042 // Start the process. It will either succeed and return a result containing
2043 // the PID of the new process, or else throw a RuntimeException.
2044 Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread",
Jeff Sharkey5b1ada22012-08-14 18:47:09 -07002045 app.processName, uid, uid, gids, debugFlags, mountExternal,
Stephen Smalley83d9eda2012-01-13 08:34:17 -05002046 app.info.targetSdkVersion, null, null);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
2049 synchronized (bs) {
2050 if (bs.isOnBattery()) {
2051 app.batteryStats.incStartsLocked();
2052 }
2053 }
2054
Jeff Brown3f9dd282011-07-08 20:02:19 -07002055 EventLog.writeEvent(EventLogTags.AM_PROC_START, startResult.pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 app.processName, hostingType,
2057 hostingNameStr != null ? hostingNameStr : "");
2058
2059 if (app.persistent) {
Jeff Brown3f9dd282011-07-08 20:02:19 -07002060 Watchdog.getInstance().processStarted(app.processName, startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 }
2062
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002063 StringBuilder buf = mStringBuilder;
2064 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 buf.append("Start proc ");
2066 buf.append(app.processName);
2067 buf.append(" for ");
2068 buf.append(hostingType);
2069 if (hostingNameStr != null) {
2070 buf.append(" ");
2071 buf.append(hostingNameStr);
2072 }
2073 buf.append(": pid=");
Jeff Brown3f9dd282011-07-08 20:02:19 -07002074 buf.append(startResult.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 buf.append(" uid=");
2076 buf.append(uid);
2077 buf.append(" gids={");
2078 if (gids != null) {
2079 for (int gi=0; gi<gids.length; gi++) {
2080 if (gi != 0) buf.append(", ");
2081 buf.append(gids[gi]);
2082
2083 }
2084 }
2085 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08002086 Slog.i(TAG, buf.toString());
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002087 app.setPid(startResult.pid);
Jeff Brown3f9dd282011-07-08 20:02:19 -07002088 app.usingWrapper = startResult.usingWrapper;
2089 app.removed = false;
2090 synchronized (mPidsSelfLocked) {
2091 this.mPidsSelfLocked.put(startResult.pid, app);
2092 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
2093 msg.obj = app;
2094 mHandler.sendMessageDelayed(msg, startResult.usingWrapper
2095 ? PROC_START_TIMEOUT_WITH_WRAPPER : PROC_START_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 }
2097 } catch (RuntimeException e) {
2098 // XXX do better error recovery.
Dianne Hackbornf88dd0b2012-08-08 17:20:32 -07002099 app.setPid(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002100 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 }
2102 }
2103
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002104 void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 if (resumed) {
2106 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
2107 } else {
2108 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
2109 }
2110 }
2111
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002112 boolean startHomeActivityLocked(int userId, UserStartedState startingUser) {
Mike Lockwooda8f767a2011-08-31 14:32:37 -04002113 if (mHeadless) {
2114 // Added because none of the other calls to ensureBootCompleted seem to fire
2115 // when running headless.
2116 ensureBootCompleted();
2117 return false;
2118 }
2119
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002120 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2121 && mTopAction == null) {
2122 // We are running in factory test mode, but unable to find
2123 // the factory test app, so just sit around displaying the
2124 // error message and don't try to start anything.
2125 return false;
2126 }
2127 Intent intent = new Intent(
2128 mTopAction,
2129 mTopData != null ? Uri.parse(mTopData) : null);
2130 intent.setComponent(mTopComponent);
2131 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
2132 intent.addCategory(Intent.CATEGORY_HOME);
2133 }
2134 ActivityInfo aInfo =
2135 intent.resolveActivityInfo(mContext.getPackageManager(),
2136 STOCK_PM_FLAGS);
2137 if (aInfo != null) {
2138 intent.setComponent(new ComponentName(
2139 aInfo.applicationInfo.packageName, aInfo.name));
2140 // Don't do this if the home app is currently being
2141 // instrumented.
Amith Yamasani742a6712011-05-04 14:49:28 -07002142 aInfo = new ActivityInfo(aInfo);
2143 aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002144 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
2145 aInfo.applicationInfo.uid);
2146 if (app == null || app.instrumentationClass == null) {
2147 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborna4972e92012-03-14 10:38:05 -07002148 mMainStack.startActivityLocked(null, intent, null, aInfo,
2149 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002150 }
2151 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002152 if (startingUser != null) {
2153 mMainStack.addStartingUserLocked(startingUser);
2154 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002155
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002156 return true;
2157 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002158
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002159 /**
2160 * Starts the "new version setup screen" if appropriate.
2161 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002162 void startSetupActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002163 // Only do this once per boot.
2164 if (mCheckedForSetup) {
2165 return;
2166 }
2167
2168 // We will show this screen if the current one is a different
2169 // version than the last one shown, and we are not running in
2170 // low-level factory test mode.
2171 final ContentResolver resolver = mContext.getContentResolver();
2172 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
2173 Settings.Secure.getInt(resolver,
2174 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
2175 mCheckedForSetup = true;
2176
2177 // See if we should be showing the platform update setup UI.
2178 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
2179 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
2180 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
2181
2182 // We don't allow third party apps to replace this.
2183 ResolveInfo ri = null;
2184 for (int i=0; ris != null && i<ris.size(); i++) {
2185 if ((ris.get(i).activityInfo.applicationInfo.flags
2186 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2187 ri = ris.get(i);
2188 break;
2189 }
2190 }
2191
2192 if (ri != null) {
2193 String vers = ri.activityInfo.metaData != null
2194 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
2195 : null;
2196 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
2197 vers = ri.activityInfo.applicationInfo.metaData.getString(
2198 Intent.METADATA_SETUP_VERSION);
2199 }
2200 String lastVers = Settings.Secure.getString(
2201 resolver, Settings.Secure.LAST_SETUP_SHOWN);
2202 if (vers != null && !vers.equals(lastVers)) {
2203 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2204 intent.setComponent(new ComponentName(
2205 ri.activityInfo.packageName, ri.activityInfo.name));
Dianne Hackborna4972e92012-03-14 10:38:05 -07002206 mMainStack.startActivityLocked(null, intent, null, ri.activityInfo,
2207 null, null, 0, 0, 0, 0, null, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002208 }
2209 }
2210 }
2211 }
2212
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002213 CompatibilityInfo compatibilityInfoForPackageLocked(ApplicationInfo ai) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002214 return mCompatModePackages.compatibilityInfoForPackageLocked(ai);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002215 }
2216
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002217 void enforceNotIsolatedCaller(String caller) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002218 if (UserHandle.isIsolated(Binder.getCallingUid())) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002219 throw new SecurityException("Isolated process not allowed to call " + caller);
2220 }
2221 }
2222
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002223 public int getFrontActivityScreenCompatMode() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002224 enforceNotIsolatedCaller("getFrontActivityScreenCompatMode");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002225 synchronized (this) {
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002226 return mCompatModePackages.getFrontActivityScreenCompatModeLocked();
2227 }
2228 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002229
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002230 public void setFrontActivityScreenCompatMode(int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002231 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2232 "setFrontActivityScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002233 synchronized (this) {
2234 mCompatModePackages.setFrontActivityScreenCompatModeLocked(mode);
2235 }
2236 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002237
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002238 public int getPackageScreenCompatMode(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002239 enforceNotIsolatedCaller("getPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002240 synchronized (this) {
2241 return mCompatModePackages.getPackageScreenCompatModeLocked(packageName);
2242 }
2243 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002244
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002245 public void setPackageScreenCompatMode(String packageName, int mode) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002246 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2247 "setPackageScreenCompatMode");
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -07002248 synchronized (this) {
2249 mCompatModePackages.setPackageScreenCompatModeLocked(packageName, mode);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002250 }
2251 }
2252
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002253 public boolean getPackageAskScreenCompat(String packageName) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002254 enforceNotIsolatedCaller("getPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002255 synchronized (this) {
2256 return mCompatModePackages.getPackageAskCompatModeLocked(packageName);
2257 }
2258 }
2259
2260 public void setPackageAskScreenCompat(String packageName, boolean ask) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002261 enforceCallingPermission(android.Manifest.permission.SET_SCREEN_COMPATIBILITY,
2262 "setPackageAskScreenCompat");
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07002263 synchronized (this) {
2264 mCompatModePackages.setPackageAskCompatModeLocked(packageName, ask);
2265 }
2266 }
2267
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 void reportResumedActivityLocked(ActivityRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002269 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002270 updateUsageStats(r, true);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002272
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002273 private void dispatchProcessesChanged() {
2274 int N;
2275 synchronized (this) {
2276 N = mPendingProcessChanges.size();
2277 if (mActiveProcessChanges.length < N) {
2278 mActiveProcessChanges = new ProcessChangeItem[N];
2279 }
2280 mPendingProcessChanges.toArray(mActiveProcessChanges);
2281 mAvailProcessChanges.addAll(mPendingProcessChanges);
2282 mPendingProcessChanges.clear();
2283 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "*** Delivering " + N + " process changes");
2284 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002285 int i = mProcessObservers.beginBroadcast();
2286 while (i > 0) {
2287 i--;
2288 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2289 if (observer != null) {
2290 try {
Dianne Hackborna93c2c12012-05-31 15:29:36 -07002291 for (int j=0; j<N; j++) {
2292 ProcessChangeItem item = mActiveProcessChanges[j];
2293 if ((item.changes&ProcessChangeItem.CHANGE_ACTIVITIES) != 0) {
2294 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "ACTIVITIES CHANGED pid="
2295 + item.pid + " uid=" + item.uid + ": "
2296 + item.foregroundActivities);
2297 observer.onForegroundActivitiesChanged(item.pid, item.uid,
2298 item.foregroundActivities);
2299 }
2300 if ((item.changes&ProcessChangeItem.CHANGE_IMPORTANCE) != 0) {
2301 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "IMPORTANCE CHANGED pid="
2302 + item.pid + " uid=" + item.uid + ": " + item.importance);
2303 observer.onImportanceChanged(item.pid, item.uid,
2304 item.importance);
2305 }
2306 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07002307 } catch (RemoteException e) {
2308 }
2309 }
2310 }
2311 mProcessObservers.finishBroadcast();
2312 }
2313
2314 private void dispatchProcessDied(int pid, int uid) {
2315 int i = mProcessObservers.beginBroadcast();
2316 while (i > 0) {
2317 i--;
2318 final IProcessObserver observer = mProcessObservers.getBroadcastItem(i);
2319 if (observer != null) {
2320 try {
2321 observer.onProcessDied(pid, uid);
2322 } catch (RemoteException e) {
2323 }
2324 }
2325 }
2326 mProcessObservers.finishBroadcast();
2327 }
2328
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002329 final void doPendingActivityLaunchesLocked(boolean doResume) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002330 final int N = mPendingActivityLaunches.size();
2331 if (N <= 0) {
2332 return;
2333 }
2334 for (int i=0; i<N; i++) {
2335 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002336 mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07002337 pal.startFlags, doResume && i == (N-1), null);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002338 }
2339 mPendingActivityLaunches.clear();
2340 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002341
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002342 public final int startActivity(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002343 Intent intent, String resolvedType, IBinder resultTo,
2344 String resultWho, int requestCode, int startFlags,
2345 String profileFile, ParcelFileDescriptor profileFd, Bundle options) {
Amith Yamasani82644082012-08-03 13:09:11 -07002346 return startActivityAsUser(caller, intent, resolvedType, resultTo, resultWho, requestCode,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002347 startFlags, profileFile, profileFd, options, UserHandle.getCallingUserId());
Amith Yamasani82644082012-08-03 13:09:11 -07002348 }
2349
2350 public final int startActivityAsUser(IApplicationThread caller,
2351 Intent intent, String resolvedType, IBinder resultTo,
2352 String resultWho, int requestCode, int startFlags,
2353 String profileFile, ParcelFileDescriptor profileFd, Bundle options, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002354 enforceNotIsolatedCaller("startActivity");
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002355 if (userId != UserHandle.getCallingUserId()) {
Amith Yamasani82644082012-08-03 13:09:11 -07002356 // Requesting a different user, make sure that they have the permission
2357 if (checkComponentPermission(
2358 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
2359 Binder.getCallingPid(), Binder.getCallingUid(), -1, true)
2360 == PackageManager.PERMISSION_GRANTED) {
2361 // Translate to the current user id, if caller wasn't aware
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002362 if (userId == UserHandle.USER_CURRENT) {
Amith Yamasani82644082012-08-03 13:09:11 -07002363 userId = mCurrentUserId;
2364 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002365 } else {
Amith Yamasani82644082012-08-03 13:09:11 -07002366 String msg = "Permission Denial: "
2367 + "Request to startActivity as user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002368 + " but is calling from user " + UserHandle.getCallingUserId()
Amith Yamasani82644082012-08-03 13:09:11 -07002369 + "; this requires "
2370 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
2371 Slog.w(TAG, msg);
2372 throw new SecurityException(msg);
2373 }
2374 } else {
2375 if (intent.getCategories() != null
2376 && intent.getCategories().contains(Intent.CATEGORY_HOME)) {
2377 // Requesting home, set the identity to the current user
2378 // HACK!
2379 userId = mCurrentUserId;
2380 } else {
2381 // TODO: Fix this in a better way - calls coming from SystemUI should probably carry
2382 // the current user's userId
2383 if (Binder.getCallingUid() < Process.FIRST_APPLICATION_UID) {
2384 userId = 0;
2385 } else {
2386 userId = Binder.getOrigCallingUser();
2387 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002388 }
2389 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002390 return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002391 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
2392 null, null, options, userId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002393 }
2394
2395 public final WaitResult startActivityAndWait(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002396 Intent intent, String resolvedType, IBinder resultTo,
2397 String resultWho, int requestCode, int startFlags, String profileFile,
2398 ParcelFileDescriptor profileFd, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002399 enforceNotIsolatedCaller("startActivityAndWait");
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002400 WaitResult res = new WaitResult();
Amith Yamasani742a6712011-05-04 14:49:28 -07002401 int userId = Binder.getOrigCallingUser();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002402 mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002403 resultTo, resultWho, requestCode, startFlags, profileFile, profileFd,
2404 res, null, options, userId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002405 return res;
2406 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08002407
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002408 public final int startActivityWithConfig(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002409 Intent intent, String resolvedType, IBinder resultTo,
2410 String resultWho, int requestCode, int startFlags, Configuration config,
2411 Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002412 enforceNotIsolatedCaller("startActivityWithConfig");
Amith Yamasani742a6712011-05-04 14:49:28 -07002413 int ret = mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002414 resultTo, resultWho, requestCode, startFlags,
2415 null, null, null, config, options, Binder.getOrigCallingUser());
Amith Yamasani742a6712011-05-04 14:49:28 -07002416 return ret;
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002417 }
2418
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002419 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002420 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002421 IBinder resultTo, String resultWho, int requestCode,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002422 int flagsMask, int flagsValues, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002423 enforceNotIsolatedCaller("startActivityIntentSender");
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002424 // Refuse possible leaked file descriptors
2425 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
2426 throw new IllegalArgumentException("File descriptors passed in Intent");
2427 }
2428
2429 IIntentSender sender = intent.getTarget();
2430 if (!(sender instanceof PendingIntentRecord)) {
2431 throw new IllegalArgumentException("Bad PendingIntent object");
2432 }
2433
2434 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002435
2436 synchronized (this) {
2437 // If this is coming from the currently resumed activity, it is
2438 // effectively saying that app switches are allowed at this point.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 if (mMainStack.mResumedActivity != null
2440 && mMainStack.mResumedActivity.info.applicationInfo.uid ==
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002441 Binder.getCallingUid()) {
2442 mAppSwitchesAllowedTime = 0;
2443 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002444 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07002445 int ret = pir.sendInner(0, fillInIntent, resolvedType, null, null,
2446 resultTo, resultWho, requestCode, flagsMask, flagsValues, options);
Amith Yamasani742a6712011-05-04 14:49:28 -07002447 return ret;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002448 }
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 public boolean startNextMatchingActivity(IBinder callingActivity,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002451 Intent intent, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 // Refuse possible leaked file descriptors
2453 if (intent != null && intent.hasFileDescriptors() == true) {
2454 throw new IllegalArgumentException("File descriptors passed in Intent");
2455 }
2456
2457 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002458 ActivityRecord r = mMainStack.isInStackLocked(callingActivity);
2459 if (r == null) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002460 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 return false;
2462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 if (r.app == null || r.app.thread == null) {
2464 // The caller is not running... d'oh!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002465 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 return false;
2467 }
2468 intent = new Intent(intent);
2469 // The caller is not allowed to change the data.
2470 intent.setDataAndType(r.intent.getData(), r.intent.getType());
2471 // And we are resetting to find the next component...
2472 intent.setComponent(null);
2473
2474 ActivityInfo aInfo = null;
2475 try {
2476 List<ResolveInfo> resolves =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002477 AppGlobals.getPackageManager().queryIntentActivities(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 intent, r.resolvedType,
Amith Yamasani483f3b02012-03-13 16:08:00 -07002479 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002480 UserHandle.getCallingUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481
2482 // Look for the original activity in the list...
2483 final int N = resolves != null ? resolves.size() : 0;
2484 for (int i=0; i<N; i++) {
2485 ResolveInfo rInfo = resolves.get(i);
2486 if (rInfo.activityInfo.packageName.equals(r.packageName)
2487 && rInfo.activityInfo.name.equals(r.info.name)) {
2488 // We found the current one... the next matching is
2489 // after it.
2490 i++;
2491 if (i<N) {
2492 aInfo = resolves.get(i).activityInfo;
2493 }
2494 break;
2495 }
2496 }
2497 } catch (RemoteException e) {
2498 }
2499
2500 if (aInfo == null) {
2501 // Nobody who is next!
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07002502 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 return false;
2504 }
2505
2506 intent.setComponent(new ComponentName(
2507 aInfo.applicationInfo.packageName, aInfo.name));
2508 intent.setFlags(intent.getFlags()&~(
2509 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
2510 Intent.FLAG_ACTIVITY_CLEAR_TOP|
2511 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
2512 Intent.FLAG_ACTIVITY_NEW_TASK));
2513
2514 // Okay now we need to start the new activity, replacing the
2515 // currently running activity. This is a little tricky because
2516 // we want to start the new one as if the current one is finished,
2517 // but not finish the current one first so that there is no flicker.
2518 // And thus...
2519 final boolean wasFinishing = r.finishing;
2520 r.finishing = true;
2521
2522 // Propagate reply information over to the new activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002523 final ActivityRecord resultTo = r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 final String resultWho = r.resultWho;
2525 final int requestCode = r.requestCode;
2526 r.resultTo = null;
2527 if (resultTo != null) {
2528 resultTo.removeResultsLocked(r, resultWho, requestCode);
2529 }
2530
2531 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002532 int res = mMainStack.startActivityLocked(r.app.thread, intent,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002533 r.resolvedType, aInfo, resultTo != null ? resultTo.appToken : null,
2534 resultWho, requestCode, -1, r.launchedFromUid, 0,
2535 options, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 Binder.restoreCallingIdentity(origId);
2537
2538 r.finishing = wasFinishing;
Dianne Hackborna4972e92012-03-14 10:38:05 -07002539 if (res != ActivityManager.START_SUCCESS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 return false;
2541 }
2542 return true;
2543 }
2544 }
2545
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002546 public final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 Intent intent, String resolvedType, IBinder resultTo,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002548 String resultWho, int requestCode, int startFlags, Bundle options) {
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002549
2550 // This is so super not safe, that only the system (or okay root)
2551 // can do it.
2552 final int callingUid = Binder.getCallingUid();
2553 if (callingUid != 0 && callingUid != Process.myUid()) {
2554 throw new SecurityException(
2555 "startActivityInPackage only available to the system");
2556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557
Amith Yamasani742a6712011-05-04 14:49:28 -07002558 int ret = mMainStack.startActivityMayWait(null, uid, intent, resolvedType,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002559 resultTo, resultWho, requestCode, startFlags,
Amith Yamasani7300dbe2012-08-17 14:17:57 -07002560 null, null, null, null, options, UserHandle.getUserId(uid));
Amith Yamasani742a6712011-05-04 14:49:28 -07002561 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002562 }
2563
2564 public final int startActivities(IApplicationThread caller,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002565 Intent[] intents, String[] resolvedTypes, IBinder resultTo, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08002566 enforceNotIsolatedCaller("startActivities");
Amith Yamasani742a6712011-05-04 14:49:28 -07002567 int ret = mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002568 options, Binder.getOrigCallingUser());
Amith Yamasani742a6712011-05-04 14:49:28 -07002569 return ret;
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002570 }
2571
2572 public final int startActivitiesInPackage(int uid,
Dianne Hackborna4972e92012-03-14 10:38:05 -07002573 Intent[] intents, String[] resolvedTypes, IBinder resultTo,
2574 Bundle options) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002575
2576 // This is so super not safe, that only the system (or okay root)
2577 // can do it.
2578 final int callingUid = Binder.getCallingUid();
2579 if (callingUid != 0 && callingUid != Process.myUid()) {
2580 throw new SecurityException(
2581 "startActivityInPackage only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 }
Amith Yamasani742a6712011-05-04 14:49:28 -07002583 int ret = mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002584 options, UserHandle.getUserId(uid));
Amith Yamasani742a6712011-05-04 14:49:28 -07002585 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002588 final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 int N = mRecentTasks.size();
Dianne Hackborn7c0e75e2010-12-21 19:15:40 -08002590 // Quick case: check if the top-most recent task is the same.
2591 if (N > 0 && mRecentTasks.get(0) == task) {
2592 return;
2593 }
2594 // Remove any existing entries that are the same kind of task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 for (int i=0; i<N; i++) {
2596 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07002597 if (task.userId == tr.userId
2598 && ((task.affinity != null && task.affinity.equals(tr.affinity))
2599 || (task.intent != null && task.intent.filterEquals(tr.intent)))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 mRecentTasks.remove(i);
2601 i--;
2602 N--;
2603 if (task.intent == null) {
2604 // If the new recent task we are adding is not fully
2605 // specified, then replace it with the existing recent task.
2606 task = tr;
2607 }
2608 }
2609 }
2610 if (N >= MAX_RECENT_TASKS) {
2611 mRecentTasks.remove(N-1);
2612 }
2613 mRecentTasks.add(0, task);
2614 }
2615
2616 public void setRequestedOrientation(IBinder token,
2617 int requestedOrientation) {
2618 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002619 ActivityRecord r = mMainStack.isInStackLocked(token);
2620 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 return;
2622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbe707852011-11-11 14:32:10 -08002624 mWindowManager.setAppOrientation(r.appToken, requestedOrientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002626 mConfiguration,
Dianne Hackbornbe707852011-11-11 14:32:10 -08002627 r.mayFreezeScreenLocked(r.app) ? r.appToken : null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 if (config != null) {
2629 r.frozenBeforeDestroy = true;
Dianne Hackborn813075a62011-11-14 17:45:19 -08002630 if (!updateConfigurationLocked(config, r, false, false)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002631 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 }
2633 }
2634 Binder.restoreCallingIdentity(origId);
2635 }
2636 }
2637
2638 public int getRequestedOrientation(IBinder token) {
2639 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002640 ActivityRecord r = mMainStack.isInStackLocked(token);
2641 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2643 }
Dianne Hackbornbe707852011-11-11 14:32:10 -08002644 return mWindowManager.getAppOrientation(r.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 }
2646 }
2647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 /**
2649 * This is the internal entry point for handling Activity.finish().
2650 *
2651 * @param token The Binder token referencing the Activity we want to finish.
2652 * @param resultCode Result code, if any, from this Activity.
2653 * @param resultData Result data (Intent), if any, from this Activity.
2654 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11002655 * @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 -08002656 */
2657 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
2658 // Refuse possible leaked file descriptors
2659 if (resultData != null && resultData.hasFileDescriptors() == true) {
2660 throw new IllegalArgumentException("File descriptors passed in Intent");
2661 }
2662
2663 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002664 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 // Find the first activity that is not finishing.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002666 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 if (next != null) {
2668 // ask watcher if this is allowed
2669 boolean resumeOK = true;
2670 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002671 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002673 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 }
2675
2676 if (!resumeOK) {
2677 return false;
2678 }
2679 }
2680 }
2681 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 boolean res = mMainStack.requestFinishActivityLocked(token, resultCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 resultData, "app-request");
2684 Binder.restoreCallingIdentity(origId);
2685 return res;
2686 }
2687 }
2688
Dianne Hackborn860755f2010-06-03 18:47:52 -07002689 public final void finishHeavyWeightApp() {
2690 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2691 != PackageManager.PERMISSION_GRANTED) {
2692 String msg = "Permission Denial: finishHeavyWeightApp() from pid="
2693 + Binder.getCallingPid()
2694 + ", uid=" + Binder.getCallingUid()
2695 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2696 Slog.w(TAG, msg);
2697 throw new SecurityException(msg);
2698 }
2699
2700 synchronized(this) {
2701 if (mHeavyWeightProcess == null) {
2702 return;
2703 }
2704
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002705 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
Dianne Hackborn860755f2010-06-03 18:47:52 -07002706 mHeavyWeightProcess.activities);
2707 for (int i=0; i<activities.size(); i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002708 ActivityRecord r = activities.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002709 if (!r.finishing) {
Dianne Hackbornbe707852011-11-11 14:32:10 -08002710 int index = mMainStack.indexOfTokenLocked(r.appToken);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002711 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002712 mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED,
Dianne Hackborn860755f2010-06-03 18:47:52 -07002713 null, "finish-heavy");
2714 }
2715 }
2716 }
2717
2718 mHeavyWeightProcess = null;
2719 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
2720 }
2721 }
2722
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002723 public void crashApplication(int uid, int initialPid, String packageName,
2724 String message) {
2725 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2726 != PackageManager.PERMISSION_GRANTED) {
2727 String msg = "Permission Denial: crashApplication() from pid="
2728 + Binder.getCallingPid()
2729 + ", uid=" + Binder.getCallingUid()
2730 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2731 Slog.w(TAG, msg);
2732 throw new SecurityException(msg);
2733 }
2734
2735 synchronized(this) {
2736 ProcessRecord proc = null;
2737
2738 // Figure out which process to kill. We don't trust that initialPid
2739 // still has any relation to current pids, so must scan through the
2740 // list.
2741 synchronized (mPidsSelfLocked) {
2742 for (int i=0; i<mPidsSelfLocked.size(); i++) {
2743 ProcessRecord p = mPidsSelfLocked.valueAt(i);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08002744 if (p.uid != uid) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002745 continue;
2746 }
2747 if (p.pid == initialPid) {
2748 proc = p;
2749 break;
2750 }
2751 for (String str : p.pkgList) {
2752 if (str.equals(packageName)) {
2753 proc = p;
2754 }
2755 }
2756 }
2757 }
2758
2759 if (proc == null) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07002760 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002761 + " initialPid=" + initialPid
2762 + " packageName=" + packageName);
2763 return;
2764 }
2765
2766 if (proc.thread != null) {
Dianne Hackborn9f531192010-08-04 17:48:03 -07002767 if (proc.pid == Process.myPid()) {
2768 Log.w(TAG, "crashApplication: trying to crash self!");
2769 return;
2770 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002771 long ident = Binder.clearCallingIdentity();
2772 try {
2773 proc.thread.scheduleCrash(message);
2774 } catch (RemoteException e) {
2775 }
2776 Binder.restoreCallingIdentity(ident);
2777 }
2778 }
2779 }
2780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 public final void finishSubActivity(IBinder token, String resultWho,
2782 int requestCode) {
2783 synchronized(this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002785 mMainStack.finishSubActivityLocked(token, resultWho, requestCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 Binder.restoreCallingIdentity(origId);
2787 }
2788 }
2789
Dianne Hackbornecc5a9c2012-04-26 18:56:09 -07002790 public boolean finishActivityAffinity(IBinder token) {
2791 synchronized(this) {
2792 final long origId = Binder.clearCallingIdentity();
2793 boolean res = mMainStack.finishActivityAffinityLocked(token);
2794 Binder.restoreCallingIdentity(origId);
2795 return res;
2796 }
2797 }
2798
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002799 public boolean willActivityBeVisible(IBinder token) {
2800 synchronized(this) {
2801 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002802 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2803 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08002804 if (r.appToken == token) {
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002805 return true;
2806 }
2807 if (r.fullscreen && !r.finishing) {
2808 return false;
2809 }
2810 }
2811 return true;
2812 }
2813 }
2814
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002815 public void overridePendingTransition(IBinder token, String packageName,
2816 int enterAnim, int exitAnim) {
2817 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002818 ActivityRecord self = mMainStack.isInStackLocked(token);
2819 if (self == null) {
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002820 return;
2821 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002822
2823 final long origId = Binder.clearCallingIdentity();
2824
2825 if (self.state == ActivityState.RESUMED
2826 || self.state == ActivityState.PAUSING) {
2827 mWindowManager.overridePendingAppTransition(packageName,
Dianne Hackborn84375872012-06-01 19:03:50 -07002828 enterAnim, exitAnim, null);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002829 }
2830
2831 Binder.restoreCallingIdentity(origId);
2832 }
2833 }
2834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 * Main function for removing an existing process from the activity manager
2837 * as a result of that process going away. Clears out all connections
2838 * to the process.
2839 */
2840 private final void handleAppDiedLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -07002841 boolean restarting, boolean allowRestart) {
2842 cleanUpApplicationRecordLocked(app, restarting, allowRestart, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002844 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 }
2846
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002847 if (mProfileProc == app) {
2848 clearProfilerLocked();
2849 }
2850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 // Just in case...
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08002852 if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) {
2853 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity);
2854 mMainStack.mPausingActivity = null;
2855 }
2856 if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) {
2857 mMainStack.mLastPausedActivity = null;
2858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859
2860 // Remove this application's activities from active lists.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002861 mMainStack.removeHistoryRecordsForAppLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862
2863 boolean atTop = true;
2864 boolean hasVisibleActivities = false;
2865
2866 // Clean out the history list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002867 int i = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002868 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 TAG, "Removing app " + app + " from history with " + i + " entries");
2870 while (i > 0) {
2871 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002872 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002873 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2875 if (r.app == app) {
2876 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002877 if (ActivityStack.DEBUG_ADD_REMOVE) {
2878 RuntimeException here = new RuntimeException("here");
2879 here.fillInStackTrace();
2880 Slog.i(TAG, "Removing activity " + r + " from stack at " + i
2881 + ": haveState=" + r.haveState
2882 + " stateNotNeeded=" + r.stateNotNeeded
2883 + " finishing=" + r.finishing
2884 + " state=" + r.state, here);
2885 }
2886 if (!r.finishing) {
2887 Slog.w(TAG, "Force removing " + r + ": app died, no saved state");
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -08002888 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
2889 System.identityHashCode(r),
2890 r.task.taskId, r.shortComponentName,
2891 "proc died without state saved");
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002892 }
Dianne Hackborn5c607432012-02-28 14:44:19 -08002893 mMainStack.removeActivityFromHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894
2895 } else {
2896 // We have the current state for this activity, so
2897 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002898 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 TAG, "Keeping entry, setting app to null");
2900 if (r.visible) {
2901 hasVisibleActivities = true;
2902 }
2903 r.app = null;
2904 r.nowVisible = false;
2905 if (!r.haveState) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07002906 if (ActivityStack.DEBUG_SAVED_STATE) Slog.i(TAG,
2907 "App died, clearing saved state of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 r.icicle = null;
2909 }
2910 }
2911
Dianne Hackbornce86ba82011-07-13 19:33:41 -07002912 r.stack.cleanUpActivityLocked(r, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 }
2914 atTop = false;
2915 }
2916
2917 app.activities.clear();
2918
2919 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002920 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 + " running instrumentation " + app.instrumentationClass);
2922 Bundle info = new Bundle();
2923 info.putString("shortMsg", "Process crashed.");
2924 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
2925 }
2926
2927 if (!restarting) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002928 if (!mMainStack.resumeTopActivityLocked(null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 // If there was nothing to resume, and we are not already
2930 // restarting this process, but there is a visible activity that
2931 // is hosted by the process... then make sure all visible
2932 // activities are running, taking care of restarting this
2933 // process.
2934 if (hasVisibleActivities) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002935 mMainStack.ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937 }
2938 }
2939 }
2940
2941 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
2942 IBinder threadBinder = thread.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002944 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2945 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
2947 return i;
2948 }
2949 }
2950 return -1;
2951 }
2952
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002953 final ProcessRecord getRecordForAppLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 IApplicationThread thread) {
2955 if (thread == null) {
2956 return null;
2957 }
2958
2959 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002960 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 }
2962
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002963 final void appDiedLocked(ProcessRecord app, int pid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 IApplicationThread thread) {
2965
2966 mProcDeaths[0]++;
2967
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002968 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
2969 synchronized (stats) {
2970 stats.noteProcessDiedLocked(app.info.uid, pid);
2971 }
2972
Magnus Edlund7bb25812010-02-24 15:45:06 +01002973 // Clean up already done if the process has been re-started.
2974 if (app.pid == pid && app.thread != null &&
2975 app.thread.asBinder() == thread.asBinder()) {
Dianne Hackborn906497c2010-05-10 15:57:38 -07002976 if (!app.killedBackground) {
2977 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
2978 + ") has died.");
2979 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08002980 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002981 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 TAG, "Dying app: " + app + ", pid: " + pid
2983 + ", thread: " + thread.asBinder());
2984 boolean doLowMem = app.instrumentationClass == null;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07002985 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986
2987 if (doLowMem) {
2988 // If there are no longer any background processes running,
2989 // and the app that died was not running instrumentation,
2990 // then tell everyone we are now low on memory.
2991 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002992 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2993 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn7d608422011-08-07 16:24:18 -07002994 if (rec.thread != null && rec.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 haveBg = true;
2996 break;
2997 }
2998 }
2999
3000 if (!haveBg) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003001 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003002 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003003 for (int i=mLruProcesses.size()-1; i>=0; i--) {
3004 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07003005 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003006 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
3007 // The low memory report is overriding any current
3008 // state for a GC request. Make sure to do
Dianne Hackborn32907cf2010-06-10 17:50:20 -07003009 // heavy/important/visible/foreground processes first.
Dianne Hackborn7d608422011-08-07 16:24:18 -07003010 if (rec.setAdj <= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003011 rec.lastRequestedGc = 0;
3012 } else {
3013 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003015 rec.reportLowMemory = true;
3016 rec.lastLowMemory = now;
3017 mProcessesToGc.remove(rec);
3018 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 }
3020 }
Dianne Hackborn04d6db32011-11-04 20:07:24 -07003021 mHandler.sendEmptyMessage(REPORT_MEM_USAGE);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003022 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 }
3024 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01003025 } else if (app.pid != pid) {
3026 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08003027 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01003028 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08003029 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003030 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003031 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 + thread.asBinder());
3033 }
3034 }
3035
Dan Egnor42471dd2010-01-07 17:25:22 -08003036 /**
3037 * If a stack trace dump file is configured, dump process stack traces.
Christopher Tate6ee412d2010-05-28 12:01:56 -07003038 * @param clearTraces causes the dump file to be erased prior to the new
3039 * traces being written, if true; when false, the new traces will be
3040 * appended to any existing file content.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003041 * @param firstPids of dalvik VM processes to dump stack traces for first
3042 * @param lastPids of dalvik VM processes to dump stack traces for last
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003043 * @param nativeProcs optional list of native process names to dump stack crawls
Dan Egnor42471dd2010-01-07 17:25:22 -08003044 * @return file containing stack traces, or null if no dump file is configured
3045 */
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003046 public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003047 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003048 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3049 if (tracesPath == null || tracesPath.length() == 0) {
3050 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003052
3053 File tracesFile = new File(tracesPath);
3054 try {
3055 File tracesDir = tracesFile.getParentFile();
rpcraigec7ed14c2012-07-25 13:10:37 -04003056 if (!tracesDir.exists()) {
3057 tracesFile.mkdirs();
3058 if (!SELinux.restorecon(tracesDir)) {
3059 return null;
3060 }
3061 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003062 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3063
Christopher Tate6ee412d2010-05-28 12:01:56 -07003064 if (clearTraces && tracesFile.exists()) tracesFile.delete();
Dan Egnor42471dd2010-01-07 17:25:22 -08003065 tracesFile.createNewFile();
3066 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3067 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003068 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08003069 return null;
3070 }
3071
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003072 dumpStackTraces(tracesPath, firstPids, processStats, lastPids, nativeProcs);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003073 return tracesFile;
3074 }
3075
3076 private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids,
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003077 ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003078 // Use a FileObserver to detect when traces finish writing.
3079 // The order of traces is considered important to maintain for legibility.
3080 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
3081 public synchronized void onEvent(int event, String path) { notify(); }
3082 };
3083
3084 try {
3085 observer.startWatching();
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003086
3087 // First collect all of the stacks of the most important pids.
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003088 if (firstPids != null) {
3089 try {
3090 int num = firstPids.size();
3091 for (int i = 0; i < num; i++) {
3092 synchronized (observer) {
3093 Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
3094 observer.wait(200); // Wait for write-close, give up after 200msec
3095 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003096 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003097 } catch (InterruptedException e) {
3098 Log.wtf(TAG, e);
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003099 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003100 }
3101
3102 // Next measure CPU usage.
3103 if (processStats != null) {
3104 processStats.init();
3105 System.gc();
3106 processStats.update();
3107 try {
3108 synchronized (processStats) {
3109 processStats.wait(500); // measure over 1/2 second.
3110 }
3111 } catch (InterruptedException e) {
3112 }
3113 processStats.update();
3114
3115 // We'll take the stack crawls of just the top apps using CPU.
3116 final int N = processStats.countWorkingStats();
3117 int numProcs = 0;
3118 for (int i=0; i<N && numProcs<5; i++) {
3119 ProcessStats.Stats stats = processStats.getWorkingStats(i);
3120 if (lastPids.indexOfKey(stats.pid) >= 0) {
3121 numProcs++;
3122 try {
3123 synchronized (observer) {
3124 Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
3125 observer.wait(200); // Wait for write-close, give up after 200msec
3126 }
3127 } catch (InterruptedException e) {
3128 Log.wtf(TAG, e);
3129 }
3130
3131 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003132 }
3133 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003134
Dan Egnor42471dd2010-01-07 17:25:22 -08003135 } finally {
3136 observer.stopWatching();
3137 }
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003138
3139 if (nativeProcs != null) {
3140 int[] pids = Process.getPidsForCommands(nativeProcs);
3141 if (pids != null) {
3142 for (int pid : pids) {
3143 Debug.dumpNativeBacktraceToFile(pid, tracesPath);
3144 }
3145 }
3146 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003149 final void logAppTooSlow(ProcessRecord app, long startTime, String msg) {
Dianne Hackborn69dc66e2012-03-26 10:50:54 -07003150 if (true || IS_USER_BUILD) {
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003151 return;
3152 }
3153 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
3154 if (tracesPath == null || tracesPath.length() == 0) {
3155 return;
3156 }
3157
3158 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
3159 StrictMode.allowThreadDiskWrites();
3160 try {
3161 final File tracesFile = new File(tracesPath);
3162 final File tracesDir = tracesFile.getParentFile();
3163 final File tracesTmp = new File(tracesDir, "__tmp__");
3164 try {
rpcraigec7ed14c2012-07-25 13:10:37 -04003165 if (!tracesDir.exists()) {
3166 tracesFile.mkdirs();
3167 if (!SELinux.restorecon(tracesDir.getPath())) {
3168 return;
3169 }
3170 }
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003171 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
3172
3173 if (tracesFile.exists()) {
3174 tracesTmp.delete();
3175 tracesFile.renameTo(tracesTmp);
3176 }
3177 StringBuilder sb = new StringBuilder();
3178 Time tobj = new Time();
3179 tobj.set(System.currentTimeMillis());
3180 sb.append(tobj.format("%Y-%m-%d %H:%M:%S"));
3181 sb.append(": ");
3182 TimeUtils.formatDuration(SystemClock.uptimeMillis()-startTime, sb);
3183 sb.append(" since ");
3184 sb.append(msg);
3185 FileOutputStream fos = new FileOutputStream(tracesFile);
3186 fos.write(sb.toString().getBytes());
3187 if (app == null) {
3188 fos.write("\n*** No application process!".getBytes());
3189 }
3190 fos.close();
3191 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
3192 } catch (IOException e) {
3193 Slog.w(TAG, "Unable to prepare slow app traces file: " + tracesPath, e);
3194 return;
3195 }
3196
3197 if (app != null) {
3198 ArrayList<Integer> firstPids = new ArrayList<Integer>();
3199 firstPids.add(app.pid);
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003200 dumpStackTraces(tracesPath, firstPids, null, null, null);
Dianne Hackborn2a29b3a2012-03-15 15:48:38 -07003201 }
3202
3203 File lastTracesFile = null;
3204 File curTracesFile = null;
3205 for (int i=9; i>=0; i--) {
3206 String name = String.format("slow%02d.txt", i);
3207 curTracesFile = new File(tracesDir, name);
3208 if (curTracesFile.exists()) {
3209 if (lastTracesFile != null) {
3210 curTracesFile.renameTo(lastTracesFile);
3211 } else {
3212 curTracesFile.delete();
3213 }
3214 }
3215 lastTracesFile = curTracesFile;
3216 }
3217 tracesFile.renameTo(curTracesFile);
3218 if (tracesTmp.exists()) {
3219 tracesTmp.renameTo(tracesFile);
3220 }
3221 } finally {
3222 StrictMode.setThreadPolicy(oldPolicy);
3223 }
3224 }
3225
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003226 final void appNotResponding(ProcessRecord app, ActivityRecord activity,
3227 ActivityRecord parent, final String annotation) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003228 ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
3229 SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
3230
Dianne Hackborn287952c2010-09-22 22:34:31 -07003231 if (mController != null) {
3232 try {
3233 // 0 == continue, -1 = kill process immediately
3234 int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
3235 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3236 } catch (RemoteException e) {
3237 mController = null;
3238 }
3239 }
3240
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003241 long anrTime = SystemClock.uptimeMillis();
3242 if (MONITOR_CPU_USAGE) {
3243 updateCpuStatsNow();
3244 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003245
3246 synchronized (this) {
3247 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
3248 if (mShuttingDown) {
3249 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
3250 return;
3251 } else if (app.notResponding) {
3252 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
3253 return;
3254 } else if (app.crashing) {
3255 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
3256 return;
3257 }
3258
3259 // In case we come through here for the same app before completing
3260 // this one, mark as anring now so we will bail out.
3261 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08003262
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003263 // Log the ANR to the event log.
3264 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
3265 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08003266
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003267 // Dump thread traces as quickly as we can, starting with "interesting" processes.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003268 firstPids.add(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003269
3270 int parentPid = app.pid;
3271 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003272 if (parentPid != app.pid) firstPids.add(parentPid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003273
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003274 if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08003275
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003276 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
3277 ProcessRecord r = mLruProcesses.get(i);
3278 if (r != null && r.thread != null) {
3279 int pid = r.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003280 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
3281 if (r.persistent) {
3282 firstPids.add(pid);
3283 } else {
3284 lastPids.put(pid, Boolean.TRUE);
3285 }
3286 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 }
3289 }
3290
Dan Egnor42471dd2010-01-07 17:25:22 -08003291 // Log the ANR to the main log.
Jeff Browndeb6ed82012-04-10 14:26:26 -07003292 StringBuilder info = new StringBuilder();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003293 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08003294 info.append("ANR in ").append(app.processName);
3295 if (activity != null && activity.shortComponentName != null) {
3296 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07003297 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08003298 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003300 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 }
Dan Egnor42471dd2010-01-07 17:25:22 -08003302 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08003303 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305
Dianne Hackborn287952c2010-09-22 22:34:31 -07003306 final ProcessStats processStats = new ProcessStats(true);
3307
Dianne Hackbornf72467a2012-06-08 17:23:59 -07003308 File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003309
Dan Egnor42471dd2010-01-07 17:25:22 -08003310 String cpuInfo = null;
3311 if (MONITOR_CPU_USAGE) {
3312 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003313 synchronized (mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003314 cpuInfo = mProcessStats.printCurrentState(anrTime);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003315 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003316 info.append(processStats.printCurrentLoad());
Dan Egnor42471dd2010-01-07 17:25:22 -08003317 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
3319
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07003320 info.append(processStats.printCurrentState(anrTime));
3321
Joe Onorato8a9b2202010-02-26 18:56:32 -08003322 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08003323 if (tracesFile == null) {
3324 // There is no trace file, so dump (only) the alleged culprit's threads to the log
3325 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
3326 }
3327
Jeff Sharkeya353d262011-10-28 11:12:06 -07003328 addErrorToDropBox("anr", app, app.processName, activity, parent, annotation,
3329 cpuInfo, tracesFile, null);
Dan Egnor42471dd2010-01-07 17:25:22 -08003330
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003331 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08003333 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
3334 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08003336 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
3337 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003340 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 }
3342 }
3343
Dan Egnor42471dd2010-01-07 17:25:22 -08003344 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
3345 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
3346 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003347
3348 synchronized (this) {
3349 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003350 Slog.w(TAG, "Killing " + app + ": background ANR");
3351 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
3352 app.processName, app.setAdj, "background ANR");
3353 Process.killProcessQuiet(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07003354 return;
3355 }
3356
3357 // Set the app's notResponding state, and look up the errorReportReceiver
3358 makeAppNotRespondingLocked(app,
3359 activity != null ? activity.shortComponentName : null,
3360 annotation != null ? "ANR " + annotation : "ANR",
3361 info.toString());
3362
3363 // Bring up the infamous App Not Responding dialog
3364 Message msg = Message.obtain();
3365 HashMap map = new HashMap();
3366 msg.what = SHOW_NOT_RESPONDING_MSG;
3367 msg.obj = map;
3368 map.put("app", app);
3369 if (activity != null) {
3370 map.put("activity", activity);
3371 }
3372
3373 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08003374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 }
3376
Dianne Hackborn0dad3642010-09-09 21:25:35 -07003377 final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
3378 if (!mLaunchWarningShown) {
3379 mLaunchWarningShown = true;
3380 mHandler.post(new Runnable() {
3381 @Override
3382 public void run() {
3383 synchronized (ActivityManagerService.this) {
3384 final Dialog d = new LaunchWarningWindow(mContext, cur, next);
3385 d.show();
3386 mHandler.postDelayed(new Runnable() {
3387 @Override
3388 public void run() {
3389 synchronized (ActivityManagerService.this) {
3390 d.dismiss();
3391 mLaunchWarningShown = false;
3392 }
3393 }
3394 }, 4000);
3395 }
3396 }
3397 });
3398 }
3399 }
3400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 public boolean clearApplicationUserData(final String packageName,
Amith Yamasani742a6712011-05-04 14:49:28 -07003402 final IPackageDataObserver observer, final int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003403 enforceNotIsolatedCaller("clearApplicationUserData");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 int uid = Binder.getCallingUid();
3405 int pid = Binder.getCallingPid();
3406 long callingId = Binder.clearCallingIdentity();
3407 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003408 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 int pkgUid = -1;
3410 synchronized(this) {
3411 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003412 pkgUid = pm.getPackageUid(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 } catch (RemoteException e) {
3414 }
3415 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003416 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 return false;
3418 }
3419 if (uid == pkgUid || checkComponentPermission(
3420 android.Manifest.permission.CLEAR_APP_USER_DATA,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08003421 pid, uid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003423 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 } else {
3425 throw new SecurityException(pid+" does not have permission:"+
3426 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
3427 "for process:"+packageName);
3428 }
3429 }
3430
3431 try {
3432 //clear application user data
Amith Yamasani483f3b02012-03-13 16:08:00 -07003433 pm.clearApplicationUserData(packageName, observer, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
3435 Uri.fromParts("package", packageName, null));
3436 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003437 broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
Amith Yamasani742a6712011-05-04 14:49:28 -07003438 null, null, 0, null, null, null, false, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 } catch (RemoteException e) {
3440 }
3441 } finally {
3442 Binder.restoreCallingIdentity(callingId);
3443 }
3444 return true;
3445 }
3446
Dianne Hackborn03abb812010-01-04 18:43:19 -08003447 public void killBackgroundProcesses(final String packageName) {
3448 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3449 != PackageManager.PERMISSION_GRANTED &&
3450 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
3451 != PackageManager.PERMISSION_GRANTED) {
3452 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453 + Binder.getCallingPid()
3454 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08003455 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003456 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 throw new SecurityException(msg);
3458 }
3459
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003460 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 long callingId = Binder.clearCallingIdentity();
3462 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003463 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 int pkgUid = -1;
3465 synchronized(this) {
3466 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003467 pkgUid = pm.getPackageUid(packageName, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 } catch (RemoteException e) {
3469 }
3470 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003471 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 return;
3473 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003474 killPackageProcessesLocked(packageName, pkgUid, -1,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003475 ProcessList.SERVICE_ADJ, false, true, true, false, "kill background");
3476 }
3477 } finally {
3478 Binder.restoreCallingIdentity(callingId);
3479 }
3480 }
3481
3482 public void killAllBackgroundProcesses() {
3483 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3484 != PackageManager.PERMISSION_GRANTED) {
3485 String msg = "Permission Denial: killAllBackgroundProcesses() from pid="
3486 + Binder.getCallingPid()
3487 + ", uid=" + Binder.getCallingUid()
3488 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
3489 Slog.w(TAG, msg);
3490 throw new SecurityException(msg);
3491 }
3492
3493 long callingId = Binder.clearCallingIdentity();
3494 try {
3495 synchronized(this) {
3496 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
3497 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3498 final int NA = apps.size();
3499 for (int ia=0; ia<NA; ia++) {
3500 ProcessRecord app = apps.valueAt(ia);
3501 if (app.persistent) {
3502 // we don't kill persistent processes
3503 continue;
3504 }
3505 if (app.removed) {
3506 procs.add(app);
3507 } else if (app.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
3508 app.removed = true;
3509 procs.add(app);
3510 }
3511 }
3512 }
3513
3514 int N = procs.size();
3515 for (int i=0; i<N; i++) {
3516 removeProcessLocked(procs.get(i), false, true, "kill all background");
3517 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003518 }
3519 } finally {
3520 Binder.restoreCallingIdentity(callingId);
3521 }
3522 }
3523
3524 public void forceStopPackage(final String packageName) {
3525 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
3526 != PackageManager.PERMISSION_GRANTED) {
3527 String msg = "Permission Denial: forceStopPackage() from pid="
3528 + Binder.getCallingPid()
3529 + ", uid=" + Binder.getCallingUid()
3530 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003531 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003532 throw new SecurityException(msg);
3533 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003534 final int userId = UserHandle.getCallingUserId();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003535 long callingId = Binder.clearCallingIdentity();
3536 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003537 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003538 int pkgUid = -1;
3539 synchronized(this) {
3540 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003541 pkgUid = pm.getPackageUid(packageName, userId);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003542 } catch (RemoteException e) {
3543 }
3544 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003545 Slog.w(TAG, "Invalid packageName: " + packageName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003546 return;
3547 }
3548 forceStopPackageLocked(packageName, pkgUid);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003549 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003550 pm.setPackageStoppedState(packageName, true, userId);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003551 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08003552 } catch (IllegalArgumentException e) {
3553 Slog.w(TAG, "Failed trying to unstop package "
3554 + packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08003555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557 } finally {
3558 Binder.restoreCallingIdentity(callingId);
3559 }
3560 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003561
3562 /*
3563 * The pkg name and uid have to be specified.
3564 * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
3565 */
3566 public void killApplicationWithUid(String pkg, int uid) {
3567 if (pkg == null) {
3568 return;
3569 }
3570 // Make sure the uid is valid.
3571 if (uid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003572 Slog.w(TAG, "Invalid uid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003573 return;
3574 }
3575 int callerUid = Binder.getCallingUid();
3576 // Only the system server can kill an application
3577 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003578 // Post an aysnc message to kill the application
3579 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
3580 msg.arg1 = uid;
3581 msg.arg2 = 0;
3582 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07003583 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003584 } else {
3585 throw new SecurityException(callerUid + " cannot kill pkg: " +
3586 pkg);
3587 }
3588 }
3589
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003590 public void closeSystemDialogs(String reason) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003591 enforceNotIsolatedCaller("closeSystemDialogs");
Dianne Hackborne302a162012-05-15 14:58:32 -07003592
3593 final int uid = Binder.getCallingUid();
3594 final long origId = Binder.clearCallingIdentity();
3595 synchronized (this) {
3596 closeSystemDialogsLocked(uid, reason);
3597 }
3598 Binder.restoreCallingIdentity(origId);
3599 }
3600
3601 void closeSystemDialogsLocked(int callingUid, String reason) {
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003602 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003603 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003604 if (reason != null) {
3605 intent.putExtra("reason", reason);
3606 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003607 mWindowManager.closeSystemDialogs(reason);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003608
Dianne Hackborne302a162012-05-15 14:58:32 -07003609 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
3610 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn1927ae82012-06-22 15:21:36 -07003611 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Dianne Hackborne302a162012-05-15 14:58:32 -07003612 r.stack.finishActivityLocked(r, i,
3613 Activity.RESULT_CANCELED, null, "close-sys");
Dianne Hackbornffa42482009-09-23 22:20:11 -07003614 }
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003615 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003616
3617 broadcastIntentLocked(null, null, intent, null,
3618 null, 0, null, null, null, false, false, -1,
3619 callingUid, 0 /* TODO: Verify */);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003620 }
Dianne Hackborne302a162012-05-15 14:58:32 -07003621
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003622 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003623 throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003624 enforceNotIsolatedCaller("getProcessMemoryInfo");
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003625 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
3626 for (int i=pids.length-1; i>=0; i--) {
3627 infos[i] = new Debug.MemoryInfo();
3628 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003629 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003630 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003631 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07003632
Dianne Hackbornb437e092011-08-05 17:50:29 -07003633 public long[] getProcessPss(int[] pids) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08003634 enforceNotIsolatedCaller("getProcessPss");
Dianne Hackbornb437e092011-08-05 17:50:29 -07003635 long[] pss = new long[pids.length];
3636 for (int i=pids.length-1; i>=0; i--) {
3637 pss[i] = Debug.getPss(pids[i]);
3638 }
3639 return pss;
3640 }
3641
Christopher Tate5e1ab332009-09-01 20:32:49 -07003642 public void killApplicationProcess(String processName, int uid) {
3643 if (processName == null) {
3644 return;
3645 }
3646
3647 int callerUid = Binder.getCallingUid();
3648 // Only the system server can kill an application
3649 if (callerUid == Process.SYSTEM_UID) {
3650 synchronized (this) {
3651 ProcessRecord app = getProcessRecordLocked(processName, uid);
Christopher Tate4a627c72011-04-01 14:43:32 -07003652 if (app != null && app.thread != null) {
Christopher Tate5e1ab332009-09-01 20:32:49 -07003653 try {
3654 app.thread.scheduleSuicide();
3655 } catch (RemoteException e) {
3656 // If the other end already died, then our work here is done.
3657 }
3658 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003659 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07003660 + processName + " / " + uid);
3661 }
3662 }
3663 } else {
3664 throw new SecurityException(callerUid + " cannot kill app process: " +
3665 processName);
3666 }
3667 }
3668
Dianne Hackborn03abb812010-01-04 18:43:19 -08003669 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003670 forceStopPackageLocked(packageName, uid, false, false, true, false,
3671 UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
3673 Uri.fromParts("package", packageName, null));
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003674 if (!mProcessesReady) {
3675 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 intent.putExtra(Intent.EXTRA_UID, uid);
3678 broadcastIntentLocked(null, null, intent,
3679 null, null, 0, null, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -07003680 false, false,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07003681 MY_PID, Process.SYSTEM_UID, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003683
3684 private void forceStopUserLocked(int userId) {
3685 forceStopPackageLocked(null, -1, false, false, true, false, userId);
3686 Intent intent = new Intent(Intent.ACTION_USER_STOPPED);
3687 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3688 intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
3689 broadcastIntentLocked(null, null, intent,
3690 null, null, 0, null, null, null,
3691 false, false,
3692 MY_PID, Process.SYSTEM_UID, userId);
3693 }
3694
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003695 private final boolean killPackageProcessesLocked(String packageName, int uid,
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003696 int userId, int minOomAdj, boolean callerWillRestart, boolean allowRestart,
3697 boolean doit, boolean evenPersistent, String reason) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003698 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699
Dianne Hackborn03abb812010-01-04 18:43:19 -08003700 // Remove all processes this package may have touched: all with the
3701 // same UID (except for the system or root user), and all whose name
3702 // matches the package name.
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003703 final String procNamePrefix = packageName != null ? (packageName + ":") : null;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003704 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3705 final int NA = apps.size();
3706 for (int ia=0; ia<NA; ia++) {
3707 ProcessRecord app = apps.valueAt(ia);
Christopher Tate3dacd842011-08-19 14:56:15 -07003708 if (app.persistent && !evenPersistent) {
Christopher Tate064d8422011-07-26 15:38:07 -07003709 // we don't kill persistent processes
3710 continue;
3711 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003712 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003713 if (doit) {
3714 procs.add(app);
3715 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003716 // If no package is specified, we call all processes under the
3717 // give user id.
3718 } else if (packageName == null) {
3719 if (app.userId == userId) {
3720 if (app.setAdj >= minOomAdj) {
3721 if (!doit) {
3722 return true;
3723 }
3724 app.removed = true;
3725 procs.add(app);
3726 }
3727 }
Amith Yamasani13593602012-03-22 16:16:17 -07003728 // If uid is specified and the uid and process name match
3729 // Or, the uid is not specified and the process name matches
3730 } else if (((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid)
Amith Yamasani34db3d62012-04-02 16:35:19 -07003731 || ((app.processName.equals(packageName)
3732 || app.processName.startsWith(procNamePrefix))
3733 && uid < 0))) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003734 if (app.setAdj >= minOomAdj) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003735 if (!doit) {
3736 return true;
3737 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003738 app.removed = true;
3739 procs.add(app);
3740 }
3741 }
3742 }
3743 }
3744
3745 int N = procs.size();
3746 for (int i=0; i<N; i++) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003747 removeProcessLocked(procs.get(i), callerWillRestart, allowRestart, reason);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003748 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003749 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003750 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003751
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003752 private final boolean forceStopPackageLocked(String name, int uid,
Christopher Tate3dacd842011-08-19 14:56:15 -07003753 boolean callerWillRestart, boolean purgeCache, boolean doit,
Amith Yamasani483f3b02012-03-13 16:08:00 -07003754 boolean evenPersistent, int userId) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07003755 int i;
3756 int N;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003758 if (uid < 0 && name != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07003760 uid = AppGlobals.getPackageManager().getPackageUid(name, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 } catch (RemoteException e) {
3762 }
3763 }
3764
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003765 if (doit) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003766 if (name != null) {
3767 Slog.i(TAG, "Force stopping package " + name + " uid=" + uid);
3768 } else {
3769 Slog.i(TAG, "Force stopping user " + userId);
3770 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003771
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003772 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
3773 while (badApps.hasNext()) {
3774 SparseArray<Long> ba = badApps.next();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003775 for (i=ba.size()-1; i>=0; i--) {
3776 boolean remove = false;
3777 final int entUid = ba.keyAt(i);
3778 if (name != null) {
3779 if (entUid == uid) {
3780 remove = true;
3781 }
3782 } else if (UserHandle.getUserId(entUid) == userId) {
3783 remove = true;
3784 }
3785 if (remove) {
3786 ba.removeAt(i);
3787 }
3788 }
3789 if (ba.size() == 0) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003790 badApps.remove();
3791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 }
3793 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003794
3795 boolean didSomething = killPackageProcessesLocked(name, uid,
3796 name == null ? userId : -1 , -100, callerWillRestart, false,
3797 doit, evenPersistent,
3798 name == null ? ("force stop user " + userId) : ("force stop " + name));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003800 TaskRecord lastTask = null;
3801 for (i=0; i<mMainStack.mHistory.size(); i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003802 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003803 final boolean samePackage = r.packageName.equals(name)
3804 || (name == null && r.userId == userId);
Amith Yamasani13593602012-03-22 16:16:17 -07003805 if (r.userId == userId
3806 && (samePackage || r.task == lastTask)
Christopher Tate3dacd842011-08-19 14:56:15 -07003807 && (r.app == null || evenPersistent || !r.app.persistent)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003808 if (!doit) {
Dianne Hackborn80a7ac12011-09-22 18:32:52 -07003809 if (r.finishing) {
3810 // If this activity is just finishing, then it is not
3811 // interesting as far as something to stop.
3812 continue;
3813 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003814 return true;
3815 }
3816 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003817 Slog.i(TAG, " Force finishing activity " + r);
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003818 if (samePackage) {
3819 if (r.app != null) {
3820 r.app.removed = true;
3821 }
3822 r.app = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 }
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003824 lastTask = r.task;
3825 if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07003826 null, "force-stop", true)) {
Dianne Hackborne5ba16a2011-09-09 12:35:29 -07003827 i--;
3828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 }
3830 }
3831
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003832 if (mServices.forceStopLocked(name, userId, evenPersistent, doit)) {
3833 if (!doit) {
3834 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003836 didSomething = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003838
3839 ArrayList<ContentProviderRecord> providers = new ArrayList<ContentProviderRecord>();
Amith Yamasani13593602012-03-22 16:16:17 -07003840 for (ContentProviderRecord provider : mProviderMap.getProvidersByClass(userId).values()) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003841 if ((name == null || provider.info.packageName.equals(name))
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003842 && (provider.proc == null || evenPersistent || !provider.proc.persistent)) {
3843 if (!doit) {
3844 return true;
3845 }
3846 didSomething = true;
3847 providers.add(provider);
3848 }
3849 }
3850
3851 N = providers.size();
3852 for (i=0; i<N; i++) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003853 removeDyingProviderLocked(null, providers.get(i), true);
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07003854 }
3855
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003856 if (doit) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07003857 if (purgeCache && name != null) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003858 AttributeCache ac = AttributeCache.instance();
3859 if (ac != null) {
3860 ac.removePackage(name);
3861 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003862 }
Dianne Hackborn38cc8962011-10-13 11:33:55 -07003863 if (mBooted) {
3864 mMainStack.resumeTopActivityLocked(null);
3865 mMainStack.scheduleIdleLocked();
3866 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003867 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003868
3869 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 }
3871
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003872 private final boolean removeProcessLocked(ProcessRecord app,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003873 boolean callerWillRestart, boolean allowRestart, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 final String name = app.processName;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003875 final int uid = app.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003876 if (DEBUG_PROCESSES) Slog.d(
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003877 TAG, "Force removing proc " + app.toShortString() + " (" + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 + "/" + uid + ")");
3879
3880 mProcessNames.remove(name, uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003881 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003882 if (mHeavyWeightProcess == app) {
3883 mHeavyWeightProcess = null;
3884 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 boolean needRestart = false;
3887 if (app.pid > 0 && app.pid != MY_PID) {
3888 int pid = app.pid;
3889 synchronized (mPidsSelfLocked) {
3890 mPidsSelfLocked.remove(pid);
3891 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3892 }
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003893 Slog.i(TAG, "Killing proc " + app.toShortString() + ": " + reason);
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003894 handleAppDiedLocked(app, true, allowRestart);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003895 mLruProcesses.remove(app);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08003896 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003898 if (app.persistent && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 if (!callerWillRestart) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003900 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003901 } else {
3902 needRestart = true;
3903 }
3904 }
3905 } else {
3906 mRemovedProcesses.add(app);
3907 }
3908
3909 return needRestart;
3910 }
3911
3912 private final void processStartTimedOutLocked(ProcessRecord app) {
3913 final int pid = app.pid;
3914 boolean gone = false;
3915 synchronized (mPidsSelfLocked) {
3916 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
3917 if (knownApp != null && knownApp.thread == null) {
3918 mPidsSelfLocked.remove(pid);
3919 gone = true;
3920 }
3921 }
3922
3923 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003924 Slog.w(TAG, "Process " + app + " failed to attach");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003925 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003926 app.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003927 mProcessNames.remove(app.processName, app.uid);
3928 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003929 if (mHeavyWeightProcess == app) {
3930 mHeavyWeightProcess = null;
3931 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3932 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003933 // Take care of any launching providers waiting for this process.
3934 checkAppInLaunchingProvidersLocked(app, true);
3935 // Take care of any services that are waiting for the process.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07003936 mServices.processStartTimedOutLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003937 EventLog.writeEvent(EventLogTags.AM_KILL, pid,
3938 app.processName, app.setAdj, "start timeout");
3939 Process.killProcessQuiet(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07003940 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003941 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07003942 try {
3943 IBackupManager bm = IBackupManager.Stub.asInterface(
3944 ServiceManager.getService(Context.BACKUP_SERVICE));
3945 bm.agentDisconnected(app.info.packageName);
3946 } catch (RemoteException e) {
3947 // Can't happen; the backup manager is local
3948 }
3949 }
Christopher Tatef46723b2012-01-26 14:19:24 -08003950 if (isPendingBroadcastProcessLocked(pid)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003951 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Christopher Tatef46723b2012-01-26 14:19:24 -08003952 skipPendingBroadcastLocked(pid);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003954 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003955 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 }
3957 }
3958
3959 private final boolean attachApplicationLocked(IApplicationThread thread,
3960 int pid) {
3961
3962 // Find the application record that is being attached... either via
3963 // the pid if we are running in multiple processes, or just pull the
3964 // next app record if we are emulating process with anonymous threads.
3965 ProcessRecord app;
3966 if (pid != MY_PID && pid >= 0) {
3967 synchronized (mPidsSelfLocked) {
3968 app = mPidsSelfLocked.get(pid);
3969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 } else {
3971 app = null;
3972 }
3973
3974 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003975 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003977 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 if (pid > 0 && pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003979 Process.killProcessQuiet(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 } else {
3981 try {
3982 thread.scheduleExit();
3983 } catch (Exception e) {
3984 // Ignore exceptions.
3985 }
3986 }
3987 return false;
3988 }
3989
3990 // If this application record is still attached to a previous
3991 // process, clean it up now.
3992 if (app.thread != null) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -07003993 handleAppDiedLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003994 }
3995
3996 // Tell the process all about itself.
3997
Joe Onorato8a9b2202010-02-26 18:56:32 -08003998 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 TAG, "Binding process pid " + pid + " to record " + app);
4000
4001 String processName = app.processName;
4002 try {
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07004003 AppDeathRecipient adr = new AppDeathRecipient(
4004 app, pid, thread);
4005 thread.asBinder().linkToDeath(adr, 0);
4006 app.deathRecipient = adr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 } catch (RemoteException e) {
4008 app.resetPackageList();
4009 startProcessLocked(app, "link fail", processName);
4010 return false;
4011 }
4012
Doug Zongker2bec3d42009-12-04 12:52:44 -08004013 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014
4015 app.thread = thread;
4016 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08004017 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
4018 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 app.forcingToForeground = null;
4020 app.foregroundServices = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07004021 app.hasShownUi = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 app.debugging = false;
4023
4024 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
4025
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004026 boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004027 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004029 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004030 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004031 }
4032
Joe Onorato8a9b2202010-02-26 18:56:32 -08004033 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 TAG, "New app record " + app
4035 + " thread=" + thread.asBinder() + " pid=" + pid);
4036 try {
4037 int testMode = IApplicationThread.DEBUG_OFF;
4038 if (mDebugApp != null && mDebugApp.equals(processName)) {
4039 testMode = mWaitForDebugger
4040 ? IApplicationThread.DEBUG_WAIT
4041 : IApplicationThread.DEBUG_ON;
4042 app.debugging = true;
4043 if (mDebugTransient) {
4044 mDebugApp = mOrigDebugApp;
4045 mWaitForDebugger = mOrigWaitForDebugger;
4046 }
4047 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004048 String profileFile = app.instrumentationProfileFile;
4049 ParcelFileDescriptor profileFd = null;
4050 boolean profileAutoStop = false;
4051 if (mProfileApp != null && mProfileApp.equals(processName)) {
4052 mProfileProc = app;
4053 profileFile = mProfileFile;
4054 profileFd = mProfileFd;
4055 profileAutoStop = mAutoStopProfiler;
4056 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08004057 boolean enableOpenGlTrace = false;
4058 if (mOpenGlTraceApp != null && mOpenGlTraceApp.equals(processName)) {
4059 enableOpenGlTrace = true;
4060 mOpenGlTraceApp = null;
4061 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004062
Christopher Tate181fafa2009-05-14 11:12:14 -07004063 // If the app is being launched for restore or full backup, set it up specially
4064 boolean isRestrictedBackupMode = false;
4065 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
4066 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
Christopher Tate75a99702011-05-18 16:28:19 -07004067 || (mBackupTarget.backupMode == BackupRecord.RESTORE_FULL)
Christopher Tate181fafa2009-05-14 11:12:14 -07004068 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
4069 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004070
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07004071 ensurePackageDexOpt(app.instrumentationInfo != null
4072 ? app.instrumentationInfo.packageName
4073 : app.info.packageName);
4074 if (app.instrumentationClass != null) {
4075 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004076 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004077 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004078 + processName + " with config " + mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004079 ApplicationInfo appInfo = app.instrumentationInfo != null
4080 ? app.instrumentationInfo : app.info;
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -07004081 app.compat = compatibilityInfoForPackageLocked(appInfo);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004082 if (profileFd != null) {
4083 profileFd = profileFd.dup();
4084 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004085 thread.bindApplication(processName, appInfo, providers,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004086 app.instrumentationClass, profileFile, profileFd, profileAutoStop,
Siva Velusamy92a8b222012-03-09 16:24:04 -08004087 app.instrumentationArguments, app.instrumentationWatcher, testMode,
4088 enableOpenGlTrace, isRestrictedBackupMode || !normalMode, app.persistent,
Dianne Hackborn813075a62011-11-14 17:45:19 -08004089 new Configuration(mConfiguration), app.compat, getCommonServicesLocked(),
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004090 mCoreSettingsObserver.getCoreSettingsLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004091 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004092 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 } catch (Exception e) {
4094 // todo: Yikes! What should we do? For now we will try to
4095 // start another process, but that could easily get us in
4096 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08004097 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098
4099 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -07004100 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 startProcessLocked(app, "bind fail", processName);
4102 return false;
4103 }
4104
4105 // Remove this record from the list of starting applications.
4106 mPersistentStartingProcesses.remove(app);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004107 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
4108 "Attach application locked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 mProcessesOnHold.remove(app);
4110
4111 boolean badApp = false;
4112 boolean didSomething = false;
4113
4114 // See if the top visible activity is waiting to run in this process...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004115 ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
Christopher Tate04c0af82010-06-07 18:35:20 -07004116 if (hr != null && normalMode) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004117 if (hr.app == null && app.uid == hr.info.applicationInfo.uid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 && processName.equals(hr.processName)) {
4119 try {
Mike Lockwood3a74bd32011-08-12 13:55:22 -07004120 if (mHeadless) {
4121 Slog.e(TAG, "Starting activities not supported on headless device: " + hr);
4122 } else if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 didSomething = true;
4124 }
4125 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004126 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 + hr.intent.getComponent().flattenToShortString(), e);
4128 badApp = true;
4129 }
4130 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004131 mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 }
4133 }
4134
4135 // Find any services that should be running in this process...
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004136 if (!badApp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 try {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07004138 didSomething |= mServices.attachApplicationLocked(app, processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 } catch (Exception e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 badApp = true;
4141 }
4142 }
4143
Christopher Tatef46723b2012-01-26 14:19:24 -08004144 // Check if a next-broadcast receiver is in this process...
4145 if (!badApp && isPendingBroadcastProcessLocked(pid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 try {
Christopher Tatef46723b2012-01-26 14:19:24 -08004147 didSomething = sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 } catch (Exception e) {
Christopher Tatef46723b2012-01-26 14:19:24 -08004149 // If the app died trying to launch the receiver we declare it 'bad'
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 badApp = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 }
4152 }
4153
Christopher Tate181fafa2009-05-14 11:12:14 -07004154 // Check whether the next backup agent is in this process...
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004155 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004156 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004157 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07004158 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004159 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo,
4160 compatibilityInfoForPackageLocked(mBackupTarget.appInfo),
4161 mBackupTarget.backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -07004162 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004163 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07004164 e.printStackTrace();
4165 }
4166 }
4167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 if (badApp) {
4169 // todo: Also need to kill application to deal with all
4170 // kinds of exceptions.
Dianne Hackborn130b0d22011-07-26 22:07:48 -07004171 handleAppDiedLocked(app, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 return false;
4173 }
4174
4175 if (!didSomething) {
4176 updateOomAdjLocked();
4177 }
4178
4179 return true;
4180 }
4181
4182 public final void attachApplication(IApplicationThread thread) {
4183 synchronized (this) {
4184 int callingPid = Binder.getCallingPid();
4185 final long origId = Binder.clearCallingIdentity();
4186 attachApplicationLocked(thread, callingPid);
4187 Binder.restoreCallingIdentity(origId);
4188 }
4189 }
4190
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004191 public final void activityIdle(IBinder token, Configuration config, boolean stopProfiling) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004193 ActivityRecord r = mMainStack.activityIdleInternal(token, false, config);
4194 if (stopProfiling) {
4195 synchronized (this) {
4196 if (mProfileProc == r.app) {
4197 if (mProfileFd != null) {
4198 try {
4199 mProfileFd.close();
4200 } catch (IOException e) {
4201 }
4202 clearProfilerLocked();
4203 }
4204 }
4205 }
4206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 Binder.restoreCallingIdentity(origId);
4208 }
4209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08004211 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004212 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 mWindowManager.enableScreenAfterBoot();
Jeff Brownc042ee22012-05-08 13:03:42 -07004214
4215 synchronized (this) {
4216 updateEventDispatchingLocked();
4217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 }
4219
Dianne Hackborn661cd522011-08-22 00:26:20 -07004220 public void showBootMessage(final CharSequence msg, final boolean always) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004221 enforceNotIsolatedCaller("showBootMessage");
Dianne Hackborn661cd522011-08-22 00:26:20 -07004222 mWindowManager.showBootMessage(msg, always);
4223 }
4224
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004225 public void dismissKeyguardOnNextActivity() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004226 enforceNotIsolatedCaller("dismissKeyguardOnNextActivity");
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004227 final long token = Binder.clearCallingIdentity();
4228 try {
4229 synchronized (this) {
4230 if (mLockScreenShown) {
4231 mLockScreenShown = false;
4232 comeOutOfSleepIfNeededLocked();
4233 }
4234 mMainStack.dismissKeyguardOnNextActivityLocked();
Dianne Hackborn1e88e982012-04-24 18:35:55 -07004235 }
Jeff Sharkey7ffaa982012-04-30 16:59:05 -07004236 } finally {
4237 Binder.restoreCallingIdentity(token);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07004238 }
4239 }
4240
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004241 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004242 IntentFilter pkgFilter = new IntentFilter();
4243 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
4244 pkgFilter.addDataScheme("package");
4245 mContext.registerReceiver(new BroadcastReceiver() {
4246 @Override
4247 public void onReceive(Context context, Intent intent) {
4248 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
4249 if (pkgs != null) {
4250 for (String pkg : pkgs) {
Vairavan Srinivasan61f07652010-07-22 13:36:40 -07004251 synchronized (ActivityManagerService.this) {
Amith Yamasani483f3b02012-03-13 16:08:00 -07004252 if (forceStopPackageLocked(pkg, -1, false, false, false, false, 0)) {
4253 setResultCode(Activity.RESULT_OK);
4254 return;
4255 }
4256 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004257 }
4258 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004259 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004260 }, pkgFilter);
Amith Yamasani13593602012-03-22 16:16:17 -07004261
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004262 synchronized (this) {
4263 // Ensure that any processes we had put on hold are now started
4264 // up.
4265 final int NP = mProcessesOnHold.size();
4266 if (NP > 0) {
4267 ArrayList<ProcessRecord> procs =
4268 new ArrayList<ProcessRecord>(mProcessesOnHold);
4269 for (int ip=0; ip<NP; ip++) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07004270 if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
4271 + procs.get(ip));
4272 startProcessLocked(procs.get(ip), "on-hold", null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004273 }
4274 }
4275
4276 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07004277 // Start looking for apps that are abusing wake locks.
4278 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07004279 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08004280 // Tell anyone interested that we are done booting!
Dianne Hackbornf4c454b2010-08-11 12:47:41 -07004281 SystemProperties.set("sys.boot_completed", "1");
Guang Zhu191713a2012-01-12 12:02:22 -08004282 SystemProperties.set("dev.bootcomplete", "1");
Dianne Hackborn80a4af22012-08-27 19:18:31 -07004283 for (int i=0; i<mStartedUsers.size(); i++) {
4284 UserStartedState uss = mStartedUsers.valueAt(i);
4285 if (uss.mState == UserStartedState.STATE_BOOTING) {
4286 uss.mState = UserStartedState.STATE_RUNNING;
4287 broadcastIntentLocked(null, null,
4288 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
4289 null, null, 0, null, null,
4290 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
4291 false, false, MY_PID, Process.SYSTEM_UID,
4292 mStartedUsers.keyAt(i));
4293 }
Amith Yamasani4860cfc2012-08-08 19:14:18 -07004294 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004295 }
4296 }
4297 }
4298
4299 final void ensureBootCompleted() {
4300 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004301 boolean enableScreen;
4302 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004303 booting = mBooting;
4304 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004305 enableScreen = !mBooted;
4306 mBooted = true;
4307 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004308
4309 if (booting) {
4310 finishBooting();
4311 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004312
4313 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004314 enableScreenAfterBoot();
4315 }
4316 }
4317
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004318 public final void activityPaused(IBinder token) {
4319 final long origId = Binder.clearCallingIdentity();
4320 mMainStack.activityPaused(token, false);
4321 Binder.restoreCallingIdentity(origId);
4322 }
4323
4324 public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail,
4325 CharSequence description) {
4326 if (localLOGV) Slog.v(
4327 TAG, "Activity stopped: token=" + token);
4328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 // Refuse possible leaked file descriptors
4330 if (icicle != null && icicle.hasFileDescriptors()) {
4331 throw new IllegalArgumentException("File descriptors passed in Bundle");
4332 }
4333
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004334 ActivityRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335
4336 final long origId = Binder.clearCallingIdentity();
4337
4338 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004339 r = mMainStack.isInStackLocked(token);
4340 if (r != null) {
4341 r.stack.activityStoppedLocked(r, icicle, thumbnail, description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 }
4343 }
4344
4345 if (r != null) {
4346 sendPendingThumbnail(r, null, null, null, false);
4347 }
4348
4349 trimApplications();
4350
4351 Binder.restoreCallingIdentity(origId);
4352 }
4353
4354 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004355 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004356 mMainStack.activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
4358
4359 public String getCallingPackage(IBinder token) {
4360 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004361 ActivityRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07004362 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 }
4364 }
4365
4366 public ComponentName getCallingActivity(IBinder token) {
4367 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004368 ActivityRecord r = getCallingRecordLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 return r != null ? r.intent.getComponent() : null;
4370 }
4371 }
4372
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004373 private ActivityRecord getCallingRecordLocked(IBinder token) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004374 ActivityRecord r = mMainStack.isInStackLocked(token);
4375 if (r == null) {
4376 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004378 return r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 }
4380
4381 public ComponentName getActivityClassForToken(IBinder token) {
4382 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004383 ActivityRecord r = mMainStack.isInStackLocked(token);
4384 if (r == null) {
4385 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004387 return r.intent.getComponent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 }
4389 }
4390
4391 public String getPackageForToken(IBinder token) {
4392 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004393 ActivityRecord r = mMainStack.isInStackLocked(token);
4394 if (r == null) {
4395 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004397 return r.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 }
4399 }
4400
4401 public IIntentSender getIntentSender(int type,
4402 String packageName, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004403 int requestCode, Intent[] intents, String[] resolvedTypes,
4404 int flags, Bundle options) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004405 enforceNotIsolatedCaller("getIntentSender");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 // Refuse possible leaked file descriptors
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004407 if (intents != null) {
4408 if (intents.length < 1) {
4409 throw new IllegalArgumentException("Intents array length must be >= 1");
4410 }
4411 for (int i=0; i<intents.length; i++) {
4412 Intent intent = intents[i];
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004413 if (intent != null) {
4414 if (intent.hasFileDescriptors()) {
4415 throw new IllegalArgumentException("File descriptors passed in Intent");
4416 }
Dianne Hackborna4972e92012-03-14 10:38:05 -07004417 if (type == ActivityManager.INTENT_SENDER_BROADCAST &&
Dianne Hackborn52b0ce02011-04-14 13:09:32 -07004418 (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
4419 throw new IllegalArgumentException(
4420 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
4421 }
4422 intents[i] = new Intent(intent);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004423 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004424 }
4425 if (resolvedTypes != null && resolvedTypes.length != intents.length) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004426 throw new IllegalArgumentException(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004427 "Intent array length does not match resolvedTypes length");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004428 }
4429 }
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004430 if (options != null) {
4431 if (options.hasFileDescriptors()) {
4432 throw new IllegalArgumentException("File descriptors passed in options");
4433 }
4434 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07004435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 synchronized(this) {
4437 int callingUid = Binder.getCallingUid();
4438 try {
Jeff Brown10e89712011-07-08 18:52:57 -07004439 if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004440 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004441 .getPackageUid(packageName, UserHandle.getUserId(callingUid));
4442 if (!UserHandle.isSameApp(callingUid, uid)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 String msg = "Permission Denial: getIntentSender() from pid="
4444 + Binder.getCallingPid()
4445 + ", uid=" + Binder.getCallingUid()
4446 + ", (need uid=" + uid + ")"
4447 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004448 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 throw new SecurityException(msg);
4450 }
4451 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004452
Amith Yamasani742a6712011-05-04 14:49:28 -07004453 if (DEBUG_MU)
4454 Slog.i(TAG_MU, "Getting intent sender for origCallingUid="
4455 + Binder.getOrigCallingUid());
4456 return getIntentSenderLocked(type, packageName, Binder.getOrigCallingUid(),
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004457 token, resultWho, requestCode, intents, resolvedTypes, flags, options);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 } catch (RemoteException e) {
4460 throw new SecurityException(e);
4461 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004462 }
4463 }
4464
4465 IIntentSender getIntentSenderLocked(int type,
4466 String packageName, int callingUid, IBinder token, String resultWho,
Dianne Hackborn7a2195c2012-03-19 17:38:00 -07004467 int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
4468 Bundle options) {
Amith Yamasani742a6712011-05-04 14:49:28 -07004469 if (DEBUG_MU)
4470 Slog.v(TAG_MU, "getIntentSenderLocked(): uid=" + callingUid);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004471 ActivityRecord activity = null;
Dianne Hackborna4972e92012-03-14 10:38:05 -07004472 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004473 activity = mMainStack.isInStackLocked(token);
4474 if (activity == null) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004475 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004477 if (activity.finishing) {
4478 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004480 }
4481
4482 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
4483 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
4484 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
4485 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
4486 |PendingIntent.FLAG_UPDATE_CURRENT);
4487
4488 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
4489 type, packageName, activity, resultWho,
Amith Yamasani4ea60692012-08-28 14:34:53 -07004490 requestCode, intents, resolvedTypes, flags, options,
4491 UserHandle.getUserId(callingUid));
Dianne Hackborn860755f2010-06-03 18:47:52 -07004492 WeakReference<PendingIntentRecord> ref;
4493 ref = mIntentSenderRecords.get(key);
4494 PendingIntentRecord rec = ref != null ? ref.get() : null;
4495 if (rec != null) {
4496 if (!cancelCurrent) {
4497 if (updateCurrent) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004498 if (rec.key.requestIntent != null) {
Adam Powell501d4a52012-04-30 15:03:57 -07004499 rec.key.requestIntent.replaceExtras(intents != null ?
4500 intents[intents.length - 1] : null);
Dianne Hackborn621e17d2010-11-22 15:59:56 -08004501 }
4502 if (intents != null) {
4503 intents[intents.length-1] = rec.key.requestIntent;
4504 rec.key.allIntents = intents;
4505 rec.key.allResolvedTypes = resolvedTypes;
4506 } else {
4507 rec.key.allIntents = null;
4508 rec.key.allResolvedTypes = null;
4509 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 return rec;
4512 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004513 rec.canceled = true;
4514 mIntentSenderRecords.remove(key);
4515 }
4516 if (noCreate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 return rec;
4518 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004519 rec = new PendingIntentRecord(this, key, callingUid);
4520 mIntentSenderRecords.put(key, rec.ref);
Dianne Hackborna4972e92012-03-14 10:38:05 -07004521 if (type == ActivityManager.INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07004522 if (activity.pendingResults == null) {
4523 activity.pendingResults
4524 = new HashSet<WeakReference<PendingIntentRecord>>();
4525 }
4526 activity.pendingResults.add(rec.ref);
4527 }
4528 return rec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 }
4530
4531 public void cancelIntentSender(IIntentSender sender) {
4532 if (!(sender instanceof PendingIntentRecord)) {
4533 return;
4534 }
4535 synchronized(this) {
4536 PendingIntentRecord rec = (PendingIntentRecord)sender;
4537 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004538 int uid = AppGlobals.getPackageManager()
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004539 .getPackageUid(rec.key.packageName, UserHandle.getCallingUserId());
4540 if (!UserHandle.isSameApp(uid, Binder.getCallingUid())) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 String msg = "Permission Denial: cancelIntentSender() from pid="
4542 + Binder.getCallingPid()
4543 + ", uid=" + Binder.getCallingUid()
4544 + " is not allowed to cancel packges "
4545 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004546 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 throw new SecurityException(msg);
4548 }
4549 } catch (RemoteException e) {
4550 throw new SecurityException(e);
4551 }
4552 cancelIntentSenderLocked(rec, true);
4553 }
4554 }
4555
4556 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
4557 rec.canceled = true;
4558 mIntentSenderRecords.remove(rec.key);
4559 if (cleanActivity && rec.key.activity != null) {
4560 rec.key.activity.pendingResults.remove(rec.ref);
4561 }
4562 }
4563
4564 public String getPackageForIntentSender(IIntentSender pendingResult) {
4565 if (!(pendingResult instanceof PendingIntentRecord)) {
4566 return null;
4567 }
Brad Fitzpatrickb213d102010-04-19 11:58:52 -07004568 try {
4569 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4570 return res.key.packageName;
4571 } catch (ClassCastException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 }
4573 return null;
4574 }
4575
Christopher Tatec4a07d12012-04-06 14:19:13 -07004576 public int getUidForIntentSender(IIntentSender sender) {
4577 if (sender instanceof PendingIntentRecord) {
4578 try {
4579 PendingIntentRecord res = (PendingIntentRecord)sender;
4580 return res.uid;
4581 } catch (ClassCastException e) {
4582 }
4583 }
4584 return -1;
4585 }
4586
Dianne Hackborn6c418d52011-06-29 14:05:33 -07004587 public boolean isIntentSenderTargetedToPackage(IIntentSender pendingResult) {
4588 if (!(pendingResult instanceof PendingIntentRecord)) {
4589 return false;
4590 }
4591 try {
4592 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4593 if (res.key.allIntents == null) {
4594 return false;
4595 }
4596 for (int i=0; i<res.key.allIntents.length; i++) {
4597 Intent intent = res.key.allIntents[i];
4598 if (intent.getPackage() != null && intent.getComponent() != null) {
4599 return false;
4600 }
4601 }
4602 return true;
4603 } catch (ClassCastException e) {
4604 }
4605 return false;
4606 }
4607
Dianne Hackborn1927ae82012-06-22 15:21:36 -07004608 public boolean isIntentSenderAnActivity(IIntentSender pendingResult) {
4609 if (!(pendingResult instanceof PendingIntentRecord)) {
4610 return false;
4611 }
4612 try {
4613 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4614 if (res.key.type == ActivityManager.INTENT_SENDER_ACTIVITY) {
4615 return true;
4616 }
4617 return false;
4618 } catch (ClassCastException e) {
4619 }
4620 return false;
4621 }
4622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 public void setProcessLimit(int max) {
4624 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4625 "setProcessLimit()");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004626 synchronized (this) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07004627 mProcessLimit = max < 0 ? ProcessList.MAX_HIDDEN_APPS : max;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004628 mProcessLimitOverride = max;
4629 }
4630 trimApplications();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 }
4632
4633 public int getProcessLimit() {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004634 synchronized (this) {
4635 return mProcessLimitOverride;
4636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 }
4638
4639 void foregroundTokenDied(ForegroundToken token) {
4640 synchronized (ActivityManagerService.this) {
4641 synchronized (mPidsSelfLocked) {
4642 ForegroundToken cur
4643 = mForegroundProcesses.get(token.pid);
4644 if (cur != token) {
4645 return;
4646 }
4647 mForegroundProcesses.remove(token.pid);
4648 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
4649 if (pr == null) {
4650 return;
4651 }
4652 pr.forcingToForeground = null;
4653 pr.foregroundServices = false;
4654 }
4655 updateOomAdjLocked();
4656 }
4657 }
4658
4659 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
4660 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4661 "setProcessForeground()");
4662 synchronized(this) {
4663 boolean changed = false;
4664
4665 synchronized (mPidsSelfLocked) {
4666 ProcessRecord pr = mPidsSelfLocked.get(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004667 if (pr == null && isForeground) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004668 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 return;
4670 }
4671 ForegroundToken oldToken = mForegroundProcesses.get(pid);
4672 if (oldToken != null) {
4673 oldToken.token.unlinkToDeath(oldToken, 0);
4674 mForegroundProcesses.remove(pid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004675 if (pr != null) {
4676 pr.forcingToForeground = null;
4677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 changed = true;
4679 }
4680 if (isForeground && token != null) {
4681 ForegroundToken newToken = new ForegroundToken() {
4682 public void binderDied() {
4683 foregroundTokenDied(this);
4684 }
4685 };
4686 newToken.pid = pid;
4687 newToken.token = token;
4688 try {
4689 token.linkToDeath(newToken, 0);
4690 mForegroundProcesses.put(pid, newToken);
4691 pr.forcingToForeground = token;
4692 changed = true;
4693 } catch (RemoteException e) {
4694 // If the process died while doing this, we will later
4695 // do the cleanup with the process death link.
4696 }
4697 }
4698 }
4699
4700 if (changed) {
4701 updateOomAdjLocked();
4702 }
4703 }
4704 }
4705
4706 // =========================================================
4707 // PERMISSIONS
4708 // =========================================================
4709
4710 static class PermissionController extends IPermissionController.Stub {
4711 ActivityManagerService mActivityManagerService;
4712 PermissionController(ActivityManagerService activityManagerService) {
4713 mActivityManagerService = activityManagerService;
4714 }
4715
4716 public boolean checkPermission(String permission, int pid, int uid) {
4717 return mActivityManagerService.checkPermission(permission, pid,
4718 uid) == PackageManager.PERMISSION_GRANTED;
4719 }
4720 }
4721
4722 /**
4723 * This can be called with or without the global lock held.
4724 */
4725 int checkComponentPermission(String permission, int pid, int uid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08004726 int owningUid, boolean exported) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 // We might be performing an operation on behalf of an indirect binder
4728 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
4729 // client identity accordingly before proceeding.
4730 Identity tlsIdentity = sCallerIdentity.get();
4731 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004732 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004733 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
4734 uid = tlsIdentity.uid;
4735 pid = tlsIdentity.pid;
4736 }
4737
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004738 if (pid == MY_PID) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004739 return PackageManager.PERMISSION_GRANTED;
4740 }
Dianne Hackborn5320eb82012-05-18 12:05:04 -07004741
4742 return ActivityManager.checkComponentPermission(permission, uid,
4743 owningUid, exported);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 }
4745
4746 /**
4747 * As the only public entry point for permissions checking, this method
4748 * can enforce the semantic that requesting a check on a null global
4749 * permission is automatically denied. (Internally a null permission
4750 * string is used when calling {@link #checkComponentPermission} in cases
4751 * when only uid-based security is needed.)
4752 *
4753 * This can be called with or without the global lock held.
4754 */
4755 public int checkPermission(String permission, int pid, int uid) {
4756 if (permission == null) {
4757 return PackageManager.PERMISSION_DENIED;
4758 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004759 return checkComponentPermission(permission, pid, UserHandle.getAppId(uid), -1, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 }
4761
4762 /**
4763 * Binder IPC calls go through the public entry point.
4764 * This can be called with or without the global lock held.
4765 */
4766 int checkCallingPermission(String permission) {
4767 return checkPermission(permission,
4768 Binder.getCallingPid(),
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004769 UserHandle.getAppId(Binder.getCallingUid()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 }
4771
4772 /**
4773 * This can be called with or without the global lock held.
4774 */
4775 void enforceCallingPermission(String permission, String func) {
4776 if (checkCallingPermission(permission)
4777 == PackageManager.PERMISSION_GRANTED) {
4778 return;
4779 }
4780
4781 String msg = "Permission Denial: " + func + " from pid="
4782 + Binder.getCallingPid()
4783 + ", uid=" + Binder.getCallingUid()
4784 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004785 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786 throw new SecurityException(msg);
4787 }
4788
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004789 /**
4790 * Determine if UID is holding permissions required to access {@link Uri} in
4791 * the given {@link ProviderInfo}. Final permission checking is always done
4792 * in {@link ContentProvider}.
4793 */
4794 private final boolean checkHoldingPermissionsLocked(
4795 IPackageManager pm, ProviderInfo pi, Uri uri, int uid, int modeFlags) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004796 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4797 "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid);
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004798
4799 if (pi.applicationInfo.uid == uid) {
4800 return true;
4801 } else if (!pi.exported) {
4802 return false;
4803 }
4804
4805 boolean readMet = (modeFlags & Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
4806 boolean writeMet = (modeFlags & Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004807 try {
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004808 // check if target holds top-level <provider> permissions
4809 if (!readMet && pi.readPermission != null
4810 && (pm.checkUidPermission(pi.readPermission, uid) == PERMISSION_GRANTED)) {
4811 readMet = true;
4812 }
4813 if (!writeMet && pi.writePermission != null
4814 && (pm.checkUidPermission(pi.writePermission, uid) == PERMISSION_GRANTED)) {
4815 writeMet = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004817
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004818 // track if unprotected read/write is allowed; any denied
4819 // <path-permission> below removes this ability
4820 boolean allowDefaultRead = pi.readPermission == null;
4821 boolean allowDefaultWrite = pi.writePermission == null;
Dianne Hackborn48058e82010-09-27 16:53:23 -07004822
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004823 // check if target holds any <path-permission> that match uri
4824 final PathPermission[] pps = pi.pathPermissions;
4825 if (pps != null) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004826 final String path = uri.getPath();
4827 int i = pps.length;
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004828 while (i > 0 && (!readMet || !writeMet)) {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004829 i--;
4830 PathPermission pp = pps[i];
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004831 if (pp.match(path)) {
4832 if (!readMet) {
4833 final String pprperm = pp.getReadPermission();
4834 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
4835 + pprperm + " for " + pp.getPath()
4836 + ": match=" + pp.match(path)
4837 + " check=" + pm.checkUidPermission(pprperm, uid));
4838 if (pprperm != null) {
4839 if (pm.checkUidPermission(pprperm, uid) == PERMISSION_GRANTED) {
4840 readMet = true;
4841 } else {
4842 allowDefaultRead = false;
4843 }
4844 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004845 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004846 if (!writeMet) {
4847 final String ppwperm = pp.getWritePermission();
4848 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
4849 + ppwperm + " for " + pp.getPath()
4850 + ": match=" + pp.match(path)
4851 + " check=" + pm.checkUidPermission(ppwperm, uid));
4852 if (ppwperm != null) {
4853 if (pm.checkUidPermission(ppwperm, uid) == PERMISSION_GRANTED) {
4854 writeMet = true;
4855 } else {
4856 allowDefaultWrite = false;
4857 }
4858 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004859 }
4860 }
4861 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004862 }
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004863
4864 // grant unprotected <provider> read/write, if not blocked by
4865 // <path-permission> above
4866 if (allowDefaultRead) readMet = true;
4867 if (allowDefaultWrite) writeMet = true;
4868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 } catch (RemoteException e) {
4870 return false;
4871 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004872
Jeff Sharkey110a6b62012-03-12 11:12:41 -07004873 return readMet && writeMet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 }
4875
4876 private final boolean checkUriPermissionLocked(Uri uri, int uid,
4877 int modeFlags) {
4878 // Root gets to do everything.
Jeff Brown10e89712011-07-08 18:52:57 -07004879 if (uid == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 return true;
4881 }
4882 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
4883 if (perms == null) return false;
4884 UriPermission perm = perms.get(uri);
4885 if (perm == null) return false;
4886 return (modeFlags&perm.modeFlags) == modeFlags;
4887 }
4888
4889 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08004890 enforceNotIsolatedCaller("checkUriPermission");
4891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 // Another redirected-binder-call permissions check as in
4893 // {@link checkComponentPermission}.
4894 Identity tlsIdentity = sCallerIdentity.get();
4895 if (tlsIdentity != null) {
4896 uid = tlsIdentity.uid;
4897 pid = tlsIdentity.pid;
4898 }
4899
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004900 uid = UserHandle.getAppId(uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901 // Our own process gets to do everything.
4902 if (pid == MY_PID) {
4903 return PackageManager.PERMISSION_GRANTED;
4904 }
4905 synchronized(this) {
4906 return checkUriPermissionLocked(uri, uid, modeFlags)
4907 ? PackageManager.PERMISSION_GRANTED
4908 : PackageManager.PERMISSION_DENIED;
4909 }
4910 }
4911
Dianne Hackborn39792d22010-08-19 18:01:52 -07004912 /**
4913 * Check if the targetPkg can be granted permission to access uri by
4914 * the callingUid using the given modeFlags. Throws a security exception
4915 * if callingUid is not allowed to do this. Returns the uid of the target
4916 * if the URI permission grant should be performed; returns -1 if it is not
4917 * needed (for example targetPkg already has permission to access the URI).
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004918 * If you already know the uid of the target, you can supply it in
4919 * lastTargetUid else set that to -1.
Dianne Hackborn39792d22010-08-19 18:01:52 -07004920 */
4921 int checkGrantUriPermissionLocked(int callingUid, String targetPkg,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004922 Uri uri, int modeFlags, int lastTargetUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4924 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4925 if (modeFlags == 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004926 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 }
4928
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004929 if (targetPkg != null) {
4930 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4931 "Checking grant " + targetPkg + " permission to " + uri);
4932 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004933
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004934 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935
4936 // If this is not a content: uri, we can't do anything with it.
4937 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004938 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004939 "Can't grant URI permission for non-content URI: " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004940 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942
4943 String name = uri.getAuthority();
4944 ProviderInfo pi = null;
Amith Yamasani742a6712011-05-04 14:49:28 -07004945 ContentProviderRecord cpr = mProviderMap.getProviderByName(name,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004946 UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 if (cpr != null) {
4948 pi = cpr.info;
4949 } else {
4950 try {
4951 pi = pm.resolveContentProvider(name,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004952 PackageManager.GET_URI_PERMISSION_PATTERNS, UserHandle.getUserId(callingUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004953 } catch (RemoteException ex) {
4954 }
4955 }
4956 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07004957 Slog.w(TAG, "No content provider found for permission check: " + uri.toSafeString());
Dianne Hackborn39792d22010-08-19 18:01:52 -07004958 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
4960
Dianne Hackborn21c241e2012-03-08 13:57:23 -08004961 int targetUid = lastTargetUid;
4962 if (targetUid < 0 && targetPkg != null) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004963 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07004964 targetUid = pm.getPackageUid(targetPkg, UserHandle.getUserId(callingUid));
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004965 if (targetUid < 0) {
4966 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4967 "Can't grant URI permission no uid for: " + targetPkg);
4968 return -1;
4969 }
4970 } catch (RemoteException ex) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004971 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973 }
4974
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004975 if (targetUid >= 0) {
4976 // First... does the target actually need this permission?
4977 if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) {
4978 // No need to grant the target this permission.
4979 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4980 "Target " + targetPkg + " already has full permission to " + uri);
4981 return -1;
4982 }
4983 } else {
4984 // First... there is no target package, so can anyone access it?
4985 boolean allowed = pi.exported;
4986 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
4987 if (pi.readPermission != null) {
4988 allowed = false;
4989 }
4990 }
4991 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
4992 if (pi.writePermission != null) {
4993 allowed = false;
4994 }
4995 }
4996 if (allowed) {
4997 return -1;
4998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
5000
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005001 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005002 if (!pi.grantUriPermissions) {
5003 throw new SecurityException("Provider " + pi.packageName
5004 + "/" + pi.name
5005 + " does not allow granting of Uri permissions (uri "
5006 + uri + ")");
5007 }
5008 if (pi.uriPermissionPatterns != null) {
5009 final int N = pi.uriPermissionPatterns.length;
5010 boolean allowed = false;
5011 for (int i=0; i<N; i++) {
5012 if (pi.uriPermissionPatterns[i] != null
5013 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
5014 allowed = true;
5015 break;
5016 }
5017 }
5018 if (!allowed) {
5019 throw new SecurityException("Provider " + pi.packageName
5020 + "/" + pi.name
5021 + " does not allow granting of permission to path of Uri "
5022 + uri);
5023 }
5024 }
5025
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005026 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 // this uri?
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005028 if (callingUid != Process.myUid()) {
5029 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
5030 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
5031 throw new SecurityException("Uid " + callingUid
5032 + " does not have permission to uri " + uri);
5033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005034 }
5035 }
5036
Dianne Hackborn39792d22010-08-19 18:01:52 -07005037 return targetUid;
5038 }
5039
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005040 public int checkGrantUriPermission(int callingUid, String targetPkg,
5041 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005042 enforceNotIsolatedCaller("checkGrantUriPermission");
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005043 synchronized(this) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005044 return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005045 }
5046 }
5047
Dianne Hackborn39792d22010-08-19 18:01:52 -07005048 void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg,
5049 Uri uri, int modeFlags, UriPermissionOwner owner) {
5050 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5051 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5052 if (modeFlags == 0) {
5053 return;
5054 }
5055
5056 // So here we are: the caller has the assumed permission
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005057 // to the uri, and the target doesn't. Let's now give this to
5058 // the target.
5059
Joe Onorato8a9b2202010-02-26 18:56:32 -08005060 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07005061 "Granting " + targetPkg + "/" + targetUid + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 HashMap<Uri, UriPermission> targetUris
5064 = mGrantedUriPermissions.get(targetUid);
5065 if (targetUris == null) {
5066 targetUris = new HashMap<Uri, UriPermission>();
5067 mGrantedUriPermissions.put(targetUid, targetUris);
5068 }
5069
5070 UriPermission perm = targetUris.get(uri);
5071 if (perm == null) {
5072 perm = new UriPermission(targetUid, uri);
5073 targetUris.put(uri, perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07005075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 perm.modeFlags |= modeFlags;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005077 if (owner == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 perm.globalModeFlags |= modeFlags;
Vairavan Srinivasan91c12c22011-01-21 18:26:06 -08005079 } else {
5080 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
5081 perm.readOwners.add(owner);
5082 owner.addReadPermission(perm);
5083 }
5084 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
5085 perm.writeOwners.add(owner);
5086 owner.addWritePermission(perm);
5087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 }
5089 }
5090
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005091 void grantUriPermissionLocked(int callingUid, String targetPkg, Uri uri,
5092 int modeFlags, UriPermissionOwner owner) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005093 if (targetPkg == null) {
5094 throw new NullPointerException("targetPkg");
5095 }
5096
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005097 int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags, -1);
Dianne Hackborn39792d22010-08-19 18:01:52 -07005098 if (targetUid < 0) {
5099 return;
5100 }
5101
5102 grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner);
5103 }
5104
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005105 static class NeededUriGrants extends ArrayList<Uri> {
5106 final String targetPkg;
5107 final int targetUid;
5108 final int flags;
5109
5110 NeededUriGrants(String _targetPkg, int _targetUid, int _flags) {
5111 targetPkg = _targetPkg;
5112 targetUid = _targetUid;
5113 flags = _flags;
5114 }
5115 }
5116
Dianne Hackborn39792d22010-08-19 18:01:52 -07005117 /**
5118 * Like checkGrantUriPermissionLocked, but takes an Intent.
5119 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005120 NeededUriGrants checkGrantUriPermissionFromIntentLocked(int callingUid,
5121 String targetPkg, Intent intent, int mode, NeededUriGrants needed) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07005122 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005123 "Checking URI perm to data=" + (intent != null ? intent.getData() : null)
5124 + " clip=" + (intent != null ? intent.getClipData() : null)
Dianne Hackbornb424b632010-08-18 15:59:05 -07005125 + " from " + intent + "; flags=0x"
5126 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
5127
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07005128 if (targetPkg == null) {
5129 throw new NullPointerException("targetPkg");
5130 }
5131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 if (intent == null) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005133 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005134 }
5135 Uri data = intent.getData();
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005136 ClipData clip = intent.getClipData();
5137 if (data == null && clip == null) {
5138 return null;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005139 }
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005140 if (data != null) {
5141 int target = checkGrantUriPermissionLocked(callingUid, targetPkg, data,
5142 mode, needed != null ? needed.targetUid : -1);
5143 if (target > 0) {
5144 if (needed == null) {
5145 needed = new NeededUriGrants(targetPkg, target, mode);
5146 }
5147 needed.add(data);
5148 }
5149 }
5150 if (clip != null) {
5151 for (int i=0; i<clip.getItemCount(); i++) {
5152 Uri uri = clip.getItemAt(i).getUri();
5153 if (uri != null) {
5154 int target = -1;
5155 target = checkGrantUriPermissionLocked(callingUid, targetPkg, uri,
5156 mode, needed != null ? needed.targetUid : -1);
5157 if (target > 0) {
5158 if (needed == null) {
5159 needed = new NeededUriGrants(targetPkg, target, mode);
5160 }
5161 needed.add(uri);
5162 }
5163 } else {
5164 Intent clipIntent = clip.getItemAt(i).getIntent();
5165 if (clipIntent != null) {
5166 NeededUriGrants newNeeded = checkGrantUriPermissionFromIntentLocked(
5167 callingUid, targetPkg, clipIntent, mode, needed);
5168 if (newNeeded != null) {
5169 needed = newNeeded;
5170 }
5171 }
5172 }
5173 }
5174 }
5175
5176 return needed;
Dianne Hackborn39792d22010-08-19 18:01:52 -07005177 }
5178
5179 /**
5180 * Like grantUriPermissionUncheckedLocked, but takes an Intent.
5181 */
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005182 void grantUriPermissionUncheckedFromIntentLocked(NeededUriGrants needed,
5183 UriPermissionOwner owner) {
5184 if (needed != null) {
5185 for (int i=0; i<needed.size(); i++) {
5186 grantUriPermissionUncheckedLocked(needed.targetUid, needed.targetPkg,
5187 needed.get(i), needed.flags, owner);
5188 }
5189 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005190 }
5191
5192 void grantUriPermissionFromIntentLocked(int callingUid,
5193 String targetPkg, Intent intent, UriPermissionOwner owner) {
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005194 NeededUriGrants needed = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg,
Dianne Hackbornd9781fe2012-03-08 18:04:18 -08005195 intent, intent != null ? intent.getFlags() : 0, null);
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005196 if (needed == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005197 return;
5198 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07005199
Dianne Hackborn21c241e2012-03-08 13:57:23 -08005200 grantUriPermissionUncheckedFromIntentLocked(needed, owner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 }
5202
5203 public void grantUriPermission(IApplicationThread caller, String targetPkg,
5204 Uri uri, int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005205 enforceNotIsolatedCaller("grantUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005206 synchronized(this) {
5207 final ProcessRecord r = getRecordForAppLocked(caller);
5208 if (r == null) {
5209 throw new SecurityException("Unable to find app for caller "
5210 + caller
5211 + " when granting permission to uri " + uri);
5212 }
5213 if (targetPkg == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005214 throw new IllegalArgumentException("null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 }
5216 if (uri == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07005217 throw new IllegalArgumentException("null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 }
5219
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005220 grantUriPermissionLocked(r.uid, targetPkg, uri, modeFlags,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 null);
5222 }
5223 }
5224
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005225 void removeUriPermissionIfNeededLocked(UriPermission perm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
5227 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
5228 HashMap<Uri, UriPermission> perms
5229 = mGrantedUriPermissions.get(perm.uid);
5230 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005231 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005232 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005233 perms.remove(perm.uri);
5234 if (perms.size() == 0) {
5235 mGrantedUriPermissions.remove(perm.uid);
5236 }
5237 }
5238 }
5239 }
5240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 private void revokeUriPermissionLocked(int callingUid, Uri uri,
5242 int modeFlags) {
5243 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5244 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5245 if (modeFlags == 0) {
5246 return;
5247 }
5248
Joe Onorato8a9b2202010-02-26 18:56:32 -08005249 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005250 "Revoking all granted permissions to " + uri);
5251
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005252 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005253
5254 final String authority = uri.getAuthority();
5255 ProviderInfo pi = null;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005256 int userId = UserHandle.getUserId(callingUid);
Amith Yamasani483f3b02012-03-13 16:08:00 -07005257 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 if (cpr != null) {
5259 pi = cpr.info;
5260 } else {
5261 try {
5262 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005263 PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 } catch (RemoteException ex) {
5265 }
5266 }
5267 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005268 Slog.w(TAG, "No content provider found for permission revoke: " + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 return;
5270 }
5271
5272 // Does the caller have this permission on the URI?
Dianne Hackborn48058e82010-09-27 16:53:23 -07005273 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 // Right now, if you are not the original owner of the permission,
5275 // you are not allowed to revoke it.
5276 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
5277 throw new SecurityException("Uid " + callingUid
5278 + " does not have permission to uri " + uri);
5279 //}
5280 }
5281
5282 // Go through all of the permissions and remove any that match.
5283 final List<String> SEGMENTS = uri.getPathSegments();
5284 if (SEGMENTS != null) {
5285 final int NS = SEGMENTS.size();
5286 int N = mGrantedUriPermissions.size();
5287 for (int i=0; i<N; i++) {
5288 HashMap<Uri, UriPermission> perms
5289 = mGrantedUriPermissions.valueAt(i);
5290 Iterator<UriPermission> it = perms.values().iterator();
5291 toploop:
5292 while (it.hasNext()) {
5293 UriPermission perm = it.next();
5294 Uri targetUri = perm.uri;
5295 if (!authority.equals(targetUri.getAuthority())) {
5296 continue;
5297 }
5298 List<String> targetSegments = targetUri.getPathSegments();
5299 if (targetSegments == null) {
5300 continue;
5301 }
5302 if (targetSegments.size() < NS) {
5303 continue;
5304 }
5305 for (int j=0; j<NS; j++) {
5306 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
5307 continue toploop;
5308 }
5309 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005310 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08005311 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312 perm.clearModes(modeFlags);
5313 if (perm.modeFlags == 0) {
5314 it.remove();
5315 }
5316 }
5317 if (perms.size() == 0) {
5318 mGrantedUriPermissions.remove(
5319 mGrantedUriPermissions.keyAt(i));
5320 N--;
5321 i--;
5322 }
5323 }
5324 }
5325 }
5326
5327 public void revokeUriPermission(IApplicationThread caller, Uri uri,
5328 int modeFlags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005329 enforceNotIsolatedCaller("revokeUriPermission");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 synchronized(this) {
5331 final ProcessRecord r = getRecordForAppLocked(caller);
5332 if (r == null) {
5333 throw new SecurityException("Unable to find app for caller "
5334 + caller
5335 + " when revoking permission to uri " + uri);
5336 }
5337 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005338 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 return;
5340 }
5341
5342 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
5343 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
5344 if (modeFlags == 0) {
5345 return;
5346 }
5347
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005348 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005349
5350 final String authority = uri.getAuthority();
5351 ProviderInfo pi = null;
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005352 ContentProviderRecord cpr = mProviderMap.getProviderByName(authority, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 if (cpr != null) {
5354 pi = cpr.info;
5355 } else {
5356 try {
5357 pi = pm.resolveContentProvider(authority,
Amith Yamasani483f3b02012-03-13 16:08:00 -07005358 PackageManager.GET_URI_PERMISSION_PATTERNS, r.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005359 } catch (RemoteException ex) {
5360 }
5361 }
5362 if (pi == null) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -07005363 Slog.w(TAG, "No content provider found for permission revoke: "
5364 + uri.toSafeString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 return;
5366 }
5367
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005368 revokeUriPermissionLocked(r.uid, uri, modeFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369 }
5370 }
5371
Dianne Hackborn7e269642010-08-25 19:50:20 -07005372 @Override
5373 public IBinder newUriPermissionOwner(String name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005374 enforceNotIsolatedCaller("newUriPermissionOwner");
Dianne Hackborn7e269642010-08-25 19:50:20 -07005375 synchronized(this) {
5376 UriPermissionOwner owner = new UriPermissionOwner(this, name);
5377 return owner.getExternalTokenLocked();
5378 }
5379 }
5380
5381 @Override
5382 public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg,
5383 Uri uri, int modeFlags) {
5384 synchronized(this) {
5385 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5386 if (owner == null) {
5387 throw new IllegalArgumentException("Unknown owner: " + token);
5388 }
5389 if (fromUid != Binder.getCallingUid()) {
5390 if (Binder.getCallingUid() != Process.myUid()) {
5391 // Only system code can grant URI permissions on behalf
5392 // of other users.
5393 throw new SecurityException("nice try");
5394 }
5395 }
5396 if (targetPkg == null) {
5397 throw new IllegalArgumentException("null target");
5398 }
5399 if (uri == null) {
5400 throw new IllegalArgumentException("null uri");
5401 }
5402
5403 grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner);
5404 }
5405 }
5406
5407 @Override
5408 public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) {
5409 synchronized(this) {
5410 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
5411 if (owner == null) {
5412 throw new IllegalArgumentException("Unknown owner: " + token);
5413 }
5414
5415 if (uri == null) {
5416 owner.removeUriPermissionsLocked(mode);
5417 } else {
5418 owner.removeUriPermissionLocked(uri, mode);
5419 }
5420 }
5421 }
5422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005423 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
5424 synchronized (this) {
5425 ProcessRecord app =
5426 who != null ? getRecordForAppLocked(who) : null;
5427 if (app == null) return;
5428
5429 Message msg = Message.obtain();
5430 msg.what = WAIT_FOR_DEBUGGER_MSG;
5431 msg.obj = app;
5432 msg.arg1 = waiting ? 1 : 0;
5433 mHandler.sendMessage(msg);
5434 }
5435 }
5436
5437 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07005438 final long homeAppMem = mProcessList.getMemLevel(ProcessList.HOME_APP_ADJ);
5439 final long hiddenAppMem = mProcessList.getMemLevel(ProcessList.HIDDEN_APP_MIN_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 outInfo.availMem = Process.getFreeMemory();
Dianne Hackborn59325eb2012-05-09 18:45:20 -07005441 outInfo.totalMem = Process.getTotalMemory();
Dianne Hackborn7d608422011-08-07 16:24:18 -07005442 outInfo.threshold = homeAppMem;
5443 outInfo.lowMemory = outInfo.availMem < (homeAppMem + ((hiddenAppMem-homeAppMem)/2));
5444 outInfo.hiddenAppThreshold = hiddenAppMem;
5445 outInfo.secondaryServerThreshold = mProcessList.getMemLevel(
Dianne Hackborne02c88a2011-10-28 13:58:15 -07005446 ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07005447 outInfo.visibleAppThreshold = mProcessList.getMemLevel(
5448 ProcessList.VISIBLE_APP_ADJ);
5449 outInfo.foregroundAppThreshold = mProcessList.getMemLevel(
5450 ProcessList.FOREGROUND_APP_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 }
5452
5453 // =========================================================
5454 // TASK MANAGEMENT
5455 // =========================================================
5456
5457 public List getTasks(int maxNum, int flags,
5458 IThumbnailReceiver receiver) {
5459 ArrayList list = new ArrayList();
5460
5461 PendingThumbnailsRecord pending = null;
5462 IApplicationThread topThumbnail = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005463 ActivityRecord topRecord = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464
5465 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005466 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005467 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
5468 + ", receiver=" + receiver);
5469
5470 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
5471 != PackageManager.PERMISSION_GRANTED) {
5472 if (receiver != null) {
5473 // If the caller wants to wait for pending thumbnails,
5474 // it ain't gonna get them.
5475 try {
5476 receiver.finished();
5477 } catch (RemoteException ex) {
5478 }
5479 }
5480 String msg = "Permission Denial: getTasks() from pid="
5481 + Binder.getCallingPid()
5482 + ", uid=" + Binder.getCallingUid()
5483 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005484 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 throw new SecurityException(msg);
5486 }
5487
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005488 int pos = mMainStack.mHistory.size()-1;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005489 ActivityRecord next =
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005490 pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005491 ActivityRecord top = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 TaskRecord curTask = null;
5493 int numActivities = 0;
5494 int numRunning = 0;
5495 while (pos >= 0 && maxNum > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005496 final ActivityRecord r = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 pos--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005498 next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499
5500 // Initialize state for next task if needed.
5501 if (top == null ||
5502 (top.state == ActivityState.INITIALIZING
5503 && top.task == r.task)) {
5504 top = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005505 curTask = r.task;
5506 numActivities = numRunning = 0;
5507 }
5508
5509 // Add 'r' into the current task.
5510 numActivities++;
5511 if (r.app != null && r.app.thread != null) {
5512 numRunning++;
5513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514
Joe Onorato8a9b2202010-02-26 18:56:32 -08005515 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005516 TAG, r.intent.getComponent().flattenToShortString()
5517 + ": task=" + r.task);
5518
5519 // If the next one is a different task, generate a new
5520 // TaskInfo entry for what we have.
5521 if (next == null || next.task != curTask) {
5522 ActivityManager.RunningTaskInfo ci
5523 = new ActivityManager.RunningTaskInfo();
5524 ci.id = curTask.taskId;
5525 ci.baseActivity = r.intent.getComponent();
5526 ci.topActivity = top.intent.getComponent();
Dianne Hackbornf26fd992011-04-08 18:14:09 -07005527 if (top.thumbHolder != null) {
5528 ci.description = top.thumbHolder.lastDescription;
5529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 ci.numActivities = numActivities;
5531 ci.numRunning = numRunning;
5532 //System.out.println(
5533 // "#" + maxNum + ": " + " descr=" + ci.description);
5534 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005535 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 TAG, "State=" + top.state + "Idle=" + top.idle
5537 + " app=" + top.app
5538 + " thr=" + (top.app != null ? top.app.thread : null));
5539 if (top.state == ActivityState.RESUMED
5540 || top.state == ActivityState.PAUSING) {
5541 if (top.idle && top.app != null
5542 && top.app.thread != null) {
5543 topRecord = top;
5544 topThumbnail = top.app.thread;
5545 } else {
5546 top.thumbnailNeeded = true;
5547 }
5548 }
5549 if (pending == null) {
5550 pending = new PendingThumbnailsRecord(receiver);
5551 }
5552 pending.pendingRecords.add(top);
5553 }
5554 list.add(ci);
5555 maxNum--;
5556 top = null;
5557 }
5558 }
5559
5560 if (pending != null) {
5561 mPendingThumbnails.add(pending);
5562 }
5563 }
5564
Joe Onorato8a9b2202010-02-26 18:56:32 -08005565 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566
5567 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005568 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08005570 topThumbnail.requestThumbnail(topRecord.appToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005571 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005572 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
Dianne Hackbornbe707852011-11-11 14:32:10 -08005573 sendPendingThumbnail(null, topRecord.appToken, null, null, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005574 }
5575 }
5576
5577 if (pending == null && receiver != null) {
5578 // In this case all thumbnails were available and the client
5579 // is being asked to be told when the remaining ones come in...
5580 // which is unusually, since the top-most currently running
5581 // activity should never have a canned thumbnail! Oh well.
5582 try {
5583 receiver.finished();
5584 } catch (RemoteException ex) {
5585 }
5586 }
5587
5588 return list;
5589 }
5590
5591 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
Amith Yamasani82644082012-08-03 13:09:11 -07005592 int flags, int userId) {
Amith Yamasani742a6712011-05-04 14:49:28 -07005593 final int callingUid = Binder.getCallingUid();
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005594 if (userId != UserHandle.getCallingUserId()) {
Amith Yamasani82644082012-08-03 13:09:11 -07005595 // Check if the caller is holding permissions for cross-user requests.
5596 if (checkComponentPermission(
5597 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
5598 Binder.getCallingPid(), callingUid, -1, true)
5599 != PackageManager.PERMISSION_GRANTED) {
5600 String msg = "Permission Denial: "
5601 + "Request to get recent tasks for user " + userId
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005602 + " but is calling from user " + UserHandle.getUserId(callingUid)
Amith Yamasani82644082012-08-03 13:09:11 -07005603 + "; this requires "
5604 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
5605 Slog.w(TAG, msg);
5606 throw new SecurityException(msg);
5607 } else {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07005608 if (userId == UserHandle.USER_CURRENT) {
Amith Yamasani82644082012-08-03 13:09:11 -07005609 userId = mCurrentUserId;
5610 }
5611 }
5612 }
5613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 synchronized (this) {
5615 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
5616 "getRecentTasks()");
Dianne Hackborn8238e712012-04-24 11:15:40 -07005617 final boolean detailed = checkCallingPermission(
5618 android.Manifest.permission.GET_DETAILED_TASKS)
5619 == PackageManager.PERMISSION_GRANTED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005621 IPackageManager pm = AppGlobals.getPackageManager();
Amith Yamasani82644082012-08-03 13:09:11 -07005622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 final int N = mRecentTasks.size();
5624 ArrayList<ActivityManager.RecentTaskInfo> res
5625 = new ArrayList<ActivityManager.RecentTaskInfo>(
5626 maxNum < N ? maxNum : N);
5627 for (int i=0; i<N && maxNum > 0; i++) {
5628 TaskRecord tr = mRecentTasks.get(i);
Amith Yamasani742a6712011-05-04 14:49:28 -07005629 // Only add calling user's recent tasks
Amith Yamasani82644082012-08-03 13:09:11 -07005630 if (tr.userId != userId) continue;
Dianne Hackborn905577f2011-09-07 18:31:28 -07005631 // Return the entry if desired by the caller. We always return
5632 // the first entry, because callers always expect this to be the
Amith Yamasani742a6712011-05-04 14:49:28 -07005633 // foreground app. We may filter others if the caller has
Dianne Hackborn905577f2011-09-07 18:31:28 -07005634 // not supplied RECENT_WITH_EXCLUDED and there is some reason
5635 // we should exclude the entry.
Amith Yamasani742a6712011-05-04 14:49:28 -07005636
Dianne Hackborn905577f2011-09-07 18:31:28 -07005637 if (i == 0
5638 || ((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 || (tr.intent == null)
5640 || ((tr.intent.getFlags()
5641 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
5642 ActivityManager.RecentTaskInfo rti
5643 = new ActivityManager.RecentTaskInfo();
5644 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005645 rti.persistentId = tr.taskId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005646 rti.baseIntent = new Intent(
5647 tr.intent != null ? tr.intent : tr.affinityIntent);
Dianne Hackborn8238e712012-04-24 11:15:40 -07005648 if (!detailed) {
5649 rti.baseIntent.replaceExtras((Bundle)null);
5650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 rti.origActivity = tr.origActivity;
Dianne Hackbornd2835932010-12-13 16:28:46 -08005652 rti.description = tr.lastDescription;
5653
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005654 if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) {
5655 // Check whether this activity is currently available.
5656 try {
5657 if (rti.origActivity != null) {
Amith Yamasani82644082012-08-03 13:09:11 -07005658 if (pm.getActivityInfo(rti.origActivity, 0, userId)
Amith Yamasani483f3b02012-03-13 16:08:00 -07005659 == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005660 continue;
5661 }
5662 } else if (rti.baseIntent != null) {
5663 if (pm.queryIntentActivities(rti.baseIntent,
Amith Yamasani82644082012-08-03 13:09:11 -07005664 null, 0, userId) == null) {
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07005665 continue;
5666 }
5667 }
5668 } catch (RemoteException e) {
5669 // Will never happen.
5670 }
5671 }
5672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005673 res.add(rti);
5674 maxNum--;
5675 }
5676 }
5677 return res;
5678 }
5679 }
5680
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005681 private TaskRecord taskForIdLocked(int id) {
5682 final int N = mRecentTasks.size();
5683 for (int i=0; i<N; i++) {
5684 TaskRecord tr = mRecentTasks.get(i);
5685 if (tr.taskId == id) {
5686 return tr;
Dianne Hackbornd94df452011-02-16 18:53:31 -08005687 }
5688 }
5689 return null;
5690 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005691
5692 public ActivityManager.TaskThumbnails getTaskThumbnails(int id) {
5693 synchronized (this) {
5694 enforceCallingPermission(android.Manifest.permission.READ_FRAME_BUFFER,
5695 "getTaskThumbnails()");
5696 TaskRecord tr = taskForIdLocked(id);
5697 if (tr != null) {
5698 return mMainStack.getTaskThumbnailsLocked(tr);
5699 }
5700 }
5701 return null;
5702 }
5703
5704 public boolean removeSubTask(int taskId, int subTaskIndex) {
5705 synchronized (this) {
5706 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5707 "removeSubTask()");
5708 long ident = Binder.clearCallingIdentity();
5709 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005710 return mMainStack.removeTaskActivitiesLocked(taskId, subTaskIndex,
5711 true) != null;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005712 } finally {
5713 Binder.restoreCallingIdentity(ident);
5714 }
5715 }
5716 }
5717
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005718 private void cleanUpRemovedTaskLocked(TaskRecord tr, int flags) {
5719 final boolean killProcesses = (flags&ActivityManager.REMOVE_TASK_KILL_PROCESS) != 0;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005720 Intent baseIntent = new Intent(
5721 tr.intent != null ? tr.intent : tr.affinityIntent);
5722 ComponentName component = baseIntent.getComponent();
5723 if (component == null) {
5724 Slog.w(TAG, "Now component for base intent of task: " + tr);
5725 return;
5726 }
5727
5728 // Find any running services associated with this app.
Dianne Hackborn599db5c2012-08-03 19:28:48 -07005729 mServices.cleanUpRemovedTaskLocked(tr, component, baseIntent);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005730
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005731 if (killProcesses) {
5732 // Find any running processes associated with this app.
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005733 final String pkg = component.getPackageName();
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005734 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005735 HashMap<String, SparseArray<ProcessRecord>> pmap = mProcessNames.getMap();
5736 for (SparseArray<ProcessRecord> uids : pmap.values()) {
5737 for (int i=0; i<uids.size(); i++) {
5738 ProcessRecord proc = uids.valueAt(i);
5739 if (proc.userId != tr.userId) {
5740 continue;
5741 }
5742 if (!proc.pkgList.contains(pkg)) {
5743 continue;
5744 }
5745 procs.add(proc);
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005746 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005747 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005748
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005749 // Kill the running processes.
5750 for (int i=0; i<procs.size(); i++) {
5751 ProcessRecord pr = procs.get(i);
5752 if (pr.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
5753 Slog.i(TAG, "Killing " + pr.toShortString() + ": remove task");
5754 EventLog.writeEvent(EventLogTags.AM_KILL, pr.pid,
5755 pr.processName, pr.setAdj, "remove task");
Dianne Hackborn45a25bc2012-06-28 13:49:17 -07005756 pr.killedBackground = true;
Dianne Hackborn8894cc52011-07-01 16:28:17 -07005757 Process.killProcessQuiet(pr.pid);
5758 } else {
5759 pr.waitingToKill = "remove task";
5760 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005761 }
5762 }
5763 }
5764
5765 public boolean removeTask(int taskId, int flags) {
5766 synchronized (this) {
5767 enforceCallingPermission(android.Manifest.permission.REMOVE_TASKS,
5768 "removeTask()");
5769 long ident = Binder.clearCallingIdentity();
5770 try {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005771 ActivityRecord r = mMainStack.removeTaskActivitiesLocked(taskId, -1,
5772 false);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005773 if (r != null) {
5774 mRecentTasks.remove(r.task);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005775 cleanUpRemovedTaskLocked(r.task, flags);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005776 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005777 } else {
5778 TaskRecord tr = null;
5779 int i=0;
5780 while (i < mRecentTasks.size()) {
5781 TaskRecord t = mRecentTasks.get(i);
5782 if (t.taskId == taskId) {
5783 tr = t;
5784 break;
5785 }
5786 i++;
5787 }
5788 if (tr != null) {
5789 if (tr.numActivities <= 0) {
5790 // Caller is just removing a recent task that is
5791 // not actively running. That is easy!
5792 mRecentTasks.remove(i);
Dianne Hackborn9da2d402012-03-15 13:43:08 -07005793 cleanUpRemovedTaskLocked(tr, flags);
5794 return true;
Dianne Hackborneeb1dca2011-09-08 13:30:11 -07005795 } else {
5796 Slog.w(TAG, "removeTask: task " + taskId
5797 + " does not have activities to remove, "
5798 + " but numActivities=" + tr.numActivities
5799 + ": " + tr);
5800 }
5801 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005802 }
5803 } finally {
5804 Binder.restoreCallingIdentity(ident);
5805 }
5806 }
5807 return false;
5808 }
Dianne Hackbornd94df452011-02-16 18:53:31 -08005809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
5811 int j;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005812 TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005813 TaskRecord jt = startTask;
5814
5815 // First look backwards
5816 for (j=startIndex-1; j>=0; j--) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005817 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 if (r.task != jt) {
5819 jt = r.task;
5820 if (affinity.equals(jt.affinity)) {
5821 return j;
5822 }
5823 }
5824 }
5825
5826 // Now look forwards
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005827 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 jt = startTask;
5829 for (j=startIndex+1; j<N; j++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005830 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 if (r.task != jt) {
5832 if (affinity.equals(jt.affinity)) {
5833 return j;
5834 }
5835 jt = r.task;
5836 }
5837 }
5838
5839 // Might it be at the top?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005840 if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 return N-1;
5842 }
5843
5844 return -1;
5845 }
5846
5847 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005848 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 */
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005850 public void moveTaskToFront(int task, int flags, Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5852 "moveTaskToFront()");
5853
5854 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005855 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5856 Binder.getCallingUid(), "Task to front")) {
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005857 ActivityOptions.abort(options);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005858 return;
5859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 final long origId = Binder.clearCallingIdentity();
5861 try {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005862 TaskRecord tr = taskForIdLocked(task);
5863 if (tr != null) {
5864 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
5865 mMainStack.mUserLeaving = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005867 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5868 // Caller wants the home activity moved with it. To accomplish this,
5869 // we'll just move the home task to the top first.
5870 mMainStack.moveHomeToFrontLocked();
5871 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005872 mMainStack.moveTaskToFrontLocked(tr, null, options);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07005873 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005874 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005875 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
5876 ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 if (hr.task.taskId == task) {
Dianne Hackbornd94df452011-02-16 18:53:31 -08005878 if ((flags&ActivityManager.MOVE_TASK_NO_USER_ACTION) == 0) {
5879 mMainStack.mUserLeaving = true;
5880 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005881 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5882 // Caller wants the home activity moved with it. To accomplish this,
5883 // we'll just move the home task to the top first.
5884 mMainStack.moveHomeToFrontLocked();
5885 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005886 mMainStack.moveTaskToFrontLocked(hr.task, null, options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 return;
5888 }
5889 }
5890 } finally {
5891 Binder.restoreCallingIdentity(origId);
5892 }
Dianne Hackborn8078d8c2012-03-20 11:11:26 -07005893 ActivityOptions.abort(options);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 }
5895 }
5896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005897 public void moveTaskToBack(int task) {
5898 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5899 "moveTaskToBack()");
5900
5901 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005902 if (mMainStack.mResumedActivity != null
5903 && mMainStack.mResumedActivity.task.taskId == task) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005904 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5905 Binder.getCallingUid(), "Task to back")) {
5906 return;
5907 }
5908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005910 mMainStack.moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005911 Binder.restoreCallingIdentity(origId);
5912 }
5913 }
5914
5915 /**
5916 * Moves an activity, and all of the other activities within the same task, to the bottom
5917 * of the history stack. The activity's order within the task is unchanged.
5918 *
5919 * @param token A reference to the activity we wish to move
5920 * @param nonRoot If false then this only works if the activity is the root
5921 * of a task; if true it will work for any activity in a task.
5922 * @return Returns true if the move completed, false if not.
5923 */
5924 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08005925 enforceNotIsolatedCaller("moveActivityTaskToBack");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 synchronized(this) {
5927 final long origId = Binder.clearCallingIdentity();
5928 int taskId = getTaskForActivityLocked(token, !nonRoot);
5929 if (taskId >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005930 return mMainStack.moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 }
5932 Binder.restoreCallingIdentity(origId);
5933 }
5934 return false;
5935 }
5936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 public void moveTaskBackwards(int task) {
5938 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5939 "moveTaskBackwards()");
5940
5941 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005942 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5943 Binder.getCallingUid(), "Task backwards")) {
5944 return;
5945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 final long origId = Binder.clearCallingIdentity();
5947 moveTaskBackwardsLocked(task);
5948 Binder.restoreCallingIdentity(origId);
5949 }
5950 }
5951
5952 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005953 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 }
5955
5956 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
5957 synchronized(this) {
5958 return getTaskForActivityLocked(token, onlyRoot);
5959 }
5960 }
5961
5962 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005963 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 TaskRecord lastTask = null;
5965 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005966 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornbe707852011-11-11 14:32:10 -08005967 if (r.appToken == token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005968 if (!onlyRoot || lastTask != r.task) {
5969 return r.task.taskId;
5970 }
5971 return -1;
5972 }
5973 lastTask = r.task;
5974 }
5975
5976 return -1;
5977 }
5978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005979 // =========================================================
5980 // THUMBNAILS
5981 // =========================================================
5982
5983 public void reportThumbnail(IBinder token,
5984 Bitmap thumbnail, CharSequence description) {
5985 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
5986 final long origId = Binder.clearCallingIdentity();
5987 sendPendingThumbnail(null, token, thumbnail, description, true);
5988 Binder.restoreCallingIdentity(origId);
5989 }
5990
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005991 final void sendPendingThumbnail(ActivityRecord r, IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 Bitmap thumbnail, CharSequence description, boolean always) {
5993 TaskRecord task = null;
5994 ArrayList receivers = null;
5995
5996 //System.out.println("Send pending thumbnail: " + r);
5997
5998 synchronized(this) {
5999 if (r == null) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07006000 r = mMainStack.isInStackLocked(token);
6001 if (r == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006002 return;
6003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006004 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07006005 if (thumbnail == null && r.thumbHolder != null) {
6006 thumbnail = r.thumbHolder.lastThumbnail;
6007 description = r.thumbHolder.lastDescription;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 }
6009 if (thumbnail == null && !always) {
6010 // If there is no thumbnail, and this entry is not actually
6011 // going away, then abort for now and pick up the next
6012 // thumbnail we get.
6013 return;
6014 }
6015 task = r.task;
6016
6017 int N = mPendingThumbnails.size();
6018 int i=0;
6019 while (i<N) {
6020 PendingThumbnailsRecord pr =
6021 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
6022 //System.out.println("Looking in " + pr.pendingRecords);
6023 if (pr.pendingRecords.remove(r)) {
6024 if (receivers == null) {
6025 receivers = new ArrayList();
6026 }
6027 receivers.add(pr);
6028 if (pr.pendingRecords.size() == 0) {
6029 pr.finished = true;
6030 mPendingThumbnails.remove(i);
6031 N--;
6032 continue;
6033 }
6034 }
6035 i++;
6036 }
6037 }
6038
6039 if (receivers != null) {
6040 final int N = receivers.size();
6041 for (int i=0; i<N; i++) {
6042 try {
6043 PendingThumbnailsRecord pr =
6044 (PendingThumbnailsRecord)receivers.get(i);
6045 pr.receiver.newThumbnail(
6046 task != null ? task.taskId : -1, thumbnail, description);
6047 if (pr.finished) {
6048 pr.receiver.finished();
6049 }
6050 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006051 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006052 }
6053 }
6054 }
6055 }
6056
6057 // =========================================================
6058 // CONTENT PROVIDERS
6059 // =========================================================
6060
Jeff Brown10e89712011-07-08 18:52:57 -07006061 private final List<ProviderInfo> generateApplicationProvidersLocked(ProcessRecord app) {
6062 List<ProviderInfo> providers = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006064 providers = AppGlobals.getPackageManager().
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006065 queryContentProviders(app.processName, app.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07006066 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 } catch (RemoteException ex) {
6068 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006069 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006070 Slog.v(TAG_MU, "generateApplicationProvidersLocked, app.info.uid = " + app.uid);
6071 int userId = app.userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006072 if (providers != null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006073 int N = providers.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 for (int i=0; i<N; i++) {
6075 ProviderInfo cpi =
6076 (ProviderInfo)providers.get(i);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006077 boolean singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6078 cpi.name, cpi.flags);
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006079 if (singleton && UserHandle.getUserId(app.uid) != 0) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006080 // This is a singleton provider, but a user besides the
6081 // default user is asking to initialize a process it runs
6082 // in... well, no, it doesn't actually run in this process,
6083 // it runs in the process of the default user. Get rid of it.
6084 providers.remove(i);
6085 N--;
6086 continue;
6087 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006088
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006089 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006090 ContentProviderRecord cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 if (cpr == null) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006092 cpr = new ContentProviderRecord(this, cpi, app.info, comp, singleton);
Amith Yamasani742a6712011-05-04 14:49:28 -07006093 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 }
Amith Yamasani742a6712011-05-04 14:49:28 -07006095 if (DEBUG_MU)
6096 Slog.v(TAG_MU, "generateApplicationProvidersLocked, cpi.uid = " + cpr.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006097 app.pubProviders.put(cpi.name, cpr);
6098 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07006099 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 }
6101 }
6102 return providers;
6103 }
6104
Jeff Sharkey110a6b62012-03-12 11:12:41 -07006105 /**
6106 * Check if {@link ProcessRecord} has a possible chance at accessing the
6107 * given {@link ProviderInfo}. Final permission checking is always done
6108 * in {@link ContentProvider}.
6109 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006110 private final String checkContentProviderPermissionLocked(
Dianne Hackbornb424b632010-08-18 15:59:05 -07006111 ProviderInfo cpi, ProcessRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006113 final int callingUid = (r != null) ? r.uid : Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006115 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006116 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 return null;
6118 }
6119 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006120 cpi.applicationInfo.uid, cpi.exported)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 == PackageManager.PERMISSION_GRANTED) {
6122 return null;
6123 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006124
6125 PathPermission[] pps = cpi.pathPermissions;
6126 if (pps != null) {
6127 int i = pps.length;
6128 while (i > 0) {
6129 i--;
6130 PathPermission pp = pps[i];
6131 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006132 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackbornb424b632010-08-18 15:59:05 -07006133 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006134 return null;
6135 }
6136 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006137 cpi.applicationInfo.uid, cpi.exported)
Dianne Hackborn2af632f2009-07-08 14:56:37 -07006138 == PackageManager.PERMISSION_GRANTED) {
6139 return null;
6140 }
6141 }
6142 }
6143
Dianne Hackbornb424b632010-08-18 15:59:05 -07006144 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
6145 if (perms != null) {
6146 for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) {
6147 if (uri.getKey().getAuthority().equals(cpi.authority)) {
6148 return null;
6149 }
6150 }
6151 }
6152
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08006153 String msg;
6154 if (!cpi.exported) {
6155 msg = "Permission Denial: opening provider " + cpi.name
6156 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6157 + ", uid=" + callingUid + ") that is not exported from uid "
6158 + cpi.applicationInfo.uid;
6159 } else {
6160 msg = "Permission Denial: opening provider " + cpi.name
6161 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
6162 + ", uid=" + callingUid + ") requires "
6163 + cpi.readPermission + " or " + cpi.writePermission;
6164 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006165 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006166 return msg;
6167 }
6168
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006169 ContentProviderConnection incProviderCountLocked(ProcessRecord r,
6170 final ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006171 if (r != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006172 for (int i=0; i<r.conProviders.size(); i++) {
6173 ContentProviderConnection conn = r.conProviders.get(i);
6174 if (conn.provider == cpr) {
6175 if (DEBUG_PROVIDER) Slog.v(TAG,
6176 "Adding provider requested by "
6177 + r.processName + " from process "
6178 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
6179 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6180 if (stable) {
6181 conn.stableCount++;
6182 conn.numStableIncs++;
6183 } else {
6184 conn.unstableCount++;
6185 conn.numUnstableIncs++;
6186 }
6187 return conn;
6188 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006189 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006190 ContentProviderConnection conn = new ContentProviderConnection(cpr, r);
6191 if (stable) {
6192 conn.stableCount = 1;
6193 conn.numStableIncs = 1;
6194 } else {
6195 conn.unstableCount = 1;
6196 conn.numUnstableIncs = 1;
6197 }
6198 cpr.connections.add(conn);
6199 r.conProviders.add(conn);
6200 return conn;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006201 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006202 cpr.addExternalProcessHandleLocked(externalProcessToken);
6203 return null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006204 }
6205
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006206 boolean decProviderCountLocked(ContentProviderConnection conn,
6207 ContentProviderRecord cpr, IBinder externalProcessToken, boolean stable) {
6208 if (conn != null) {
6209 cpr = conn.provider;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006210 if (DEBUG_PROVIDER) Slog.v(TAG,
6211 "Removing provider requested by "
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006212 + conn.client.processName + " from process "
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006213 + cpr.info.processName + ": " + cpr.name.flattenToShortString()
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006214 + " scnt=" + conn.stableCount + " uscnt=" + conn.unstableCount);
6215 if (stable) {
6216 conn.stableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006217 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006218 conn.unstableCount--;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006219 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006220 if (conn.stableCount == 0 && conn.unstableCount == 0) {
6221 cpr.connections.remove(conn);
6222 conn.client.conProviders.remove(conn);
6223 return true;
6224 }
6225 return false;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006226 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006227 cpr.removeExternalProcessHandleLocked(externalProcessToken);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006228 return false;
6229 }
6230
Amith Yamasani742a6712011-05-04 14:49:28 -07006231 private final ContentProviderHolder getContentProviderImpl(IApplicationThread caller,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006232 String name, IBinder token, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 ContentProviderRecord cpr;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006234 ContentProviderConnection conn = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006235 ProviderInfo cpi = null;
6236
6237 synchronized(this) {
6238 ProcessRecord r = null;
6239 if (caller != null) {
6240 r = getRecordForAppLocked(caller);
6241 if (r == null) {
6242 throw new SecurityException(
6243 "Unable to find app for caller " + caller
6244 + " (pid=" + Binder.getCallingPid()
6245 + ") when getting content provider " + name);
6246 }
6247 }
6248
6249 // First check if this content provider has been published...
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006250 int userId = UserHandle.getUserId(r != null ? r.uid : Binder.getCallingUid());
Amith Yamasani742a6712011-05-04 14:49:28 -07006251 cpr = mProviderMap.getProviderByName(name, userId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006252 boolean providerRunning = cpr != null;
6253 if (providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 cpi = cpr.info;
Dianne Hackbornb424b632010-08-18 15:59:05 -07006255 String msg;
6256 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6257 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 }
6259
6260 if (r != null && cpr.canRunHere(r)) {
6261 // This provider has been published or is in the process
6262 // of being published... but it is also allowed to run
6263 // in the caller's process, so don't make a connection
6264 // and just let the caller instantiate its own instance.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006265 ContentProviderHolder holder = cpr.newHolder(null);
6266 // don't give caller the provider object, it needs
6267 // to make its own.
6268 holder.provider = null;
6269 return holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006270 }
6271
6272 final long origId = Binder.clearCallingIdentity();
6273
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006274 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006275 // return it right away.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006276 conn = incProviderCountLocked(r, cpr, token, stable);
6277 if (conn != null && (conn.stableCount+conn.unstableCount) == 1) {
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006278 if (cpr.proc != null && r.setAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006279 // If this is a perceptible app accessing the provider,
Dianne Hackborn906497c2010-05-10 15:57:38 -07006280 // make sure to count it as being accessed and thus
6281 // back up on the LRU list. This is good because
6282 // content providers are often expensive to start.
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006283 updateLruProcessLocked(cpr.proc, false, true);
Dianne Hackborn906497c2010-05-10 15:57:38 -07006284 }
Dianne Hackborn6f86c0e2010-05-11 14:20:52 -07006285 }
6286
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006287 if (cpr.proc != null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006288 if (false) {
6289 if (cpr.name.flattenToShortString().equals(
6290 "com.android.providers.calendar/.CalendarProvider2")) {
6291 Slog.v(TAG, "****************** KILLING "
6292 + cpr.name.flattenToShortString());
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006293 Process.killProcess(cpr.proc.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006294 }
6295 }
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006296 boolean success = updateOomAdjLocked(cpr.proc);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006297 if (DEBUG_PROVIDER) Slog.i(TAG, "Adjust success: " + success);
6298 // NOTE: there is still a race here where a signal could be
6299 // pending on the process even though we managed to update its
6300 // adj level. Not sure what to do about this, but at least
6301 // the race is now smaller.
6302 if (!success) {
6303 // Uh oh... it looks like the provider's process
6304 // has been killed on us. We need to wait for a new
6305 // process to be started, and make sure its death
6306 // doesn't kill our process.
6307 Slog.i(TAG,
6308 "Existing provider " + cpr.name.flattenToShortString()
6309 + " is crashing; detaching " + r);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006310 boolean lastRef = decProviderCountLocked(conn, cpr, token, stable);
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006311 appDiedLocked(cpr.proc, cpr.proc.pid, cpr.proc.thread);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006312 if (!lastRef) {
6313 // This wasn't the last ref our process had on
6314 // the provider... we have now been killed, bail.
6315 return null;
6316 }
6317 providerRunning = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006318 conn = null;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006320 }
6321
6322 Binder.restoreCallingIdentity(origId);
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006324
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006325 boolean singleton;
Dianne Hackborn295e3c22011-08-25 13:19:08 -07006326 if (!providerRunning) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006328 cpi = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07006329 resolveContentProvider(name,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006330 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006331 } catch (RemoteException ex) {
6332 }
6333 if (cpi == null) {
6334 return null;
6335 }
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006336 singleton = isSingleton(cpi.processName, cpi.applicationInfo,
6337 cpi.name, cpi.flags);
6338 if (singleton) {
Amith Yamasania4a54e22012-04-16 15:44:19 -07006339 userId = 0;
6340 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006341 cpi.applicationInfo = getAppInfoForUser(cpi.applicationInfo, userId);
Amith Yamasani742a6712011-05-04 14:49:28 -07006342
Dianne Hackbornb424b632010-08-18 15:59:05 -07006343 String msg;
6344 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
6345 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006346 }
6347
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07006348 if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006349 && !cpi.processName.equals("system")) {
6350 // If this content provider does not run in the system
6351 // process, and the system is not yet ready to run other
6352 // processes, then fail fast instead of hanging.
6353 throw new IllegalArgumentException(
6354 "Attempt to launch content provider before system ready");
6355 }
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006356
Dianne Hackborn80a4af22012-08-27 19:18:31 -07006357 // Make sure that the user who owns this provider is started. If not,
6358 // we don't want to allow it to run.
6359 if (mStartedUsers.get(userId) == null) {
6360 Slog.w(TAG, "Unable to launch app "
6361 + cpi.applicationInfo.packageName + "/"
6362 + cpi.applicationInfo.uid + " for provider "
6363 + name + ": user " + userId + " is stopped");
6364 return null;
6365 }
6366
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006367 ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
Amith Yamasani483f3b02012-03-13 16:08:00 -07006368 cpr = mProviderMap.getProviderByClass(comp, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 final boolean firstClass = cpr == null;
6370 if (firstClass) {
6371 try {
6372 ApplicationInfo ai =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006373 AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 getApplicationInfo(
6375 cpi.applicationInfo.packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07006376 STOCK_PM_FLAGS, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006378 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006379 + cpi.name);
6380 return null;
6381 }
Amith Yamasani483f3b02012-03-13 16:08:00 -07006382 ai = getAppInfoForUser(ai, userId);
Dianne Hackborn7d19e022012-08-07 19:12:33 -07006383 cpr = new ContentProviderRecord(this, cpi, ai, comp, singleton);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 } catch (RemoteException ex) {
6385 // pm is in same process, this will never happen.
6386 }
6387 }
6388
6389 if (r != null && cpr.canRunHere(r)) {
6390 // If this is a multiprocess provider, then just return its
6391 // info and allow the caller to instantiate it. Only do
6392 // this if the provider is the same user as the caller's
6393 // process, or can run as root (so can be in any process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006394 return cpr.newHolder(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006395 }
6396
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006397 if (DEBUG_PROVIDER) {
6398 RuntimeException e = new RuntimeException("here");
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006399 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07006400 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 }
6402
6403 // This is single process, and our app is now connecting to it.
6404 // See if we are already in the process of launching this
6405 // provider.
6406 final int N = mLaunchingProviders.size();
6407 int i;
6408 for (i=0; i<N; i++) {
6409 if (mLaunchingProviders.get(i) == cpr) {
6410 break;
6411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 }
6413
6414 // If the provider is not already being launched, then get it
6415 // started.
6416 if (i >= N) {
6417 final long origId = Binder.clearCallingIdentity();
Dianne Hackborne7f97212011-02-24 14:40:20 -08006418
6419 try {
6420 // Content provider is now in use, its package can't be stopped.
6421 try {
6422 AppGlobals.getPackageManager().setPackageStoppedState(
Amith Yamasani483f3b02012-03-13 16:08:00 -07006423 cpr.appInfo.packageName, false, userId);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006424 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08006425 } catch (IllegalArgumentException e) {
6426 Slog.w(TAG, "Failed trying to unstop package "
6427 + cpr.appInfo.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006428 }
6429
6430 ProcessRecord proc = startProcessLocked(cpi.processName,
6431 cpr.appInfo, false, 0, "content provider",
6432 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006433 cpi.name), false, false);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006434 if (proc == null) {
6435 Slog.w(TAG, "Unable to launch app "
6436 + cpi.applicationInfo.packageName + "/"
6437 + cpi.applicationInfo.uid + " for provider "
6438 + name + ": process is bad");
6439 return null;
6440 }
6441 cpr.launchingApp = proc;
6442 mLaunchingProviders.add(cpr);
6443 } finally {
6444 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 }
6447
6448 // Make sure the provider is published (the same provider class
6449 // may be published under multiple names).
6450 if (firstClass) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006451 mProviderMap.putProviderByClass(comp, cpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006452 }
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006453
Amith Yamasani742a6712011-05-04 14:49:28 -07006454 mProviderMap.putProviderByName(name, cpr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006455 conn = incProviderCountLocked(r, cpr, token, stable);
6456 if (conn != null) {
6457 conn.waiting = true;
6458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459 }
6460 }
6461
6462 // Wait for the provider to be published...
6463 synchronized (cpr) {
6464 while (cpr.provider == null) {
6465 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006466 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006467 + cpi.applicationInfo.packageName + "/"
6468 + cpi.applicationInfo.uid + " for provider "
6469 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006470 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 cpi.applicationInfo.packageName,
6472 cpi.applicationInfo.uid, name);
6473 return null;
6474 }
6475 try {
Amith Yamasani742a6712011-05-04 14:49:28 -07006476 if (DEBUG_MU) {
6477 Slog.v(TAG_MU, "Waiting to start provider " + cpr + " launchingApp="
6478 + cpr.launchingApp);
6479 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006480 if (conn != null) {
6481 conn.waiting = true;
6482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006483 cpr.wait();
6484 } catch (InterruptedException ex) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006485 } finally {
6486 if (conn != null) {
6487 conn.waiting = false;
6488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 }
6490 }
6491 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006492 return cpr != null ? cpr.newHolder(conn) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006493 }
6494
6495 public final ContentProviderHolder getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006496 IApplicationThread caller, String name, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006497 enforceNotIsolatedCaller("getContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 if (caller == null) {
6499 String msg = "null IApplicationThread when getting content provider "
6500 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006501 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502 throw new SecurityException(msg);
6503 }
6504
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006505 return getContentProviderImpl(caller, name, null, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 }
6507
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006508 public ContentProviderHolder getContentProviderExternal(String name, IBinder token) {
6509 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6510 "Do not have permission in call getContentProviderExternal()");
6511 return getContentProviderExternalUnchecked(name, token);
6512 }
6513
6514 private ContentProviderHolder getContentProviderExternalUnchecked(String name,IBinder token) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006515 return getContentProviderImpl(null, name, token, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516 }
6517
6518 /**
6519 * Drop a content provider from a ProcessRecord's bookkeeping
6520 * @param cpr
6521 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006522 public void removeContentProvider(IBinder connection, boolean stable) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006523 enforceNotIsolatedCaller("removeContentProvider");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 synchronized (this) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006525 ContentProviderConnection conn;
6526 try {
6527 conn = (ContentProviderConnection)connection;
6528 } catch (ClassCastException e) {
6529 String msg ="removeContentProvider: " + connection
6530 + " not a ContentProviderConnection";
6531 Slog.w(TAG, msg);
6532 throw new IllegalArgumentException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006533 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006534 if (conn == null) {
6535 throw new NullPointerException("connection is null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006536 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006537 if (decProviderCountLocked(conn, null, null, stable)) {
6538 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 }
6541 }
6542
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006543 public void removeContentProviderExternal(String name, IBinder token) {
6544 enforceCallingPermission(android.Manifest.permission.ACCESS_CONTENT_PROVIDERS_EXTERNALLY,
6545 "Do not have permission in call removeContentProviderExternal()");
6546 removeContentProviderExternalUnchecked(name, token);
6547 }
6548
6549 private void removeContentProviderExternalUnchecked(String name, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006550 synchronized (this) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006551 ContentProviderRecord cpr = mProviderMap.getProviderByName(name,
6552 Binder.getOrigCallingUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006553 if(cpr == null) {
6554 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08006555 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006556 return;
6557 }
6558
6559 //update content provider record entry info
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006560 ComponentName comp = new ComponentName(cpr.info.packageName, cpr.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006561 ContentProviderRecord localCpr = mProviderMap.getProviderByClass(comp,
6562 Binder.getOrigCallingUser());
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006563 if (localCpr.hasExternalProcessHandles()) {
6564 if (localCpr.removeExternalProcessHandleLocked(token)) {
6565 updateOomAdjLocked();
6566 } else {
6567 Slog.e(TAG, "Attmpt to remove content provider " + localCpr
6568 + " with no external reference for token: "
6569 + token + ".");
6570 }
6571 } else {
6572 Slog.e(TAG, "Attmpt to remove content provider: " + localCpr
6573 + " with no external references.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006575 }
6576 }
6577
6578 public final void publishContentProviders(IApplicationThread caller,
6579 List<ContentProviderHolder> providers) {
6580 if (providers == null) {
6581 return;
6582 }
6583
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006584 enforceNotIsolatedCaller("publishContentProviders");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006585 synchronized (this) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006586 final ProcessRecord r = getRecordForAppLocked(caller);
Amith Yamasani742a6712011-05-04 14:49:28 -07006587 if (DEBUG_MU)
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006588 Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 if (r == null) {
6590 throw new SecurityException(
6591 "Unable to find app for caller " + caller
6592 + " (pid=" + Binder.getCallingPid()
6593 + ") when publishing content providers");
6594 }
6595
6596 final long origId = Binder.clearCallingIdentity();
6597
6598 final int N = providers.size();
6599 for (int i=0; i<N; i++) {
6600 ContentProviderHolder src = providers.get(i);
6601 if (src == null || src.info == null || src.provider == null) {
6602 continue;
6603 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07006604 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006605 if (DEBUG_MU)
6606 Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006607 if (dst != null) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07006608 ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
Amith Yamasani742a6712011-05-04 14:49:28 -07006609 mProviderMap.putProviderByClass(comp, dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006610 String names[] = dst.info.authority.split(";");
6611 for (int j = 0; j < names.length; j++) {
Amith Yamasani742a6712011-05-04 14:49:28 -07006612 mProviderMap.putProviderByName(names[j], dst);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006613 }
6614
6615 int NL = mLaunchingProviders.size();
6616 int j;
6617 for (j=0; j<NL; j++) {
6618 if (mLaunchingProviders.get(j) == dst) {
6619 mLaunchingProviders.remove(j);
6620 j--;
6621 NL--;
6622 }
6623 }
6624 synchronized (dst) {
6625 dst.provider = src.provider;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -07006626 dst.proc = r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006627 dst.notifyAll();
6628 }
6629 updateOomAdjLocked(r);
6630 }
6631 }
6632
6633 Binder.restoreCallingIdentity(origId);
6634 }
6635 }
6636
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006637 public boolean refContentProvider(IBinder connection, int stable, int unstable) {
6638 ContentProviderConnection conn;
6639 try {
6640 conn = (ContentProviderConnection)connection;
6641 } catch (ClassCastException e) {
6642 String msg ="refContentProvider: " + connection
6643 + " not a ContentProviderConnection";
6644 Slog.w(TAG, msg);
6645 throw new IllegalArgumentException(msg);
6646 }
6647 if (conn == null) {
6648 throw new NullPointerException("connection is null");
6649 }
6650
6651 synchronized (this) {
6652 if (stable > 0) {
6653 conn.numStableIncs += stable;
6654 }
6655 stable = conn.stableCount + stable;
6656 if (stable < 0) {
6657 throw new IllegalStateException("stableCount < 0: " + stable);
6658 }
6659
6660 if (unstable > 0) {
6661 conn.numUnstableIncs += unstable;
6662 }
6663 unstable = conn.unstableCount + unstable;
6664 if (unstable < 0) {
6665 throw new IllegalStateException("unstableCount < 0: " + unstable);
6666 }
6667
6668 if ((stable+unstable) <= 0) {
6669 throw new IllegalStateException("ref counts can't go to zero here: stable="
6670 + stable + " unstable=" + unstable);
6671 }
6672 conn.stableCount = stable;
6673 conn.unstableCount = unstable;
6674 return !conn.dead;
6675 }
6676 }
6677
6678 public void unstableProviderDied(IBinder connection) {
6679 ContentProviderConnection conn;
6680 try {
6681 conn = (ContentProviderConnection)connection;
6682 } catch (ClassCastException e) {
6683 String msg ="refContentProvider: " + connection
6684 + " not a ContentProviderConnection";
6685 Slog.w(TAG, msg);
6686 throw new IllegalArgumentException(msg);
6687 }
6688 if (conn == null) {
6689 throw new NullPointerException("connection is null");
6690 }
6691
6692 // Safely retrieve the content provider associated with the connection.
6693 IContentProvider provider;
6694 synchronized (this) {
6695 provider = conn.provider.provider;
6696 }
6697
6698 if (provider == null) {
6699 // Um, yeah, we're way ahead of you.
6700 return;
6701 }
6702
6703 // Make sure the caller is being honest with us.
6704 if (provider.asBinder().pingBinder()) {
6705 // Er, no, still looks good to us.
6706 synchronized (this) {
6707 Slog.w(TAG, "unstableProviderDied: caller " + Binder.getCallingUid()
6708 + " says " + conn + " died, but we don't agree");
6709 return;
6710 }
6711 }
6712
6713 // Well look at that! It's dead!
6714 synchronized (this) {
6715 if (conn.provider.provider != provider) {
6716 // But something changed... good enough.
6717 return;
6718 }
6719
6720 ProcessRecord proc = conn.provider.proc;
6721 if (proc == null || proc.thread == null) {
6722 // Seems like the process is already cleaned up.
6723 return;
6724 }
6725
6726 // As far as we're concerned, this is just like receiving a
6727 // death notification... just a bit prematurely.
6728 Slog.i(TAG, "Process " + proc.processName + " (pid " + proc.pid
6729 + ") early provider death");
Dianne Hackbornbd145db2012-06-05 16:20:46 -07006730 final long ident = Binder.clearCallingIdentity();
6731 try {
6732 appDiedLocked(proc, proc.pid, proc.thread);
6733 } finally {
6734 Binder.restoreCallingIdentity(ident);
6735 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006736 }
6737 }
6738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006739 public static final void installSystemProviders() {
Jeff Brown10e89712011-07-08 18:52:57 -07006740 List<ProviderInfo> providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06006741 synchronized (mSelf) {
6742 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
6743 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08006744 if (providers != null) {
6745 for (int i=providers.size()-1; i>=0; i--) {
6746 ProviderInfo pi = (ProviderInfo)providers.get(i);
6747 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
6748 Slog.w(TAG, "Not installing system proc provider " + pi.name
6749 + ": not system .apk");
6750 providers.remove(i);
6751 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08006752 }
6753 }
6754 }
Josh Bartel2ecce342010-02-25 10:55:48 -06006755 if (providers != null) {
6756 mSystemThread.installSystemProviders(providers);
6757 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006758
6759 mSelf.mCoreSettingsObserver = new CoreSettingsObserver(mSelf);
Mark Brophyc6350272011-08-05 16:16:39 +01006760
6761 mSelf.mUsageStatsService.monitorPackages();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006762 }
6763
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006764 /**
6765 * Allows app to retrieve the MIME type of a URI without having permission
6766 * to access its content provider.
6767 *
6768 * CTS tests for this functionality can be run with "runtest cts-appsecurity".
6769 *
6770 * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
6771 * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
6772 */
6773 public String getProviderMimeType(Uri uri) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006774 enforceNotIsolatedCaller("getProviderMimeType");
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006775 final String name = uri.getAuthority();
6776 final long ident = Binder.clearCallingIdentity();
6777 ContentProviderHolder holder = null;
6778
6779 try {
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006780 holder = getContentProviderExternalUnchecked(name, null);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006781 if (holder != null) {
6782 return holder.provider.getType(uri);
6783 }
6784 } catch (RemoteException e) {
6785 Log.w(TAG, "Content provider dead retrieving " + uri, e);
6786 return null;
6787 } finally {
6788 if (holder != null) {
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006789 removeContentProviderExternalUnchecked(name, null);
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006790 }
6791 Binder.restoreCallingIdentity(ident);
6792 }
6793
6794 return null;
6795 }
6796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006797 // =========================================================
6798 // GLOBAL MANAGEMENT
6799 // =========================================================
6800
6801 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006802 ApplicationInfo info, String customProcess, boolean isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006803 String proc = customProcess != null ? customProcess : info.processName;
6804 BatteryStatsImpl.Uid.Proc ps = null;
6805 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006806 int uid = info.uid;
6807 if (isolated) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006808 int userId = UserHandle.getUserId(uid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006809 int stepsLeft = Process.LAST_ISOLATED_UID - Process.FIRST_ISOLATED_UID + 1;
6810 uid = 0;
6811 while (true) {
6812 if (mNextIsolatedProcessUid < Process.FIRST_ISOLATED_UID
6813 || mNextIsolatedProcessUid > Process.LAST_ISOLATED_UID) {
6814 mNextIsolatedProcessUid = Process.FIRST_ISOLATED_UID;
6815 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006816 uid = UserHandle.getUid(userId, mNextIsolatedProcessUid);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006817 mNextIsolatedProcessUid++;
6818 if (mIsolatedProcesses.indexOfKey(uid) < 0) {
6819 // No process for this uid, use it.
6820 break;
6821 }
6822 stepsLeft--;
6823 if (stepsLeft <= 0) {
6824 return null;
6825 }
6826 }
6827 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006828 synchronized (stats) {
6829 ps = stats.getProcessStatsLocked(info.uid, proc);
6830 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006831 return new ProcessRecord(ps, thread, info, proc, uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006832 }
6833
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006834 final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) {
6835 ProcessRecord app;
6836 if (!isolated) {
6837 app = getProcessRecordLocked(info.processName, info.uid);
6838 } else {
6839 app = null;
6840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841
6842 if (app == null) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08006843 app = newProcessRecordLocked(null, info, null, isolated);
6844 mProcessNames.put(info.processName, app.uid, app);
6845 if (isolated) {
6846 mIsolatedProcesses.put(app.uid, app);
6847 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08006848 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006849 }
6850
Dianne Hackborne7f97212011-02-24 14:40:20 -08006851 // This package really, really can not be stopped.
6852 try {
6853 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07006854 info.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -08006855 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -08006856 } catch (IllegalArgumentException e) {
6857 Slog.w(TAG, "Failed trying to unstop package "
6858 + info.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -08006859 }
6860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006861 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
6862 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
6863 app.persistent = true;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07006864 app.maxAdj = ProcessList.PERSISTENT_PROC_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 }
6866 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
6867 mPersistentStartingProcesses.add(app);
6868 startProcessLocked(app, "added application", app.processName);
6869 }
6870
6871 return app;
6872 }
6873
6874 public void unhandledBack() {
6875 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
6876 "unhandledBack()");
6877
6878 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006879 int count = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08006880 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006881 TAG, "Performing unhandledBack(): stack size = " + count);
6882 if (count > 1) {
6883 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006884 mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006885 count-1, Activity.RESULT_CANCELED, null, "unhandled-back");
6886 Binder.restoreCallingIdentity(origId);
6887 }
6888 }
6889 }
6890
6891 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08006892 enforceNotIsolatedCaller("openContentUri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006893 String name = uri.getAuthority();
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006894 ContentProviderHolder cph = getContentProviderExternalUnchecked(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006895 ParcelFileDescriptor pfd = null;
6896 if (cph != null) {
6897 // We record the binder invoker's uid in thread-local storage before
6898 // going to the content provider to open the file. Later, in the code
6899 // that handles all permissions checks, we look for this uid and use
6900 // that rather than the Activity Manager's own uid. The effect is that
6901 // we do the check against the caller's permissions even though it looks
6902 // to the content provider like the Activity Manager itself is making
6903 // the request.
6904 sCallerIdentity.set(new Identity(
6905 Binder.getCallingPid(), Binder.getCallingUid()));
6906 try {
6907 pfd = cph.provider.openFile(uri, "r");
6908 } catch (FileNotFoundException e) {
6909 // do nothing; pfd will be returned null
6910 } finally {
6911 // Ensure that whatever happens, we clean up the identity state
6912 sCallerIdentity.remove();
6913 }
6914
6915 // We've got the fd now, so we're done with the provider.
Svetoslav Ganov25872aa2012-02-03 19:19:09 -08006916 removeContentProviderExternalUnchecked(name, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006917 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006918 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006919 }
6920 return pfd;
6921 }
6922
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006923 // Actually is sleeping or shutting down or whatever else in the future
6924 // is an inactive state.
6925 public boolean isSleeping() {
6926 return mSleeping || mShuttingDown;
6927 }
6928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006929 public void goingToSleep() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006930 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
6931 != PackageManager.PERMISSION_GRANTED) {
6932 throw new SecurityException("Requires permission "
6933 + android.Manifest.permission.DEVICE_POWER);
6934 }
6935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006937 mWentToSleep = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07006938 updateEventDispatchingLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006939
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006940 if (!mSleeping) {
6941 mSleeping = true;
6942 mMainStack.stopIfSleepingLocked();
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07006943
Dianne Hackbornff5b1582012-04-12 17:24:07 -07006944 // Initialize the wake times of all processes.
6945 checkExcessivePowerUsageLocked(false);
6946 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
6947 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
6948 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
6949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 }
6951 }
6952
Dianne Hackborn55280a92009-05-07 15:53:46 -07006953 public boolean shutdown(int timeout) {
6954 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
6955 != PackageManager.PERMISSION_GRANTED) {
6956 throw new SecurityException("Requires permission "
6957 + android.Manifest.permission.SHUTDOWN);
6958 }
6959
6960 boolean timedout = false;
6961
6962 synchronized(this) {
6963 mShuttingDown = true;
Jeff Brownc042ee22012-05-08 13:03:42 -07006964 updateEventDispatchingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07006965
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006966 if (mMainStack.mResumedActivity != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006967 mMainStack.stopIfSleepingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07006968 final long endTime = System.currentTimeMillis() + timeout;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006969 while (mMainStack.mResumedActivity != null
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08006970 || mMainStack.mPausingActivity != null) {
Dianne Hackborn55280a92009-05-07 15:53:46 -07006971 long delay = endTime - System.currentTimeMillis();
6972 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006973 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07006974 timedout = true;
6975 break;
6976 }
6977 try {
6978 this.wait();
6979 } catch (InterruptedException e) {
6980 }
6981 }
6982 }
6983 }
6984
6985 mUsageStatsService.shutdown();
6986 mBatteryStatsService.shutdown();
6987
6988 return timedout;
6989 }
6990
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08006991 public final void activitySlept(IBinder token) {
6992 if (localLOGV) Slog.v(
6993 TAG, "Activity slept: token=" + token);
6994
6995 ActivityRecord r = null;
6996
6997 final long origId = Binder.clearCallingIdentity();
6998
6999 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007000 r = mMainStack.isInStackLocked(token);
7001 if (r != null) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08007002 mMainStack.activitySleptLocked(r);
7003 }
7004 }
7005
7006 Binder.restoreCallingIdentity(origId);
7007 }
7008
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007009 private void comeOutOfSleepIfNeededLocked() {
7010 if (!mWentToSleep && !mLockScreenShown) {
7011 if (mSleeping) {
7012 mSleeping = false;
7013 mMainStack.awakeFromSleepingLocked();
7014 mMainStack.resumeTopActivityLocked(null);
7015 }
7016 }
7017 }
7018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 public void wakingUp() {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007020 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
7021 != PackageManager.PERMISSION_GRANTED) {
7022 throw new SecurityException("Requires permission "
7023 + android.Manifest.permission.DEVICE_POWER);
7024 }
7025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007026 synchronized(this) {
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007027 mWentToSleep = false;
Jeff Brownc042ee22012-05-08 13:03:42 -07007028 updateEventDispatchingLocked();
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007029 comeOutOfSleepIfNeededLocked();
7030 }
7031 }
7032
Jeff Brownc042ee22012-05-08 13:03:42 -07007033 private void updateEventDispatchingLocked() {
7034 mWindowManager.setEventDispatching(mBooted && !mWentToSleep && !mShuttingDown);
7035 }
7036
Dianne Hackbornff5b1582012-04-12 17:24:07 -07007037 public void setLockScreenShown(boolean shown) {
7038 if (checkCallingPermission(android.Manifest.permission.DEVICE_POWER)
7039 != PackageManager.PERMISSION_GRANTED) {
7040 throw new SecurityException("Requires permission "
7041 + android.Manifest.permission.DEVICE_POWER);
7042 }
7043
7044 synchronized(this) {
7045 mLockScreenShown = shown;
7046 comeOutOfSleepIfNeededLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047 }
7048 }
7049
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007050 public void stopAppSwitches() {
7051 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
7052 != PackageManager.PERMISSION_GRANTED) {
7053 throw new SecurityException("Requires permission "
7054 + android.Manifest.permission.STOP_APP_SWITCHES);
7055 }
7056
7057 synchronized(this) {
7058 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
7059 + APP_SWITCH_DELAY_TIME;
7060 mDidAppSwitch = false;
7061 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
7062 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
7063 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
7064 }
7065 }
7066
7067 public void resumeAppSwitches() {
7068 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
7069 != PackageManager.PERMISSION_GRANTED) {
7070 throw new SecurityException("Requires permission "
7071 + android.Manifest.permission.STOP_APP_SWITCHES);
7072 }
7073
7074 synchronized(this) {
7075 // Note that we don't execute any pending app switches... we will
7076 // let those wait until either the timeout, or the next start
7077 // activity request.
7078 mAppSwitchesAllowedTime = 0;
7079 }
7080 }
7081
7082 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
7083 String name) {
7084 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
7085 return true;
7086 }
7087
7088 final int perm = checkComponentPermission(
7089 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -08007090 callingUid, -1, true);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007091 if (perm == PackageManager.PERMISSION_GRANTED) {
7092 return true;
7093 }
7094
Joe Onorato8a9b2202010-02-26 18:56:32 -08007095 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007096 return false;
7097 }
7098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 public void setDebugApp(String packageName, boolean waitForDebugger,
7100 boolean persistent) {
7101 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
7102 "setDebugApp()");
7103
7104 // Note that this is not really thread safe if there are multiple
7105 // callers into it at the same time, but that's not a situation we
7106 // care about.
7107 if (persistent) {
7108 final ContentResolver resolver = mContext.getContentResolver();
7109 Settings.System.putString(
7110 resolver, Settings.System.DEBUG_APP,
7111 packageName);
7112 Settings.System.putInt(
7113 resolver, Settings.System.WAIT_FOR_DEBUGGER,
7114 waitForDebugger ? 1 : 0);
7115 }
7116
7117 synchronized (this) {
7118 if (!persistent) {
7119 mOrigDebugApp = mDebugApp;
7120 mOrigWaitForDebugger = mWaitForDebugger;
7121 }
7122 mDebugApp = packageName;
7123 mWaitForDebugger = waitForDebugger;
7124 mDebugTransient = !persistent;
7125 if (packageName != null) {
7126 final long origId = Binder.clearCallingIdentity();
Amith Yamasani483f3b02012-03-13 16:08:00 -07007127 forceStopPackageLocked(packageName, -1, false, false, true, true, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 Binder.restoreCallingIdentity(origId);
7129 }
7130 }
7131 }
7132
Siva Velusamy92a8b222012-03-09 16:24:04 -08007133 void setOpenGlTraceApp(ApplicationInfo app, String processName) {
7134 synchronized (this) {
7135 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7136 if (!isDebuggable) {
7137 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7138 throw new SecurityException("Process not debuggable: " + app.packageName);
7139 }
7140 }
7141
7142 mOpenGlTraceApp = processName;
7143 }
7144 }
7145
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07007146 void setProfileApp(ApplicationInfo app, String processName, String profileFile,
7147 ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
7148 synchronized (this) {
7149 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
7150 if (!isDebuggable) {
7151 if ((app.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
7152 throw new SecurityException("Process not debuggable: " + app.packageName);
7153 }
7154 }
7155 mProfileApp = processName;
7156 mProfileFile = profileFile;
7157 if (mProfileFd != null) {
7158 try {
7159 mProfileFd.close();
7160 } catch (IOException e) {
7161 }
7162 mProfileFd = null;
7163 }
7164 mProfileFd = profileFd;
7165 mProfileType = 0;
7166 mAutoStopProfiler = autoStopProfiler;
7167 }
7168 }
7169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007170 public void setAlwaysFinish(boolean enabled) {
7171 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
7172 "setAlwaysFinish()");
7173
7174 Settings.System.putInt(
7175 mContext.getContentResolver(),
7176 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
7177
7178 synchronized (this) {
7179 mAlwaysFinishActivities = enabled;
7180 }
7181 }
7182
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007183 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007184 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007185 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007186 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007187 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007188 }
7189 }
7190
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08007191 public boolean isUserAMonkey() {
7192 // For now the fact that there is a controller implies
7193 // we have a monkey.
7194 synchronized (this) {
7195 return mController != null;
7196 }
7197 }
7198
Jeff Sharkeya4620792011-05-20 15:29:23 -07007199 public void registerProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007200 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
7201 "registerProcessObserver()");
7202 synchronized (this) {
7203 mProcessObservers.register(observer);
7204 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007205 }
7206
7207 public void unregisterProcessObserver(IProcessObserver observer) {
Dianne Hackborn21fbd1f2012-02-10 10:38:10 -08007208 synchronized (this) {
7209 mProcessObservers.unregister(observer);
7210 }
Jeff Sharkeya4620792011-05-20 15:29:23 -07007211 }
7212
Daniel Sandler69a48172010-06-23 16:29:36 -04007213 public void setImmersive(IBinder token, boolean immersive) {
7214 synchronized(this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007215 ActivityRecord r = mMainStack.isInStackLocked(token);
7216 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007217 throw new IllegalArgumentException();
7218 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007219 r.immersive = immersive;
7220 }
7221 }
7222
7223 public boolean isImmersive(IBinder token) {
7224 synchronized (this) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -07007225 ActivityRecord r = mMainStack.isInStackLocked(token);
7226 if (r == null) {
Daniel Sandler69a48172010-06-23 16:29:36 -04007227 throw new IllegalArgumentException();
7228 }
Daniel Sandler69a48172010-06-23 16:29:36 -04007229 return r.immersive;
7230 }
7231 }
7232
7233 public boolean isTopActivityImmersive() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08007234 enforceNotIsolatedCaller("startActivity");
Daniel Sandler69a48172010-06-23 16:29:36 -04007235 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007236 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Daniel Sandler69a48172010-06-23 16:29:36 -04007237 return (r != null) ? r.immersive : false;
7238 }
7239 }
7240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 public final void enterSafeMode() {
7242 synchronized(this) {
7243 // It only makes sense to do this before the system is ready
7244 // and started launching other packages.
7245 if (!mSystemReady) {
7246 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007247 AppGlobals.getPackageManager().enterSafeMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007248 } catch (RemoteException e) {
7249 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 }
7251 }
7252 }
7253
Jeff Brownb09abc12011-01-13 21:08:27 -08007254 public final void showSafeModeOverlay() {
7255 View v = LayoutInflater.from(mContext).inflate(
7256 com.android.internal.R.layout.safe_mode, null);
7257 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
7258 lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
7259 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
7260 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
Fabrice Di Meglioaac0d4e2012-07-19 19:21:26 -07007261 lp.gravity = Gravity.BOTTOM | Gravity.START;
Jeff Brownb09abc12011-01-13 21:08:27 -08007262 lp.format = v.getBackground().getOpacity();
7263 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
7264 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
7265 ((WindowManager)mContext.getSystemService(
7266 Context.WINDOW_SERVICE)).addView(v, lp);
7267 }
7268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007269 public void noteWakeupAlarm(IIntentSender sender) {
7270 if (!(sender instanceof PendingIntentRecord)) {
7271 return;
7272 }
7273 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
7274 synchronized (stats) {
7275 if (mBatteryStatsService.isOnBattery()) {
7276 mBatteryStatsService.enforceCallingPermission();
7277 PendingIntentRecord rec = (PendingIntentRecord)sender;
7278 int MY_UID = Binder.getCallingUid();
7279 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
7280 BatteryStatsImpl.Uid.Pkg pkg =
7281 stats.getPackageStatsLocked(uid, rec.key.packageName);
7282 pkg.incWakeupsLocked();
7283 }
7284 }
7285 }
7286
Dianne Hackborn64825172011-03-02 21:32:58 -08007287 public boolean killPids(int[] pids, String pReason, boolean secure) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007289 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007291 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007292 // XXX Note: don't acquire main activity lock here, because the window
7293 // manager calls in with its locks held.
7294
7295 boolean killed = false;
7296 synchronized (mPidsSelfLocked) {
7297 int[] types = new int[pids.length];
7298 int worstType = 0;
7299 for (int i=0; i<pids.length; i++) {
7300 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7301 if (proc != null) {
7302 int type = proc.setAdj;
7303 types[i] = type;
7304 if (type > worstType) {
7305 worstType = type;
7306 }
7307 }
7308 }
7309
Dianne Hackborn64825172011-03-02 21:32:58 -08007310 // If the worst oom_adj is somewhere in the hidden proc LRU range,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007311 // then constrain it so we will kill all hidden procs.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007312 if (worstType < ProcessList.HIDDEN_APP_MAX_ADJ
7313 && worstType > ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07007314 worstType = ProcessList.HIDDEN_APP_MIN_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 }
Dianne Hackborn64825172011-03-02 21:32:58 -08007316
7317 // If this is not a secure call, don't let it kill processes that
7318 // are important.
Dianne Hackborne02c88a2011-10-28 13:58:15 -07007319 if (!secure && worstType < ProcessList.SERVICE_ADJ) {
7320 worstType = ProcessList.SERVICE_ADJ;
Dianne Hackborn64825172011-03-02 21:32:58 -08007321 }
7322
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007323 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007324 for (int i=0; i<pids.length; i++) {
7325 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
7326 if (proc == null) {
7327 continue;
7328 }
7329 int adj = proc.setAdj;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007330 if (adj >= worstType && !proc.killedBackground) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07007331 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07007332 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
7333 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007334 killed = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -07007335 proc.killedBackground = true;
7336 Process.killProcessQuiet(pids[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 }
7338 }
7339 }
7340 return killed;
7341 }
Jeff Sharkeyb9a07012012-03-22 17:00:04 -07007342
7343 @Override
7344 public boolean killProcessesBelowForeground(String reason) {
7345 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7346 throw new SecurityException("killProcessesBelowForeground() only available to system");
7347 }
7348
7349 return killProcessesBelowAdj(ProcessList.FOREGROUND_APP_ADJ, reason);
7350 }
7351
7352 private boolean killProcessesBelowAdj(int belowAdj, String reason) {
7353 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
7354 throw new SecurityException("killProcessesBelowAdj() only available to system");
7355 }
7356
7357 boolean killed = false;
7358 synchronized (mPidsSelfLocked) {
7359 final int size = mPidsSelfLocked.size();
7360 for (int i = 0; i < size; i++) {
7361 final int pid = mPidsSelfLocked.keyAt(i);
7362 final ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7363 if (proc == null) continue;
7364
7365 final int adj = proc.setAdj;
7366 if (adj > belowAdj && !proc.killedBackground) {
7367 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
7368 EventLog.writeEvent(
7369 EventLogTags.AM_KILL, proc.pid, proc.processName, adj, reason);
7370 killed = true;
7371 proc.killedBackground = true;
7372 Process.killProcessQuiet(pid);
7373 }
7374 }
7375 }
7376 return killed;
7377 }
7378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 public final void startRunning(String pkg, String cls, String action,
7380 String data) {
7381 synchronized(this) {
7382 if (mStartRunning) {
7383 return;
7384 }
7385 mStartRunning = true;
7386 mTopComponent = pkg != null && cls != null
7387 ? new ComponentName(pkg, cls) : null;
7388 mTopAction = action != null ? action : Intent.ACTION_MAIN;
7389 mTopData = data;
7390 if (!mSystemReady) {
7391 return;
7392 }
7393 }
7394
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007395 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396 }
7397
7398 private void retrieveSettings() {
7399 final ContentResolver resolver = mContext.getContentResolver();
7400 String debugApp = Settings.System.getString(
7401 resolver, Settings.System.DEBUG_APP);
7402 boolean waitForDebugger = Settings.System.getInt(
7403 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
7404 boolean alwaysFinishActivities = Settings.System.getInt(
7405 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
7406
7407 Configuration configuration = new Configuration();
7408 Settings.System.getConfiguration(resolver, configuration);
7409
7410 synchronized (this) {
7411 mDebugApp = mOrigDebugApp = debugApp;
7412 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
7413 mAlwaysFinishActivities = alwaysFinishActivities;
7414 // This happens before any activities are started, so we can
7415 // change mConfiguration in-place.
Dianne Hackborn813075a62011-11-14 17:45:19 -08007416 updateConfigurationLocked(configuration, null, false, true);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007417 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 }
7419 }
7420
7421 public boolean testIsSystemReady() {
7422 // no need to synchronize(this) just to read & return the value
7423 return mSystemReady;
7424 }
7425
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007426 private static File getCalledPreBootReceiversFile() {
7427 File dataDir = Environment.getDataDirectory();
7428 File systemDir = new File(dataDir, "system");
7429 File fname = new File(systemDir, "called_pre_boots.dat");
7430 return fname;
7431 }
Dianne Hackborn661cd522011-08-22 00:26:20 -07007432
7433 static final int LAST_DONE_VERSION = 10000;
7434
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007435 private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
7436 ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
7437 File file = getCalledPreBootReceiversFile();
7438 FileInputStream fis = null;
7439 try {
7440 fis = new FileInputStream(file);
7441 DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007442 int fvers = dis.readInt();
7443 if (fvers == LAST_DONE_VERSION) {
7444 String vers = dis.readUTF();
7445 String codename = dis.readUTF();
7446 String build = dis.readUTF();
7447 if (android.os.Build.VERSION.RELEASE.equals(vers)
7448 && android.os.Build.VERSION.CODENAME.equals(codename)
7449 && android.os.Build.VERSION.INCREMENTAL.equals(build)) {
7450 int num = dis.readInt();
7451 while (num > 0) {
7452 num--;
7453 String pkg = dis.readUTF();
7454 String cls = dis.readUTF();
7455 lastDoneReceivers.add(new ComponentName(pkg, cls));
7456 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007457 }
7458 }
7459 } catch (FileNotFoundException e) {
7460 } catch (IOException e) {
7461 Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
7462 } finally {
7463 if (fis != null) {
7464 try {
7465 fis.close();
7466 } catch (IOException e) {
7467 }
7468 }
7469 }
7470 return lastDoneReceivers;
7471 }
7472
7473 private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
7474 File file = getCalledPreBootReceiversFile();
7475 FileOutputStream fos = null;
7476 DataOutputStream dos = null;
7477 try {
7478 Slog.i(TAG, "Writing new set of last done pre-boot receivers...");
7479 fos = new FileOutputStream(file);
7480 dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
Dianne Hackborn661cd522011-08-22 00:26:20 -07007481 dos.writeInt(LAST_DONE_VERSION);
7482 dos.writeUTF(android.os.Build.VERSION.RELEASE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007483 dos.writeUTF(android.os.Build.VERSION.CODENAME);
Dianne Hackborn661cd522011-08-22 00:26:20 -07007484 dos.writeUTF(android.os.Build.VERSION.INCREMENTAL);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007485 dos.writeInt(list.size());
7486 for (int i=0; i<list.size(); i++) {
7487 dos.writeUTF(list.get(i).getPackageName());
7488 dos.writeUTF(list.get(i).getClassName());
7489 }
7490 } catch (IOException e) {
7491 Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
7492 file.delete();
7493 } finally {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07007494 FileUtils.sync(fos);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007495 if (dos != null) {
7496 try {
7497 dos.close();
7498 } catch (IOException e) {
7499 // TODO Auto-generated catch block
7500 e.printStackTrace();
7501 }
7502 }
7503 }
7504 }
7505
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007506 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 synchronized(this) {
7508 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007509 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510 return;
7511 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007512
7513 // Check to see if there are any update receivers to run.
7514 if (!mDidUpdate) {
7515 if (mWaitingUpdate) {
7516 return;
7517 }
7518 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
7519 List<ResolveInfo> ris = null;
7520 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007521 ris = AppGlobals.getPackageManager().queryIntentReceivers(
Amith Yamasani483f3b02012-03-13 16:08:00 -07007522 intent, null, 0, 0);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007523 } catch (RemoteException e) {
7524 }
7525 if (ris != null) {
7526 for (int i=ris.size()-1; i>=0; i--) {
7527 if ((ris.get(i).activityInfo.applicationInfo.flags
7528 &ApplicationInfo.FLAG_SYSTEM) == 0) {
7529 ris.remove(i);
7530 }
7531 }
7532 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007533
7534 ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
7535
7536 final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007537 for (int i=0; i<ris.size(); i++) {
7538 ActivityInfo ai = ris.get(i).activityInfo;
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007539 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7540 if (lastDoneReceivers.contains(comp)) {
7541 ris.remove(i);
7542 i--;
7543 }
7544 }
Dianne Hackborn29aae6f2011-08-18 18:30:09 -07007545
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007546 for (int i=0; i<ris.size(); i++) {
7547 ActivityInfo ai = ris.get(i).activityInfo;
7548 ComponentName comp = new ComponentName(ai.packageName, ai.name);
7549 doneReceivers.add(comp);
7550 intent.setComponent(comp);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007551 IIntentReceiver finisher = null;
Dianne Hackbornd6847842010-01-12 18:14:19 -08007552 if (i == ris.size()-1) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007553 finisher = new IIntentReceiver.Stub() {
7554 public void performReceive(Intent intent, int resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -07007555 String data, Bundle extras, boolean ordered,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07007556 boolean sticky) {
7557 // The raw IIntentReceiver interface is called
7558 // with the AM lock held, so redispatch to
7559 // execute our code without the lock.
7560 mHandler.post(new Runnable() {
7561 public void run() {
7562 synchronized (ActivityManagerService.this) {
7563 mDidUpdate = true;
7564 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007565 writeLastDonePreBootReceivers(doneReceivers);
Dianne Hackborn661cd522011-08-22 00:26:20 -07007566 showBootMessage(mContext.getText(
7567 R.string.android_upgrading_complete),
7568 false);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07007569 systemReady(goingCallback);
7570 }
7571 });
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007572 }
7573 };
7574 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007575 Slog.i(TAG, "Sending system update to: " + intent.getComponent());
Amith Yamasani742a6712011-05-04 14:49:28 -07007576 /* TODO: Send this to all users */
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007577 broadcastIntentLocked(null, null, intent, null, finisher,
Amith Yamasani742a6712011-05-04 14:49:28 -07007578 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID,
Amith Yamasanic600e21f2012-02-14 16:08:07 -08007579 0 /* UserId zero */);
Dianne Hackbornd6847842010-01-12 18:14:19 -08007580 if (finisher != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007581 mWaitingUpdate = true;
7582 }
7583 }
7584 }
7585 if (mWaitingUpdate) {
7586 return;
7587 }
7588 mDidUpdate = true;
7589 }
7590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007591 mSystemReady = true;
7592 if (!mStartRunning) {
7593 return;
7594 }
7595 }
7596
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007597 ArrayList<ProcessRecord> procsToKill = null;
7598 synchronized(mPidsSelfLocked) {
7599 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
7600 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
7601 if (!isAllowedWhileBooting(proc.info)){
7602 if (procsToKill == null) {
7603 procsToKill = new ArrayList<ProcessRecord>();
7604 }
7605 procsToKill.add(proc);
7606 }
7607 }
7608 }
7609
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007610 synchronized(this) {
7611 if (procsToKill != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007612 for (int i=procsToKill.size()-1; i>=0; i--) {
7613 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007614 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08007615 removeProcessLocked(proc, true, false, "system update done");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007616 }
7617 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007618
7619 // Now that we have cleaned up any update processes, we
7620 // are ready to start launching real processes and know that
7621 // we won't trample on them any more.
7622 mProcessesReady = true;
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007623 }
7624
Joe Onorato8a9b2202010-02-26 18:56:32 -08007625 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007626 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 SystemClock.uptimeMillis());
7628
7629 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007630 // Make sure we have no pre-ready processes sitting around.
7631
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007632 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
7633 ResolveInfo ri = mContext.getPackageManager()
7634 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07007635 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 CharSequence errorMsg = null;
7637 if (ri != null) {
7638 ActivityInfo ai = ri.activityInfo;
7639 ApplicationInfo app = ai.applicationInfo;
7640 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
7641 mTopAction = Intent.ACTION_FACTORY_TEST;
7642 mTopData = null;
7643 mTopComponent = new ComponentName(app.packageName,
7644 ai.name);
7645 } else {
7646 errorMsg = mContext.getResources().getText(
7647 com.android.internal.R.string.factorytest_not_system);
7648 }
7649 } else {
7650 errorMsg = mContext.getResources().getText(
7651 com.android.internal.R.string.factorytest_no_action);
7652 }
7653 if (errorMsg != null) {
7654 mTopAction = null;
7655 mTopData = null;
7656 mTopComponent = null;
7657 Message msg = Message.obtain();
7658 msg.what = SHOW_FACTORY_ERROR_MSG;
7659 msg.getData().putCharSequence("msg", errorMsg);
7660 mHandler.sendMessage(msg);
7661 }
7662 }
7663 }
7664
7665 retrieveSettings();
7666
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07007667 if (goingCallback != null) goingCallback.run();
7668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007669 synchronized (this) {
7670 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
7671 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007672 List apps = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07007673 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007674 if (apps != null) {
7675 int N = apps.size();
7676 int i;
7677 for (i=0; i<N; i++) {
7678 ApplicationInfo info
7679 = (ApplicationInfo)apps.get(i);
7680 if (info != null &&
7681 !info.packageName.equals("android")) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007682 addAppLocked(info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007683 }
7684 }
7685 }
7686 } catch (RemoteException ex) {
7687 // pm is in same process, this will never happen.
7688 }
7689 }
7690
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007691 // Start up initial activity.
7692 mBooting = true;
7693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007695 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 Message msg = Message.obtain();
7697 msg.what = SHOW_UID_ERROR_MSG;
7698 mHandler.sendMessage(msg);
7699 }
7700 } catch (RemoteException e) {
7701 }
7702
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007703 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 }
7705 }
7706
Dan Egnorb7f03672009-12-09 16:22:32 -08007707 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007708 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007709 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007710 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007711 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 startAppProblemLocked(app);
7713 app.stopFreezingAllLocked();
7714 return handleAppCrashLocked(app);
7715 }
7716
Dan Egnorb7f03672009-12-09 16:22:32 -08007717 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007718 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08007720 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08007721 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
7722 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007723 startAppProblemLocked(app);
7724 app.stopFreezingAllLocked();
7725 }
7726
7727 /**
7728 * Generate a process error record, suitable for attachment to a ProcessRecord.
7729 *
7730 * @param app The ProcessRecord in which the error occurred.
7731 * @param condition Crashing, Application Not Responding, etc. Values are defined in
7732 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08007733 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 * @param shortMsg Short message describing the crash.
7735 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08007736 * @param stackTrace Full crash stack trace, may be null.
7737 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007738 * @return Returns a fully-formed AppErrorStateInfo record.
7739 */
7740 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08007741 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007742 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08007743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007744 report.condition = condition;
7745 report.processName = app.processName;
7746 report.pid = app.pid;
7747 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08007748 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 report.shortMsg = shortMsg;
7750 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08007751 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007752
7753 return report;
7754 }
7755
Dan Egnor42471dd2010-01-07 17:25:22 -08007756 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007757 synchronized (this) {
7758 app.crashing = false;
7759 app.crashingReport = null;
7760 app.notResponding = false;
7761 app.notRespondingReport = null;
7762 if (app.anrDialog == fromDialog) {
7763 app.anrDialog = null;
7764 }
7765 if (app.waitDialog == fromDialog) {
7766 app.waitDialog = null;
7767 }
7768 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08007769 handleAppCrashLocked(app);
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007770 Slog.i(ActivityManagerService.TAG, "Killing " + app + ": user's request");
Dianne Hackborn8633e682010-04-22 16:03:41 -07007771 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
7772 app.processName, app.setAdj, "user's request after error");
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07007773 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007775 }
7776 }
Dan Egnor42471dd2010-01-07 17:25:22 -08007777
Dan Egnorb7f03672009-12-09 16:22:32 -08007778 private boolean handleAppCrashLocked(ProcessRecord app) {
Mike Lockwood86548c42011-09-13 17:21:46 -04007779 if (mHeadless) {
7780 Log.e(TAG, "handleAppCrashLocked: " + app.processName);
7781 return false;
7782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 long now = SystemClock.uptimeMillis();
7784
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007785 Long crashTime;
7786 if (!app.isolated) {
7787 crashTime = mProcessCrashTimes.get(app.info.processName, app.uid);
7788 } else {
7789 crashTime = null;
7790 }
Dianne Hackborn7d608422011-08-07 16:24:18 -07007791 if (crashTime != null && now < crashTime+ProcessList.MIN_CRASH_INTERVAL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007792 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08007793 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007794 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007795 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007796 app.info.processName, app.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007797 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
7798 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007800 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007802 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 }
7804 }
7805 if (!app.persistent) {
7806 // We don't want to start this process again until the user
7807 // explicitly does so... but for persistent process, we really
7808 // need to keep it running. If a persistent process is actually
7809 // repeatedly crashing, then badness for everyone.
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007810 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007811 app.info.processName);
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007812 if (!app.isolated) {
7813 // XXX We don't have a way to mark isolated processes
7814 // as bad, since they don't have a peristent identity.
7815 mBadProcesses.put(app.info.processName, app.uid, now);
7816 mProcessCrashTimes.remove(app.info.processName, app.uid);
7817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007818 app.bad = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 app.removed = true;
Dianne Hackborn130b0d22011-07-26 22:07:48 -07007820 // Don't let services in this process be restarted and potentially
7821 // annoy the user repeatedly. Unless it is persistent, since those
7822 // processes run critical code.
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -08007823 removeProcessLocked(app, false, false, "crash");
Dianne Hackborncb44d962011-03-10 17:02:27 -08007824 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007825 return false;
7826 }
Dianne Hackborncb44d962011-03-10 17:02:27 -08007827 mMainStack.resumeTopActivityLocked(null);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007828 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007829 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Kevin Hester-Chowd87a9be2012-03-05 08:01:00 -08007830 if (r != null && r.app == app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007831 // If the top running activity is from this crashing
7832 // process, then terminate it to avoid getting in a loop.
7833 Slog.w(TAG, " Force finishing activity "
7834 + r.intent.getComponent().flattenToShortString());
Dianne Hackbornbe707852011-11-11 14:32:10 -08007835 int index = mMainStack.indexOfActivityLocked(r);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007836 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007837 Activity.RESULT_CANCELED, null, "crashed");
Dianne Hackborn070783f2010-12-29 16:46:28 -08007838 // Also terminate any activities below it that aren't yet
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007839 // stopped, to avoid a situation where one will get
7840 // re-start our crashing activity once it gets resumed again.
7841 index--;
7842 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007843 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007844 if (r.state == ActivityState.RESUMED
7845 || r.state == ActivityState.PAUSING
7846 || r.state == ActivityState.PAUSED) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -08007847 if (!r.isHomeActivity || mHomeProcess != r.app) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007848 Slog.w(TAG, " Force finishing activity "
7849 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007850 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07007851 Activity.RESULT_CANCELED, null, "crashed");
7852 }
7853 }
7854 }
7855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007856 }
7857
7858 // Bump up the crash count of any services currently running in the proc.
7859 if (app.services.size() != 0) {
7860 // Any services running in the application need to be placed
7861 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07007862 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007864 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 sr.crashCount++;
7866 }
7867 }
Mattias Larssona4fd0072010-06-22 22:37:03 +02007868
7869 // If the crashing process is what we consider to be the "home process" and it has been
7870 // replaced by a third-party app, clear the package preferred activities from packages
7871 // with a home activity running in the process to prevent a repeatedly crashing app
7872 // from blocking the user to manually clear the list.
7873 if (app == mHomeProcess && mHomeProcess.activities.size() > 0
7874 && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
7875 Iterator it = mHomeProcess.activities.iterator();
7876 while (it.hasNext()) {
Jean-Baptiste Queru5ea89f72010-07-30 09:30:31 -07007877 ActivityRecord r = (ActivityRecord)it.next();
Mattias Larssona4fd0072010-06-22 22:37:03 +02007878 if (r.isHomeActivity) {
7879 Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
7880 try {
7881 ActivityThread.getPackageManager()
7882 .clearPackagePreferredActivities(r.packageName);
7883 } catch (RemoteException c) {
7884 // pm is in same process, this will never happen.
7885 }
7886 }
7887 }
7888 }
7889
Dianne Hackborna0c283e2012-02-09 10:47:01 -08007890 if (!app.isolated) {
7891 // XXX Can't keep track of crash times for isolated processes,
7892 // because they don't have a perisistent identity.
7893 mProcessCrashTimes.put(app.info.processName, app.uid, now);
7894 }
7895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 return true;
7897 }
7898
7899 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08007900 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
7901 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 skipCurrentReceiverLocked(app);
7903 }
7904
7905 void skipCurrentReceiverLocked(ProcessRecord app) {
Christopher Tatef46723b2012-01-26 14:19:24 -08007906 for (BroadcastQueue queue : mBroadcastQueues) {
7907 queue.skipCurrentReceiverLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007908 }
7909 }
7910
Dan Egnor60d87622009-12-16 16:32:58 -08007911 /**
7912 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
7913 * The application process will exit immediately after this call returns.
7914 * @param app object of the crashing app, null for the system server
7915 * @param crashInfo describing the exception
7916 */
7917 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08007918 ProcessRecord r = findAppProcess(app, "Crash");
Jeff Sharkeya353d262011-10-28 11:12:06 -07007919 final String processName = app == null ? "system_server"
7920 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08007921
7922 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07007923 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08007924 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08007925 crashInfo.exceptionClassName,
7926 crashInfo.exceptionMessage,
7927 crashInfo.throwFileName,
7928 crashInfo.throwLineNumber);
7929
Jeff Sharkeya353d262011-10-28 11:12:06 -07007930 addErrorToDropBox("crash", r, processName, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08007931
7932 crashApplication(r, crashInfo);
7933 }
7934
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007935 public void handleApplicationStrictModeViolation(
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007936 IBinder app,
7937 int violationMask,
7938 StrictMode.ViolationInfo info) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08007939 ProcessRecord r = findAppProcess(app, "StrictMode");
7940 if (r == null) {
7941 return;
7942 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007943
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007944 if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -08007945 Integer stackFingerprint = info.hashCode();
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007946 boolean logIt = true;
7947 synchronized (mAlreadyLoggedViolatedStacks) {
7948 if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
7949 logIt = false;
7950 // TODO: sub-sample into EventLog for these, with
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007951 // the info.durationMillis? Then we'd get
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007952 // the relative pain numbers, without logging all
7953 // the stack traces repeatedly. We'd want to do
7954 // likewise in the client code, which also does
7955 // dup suppression, before the Binder call.
7956 } else {
7957 if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
7958 mAlreadyLoggedViolatedStacks.clear();
7959 }
7960 mAlreadyLoggedViolatedStacks.add(stackFingerprint);
7961 }
7962 }
7963 if (logIt) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007964 logStrictModeViolationToDropBox(r, info);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007965 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007966 }
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007967
7968 if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
7969 AppErrorResult result = new AppErrorResult();
7970 synchronized (this) {
7971 final long origId = Binder.clearCallingIdentity();
7972
7973 Message msg = Message.obtain();
7974 msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
7975 HashMap<String, Object> data = new HashMap<String, Object>();
7976 data.put("result", result);
7977 data.put("app", r);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07007978 data.put("violationMask", violationMask);
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007979 data.put("info", info);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007980 msg.obj = data;
7981 mHandler.sendMessage(msg);
7982
7983 Binder.restoreCallingIdentity(origId);
7984 }
7985 int res = result.get();
Dianne Hackbornb424b632010-08-18 15:59:05 -07007986 Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07007987 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07007988 }
7989
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007990 // Depending on the policy in effect, there could be a bunch of
7991 // these in quick succession so we try to batch these together to
7992 // minimize disk writes, number of dropbox entries, and maximize
7993 // compression, by having more fewer, larger records.
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07007994 private void logStrictModeViolationToDropBox(
7995 ProcessRecord process,
7996 StrictMode.ViolationInfo info) {
7997 if (info == null) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07007998 return;
7999 }
8000 final boolean isSystemApp = process == null ||
8001 (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
8002 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07008003 final String processName = process == null ? "unknown" : process.processName;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008004 final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
8005 final DropBoxManager dbox = (DropBoxManager)
8006 mContext.getSystemService(Context.DROPBOX_SERVICE);
8007
8008 // Exit early if the dropbox isn't configured to accept this report type.
8009 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
8010
8011 boolean bufferWasEmpty;
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008012 boolean needsFlush;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008013 final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
8014 synchronized (sb) {
8015 bufferWasEmpty = sb.length() == 0;
Jeff Sharkeya353d262011-10-28 11:12:06 -07008016 appendDropBoxProcessHeaders(process, processName, sb);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008017 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
8018 sb.append("System-App: ").append(isSystemApp).append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008019 sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
8020 if (info.violationNumThisLoop != 0) {
8021 sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
8022 }
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07008023 if (info.numAnimationsRunning != 0) {
8024 sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n");
8025 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07008026 if (info.broadcastIntentAction != null) {
8027 sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n");
8028 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08008029 if (info.durationMillis != -1) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008030 sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008031 }
Brad Fitzpatrickbfbe5772011-01-19 00:10:58 -08008032 if (info.numInstances != -1) {
8033 sb.append("Instance-Count: ").append(info.numInstances).append("\n");
8034 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08008035 if (info.tags != null) {
8036 for (String tag : info.tags) {
8037 sb.append("Span-Tag: ").append(tag).append("\n");
8038 }
8039 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008040 sb.append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07008041 if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
8042 sb.append(info.crashInfo.stackTrace);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008043 }
8044 sb.append("\n");
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008045
8046 // Only buffer up to ~64k. Various logging bits truncate
8047 // things at 128k.
8048 needsFlush = (sb.length() > 64 * 1024);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008049 }
8050
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008051 // Flush immediately if the buffer's grown too large, or this
8052 // is a non-system app. Non-system apps are isolated with a
8053 // different tag & policy and not batched.
8054 //
8055 // Batching is useful during internal testing with
8056 // StrictMode settings turned up high. Without batching,
8057 // thousands of separate files could be created on boot.
8058 if (!isSystemApp || needsFlush) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008059 new Thread("Error dump: " + dropboxTag) {
8060 @Override
8061 public void run() {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008062 String report;
8063 synchronized (sb) {
8064 report = sb.toString();
8065 sb.delete(0, sb.length());
8066 sb.trimToSize();
8067 }
8068 if (report.length() != 0) {
8069 dbox.addText(dropboxTag, report);
8070 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008071 }
8072 }.start();
8073 return;
8074 }
8075
8076 // System app batching:
8077 if (!bufferWasEmpty) {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07008078 // An existing dropbox-writing thread is outstanding, so
8079 // we don't need to start it up. The existing thread will
8080 // catch the buffer appends we just did.
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008081 return;
8082 }
8083
8084 // Worker thread to both batch writes and to avoid blocking the caller on I/O.
8085 // (After this point, we shouldn't access AMS internal data structures.)
8086 new Thread("Error dump: " + dropboxTag) {
8087 @Override
8088 public void run() {
8089 // 5 second sleep to let stacks arrive and be batched together
8090 try {
8091 Thread.sleep(5000); // 5 seconds
8092 } catch (InterruptedException e) {}
8093
8094 String errorReport;
8095 synchronized (mStrictModeBuffer) {
8096 errorReport = mStrictModeBuffer.toString();
8097 if (errorReport.length() == 0) {
8098 return;
8099 }
8100 mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
8101 mStrictModeBuffer.trimToSize();
8102 }
8103 dbox.addText(dropboxTag, errorReport);
8104 }
8105 }.start();
8106 }
8107
Dan Egnor60d87622009-12-16 16:32:58 -08008108 /**
8109 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
8110 * @param app object of the crashing app, null for the system server
8111 * @param tag reported by the caller
8112 * @param crashInfo describing the context of the error
8113 * @return true if the process should exit immediately (WTF is fatal)
8114 */
8115 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08008116 ApplicationErrorReport.CrashInfo crashInfo) {
Dianne Hackborncb44d962011-03-10 17:02:27 -08008117 ProcessRecord r = findAppProcess(app, "WTF");
Jeff Sharkeya353d262011-10-28 11:12:06 -07008118 final String processName = app == null ? "system_server"
8119 : (r == null ? "unknown" : r.processName);
Dan Egnor60d87622009-12-16 16:32:58 -08008120
8121 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
Jeff Sharkeya353d262011-10-28 11:12:06 -07008122 processName,
Dan Egnor2780e732010-01-22 14:47:35 -08008123 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08008124 tag, crashInfo.exceptionMessage);
8125
Jeff Sharkeya353d262011-10-28 11:12:06 -07008126 addErrorToDropBox("wtf", r, processName, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08008127
Dianne Hackborn1ab43772011-03-15 14:38:02 -07008128 if (r != null && r.pid != Process.myPid() &&
8129 Settings.Secure.getInt(mContext.getContentResolver(),
8130 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08008131 crashApplication(r, crashInfo);
8132 return true;
8133 } else {
8134 return false;
8135 }
8136 }
8137
8138 /**
8139 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
8140 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
8141 */
Dianne Hackborncb44d962011-03-10 17:02:27 -08008142 private ProcessRecord findAppProcess(IBinder app, String reason) {
Dan Egnor60d87622009-12-16 16:32:58 -08008143 if (app == null) {
8144 return null;
8145 }
8146
8147 synchronized (this) {
8148 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
8149 final int NA = apps.size();
8150 for (int ia=0; ia<NA; ia++) {
8151 ProcessRecord p = apps.valueAt(ia);
8152 if (p.thread != null && p.thread.asBinder() == app) {
8153 return p;
8154 }
8155 }
8156 }
8157
Dianne Hackborncb44d962011-03-10 17:02:27 -08008158 Slog.w(TAG, "Can't find mystery application for " + reason
8159 + " from pid=" + Binder.getCallingPid()
8160 + " uid=" + Binder.getCallingUid() + ": " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08008161 return null;
8162 }
8163 }
8164
8165 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008166 * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
8167 * to append various headers to the dropbox log text.
Dan Egnor60d87622009-12-16 16:32:58 -08008168 */
Jeff Sharkeya353d262011-10-28 11:12:06 -07008169 private void appendDropBoxProcessHeaders(ProcessRecord process, String processName,
8170 StringBuilder sb) {
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008171 // Watchdog thread ends up invoking this function (with
8172 // a null ProcessRecord) to add the stack file to dropbox.
8173 // Do not acquire a lock on this (am) in such cases, as it
8174 // could cause a potential deadlock, if and when watchdog
8175 // is invoked due to unavailability of lock on am and it
8176 // would prevent watchdog from killing system_server.
8177 if (process == null) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008178 sb.append("Process: ").append(processName).append("\n");
Vairavan Srinivasan68a4e0a2011-02-14 20:45:59 -08008179 return;
8180 }
Brad Fitzpatrick1e02d362010-09-10 09:19:50 -07008181 // Note: ProcessRecord 'process' is guarded by the service
8182 // instance. (notably process.pkgList, which could otherwise change
8183 // concurrently during execution of this method)
8184 synchronized (this) {
Jeff Sharkeya353d262011-10-28 11:12:06 -07008185 sb.append("Process: ").append(processName).append("\n");
Dan Egnora455d192010-03-12 08:52:28 -08008186 int flags = process.info.flags;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008187 IPackageManager pm = AppGlobals.getPackageManager();
Dan Egnora455d192010-03-12 08:52:28 -08008188 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
8189 for (String pkg : process.pkgList) {
8190 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08008191 try {
Amith Yamasani483f3b02012-03-13 16:08:00 -07008192 PackageInfo pi = pm.getPackageInfo(pkg, 0, 0);
Dan Egnora455d192010-03-12 08:52:28 -08008193 if (pi != null) {
8194 sb.append(" v").append(pi.versionCode);
8195 if (pi.versionName != null) {
8196 sb.append(" (").append(pi.versionName).append(")");
8197 }
8198 }
8199 } catch (RemoteException e) {
8200 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08008201 }
Dan Egnora455d192010-03-12 08:52:28 -08008202 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08008203 }
Dan Egnora455d192010-03-12 08:52:28 -08008204 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008205 }
8206
8207 private static String processClass(ProcessRecord process) {
8208 if (process == null || process.pid == MY_PID) {
8209 return "system_server";
8210 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
8211 return "system_app";
8212 } else {
8213 return "data_app";
8214 }
8215 }
8216
8217 /**
8218 * Write a description of an error (crash, WTF, ANR) to the drop box.
8219 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
8220 * @param process which caused the error, null means the system server
8221 * @param activity which triggered the error, null if unknown
8222 * @param parent activity related to the error, null if unknown
8223 * @param subject line related to the error, null if absent
8224 * @param report in long form describing the error, null if absent
8225 * @param logFile to include in the report, null if none
8226 * @param crashInfo giving an application stack trace, null if absent
8227 */
8228 public void addErrorToDropBox(String eventType,
Jeff Sharkeya353d262011-10-28 11:12:06 -07008229 ProcessRecord process, String processName, ActivityRecord activity,
8230 ActivityRecord parent, String subject,
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07008231 final String report, final File logFile,
8232 final ApplicationErrorReport.CrashInfo crashInfo) {
8233 // NOTE -- this must never acquire the ActivityManagerService lock,
8234 // otherwise the watchdog may be prevented from resetting the system.
8235
8236 final String dropboxTag = processClass(process) + "_" + eventType;
8237 final DropBoxManager dbox = (DropBoxManager)
8238 mContext.getSystemService(Context.DROPBOX_SERVICE);
8239
8240 // Exit early if the dropbox isn't configured to accept this report type.
8241 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
8242
8243 final StringBuilder sb = new StringBuilder(1024);
Jeff Sharkeya353d262011-10-28 11:12:06 -07008244 appendDropBoxProcessHeaders(process, processName, sb);
Dan Egnora455d192010-03-12 08:52:28 -08008245 if (activity != null) {
8246 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
8247 }
8248 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
8249 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
8250 }
8251 if (parent != null && parent != activity) {
8252 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
8253 }
8254 if (subject != null) {
8255 sb.append("Subject: ").append(subject).append("\n");
8256 }
8257 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
Christian Lindeberg03d2ca62010-09-28 14:52:20 +02008258 if (Debug.isDebuggerConnected()) {
8259 sb.append("Debugger: Connected\n");
8260 }
Dan Egnora455d192010-03-12 08:52:28 -08008261 sb.append("\n");
8262
8263 // Do the rest in a worker thread to avoid blocking the caller on I/O
8264 // (After this point, we shouldn't access AMS internal data structures.)
8265 Thread worker = new Thread("Error dump: " + dropboxTag) {
8266 @Override
8267 public void run() {
8268 if (report != null) {
8269 sb.append(report);
8270 }
8271 if (logFile != null) {
8272 try {
8273 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
8274 } catch (IOException e) {
8275 Slog.e(TAG, "Error reading " + logFile, e);
8276 }
8277 }
8278 if (crashInfo != null && crashInfo.stackTrace != null) {
8279 sb.append(crashInfo.stackTrace);
8280 }
8281
8282 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
8283 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
8284 if (lines > 0) {
8285 sb.append("\n");
8286
8287 // Merge several logcat streams, and take the last N lines
8288 InputStreamReader input = null;
8289 try {
8290 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
8291 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
8292 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
8293
8294 try { logcat.getOutputStream().close(); } catch (IOException e) {}
8295 try { logcat.getErrorStream().close(); } catch (IOException e) {}
8296 input = new InputStreamReader(logcat.getInputStream());
8297
8298 int num;
8299 char[] buf = new char[8192];
8300 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
8301 } catch (IOException e) {
8302 Slog.e(TAG, "Error running logcat", e);
8303 } finally {
8304 if (input != null) try { input.close(); } catch (IOException e) {}
8305 }
8306 }
8307
8308 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08008309 }
Dan Egnora455d192010-03-12 08:52:28 -08008310 };
8311
Dianne Hackborn56385cc2012-04-30 15:07:47 -07008312 if (process == null) {
8313 // If process is null, we are being called from some internal code
8314 // and may be about to die -- run this synchronously.
8315 worker.run();
Dan Egnora455d192010-03-12 08:52:28 -08008316 } else {
8317 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08008318 }
8319 }
8320
8321 /**
8322 * Bring up the "unexpected error" dialog box for a crashing app.
8323 * Deal with edge cases (intercepts from instrumented applications,
8324 * ActivityController, error intent receivers, that sort of thing).
8325 * @param r the application crashing
8326 * @param crashInfo describing the failure
8327 */
8328 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008329 long timeMillis = System.currentTimeMillis();
8330 String shortMsg = crashInfo.exceptionClassName;
8331 String longMsg = crashInfo.exceptionMessage;
8332 String stackTrace = crashInfo.stackTrace;
8333 if (shortMsg != null && longMsg != null) {
8334 longMsg = shortMsg + ": " + longMsg;
8335 } else if (shortMsg != null) {
8336 longMsg = shortMsg;
8337 }
8338
Dan Egnor60d87622009-12-16 16:32:58 -08008339 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008340 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008341 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 try {
8343 String name = r != null ? r.processName : null;
8344 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08008345 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08008346 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008347 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 + " at watcher's request");
8349 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08008350 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008351 }
8352 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008353 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 }
8355 }
8356
8357 final long origId = Binder.clearCallingIdentity();
8358
8359 // If this process is running instrumentation, finish it.
8360 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008361 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08008363 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
8364 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 Bundle info = new Bundle();
8366 info.putString("shortMsg", shortMsg);
8367 info.putString("longMsg", longMsg);
8368 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
8369 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008370 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 }
8372
Dan Egnor60d87622009-12-16 16:32:58 -08008373 // If we can't identify the process or it's already exceeded its crash quota,
8374 // quit right away without showing a crash dialog.
8375 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008376 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08008377 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 }
8379
8380 Message msg = Message.obtain();
8381 msg.what = SHOW_ERROR_MSG;
8382 HashMap data = new HashMap();
8383 data.put("result", result);
8384 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 msg.obj = data;
8386 mHandler.sendMessage(msg);
8387
8388 Binder.restoreCallingIdentity(origId);
8389 }
8390
8391 int res = result.get();
8392
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008393 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 synchronized (this) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008395 if (r != null && !r.isolated) {
8396 // XXX Can't keep track of crash time for isolated processes,
8397 // since they don't have a persistent identity.
8398 mProcessCrashTimes.put(r.info.processName, r.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008399 SystemClock.uptimeMillis());
8400 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008401 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08008402 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008403 }
8404 }
8405
8406 if (appErrorIntent != null) {
8407 try {
8408 mContext.startActivity(appErrorIntent);
8409 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008410 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 }
Dan Egnorb7f03672009-12-09 16:22:32 -08008414
8415 Intent createAppErrorIntentLocked(ProcessRecord r,
8416 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
8417 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008418 if (report == null) {
8419 return null;
8420 }
8421 Intent result = new Intent(Intent.ACTION_APP_ERROR);
8422 result.setComponent(r.errorReportReceiver);
8423 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
8424 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8425 return result;
8426 }
8427
Dan Egnorb7f03672009-12-09 16:22:32 -08008428 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
8429 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008430 if (r.errorReportReceiver == null) {
8431 return null;
8432 }
8433
8434 if (!r.crashing && !r.notResponding) {
8435 return null;
8436 }
8437
Dan Egnorb7f03672009-12-09 16:22:32 -08008438 ApplicationErrorReport report = new ApplicationErrorReport();
8439 report.packageName = r.info.packageName;
8440 report.installerPackageName = r.errorReportReceiver.getPackageName();
8441 report.processName = r.processName;
8442 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01008443 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008444
Dan Egnorb7f03672009-12-09 16:22:32 -08008445 if (r.crashing) {
8446 report.type = ApplicationErrorReport.TYPE_CRASH;
8447 report.crashInfo = crashInfo;
8448 } else if (r.notResponding) {
8449 report.type = ApplicationErrorReport.TYPE_ANR;
8450 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008451
Dan Egnorb7f03672009-12-09 16:22:32 -08008452 report.anrInfo.activity = r.notRespondingReport.tag;
8453 report.anrInfo.cause = r.notRespondingReport.shortMsg;
8454 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008455 }
8456
Dan Egnorb7f03672009-12-09 16:22:32 -08008457 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02008458 }
8459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008460 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008461 enforceNotIsolatedCaller("getProcessesInErrorState");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008462 // assume our apps are happy - lazy create the list
8463 List<ActivityManager.ProcessErrorStateInfo> errList = null;
8464
Dianne Hackborn0c380492012-08-20 17:23:30 -07008465 final boolean allUsers = ActivityManager.checkUidPermission(
8466 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8467 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8468 int userId = UserHandle.getUserId(Binder.getCallingUid());
8469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 synchronized (this) {
8471
8472 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008473 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8474 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008475 if (!allUsers && app.userId != userId) {
8476 continue;
8477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008478 if ((app.thread != null) && (app.crashing || app.notResponding)) {
8479 // This one's in trouble, so we'll generate a report for it
8480 // crashes are higher priority (in case there's a crash *and* an anr)
8481 ActivityManager.ProcessErrorStateInfo report = null;
8482 if (app.crashing) {
8483 report = app.crashingReport;
8484 } else if (app.notResponding) {
8485 report = app.notRespondingReport;
8486 }
8487
8488 if (report != null) {
8489 if (errList == null) {
8490 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
8491 }
8492 errList.add(report);
8493 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008494 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008495 " crashing = " + app.crashing +
8496 " notResponding = " + app.notResponding);
8497 }
8498 }
8499 }
8500 }
8501
8502 return errList;
8503 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07008504
8505 static int oomAdjToImportance(int adj, ActivityManager.RunningAppProcessInfo currApp) {
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008506 if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008507 if (currApp != null) {
8508 currApp.lru = adj - ProcessList.HIDDEN_APP_MIN_ADJ + 1;
8509 }
8510 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008511 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
8512 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008513 } else if (adj >= ProcessList.HOME_APP_ADJ) {
8514 if (currApp != null) {
8515 currApp.lru = 0;
8516 }
8517 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07008518 } else if (adj >= ProcessList.SERVICE_ADJ) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07008519 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
8520 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
8521 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
8522 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
8523 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
8524 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
8525 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
8526 } else {
8527 return ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
8528 }
8529 }
8530
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008531 private void fillInProcMemInfo(ProcessRecord app,
8532 ActivityManager.RunningAppProcessInfo outInfo) {
8533 outInfo.pid = app.pid;
8534 outInfo.uid = app.info.uid;
8535 if (mHeavyWeightProcess == app) {
8536 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE;
8537 }
8538 if (app.persistent) {
8539 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
8540 }
Dianne Hackborn0c380492012-08-20 17:23:30 -07008541 if (app.hasActivities) {
8542 outInfo.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HAS_ACTIVITIES;
8543 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008544 outInfo.lastTrimLevel = app.trimMemoryLevel;
8545 int adj = app.curAdj;
8546 outInfo.importance = oomAdjToImportance(adj, outInfo);
8547 outInfo.importanceReasonCode = app.adjTypeCode;
8548 }
8549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008551 enforceNotIsolatedCaller("getRunningAppProcesses");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 // Lazy instantiation of list
8553 List<ActivityManager.RunningAppProcessInfo> runList = null;
Dianne Hackborn0c380492012-08-20 17:23:30 -07008554 final boolean allUsers = ActivityManager.checkUidPermission(
8555 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
8556 Binder.getCallingUid()) == PackageManager.PERMISSION_GRANTED;
8557 int userId = UserHandle.getUserId(Binder.getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008558 synchronized (this) {
8559 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008560 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8561 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn0c380492012-08-20 17:23:30 -07008562 if (!allUsers && app.userId != userId) {
8563 continue;
8564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
8566 // Generate process state info for running application
8567 ActivityManager.RunningAppProcessInfo currApp =
8568 new ActivityManager.RunningAppProcessInfo(app.processName,
8569 app.pid, app.getPackageList());
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008570 fillInProcMemInfo(app, currApp);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008571 if (app.adjSource instanceof ProcessRecord) {
8572 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008573 currApp.importanceReasonImportance = oomAdjToImportance(
8574 app.adjSourceOom, null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008575 } else if (app.adjSource instanceof ActivityRecord) {
8576 ActivityRecord r = (ActivityRecord)app.adjSource;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008577 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
8578 }
8579 if (app.adjTarget instanceof ComponentName) {
8580 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
8581 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008582 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008583 // + " lru=" + currApp.lru);
8584 if (runList == null) {
8585 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
8586 }
8587 runList.add(currApp);
8588 }
8589 }
8590 }
8591 return runList;
8592 }
8593
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008594 public List<ApplicationInfo> getRunningExternalApplications() {
Dianne Hackborna573f6a2012-02-09 16:12:18 -08008595 enforceNotIsolatedCaller("getRunningExternalApplications");
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008596 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
8597 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
8598 if (runningApps != null && runningApps.size() > 0) {
8599 Set<String> extList = new HashSet<String>();
8600 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
8601 if (app.pkgList != null) {
8602 for (String pkg : app.pkgList) {
8603 extList.add(pkg);
8604 }
8605 }
8606 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008607 IPackageManager pm = AppGlobals.getPackageManager();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008608 for (String pkg : extList) {
8609 try {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07008610 ApplicationInfo info = pm.getApplicationInfo(pkg, 0, UserHandle.getCallingUserId());
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07008611 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
8612 retList.add(info);
8613 }
8614 } catch (RemoteException e) {
8615 }
8616 }
8617 }
8618 return retList;
8619 }
8620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 @Override
Dianne Hackborn27ff9132012-03-06 14:57:58 -08008622 public void getMyMemoryState(ActivityManager.RunningAppProcessInfo outInfo) {
8623 enforceNotIsolatedCaller("getMyMemoryState");
8624 synchronized (this) {
8625 ProcessRecord proc;
8626 synchronized (mPidsSelfLocked) {
8627 proc = mPidsSelfLocked.get(Binder.getCallingPid());
8628 }
8629 fillInProcMemInfo(proc, outInfo);
8630 }
8631 }
8632
8633 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008635 if (checkCallingPermission(android.Manifest.permission.DUMP)
8636 != PackageManager.PERMISSION_GRANTED) {
8637 pw.println("Permission Denial: can't dump ActivityManager from from pid="
8638 + Binder.getCallingPid()
8639 + ", uid=" + Binder.getCallingUid()
8640 + " without permission "
8641 + android.Manifest.permission.DUMP);
8642 return;
8643 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008644
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008645 boolean dumpAll = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008646 boolean dumpClient = false;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008647 String dumpPackage = null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008648
8649 int opti = 0;
8650 while (opti < args.length) {
8651 String opt = args[opti];
8652 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
8653 break;
8654 }
8655 opti++;
8656 if ("-a".equals(opt)) {
8657 dumpAll = true;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008658 } else if ("-c".equals(opt)) {
8659 dumpClient = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008660 } else if ("-h".equals(opt)) {
8661 pw.println("Activity manager dump options:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008662 pw.println(" [-a] [-c] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008663 pw.println(" cmd may be one of:");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008664 pw.println(" a[ctivities]: activity stack state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008665 pw.println(" b[roadcasts] [PACKAGE_NAME]: broadcast state");
8666 pw.println(" i[ntents] [PACKAGE_NAME]: pending intent state");
8667 pw.println(" p[rocesses] [PACKAGE_NAME]: process state");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008668 pw.println(" o[om]: out of memory management");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008669 pw.println(" prov[iders] [COMP_SPEC ...]: content provider state");
Marco Nelissen18cb2872011-11-15 11:19:53 -08008670 pw.println(" provider [COMP_SPEC]: provider client-side state");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008671 pw.println(" s[ervices] [COMP_SPEC ...]: service state");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008672 pw.println(" service [COMP_SPEC]: service client-side state");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008673 pw.println(" package [PACKAGE_NAME]: all state related to given package");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008674 pw.println(" all: dump all activities");
8675 pw.println(" top: dump the top activity");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008676 pw.println(" cmd may also be a COMP_SPEC to dump activities.");
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07008677 pw.println(" COMP_SPEC may be a component name (com.foo/.myApp),");
8678 pw.println(" a partial substring in a component name, a");
8679 pw.println(" hex object identifier.");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008680 pw.println(" -a: include all available server state.");
8681 pw.println(" -c: include client state.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008683 } else {
8684 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008686 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008687
8688 long origId = Binder.clearCallingIdentity();
8689 boolean more = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008690 // Is the caller requesting to dump a particular piece of data?
8691 if (opti < args.length) {
8692 String cmd = args[opti];
8693 opti++;
8694 if ("activities".equals(cmd) || "a".equals(cmd)) {
8695 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008696 dumpActivitiesLocked(fd, pw, args, opti, true, dumpClient, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008698 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008699 String[] newArgs;
8700 String name;
8701 if (opti >= args.length) {
8702 name = null;
8703 newArgs = EMPTY_STRING_ARRAY;
8704 } else {
8705 name = args[opti];
8706 opti++;
8707 newArgs = new String[args.length - opti];
8708 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8709 args.length - opti);
8710 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008711 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008712 dumpBroadcastsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008713 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008714 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008715 String[] newArgs;
8716 String name;
8717 if (opti >= args.length) {
8718 name = null;
8719 newArgs = EMPTY_STRING_ARRAY;
8720 } else {
8721 name = args[opti];
8722 opti++;
8723 newArgs = new String[args.length - opti];
8724 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8725 args.length - opti);
8726 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008727 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008728 dumpPendingIntentsLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008729 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008730 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008731 String[] newArgs;
8732 String name;
8733 if (opti >= args.length) {
8734 name = null;
8735 newArgs = EMPTY_STRING_ARRAY;
8736 } else {
8737 name = args[opti];
8738 opti++;
8739 newArgs = new String[args.length - opti];
8740 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8741 args.length - opti);
8742 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008743 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008744 dumpProcessesLocked(fd, pw, args, opti, true, name);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008745 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07008746 } else if ("oom".equals(cmd) || "o".equals(cmd)) {
8747 synchronized (this) {
8748 dumpOomLocked(fd, pw, args, opti, true);
8749 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08008750 } else if ("provider".equals(cmd)) {
8751 String[] newArgs;
8752 String name;
8753 if (opti >= args.length) {
8754 name = null;
8755 newArgs = EMPTY_STRING_ARRAY;
8756 } else {
8757 name = args[opti];
8758 opti++;
8759 newArgs = new String[args.length - opti];
8760 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
8761 }
8762 if (!dumpProvider(fd, pw, name, newArgs, 0, dumpAll)) {
8763 pw.println("No providers match: " + name);
8764 pw.println("Use -h for help.");
8765 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008766 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
8767 synchronized (this) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008768 dumpProvidersLocked(fd, pw, args, opti, true, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008769 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008770 } else if ("service".equals(cmd)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008771 String[] newArgs;
8772 String name;
8773 if (opti >= args.length) {
8774 name = null;
8775 newArgs = EMPTY_STRING_ARRAY;
8776 } else {
8777 name = args[opti];
8778 opti++;
8779 newArgs = new String[args.length - opti];
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008780 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8781 args.length - opti);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008782 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008783 if (!mServices.dumpService(fd, pw, name, newArgs, 0, dumpAll)) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008784 pw.println("No services match: " + name);
8785 pw.println("Use -h for help.");
8786 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008787 } else if ("package".equals(cmd)) {
8788 String[] newArgs;
8789 if (opti >= args.length) {
8790 pw.println("package: no package name specified");
8791 pw.println("Use -h for help.");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008792 } else {
8793 dumpPackage = args[opti];
8794 opti++;
8795 newArgs = new String[args.length - opti];
8796 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0,
8797 args.length - opti);
8798 args = newArgs;
8799 opti = 0;
Amith Yamasani7463ada2012-04-11 15:02:39 -07008800 more = true;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008801 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008802 } else if ("services".equals(cmd) || "s".equals(cmd)) {
8803 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008804 mServices.dumpServicesLocked(fd, pw, args, opti, true, dumpClient, null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008805 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07008806 } else {
8807 // Dumping a single activity?
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008808 if (!dumpActivity(fd, pw, cmd, args, opti, dumpAll)) {
8809 pw.println("Bad activity command, or no activities match: " + cmd);
8810 pw.println("Use -h for help.");
Dianne Hackborn625ac272010-09-17 18:29:22 -07008811 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008812 }
8813 if (!more) {
8814 Binder.restoreCallingIdentity(origId);
Dianne Hackborn30d71892010-12-11 10:37:55 -08008815 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008816 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008817 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008818
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008819 // No piece of data specified, dump everything.
8820 synchronized (this) {
8821 boolean needSep;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008822 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008823 if (needSep) {
8824 pw.println(" ");
8825 }
8826 if (dumpAll) {
8827 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008828 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008829 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008830 if (needSep) {
8831 pw.println(" ");
8832 }
8833 if (dumpAll) {
8834 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008835 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008836 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008837 if (needSep) {
8838 pw.println(" ");
8839 }
8840 if (dumpAll) {
8841 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008842 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -07008843 needSep = mServices.dumpServicesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008844 if (needSep) {
8845 pw.println(" ");
8846 }
8847 if (dumpAll) {
8848 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008849 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008850 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, dumpClient, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008851 if (needSep) {
8852 pw.println(" ");
8853 }
8854 if (dumpAll) {
8855 pw.println("-------------------------------------------------------------------------------");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008856 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008857 dumpProcessesLocked(fd, pw, args, opti, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008858 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008859 Binder.restoreCallingIdentity(origId);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008860 }
Amith Yamasani7463ada2012-04-11 15:02:39 -07008861
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008862 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008863 int opti, boolean dumpAll, boolean dumpClient, String dumpPackage) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008864 pw.println("ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)");
8865 pw.println(" Main stack:");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008866 dumpHistoryList(fd, pw, mMainStack.mHistory, " ", "Hist", true, !dumpAll, dumpClient,
8867 dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008868 pw.println(" ");
8869 pw.println(" Running activities (most recent first):");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008870 dumpHistoryList(fd, pw, mMainStack.mLRUActivities, " ", "Run", false, !dumpAll, false,
8871 dumpPackage);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008872 if (mMainStack.mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008873 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008874 pw.println(" Activities waiting for another to become visible:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008875 dumpHistoryList(fd, pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008876 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008877 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008878 if (mMainStack.mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008879 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008880 pw.println(" Activities waiting to stop:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008881 dumpHistoryList(fd, pw, mMainStack.mStoppingActivities, " ", "Stop", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008882 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008883 }
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008884 if (mMainStack.mGoingToSleepActivities.size() > 0) {
8885 pw.println(" ");
8886 pw.println(" Activities waiting to sleep:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008887 dumpHistoryList(fd, pw, mMainStack.mGoingToSleepActivities, " ", "Sleep", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008888 !dumpAll, false, dumpPackage);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08008889 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008890 if (mMainStack.mFinishingActivities.size() > 0) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008891 pw.println(" ");
8892 pw.println(" Activities waiting to finish:");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008893 dumpHistoryList(fd, pw, mMainStack.mFinishingActivities, " ", "Fin", false,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008894 !dumpAll, false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008897 pw.println(" ");
Dianne Hackborn621e2fe2012-02-16 17:07:33 -08008898 if (mMainStack.mPausingActivity != null) {
8899 pw.println(" mPausingActivity: " + mMainStack.mPausingActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008900 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008901 pw.println(" mResumedActivity: " + mMainStack.mResumedActivity);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008902 pw.println(" mFocusedActivity: " + mFocusedActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008903 if (dumpAll) {
8904 pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity);
8905 pw.println(" mSleepTimeout: " + mMainStack.mSleepTimeout);
Dianne Hackborn90c52de2011-09-23 12:57:44 -07008906 pw.println(" mDismissKeyguardOnNextActivity: "
8907 + mMainStack.mDismissKeyguardOnNextActivity);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008910 if (mRecentTasks.size() > 0) {
8911 pw.println();
8912 pw.println(" Recent tasks:");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008913
8914 final int N = mRecentTasks.size();
8915 for (int i=0; i<N; i++) {
8916 TaskRecord tr = mRecentTasks.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008917 if (dumpPackage != null) {
8918 if (tr.realActivity == null ||
8919 !dumpPackage.equals(tr.realActivity)) {
8920 continue;
8921 }
8922 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008923 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
8924 pw.println(tr);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008925 if (dumpAll) {
8926 mRecentTasks.get(i).dump(pw, " ");
8927 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008928 }
8929 }
8930
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008931 if (dumpAll) {
8932 pw.println(" ");
8933 pw.println(" mCurTask: " + mCurTask);
8934 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008935
8936 return true;
8937 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07008938
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008939 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008940 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008941 boolean needSep = false;
8942 int numPers = 0;
8943
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008944 pw.println("ACTIVITY MANAGER RUNNING PROCESSES (dumpsys activity processes)");
8945
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008946 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008947 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
8948 final int NA = procs.size();
8949 for (int ia=0; ia<NA; ia++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008950 ProcessRecord r = procs.valueAt(ia);
8951 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8952 continue;
8953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 if (!needSep) {
8955 pw.println(" All known processes:");
8956 needSep = true;
8957 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008958 pw.print(r.persistent ? " *PERS*" : " *APP*");
8959 pw.print(" UID "); pw.print(procs.keyAt(ia));
8960 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 r.dump(pw, " ");
8962 if (r.persistent) {
8963 numPers++;
8964 }
8965 }
8966 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008967 }
Dianne Hackborna0c283e2012-02-09 10:47:01 -08008968
8969 if (mIsolatedProcesses.size() > 0) {
8970 if (needSep) pw.println(" ");
8971 needSep = true;
8972 pw.println(" Isolated process list (sorted by uid):");
8973 for (int i=0; i<mIsolatedProcesses.size(); i++) {
8974 ProcessRecord r = mIsolatedProcesses.valueAt(i);
8975 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8976 continue;
8977 }
8978 pw.println(String.format("%sIsolated #%2d: %s",
8979 " ", i, r.toString()));
8980 }
8981 }
8982
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008983 if (mLruProcesses.size() > 0) {
8984 if (needSep) pw.println(" ");
8985 needSep = true;
Dianne Hackborn905577f2011-09-07 18:31:28 -07008986 pw.println(" Process LRU list (sorted by oom_adj):");
Dianne Hackborn287952c2010-09-22 22:34:31 -07008987 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008988 "Proc", "PERS", false, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008989 needSep = true;
8990 }
8991
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008992 if (dumpAll) {
8993 synchronized (mPidsSelfLocked) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08008994 boolean printed = false;
8995 for (int i=0; i<mPidsSelfLocked.size(); i++) {
8996 ProcessRecord r = mPidsSelfLocked.valueAt(i);
8997 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
8998 continue;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07008999 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009000 if (!printed) {
9001 if (needSep) pw.println(" ");
9002 needSep = true;
9003 pw.println(" PID mappings:");
9004 printed = true;
9005 }
9006 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
9007 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 }
9009 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009010 }
9011
9012 if (mForegroundProcesses.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009013 synchronized (mPidsSelfLocked) {
9014 boolean printed = false;
9015 for (int i=0; i<mForegroundProcesses.size(); i++) {
9016 ProcessRecord r = mPidsSelfLocked.get(
9017 mForegroundProcesses.valueAt(i).pid);
9018 if (dumpPackage != null && (r == null
9019 || !dumpPackage.equals(r.info.packageName))) {
9020 continue;
9021 }
9022 if (!printed) {
9023 if (needSep) pw.println(" ");
9024 needSep = true;
9025 pw.println(" Foreground Processes:");
9026 printed = true;
9027 }
9028 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
9029 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
9030 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009032 }
9033
9034 if (mPersistentStartingProcesses.size() > 0) {
9035 if (needSep) pw.println(" ");
9036 needSep = true;
9037 pw.println(" Persisent processes that are starting:");
9038 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009039 "Starting Norm", "Restarting PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009041
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009042 if (mRemovedProcesses.size() > 0) {
9043 if (needSep) pw.println(" ");
9044 needSep = true;
9045 pw.println(" Processes that are being removed:");
9046 dumpProcessList(pw, this, mRemovedProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009047 "Removed Norm", "Removed PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009048 }
9049
9050 if (mProcessesOnHold.size() > 0) {
9051 if (needSep) pw.println(" ");
9052 needSep = true;
9053 pw.println(" Processes that are on old until the system is ready:");
9054 dumpProcessList(pw, this, mProcessesOnHold, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009055 "OnHold Norm", "OnHold PERS", dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009057
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009058 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, dumpPackage);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009059
9060 if (mProcessCrashTimes.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009061 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009062 long now = SystemClock.uptimeMillis();
9063 for (Map.Entry<String, SparseArray<Long>> procs
9064 : mProcessCrashTimes.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009065 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009066 SparseArray<Long> uids = procs.getValue();
9067 final int N = uids.size();
9068 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009069 int puid = uids.keyAt(i);
9070 ProcessRecord r = mProcessNames.get(pname, puid);
9071 if (dumpPackage != null && (r == null
9072 || !dumpPackage.equals(r.info.packageName))) {
9073 continue;
9074 }
9075 if (!printed) {
9076 if (needSep) pw.println(" ");
9077 needSep = true;
9078 pw.println(" Time since processes crashed:");
9079 printed = true;
9080 }
9081 pw.print(" Process "); pw.print(pname);
9082 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009083 pw.print(": last crashed ");
Dianne Hackborn27ff9132012-03-06 14:57:58 -08009084 TimeUtils.formatDuration(now-uids.valueAt(i), pw);
9085 pw.println(" ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009086 }
9087 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009089
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009090 if (mBadProcesses.getMap().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009091 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009092 for (Map.Entry<String, SparseArray<Long>> procs
9093 : mBadProcesses.getMap().entrySet()) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009094 String pname = procs.getKey();
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009095 SparseArray<Long> uids = procs.getValue();
9096 final int N = uids.size();
9097 for (int i=0; i<N; i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009098 int puid = uids.keyAt(i);
9099 ProcessRecord r = mProcessNames.get(pname, puid);
9100 if (dumpPackage != null && (r == null
9101 || !dumpPackage.equals(r.info.packageName))) {
9102 continue;
9103 }
9104 if (!printed) {
9105 if (needSep) pw.println(" ");
9106 needSep = true;
9107 pw.println(" Bad processes:");
9108 }
9109 pw.print(" Bad process "); pw.print(pname);
9110 pw.print(" uid "); pw.print(puid);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009111 pw.print(": crashed at time ");
9112 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 }
9114 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009117 pw.println();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07009118 pw.println(" mStartedUsers:");
9119 for (int i=0; i<mStartedUsers.size(); i++) {
9120 UserStartedState uss = mStartedUsers.valueAt(i);
9121 pw.print(" User #"); pw.print(uss.mHandle.getIdentifier());
9122 pw.println(":");
9123 uss.dump(" ", pw);
9124 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009125 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009126 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn50685602011-12-01 12:23:37 -08009127 if (dumpAll) {
9128 StringBuilder sb = new StringBuilder(128);
9129 sb.append(" mPreviousProcessVisibleTime: ");
9130 TimeUtils.formatDuration(mPreviousProcessVisibleTime, sb);
9131 pw.println(sb);
9132 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07009133 if (mHeavyWeightProcess != null) {
9134 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9135 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009136 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009137 if (dumpAll) {
9138 pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange);
Dianne Hackborn3d0724d2011-05-12 15:39:41 -07009139 if (mCompatModePackages.getPackages().size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009140 boolean printed = false;
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009141 for (Map.Entry<String, Integer> entry
9142 : mCompatModePackages.getPackages().entrySet()) {
9143 String pkg = entry.getKey();
9144 int mode = entry.getValue();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009145 if (dumpPackage != null && !dumpPackage.equals(pkg)) {
9146 continue;
9147 }
9148 if (!printed) {
9149 pw.println(" mScreenCompatPackages:");
9150 printed = true;
9151 }
Dianne Hackborn36cd41f2011-05-25 21:00:46 -07009152 pw.print(" "); pw.print(pkg); pw.print(": ");
9153 pw.print(mode); pw.println();
9154 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -07009155 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009156 }
Dianne Hackbornff5b1582012-04-12 17:24:07 -07009157 if (mSleeping || mWentToSleep || mLockScreenShown) {
9158 pw.println(" mSleeping=" + mSleeping + " mWentToSleep=" + mWentToSleep
9159 + " mLockScreenShown " + mLockScreenShown);
9160 }
9161 if (mShuttingDown) {
9162 pw.println(" mShuttingDown=" + mShuttingDown);
9163 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009164 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
9165 || mOrigWaitForDebugger) {
9166 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
9167 + " mDebugTransient=" + mDebugTransient
9168 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
9169 }
Siva Velusamy92a8b222012-03-09 16:24:04 -08009170 if (mOpenGlTraceApp != null) {
9171 pw.println(" mOpenGlTraceApp=" + mOpenGlTraceApp);
9172 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07009173 if (mProfileApp != null || mProfileProc != null || mProfileFile != null
9174 || mProfileFd != null) {
9175 pw.println(" mProfileApp=" + mProfileApp + " mProfileProc=" + mProfileProc);
9176 pw.println(" mProfileFile=" + mProfileFile + " mProfileFd=" + mProfileFd);
9177 pw.println(" mProfileType=" + mProfileType + " mAutoStopProfiler="
9178 + mAutoStopProfiler);
9179 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009180 if (mAlwaysFinishActivities || mController != null) {
9181 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
9182 + " mController=" + mController);
9183 }
9184 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009186 pw.println(" mStartRunning=" + mStartRunning
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07009187 + " mProcessesReady=" + mProcessesReady
9188 + " mSystemReady=" + mSystemReady);
9189 pw.println(" mBooting=" + mBooting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009190 + " mBooted=" + mBooted
9191 + " mFactoryTest=" + mFactoryTest);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009192 pw.print(" mLastPowerCheckRealtime=");
9193 TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
9194 pw.println("");
9195 pw.print(" mLastPowerCheckUptime=");
9196 TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
9197 pw.println("");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009198 pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep);
9199 pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity);
Dianne Hackborn906497c2010-05-10 15:57:38 -07009200 pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
Dianne Hackbornee7621c2012-08-13 16:42:18 -07009201 pw.println(" mNumNonHiddenProcs=" + mNumNonHiddenProcs
9202 + " mNumHiddenProcs=" + mNumHiddenProcs
9203 + " mNumServiceProcs=" + mNumServiceProcs
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009204 + " mNewNumServiceProcs=" + mNewNumServiceProcs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009205 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009206
9207 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 }
9209
Dianne Hackborn287952c2010-09-22 22:34:31 -07009210 boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009211 int opti, boolean needSep, boolean dumpAll, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009212 if (mProcessesToGc.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009213 boolean printed = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009214 long now = SystemClock.uptimeMillis();
9215 for (int i=0; i<mProcessesToGc.size(); i++) {
9216 ProcessRecord proc = mProcessesToGc.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009217 if (dumpPackage != null && !dumpPackage.equals(proc.info.packageName)) {
9218 continue;
9219 }
9220 if (!printed) {
9221 if (needSep) pw.println(" ");
9222 needSep = true;
9223 pw.println(" Processes that are waiting to GC:");
9224 printed = true;
9225 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009226 pw.print(" Process "); pw.println(proc);
9227 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
9228 pw.print(", last gced=");
9229 pw.print(now-proc.lastRequestedGc);
9230 pw.print(" ms ago, last lowMem=");
9231 pw.print(now-proc.lastLowMemory);
9232 pw.println(" ms ago");
9233
9234 }
9235 }
9236 return needSep;
9237 }
9238
9239 boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9240 int opti, boolean dumpAll) {
9241 boolean needSep = false;
9242
9243 if (mLruProcesses.size() > 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009244 if (needSep) pw.println(" ");
9245 needSep = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009246 pw.println(" OOM levels:");
Dianne Hackborn7d608422011-08-07 16:24:18 -07009247 pw.print(" SYSTEM_ADJ: "); pw.println(ProcessList.SYSTEM_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009248 pw.print(" PERSISTENT_PROC_ADJ: "); pw.println(ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009249 pw.print(" FOREGROUND_APP_ADJ: "); pw.println(ProcessList.FOREGROUND_APP_ADJ);
9250 pw.print(" VISIBLE_APP_ADJ: "); pw.println(ProcessList.VISIBLE_APP_ADJ);
9251 pw.print(" PERCEPTIBLE_APP_ADJ: "); pw.println(ProcessList.PERCEPTIBLE_APP_ADJ);
9252 pw.print(" HEAVY_WEIGHT_APP_ADJ: "); pw.println(ProcessList.HEAVY_WEIGHT_APP_ADJ);
9253 pw.print(" BACKUP_APP_ADJ: "); pw.println(ProcessList.BACKUP_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009254 pw.print(" SERVICE_ADJ: "); pw.println(ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009255 pw.print(" HOME_APP_ADJ: "); pw.println(ProcessList.HOME_APP_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009256 pw.print(" PREVIOUS_APP_ADJ: "); pw.println(ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009257 pw.print(" SERVICE_B_ADJ: "); pw.println(ProcessList.SERVICE_B_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009258 pw.print(" HIDDEN_APP_MIN_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009259 pw.print(" HIDDEN_APP_MAX_ADJ: "); pw.println(ProcessList.HIDDEN_APP_MAX_ADJ);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009260
9261 if (needSep) pw.println(" ");
9262 needSep = true;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009263 pw.println(" Process OOM control:");
Dianne Hackborn905577f2011-09-07 18:31:28 -07009264 dumpProcessOomList(pw, this, mLruProcesses, " ",
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009265 "Proc", "PERS", true, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009266 needSep = true;
9267 }
9268
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009269 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll, null);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009270
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009271 pw.println();
Dianne Hackborn287952c2010-09-22 22:34:31 -07009272 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009273 pw.println(" mPreviousProcess: " + mPreviousProcess);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009274 if (mHeavyWeightProcess != null) {
9275 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
9276 }
9277
9278 return true;
9279 }
9280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009281 /**
9282 * There are three ways to call this:
Marco Nelissen18cb2872011-11-15 11:19:53 -08009283 * - no provider specified: dump all the providers
9284 * - a flattened component name that matched an existing provider was specified as the
9285 * first arg: dump that one provider
9286 * - the first arg isn't the flattened component name of an existing provider:
9287 * dump all providers whose component contains the first arg as a substring
9288 */
9289 protected boolean dumpProvider(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9290 int opti, boolean dumpAll) {
Marco Nelissende7408c2012-02-08 14:57:38 -08009291 return mProviderMap.dumpProvider(fd, pw, name, args, opti, dumpAll);
Marco Nelissen18cb2872011-11-15 11:19:53 -08009292 }
9293
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009294 static class ItemMatcher {
9295 ArrayList<ComponentName> components;
9296 ArrayList<String> strings;
9297 ArrayList<Integer> objects;
9298 boolean all;
9299
9300 ItemMatcher() {
9301 all = true;
9302 }
9303
9304 void build(String name) {
9305 ComponentName componentName = ComponentName.unflattenFromString(name);
9306 if (componentName != null) {
9307 if (components == null) {
9308 components = new ArrayList<ComponentName>();
9309 }
9310 components.add(componentName);
9311 all = false;
9312 } else {
9313 int objectId = 0;
9314 // Not a '/' separated full component name; maybe an object ID?
9315 try {
9316 objectId = Integer.parseInt(name, 16);
9317 if (objects == null) {
9318 objects = new ArrayList<Integer>();
9319 }
9320 objects.add(objectId);
9321 all = false;
9322 } catch (RuntimeException e) {
9323 // Not an integer; just do string match.
9324 if (strings == null) {
9325 strings = new ArrayList<String>();
9326 }
9327 strings.add(name);
9328 all = false;
9329 }
9330 }
9331 }
9332
9333 int build(String[] args, int opti) {
9334 for (; opti<args.length; opti++) {
9335 String name = args[opti];
9336 if ("--".equals(name)) {
9337 return opti+1;
9338 }
9339 build(name);
9340 }
9341 return opti;
9342 }
9343
9344 boolean match(Object object, ComponentName comp) {
9345 if (all) {
9346 return true;
9347 }
9348 if (components != null) {
9349 for (int i=0; i<components.size(); i++) {
9350 if (components.get(i).equals(comp)) {
9351 return true;
9352 }
9353 }
9354 }
9355 if (objects != null) {
9356 for (int i=0; i<objects.size(); i++) {
9357 if (System.identityHashCode(object) == objects.get(i)) {
9358 return true;
9359 }
9360 }
9361 }
9362 if (strings != null) {
9363 String flat = comp.flattenToString();
9364 for (int i=0; i<strings.size(); i++) {
9365 if (flat.contains(strings.get(i))) {
9366 return true;
9367 }
9368 }
9369 }
9370 return false;
9371 }
9372 }
9373
Dianne Hackborn625ac272010-09-17 18:29:22 -07009374 /**
9375 * There are three things that cmd can be:
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009376 * - a flattened component name that matches an existing activity
Dianne Hackborn625ac272010-09-17 18:29:22 -07009377 * - the cmd arg isn't the flattened component name of an existing activity:
9378 * dump all activity whose component contains the cmd as a substring
9379 * - A hex number of the ActivityRecord object instance.
9380 */
9381 protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
9382 int opti, boolean dumpAll) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009383 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009384
9385 if ("all".equals(name)) {
9386 synchronized (this) {
9387 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07009388 activities.add(r1);
9389 }
9390 }
Dianne Hackbornf9302322011-06-14 18:36:14 -07009391 } else if ("top".equals(name)) {
9392 synchronized (this) {
9393 final int N = mMainStack.mHistory.size();
9394 if (N > 0) {
9395 activities.add((ActivityRecord)mMainStack.mHistory.get(N-1));
9396 }
9397 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009398 } else {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009399 ItemMatcher matcher = new ItemMatcher();
9400 matcher.build(name);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009401
9402 synchronized (this) {
9403 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009404 if (matcher.match(r1, r1.intent.getComponent())) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009405 activities.add(r1);
9406 }
9407 }
9408 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009409 }
9410
9411 if (activities.size() <= 0) {
9412 return false;
9413 }
9414
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009415 String[] newArgs = new String[args.length - opti];
9416 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
9417
Dianne Hackborn30d71892010-12-11 10:37:55 -08009418 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009419 boolean needSep = false;
Dianne Hackborn30d71892010-12-11 10:37:55 -08009420 for (int i=activities.size()-1; i>=0; i--) {
9421 ActivityRecord r = (ActivityRecord)activities.get(i);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009422 if (needSep) {
9423 pw.println();
9424 }
9425 needSep = true;
9426 synchronized (this) {
9427 if (lastTask != r.task) {
9428 lastTask = r.task;
9429 pw.print("TASK "); pw.print(lastTask.affinity);
9430 pw.print(" id="); pw.println(lastTask.taskId);
9431 if (dumpAll) {
9432 lastTask.dump(pw, " ");
9433 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08009434 }
9435 }
9436 dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009437 }
9438 return true;
9439 }
9440
9441 /**
9442 * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
9443 * there is a thread associated with the activity.
9444 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08009445 private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009446 final ActivityRecord r, String[] args, boolean dumpAll) {
9447 String innerPrefix = prefix + " ";
Dianne Hackborn30d71892010-12-11 10:37:55 -08009448 synchronized (this) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009449 pw.print(prefix); pw.print("ACTIVITY "); pw.print(r.shortComponentName);
9450 pw.print(" "); pw.print(Integer.toHexString(System.identityHashCode(r)));
9451 pw.print(" pid=");
Dianne Hackborn30d71892010-12-11 10:37:55 -08009452 if (r.app != null) pw.println(r.app.pid);
9453 else pw.println("(not running)");
9454 if (dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009455 r.dump(pw, innerPrefix);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009456 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07009457 }
9458 if (r.app != null && r.app.thread != null) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009459 // flush anything that is already in the PrintWriter since the thread is going
9460 // to write to the file descriptor directly
9461 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07009462 try {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009463 TransferPipe tp = new TransferPipe();
9464 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009465 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9466 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009467 tp.go(fd);
9468 } finally {
9469 tp.kill();
9470 }
9471 } catch (IOException e) {
9472 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
Dianne Hackborn625ac272010-09-17 18:29:22 -07009473 } catch (RemoteException e) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009474 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
Dianne Hackborn625ac272010-09-17 18:29:22 -07009475 }
9476 }
9477 }
9478
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009479 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009480 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009481 boolean needSep = false;
Dianne Hackborn786b4402012-08-27 15:14:02 -07009482 boolean onlyHistory = false;
9483
9484 if ("history".equals(dumpPackage)) {
9485 onlyHistory = true;
9486 dumpPackage = null;
9487 }
9488
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009489 pw.println("ACTIVITY MANAGER BROADCAST STATE (dumpsys activity broadcasts)");
Dianne Hackborn786b4402012-08-27 15:14:02 -07009490 if (!onlyHistory && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 if (mRegisteredReceivers.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009492 boolean printed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009493 Iterator it = mRegisteredReceivers.values().iterator();
9494 while (it.hasNext()) {
9495 ReceiverList r = (ReceiverList)it.next();
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009496 if (dumpPackage != null && (r.app == null ||
9497 !dumpPackage.equals(r.app.info.packageName))) {
9498 continue;
9499 }
9500 if (!printed) {
9501 pw.println(" Registered Receivers:");
9502 needSep = true;
9503 printed = true;
9504 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009505 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009506 r.dump(pw, " ");
9507 }
9508 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009509
9510 if (mReceiverResolver.dump(pw, needSep ?
9511 "\n Receiver Resolver Table:" : " Receiver Resolver Table:",
9512 " ", dumpPackage, false)) {
9513 needSep = true;
9514 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009515 }
Christopher Tatef46723b2012-01-26 14:19:24 -08009516
9517 for (BroadcastQueue q : mBroadcastQueues) {
9518 needSep = q.dumpLocked(fd, pw, args, opti, dumpAll, dumpPackage, needSep);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009520
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009521 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009522
Dianne Hackborn786b4402012-08-27 15:14:02 -07009523 if (!onlyHistory && mStickyBroadcasts != null && dumpPackage == null) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009524 if (needSep) {
9525 pw.println();
9526 }
9527 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009528 pw.println(" Sticky broadcasts:");
9529 StringBuilder sb = new StringBuilder(128);
9530 for (Map.Entry<String, ArrayList<Intent>> ent
9531 : mStickyBroadcasts.entrySet()) {
9532 pw.print(" * Sticky action "); pw.print(ent.getKey());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009533 if (dumpAll) {
9534 pw.println(":");
9535 ArrayList<Intent> intents = ent.getValue();
9536 final int N = intents.size();
9537 for (int i=0; i<N; i++) {
9538 sb.setLength(0);
9539 sb.append(" Intent: ");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08009540 intents.get(i).toShortString(sb, false, true, false, false);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009541 pw.println(sb.toString());
9542 Bundle bundle = intents.get(i).getExtras();
9543 if (bundle != null) {
9544 pw.print(" ");
9545 pw.println(bundle.toString());
9546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009547 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009548 } else {
9549 pw.println("");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009550 }
9551 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009552 needSep = true;
9553 }
9554
Dianne Hackborn786b4402012-08-27 15:14:02 -07009555 if (!onlyHistory && dumpAll) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009556 pw.println();
Christopher Tatef46723b2012-01-26 14:19:24 -08009557 for (BroadcastQueue queue : mBroadcastQueues) {
9558 pw.println(" mBroadcastsScheduled [" + queue.mQueueName + "]="
9559 + queue.mBroadcastsScheduled);
9560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009561 pw.println(" mHandler:");
9562 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009563 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009565
9566 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009567 }
9568
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009569 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009570 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackborn9da2d402012-03-15 13:43:08 -07009571 boolean needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009572
Dianne Hackborn1c9b2602011-08-19 14:08:43 -07009573 ItemMatcher matcher = new ItemMatcher();
9574 matcher.build(args, opti);
9575
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009576 pw.println("ACTIVITY MANAGER CONTENT PROVIDERS (dumpsys activity providers)");
Amith Yamasani742a6712011-05-04 14:49:28 -07009577
9578 mProviderMap.dumpProvidersLocked(pw, dumpAll);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009579
9580 if (mLaunchingProviders.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009581 boolean printed = false;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009582 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009583 ContentProviderRecord r = mLaunchingProviders.get(i);
9584 if (dumpPackage != null && !dumpPackage.equals(r.name.getPackageName())) {
9585 continue;
9586 }
9587 if (!printed) {
9588 if (needSep) pw.println(" ");
9589 needSep = true;
9590 pw.println(" Launching content providers:");
9591 printed = true;
9592 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009593 pw.print(" Launching #"); pw.print(i); pw.print(": ");
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009594 pw.println(r);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009595 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009596 }
9597
9598 if (mGrantedUriPermissions.size() > 0) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009599 if (needSep) pw.println();
9600 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009601 pw.println("Granted Uri Permissions:");
9602 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
9603 int uid = mGrantedUriPermissions.keyAt(i);
9604 HashMap<Uri, UriPermission> perms
9605 = mGrantedUriPermissions.valueAt(i);
9606 pw.print(" * UID "); pw.print(uid);
9607 pw.println(" holds:");
9608 for (UriPermission perm : perms.values()) {
9609 pw.print(" "); pw.println(perm);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009610 if (dumpAll) {
9611 perm.dump(pw, " ");
9612 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009613 }
9614 }
9615 needSep = true;
9616 }
9617
9618 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009619 }
9620
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009621 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009622 int opti, boolean dumpAll, String dumpPackage) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009623 boolean needSep = false;
9624
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009625 if (mIntentSenderRecords.size() > 0) {
9626 boolean printed = false;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009627 Iterator<WeakReference<PendingIntentRecord>> it
9628 = mIntentSenderRecords.values().iterator();
9629 while (it.hasNext()) {
9630 WeakReference<PendingIntentRecord> ref = it.next();
9631 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009632 if (dumpPackage != null && (rec == null
9633 || !dumpPackage.equals(rec.key.packageName))) {
9634 continue;
9635 }
9636 if (!printed) {
9637 pw.println("ACTIVITY MANAGER PENDING INTENTS (dumpsys activity intents)");
9638 printed = true;
9639 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009640 needSep = true;
9641 if (rec != null) {
9642 pw.print(" * "); pw.println(rec);
9643 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009644 rec.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009646 } else {
9647 pw.print(" * "); pw.println(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 }
9649 }
9650 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009651
9652 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009653 }
9654
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009655 private static final void dumpHistoryList(FileDescriptor fd, PrintWriter pw, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009656 String prefix, String label, boolean complete, boolean brief, boolean client,
9657 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009658 TaskRecord lastTask = null;
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009659 boolean needNL = false;
9660 final String innerPrefix = prefix + " ";
9661 final String[] args = new String[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009662 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009663 final ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009664 if (dumpPackage != null && !dumpPackage.equals(r.packageName)) {
9665 continue;
9666 }
Dianne Hackbornf26fd992011-04-08 18:14:09 -07009667 final boolean full = !brief && (complete || !r.isInHistory());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009668 if (needNL) {
9669 pw.println(" ");
9670 needNL = false;
9671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672 if (lastTask != r.task) {
9673 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009674 pw.print(prefix);
9675 pw.print(full ? "* " : " ");
9676 pw.println(lastTask);
9677 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009678 lastTask.dump(pw, prefix + " ");
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009679 } else if (complete) {
9680 // Complete + brief == give a summary. Isn't that obvious?!?
9681 if (lastTask.intent != null) {
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009682 pw.print(prefix); pw.print(" ");
Dianne Hackborn21c241e2012-03-08 13:57:23 -08009683 pw.println(lastTask.intent.toInsecureStringWithClip());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009684 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009687 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
9688 pw.print(" #"); pw.print(i); pw.print(": ");
9689 pw.println(r);
9690 if (full) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009691 r.dump(pw, innerPrefix);
9692 } else if (complete) {
9693 // Complete + brief == give a summary. Isn't that obvious?!?
Dianne Hackborn90c52de2011-09-23 12:57:44 -07009694 pw.print(innerPrefix); pw.println(r.intent.toInsecureString());
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009695 if (r.app != null) {
9696 pw.print(innerPrefix); pw.println(r.app);
9697 }
9698 }
9699 if (client && r.app != null && r.app.thread != null) {
9700 // flush anything that is already in the PrintWriter since the thread is going
9701 // to write to the file descriptor directly
9702 pw.flush();
9703 try {
9704 TransferPipe tp = new TransferPipe();
9705 try {
Dianne Hackbornbe707852011-11-11 14:32:10 -08009706 r.app.thread.dumpActivity(tp.getWriteFd().getFileDescriptor(),
9707 r.appToken, innerPrefix, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009708 // Short timeout, since blocking here can
9709 // deadlock with the application.
9710 tp.go(fd, 2000);
9711 } finally {
9712 tp.kill();
9713 }
9714 } catch (IOException e) {
9715 pw.println(innerPrefix + "Failure while dumping the activity: " + e);
9716 } catch (RemoteException e) {
9717 pw.println(innerPrefix + "Got a RemoteException while dumping the activity");
9718 }
9719 needNL = true;
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07009720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 }
9722 }
9723
Dianne Hackborn09c916b2009-12-08 14:50:51 -08009724 private static String buildOomTag(String prefix, String space, int val, int base) {
9725 if (val == base) {
9726 if (space == null) return prefix;
9727 return prefix + " ";
9728 }
9729 return prefix + "+" + Integer.toString(val-base);
9730 }
9731
9732 private static final int dumpProcessList(PrintWriter pw,
9733 ActivityManagerService service, List list,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009734 String prefix, String normalLabel, String persistentLabel,
9735 String dumpPackage) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009736 int numPers = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07009737 final int N = list.size()-1;
9738 for (int i=N; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009739 ProcessRecord r = (ProcessRecord)list.get(i);
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009740 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9741 continue;
9742 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009743 pw.println(String.format("%s%s #%2d: %s",
9744 prefix, (r.persistent ? persistentLabel : normalLabel),
9745 i, r.toString()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009746 if (r.persistent) {
9747 numPers++;
9748 }
9749 }
9750 return numPers;
9751 }
9752
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009753 private static final boolean dumpProcessOomList(PrintWriter pw,
Dianne Hackborn905577f2011-09-07 18:31:28 -07009754 ActivityManagerService service, List<ProcessRecord> origList,
Dianne Hackborn287952c2010-09-22 22:34:31 -07009755 String prefix, String normalLabel, String persistentLabel,
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009756 boolean inclDetails, String dumpPackage) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009757
Dianne Hackborn905577f2011-09-07 18:31:28 -07009758 ArrayList<Pair<ProcessRecord, Integer>> list
9759 = new ArrayList<Pair<ProcessRecord, Integer>>(origList.size());
9760 for (int i=0; i<origList.size(); i++) {
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009761 ProcessRecord r = origList.get(i);
9762 if (dumpPackage != null && !dumpPackage.equals(r.info.packageName)) {
9763 continue;
9764 }
Dianne Hackborn905577f2011-09-07 18:31:28 -07009765 list.add(new Pair<ProcessRecord, Integer>(origList.get(i), i));
9766 }
9767
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009768 if (list.size() <= 0) {
9769 return false;
9770 }
9771
Dianne Hackborn905577f2011-09-07 18:31:28 -07009772 Comparator<Pair<ProcessRecord, Integer>> comparator
9773 = new Comparator<Pair<ProcessRecord, Integer>>() {
9774 @Override
9775 public int compare(Pair<ProcessRecord, Integer> object1,
9776 Pair<ProcessRecord, Integer> object2) {
9777 if (object1.first.setAdj != object2.first.setAdj) {
9778 return object1.first.setAdj > object2.first.setAdj ? -1 : 1;
9779 }
9780 if (object1.second.intValue() != object2.second.intValue()) {
9781 return object1.second.intValue() > object2.second.intValue() ? -1 : 1;
9782 }
9783 return 0;
9784 }
9785 };
9786
9787 Collections.sort(list, comparator);
9788
Dianne Hackborn287952c2010-09-22 22:34:31 -07009789 final long curRealtime = SystemClock.elapsedRealtime();
9790 final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
9791 final long curUptime = SystemClock.uptimeMillis();
9792 final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
9793
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009794 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborn905577f2011-09-07 18:31:28 -07009795 ProcessRecord r = list.get(i).first;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009796 String oomAdj;
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009797 if (r.setAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn7d608422011-08-07 16:24:18 -07009798 oomAdj = buildOomTag("bak", " ", r.setAdj, ProcessList.HIDDEN_APP_MIN_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009799 } else if (r.setAdj >= ProcessList.SERVICE_B_ADJ) {
9800 oomAdj = buildOomTag("svcb ", null, r.setAdj, ProcessList.SERVICE_B_ADJ);
Dianne Hackbornf35fe232011-11-01 19:25:20 -07009801 } else if (r.setAdj >= ProcessList.PREVIOUS_APP_ADJ) {
9802 oomAdj = buildOomTag("prev ", null, r.setAdj, ProcessList.PREVIOUS_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009803 } else if (r.setAdj >= ProcessList.HOME_APP_ADJ) {
9804 oomAdj = buildOomTag("home ", null, r.setAdj, ProcessList.HOME_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009805 } else if (r.setAdj >= ProcessList.SERVICE_ADJ) {
9806 oomAdj = buildOomTag("svc ", null, r.setAdj, ProcessList.SERVICE_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009807 } else if (r.setAdj >= ProcessList.BACKUP_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -08009808 oomAdj = buildOomTag("bkup ", null, r.setAdj, ProcessList.BACKUP_APP_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009809 } else if (r.setAdj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
9810 oomAdj = buildOomTag("hvy ", null, r.setAdj, ProcessList.HEAVY_WEIGHT_APP_ADJ);
9811 } else if (r.setAdj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
9812 oomAdj = buildOomTag("prcp ", null, r.setAdj, ProcessList.PERCEPTIBLE_APP_ADJ);
9813 } else if (r.setAdj >= ProcessList.VISIBLE_APP_ADJ) {
9814 oomAdj = buildOomTag("vis ", null, r.setAdj, ProcessList.VISIBLE_APP_ADJ);
9815 } else if (r.setAdj >= ProcessList.FOREGROUND_APP_ADJ) {
9816 oomAdj = buildOomTag("fore ", null, r.setAdj, ProcessList.FOREGROUND_APP_ADJ);
Dianne Hackborne02c88a2011-10-28 13:58:15 -07009817 } else if (r.setAdj >= ProcessList.PERSISTENT_PROC_ADJ) {
9818 oomAdj = buildOomTag("pers ", null, r.setAdj, ProcessList.PERSISTENT_PROC_ADJ);
Dianne Hackborn7d608422011-08-07 16:24:18 -07009819 } else if (r.setAdj >= ProcessList.SYSTEM_ADJ) {
9820 oomAdj = buildOomTag("sys ", null, r.setAdj, ProcessList.SYSTEM_ADJ);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009821 } else {
9822 oomAdj = Integer.toString(r.setAdj);
9823 }
9824 String schedGroup;
9825 switch (r.setSchedGroup) {
9826 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
9827 schedGroup = "B";
9828 break;
9829 case Process.THREAD_GROUP_DEFAULT:
9830 schedGroup = "F";
9831 break;
9832 default:
9833 schedGroup = Integer.toString(r.setSchedGroup);
9834 break;
9835 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -07009836 String foreground;
9837 if (r.foregroundActivities) {
9838 foreground = "A";
9839 } else if (r.foregroundServices) {
9840 foreground = "S";
9841 } else {
9842 foreground = " ";
9843 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07009844 pw.println(String.format("%s%s #%2d: adj=%s/%s%s trm=%2d %s (%s)",
Dianne Hackborn287952c2010-09-22 22:34:31 -07009845 prefix, (r.persistent ? persistentLabel : normalLabel),
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009846 (origList.size()-1)-list.get(i).second, oomAdj, schedGroup,
9847 foreground, r.trimMemoryLevel, r.toShortString(), r.adjType));
Dianne Hackborn287952c2010-09-22 22:34:31 -07009848 if (r.adjSource != null || r.adjTarget != null) {
9849 pw.print(prefix);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009850 pw.print(" ");
Dianne Hackborn287952c2010-09-22 22:34:31 -07009851 if (r.adjTarget instanceof ComponentName) {
9852 pw.print(((ComponentName)r.adjTarget).flattenToShortString());
9853 } else if (r.adjTarget != null) {
9854 pw.print(r.adjTarget.toString());
9855 } else {
9856 pw.print("{null}");
9857 }
9858 pw.print("<=");
9859 if (r.adjSource instanceof ProcessRecord) {
9860 pw.print("Proc{");
9861 pw.print(((ProcessRecord)r.adjSource).toShortString());
9862 pw.println("}");
9863 } else if (r.adjSource != null) {
9864 pw.println(r.adjSource.toString());
9865 } else {
9866 pw.println("{null}");
9867 }
9868 }
9869 if (inclDetails) {
9870 pw.print(prefix);
9871 pw.print(" ");
9872 pw.print("oom: max="); pw.print(r.maxAdj);
9873 pw.print(" hidden="); pw.print(r.hiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -07009874 pw.print(" empty="); pw.print(r.emptyAdj);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009875 pw.print(" curRaw="); pw.print(r.curRawAdj);
9876 pw.print(" setRaw="); pw.print(r.setRawAdj);
9877 pw.print(" cur="); pw.print(r.curAdj);
9878 pw.print(" set="); pw.println(r.setAdj);
9879 pw.print(prefix);
9880 pw.print(" ");
9881 pw.print("keeping="); pw.print(r.keeping);
9882 pw.print(" hidden="); pw.print(r.hidden);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07009883 pw.print(" empty="); pw.print(r.empty);
9884 pw.print(" hasAboveClient="); pw.println(r.hasAboveClient);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009885
9886 if (!r.keeping) {
9887 if (r.lastWakeTime != 0) {
9888 long wtime;
9889 BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
9890 synchronized (stats) {
9891 wtime = stats.getProcessWakeTime(r.info.uid,
9892 r.pid, curRealtime);
9893 }
9894 long timeUsed = wtime - r.lastWakeTime;
9895 pw.print(prefix);
9896 pw.print(" ");
9897 pw.print("keep awake over ");
9898 TimeUtils.formatDuration(realtimeSince, pw);
9899 pw.print(" used ");
9900 TimeUtils.formatDuration(timeUsed, pw);
9901 pw.print(" (");
9902 pw.print((timeUsed*100)/realtimeSince);
9903 pw.println("%)");
9904 }
9905 if (r.lastCpuTime != 0) {
9906 long timeUsed = r.curCpuTime - r.lastCpuTime;
9907 pw.print(prefix);
9908 pw.print(" ");
9909 pw.print("run cpu over ");
9910 TimeUtils.formatDuration(uptimeSince, pw);
9911 pw.print(" used ");
9912 TimeUtils.formatDuration(timeUsed, pw);
9913 pw.print(" (");
9914 pw.print((timeUsed*100)/uptimeSince);
9915 pw.println("%)");
9916 }
9917 }
9918 }
9919 }
Dianne Hackbornee9aef02011-11-16 13:21:46 -08009920 return true;
Dianne Hackborn287952c2010-09-22 22:34:31 -07009921 }
9922
Dianne Hackbornb437e092011-08-05 17:50:29 -07009923 ArrayList<ProcessRecord> collectProcesses(PrintWriter pw, int start, String[] args) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009924 ArrayList<ProcessRecord> procs;
9925 synchronized (this) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009926 if (args != null && args.length > start
9927 && args[start].charAt(0) != '-') {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009928 procs = new ArrayList<ProcessRecord>();
9929 int pid = -1;
9930 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009931 pid = Integer.parseInt(args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009932 } catch (NumberFormatException e) {
9933
9934 }
9935 for (int i=mLruProcesses.size()-1; i>=0; i--) {
9936 ProcessRecord proc = mLruProcesses.get(i);
9937 if (proc.pid == pid) {
9938 procs.add(proc);
Dianne Hackbornb437e092011-08-05 17:50:29 -07009939 } else if (proc.processName.equals(args[start])) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009940 procs.add(proc);
9941 }
9942 }
9943 if (procs.size() <= 0) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009944 pw.println("No process found for: " + args[start]);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009945 return null;
9946 }
9947 } else {
9948 procs = new ArrayList<ProcessRecord>(mLruProcesses);
9949 }
9950 }
9951 return procs;
9952 }
9953
9954 final void dumpGraphicsHardwareUsage(FileDescriptor fd,
9955 PrintWriter pw, String[] args) {
Dianne Hackbornb437e092011-08-05 17:50:29 -07009956 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009957 if (procs == null) {
9958 return;
9959 }
9960
9961 long uptime = SystemClock.uptimeMillis();
9962 long realtime = SystemClock.elapsedRealtime();
9963 pw.println("Applications Graphics Acceleration Info:");
9964 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
9965
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009966 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
9967 ProcessRecord r = procs.get(i);
Chet Haase9c1e23b2011-03-24 10:51:31 -07009968 if (r.thread != null) {
9969 pw.println("\n** Graphics info for pid " + r.pid + " [" + r.processName + "] **");
9970 pw.flush();
9971 try {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009972 TransferPipe tp = new TransferPipe();
9973 try {
9974 r.thread.dumpGfxInfo(tp.getWriteFd().getFileDescriptor(), args);
9975 tp.go(fd);
9976 } finally {
9977 tp.kill();
9978 }
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009979 } catch (IOException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009980 pw.println("Failure while dumping the app: " + r);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07009981 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07009982 } catch (RemoteException e) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07009983 pw.println("Got a RemoteException while dumping the app " + r);
Chet Haase9c1e23b2011-03-24 10:51:31 -07009984 pw.flush();
9985 }
9986 }
9987 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07009988 }
9989
Jeff Brown6754ba22011-12-14 20:20:01 -08009990 final void dumpDbInfo(FileDescriptor fd, PrintWriter pw, String[] args) {
9991 ArrayList<ProcessRecord> procs = collectProcesses(pw, 0, args);
9992 if (procs == null) {
9993 return;
9994 }
9995
9996 pw.println("Applications Database Info:");
9997
9998 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
9999 ProcessRecord r = procs.get(i);
10000 if (r.thread != null) {
10001 pw.println("\n** Database info for pid " + r.pid + " [" + r.processName + "] **");
10002 pw.flush();
10003 try {
10004 TransferPipe tp = new TransferPipe();
10005 try {
10006 r.thread.dumpDbInfo(tp.getWriteFd().getFileDescriptor(), args);
10007 tp.go(fd);
10008 } finally {
10009 tp.kill();
10010 }
10011 } catch (IOException e) {
10012 pw.println("Failure while dumping the app: " + r);
10013 pw.flush();
10014 } catch (RemoteException e) {
10015 pw.println("Got a RemoteException while dumping the app " + r);
10016 pw.flush();
10017 }
10018 }
10019 }
10020 }
10021
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010022 final static class MemItem {
10023 final String label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010024 final String shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010025 final long pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010026 final int id;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010027 ArrayList<MemItem> subitems;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010028
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010029 public MemItem(String _label, String _shortLabel, long _pss, int _id) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010030 label = _label;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010031 shortLabel = _shortLabel;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010032 pss = _pss;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010033 id = _id;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010034 }
10035 }
10036
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010037 static final void dumpMemItems(PrintWriter pw, String prefix, ArrayList<MemItem> items,
Dianne Hackbornb437e092011-08-05 17:50:29 -070010038 boolean sort) {
10039 if (sort) {
10040 Collections.sort(items, new Comparator<MemItem>() {
10041 @Override
10042 public int compare(MemItem lhs, MemItem rhs) {
10043 if (lhs.pss < rhs.pss) {
10044 return 1;
10045 } else if (lhs.pss > rhs.pss) {
10046 return -1;
10047 }
10048 return 0;
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010049 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010050 });
10051 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010052
10053 for (int i=0; i<items.size(); i++) {
10054 MemItem mi = items.get(i);
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010055 pw.print(prefix); pw.printf("%7d kB: ", mi.pss); pw.println(mi.label);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010056 if (mi.subitems != null) {
10057 dumpMemItems(pw, prefix + " ", mi.subitems, true);
10058 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010059 }
10060 }
10061
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010062 // These are in KB.
10063 static final long[] DUMP_MEM_BUCKETS = new long[] {
10064 5*1024, 7*1024, 10*1024, 15*1024, 20*1024, 30*1024, 40*1024, 80*1024,
10065 120*1024, 160*1024, 200*1024,
10066 250*1024, 300*1024, 350*1024, 400*1024, 500*1024, 600*1024, 800*1024,
10067 1*1024*1024, 2*1024*1024, 5*1024*1024, 10*1024*1024, 20*1024*1024
10068 };
10069
Dianne Hackborn672342c2011-11-29 11:29:02 -080010070 static final void appendMemBucket(StringBuilder out, long memKB, String label,
10071 boolean stackLike) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010072 int start = label.lastIndexOf('.');
10073 if (start >= 0) start++;
10074 else start = 0;
10075 int end = label.length();
10076 for (int i=0; i<DUMP_MEM_BUCKETS.length; i++) {
10077 if (DUMP_MEM_BUCKETS[i] >= memKB) {
10078 long bucket = DUMP_MEM_BUCKETS[i]/1024;
10079 out.append(bucket);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010080 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010081 out.append(label, start, end);
10082 return;
10083 }
10084 }
10085 out.append(memKB/1024);
Dianne Hackborn672342c2011-11-29 11:29:02 -080010086 out.append(stackLike ? "MB." : "MB ");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010087 out.append(label, start, end);
10088 }
10089
10090 static final int[] DUMP_MEM_OOM_ADJ = new int[] {
10091 ProcessList.SYSTEM_ADJ, ProcessList.PERSISTENT_PROC_ADJ, ProcessList.FOREGROUND_APP_ADJ,
10092 ProcessList.VISIBLE_APP_ADJ, ProcessList.PERCEPTIBLE_APP_ADJ, ProcessList.HEAVY_WEIGHT_APP_ADJ,
10093 ProcessList.BACKUP_APP_ADJ, ProcessList.SERVICE_ADJ, ProcessList.HOME_APP_ADJ,
10094 ProcessList.PREVIOUS_APP_ADJ, ProcessList.SERVICE_B_ADJ, ProcessList.HIDDEN_APP_MAX_ADJ
10095 };
10096 static final String[] DUMP_MEM_OOM_LABEL = new String[] {
10097 "System", "Persistent", "Foreground",
10098 "Visible", "Perceptible", "Heavy Weight",
10099 "Backup", "A Services", "Home", "Previous",
10100 "B Services", "Background"
10101 };
10102
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010103 final void dumpApplicationMemoryUsage(FileDescriptor fd,
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010104 PrintWriter pw, String prefix, String[] args, boolean brief,
Dianne Hackborn672342c2011-11-29 11:29:02 -080010105 PrintWriter categoryPw, StringBuilder outTag, StringBuilder outStack) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010106 boolean dumpAll = false;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010107 boolean oomOnly = false;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010108
10109 int opti = 0;
10110 while (opti < args.length) {
10111 String opt = args[opti];
10112 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
10113 break;
10114 }
10115 opti++;
10116 if ("-a".equals(opt)) {
10117 dumpAll = true;
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010118 } else if ("--oom".equals(opt)) {
10119 oomOnly = true;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010120 } else if ("-h".equals(opt)) {
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010121 pw.println("meminfo dump options: [-a] [--oom] [process]");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010122 pw.println(" -a: include all available information for each process.");
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010123 pw.println(" --oom: only show processes organized by oom adj.");
Dianne Hackbornb437e092011-08-05 17:50:29 -070010124 pw.println("If [process] is specified it can be the name or ");
10125 pw.println("pid of a specific process to dump.");
10126 return;
10127 } else {
10128 pw.println("Unknown argument: " + opt + "; use -h for help");
10129 }
10130 }
10131
10132 ArrayList<ProcessRecord> procs = collectProcesses(pw, opti, args);
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010133 if (procs == null) {
10134 return;
10135 }
10136
Dianne Hackborn6447ca32009-04-07 19:50:08 -070010137 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010138 long uptime = SystemClock.uptimeMillis();
10139 long realtime = SystemClock.elapsedRealtime();
Dianne Hackbornb437e092011-08-05 17:50:29 -070010140
10141 if (procs.size() == 1 || isCheckinRequest) {
10142 dumpAll = true;
10143 }
10144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010145 if (isCheckinRequest) {
10146 // short checkin version
10147 pw.println(uptime + "," + realtime);
10148 pw.flush();
10149 } else {
10150 pw.println("Applications Memory Usage (kB):");
10151 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
10152 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010153
Dianne Hackbornb437e092011-08-05 17:50:29 -070010154 String[] innerArgs = new String[args.length-opti];
10155 System.arraycopy(args, opti, innerArgs, 0, args.length-opti);
10156
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010157 ArrayList<MemItem> procMems = new ArrayList<MemItem>();
10158 long nativePss=0, dalvikPss=0, otherPss=0;
10159 long[] miscPss = new long[Debug.MemoryInfo.NUM_OTHER_STATS];
10160
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010161 long oomPss[] = new long[DUMP_MEM_OOM_LABEL.length];
10162 ArrayList<MemItem>[] oomProcs = (ArrayList<MemItem>[])
10163 new ArrayList[DUMP_MEM_OOM_LABEL.length];
Dianne Hackbornb437e092011-08-05 17:50:29 -070010164
10165 long totalPss = 0;
10166
Dianne Hackborne17aeb32011-04-07 15:11:57 -070010167 for (int i = procs.size() - 1 ; i >= 0 ; i--) {
10168 ProcessRecord r = procs.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010169 if (r.thread != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010170 if (!isCheckinRequest && dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
10172 pw.flush();
10173 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010174 Debug.MemoryInfo mi = null;
Dianne Hackbornb437e092011-08-05 17:50:29 -070010175 if (dumpAll) {
10176 try {
10177 mi = r.thread.dumpMemInfo(fd, isCheckinRequest, dumpAll, innerArgs);
10178 } catch (RemoteException e) {
10179 if (!isCheckinRequest) {
10180 pw.println("Got RemoteException!");
10181 pw.flush();
10182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010184 } else {
10185 mi = new Debug.MemoryInfo();
10186 Debug.getMemoryInfo(r.pid, mi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010188
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010189 if (!isCheckinRequest && mi != null) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010190 long myTotalPss = mi.getTotalPss();
10191 totalPss += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010192 MemItem pssItem = new MemItem(r.processName + " (pid " + r.pid + ")",
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010193 r.processName, myTotalPss, 0);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010194 procMems.add(pssItem);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010195
10196 nativePss += mi.nativePss;
10197 dalvikPss += mi.dalvikPss;
10198 otherPss += mi.otherPss;
10199 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
10200 long mem = mi.getOtherPss(j);
10201 miscPss[j] += mem;
10202 otherPss -= mem;
10203 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010204
10205 for (int oomIndex=0; oomIndex<oomPss.length; oomIndex++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010206 if (r.setAdj <= DUMP_MEM_OOM_ADJ[oomIndex]
10207 || oomIndex == (oomPss.length-1)) {
Dianne Hackbornb437e092011-08-05 17:50:29 -070010208 oomPss[oomIndex] += myTotalPss;
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010209 if (oomProcs[oomIndex] == null) {
10210 oomProcs[oomIndex] = new ArrayList<MemItem>();
10211 }
10212 oomProcs[oomIndex].add(pssItem);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010213 break;
10214 }
10215 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 }
10218 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010219
10220 if (!isCheckinRequest && procs.size() > 1) {
10221 ArrayList<MemItem> catMems = new ArrayList<MemItem>();
10222
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010223 catMems.add(new MemItem("Native", "Native", nativePss, -1));
10224 catMems.add(new MemItem("Dalvik", "Dalvik", dalvikPss, -2));
10225 catMems.add(new MemItem("Unknown", "Unknown", otherPss, -3));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010226 for (int j=0; j<Debug.MemoryInfo.NUM_OTHER_STATS; j++) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010227 String label = Debug.MemoryInfo.getOtherLabel(j);
10228 catMems.add(new MemItem(label, label, miscPss[j], j));
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010229 }
10230
Dianne Hackbornb437e092011-08-05 17:50:29 -070010231 ArrayList<MemItem> oomMems = new ArrayList<MemItem>();
10232 for (int j=0; j<oomPss.length; j++) {
10233 if (oomPss[j] != 0) {
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010234 String label = DUMP_MEM_OOM_LABEL[j];
10235 MemItem item = new MemItem(label, label, oomPss[j],
10236 DUMP_MEM_OOM_ADJ[j]);
Dianne Hackborna4bacb82011-08-24 15:12:38 -070010237 item.subitems = oomProcs[j];
10238 oomMems.add(item);
Dianne Hackbornb437e092011-08-05 17:50:29 -070010239 }
10240 }
10241
Dianne Hackborn672342c2011-11-29 11:29:02 -080010242 if (outTag != null || outStack != null) {
10243 if (outTag != null) {
10244 appendMemBucket(outTag, totalPss, "total", false);
10245 }
10246 if (outStack != null) {
10247 appendMemBucket(outStack, totalPss, "total", true);
10248 }
10249 boolean firstLine = true;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010250 for (int i=0; i<oomMems.size(); i++) {
10251 MemItem miCat = oomMems.get(i);
10252 if (miCat.subitems == null || miCat.subitems.size() < 1) {
10253 continue;
10254 }
10255 if (miCat.id < ProcessList.SERVICE_ADJ
10256 || miCat.id == ProcessList.HOME_APP_ADJ
10257 || miCat.id == ProcessList.PREVIOUS_APP_ADJ) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010258 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10259 outTag.append(" / ");
10260 }
10261 if (outStack != null) {
10262 if (miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10263 if (firstLine) {
10264 outStack.append(":");
10265 firstLine = false;
10266 }
10267 outStack.append("\n\t at ");
10268 } else {
10269 outStack.append("$");
10270 }
10271 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010272 for (int j=0; j<miCat.subitems.size(); j++) {
10273 MemItem mi = miCat.subitems.get(j);
10274 if (j > 0) {
Dianne Hackborn672342c2011-11-29 11:29:02 -080010275 if (outTag != null) {
10276 outTag.append(" ");
10277 }
10278 if (outStack != null) {
10279 outStack.append("$");
10280 }
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010281 }
Dianne Hackborn672342c2011-11-29 11:29:02 -080010282 if (outTag != null && miCat.id <= ProcessList.FOREGROUND_APP_ADJ) {
10283 appendMemBucket(outTag, mi.pss, mi.shortLabel, false);
10284 }
10285 if (outStack != null) {
10286 appendMemBucket(outStack, mi.pss, mi.shortLabel, true);
10287 }
10288 }
10289 if (outStack != null && miCat.id >= ProcessList.FOREGROUND_APP_ADJ) {
10290 outStack.append("(");
10291 for (int k=0; k<DUMP_MEM_OOM_ADJ.length; k++) {
10292 if (DUMP_MEM_OOM_ADJ[k] == miCat.id) {
10293 outStack.append(DUMP_MEM_OOM_LABEL[k]);
10294 outStack.append(":");
10295 outStack.append(DUMP_MEM_OOM_ADJ[k]);
10296 }
10297 }
10298 outStack.append(")");
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010299 }
10300 }
10301 }
10302 }
10303
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010304 if (!brief && !oomOnly) {
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010305 pw.println();
10306 pw.println("Total PSS by process:");
10307 dumpMemItems(pw, " ", procMems, true);
10308 pw.println();
10309 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010310 pw.println("Total PSS by OOM adjustment:");
10311 dumpMemItems(pw, " ", oomMems, false);
Dianne Hackborne4d4fbc2011-11-08 11:53:28 -080010312 if (!oomOnly) {
10313 PrintWriter out = categoryPw != null ? categoryPw : pw;
10314 out.println();
10315 out.println("Total PSS by category:");
10316 dumpMemItems(out, " ", catMems, true);
Dianne Hackborn04d6db32011-11-04 20:07:24 -070010317 }
Dianne Hackbornb437e092011-08-05 17:50:29 -070010318 pw.println();
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080010319 pw.print("Total PSS: "); pw.print(totalPss); pw.println(" kB");
Dianne Hackbornd3e677b2012-04-05 14:58:18 -070010320 final int[] SINGLE_LONG_FORMAT = new int[] {
10321 Process.PROC_SPACE_TERM|Process.PROC_OUT_LONG
10322 };
10323 long[] longOut = new long[1];
10324 Process.readProcFile("/sys/kernel/mm/ksm/pages_shared",
10325 SINGLE_LONG_FORMAT, null, longOut, null);
10326 long shared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10327 longOut[0] = 0;
10328 Process.readProcFile("/sys/kernel/mm/ksm/pages_sharing",
10329 SINGLE_LONG_FORMAT, null, longOut, null);
10330 long sharing = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10331 longOut[0] = 0;
10332 Process.readProcFile("/sys/kernel/mm/ksm/pages_unshared",
10333 SINGLE_LONG_FORMAT, null, longOut, null);
10334 long unshared = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10335 longOut[0] = 0;
10336 Process.readProcFile("/sys/kernel/mm/ksm/pages_volatile",
10337 SINGLE_LONG_FORMAT, null, longOut, null);
10338 long voltile = longOut[0] * ProcessList.PAGE_SIZE / 1024;
10339 pw.print(" KSM: "); pw.print(sharing); pw.print(" kB saved from shared ");
10340 pw.print(shared); pw.println(" kB");
10341 pw.print(" "); pw.print(unshared); pw.print(" kB unshared; ");
10342 pw.print(voltile); pw.println(" kB volatile");
Dianne Hackborn0e3328f2011-07-17 13:31:17 -070010343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344 }
10345
10346 /**
10347 * Searches array of arguments for the specified string
10348 * @param args array of argument strings
10349 * @param value value to search for
10350 * @return true if the value is contained in the array
10351 */
10352 private static boolean scanArgs(String[] args, String value) {
10353 if (args != null) {
10354 for (String arg : args) {
10355 if (value.equals(arg)) {
10356 return true;
10357 }
10358 }
10359 }
10360 return false;
10361 }
10362
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010363 private final boolean removeDyingProviderLocked(ProcessRecord proc,
10364 ContentProviderRecord cpr, boolean always) {
10365 final boolean inLaunching = mLaunchingProviders.contains(cpr);
10366
10367 if (!inLaunching || always) {
10368 synchronized (cpr) {
10369 cpr.launchingApp = null;
10370 cpr.notifyAll();
10371 }
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010372 mProviderMap.removeProviderByClass(cpr.name, UserHandle.getUserId(cpr.uid));
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010373 String names[] = cpr.info.authority.split(";");
10374 for (int j = 0; j < names.length; j++) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010375 mProviderMap.removeProviderByName(names[j], UserHandle.getUserId(cpr.uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 }
10377 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010378
10379 for (int i=0; i<cpr.connections.size(); i++) {
10380 ContentProviderConnection conn = cpr.connections.get(i);
10381 if (conn.waiting) {
10382 // If this connection is waiting for the provider, then we don't
10383 // need to mess with its process unless we are always removing
10384 // or for some reason the provider is not currently launching.
10385 if (inLaunching && !always) {
10386 continue;
10387 }
10388 }
10389 ProcessRecord capp = conn.client;
10390 conn.dead = true;
10391 if (conn.stableCount > 0) {
10392 if (!capp.persistent && capp.thread != null
10393 && capp.pid != 0
10394 && capp.pid != MY_PID) {
10395 Slog.i(TAG, "Kill " + capp.processName
10396 + " (pid " + capp.pid + "): provider " + cpr.info.name
10397 + " in dying process " + (proc != null ? proc.processName : "??"));
10398 EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid,
10399 capp.processName, capp.setAdj, "dying provider "
10400 + cpr.name.toShortString());
10401 Process.killProcessQuiet(capp.pid);
10402 }
10403 } else if (capp.thread != null && conn.provider.provider != null) {
10404 try {
10405 capp.thread.unstableProviderDied(conn.provider.provider.asBinder());
10406 } catch (RemoteException e) {
10407 }
10408 // In the protocol here, we don't expect the client to correctly
10409 // clean up this connection, we'll just remove it.
10410 cpr.connections.remove(i);
10411 conn.client.conProviders.remove(conn);
10412 }
10413 }
10414
10415 if (inLaunching && always) {
10416 mLaunchingProviders.remove(cpr);
10417 }
10418 return inLaunching;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010419 }
10420
10421 /**
10422 * Main code for cleaning up a process when it has gone away. This is
10423 * called both as a result of the process dying, or directly when stopping
10424 * a process when running in single process mode.
10425 */
10426 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
Dianne Hackborn130b0d22011-07-26 22:07:48 -070010427 boolean restarting, boolean allowRestart, int index) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010428 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010429 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 }
10431
Dianne Hackborn36124872009-10-08 16:22:03 -070010432 mProcessesToGc.remove(app);
10433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 // Dismiss any open dialogs.
10435 if (app.crashDialog != null) {
10436 app.crashDialog.dismiss();
10437 app.crashDialog = null;
10438 }
10439 if (app.anrDialog != null) {
10440 app.anrDialog.dismiss();
10441 app.anrDialog = null;
10442 }
10443 if (app.waitDialog != null) {
10444 app.waitDialog.dismiss();
10445 app.waitDialog = null;
10446 }
10447
10448 app.crashing = false;
10449 app.notResponding = false;
10450
10451 app.resetPackageList();
Dianne Hackborn1b64e0d2011-07-17 15:23:59 -070010452 app.unlinkDeathRecipient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 app.thread = null;
10454 app.forcingToForeground = null;
10455 app.foregroundServices = false;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010456 app.foregroundActivities = false;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070010457 app.hasShownUi = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070010458 app.hasAboveClient = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010459
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010460 mServices.killServicesLocked(app, allowRestart);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010461
10462 boolean restart = false;
10463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010464 // Remove published content providers.
10465 if (!app.pubProviders.isEmpty()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010466 Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010467 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010468 ContentProviderRecord cpr = it.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010469
10470 final boolean always = app.bad || !allowRestart;
10471 if (removeDyingProviderLocked(app, cpr, always) || always) {
10472 // We left the provider in the launching list, need to
10473 // restart it.
10474 restart = true;
10475 }
10476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010477 cpr.provider = null;
Dianne Hackborn2a6bcda2011-09-21 15:07:05 -070010478 cpr.proc = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010479 }
10480 app.pubProviders.clear();
10481 }
10482
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010483 // Take care of any launching providers waiting for this process.
10484 if (checkAppInLaunchingProvidersLocked(app, false)) {
10485 restart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488 // Unregister from connected content providers.
10489 if (!app.conProviders.isEmpty()) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010490 for (int i=0; i<app.conProviders.size(); i++) {
10491 ContentProviderConnection conn = app.conProviders.get(i);
10492 conn.provider.connections.remove(conn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010493 }
10494 app.conProviders.clear();
10495 }
10496
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010497 // At this point there may be remaining entries in mLaunchingProviders
10498 // where we were the only one waiting, so they are no longer of use.
10499 // Look for these and clean up if found.
10500 // XXX Commented out for now. Trying to figure out a way to reproduce
10501 // the actual situation to identify what is actually going on.
10502 if (false) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010503 for (int i=0; i<mLaunchingProviders.size(); i++) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010504 ContentProviderRecord cpr = (ContentProviderRecord)
10505 mLaunchingProviders.get(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010506 if (cpr.connections.size() <= 0 && !cpr.hasExternalProcessHandles()) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010507 synchronized (cpr) {
10508 cpr.launchingApp = null;
10509 cpr.notifyAll();
10510 }
10511 }
10512 }
10513 }
10514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 skipCurrentReceiverLocked(app);
10516
10517 // Unregister any receivers.
10518 if (app.receivers.size() > 0) {
10519 Iterator<ReceiverList> it = app.receivers.iterator();
10520 while (it.hasNext()) {
10521 removeReceiverLocked(it.next());
10522 }
10523 app.receivers.clear();
10524 }
10525
Christopher Tate181fafa2009-05-14 11:12:14 -070010526 // If the app is undergoing backup, tell the backup manager about it
10527 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010528 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -070010529 try {
10530 IBackupManager bm = IBackupManager.Stub.asInterface(
10531 ServiceManager.getService(Context.BACKUP_SERVICE));
10532 bm.agentDisconnected(app.info.packageName);
10533 } catch (RemoteException e) {
10534 // can't happen; backup manager is local
10535 }
10536 }
10537
Dianne Hackborna93c2c12012-05-31 15:29:36 -070010538 for (int i = mPendingProcessChanges.size()-1; i>=0; i--) {
10539 ProcessChangeItem item = mPendingProcessChanges.get(i);
10540 if (item.pid == app.pid) {
10541 mPendingProcessChanges.remove(i);
10542 mAvailProcessChanges.add(item);
10543 }
10544 }
Jeff Sharkey287bd832011-05-28 19:36:26 -070010545 mHandler.obtainMessage(DISPATCH_PROCESS_DIED, app.pid, app.info.uid, null).sendToTarget();
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070010546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 // If the caller is restarting this app, then leave it in its
10548 // current lists and let the caller take care of it.
10549 if (restarting) {
10550 return;
10551 }
10552
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010553 if (!app.persistent || app.isolated) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010554 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010555 "Removing non-persistent process during cleanup: " + app);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010556 mProcessNames.remove(app.processName, app.uid);
10557 mIsolatedProcesses.remove(app.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -070010558 if (mHeavyWeightProcess == app) {
10559 mHeavyWeightProcess = null;
10560 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
10561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010562 } else if (!app.removed) {
10563 // This app is persistent, so we need to keep its record around.
10564 // If it is not already on the pending app list, add it there
10565 // and start a new process for it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 if (mPersistentStartingProcesses.indexOf(app) < 0) {
10567 mPersistentStartingProcesses.add(app);
10568 restart = true;
10569 }
10570 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010571 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
10572 "Clean-up removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573 mProcessesOnHold.remove(app);
10574
The Android Open Source Project4df24232009-03-05 14:34:35 -080010575 if (app == mHomeProcess) {
10576 mHomeProcess = null;
10577 }
Dianne Hackbornf35fe232011-11-01 19:25:20 -070010578 if (app == mPreviousProcess) {
10579 mPreviousProcess = null;
10580 }
10581
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010582 if (restart && !app.isolated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 // We have components that still need to be running in the
10584 // process, so re-launch it.
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010585 mProcessNames.put(app.processName, app.uid, app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586 startProcessLocked(app, "restart", app.processName);
10587 } else if (app.pid > 0 && app.pid != MY_PID) {
10588 // Goodbye!
10589 synchronized (mPidsSelfLocked) {
10590 mPidsSelfLocked.remove(app.pid);
10591 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
10592 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010593 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 }
10595 }
10596
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010597 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
10598 // Look through the content providers we are waiting to have launched,
10599 // and if any run in this process then either schedule a restart of
10600 // the process or kill the client waiting for it if this process has
10601 // gone bad.
10602 int NL = mLaunchingProviders.size();
10603 boolean restart = false;
10604 for (int i=0; i<NL; i++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070010605 ContentProviderRecord cpr = mLaunchingProviders.get(i);
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010606 if (cpr.launchingApp == app) {
10607 if (!alwaysBad && !app.bad) {
10608 restart = true;
10609 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070010610 removeDyingProviderLocked(app, cpr, true);
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010611 NL = mLaunchingProviders.size();
10612 }
10613 }
10614 }
10615 return restart;
10616 }
10617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010618 // =========================================================
10619 // SERVICES
10620 // =========================================================
10621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
10623 int flags) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010624 enforceNotIsolatedCaller("getServices");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010626 return mServices.getRunningServiceInfoLocked(maxNum, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010627 }
10628 }
10629
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010630 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010631 enforceNotIsolatedCaller("getRunningServiceControlPanel");
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010632 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010633 return mServices.getRunningServiceControlPanelLocked(name);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010634 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010635 }
10636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 public ComponentName startService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010638 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010639 enforceNotIsolatedCaller("startService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 // Refuse possible leaked file descriptors
10641 if (service != null && service.hasFileDescriptors() == true) {
10642 throw new IllegalArgumentException("File descriptors passed in Intent");
10643 }
10644
Amith Yamasani742a6712011-05-04 14:49:28 -070010645 if (DEBUG_SERVICE)
10646 Slog.v(TAG, "startService: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 synchronized(this) {
10648 final int callingPid = Binder.getCallingPid();
10649 final int callingUid = Binder.getCallingUid();
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010650 checkValidCaller(callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010652 ComponentName res = mServices.startServiceLocked(caller, service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010653 resolvedType, callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010654 Binder.restoreCallingIdentity(origId);
10655 return res;
10656 }
10657 }
10658
10659 ComponentName startServiceInPackage(int uid,
10660 Intent service, String resolvedType) {
10661 synchronized(this) {
Amith Yamasani742a6712011-05-04 14:49:28 -070010662 if (DEBUG_SERVICE)
10663 Slog.v(TAG, "startServiceInPackage: " + service + " type=" + resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010664 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010665 ComponentName res = mServices.startServiceLocked(null, service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010666 resolvedType, -1, uid, UserHandle.getUserId(uid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010667 Binder.restoreCallingIdentity(origId);
10668 return res;
10669 }
10670 }
10671
10672 public int stopService(IApplicationThread caller, Intent service,
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010673 String resolvedType, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010674 enforceNotIsolatedCaller("stopService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 // Refuse possible leaked file descriptors
10676 if (service != null && service.hasFileDescriptors() == true) {
10677 throw new IllegalArgumentException("File descriptors passed in Intent");
10678 }
10679
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010680 checkValidCaller(Binder.getCallingUid(), userId);
10681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010682 synchronized(this) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -070010683 return mServices.stopServiceLocked(caller, service, resolvedType, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010685 }
10686
10687 public IBinder peekService(Intent service, String resolvedType) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010688 enforceNotIsolatedCaller("peekService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689 // Refuse possible leaked file descriptors
10690 if (service != null && service.hasFileDescriptors() == true) {
10691 throw new IllegalArgumentException("File descriptors passed in Intent");
10692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010694 return mServices.peekServiceLocked(service, resolvedType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010696 }
10697
10698 public boolean stopServiceToken(ComponentName className, IBinder token,
10699 int startId) {
10700 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010701 return mServices.stopServiceTokenLocked(className, token, startId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010703 }
10704
10705 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070010706 int id, Notification notification, boolean removeNotification) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010707 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010708 mServices.setServiceForegroundLocked(className, token, id, notification,
10709 removeNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 }
10711 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070010712
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010713 boolean isSingleton(String componentProcessName, ApplicationInfo aInfo,
10714 String className, int flags) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070010715 boolean result = false;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010716 if (UserHandle.getAppId(aInfo.uid) >= Process.FIRST_APPLICATION_UID) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010717 if ((flags&ServiceInfo.FLAG_SINGLE_USER) != 0) {
10718 if (ActivityManager.checkUidPermission(
10719 android.Manifest.permission.INTERACT_ACROSS_USERS,
10720 aInfo.uid) != PackageManager.PERMISSION_GRANTED) {
10721 ComponentName comp = new ComponentName(aInfo.packageName, className);
10722 String msg = "Permission Denial: Component " + comp.flattenToShortString()
10723 + " requests FLAG_SINGLE_USER, but app does not hold "
10724 + android.Manifest.permission.INTERACT_ACROSS_USERS;
10725 Slog.w(TAG, msg);
10726 throw new SecurityException(msg);
10727 }
10728 result = true;
10729 }
Amith Yamasania4a54e22012-04-16 15:44:19 -070010730 } else if (componentProcessName == aInfo.packageName) {
10731 result = (aInfo.flags & ApplicationInfo.FLAG_PERSISTENT) != 0;
10732 } else if ("system".equals(componentProcessName)) {
10733 result = true;
10734 }
10735 if (DEBUG_MU) {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070010736 Slog.v(TAG, "isSingleton(" + componentProcessName + ", " + aInfo
10737 + ", " + className + ", 0x" + Integer.toHexString(flags) + ") = " + result);
Amith Yamasania4a54e22012-04-16 15:44:19 -070010738 }
10739 return result;
10740 }
10741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 public int bindService(IApplicationThread caller, IBinder token,
10743 Intent service, String resolvedType,
Amith Yamasani37ce3a82012-02-06 12:04:42 -080010744 IServiceConnection connection, int flags, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010745 enforceNotIsolatedCaller("bindService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746 // Refuse possible leaked file descriptors
10747 if (service != null && service.hasFileDescriptors() == true) {
10748 throw new IllegalArgumentException("File descriptors passed in Intent");
10749 }
10750
Jeff Sharkey35744c12012-08-28 16:48:05 -070010751 if (userId != UserHandle.getCallingUserId()) {
10752 // Requesting a different user, make sure that they have permission
10753 if (checkComponentPermission(
10754 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
10755 Binder.getCallingPid(), Binder.getCallingUid(), -1, true)
10756 == PackageManager.PERMISSION_GRANTED) {
10757 // Translate to the current user id, if caller wasn't aware
10758 if (userId == UserHandle.USER_CURRENT) {
10759 userId = mCurrentUserId;
10760 }
10761 } else {
10762 String msg = "Permission Denial: Request to bindService as user " + userId
10763 + " but is calling from user " + UserHandle.getCallingUserId()
10764 + "; this requires "
10765 + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
10766 Slog.w(TAG, msg);
10767 throw new SecurityException(msg);
10768 }
10769 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -080010770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010771 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010772 return mServices.bindServiceLocked(caller, token, service, resolvedType,
10773 connection, flags, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 }
10775 }
10776
10777 public boolean unbindService(IServiceConnection connection) {
10778 synchronized (this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010779 return mServices.unbindServiceLocked(connection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010781 }
10782
10783 public void publishService(IBinder token, Intent intent, IBinder service) {
10784 // Refuse possible leaked file descriptors
10785 if (intent != null && intent.hasFileDescriptors() == true) {
10786 throw new IllegalArgumentException("File descriptors passed in Intent");
10787 }
10788
10789 synchronized(this) {
10790 if (!(token instanceof ServiceRecord)) {
10791 throw new IllegalArgumentException("Invalid service token");
10792 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010793 mServices.publishServiceLocked((ServiceRecord)token, intent, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010794 }
10795 }
10796
10797 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
10798 // Refuse possible leaked file descriptors
10799 if (intent != null && intent.hasFileDescriptors() == true) {
10800 throw new IllegalArgumentException("File descriptors passed in Intent");
10801 }
10802
10803 synchronized(this) {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010804 mServices.unbindFinishedLocked((ServiceRecord)token, intent, doRebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010805 }
10806 }
10807
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010808 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010809 synchronized(this) {
10810 if (!(token instanceof ServiceRecord)) {
10811 throw new IllegalArgumentException("Invalid service token");
10812 }
Dianne Hackborn599db5c2012-08-03 19:28:48 -070010813 mServices.serviceDoneExecutingLocked((ServiceRecord)token, type, startId, res);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010815 }
10816
10817 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -070010818 // BACKUP AND RESTORE
10819 // =========================================================
10820
10821 // Cause the target app to be launched if necessary and its backup agent
10822 // instantiated. The backup agent will invoke backupAgentCreated() on the
10823 // activity manager to announce its creation.
10824 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010825 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070010826 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
10827
10828 synchronized(this) {
10829 // !!! TODO: currently no check here that we're already bound
10830 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
10831 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
10832 synchronized (stats) {
10833 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
10834 }
10835
Dianne Hackborne7f97212011-02-24 14:40:20 -080010836 // Backup agent is now in use, its package can't be stopped.
10837 try {
10838 AppGlobals.getPackageManager().setPackageStoppedState(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070010839 app.packageName, false, UserHandle.getUserId(app.uid));
Dianne Hackborne7f97212011-02-24 14:40:20 -080010840 } catch (RemoteException e) {
Dianne Hackborna925cd42011-03-10 13:18:20 -080010841 } catch (IllegalArgumentException e) {
10842 Slog.w(TAG, "Failed trying to unstop package "
10843 + app.packageName + ": " + e);
Dianne Hackborne7f97212011-02-24 14:40:20 -080010844 }
10845
Christopher Tate181fafa2009-05-14 11:12:14 -070010846 BackupRecord r = new BackupRecord(ss, app, backupMode);
Christopher Tate4a627c72011-04-01 14:43:32 -070010847 ComponentName hostingName = (backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL)
10848 ? new ComponentName(app.packageName, app.backupAgentName)
10849 : new ComponentName("android", "FullBackupAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -070010850 // startProcessLocked() returns existing proc's record if it's already running
10851 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborna0c283e2012-02-09 10:47:01 -080010852 false, 0, "backup", hostingName, false, false);
Christopher Tate181fafa2009-05-14 11:12:14 -070010853 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010854 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -070010855 return false;
10856 }
10857
10858 r.app = proc;
10859 mBackupTarget = r;
10860 mBackupAppName = app.packageName;
10861
Christopher Tate6fa95972009-06-05 18:43:55 -070010862 // Try not to kill the process during backup
10863 updateOomAdjLocked(proc);
10864
Christopher Tate181fafa2009-05-14 11:12:14 -070010865 // If the process is already attached, schedule the creation of the backup agent now.
10866 // If it is not yet live, this will be done when it attaches to the framework.
10867 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010868 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -070010869 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040010870 proc.thread.scheduleCreateBackupAgent(app,
10871 compatibilityInfoForPackageLocked(app), backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070010872 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -070010873 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -070010874 }
10875 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010876 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -070010877 }
10878 // Invariants: at this point, the target app process exists and the application
10879 // is either already running or in the process of coming up. mBackupTarget and
10880 // mBackupAppName describe the app, so that when it binds back to the AM we
10881 // know that it's scheduled for a backup-agent operation.
10882 }
10883
10884 return true;
10885 }
10886
10887 // A backup agent has just come up
10888 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010889 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -070010890 + " = " + agent);
10891
10892 synchronized(this) {
10893 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010894 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -070010895 return;
10896 }
Dianne Hackborn06740692010-09-22 22:46:21 -070010897 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010898
Dianne Hackborn06740692010-09-22 22:46:21 -070010899 long oldIdent = Binder.clearCallingIdentity();
10900 try {
10901 IBackupManager bm = IBackupManager.Stub.asInterface(
10902 ServiceManager.getService(Context.BACKUP_SERVICE));
10903 bm.agentConnected(agentPackageName, agent);
10904 } catch (RemoteException e) {
10905 // can't happen; the backup manager service is local
10906 } catch (Exception e) {
10907 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
10908 e.printStackTrace();
10909 } finally {
10910 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -070010911 }
10912 }
10913
10914 // done with this agent
10915 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010916 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -070010917 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010918 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -070010919 return;
10920 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010921
10922 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -070010923 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010924 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -070010925 return;
10926 }
10927
Christopher Tate181fafa2009-05-14 11:12:14 -070010928 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010929 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -070010930 return;
10931 }
10932
Christopher Tate6fa95972009-06-05 18:43:55 -070010933 ProcessRecord proc = mBackupTarget.app;
10934 mBackupTarget = null;
10935 mBackupAppName = null;
10936
10937 // Not backing this app up any more; reset its OOM adjustment
10938 updateOomAdjLocked(proc);
10939
Christopher Tatec7b31e32009-06-10 15:49:30 -070010940 // If the app crashed during backup, 'thread' will be null here
10941 if (proc.thread != null) {
10942 try {
Dianne Hackborne2515ee2011-04-27 18:52:56 -040010943 proc.thread.scheduleDestroyBackupAgent(appInfo,
10944 compatibilityInfoForPackageLocked(appInfo));
Christopher Tatec7b31e32009-06-10 15:49:30 -070010945 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010946 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -070010947 e.printStackTrace();
10948 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010949 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010950 }
10951 }
10952 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010953 // BROADCASTS
10954 // =========================================================
10955
Josh Bartel7f208742010-02-25 11:01:44 -060010956 private final List getStickiesLocked(String action, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010957 List cur) {
10958 final ContentResolver resolver = mContext.getContentResolver();
10959 final ArrayList<Intent> list = mStickyBroadcasts.get(action);
10960 if (list == null) {
10961 return cur;
10962 }
10963 int N = list.size();
10964 for (int i=0; i<N; i++) {
10965 Intent intent = list.get(i);
10966 if (filter.match(resolver, intent, true, TAG) >= 0) {
10967 if (cur == null) {
10968 cur = new ArrayList<Intent>();
10969 }
10970 cur.add(intent);
10971 }
10972 }
10973 return cur;
10974 }
10975
Christopher Tatef46723b2012-01-26 14:19:24 -080010976 boolean isPendingBroadcastProcessLocked(int pid) {
10977 return mFgBroadcastQueue.isPendingBroadcastProcessLocked(pid)
10978 || mBgBroadcastQueue.isPendingBroadcastProcessLocked(pid);
10979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010980
Christopher Tatef46723b2012-01-26 14:19:24 -080010981 void skipPendingBroadcastLocked(int pid) {
10982 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
10983 for (BroadcastQueue queue : mBroadcastQueues) {
10984 queue.skipPendingBroadcastLocked(pid);
10985 }
10986 }
10987
10988 // The app just attached; send any pending broadcasts that it should receive
10989 boolean sendPendingBroadcastsLocked(ProcessRecord app) {
10990 boolean didSomething = false;
10991 for (BroadcastQueue queue : mBroadcastQueues) {
10992 didSomething |= queue.sendPendingBroadcastsLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 }
Christopher Tatef46723b2012-01-26 14:19:24 -080010994 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010995 }
10996
Dianne Hackborn6c418d52011-06-29 14:05:33 -070010997 public Intent registerReceiver(IApplicationThread caller, String callerPackage,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 IIntentReceiver receiver, IntentFilter filter, String permission) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080010999 enforceNotIsolatedCaller("registerReceiver");
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011000 int callingUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 synchronized(this) {
11002 ProcessRecord callerApp = null;
11003 if (caller != null) {
11004 callerApp = getRecordForAppLocked(caller);
11005 if (callerApp == null) {
11006 throw new SecurityException(
11007 "Unable to find app for caller " + caller
11008 + " (pid=" + Binder.getCallingPid()
11009 + ") when registering receiver " + receiver);
11010 }
Dianne Hackborn6c418d52011-06-29 14:05:33 -070011011 if (callerApp.info.uid != Process.SYSTEM_UID &&
11012 !callerApp.pkgList.contains(callerPackage)) {
11013 throw new SecurityException("Given caller package " + callerPackage
11014 + " is not running in process " + callerApp);
11015 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011016 callingUid = callerApp.info.uid;
Dianne Hackborn6c418d52011-06-29 14:05:33 -070011017 } else {
11018 callerPackage = null;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011019 callingUid = Binder.getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 }
11021
11022 List allSticky = null;
11023
11024 // Look for any matching sticky broadcasts...
11025 Iterator actions = filter.actionsIterator();
11026 if (actions != null) {
11027 while (actions.hasNext()) {
11028 String action = (String)actions.next();
Josh Bartel7f208742010-02-25 11:01:44 -060011029 allSticky = getStickiesLocked(action, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 }
11031 } else {
Josh Bartel7f208742010-02-25 11:01:44 -060011032 allSticky = getStickiesLocked(null, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011033 }
11034
11035 // The first sticky in the list is returned directly back to
11036 // the client.
11037 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
11038
Joe Onorato8a9b2202010-02-26 18:56:32 -080011039 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011040 + ": " + sticky);
11041
11042 if (receiver == null) {
11043 return sticky;
11044 }
11045
11046 ReceiverList rl
11047 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
11048 if (rl == null) {
11049 rl = new ReceiverList(this, callerApp,
11050 Binder.getCallingPid(),
11051 Binder.getCallingUid(), receiver);
11052 if (rl.app != null) {
11053 rl.app.receivers.add(rl);
11054 } else {
11055 try {
11056 receiver.asBinder().linkToDeath(rl, 0);
11057 } catch (RemoteException e) {
11058 return sticky;
11059 }
11060 rl.linkedToDeath = true;
11061 }
11062 mRegisteredReceivers.put(receiver.asBinder(), rl);
11063 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011064 BroadcastFilter bf = new BroadcastFilter(filter, rl, callerPackage,
11065 permission, callingUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 rl.add(bf);
11067 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011068 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011069 }
11070 mReceiverResolver.addFilter(bf);
11071
11072 // Enqueue broadcasts for all existing stickies that match
11073 // this filter.
11074 if (allSticky != null) {
11075 ArrayList receivers = new ArrayList();
11076 receivers.add(bf);
11077
11078 int N = allSticky.size();
11079 for (int i=0; i<N; i++) {
11080 Intent intent = (Intent)allSticky.get(i);
Christopher Tatef46723b2012-01-26 14:19:24 -080011081 BroadcastQueue queue = broadcastQueueForIntent(intent);
11082 BroadcastRecord r = new BroadcastRecord(queue, intent, null,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011083 null, -1, -1, null, receivers, null, 0, null, null,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011084 false, true, true, -1);
Christopher Tatef46723b2012-01-26 14:19:24 -080011085 queue.enqueueParallelBroadcastLocked(r);
11086 queue.scheduleBroadcastsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011087 }
11088 }
11089
11090 return sticky;
11091 }
11092 }
11093
11094 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011095 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096
Christopher Tatef46723b2012-01-26 14:19:24 -080011097 final long origId = Binder.clearCallingIdentity();
11098 try {
11099 boolean doTrim = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011100
Christopher Tatef46723b2012-01-26 14:19:24 -080011101 synchronized(this) {
11102 ReceiverList rl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011103 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
Christopher Tatef46723b2012-01-26 14:19:24 -080011104 if (rl != null) {
11105 if (rl.curBroadcast != null) {
11106 BroadcastRecord r = rl.curBroadcast;
11107 final boolean doNext = finishReceiverLocked(
11108 receiver.asBinder(), r.resultCode, r.resultData,
11109 r.resultExtras, r.resultAbort, true);
11110 if (doNext) {
11111 doTrim = true;
11112 r.queue.processNextBroadcast(false);
11113 }
11114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115
Christopher Tatef46723b2012-01-26 14:19:24 -080011116 if (rl.app != null) {
11117 rl.app.receivers.remove(rl);
11118 }
11119 removeReceiverLocked(rl);
11120 if (rl.linkedToDeath) {
11121 rl.linkedToDeath = false;
11122 rl.receiver.asBinder().unlinkToDeath(rl, 0);
11123 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011124 }
11125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126
Christopher Tatef46723b2012-01-26 14:19:24 -080011127 // If we actually concluded any broadcasts, we might now be able
11128 // to trim the recipients' apps from our working set
11129 if (doTrim) {
11130 trimApplications();
11131 return;
11132 }
11133
11134 } finally {
11135 Binder.restoreCallingIdentity(origId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 }
11138
11139 void removeReceiverLocked(ReceiverList rl) {
11140 mRegisteredReceivers.remove(rl.receiver.asBinder());
11141 int N = rl.size();
11142 for (int i=0; i<N; i++) {
11143 mReceiverResolver.removeFilter(rl.get(i));
11144 }
11145 }
11146
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011147 private final void sendPackageBroadcastLocked(int cmd, String[] packages) {
11148 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
11149 ProcessRecord r = mLruProcesses.get(i);
11150 if (r.thread != null) {
11151 try {
11152 r.thread.dispatchPackageBroadcast(cmd, packages);
11153 } catch (RemoteException ex) {
11154 }
11155 }
11156 }
11157 }
11158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011159 private final int broadcastIntentLocked(ProcessRecord callerApp,
11160 String callerPackage, Intent intent, String resolvedType,
11161 IIntentReceiver resultTo, int resultCode, String resultData,
11162 Bundle map, String requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011163 boolean ordered, boolean sticky, int callingPid, int callingUid,
11164 int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011165 intent = new Intent(intent);
11166
Dianne Hackborne7f97212011-02-24 14:40:20 -080011167 // By default broadcasts do not go to stopped apps.
11168 intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
11169
Joe Onorato8a9b2202010-02-26 18:56:32 -080011170 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011171 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
Amith Yamasani13593602012-03-22 16:16:17 -070011172 + " ordered=" + ordered + " userid=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011173 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011174 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011175 }
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011176
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011177 // If the caller is trying to send this broadcast to a different
11178 // user, verify that is allowed.
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011179 if (UserHandle.getUserId(callingUid) != userId) {
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011180 if (checkComponentPermission(
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011181 android.Manifest.permission.INTERACT_ACROSS_USERS,
11182 callingPid, callingUid, -1, true) != PackageManager.PERMISSION_GRANTED
11183 && checkComponentPermission(
11184 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
11185 callingPid, callingUid, -1, true)
11186 != PackageManager.PERMISSION_GRANTED) {
11187 String msg = "Permission Denial: " + intent.getAction()
11188 + " broadcast from " + callerPackage
11189 + " asks to send as user " + userId
11190 + " but is calling from user " + UserHandle.getUserId(callingUid)
11191 + "; this requires "
11192 + android.Manifest.permission.INTERACT_ACROSS_USERS;
11193 Slog.w(TAG, msg);
11194 throw new SecurityException(msg);
11195 } else {
11196 if (userId == UserHandle.USER_CURRENT) {
11197 userId = mCurrentUserId;
Dianne Hackbornb4163a62012-08-02 18:31:26 -070011198 }
11199 }
11200 }
11201
Dianne Hackborn80a4af22012-08-27 19:18:31 -070011202 // Make sure that the user who is receiving this broadcast is started
11203 // If not, we will just skip it.
11204 if (mStartedUsers.get(userId) == null) {
11205 Slog.w(TAG, "Skipping broadcast of " + intent
11206 + ": user " + userId + " is stopped");
11207 return ActivityManager.BROADCAST_SUCCESS;
11208 }
11209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 // Handle special intents: if this broadcast is from the package
11211 // manager about a package being removed, we need to remove all of
11212 // its activities from the history stack.
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011213 final boolean uidRemoved = Intent.ACTION_UID_REMOVED.equals(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011214 intent.getAction());
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011215 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
11216 || Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011217 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 || uidRemoved) {
11219 if (checkComponentPermission(
11220 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
Dianne Hackborn6c2c5fc2011-01-18 17:02:33 -080011221 callingPid, callingUid, -1, true)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011222 == PackageManager.PERMISSION_GRANTED) {
11223 if (uidRemoved) {
11224 final Bundle intentExtras = intent.getExtras();
11225 final int uid = intentExtras != null
11226 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
11227 if (uid >= 0) {
11228 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
11229 synchronized (bs) {
11230 bs.removeUidStatsLocked(uid);
11231 }
11232 }
11233 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011234 // If resources are unvailble just force stop all
11235 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080011236 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011237 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
11238 if (list != null && (list.length > 0)) {
11239 for (String pkg : list) {
Amith Yamasani483f3b02012-03-13 16:08:00 -070011240 forceStopPackageLocked(pkg, -1, false, true, true, false, userId);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011241 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011242 sendPackageBroadcastLocked(
11243 IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011244 }
11245 } else {
11246 Uri data = intent.getData();
11247 String ssp;
11248 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11249 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
11250 forceStopPackageLocked(ssp,
Amith Yamasani483f3b02012-03-13 16:08:00 -070011251 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true,
11252 false, userId);
Dianne Hackbornde7faf62009-06-30 13:27:30 -070011253 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011254 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070011255 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
11256 new String[] {ssp});
11257 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 }
11259 }
11260 }
11261 } else {
11262 String msg = "Permission Denial: " + intent.getAction()
11263 + " broadcast from " + callerPackage + " (pid=" + callingPid
11264 + ", uid=" + callingUid + ")"
11265 + " requires "
11266 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011267 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011268 throw new SecurityException(msg);
11269 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011270
11271 // Special case for adding a package: by default turn on compatibility
11272 // mode.
11273 } else if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
Dianne Hackborn8ea5e1d2011-05-27 16:45:31 -070011274 Uri data = intent.getData();
11275 String ssp;
11276 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
11277 mCompatModePackages.handlePackageAddedLocked(ssp,
11278 intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011279 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011280 }
11281
11282 /*
11283 * If this is the time zone changed action, queue up a message that will reset the timezone
11284 * of all currently running processes. This message will get queued up before the broadcast
11285 * happens.
11286 */
11287 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
11288 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
11289 }
11290
Robert Greenwalt03595d02010-11-02 14:08:23 -070011291 if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {
11292 mHandler.sendEmptyMessage(CLEAR_DNS_CACHE);
11293 }
11294
Robert Greenwalt434203a2010-10-11 16:00:27 -070011295 if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {
11296 ProxyProperties proxy = intent.getParcelableExtra("proxy");
11297 mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy));
11298 }
11299
Dianne Hackborn854060af2009-07-09 18:14:31 -070011300 /*
11301 * Prevent non-system code (defined here to be non-persistent
11302 * processes) from sending protected broadcasts.
11303 */
11304 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
fredc0f420372012-04-12 00:02:00 -070011305 || callingUid == Process.SHELL_UID || callingUid == Process.BLUETOOTH_UID ||
11306 callingUid == 0) {
Dianne Hackborn854060af2009-07-09 18:14:31 -070011307 // Always okay.
11308 } else if (callerApp == null || !callerApp.persistent) {
11309 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011310 if (AppGlobals.getPackageManager().isProtectedBroadcast(
Dianne Hackborn854060af2009-07-09 18:14:31 -070011311 intent.getAction())) {
11312 String msg = "Permission Denial: not allowed to send broadcast "
11313 + intent.getAction() + " from pid="
11314 + callingPid + ", uid=" + callingUid;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011315 Slog.w(TAG, msg);
Dianne Hackborn854060af2009-07-09 18:14:31 -070011316 throw new SecurityException(msg);
11317 }
11318 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011319 Slog.w(TAG, "Remote exception", e);
Dianne Hackborna4972e92012-03-14 10:38:05 -070011320 return ActivityManager.BROADCAST_SUCCESS;
Dianne Hackborn854060af2009-07-09 18:14:31 -070011321 }
11322 }
11323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011324 // Add to the sticky list if requested.
11325 if (sticky) {
11326 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
11327 callingPid, callingUid)
11328 != PackageManager.PERMISSION_GRANTED) {
11329 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
11330 + callingPid + ", uid=" + callingUid
11331 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011332 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011333 throw new SecurityException(msg);
11334 }
11335 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011336 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 + " and enforce permission " + requiredPermission);
Dianne Hackborna4972e92012-03-14 10:38:05 -070011338 return ActivityManager.BROADCAST_STICKY_CANT_HAVE_PERMISSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011339 }
11340 if (intent.getComponent() != null) {
11341 throw new SecurityException(
11342 "Sticky broadcasts can't target a specific component");
11343 }
11344 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
11345 if (list == null) {
11346 list = new ArrayList<Intent>();
11347 mStickyBroadcasts.put(intent.getAction(), list);
11348 }
11349 int N = list.size();
11350 int i;
11351 for (i=0; i<N; i++) {
11352 if (intent.filterEquals(list.get(i))) {
11353 // This sticky already exists, replace it.
11354 list.set(i, new Intent(intent));
11355 break;
11356 }
11357 }
11358 if (i >= N) {
11359 list.add(new Intent(intent));
11360 }
11361 }
11362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011363 // Figure out who all will receive this broadcast.
11364 List receivers = null;
11365 List<BroadcastFilter> registeredReceivers = null;
11366 try {
Dianne Hackborn7d19e022012-08-07 19:12:33 -070011367 // Need to resolve the intent to interested receivers...
11368 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
11369 == 0) {
11370 receivers = AppGlobals.getPackageManager().queryIntentReceivers(
11371 intent, resolvedType, STOCK_PM_FLAGS, userId);
11372 }
11373 if (intent.getComponent() == null) {
11374 registeredReceivers = mReceiverResolver.queryIntent(intent,
11375 resolvedType, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 }
11377 } catch (RemoteException ex) {
11378 // pm is in same process, this will never happen.
11379 }
11380
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011381 final boolean replacePending =
11382 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
11383
Joe Onorato8a9b2202010-02-26 18:56:32 -080011384 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011385 + " replacePending=" + replacePending);
11386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
11388 if (!ordered && NR > 0) {
11389 // If we are not serializing this broadcast, then send the
11390 // registered receivers separately so they don't wait for the
11391 // components to be launched.
Christopher Tatef46723b2012-01-26 14:19:24 -080011392 final BroadcastQueue queue = broadcastQueueForIntent(intent);
11393 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 callerPackage, callingPid, callingUid, requiredPermission,
11395 registeredReceivers, resultTo, resultCode, resultData, map,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011396 ordered, sticky, false, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011397 if (DEBUG_BROADCAST) Slog.v(
Christopher Tatef46723b2012-01-26 14:19:24 -080011398 TAG, "Enqueueing parallel broadcast " + r);
11399 final boolean replaced = replacePending && queue.replaceParallelBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011400 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011401 queue.enqueueParallelBroadcastLocked(r);
11402 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011404 registeredReceivers = null;
11405 NR = 0;
11406 }
11407
11408 // Merge into one list.
11409 int ir = 0;
11410 if (receivers != null) {
11411 // A special case for PACKAGE_ADDED: do not allow the package
11412 // being added to see this broadcast. This prevents them from
11413 // using this as a back door to get run as soon as they are
11414 // installed. Maybe in the future we want to have a special install
11415 // broadcast or such for apps, but we'd like to deliberately make
11416 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011417 String skipPackages[] = null;
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011418 if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
11419 || Intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
11420 || Intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011421 Uri data = intent.getData();
11422 if (data != null) {
11423 String pkgName = data.getSchemeSpecificPart();
11424 if (pkgName != null) {
11425 skipPackages = new String[] { pkgName };
11426 }
11427 }
Dianne Hackborn0f1de9a2011-05-11 17:34:49 -070011428 } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011429 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -070011430 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080011431 if (skipPackages != null && (skipPackages.length > 0)) {
11432 for (String skipPackage : skipPackages) {
11433 if (skipPackage != null) {
11434 int NT = receivers.size();
11435 for (int it=0; it<NT; it++) {
11436 ResolveInfo curt = (ResolveInfo)receivers.get(it);
11437 if (curt.activityInfo.packageName.equals(skipPackage)) {
11438 receivers.remove(it);
11439 it--;
11440 NT--;
11441 }
11442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 }
11444 }
11445 }
11446
11447 int NT = receivers != null ? receivers.size() : 0;
11448 int it = 0;
11449 ResolveInfo curt = null;
11450 BroadcastFilter curr = null;
11451 while (it < NT && ir < NR) {
11452 if (curt == null) {
11453 curt = (ResolveInfo)receivers.get(it);
11454 }
11455 if (curr == null) {
11456 curr = registeredReceivers.get(ir);
11457 }
11458 if (curr.getPriority() >= curt.priority) {
11459 // Insert this broadcast record into the final list.
11460 receivers.add(it, curr);
11461 ir++;
11462 curr = null;
11463 it++;
11464 NT++;
11465 } else {
11466 // Skip to the next ResolveInfo in the final list.
11467 it++;
11468 curt = null;
11469 }
11470 }
11471 }
11472 while (ir < NR) {
11473 if (receivers == null) {
11474 receivers = new ArrayList();
11475 }
11476 receivers.add(registeredReceivers.get(ir));
11477 ir++;
11478 }
11479
11480 if ((receivers != null && receivers.size() > 0)
11481 || resultTo != null) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011482 BroadcastQueue queue = broadcastQueueForIntent(intent);
11483 BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011484 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -080011485 receivers, resultTo, resultCode, resultData, map, ordered,
Amith Yamasani8bf06ed2012-08-27 19:30:30 -070011486 sticky, false, userId);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011487 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011488 TAG, "Enqueueing ordered broadcast " + r
Christopher Tatef46723b2012-01-26 14:19:24 -080011489 + ": prev had " + queue.mOrderedBroadcasts.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 if (DEBUG_BROADCAST) {
11491 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011492 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 }
Christopher Tatef46723b2012-01-26 14:19:24 -080011494 boolean replaced = replacePending && queue.replaceOrderedBroadcastLocked(r);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011495 if (!replaced) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011496 queue.enqueueOrderedBroadcastLocked(r);
11497 queue.scheduleBroadcastsLocked();
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011499 }
11500
Dianne Hackborna4972e92012-03-14 10:38:05 -070011501 return ActivityManager.BROADCAST_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011502 }
11503
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011504 final Intent verifyBroadcastLocked(Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011505 // Refuse possible leaked file descriptors
11506 if (intent != null && intent.hasFileDescriptors() == true) {
11507 throw new IllegalArgumentException("File descriptors passed in Intent");
11508 }
11509
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011510 int flags = intent.getFlags();
11511
11512 if (!mProcessesReady) {
11513 // if the caller really truly claims to know what they're doing, go
11514 // ahead and allow the broadcast without launching any receivers
11515 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
11516 intent = new Intent(intent);
11517 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
11518 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
11519 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
11520 + " before boot completion");
11521 throw new IllegalStateException("Cannot broadcast before boot completed");
11522 }
11523 }
11524
11525 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
11526 throw new IllegalArgumentException(
11527 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
11528 }
11529
11530 return intent;
11531 }
11532
11533 public final int broadcastIntent(IApplicationThread caller,
11534 Intent intent, String resolvedType, IIntentReceiver resultTo,
11535 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011536 String requiredPermission, boolean serialized, boolean sticky, int userId) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011537 enforceNotIsolatedCaller("broadcastIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011539 intent = verifyBroadcastLocked(intent);
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011541 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11542 final int callingPid = Binder.getCallingPid();
11543 final int callingUid = Binder.getCallingUid();
11544 final long origId = Binder.clearCallingIdentity();
11545 int res = broadcastIntentLocked(callerApp,
11546 callerApp != null ? callerApp.info.packageName : null,
11547 intent, resolvedType, resultTo,
Amith Yamasani742a6712011-05-04 14:49:28 -070011548 resultCode, resultData, map, requiredPermission, serialized, sticky,
11549 callingPid, callingUid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 Binder.restoreCallingIdentity(origId);
11551 return res;
11552 }
11553 }
11554
11555 int broadcastIntentInPackage(String packageName, int uid,
11556 Intent intent, String resolvedType, IIntentReceiver resultTo,
11557 int resultCode, String resultData, Bundle map,
Amith Yamasani742a6712011-05-04 14:49:28 -070011558 String requiredPermission, boolean serialized, boolean sticky, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011560 intent = verifyBroadcastLocked(intent);
11561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011562 final long origId = Binder.clearCallingIdentity();
11563 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
11564 resultTo, resultCode, resultData, map, requiredPermission,
Amith Yamasani742a6712011-05-04 14:49:28 -070011565 serialized, sticky, -1, uid, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011566 Binder.restoreCallingIdentity(origId);
11567 return res;
11568 }
11569 }
11570
Amith Yamasani742a6712011-05-04 14:49:28 -070011571 // TODO: Use the userId; maybe mStickyBroadcasts need to be tied to the user.
11572 public final void unbroadcastIntent(IApplicationThread caller, Intent intent, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011573 // Refuse possible leaked file descriptors
11574 if (intent != null && intent.hasFileDescriptors() == true) {
11575 throw new IllegalArgumentException("File descriptors passed in Intent");
11576 }
11577
11578 synchronized(this) {
11579 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
11580 != PackageManager.PERMISSION_GRANTED) {
11581 String msg = "Permission Denial: unbroadcastIntent() from pid="
11582 + Binder.getCallingPid()
11583 + ", uid=" + Binder.getCallingUid()
11584 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011585 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011586 throw new SecurityException(msg);
11587 }
11588 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
11589 if (list != null) {
11590 int N = list.size();
11591 int i;
11592 for (i=0; i<N; i++) {
11593 if (intent.filterEquals(list.get(i))) {
11594 list.remove(i);
11595 break;
11596 }
11597 }
11598 }
11599 }
11600 }
11601
11602 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
11603 String resultData, Bundle resultExtras, boolean resultAbort,
11604 boolean explicit) {
Christopher Tatef46723b2012-01-26 14:19:24 -080011605 final BroadcastRecord r = broadcastRecordForReceiverLocked(receiver);
11606 if (r == null) {
11607 Slog.w(TAG, "finishReceiver called but not found on queue");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 return false;
11609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610
Christopher Tatef46723b2012-01-26 14:19:24 -080011611 return r.queue.finishReceiverLocked(r, resultCode, resultData, resultExtras, resultAbort,
11612 explicit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011613 }
11614
11615 public void finishReceiver(IBinder who, int resultCode, String resultData,
11616 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011617 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011618
11619 // Refuse possible leaked file descriptors
11620 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
11621 throw new IllegalArgumentException("File descriptors passed in Bundle");
11622 }
11623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011624 final long origId = Binder.clearCallingIdentity();
Christopher Tatef46723b2012-01-26 14:19:24 -080011625 try {
11626 boolean doNext = false;
11627 BroadcastRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011628
Christopher Tatef46723b2012-01-26 14:19:24 -080011629 synchronized(this) {
11630 r = broadcastRecordForReceiverLocked(who);
11631 if (r != null) {
11632 doNext = r.queue.finishReceiverLocked(r, resultCode,
11633 resultData, resultExtras, resultAbort, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011635 }
Jeff Brown4d94a762010-09-23 11:33:28 -070011636
Christopher Tatef46723b2012-01-26 14:19:24 -080011637 if (doNext) {
11638 r.queue.processNextBroadcast(false);
11639 }
11640 trimApplications();
11641 } finally {
11642 Binder.restoreCallingIdentity(origId);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011646 // =========================================================
11647 // INSTRUMENTATION
11648 // =========================================================
11649
11650 public boolean startInstrumentation(ComponentName className,
11651 String profileFile, int flags, Bundle arguments,
11652 IInstrumentationWatcher watcher) {
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011653 enforceNotIsolatedCaller("startInstrumentation");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011654 // Refuse possible leaked file descriptors
11655 if (arguments != null && arguments.hasFileDescriptors()) {
11656 throw new IllegalArgumentException("File descriptors passed in Bundle");
11657 }
11658
11659 synchronized(this) {
11660 InstrumentationInfo ii = null;
11661 ApplicationInfo ai = null;
11662 try {
11663 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070011664 className, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011665 ai = mContext.getPackageManager().getApplicationInfo(
Amith Yamasani483f3b02012-03-13 16:08:00 -070011666 ii.targetPackage, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011667 } catch (PackageManager.NameNotFoundException e) {
11668 }
11669 if (ii == null) {
11670 reportStartInstrumentationFailure(watcher, className,
11671 "Unable to find instrumentation info for: " + className);
11672 return false;
11673 }
11674 if (ai == null) {
11675 reportStartInstrumentationFailure(watcher, className,
11676 "Unable to find instrumentation target package: " + ii.targetPackage);
11677 return false;
11678 }
11679
11680 int match = mContext.getPackageManager().checkSignatures(
11681 ii.targetPackage, ii.packageName);
11682 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
11683 String msg = "Permission Denial: starting instrumentation "
11684 + className + " from pid="
11685 + Binder.getCallingPid()
11686 + ", uid=" + Binder.getCallingPid()
11687 + " not allowed because package " + ii.packageName
11688 + " does not have a signature matching the target "
11689 + ii.targetPackage;
11690 reportStartInstrumentationFailure(watcher, className, msg);
11691 throw new SecurityException(msg);
11692 }
11693
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011694 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 final long origId = Binder.clearCallingIdentity();
Christopher Tate3dacd842011-08-19 14:56:15 -070011696 // Instrumentation can kill and relaunch even persistent processes
Amith Yamasani483f3b02012-03-13 16:08:00 -070011697 forceStopPackageLocked(ii.targetPackage, -1, true, false, true, true, userId);
Dianne Hackborna0c283e2012-02-09 10:47:01 -080011698 ProcessRecord app = addAppLocked(ai, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011699 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011700 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011701 app.instrumentationProfileFile = profileFile;
11702 app.instrumentationArguments = arguments;
11703 app.instrumentationWatcher = watcher;
11704 app.instrumentationResultClass = className;
11705 Binder.restoreCallingIdentity(origId);
11706 }
11707
11708 return true;
11709 }
11710
11711 /**
11712 * Report errors that occur while attempting to start Instrumentation. Always writes the
11713 * error to the logs, but if somebody is watching, send the report there too. This enables
11714 * the "am" command to report errors with more information.
11715 *
11716 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
11717 * @param cn The component name of the instrumentation.
11718 * @param report The error report.
11719 */
11720 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
11721 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011722 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011723 try {
11724 if (watcher != null) {
11725 Bundle results = new Bundle();
11726 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
11727 results.putString("Error", report);
11728 watcher.instrumentationStatus(cn, -1, results);
11729 }
11730 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011731 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011732 }
11733 }
11734
11735 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
11736 if (app.instrumentationWatcher != null) {
11737 try {
11738 // NOTE: IInstrumentationWatcher *must* be oneway here
11739 app.instrumentationWatcher.instrumentationFinished(
11740 app.instrumentationClass,
11741 resultCode,
11742 results);
11743 } catch (RemoteException e) {
11744 }
11745 }
11746 app.instrumentationWatcher = null;
11747 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011748 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011749 app.instrumentationProfileFile = null;
11750 app.instrumentationArguments = null;
11751
Dianne Hackborn80a4af22012-08-27 19:18:31 -070011752 forceStopPackageLocked(app.info.packageName, -1, false, false, true, true, app.userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 }
11754
11755 public void finishInstrumentation(IApplicationThread target,
11756 int resultCode, Bundle results) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070011757 int userId = UserHandle.getCallingUserId();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 // Refuse possible leaked file descriptors
11759 if (results != null && results.hasFileDescriptors()) {
11760 throw new IllegalArgumentException("File descriptors passed in Intent");
11761 }
11762
11763 synchronized(this) {
11764 ProcessRecord app = getRecordForAppLocked(target);
11765 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011766 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011767 return;
11768 }
11769 final long origId = Binder.clearCallingIdentity();
11770 finishInstrumentationLocked(app, resultCode, results);
11771 Binder.restoreCallingIdentity(origId);
11772 }
11773 }
11774
11775 // =========================================================
11776 // CONFIGURATION
11777 // =========================================================
11778
11779 public ConfigurationInfo getDeviceConfigurationInfo() {
11780 ConfigurationInfo config = new ConfigurationInfo();
11781 synchronized (this) {
11782 config.reqTouchScreen = mConfiguration.touchscreen;
11783 config.reqKeyboardType = mConfiguration.keyboard;
11784 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011785 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
11786 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011787 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
11788 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011789 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
11790 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011791 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
11792 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070011793 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011794 }
11795 return config;
11796 }
11797
11798 public Configuration getConfiguration() {
11799 Configuration ci;
11800 synchronized(this) {
11801 ci = new Configuration(mConfiguration);
11802 }
11803 return ci;
11804 }
11805
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011806 public void updatePersistentConfiguration(Configuration values) {
11807 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
11808 "updateConfiguration()");
11809 enforceCallingPermission(android.Manifest.permission.WRITE_SETTINGS,
11810 "updateConfiguration()");
11811 if (values == null) {
11812 throw new NullPointerException("Configuration must not be null");
11813 }
11814
11815 synchronized(this) {
11816 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn813075a62011-11-14 17:45:19 -080011817 updateConfigurationLocked(values, null, true, false);
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011818 Binder.restoreCallingIdentity(origId);
11819 }
11820 }
11821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011822 public void updateConfiguration(Configuration values) {
11823 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
11824 "updateConfiguration()");
11825
11826 synchronized(this) {
11827 if (values == null && mWindowManager != null) {
11828 // sentinel: fetch the current configuration from the window manager
11829 values = mWindowManager.computeNewConfiguration();
11830 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070011831
11832 if (mWindowManager != null) {
11833 mProcessList.applyDisplaySize(mWindowManager);
11834 }
11835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011836 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011837 if (values != null) {
11838 Settings.System.clearConfiguration(values);
11839 }
Dianne Hackborn813075a62011-11-14 17:45:19 -080011840 updateConfigurationLocked(values, null, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 Binder.restoreCallingIdentity(origId);
11842 }
11843 }
11844
11845 /**
11846 * Do either or both things: (1) change the current configuration, and (2)
11847 * make sure the given activity is running with the (now) current
11848 * configuration. Returns true if the activity has been left running, or
11849 * false if <var>starting</var> is being destroyed to match the new
11850 * configuration.
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011851 * @param persistent TODO
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 */
Dianne Hackborna573f6a2012-02-09 16:12:18 -080011853 boolean updateConfigurationLocked(Configuration values,
Dianne Hackborn813075a62011-11-14 17:45:19 -080011854 ActivityRecord starting, boolean persistent, boolean initLocale) {
Mike Lockwood3a74bd32011-08-12 13:55:22 -070011855 // do nothing if we are headless
11856 if (mHeadless) return true;
11857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011858 int changes = 0;
11859
11860 boolean kept = true;
11861
11862 if (values != null) {
11863 Configuration newConfig = new Configuration(mConfiguration);
11864 changes = newConfig.updateFrom(values);
11865 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011866 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011867 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 }
11869
Doug Zongker2bec3d42009-12-04 12:52:44 -080011870 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011871
Dianne Hackborn813075a62011-11-14 17:45:19 -080011872 if (values.locale != null && !initLocale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 saveLocaleLocked(values.locale,
11874 !values.locale.equals(mConfiguration.locale),
11875 values.userSetLocale);
11876 }
11877
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011878 mConfigurationSeq++;
11879 if (mConfigurationSeq <= 0) {
11880 mConfigurationSeq = 1;
11881 }
11882 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011883 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011884 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011885
11886 final Configuration configCopy = new Configuration(mConfiguration);
Joe Onorato54a4a412011-11-02 20:50:08 -070011887
11888 // TODO: If our config changes, should we auto dismiss any currently
11889 // showing dialogs?
11890 mShowDialogs = shouldShowDialogs(newConfig);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011891
Dianne Hackborn826d17c2009-11-12 12:55:51 -080011892 AttributeCache ac = AttributeCache.instance();
11893 if (ac != null) {
Dianne Hackborn813075a62011-11-14 17:45:19 -080011894 ac.updateConfiguration(configCopy);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080011895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011896
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070011897 // Make sure all resources in our process are updated
11898 // right now, so that anyone who is going to retrieve
11899 // resource values after we return will be sure to get
11900 // the new ones. This is especially important during
11901 // boot, where the first config change needs to guarantee
11902 // all resources have that config before following boot
11903 // code is executed.
Dianne Hackborn813075a62011-11-14 17:45:19 -080011904 mSystemThread.applyConfigurationToResources(configCopy);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -070011905
Dianne Hackborn31ca8542011-07-19 14:58:28 -070011906 if (persistent && Settings.System.hasInterestingConfigurationChanges(changes)) {
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011907 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011908 msg.obj = new Configuration(configCopy);
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011909 mHandler.sendMessage(msg);
11910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011911
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011912 for (int i=mLruProcesses.size()-1; i>=0; i--) {
11913 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011914 try {
11915 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011916 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011917 + app.processName + " new config " + mConfiguration);
Dianne Hackborn813075a62011-11-14 17:45:19 -080011918 app.thread.scheduleConfigurationChanged(configCopy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011919 }
11920 } catch (Exception e) {
11921 }
11922 }
11923 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011924 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
11925 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011926 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -070011927 null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080011928 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
11929 broadcastIntentLocked(null, null,
11930 new Intent(Intent.ACTION_LOCALE_CHANGED),
11931 null, null, 0, null, null,
Amith Yamasani742a6712011-05-04 14:49:28 -070011932 null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080011933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 }
11935 }
11936
11937 if (changes != 0 && starting == null) {
11938 // If the configuration changed, and the caller is not already
11939 // in the process of starting an activity, then find the top
11940 // activity to check if its configuration needs to change.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011941 starting = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011942 }
11943
11944 if (starting != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011945 kept = mMainStack.ensureActivityConfigurationLocked(starting, changes);
Dianne Hackborn5f4d6432010-12-21 20:40:11 -080011946 // And we need to make sure at this point that all other activities
11947 // are made visible with the correct configuration.
11948 mMainStack.ensureActivitiesVisibleLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011949 }
11950
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011951 if (values != null && mWindowManager != null) {
11952 mWindowManager.setNewConfiguration(mConfiguration);
11953 }
11954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011955 return kept;
11956 }
Joe Onorato54a4a412011-11-02 20:50:08 -070011957
11958 /**
11959 * Decide based on the configuration whether we should shouw the ANR,
11960 * crash, etc dialogs. The idea is that if there is no affordnace to
11961 * press the on-screen buttons, we shouldn't show the dialog.
11962 *
11963 * A thought: SystemUI might also want to get told about this, the Power
11964 * dialog / global actions also might want different behaviors.
11965 */
11966 private static final boolean shouldShowDialogs(Configuration config) {
11967 return !(config.keyboard == Configuration.KEYBOARD_NOKEYS
11968 && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH);
11969 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970
11971 /**
11972 * Save the locale. You must be inside a synchronized (this) block.
11973 */
11974 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
11975 if(isDiff) {
11976 SystemProperties.set("user.language", l.getLanguage());
11977 SystemProperties.set("user.region", l.getCountry());
11978 }
11979
11980 if(isPersist) {
11981 SystemProperties.set("persist.sys.language", l.getLanguage());
11982 SystemProperties.set("persist.sys.country", l.getCountry());
11983 SystemProperties.set("persist.sys.localevar", l.getVariant());
11984 }
11985 }
11986
Adam Powelldd8fab22012-03-22 17:47:27 -070011987 @Override
11988 public boolean targetTaskAffinityMatchesActivity(IBinder token, String destAffinity) {
11989 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070011990 return srec != null && srec.task.affinity != null &&
11991 srec.task.affinity.equals(destAffinity);
Adam Powelldd8fab22012-03-22 17:47:27 -070011992 }
11993
11994 public boolean navigateUpTo(IBinder token, Intent destIntent, int resultCode,
11995 Intent resultData) {
11996 ComponentName dest = destIntent.getComponent();
11997
11998 synchronized (this) {
11999 ActivityRecord srec = ActivityRecord.forToken(token);
Adam Powellb71a5bc2012-04-24 14:20:57 -070012000 if (srec == null) {
12001 return false;
12002 }
Adam Powelldd8fab22012-03-22 17:47:27 -070012003 ArrayList<ActivityRecord> history = srec.stack.mHistory;
12004 final int start = history.indexOf(srec);
12005 if (start < 0) {
12006 // Current activity is not in history stack; do nothing.
12007 return false;
12008 }
12009 int finishTo = start - 1;
12010 ActivityRecord parent = null;
12011 boolean foundParentInTask = false;
12012 if (dest != null) {
12013 TaskRecord tr = srec.task;
12014 for (int i = start - 1; i >= 0; i--) {
12015 ActivityRecord r = history.get(i);
12016 if (tr != r.task) {
12017 // Couldn't find parent in the same task; stop at the one above this.
12018 // (Root of current task; in-app "home" behavior)
12019 // Always at least finish the current activity.
12020 finishTo = Math.min(start - 1, i + 1);
12021 parent = history.get(finishTo);
12022 break;
12023 } else if (r.info.packageName.equals(dest.getPackageName()) &&
12024 r.info.name.equals(dest.getClassName())) {
12025 finishTo = i;
12026 parent = r;
12027 foundParentInTask = true;
12028 break;
12029 }
12030 }
12031 }
12032
12033 if (mController != null) {
12034 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
12035 if (next != null) {
12036 // ask watcher if this is allowed
12037 boolean resumeOK = true;
12038 try {
12039 resumeOK = mController.activityResuming(next.packageName);
12040 } catch (RemoteException e) {
12041 mController = null;
12042 }
12043
12044 if (!resumeOK) {
12045 return false;
12046 }
12047 }
12048 }
12049 final long origId = Binder.clearCallingIdentity();
12050 for (int i = start; i > finishTo; i--) {
12051 ActivityRecord r = history.get(i);
12052 mMainStack.requestFinishActivityLocked(r.appToken, resultCode, resultData,
12053 "navigate-up");
12054 // Only return the supplied result for the first activity finished
12055 resultCode = Activity.RESULT_CANCELED;
12056 resultData = null;
12057 }
12058
12059 if (parent != null && foundParentInTask) {
12060 final int parentLaunchMode = parent.info.launchMode;
12061 final int destIntentFlags = destIntent.getFlags();
12062 if (parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE ||
12063 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TASK ||
12064 parentLaunchMode == ActivityInfo.LAUNCH_SINGLE_TOP ||
12065 (destIntentFlags & Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
Adam Powell69de7e12012-05-07 18:42:24 -070012066 parent.deliverNewIntentLocked(srec.info.applicationInfo.uid, destIntent);
Adam Powelldd8fab22012-03-22 17:47:27 -070012067 } else {
12068 try {
12069 ActivityInfo aInfo = AppGlobals.getPackageManager().getActivityInfo(
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070012070 destIntent.getComponent(), 0, UserHandle.getCallingUserId());
Adam Powelldd8fab22012-03-22 17:47:27 -070012071 int res = mMainStack.startActivityLocked(srec.app.thread, destIntent,
12072 null, aInfo, parent.appToken, null,
12073 0, -1, parent.launchedFromUid, 0, null, true, null);
12074 foundParentInTask = res == ActivityManager.START_SUCCESS;
12075 } catch (RemoteException e) {
12076 foundParentInTask = false;
12077 }
12078 mMainStack.requestFinishActivityLocked(parent.appToken, resultCode,
12079 resultData, "navigate-up");
12080 }
12081 }
12082 Binder.restoreCallingIdentity(origId);
12083 return foundParentInTask;
12084 }
12085 }
12086
Dianne Hackborn5320eb82012-05-18 12:05:04 -070012087 public int getLaunchedFromUid(IBinder activityToken) {
12088 ActivityRecord srec = ActivityRecord.forToken(activityToken);
12089 if (srec == null) {
12090 return -1;
12091 }
12092 return srec.launchedFromUid;
12093 }
12094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012095 // =========================================================
12096 // LIFETIME MANAGEMENT
12097 // =========================================================
12098
Christopher Tatef46723b2012-01-26 14:19:24 -080012099 // Returns which broadcast queue the app is the current [or imminent] receiver
12100 // on, or 'null' if the app is not an active broadcast recipient.
12101 private BroadcastQueue isReceivingBroadcast(ProcessRecord app) {
12102 BroadcastRecord r = app.curReceiver;
12103 if (r != null) {
12104 return r.queue;
12105 }
12106
12107 // It's not the current receiver, but it might be starting up to become one
12108 synchronized (this) {
12109 for (BroadcastQueue queue : mBroadcastQueues) {
12110 r = queue.mPendingBroadcast;
12111 if (r != null && r.curApp == app) {
12112 // found it; report which queue it's in
12113 return queue;
12114 }
12115 }
12116 }
12117
12118 return null;
12119 }
12120
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012121 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012122 int emptyAdj, ProcessRecord TOP_APP, boolean recursed, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012124 // This adjustment has already been computed. If we are calling
12125 // from the top, we may have already computed our adjustment with
12126 // an earlier hidden adjustment that isn't really for us... if
12127 // so, use the new hidden adjustment.
12128 if (!recursed && app.hidden) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012129 app.curAdj = app.curRawAdj = app.nonStoppingAdj =
12130 app.hasActivities ? hiddenAdj : emptyAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012131 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012132 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012133 }
12134
12135 if (app.thread == null) {
12136 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012137 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012138 return (app.curAdj=app.curRawAdj=ProcessList.HIDDEN_APP_MAX_ADJ);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012139 }
12140
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012141 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
12142 app.adjSource = null;
12143 app.adjTarget = null;
12144 app.empty = false;
12145 app.hidden = false;
12146
12147 final int activitiesSize = app.activities.size();
12148
Dianne Hackborn7d608422011-08-07 16:24:18 -070012149 if (app.maxAdj <= ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012150 // The max adjustment doesn't allow this app to be anything
12151 // below foreground, so it is not worth doing work for it.
12152 app.adjType = "fixed";
12153 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012154 app.curRawAdj = app.nonStoppingAdj = app.maxAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012155 app.hasActivities = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012156 app.foregroundActivities = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012157 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012158 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012159 // System process can do UI, and when they do we want to have
12160 // them trim their memory after the user leaves the UI. To
12161 // facilitate this, here we need to determine whether or not it
12162 // is currently showing UI.
12163 app.systemNoUi = true;
12164 if (app == TOP_APP) {
12165 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012166 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012167 } else if (activitiesSize > 0) {
12168 for (int j = 0; j < activitiesSize; j++) {
12169 final ActivityRecord r = app.activities.get(j);
12170 if (r.visible) {
12171 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012172 }
12173 if (r.app == app) {
12174 app.hasActivities = true;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012175 }
12176 }
12177 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012178 return (app.curAdj=app.maxAdj);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012179 }
12180
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012181 app.keeping = false;
12182 app.systemNoUi = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012183 app.hasActivities = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184
The Android Open Source Project4df24232009-03-05 14:34:35 -080012185 // Determine the importance of the process, starting with most
12186 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012188 int schedGroup;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012189 boolean foregroundActivities = false;
12190 boolean interesting = false;
Christopher Tatef46723b2012-01-26 14:19:24 -080012191 BroadcastQueue queue;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012192 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193 // The last app on the list is the foreground app.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012194 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012195 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012196 app.adjType = "top-activity";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012197 foregroundActivities = true;
12198 interesting = true;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012199 app.hasActivities = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012200 } else if (app.instrumentationClass != null) {
12201 // Don't want to kill running instrumentation.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012202 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012203 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012204 app.adjType = "instrumentation";
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012205 interesting = true;
Christopher Tatef46723b2012-01-26 14:19:24 -080012206 } else if ((queue = isReceivingBroadcast(app)) != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012207 // An app that is currently receiving a broadcast also
Christopher Tatef46723b2012-01-26 14:19:24 -080012208 // counts as being in the foreground for OOM killer purposes.
12209 // It's placed in a sched group based on the nature of the
12210 // broadcast as reflected by which queue it's active in.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012211 adj = ProcessList.FOREGROUND_APP_ADJ;
Christopher Tatef46723b2012-01-26 14:19:24 -080012212 schedGroup = (queue == mFgBroadcastQueue)
12213 ? Process.THREAD_GROUP_DEFAULT : Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012214 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 } else if (app.executingServices.size() > 0) {
12216 // An app that is currently executing a service callback also
12217 // counts as being in the foreground.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012218 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012219 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012220 app.adjType = "exec-service";
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012221 } else {
12222 // Assume process is hidden (has activities); we will correct
12223 // later if this is not the case.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012224 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012225 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012226 app.hidden = true;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012227 app.adjType = "bg-activities";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012229
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012230 boolean hasStoppingActivities = false;
12231
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012232 // Examine all activities if not already foreground.
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012233 if (!foregroundActivities && activitiesSize > 0) {
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012234 for (int j = 0; j < activitiesSize; j++) {
12235 final ActivityRecord r = app.activities.get(j);
12236 if (r.visible) {
12237 // App has a visible activity; only upgrade adjustment.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012238 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12239 adj = ProcessList.VISIBLE_APP_ADJ;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012240 app.adjType = "visible";
12241 }
12242 schedGroup = Process.THREAD_GROUP_DEFAULT;
12243 app.hidden = false;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012244 app.hasActivities = true;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012245 foregroundActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012246 break;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012247 } else if (r.state == ActivityState.PAUSING || r.state == ActivityState.PAUSED) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012248 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12249 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012250 app.adjType = "pausing";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012251 }
Dianne Hackborn8bf0aa92011-11-29 13:54:43 -080012252 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012253 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012254 } else if (r.state == ActivityState.STOPPING) {
12255 // We will apply the actual adjustment later, because
12256 // we want to allow this process to immediately go through
12257 // any memory trimming that is in effect.
12258 app.hidden = false;
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012259 foregroundActivities = true;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012260 hasStoppingActivities = true;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012261 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012262 if (r.app == app) {
12263 app.hasActivities = true;
12264 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012265 }
12266 }
12267
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012268 if (adj == hiddenAdj && !app.hasActivities) {
12269 // Whoops, this process is completely empty as far as we know
12270 // at this point.
12271 adj = emptyAdj;
12272 app.empty = true;
12273 app.adjType = "bg-empty";
12274 }
12275
Dianne Hackborn7d608422011-08-07 16:24:18 -070012276 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012277 if (app.foregroundServices) {
12278 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012279 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012280 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012281 app.adjType = "foreground-service";
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012282 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012283 } else if (app.forcingToForeground != null) {
12284 // The user is aware of this app, so make it visible.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012285 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012286 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012287 app.adjType = "force-foreground";
12288 app.adjSource = app.forcingToForeground;
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012289 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012290 }
12291 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012292
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012293 if (app.foregroundServices) {
12294 interesting = true;
12295 }
12296
Dianne Hackborn7d608422011-08-07 16:24:18 -070012297 if (adj > ProcessList.HEAVY_WEIGHT_APP_ADJ && app == mHeavyWeightProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012298 // We don't want to kill the current heavy-weight process.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012299 adj = ProcessList.HEAVY_WEIGHT_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012300 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012301 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012302 app.adjType = "heavy";
12303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304
Dianne Hackborn7d608422011-08-07 16:24:18 -070012305 if (adj > ProcessList.HOME_APP_ADJ && app == mHomeProcess) {
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012306 // This process is hosting what we currently consider to be the
12307 // home app, so we don't want to let it go into the background.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012308 adj = ProcessList.HOME_APP_ADJ;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012309 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012310 app.hidden = false;
Dianne Hackborn83a6f452011-01-27 17:17:19 -080012311 app.adjType = "home";
12312 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012313
Dianne Hackbornf35fe232011-11-01 19:25:20 -070012314 if (adj > ProcessList.PREVIOUS_APP_ADJ && app == mPreviousProcess
12315 && app.activities.size() > 0) {
12316 // This was the previous process that showed UI to the user.
12317 // We want to try to keep it around more aggressively, to give
12318 // a good experience around switching between two apps.
12319 adj = ProcessList.PREVIOUS_APP_ADJ;
12320 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
12321 app.hidden = false;
12322 app.adjType = "previous";
12323 }
12324
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012325 if (false) Slog.i(TAG, "OOM " + app + ": initial adj=" + adj
12326 + " reason=" + app.adjType);
12327
The Android Open Source Project4df24232009-03-05 14:34:35 -080012328 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012329 // there are applications dependent on our services or providers, but
12330 // this gives us a baseline and makes sure we don't get into an
12331 // infinite recursion.
12332 app.adjSeq = mAdjSeq;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012333 app.curRawAdj = app.nonStoppingAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012334
Christopher Tate6fa95972009-06-05 18:43:55 -070012335 if (mBackupTarget != null && app == mBackupTarget.app) {
12336 // If possible we want to avoid killing apps while they're being backed up
Dianne Hackborn7d608422011-08-07 16:24:18 -070012337 if (adj > ProcessList.BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012338 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012339 adj = ProcessList.BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012340 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012341 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070012342 }
12343 }
12344
Dianne Hackborn7d608422011-08-07 16:24:18 -070012345 if (app.services.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012346 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012347 final long now = SystemClock.uptimeMillis();
12348 // This process is more important if the top activity is
12349 // bound to the service.
Dianne Hackborn860755f2010-06-03 18:47:52 -070012350 Iterator<ServiceRecord> jt = app.services.iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012351 while (jt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012352 ServiceRecord s = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012353 if (s.startRequested) {
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012354 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012355 // If this process has shown some UI, let it immediately
12356 // go to the LRU list because it may be pretty heavy with
12357 // UI stuff. We'll tag it with a label just to help
12358 // debug and understand what is going on.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012359 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012360 app.adjType = "started-bg-ui-services";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012361 }
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012362 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012363 if (now < (s.lastActivity + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012364 // This service has seen some activity within
12365 // recent memory, so we will keep its process ahead
12366 // of the background processes.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012367 if (adj > ProcessList.SERVICE_ADJ) {
12368 adj = ProcessList.SERVICE_ADJ;
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012369 app.adjType = "started-services";
12370 app.hidden = false;
12371 }
12372 }
12373 // If we have let the service slide into the background
12374 // state, still have some text describing what it is doing
12375 // even though the service no longer has an impact.
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012376 if (adj > ProcessList.SERVICE_ADJ) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -070012377 app.adjType = "started-bg-services";
12378 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012379 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012380 // Don't kill this process because it is doing work; it
12381 // has said it is doing work.
12382 app.keeping = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012383 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012384 if (s.connections.size() > 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012385 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012386 Iterator<ArrayList<ConnectionRecord>> kt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012387 = s.connections.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012388 while (kt.hasNext() && adj > ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012389 ArrayList<ConnectionRecord> clist = kt.next();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012390 for (int i=0; i<clist.size() && adj > ProcessList.FOREGROUND_APP_ADJ; i++) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012391 // XXX should compute this based on the max of
12392 // all connected clients.
12393 ConnectionRecord cr = clist.get(i);
12394 if (cr.binding.client == app) {
12395 // Binding to ourself is not interesting.
12396 continue;
12397 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012398 if ((cr.flags&Context.BIND_WAIVE_PRIORITY) == 0) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012399 ProcessRecord client = cr.binding.client;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012400 int clientAdj = adj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012401 int myHiddenAdj = hiddenAdj;
12402 if (myHiddenAdj > client.hiddenAdj) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012403 if (client.hiddenAdj >= ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012404 myHiddenAdj = client.hiddenAdj;
12405 } else {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012406 myHiddenAdj = ProcessList.VISIBLE_APP_ADJ;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012407 }
12408 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012409 int myEmptyAdj = emptyAdj;
12410 if (myEmptyAdj > client.emptyAdj) {
12411 if (client.emptyAdj >= ProcessList.VISIBLE_APP_ADJ) {
12412 myEmptyAdj = client.emptyAdj;
12413 } else {
12414 myEmptyAdj = ProcessList.VISIBLE_APP_ADJ;
12415 }
12416 }
12417 clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12418 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012419 String adjType = null;
12420 if ((cr.flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) {
12421 // Not doing bind OOM management, so treat
12422 // this guy more like a started service.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012423 if (app.hasShownUi && app != mHomeProcess) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012424 // If this process has shown some UI, let it immediately
12425 // go to the LRU list because it may be pretty heavy with
12426 // UI stuff. We'll tag it with a label just to help
12427 // debug and understand what is going on.
12428 if (adj > clientAdj) {
12429 adjType = "bound-bg-ui-services";
12430 }
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012431 app.hidden = false;
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012432 clientAdj = adj;
12433 } else {
Dianne Hackborn599db5c2012-08-03 19:28:48 -070012434 if (now >= (s.lastActivity
12435 + ActiveServices.MAX_SERVICE_INACTIVITY)) {
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012436 // This service has not seen activity within
12437 // recent memory, so allow it to drop to the
12438 // LRU list if there is no other reason to keep
12439 // it around. We'll also tag it with a label just
12440 // to help debug and undertand what is going on.
12441 if (adj > clientAdj) {
12442 adjType = "bound-bg-services";
12443 }
12444 clientAdj = adj;
12445 }
12446 }
12447 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012448 if (adj > clientAdj) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012449 // If this process has recently shown UI, and
12450 // the process that is binding to it is less
12451 // important than being visible, then we don't
12452 // care about the binding as much as we care
12453 // about letting this process get into the LRU
12454 // list to be killed and restarted if needed for
12455 // memory.
Dianne Hackborn98cfebc2011-10-18 13:17:33 -070012456 if (app.hasShownUi && app != mHomeProcess
12457 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012458 adjType = "bound-bg-ui-services";
12459 } else {
12460 if ((cr.flags&(Context.BIND_ABOVE_CLIENT
12461 |Context.BIND_IMPORTANT)) != 0) {
12462 adj = clientAdj;
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012463 } else if ((cr.flags&Context.BIND_NOT_VISIBLE) != 0
12464 && clientAdj < ProcessList.PERCEPTIBLE_APP_ADJ
12465 && adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12466 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12467 } else if (clientAdj > ProcessList.VISIBLE_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012468 adj = clientAdj;
12469 } else {
Dianne Hackborn2c84cfc2011-10-31 15:39:59 -070012470 app.pendingUiClean = true;
12471 if (adj > ProcessList.VISIBLE_APP_ADJ) {
12472 adj = ProcessList.VISIBLE_APP_ADJ;
12473 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012474 }
12475 if (!client.hidden) {
12476 app.hidden = false;
12477 }
12478 if (client.keeping) {
12479 app.keeping = true;
12480 }
12481 adjType = "service";
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012482 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070012483 }
12484 if (adjType != null) {
12485 app.adjType = adjType;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012486 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12487 .REASON_SERVICE_IN_USE;
12488 app.adjSource = cr.binding.client;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012489 app.adjSourceOom = clientAdj;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012490 app.adjTarget = s.name;
12491 }
12492 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12493 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12494 schedGroup = Process.THREAD_GROUP_DEFAULT;
12495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012496 }
12497 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012498 if ((cr.flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) {
12499 ActivityRecord a = cr.activity;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012500 if (a != null && adj > ProcessList.FOREGROUND_APP_ADJ &&
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012501 (a.visible || a.state == ActivityState.RESUMED
12502 || a.state == ActivityState.PAUSING)) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012503 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012504 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12505 schedGroup = Process.THREAD_GROUP_DEFAULT;
12506 }
12507 app.hidden = false;
12508 app.adjType = "service";
12509 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12510 .REASON_SERVICE_IN_USE;
12511 app.adjSource = a;
Dianne Hackborn905577f2011-09-07 18:31:28 -070012512 app.adjSourceOom = adj;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012513 app.adjTarget = s.name;
12514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 }
12517 }
12518 }
12519 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012520
Dianne Hackborn287952c2010-09-22 22:34:31 -070012521 // Finally, if this process has active services running in it, we
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012522 // would like to avoid killing it unless it would prevent the current
12523 // application from running. By default we put the process in
12524 // with the rest of the background processes; as we scan through
12525 // its services we may bump it up from there.
12526 if (adj > hiddenAdj) {
12527 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012528 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012529 app.adjType = "bg-services";
12530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 }
12532
Dianne Hackborn7d608422011-08-07 16:24:18 -070012533 if (app.pubProviders.size() != 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012534 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012535 Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator();
Dianne Hackborn7d608422011-08-07 16:24:18 -070012536 while (jt.hasNext() && (adj > ProcessList.FOREGROUND_APP_ADJ
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012537 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012538 ContentProviderRecord cpr = jt.next();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012539 for (int i = cpr.connections.size()-1;
12540 i >= 0 && (adj > ProcessList.FOREGROUND_APP_ADJ
12541 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE);
12542 i--) {
12543 ContentProviderConnection conn = cpr.connections.get(i);
12544 ProcessRecord client = conn.client;
12545 if (client == app) {
12546 // Being our own client is not interesting.
12547 continue;
12548 }
12549 int myHiddenAdj = hiddenAdj;
12550 if (myHiddenAdj > client.hiddenAdj) {
12551 if (client.hiddenAdj > ProcessList.FOREGROUND_APP_ADJ) {
12552 myHiddenAdj = client.hiddenAdj;
12553 } else {
12554 myHiddenAdj = ProcessList.FOREGROUND_APP_ADJ;
The Android Open Source Project10592532009-03-18 17:39:46 -070012555 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012556 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012557 int myEmptyAdj = emptyAdj;
12558 if (myEmptyAdj > client.emptyAdj) {
12559 if (client.emptyAdj > ProcessList.FOREGROUND_APP_ADJ) {
12560 myEmptyAdj = client.emptyAdj;
12561 } else {
12562 myEmptyAdj = ProcessList.FOREGROUND_APP_ADJ;
12563 }
12564 }
12565 int clientAdj = computeOomAdjLocked(client, myHiddenAdj,
12566 myEmptyAdj, TOP_APP, true, doingAll);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012567 if (adj > clientAdj) {
12568 if (app.hasShownUi && app != mHomeProcess
12569 && clientAdj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12570 app.adjType = "bg-ui-provider";
12571 } else {
12572 adj = clientAdj > ProcessList.FOREGROUND_APP_ADJ
12573 ? clientAdj : ProcessList.FOREGROUND_APP_ADJ;
12574 app.adjType = "provider";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012575 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012576 if (!client.hidden) {
12577 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012578 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012579 if (client.keeping) {
12580 app.keeping = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012581 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070012582 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12583 .REASON_PROVIDER_IN_USE;
12584 app.adjSource = client;
12585 app.adjSourceOom = clientAdj;
12586 app.adjTarget = cpr.name;
12587 }
12588 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12589 schedGroup = Process.THREAD_GROUP_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012590 }
12591 }
12592 // If the provider has external (non-framework) process
12593 // dependencies, ensure that its adjustment is at least
12594 // FOREGROUND_APP_ADJ.
Svetoslav Ganov25872aa2012-02-03 19:19:09 -080012595 if (cpr.hasExternalProcessHandles()) {
Dianne Hackborn7d608422011-08-07 16:24:18 -070012596 if (adj > ProcessList.FOREGROUND_APP_ADJ) {
12597 adj = ProcessList.FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012598 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012599 app.hidden = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012600 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012601 app.adjType = "provider";
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012602 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012603 }
12604 }
12605 }
12606 }
12607
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012608 if (adj == ProcessList.SERVICE_ADJ) {
12609 if (doingAll) {
12610 app.serviceb = mNewNumServiceProcs > (mNumServiceProcs/3);
12611 mNewNumServiceProcs++;
12612 }
12613 if (app.serviceb) {
12614 adj = ProcessList.SERVICE_B_ADJ;
12615 }
12616 } else {
12617 app.serviceb = false;
12618 }
12619
12620 app.nonStoppingAdj = adj;
12621
12622 if (hasStoppingActivities) {
12623 // Only upgrade adjustment.
12624 if (adj > ProcessList.PERCEPTIBLE_APP_ADJ) {
12625 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12626 app.adjType = "stopping";
12627 }
12628 }
12629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012630 app.curRawAdj = adj;
12631
Joe Onorato8a9b2202010-02-26 18:56:32 -080012632 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012633 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
12634 if (adj > app.maxAdj) {
12635 adj = app.maxAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070012636 if (app.maxAdj <= ProcessList.PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012637 schedGroup = Process.THREAD_GROUP_DEFAULT;
12638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 }
Dianne Hackborn7d608422011-08-07 16:24:18 -070012640 if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070012641 app.keeping = true;
12642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012644 if (app.hasAboveClient) {
12645 // If this process has bound to any services with BIND_ABOVE_CLIENT,
12646 // then we need to drop its adjustment to be lower than the service's
12647 // in order to honor the request. We want to drop it by one adjustment
12648 // level... but there is special meaning applied to various levels so
12649 // we will skip some of them.
Dianne Hackborn7d608422011-08-07 16:24:18 -070012650 if (adj < ProcessList.FOREGROUND_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012651 // System process will not get dropped, ever
Dianne Hackborn7d608422011-08-07 16:24:18 -070012652 } else if (adj < ProcessList.VISIBLE_APP_ADJ) {
12653 adj = ProcessList.VISIBLE_APP_ADJ;
12654 } else if (adj < ProcessList.PERCEPTIBLE_APP_ADJ) {
12655 adj = ProcessList.PERCEPTIBLE_APP_ADJ;
12656 } else if (adj < ProcessList.HIDDEN_APP_MIN_ADJ) {
12657 adj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070012658 } else if (adj < ProcessList.HIDDEN_APP_MAX_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012659 adj++;
12660 }
12661 }
12662
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012663 int importance = app.memImportance;
12664 if (importance == 0 || adj != app.curAdj || schedGroup != app.curSchedGroup) {
12665 app.curAdj = adj;
12666 app.curSchedGroup = schedGroup;
12667 if (!interesting) {
12668 // For this reporting, if there is not something explicitly
12669 // interesting in this process then we will push it to the
12670 // background importance.
12671 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12672 } else if (adj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
12673 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12674 } else if (adj >= ProcessList.SERVICE_B_ADJ) {
12675 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
12676 } else if (adj >= ProcessList.HOME_APP_ADJ) {
12677 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
12678 } else if (adj >= ProcessList.SERVICE_ADJ) {
12679 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
12680 } else if (adj >= ProcessList.HEAVY_WEIGHT_APP_ADJ) {
12681 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
12682 } else if (adj >= ProcessList.PERCEPTIBLE_APP_ADJ) {
12683 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
12684 } else if (adj >= ProcessList.VISIBLE_APP_ADJ) {
12685 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
12686 } else if (adj >= ProcessList.FOREGROUND_APP_ADJ) {
12687 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
12688 } else {
12689 importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERSISTENT;
12690 }
12691 }
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012692
Dianne Hackborna93c2c12012-05-31 15:29:36 -070012693 int changes = importance != app.memImportance ? ProcessChangeItem.CHANGE_IMPORTANCE : 0;
12694 if (foregroundActivities != app.foregroundActivities) {
12695 changes |= ProcessChangeItem.CHANGE_ACTIVITIES;
12696 }
12697 if (changes != 0) {
12698 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Changes in " + app + ": " + changes);
12699 app.memImportance = importance;
12700 app.foregroundActivities = foregroundActivities;
12701 int i = mPendingProcessChanges.size()-1;
12702 ProcessChangeItem item = null;
12703 while (i >= 0) {
12704 item = mPendingProcessChanges.get(i);
12705 if (item.pid == app.pid) {
12706 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Re-using existing item: " + item);
12707 break;
12708 }
12709 i--;
12710 }
12711 if (i < 0) {
12712 // No existing item in pending changes; need a new one.
12713 final int NA = mAvailProcessChanges.size();
12714 if (NA > 0) {
12715 item = mAvailProcessChanges.remove(NA-1);
12716 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Retreiving available item: " + item);
12717 } else {
12718 item = new ProcessChangeItem();
12719 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Allocating new item: " + item);
12720 }
12721 item.changes = 0;
12722 item.pid = app.pid;
12723 item.uid = app.info.uid;
12724 if (mPendingProcessChanges.size() == 0) {
12725 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG,
12726 "*** Enqueueing dispatch processes changed!");
12727 mHandler.obtainMessage(DISPATCH_PROCESSES_CHANGED).sendToTarget();
12728 }
12729 mPendingProcessChanges.add(item);
12730 }
12731 item.changes |= changes;
12732 item.importance = importance;
12733 item.foregroundActivities = foregroundActivities;
12734 if (DEBUG_PROCESS_OBSERVERS) Slog.i(TAG, "Item "
12735 + Integer.toHexString(System.identityHashCode(item))
12736 + " " + app.toShortString() + ": changes=" + item.changes
12737 + " importance=" + item.importance
12738 + " foreground=" + item.foregroundActivities
12739 + " type=" + app.adjType + " source=" + app.adjSource
12740 + " target=" + app.adjTarget);
Jeff Sharkeyd5cdd592011-05-03 20:27:17 -070012741 }
12742
Dianne Hackbornc68c9132011-07-29 01:25:18 -070012743 return app.curRawAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012744 }
12745
12746 /**
12747 * Ask a given process to GC right now.
12748 */
12749 final void performAppGcLocked(ProcessRecord app) {
12750 try {
12751 app.lastRequestedGc = SystemClock.uptimeMillis();
12752 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012753 if (app.reportLowMemory) {
12754 app.reportLowMemory = false;
12755 app.thread.scheduleLowMemory();
12756 } else {
12757 app.thread.processInBackground();
12758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012759 }
12760 } catch (Exception e) {
12761 // whatever.
12762 }
12763 }
12764
12765 /**
12766 * Returns true if things are idle enough to perform GCs.
12767 */
Josh Bartel7f208742010-02-25 11:01:44 -060012768 private final boolean canGcNowLocked() {
Christopher Tatef46723b2012-01-26 14:19:24 -080012769 boolean processingBroadcasts = false;
12770 for (BroadcastQueue q : mBroadcastQueues) {
12771 if (q.mParallelBroadcasts.size() != 0 || q.mOrderedBroadcasts.size() != 0) {
12772 processingBroadcasts = true;
12773 }
12774 }
12775 return !processingBroadcasts
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012776 && (mSleeping || (mMainStack.mResumedActivity != null &&
12777 mMainStack.mResumedActivity.idle));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012778 }
12779
12780 /**
12781 * Perform GCs on all processes that are waiting for it, but only
12782 * if things are idle.
12783 */
12784 final void performAppGcsLocked() {
12785 final int N = mProcessesToGc.size();
12786 if (N <= 0) {
12787 return;
12788 }
Josh Bartel7f208742010-02-25 11:01:44 -060012789 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012790 while (mProcessesToGc.size() > 0) {
12791 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackborn7d608422011-08-07 16:24:18 -070012792 if (proc.curRawAdj > ProcessList.PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012793 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
12794 <= SystemClock.uptimeMillis()) {
12795 // To avoid spamming the system, we will GC processes one
12796 // at a time, waiting a few seconds between each.
12797 performAppGcLocked(proc);
12798 scheduleAppGcsLocked();
12799 return;
12800 } else {
12801 // It hasn't been long enough since we last GCed this
12802 // process... put it in the list to wait for its time.
12803 addProcessToGcListLocked(proc);
12804 break;
12805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012806 }
12807 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012808
12809 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810 }
12811 }
12812
12813 /**
12814 * If all looks good, perform GCs on all processes waiting for them.
12815 */
12816 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060012817 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012818 performAppGcsLocked();
12819 return;
12820 }
12821 // Still not idle, wait some more.
12822 scheduleAppGcsLocked();
12823 }
12824
12825 /**
12826 * Schedule the execution of all pending app GCs.
12827 */
12828 final void scheduleAppGcsLocked() {
12829 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012830
12831 if (mProcessesToGc.size() > 0) {
12832 // Schedule a GC for the time to the next process.
12833 ProcessRecord proc = mProcessesToGc.get(0);
12834 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
12835
Dianne Hackbornce86ba82011-07-13 19:33:41 -070012836 long when = proc.lastRequestedGc + GC_MIN_INTERVAL;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012837 long now = SystemClock.uptimeMillis();
12838 if (when < (now+GC_TIMEOUT)) {
12839 when = now + GC_TIMEOUT;
12840 }
12841 mHandler.sendMessageAtTime(msg, when);
12842 }
12843 }
12844
12845 /**
12846 * Add a process to the array of processes waiting to be GCed. Keeps the
12847 * list in sorted order by the last GC time. The process can't already be
12848 * on the list.
12849 */
12850 final void addProcessToGcListLocked(ProcessRecord proc) {
12851 boolean added = false;
12852 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
12853 if (mProcessesToGc.get(i).lastRequestedGc <
12854 proc.lastRequestedGc) {
12855 added = true;
12856 mProcessesToGc.add(i+1, proc);
12857 break;
12858 }
12859 }
12860 if (!added) {
12861 mProcessesToGc.add(0, proc);
12862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863 }
12864
12865 /**
12866 * Set up to ask a process to GC itself. This will either do it
12867 * immediately, or put it on the list of processes to gc the next
12868 * time things are idle.
12869 */
12870 final void scheduleAppGcLocked(ProcessRecord app) {
12871 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012872 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 return;
12874 }
12875 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012876 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012877 scheduleAppGcsLocked();
12878 }
12879 }
12880
Dianne Hackborn287952c2010-09-22 22:34:31 -070012881 final void checkExcessivePowerUsageLocked(boolean doKills) {
12882 updateCpuStatsNow();
12883
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012884 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012885 boolean doWakeKills = doKills;
12886 boolean doCpuKills = doKills;
12887 if (mLastPowerCheckRealtime == 0) {
12888 doWakeKills = false;
12889 }
12890 if (mLastPowerCheckUptime == 0) {
12891 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012892 }
12893 if (stats.isScreenOn()) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070012894 doWakeKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012895 }
12896 final long curRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012897 final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
12898 final long curUptime = SystemClock.uptimeMillis();
12899 final long uptimeSince = curUptime - mLastPowerCheckUptime;
12900 mLastPowerCheckRealtime = curRealtime;
12901 mLastPowerCheckUptime = curUptime;
12902 if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
12903 doWakeKills = false;
12904 }
12905 if (uptimeSince < CPU_MIN_CHECK_DURATION) {
12906 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012907 }
12908 int i = mLruProcesses.size();
12909 while (i > 0) {
12910 i--;
12911 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012912 if (!app.keeping) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012913 long wtime;
12914 synchronized (stats) {
12915 wtime = stats.getProcessWakeTime(app.info.uid,
12916 app.pid, curRealtime);
12917 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012918 long wtimeUsed = wtime - app.lastWakeTime;
12919 long cputimeUsed = app.curCpuTime - app.lastCpuTime;
12920 if (DEBUG_POWER) {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012921 StringBuilder sb = new StringBuilder(128);
12922 sb.append("Wake for ");
12923 app.toShortString(sb);
12924 sb.append(": over ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012925 TimeUtils.formatDuration(realtimeSince, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012926 sb.append(" used ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012927 TimeUtils.formatDuration(wtimeUsed, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012928 sb.append(" (");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012929 sb.append((wtimeUsed*100)/realtimeSince);
12930 sb.append("%)");
12931 Slog.i(TAG, sb.toString());
12932 sb.setLength(0);
12933 sb.append("CPU for ");
12934 app.toShortString(sb);
12935 sb.append(": over ");
12936 TimeUtils.formatDuration(uptimeSince, sb);
12937 sb.append(" used ");
12938 TimeUtils.formatDuration(cputimeUsed, sb);
12939 sb.append(" (");
12940 sb.append((cputimeUsed*100)/uptimeSince);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012941 sb.append("%)");
12942 Slog.i(TAG, sb.toString());
12943 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012944 // If a process has held a wake lock for more
12945 // than 50% of the time during this period,
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070012946 // that sounds bad. Kill!
Dianne Hackborn287952c2010-09-22 22:34:31 -070012947 if (doWakeKills && realtimeSince > 0
12948 && ((wtimeUsed*100)/realtimeSince) >= 50) {
12949 synchronized (stats) {
12950 stats.reportExcessiveWakeLocked(app.info.uid, app.processName,
12951 realtimeSince, wtimeUsed);
12952 }
12953 Slog.w(TAG, "Excessive wake lock in " + app.processName
12954 + " (pid " + app.pid + "): held " + wtimeUsed
12955 + " during " + realtimeSince);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012956 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12957 app.processName, app.setAdj, "excessive wake lock");
12958 Process.killProcessQuiet(app.pid);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012959 } else if (doCpuKills && uptimeSince > 0
12960 && ((cputimeUsed*100)/uptimeSince) >= 50) {
12961 synchronized (stats) {
12962 stats.reportExcessiveCpuLocked(app.info.uid, app.processName,
12963 uptimeSince, cputimeUsed);
12964 }
12965 Slog.w(TAG, "Excessive CPU in " + app.processName
12966 + " (pid " + app.pid + "): used " + cputimeUsed
12967 + " during " + uptimeSince);
12968 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12969 app.processName, app.setAdj, "excessive cpu");
12970 Process.killProcessQuiet(app.pid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012971 } else {
12972 app.lastWakeTime = wtime;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012973 app.lastCpuTime = app.curCpuTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012974 }
12975 }
12976 }
12977 }
12978
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012979 private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj,
12980 int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012981 app.hiddenAdj = hiddenAdj;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012982 app.emptyAdj = emptyAdj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012983
12984 if (app.thread == null) {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012985 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012986 }
12987
Dianne Hackborn287952c2010-09-22 22:34:31 -070012988 final boolean wasKeeping = app.keeping;
12989
Dianne Hackborn295e3c22011-08-25 13:19:08 -070012990 boolean success = true;
12991
Dianne Hackbornee7621c2012-08-13 16:42:18 -070012992 computeOomAdjLocked(app, hiddenAdj, emptyAdj, TOP_APP, false, doingAll);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012993
Jeff Brown10e89712011-07-08 18:52:57 -070012994 if (app.curRawAdj != app.setRawAdj) {
Jeff Brown10e89712011-07-08 18:52:57 -070012995 if (wasKeeping && !app.keeping) {
12996 // This app is no longer something we want to keep. Note
12997 // its current wake lock time to later know to kill it if
12998 // it is not behaving well.
12999 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
13000 synchronized (stats) {
13001 app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
13002 app.pid, SystemClock.elapsedRealtime());
13003 }
13004 app.lastCpuTime = app.curCpuTime;
13005 }
13006
13007 app.setRawAdj = app.curRawAdj;
13008 }
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013009
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013010 if (app.curAdj != app.setAdj) {
13011 if (Process.setOomAdj(app.pid, app.curAdj)) {
Dianne Hackbornbbb09ac2011-11-30 11:31:29 -080013012 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013013 TAG, "Set " + app.pid + " " + app.processName +
13014 " adj " + app.curAdj + ": " + app.adjType);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013015 app.setAdj = app.curAdj;
Jeff Brown10e89712011-07-08 18:52:57 -070013016 } else {
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013017 success = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013018 Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj);
Jeff Brown10e89712011-07-08 18:52:57 -070013019 }
13020 }
13021 if (app.setSchedGroup != app.curSchedGroup) {
13022 app.setSchedGroup = app.curSchedGroup;
13023 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
13024 "Setting process group of " + app.processName
13025 + " to " + app.curSchedGroup);
13026 if (app.waitingToKill != null &&
13027 app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) {
13028 Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill);
13029 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13030 app.processName, app.setAdj, app.waitingToKill);
Dianne Hackborn45a25bc2012-06-28 13:49:17 -070013031 app.killedBackground = true;
Jeff Brown10e89712011-07-08 18:52:57 -070013032 Process.killProcessQuiet(app.pid);
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013033 success = false;
Jeff Brown10e89712011-07-08 18:52:57 -070013034 } else {
13035 if (true) {
13036 long oldId = Binder.clearCallingIdentity();
13037 try {
13038 Process.setProcessGroup(app.pid, app.curSchedGroup);
13039 } catch (Exception e) {
13040 Slog.w(TAG, "Failed setting process group of " + app.pid
13041 + " to " + app.curSchedGroup);
13042 e.printStackTrace();
13043 } finally {
13044 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070013045 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013046 } else {
Jeff Brown10e89712011-07-08 18:52:57 -070013047 if (app.thread != null) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070013048 try {
Jeff Brown10e89712011-07-08 18:52:57 -070013049 app.thread.setSchedulingGroup(app.curSchedGroup);
13050 } catch (RemoteException e) {
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070013051 }
13052 }
13053 }
13054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013055 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013056 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013057 }
13058
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013059 private final ActivityRecord resumedAppLocked() {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013060 ActivityRecord resumedActivity = mMainStack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013061 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn621e2fe2012-02-16 17:07:33 -080013062 resumedActivity = mMainStack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013063 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013064 resumedActivity = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013065 }
13066 }
13067 return resumedActivity;
13068 }
13069
Dianne Hackborn599db5c2012-08-03 19:28:48 -070013070 final boolean updateOomAdjLocked(ProcessRecord app) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013071 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013072 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
13073 int curAdj = app.curAdj;
Dianne Hackborn7d608422011-08-07 16:24:18 -070013074 final boolean wasHidden = curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13075 && curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013076
13077 mAdjSeq++;
13078
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013079 boolean success = updateOomAdjLocked(app, app.hiddenAdj, app.emptyAdj,
13080 TOP_APP, false);
Dianne Hackborn7d608422011-08-07 16:24:18 -070013081 final boolean nowHidden = app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ
13082 && app.curAdj <= ProcessList.HIDDEN_APP_MAX_ADJ;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013083 if (nowHidden != wasHidden) {
13084 // Changed to/from hidden state, so apps after it in the LRU
13085 // list may also be changed.
13086 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 }
Dianne Hackborn295e3c22011-08-25 13:19:08 -070013088 return success;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 }
13090
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013091 final void updateOomAdjLocked() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070013092 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013093 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
13094
13095 if (false) {
13096 RuntimeException e = new RuntimeException();
13097 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080013098 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013099 }
13100
13101 mAdjSeq++;
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013102 mNewNumServiceProcs = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013103
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080013104 // Let's determine how many processes we have running vs.
13105 // how many slots we have for background processes; we may want
13106 // to put multiple processes in a slot of there are enough of
13107 // them.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013108 int numSlots = (ProcessList.HIDDEN_APP_MAX_ADJ
13109 - ProcessList.HIDDEN_APP_MIN_ADJ + 1) / 2;
13110 int emptyFactor = (mLruProcesses.size()-mNumNonHiddenProcs-mNumHiddenProcs)/numSlots;
13111 if (emptyFactor < 1) emptyFactor = 1;
13112 int hiddenFactor = (mNumHiddenProcs > 0 ? mNumHiddenProcs : 1)/numSlots;
13113 if (hiddenFactor < 1) hiddenFactor = 1;
13114 int stepHidden = 0;
13115 int stepEmpty = 0;
13116 final int emptyProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
13117 final int hiddenProcessLimit = mProcessLimit > 1 ? mProcessLimit / 2 : mProcessLimit;
Dianne Hackborn8633e682010-04-22 16:03:41 -070013118 int numHidden = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013119 int numEmpty = 0;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013120 int numTrimming = 0;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013121
13122 mNumNonHiddenProcs = 0;
13123 mNumHiddenProcs = 0;
13124
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013125 // First update the OOM adjustment for each of the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013126 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013127 int i = mLruProcesses.size();
Dianne Hackborn7d608422011-08-07 16:24:18 -070013128 int curHiddenAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013129 int nextHiddenAdj = curHiddenAdj+1;
13130 int curEmptyAdj = ProcessList.HIDDEN_APP_MIN_ADJ;
13131 int nextEmptyAdj = curEmptyAdj+2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013132 while (i > 0) {
13133 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013134 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080013135 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013136 updateOomAdjLocked(app, curHiddenAdj, curEmptyAdj, TOP_APP, true);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013137 if (!app.killedBackground) {
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013138 if (app.curRawAdj == curHiddenAdj && app.hasActivities) {
13139 // This process was assigned as a hidden process... step the
13140 // hidden level.
13141 mNumHiddenProcs++;
13142 if (curHiddenAdj != nextHiddenAdj) {
13143 stepHidden++;
13144 if (stepHidden >= hiddenFactor) {
13145 stepHidden = 0;
13146 curHiddenAdj = nextHiddenAdj;
13147 nextHiddenAdj += 2;
13148 if (nextHiddenAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13149 nextHiddenAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13150 }
13151 }
13152 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013153 numHidden++;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013154 if (numHidden > hiddenProcessLimit) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013155 Slog.i(TAG, "No longer want " + app.processName
13156 + " (pid " + app.pid + "): hidden #" + numHidden);
13157 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13158 app.processName, app.setAdj, "too many background");
13159 app.killedBackground = true;
13160 Process.killProcessQuiet(app.pid);
Dianne Hackborn8633e682010-04-22 16:03:41 -070013161 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013162 } else {
13163 if (app.curRawAdj == curEmptyAdj || app.curRawAdj == curHiddenAdj) {
13164 // This process was assigned as an empty process... step the
13165 // empty level.
13166 if (curEmptyAdj != nextEmptyAdj) {
13167 stepEmpty++;
13168 if (stepEmpty >= emptyFactor) {
13169 stepEmpty = 0;
13170 curEmptyAdj = nextEmptyAdj;
13171 nextEmptyAdj += 2;
13172 if (nextEmptyAdj > ProcessList.HIDDEN_APP_MAX_ADJ) {
13173 nextEmptyAdj = ProcessList.HIDDEN_APP_MAX_ADJ;
13174 }
13175 }
13176 }
13177 } else if (app.curRawAdj < ProcessList.HIDDEN_APP_MIN_ADJ) {
13178 mNumNonHiddenProcs++;
13179 }
13180 if (app.curAdj >= ProcessList.HIDDEN_APP_MIN_ADJ) {
13181 numEmpty++;
13182 if (numEmpty > emptyProcessLimit) {
13183 Slog.i(TAG, "No longer want " + app.processName
13184 + " (pid " + app.pid + "): empty #" + numEmpty);
13185 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13186 app.processName, app.setAdj, "too many background");
13187 app.killedBackground = true;
13188 Process.killProcessQuiet(app.pid);
13189 }
13190 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070013191 }
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013192 if (app.isolated && app.services.size() <= 0) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013193 // If this is an isolated process, and there are no
13194 // services running in it, then the process is no longer
13195 // needed. We agressively kill these because we can by
13196 // definition not re-use the same process again, and it is
13197 // good to avoid having whatever code was running in them
13198 // left sitting around after no longer needed.
13199 Slog.i(TAG, "Isolated process " + app.processName
13200 + " (pid " + app.pid + ") no longer needed");
13201 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13202 app.processName, app.setAdj, "isolated not needed");
13203 app.killedBackground = true;
13204 Process.killProcessQuiet(app.pid);
13205 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013206 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13207 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
13208 && !app.killedBackground) {
13209 numTrimming++;
13210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013211 }
13212 }
13213
Dianne Hackborne02c88a2011-10-28 13:58:15 -070013214 mNumServiceProcs = mNewNumServiceProcs;
13215
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013216 // Now determine the memory trimming level of background processes.
13217 // Unfortunately we need to start at the back of the list to do this
13218 // properly. We only do this if the number of background apps we
13219 // are managing to keep around is less than half the maximum we desire;
13220 // if we are keeping a good number around, we'll let them use whatever
13221 // memory they want.
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013222 if (numHidden <= (ProcessList.MAX_HIDDEN_APPS/4)
13223 && numEmpty <= (ProcessList.MAX_HIDDEN_APPS/4)) {
13224 final int numHiddenAndEmpty = numHidden + numEmpty;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013225 final int N = mLruProcesses.size();
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013226 int factor = numTrimming/3;
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013227 int minFactor = 2;
13228 if (mHomeProcess != null) minFactor++;
13229 if (mPreviousProcess != null) minFactor++;
13230 if (factor < minFactor) factor = minFactor;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013231 int step = 0;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013232 int fgTrimLevel;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013233 if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/5)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013234 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL;
Dianne Hackbornee7621c2012-08-13 16:42:18 -070013235 } else if (numHiddenAndEmpty <= (ProcessList.MAX_HIDDEN_APPS/3)) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013236 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW;
13237 } else {
13238 fgTrimLevel = ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE;
13239 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013240 int curLevel = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013241 for (i=0; i<N; i++) {
13242 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013243 if (app.nonStoppingAdj >= ProcessList.HOME_APP_ADJ
13244 && app.nonStoppingAdj != ProcessList.SERVICE_B_ADJ
Dianne Hackbornd8c98fe2011-11-15 11:29:38 -080013245 && !app.killedBackground) {
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013246 if (app.trimMemoryLevel < curLevel && app.thread != null) {
13247 try {
13248 app.thread.scheduleTrimMemory(curLevel);
13249 } catch (RemoteException e) {
13250 }
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013251 if (false) {
13252 // For now we won't do this; our memory trimming seems
13253 // to be good enough at this point that destroying
13254 // activities causes more harm than good.
13255 if (curLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
13256 && app != mHomeProcess && app != mPreviousProcess) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013257 // Need to do this on its own message because the stack may not
13258 // be in a consistent state at this point.
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013259 // For these apps we will also finish their activities
13260 // to help them free memory.
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013261 mMainStack.scheduleDestroyActivities(app, false, "trim");
Dianne Hackborn77eaaf02011-12-05 18:05:31 -080013262 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013263 }
13264 }
13265 app.trimMemoryLevel = curLevel;
13266 step++;
13267 if (step >= factor) {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013268 step = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013269 switch (curLevel) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013270 case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:
13271 curLevel = ComponentCallbacks2.TRIM_MEMORY_MODERATE;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013272 break;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013273 case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
13274 curLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013275 break;
13276 }
13277 }
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013278 } else if (app.nonStoppingAdj == ProcessList.HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013279 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013280 && app.thread != null) {
13281 try {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013282 app.thread.scheduleTrimMemory(
13283 ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013284 } catch (RemoteException e) {
13285 }
13286 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013287 app.trimMemoryLevel = ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013288 } else {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013289 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013290 && app.pendingUiClean) {
13291 // If this application is now in the background and it
13292 // had done UI, then give it the special trim level to
13293 // have it free UI resources.
13294 final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN;
13295 if (app.trimMemoryLevel < level && app.thread != null) {
13296 try {
13297 app.thread.scheduleTrimMemory(level);
13298 } catch (RemoteException e) {
13299 }
13300 }
13301 app.pendingUiClean = false;
13302 }
13303 if (app.trimMemoryLevel < fgTrimLevel && app.thread != null) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013304 try {
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013305 app.thread.scheduleTrimMemory(fgTrimLevel);
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013306 } catch (RemoteException e) {
13307 }
13308 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013309 app.trimMemoryLevel = fgTrimLevel;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013310 }
13311 }
13312 } else {
13313 final int N = mLruProcesses.size();
13314 for (i=0; i<N; i++) {
13315 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -070013316 if ((app.nonStoppingAdj > ProcessList.VISIBLE_APP_ADJ || app.systemNoUi)
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013317 && app.pendingUiClean) {
13318 if (app.trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
13319 && app.thread != null) {
13320 try {
13321 app.thread.scheduleTrimMemory(
13322 ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN);
13323 } catch (RemoteException e) {
13324 }
13325 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013326 app.pendingUiClean = false;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070013327 }
Dianne Hackborn27ff9132012-03-06 14:57:58 -080013328 app.trimMemoryLevel = 0;
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013329 }
13330 }
13331
13332 if (mAlwaysFinishActivities) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -070013333 // Need to do this on its own message because the stack may not
13334 // be in a consistent state at this point.
13335 mMainStack.scheduleDestroyActivities(null, false, "always-finish");
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013337 }
13338
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070013339 final void trimApplications() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013340 synchronized (this) {
13341 int i;
13342
13343 // First remove any unused application processes whose package
13344 // has been removed.
13345 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
13346 final ProcessRecord app = mRemovedProcesses.get(i);
13347 if (app.activities.size() == 0
13348 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013349 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013350 TAG, "Exiting empty application process "
13351 + app.processName + " ("
13352 + (app.thread != null ? app.thread.asBinder() : null)
13353 + ")\n");
13354 if (app.pid > 0 && app.pid != MY_PID) {
Dianne Hackborn0c5001d2011-04-12 18:16:08 -070013355 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
13356 app.processName, app.setAdj, "empty");
13357 Process.killProcessQuiet(app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013358 } else {
13359 try {
13360 app.thread.scheduleExit();
13361 } catch (Exception e) {
13362 // Ignore exceptions.
13363 }
13364 }
Dianne Hackborn130b0d22011-07-26 22:07:48 -070013365 cleanUpApplicationRecordLocked(app, false, true, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013366 mRemovedProcesses.remove(i);
13367
13368 if (app.persistent) {
13369 if (app.persistent) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -080013370 addAppLocked(app.info, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013371 }
13372 }
13373 }
13374 }
13375
Dianne Hackbornce86ba82011-07-13 19:33:41 -070013376 // Now update the oom adj for all processes.
13377 updateOomAdjLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013378 }
13379 }
13380
13381 /** This method sends the specified signal to each of the persistent apps */
13382 public void signalPersistentProcesses(int sig) throws RemoteException {
13383 if (sig != Process.SIGNAL_USR1) {
13384 throw new SecurityException("Only SIGNAL_USR1 is allowed");
13385 }
13386
13387 synchronized (this) {
13388 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
13389 != PackageManager.PERMISSION_GRANTED) {
13390 throw new SecurityException("Requires permission "
13391 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
13392 }
13393
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013394 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
13395 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013396 if (r.thread != null && r.persistent) {
13397 Process.sendSignal(r.pid, sig);
13398 }
13399 }
13400 }
13401 }
13402
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013403 private void stopProfilerLocked(ProcessRecord proc, String path, int profileType) {
13404 if (proc == null || proc == mProfileProc) {
13405 proc = mProfileProc;
13406 path = mProfileFile;
13407 profileType = mProfileType;
13408 clearProfilerLocked();
13409 }
13410 if (proc == null) {
13411 return;
13412 }
13413 try {
13414 proc.thread.profilerControl(false, path, null, profileType);
13415 } catch (RemoteException e) {
13416 throw new IllegalStateException("Process disappeared");
13417 }
13418 }
13419
13420 private void clearProfilerLocked() {
13421 if (mProfileFd != null) {
13422 try {
13423 mProfileFd.close();
13424 } catch (IOException e) {
13425 }
13426 }
13427 mProfileApp = null;
13428 mProfileProc = null;
13429 mProfileFile = null;
13430 mProfileType = 0;
13431 mAutoStopProfiler = false;
13432 }
13433
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013434 public boolean profileControl(String process, boolean start,
Romain Guy7eabe552011-07-21 14:56:34 -070013435 String path, ParcelFileDescriptor fd, int profileType) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013436
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013437 try {
13438 synchronized (this) {
13439 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13440 // its own permission.
13441 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13442 != PackageManager.PERMISSION_GRANTED) {
13443 throw new SecurityException("Requires permission "
13444 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013445 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013446
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013447 if (start && fd == null) {
13448 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013449 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013450
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013451 ProcessRecord proc = null;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013452 if (process != null) {
13453 try {
13454 int pid = Integer.parseInt(process);
13455 synchronized (mPidsSelfLocked) {
13456 proc = mPidsSelfLocked.get(pid);
13457 }
13458 } catch (NumberFormatException e) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013459 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013460
13461 if (proc == null) {
13462 HashMap<String, SparseArray<ProcessRecord>> all
13463 = mProcessNames.getMap();
13464 SparseArray<ProcessRecord> procs = all.get(process);
13465 if (procs != null && procs.size() > 0) {
13466 proc = procs.valueAt(0);
13467 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013468 }
13469 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013470
13471 if (start && (proc == null || proc.thread == null)) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013472 throw new IllegalArgumentException("Unknown process: " + process);
13473 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013474
13475 if (start) {
13476 stopProfilerLocked(null, null, 0);
13477 setProfileApp(proc.info, proc.processName, path, fd, false);
13478 mProfileProc = proc;
13479 mProfileType = profileType;
13480 try {
13481 fd = fd.dup();
13482 } catch (IOException e) {
13483 fd = null;
13484 }
13485 proc.thread.profilerControl(start, path, fd, profileType);
13486 fd = null;
13487 mProfileFd = null;
13488 } else {
13489 stopProfilerLocked(proc, path, profileType);
13490 if (fd != null) {
13491 try {
13492 fd.close();
13493 } catch (IOException e) {
13494 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013495 }
13496 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -070013497
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013498 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070013499 }
13500 } catch (RemoteException e) {
13501 throw new IllegalStateException("Process disappeared");
13502 } finally {
13503 if (fd != null) {
13504 try {
13505 fd.close();
13506 } catch (IOException e) {
13507 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080013508 }
13509 }
13510 }
Andy McFadden824c5102010-07-09 16:26:57 -070013511
13512 public boolean dumpHeap(String process, boolean managed,
13513 String path, ParcelFileDescriptor fd) throws RemoteException {
13514
13515 try {
13516 synchronized (this) {
13517 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
13518 // its own permission (same as profileControl).
13519 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
13520 != PackageManager.PERMISSION_GRANTED) {
13521 throw new SecurityException("Requires permission "
13522 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
13523 }
13524
13525 if (fd == null) {
13526 throw new IllegalArgumentException("null fd");
13527 }
13528
13529 ProcessRecord proc = null;
13530 try {
13531 int pid = Integer.parseInt(process);
13532 synchronized (mPidsSelfLocked) {
13533 proc = mPidsSelfLocked.get(pid);
13534 }
13535 } catch (NumberFormatException e) {
13536 }
13537
13538 if (proc == null) {
13539 HashMap<String, SparseArray<ProcessRecord>> all
13540 = mProcessNames.getMap();
13541 SparseArray<ProcessRecord> procs = all.get(process);
13542 if (procs != null && procs.size() > 0) {
13543 proc = procs.valueAt(0);
13544 }
13545 }
13546
13547 if (proc == null || proc.thread == null) {
13548 throw new IllegalArgumentException("Unknown process: " + process);
13549 }
13550
Dianne Hackbornf02e57b2011-03-01 22:21:04 -080013551 boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
13552 if (!isDebuggable) {
Andy McFadden824c5102010-07-09 16:26:57 -070013553 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
13554 throw new SecurityException("Process not debuggable: " + proc);
13555 }
13556 }
13557
13558 proc.thread.dumpHeap(managed, path, fd);
13559 fd = null;
13560 return true;
13561 }
13562 } catch (RemoteException e) {
13563 throw new IllegalStateException("Process disappeared");
13564 } finally {
13565 if (fd != null) {
13566 try {
13567 fd.close();
13568 } catch (IOException e) {
13569 }
13570 }
13571 }
13572 }
13573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013574 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
13575 public void monitor() {
13576 synchronized (this) { }
13577 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013578
Dianne Hackborna573f6a2012-02-09 16:12:18 -080013579 void onCoreSettingsChange(Bundle settings) {
Svetoslav Ganov54d068e2011-03-02 12:58:40 -080013580 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
13581 ProcessRecord processRecord = mLruProcesses.get(i);
13582 try {
13583 if (processRecord.thread != null) {
13584 processRecord.thread.setCoreSettings(settings);
13585 }
13586 } catch (RemoteException re) {
13587 /* ignore */
13588 }
13589 }
13590 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -070013591
13592 // Multi-user methods
13593
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013594 @Override
Amith Yamasani742a6712011-05-04 14:49:28 -070013595 public boolean switchUser(int userId) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013596 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
13597 != PackageManager.PERMISSION_GRANTED) {
13598 String msg = "Permission Denial: switchUser() from pid="
13599 + Binder.getCallingPid()
13600 + ", uid=" + Binder.getCallingUid()
13601 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
13602 Slog.w(TAG, msg);
13603 throw new SecurityException(msg);
Amith Yamasani742a6712011-05-04 14:49:28 -070013604 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013605 synchronized (this) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013606 if (mCurrentUserId == userId) {
13607 return true;
13608 }
13609
13610 // If the user we are switching to is not currently started, then
13611 // we need to start it now.
13612 if (mStartedUsers.get(userId) == null) {
13613 mStartedUsers.put(userId, new UserStartedState(new UserHandle(userId), false));
Amith Yamasani742a6712011-05-04 14:49:28 -070013614 }
13615
13616 mCurrentUserId = userId;
13617 boolean haveActivities = mMainStack.switchUser(userId);
13618 if (!haveActivities) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013619 startHomeActivityLocked(userId, mStartedUsers.get(userId));
Amith Yamasani742a6712011-05-04 14:49:28 -070013620 }
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013621 }
Amith Yamasani13593602012-03-22 16:16:17 -070013622
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013623 long ident = Binder.clearCallingIdentity();
13624 try {
13625 // Inform of user switch
13626 Intent addedIntent = new Intent(Intent.ACTION_USER_SWITCHED);
13627 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
13628 mContext.sendBroadcast(addedIntent, android.Manifest.permission.MANAGE_USERS);
13629 } finally {
13630 Binder.restoreCallingIdentity(ident);
13631 }
Amith Yamasani13593602012-03-22 16:16:17 -070013632
Amith Yamasani4b2e9342011-03-31 12:38:53 -070013633 return true;
13634 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013635
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013636 void finishUserSwitch(UserStartedState uss) {
13637 synchronized (this) {
13638 if (uss.mState == UserStartedState.STATE_BOOTING
13639 && mStartedUsers.get(uss.mHandle.getIdentifier()) == uss) {
13640 uss.mState = UserStartedState.STATE_RUNNING;
13641 broadcastIntentLocked(null, null,
13642 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
13643 null, null, 0, null, null,
13644 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
13645 false, false, MY_PID, Process.SYSTEM_UID, uss.mHandle.getIdentifier());
13646 }
Amith Yamasani52f1d752012-03-28 18:19:29 -070013647 }
Amith Yamasani52f1d752012-03-28 18:19:29 -070013648 }
13649
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013650 @Override
13651 public int stopUser(final int userId, final IStopUserCallback callback) {
13652 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
13653 != PackageManager.PERMISSION_GRANTED) {
13654 String msg = "Permission Denial: switchUser() from pid="
13655 + Binder.getCallingPid()
13656 + ", uid=" + Binder.getCallingUid()
13657 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
13658 Slog.w(TAG, msg);
13659 throw new SecurityException(msg);
13660 }
13661 if (userId <= 0) {
13662 throw new IllegalArgumentException("Can't stop primary user " + userId);
13663 }
Amith Yamasani13593602012-03-22 16:16:17 -070013664 synchronized (this) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013665 if (mCurrentUserId == userId) {
13666 return ActivityManager.USER_OP_IS_CURRENT;
13667 }
13668
13669 final UserStartedState uss = mStartedUsers.get(userId);
13670 if (uss == null) {
13671 // User is not started, nothing to do... but we do need to
13672 // callback if requested.
13673 if (callback != null) {
13674 mHandler.post(new Runnable() {
13675 @Override
13676 public void run() {
13677 try {
13678 callback.userStopped(userId);
13679 } catch (RemoteException e) {
13680 }
13681 }
13682 });
13683 }
13684 return ActivityManager.USER_OP_SUCCESS;
13685 }
13686
13687 if (callback != null) {
13688 uss.mStopCallbacks.add(callback);
13689 }
13690
13691 if (uss.mState != UserStartedState.STATE_STOPPING) {
13692 uss.mState = UserStartedState.STATE_STOPPING;
13693
13694 long ident = Binder.clearCallingIdentity();
13695 try {
13696 // Inform of user switch
13697 Intent intent = new Intent(Intent.ACTION_SHUTDOWN);
13698 final IIntentReceiver resultReceiver = new IIntentReceiver.Stub() {
13699 @Override
13700 public void performReceive(Intent intent, int resultCode, String data,
13701 Bundle extras, boolean ordered, boolean sticky) {
13702 finishUserStop(uss);
13703 }
13704 };
13705 broadcastIntentLocked(null, null, intent,
13706 null, resultReceiver, 0, null, null, null,
13707 true, false, MY_PID, Process.SYSTEM_UID, userId);
13708 } finally {
13709 Binder.restoreCallingIdentity(ident);
Amith Yamasani13593602012-03-22 16:16:17 -070013710 }
13711 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013712 }
Amith Yamasani13593602012-03-22 16:16:17 -070013713
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013714 return ActivityManager.USER_OP_SUCCESS;
13715 }
13716
13717 void finishUserStop(UserStartedState uss) {
13718 final int userId = uss.mHandle.getIdentifier();
13719 boolean stopped;
13720 ArrayList<IStopUserCallback> callbacks;
13721 synchronized (this) {
13722 callbacks = new ArrayList<IStopUserCallback>(uss.mStopCallbacks);
13723 if (uss.mState != UserStartedState.STATE_STOPPING
13724 || mStartedUsers.get(userId) != uss) {
13725 stopped = false;
13726 } else {
13727 stopped = true;
13728 // User can no longer run.
13729 mStartedUsers.remove(userId);
13730
13731 // Clean up all state and processes associated with the user.
13732 // Kill all the processes for the user.
13733 forceStopUserLocked(userId);
Amith Yamasani13593602012-03-22 16:16:17 -070013734 }
13735 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -070013736
13737 for (int i=0; i<callbacks.size(); i++) {
13738 try {
13739 if (stopped) callbacks.get(i).userStopped(userId);
13740 else callbacks.get(i).userStopAborted(userId);
13741 } catch (RemoteException e) {
13742 }
13743 }
13744 }
13745
13746 @Override
13747 public UserInfo getCurrentUser() {
13748 if (checkCallingPermission(android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
13749 != PackageManager.PERMISSION_GRANTED) {
13750 String msg = "Permission Denial: getCurrentUser() from pid="
13751 + Binder.getCallingPid()
13752 + ", uid=" + Binder.getCallingUid()
13753 + " requires " + android.Manifest.permission.INTERACT_ACROSS_USERS_FULL;
13754 Slog.w(TAG, msg);
13755 throw new SecurityException(msg);
13756 }
13757 synchronized (this) {
13758 return getUserManager().getUserInfo(mCurrentUserId);
13759 }
Amith Yamasani13593602012-03-22 16:16:17 -070013760 }
13761
Amith Yamasani742a6712011-05-04 14:49:28 -070013762 private boolean userExists(int userId) {
Amith Yamasani258848d2012-08-10 17:06:33 -070013763 UserInfo user = getUserManager().getUserInfo(userId);
13764 return user != null;
13765 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013766
Amith Yamasani258848d2012-08-10 17:06:33 -070013767 UserManager getUserManager() {
13768 if (mUserManager == null) {
13769 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
13770 }
13771 return mUserManager;
Amith Yamasani742a6712011-05-04 14:49:28 -070013772 }
13773
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013774 private void checkValidCaller(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013775 if (UserHandle.getUserId(uid) == userId || uid == Process.SYSTEM_UID || uid == 0) return;
Amith Yamasani37ce3a82012-02-06 12:04:42 -080013776
13777 throw new SecurityException("Caller uid=" + uid
13778 + " is not privileged to communicate with user=" + userId);
13779 }
Amith Yamasani742a6712011-05-04 14:49:28 -070013780
13781 private int applyUserId(int uid, int userId) {
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013782 return UserHandle.getUid(userId, uid);
Amith Yamasani742a6712011-05-04 14:49:28 -070013783 }
13784
Dianne Hackborn599db5c2012-08-03 19:28:48 -070013785 ApplicationInfo getAppInfoForUser(ApplicationInfo info, int userId) {
Amith Yamasani2f6c9eb2012-02-06 15:31:35 -080013786 if (info == null) return null;
Amith Yamasani742a6712011-05-04 14:49:28 -070013787 ApplicationInfo newInfo = new ApplicationInfo(info);
13788 newInfo.uid = applyUserId(info.uid, userId);
Amith Yamasania4a54e22012-04-16 15:44:19 -070013789 newInfo.dataDir = USER_DATA_DIR + userId + "/"
13790 + info.packageName;
Amith Yamasani742a6712011-05-04 14:49:28 -070013791 return newInfo;
13792 }
13793
13794 ActivityInfo getActivityInfoForUser(ActivityInfo aInfo, int userId) {
Amith Yamasania4a54e22012-04-16 15:44:19 -070013795 if (aInfo == null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070013796 || (userId < 1 && aInfo.applicationInfo.uid < UserHandle.PER_USER_RANGE)) {
Amith Yamasani742a6712011-05-04 14:49:28 -070013797 return aInfo;
13798 }
13799
13800 ActivityInfo info = new ActivityInfo(aInfo);
13801 info.applicationInfo = getAppInfoForUser(info.applicationInfo, userId);
13802 return info;
13803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013804}