blob: 3b655fa7265b37b971dbb59ffe420a2d63ee2c16 [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
19import com.android.internal.os.BatteryStatsImpl;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070020import com.android.server.AttributeCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import com.android.server.IntentResolver;
22import com.android.server.ProcessMap;
23import com.android.server.ProcessStats;
24import com.android.server.SystemServer;
25import com.android.server.Watchdog;
26import com.android.server.WindowManagerService;
27
Dianne Hackborndd71fc82009-12-16 19:24:32 -080028import dalvik.system.Zygote;
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.app.Activity;
31import android.app.ActivityManager;
32import android.app.ActivityManagerNative;
33import android.app.ActivityThread;
34import android.app.AlertDialog;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020035import android.app.ApplicationErrorReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.app.Dialog;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070037import android.app.IActivityController;
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -080038import android.app.IActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.app.IActivityWatcher;
40import android.app.IApplicationThread;
41import android.app.IInstrumentationWatcher;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.app.IServiceConnection;
43import android.app.IThumbnailReceiver;
44import android.app.Instrumentation;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070045import android.app.Notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.app.PendingIntent;
47import android.app.ResultInfo;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070048import android.app.Service;
Christopher Tate45281862010-03-05 15:46:30 -080049import android.app.backup.IBackupManager;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020050import android.content.ActivityNotFoundException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080051import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.content.ComponentName;
53import android.content.ContentResolver;
54import android.content.Context;
55import android.content.Intent;
56import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070057import android.content.IIntentReceiver;
58import android.content.IIntentSender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070059import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.content.pm.ActivityInfo;
61import android.content.pm.ApplicationInfo;
62import android.content.pm.ConfigurationInfo;
63import android.content.pm.IPackageDataObserver;
64import android.content.pm.IPackageManager;
65import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080066import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.content.pm.PackageManager;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070068import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.content.pm.ProviderInfo;
70import android.content.pm.ResolveInfo;
71import android.content.pm.ServiceInfo;
72import android.content.res.Configuration;
73import android.graphics.Bitmap;
74import android.net.Uri;
75import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080076import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080077import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070078import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080079import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080081import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.FileUtils;
83import android.os.Handler;
84import android.os.IBinder;
85import android.os.IPermissionController;
86import android.os.Looper;
87import android.os.Message;
88import android.os.Parcel;
89import android.os.ParcelFileDescriptor;
90import android.os.PowerManager;
91import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070092import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.os.RemoteException;
94import android.os.ServiceManager;
95import android.os.SystemClock;
96import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.Config;
99import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800100import android.util.Slog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800101import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.util.PrintWriterPrinter;
103import android.util.SparseArray;
104import android.view.Gravity;
105import android.view.LayoutInflater;
106import android.view.View;
107import android.view.WindowManager;
108import android.view.WindowManagerPolicy;
109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import java.io.File;
111import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.io.FileNotFoundException;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200113import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800114import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import java.io.PrintWriter;
116import java.lang.IllegalStateException;
117import java.lang.ref.WeakReference;
118import java.util.ArrayList;
119import java.util.HashMap;
120import java.util.HashSet;
121import java.util.Iterator;
122import java.util.List;
123import java.util.Locale;
124import java.util.Map;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700125import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor {
128 static final String TAG = "ActivityManager";
129 static final boolean DEBUG = false;
130 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
131 static final boolean DEBUG_SWITCH = localLOGV || false;
132 static final boolean DEBUG_TASKS = localLOGV || false;
133 static final boolean DEBUG_PAUSE = localLOGV || false;
134 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
135 static final boolean DEBUG_TRANSITION = localLOGV || false;
136 static final boolean DEBUG_BROADCAST = localLOGV || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700137 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 static final boolean DEBUG_SERVICE = localLOGV || false;
139 static final boolean DEBUG_VISBILITY = localLOGV || false;
140 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700141 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800142 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700144 static final boolean DEBUG_RESULTS = localLOGV || false;
Christopher Tate436344a2009-09-30 16:17:37 -0700145 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700146 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final boolean VALIDATE_TOKENS = false;
148 static final boolean SHOW_ACTIVITY_START_TIME = true;
149
150 // Control over CPU and battery monitoring.
151 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
152 static final boolean MONITOR_CPU_USAGE = true;
153 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
154 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
155 static final boolean MONITOR_THREAD_CPU_USAGE = false;
156
Dianne Hackborn1655be42009-05-08 14:29:01 -0700157 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700158 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 private static final String SYSTEM_SECURE = "ro.secure";
161
162 // This is the maximum number of application processes we would like
163 // to have running. Due to the asynchronous nature of things, we can
164 // temporarily go beyond this limit.
165 static final int MAX_PROCESSES = 2;
166
167 // Set to false to leave processes running indefinitely, relying on
168 // the kernel killing them as resources are required.
169 static final boolean ENFORCE_PROCESS_LIMIT = false;
170
171 // This is the maximum number of activities that we would like to have
172 // running at a given time.
173 static final int MAX_ACTIVITIES = 20;
174
175 // Maximum number of recent tasks that we can remember.
176 static final int MAX_RECENT_TASKS = 20;
177
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700178 // Amount of time after a call to stopAppSwitches() during which we will
179 // prevent further untrusted switches from happening.
180 static final long APP_SWITCH_DELAY_TIME = 5*1000;
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 // How long until we reset a task when the user returns to it. Currently
183 // 30 minutes.
184 static final long ACTIVITY_INACTIVE_RESET_TIME = 1000*60*30;
185
186 // Set to true to disable the icon that is shown while a new activity
187 // is being started.
188 static final boolean SHOW_APP_STARTING_ICON = true;
189
190 // How long we wait until giving up on the last activity to pause. This
191 // is short because it directly impacts the responsiveness of starting the
192 // next activity.
193 static final int PAUSE_TIMEOUT = 500;
194
195 /**
196 * How long we can hold the launch wake lock before giving up.
197 */
198 static final int LAUNCH_TIMEOUT = 10*1000;
199
200 // How long we wait for a launched process to attach to the activity manager
201 // before we decide it's never going to come up for real.
202 static final int PROC_START_TIMEOUT = 10*1000;
203
204 // How long we wait until giving up on the last activity telling us it
205 // is idle.
206 static final int IDLE_TIMEOUT = 10*1000;
207
208 // How long to wait after going idle before forcing apps to GC.
209 static final int GC_TIMEOUT = 5*1000;
210
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700211 // The minimum amount of time between successive GC requests for a process.
212 static final int GC_MIN_INTERVAL = 60*1000;
213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 // How long we wait until giving up on an activity telling us it has
215 // finished destroying itself.
216 static final int DESTROY_TIMEOUT = 10*1000;
217
218 // How long we allow a receiver to run before giving up on it.
219 static final int BROADCAST_TIMEOUT = 10*1000;
220
221 // How long we wait for a service to finish executing.
222 static final int SERVICE_TIMEOUT = 20*1000;
223
224 // How long a service needs to be running until restarting its process
225 // is no longer considered to be a relaunch of the service.
226 static final int SERVICE_RESTART_DURATION = 5*1000;
227
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700228 // How long a service needs to be running until it will start back at
229 // SERVICE_RESTART_DURATION after being killed.
230 static final int SERVICE_RESET_RUN_DURATION = 60*1000;
231
232 // Multiplying factor to increase restart duration time by, for each time
233 // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
234 static final int SERVICE_RESTART_DURATION_FACTOR = 4;
235
236 // The minimum amount of time between restarting services that we allow.
237 // That is, when multiple services are restarting, we won't allow each
238 // to restart less than this amount of time from the last one.
239 static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 // Maximum amount of time for there to be no activity on a service before
242 // we consider it non-essential and allow its process to go on the
243 // LRU background list.
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700244 static final int MAX_SERVICE_INACTIVITY = 30*60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245
246 // How long we wait until we timeout on key dispatching.
247 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
248
249 // The minimum time we allow between crashes, for us to consider this
250 // application to be bad and stop and its services and reject broadcasts.
251 static final int MIN_CRASH_INTERVAL = 60*1000;
252
253 // How long we wait until we timeout on key dispatching during instrumentation.
254 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
255
256 // OOM adjustments for processes in various states:
257
258 // This is a process without anything currently running in it. Definitely
259 // the first to go! Value set in system/rootdir/init.rc on startup.
260 // This value is initalized in the constructor, careful when refering to
261 // this static variable externally.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800262 static final int EMPTY_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
264 // This is a process only hosting activities that are not visible,
265 // so it can be killed without any disruption. Value set in
266 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800267 static final int HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 static int HIDDEN_APP_MIN_ADJ;
269
The Android Open Source Project4df24232009-03-05 14:34:35 -0800270 // This is a process holding the home application -- we want to try
271 // avoiding killing it, even if it would normally be in the background,
272 // because the user interacts with it so much.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800273 static final int HOME_APP_ADJ;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800274
Christopher Tate6fa95972009-06-05 18:43:55 -0700275 // This is a process currently hosting a backup operation. Killing it
276 // is not entirely fatal but is generally a bad idea.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800277 static final int BACKUP_APP_ADJ;
Christopher Tate6fa95972009-06-05 18:43:55 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 // This is a process holding a secondary server -- killing it will not
280 // have much of an impact as far as the user is concerned. Value set in
281 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800282 static final int SECONDARY_SERVER_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 // This is a process only hosting activities that are visible to the
285 // user, so we'd prefer they don't disappear. Value set in
286 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800287 static final int VISIBLE_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
289 // This is the process running the current foreground app. We'd really
290 // rather not kill it! Value set in system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800291 static final int FOREGROUND_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
293 // This is a process running a core server, such as telephony. Definitely
294 // don't want to kill it, but doing so is not completely fatal.
295 static final int CORE_SERVER_ADJ = -12;
296
297 // The system process runs at the default adjustment.
298 static final int SYSTEM_ADJ = -16;
299
300 // Memory pages are 4K.
301 static final int PAGE_SIZE = 4*1024;
302
303 // Corresponding memory levels for above adjustments.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800304 static final int EMPTY_APP_MEM;
305 static final int HIDDEN_APP_MEM;
306 static final int HOME_APP_MEM;
307 static final int BACKUP_APP_MEM;
308 static final int SECONDARY_SERVER_MEM;
309 static final int VISIBLE_APP_MEM;
310 static final int FOREGROUND_APP_MEM;
311
312 // The minimum number of hidden apps we want to be able to keep around,
313 // without empty apps being able to push them out of memory.
314 static final int MIN_HIDDEN_APPS = 2;
315
316 // We put empty content processes after any hidden processes that have
317 // been idle for less than 30 seconds.
318 static final long CONTENT_APP_IDLE_OFFSET = 30*1000;
319
320 // We put empty content processes after any hidden processes that have
321 // been idle for less than 60 seconds.
322 static final long EMPTY_APP_IDLE_OFFSET = 60*1000;
323
324 static {
325 // These values are set in system/rootdir/init.rc on startup.
326 FOREGROUND_APP_ADJ =
327 Integer.valueOf(SystemProperties.get("ro.FOREGROUND_APP_ADJ"));
328 VISIBLE_APP_ADJ =
329 Integer.valueOf(SystemProperties.get("ro.VISIBLE_APP_ADJ"));
330 SECONDARY_SERVER_ADJ =
331 Integer.valueOf(SystemProperties.get("ro.SECONDARY_SERVER_ADJ"));
332 BACKUP_APP_ADJ =
333 Integer.valueOf(SystemProperties.get("ro.BACKUP_APP_ADJ"));
334 HOME_APP_ADJ =
335 Integer.valueOf(SystemProperties.get("ro.HOME_APP_ADJ"));
336 HIDDEN_APP_MIN_ADJ =
337 Integer.valueOf(SystemProperties.get("ro.HIDDEN_APP_MIN_ADJ"));
338 EMPTY_APP_ADJ =
339 Integer.valueOf(SystemProperties.get("ro.EMPTY_APP_ADJ"));
340 HIDDEN_APP_MAX_ADJ = EMPTY_APP_ADJ-1;
341 FOREGROUND_APP_MEM =
342 Integer.valueOf(SystemProperties.get("ro.FOREGROUND_APP_MEM"))*PAGE_SIZE;
343 VISIBLE_APP_MEM =
344 Integer.valueOf(SystemProperties.get("ro.VISIBLE_APP_MEM"))*PAGE_SIZE;
345 SECONDARY_SERVER_MEM =
346 Integer.valueOf(SystemProperties.get("ro.SECONDARY_SERVER_MEM"))*PAGE_SIZE;
347 BACKUP_APP_MEM =
348 Integer.valueOf(SystemProperties.get("ro.BACKUP_APP_MEM"))*PAGE_SIZE;
349 HOME_APP_MEM =
350 Integer.valueOf(SystemProperties.get("ro.HOME_APP_MEM"))*PAGE_SIZE;
351 HIDDEN_APP_MEM =
352 Integer.valueOf(SystemProperties.get("ro.HIDDEN_APP_MEM"))*PAGE_SIZE;
353 EMPTY_APP_MEM =
354 Integer.valueOf(SystemProperties.get("ro.EMPTY_APP_MEM"))*PAGE_SIZE;
355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
Dan Egnor42471dd2010-01-07 17:25:22 -0800357 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358
359 static final String[] EMPTY_STRING_ARRAY = new String[0];
360
361 enum ActivityState {
362 INITIALIZING,
363 RESUMED,
364 PAUSING,
365 PAUSED,
366 STOPPING,
367 STOPPED,
368 FINISHING,
369 DESTROYING,
370 DESTROYED
371 }
372
373 /**
374 * The back history of all previous (and possibly still
375 * running) activities. It contains HistoryRecord objects.
376 */
377 final ArrayList mHistory = new ArrayList();
378
379 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700380 * Description of a request to start a new activity, which has been held
381 * due to app switches being disabled.
382 */
383 class PendingActivityLaunch {
384 HistoryRecord r;
385 HistoryRecord sourceRecord;
386 Uri[] grantedUriPermissions;
387 int grantedMode;
388 boolean onlyIfNeeded;
389 }
390
391 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
392 = new ArrayList<PendingActivityLaunch>();
393
394 /**
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -0800395 * List of people waiting to find out about the next launched activity.
396 */
397 final ArrayList<IActivityManager.WaitResult> mWaitingActivityLaunched
398 = new ArrayList<IActivityManager.WaitResult>();
399
400 /**
401 * List of people waiting to find out about the next visible activity.
402 */
403 final ArrayList<IActivityManager.WaitResult> mWaitingActivityVisible
404 = new ArrayList<IActivityManager.WaitResult>();
405
406 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 * List of all active broadcasts that are to be executed immediately
408 * (without waiting for another broadcast to finish). Currently this only
409 * contains broadcasts to registered receivers, to avoid spinning up
410 * a bunch of processes to execute IntentReceiver components.
411 */
412 final ArrayList<BroadcastRecord> mParallelBroadcasts
413 = new ArrayList<BroadcastRecord>();
414
415 /**
416 * List of all active broadcasts that are to be executed one at a time.
417 * The object at the top of the list is the currently activity broadcasts;
418 * those after it are waiting for the top to finish..
419 */
420 final ArrayList<BroadcastRecord> mOrderedBroadcasts
421 = new ArrayList<BroadcastRecord>();
422
423 /**
Dianne Hackborn12527f92009-11-11 17:39:50 -0800424 * Historical data of past broadcasts, for debugging.
425 */
426 static final int MAX_BROADCAST_HISTORY = 100;
427 final BroadcastRecord[] mBroadcastHistory
428 = new BroadcastRecord[MAX_BROADCAST_HISTORY];
429
430 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 * Set when we current have a BROADCAST_INTENT_MSG in flight.
432 */
433 boolean mBroadcastsScheduled = false;
434
435 /**
436 * Set to indicate whether to issue an onUserLeaving callback when a
437 * newly launched activity is being brought in front of us.
438 */
439 boolean mUserLeaving = false;
440
441 /**
442 * When we are in the process of pausing an activity, before starting the
443 * next one, this variable holds the activity that is currently being paused.
444 */
445 HistoryRecord mPausingActivity = null;
446
447 /**
448 * Current activity that is resumed, or null if there is none.
449 */
450 HistoryRecord mResumedActivity = null;
451
452 /**
453 * Activity we have told the window manager to have key focus.
454 */
455 HistoryRecord mFocusedActivity = null;
456
457 /**
458 * This is the last activity that we put into the paused state. This is
459 * used to determine if we need to do an activity transition while sleeping,
460 * when we normally hold the top activity paused.
461 */
462 HistoryRecord mLastPausedActivity = null;
463
464 /**
465 * List of activities that are waiting for a new activity
466 * to become visible before completing whatever operation they are
467 * supposed to do.
468 */
469 final ArrayList mWaitingVisibleActivities = new ArrayList();
470
471 /**
472 * List of activities that are ready to be stopped, but waiting
473 * for the next activity to settle down before doing so. It contains
474 * HistoryRecord objects.
475 */
476 final ArrayList<HistoryRecord> mStoppingActivities
477 = new ArrayList<HistoryRecord>();
478
479 /**
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700480 * Animations that for the current transition have requested not to
481 * be considered for the transition animation.
482 */
483 final ArrayList<HistoryRecord> mNoAnimActivities
484 = new ArrayList<HistoryRecord>();
485
486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 * List of intents that were used to start the most recent tasks.
488 */
489 final ArrayList<TaskRecord> mRecentTasks
490 = new ArrayList<TaskRecord>();
491
492 /**
493 * List of activities that are ready to be finished, but waiting
494 * for the previous activity to settle down before doing so. It contains
495 * HistoryRecord objects.
496 */
497 final ArrayList mFinishingActivities = new ArrayList();
498
499 /**
500 * All of the applications we currently have running organized by name.
501 * The keys are strings of the application package name (as
502 * returned by the package manager), and the keys are ApplicationRecord
503 * objects.
504 */
505 final ProcessMap<ProcessRecord> mProcessNames
506 = new ProcessMap<ProcessRecord>();
507
508 /**
509 * The last time that various processes have crashed.
510 */
511 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
512
513 /**
514 * Set of applications that we consider to be bad, and will reject
515 * incoming broadcasts from (which the user has no control over).
516 * Processes are added to this set when they have crashed twice within
517 * a minimum amount of time; they are removed from it when they are
518 * later restarted (hopefully due to some user action). The value is the
519 * time it was added to the list.
520 */
521 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
522
523 /**
524 * All of the processes we currently have running organized by pid.
525 * The keys are the pid running the application.
526 *
527 * <p>NOTE: This object is protected by its own lock, NOT the global
528 * activity manager lock!
529 */
530 final SparseArray<ProcessRecord> mPidsSelfLocked
531 = new SparseArray<ProcessRecord>();
532
533 /**
534 * All of the processes that have been forced to be foreground. The key
535 * is the pid of the caller who requested it (we hold a death
536 * link on it).
537 */
538 abstract class ForegroundToken implements IBinder.DeathRecipient {
539 int pid;
540 IBinder token;
541 }
542 final SparseArray<ForegroundToken> mForegroundProcesses
543 = new SparseArray<ForegroundToken>();
544
545 /**
546 * List of records for processes that someone had tried to start before the
547 * system was ready. We don't start them at that point, but ensure they
548 * are started by the time booting is complete.
549 */
550 final ArrayList<ProcessRecord> mProcessesOnHold
551 = new ArrayList<ProcessRecord>();
552
553 /**
554 * List of records for processes that we have started and are waiting
555 * for them to call back. This is really only needed when running in
556 * single processes mode, in which case we do not have a unique pid for
557 * each process.
558 */
559 final ArrayList<ProcessRecord> mStartingProcesses
560 = new ArrayList<ProcessRecord>();
561
562 /**
563 * List of persistent applications that are in the process
564 * of being started.
565 */
566 final ArrayList<ProcessRecord> mPersistentStartingProcesses
567 = new ArrayList<ProcessRecord>();
568
569 /**
570 * Processes that are being forcibly torn down.
571 */
572 final ArrayList<ProcessRecord> mRemovedProcesses
573 = new ArrayList<ProcessRecord>();
574
575 /**
576 * List of running applications, sorted by recent usage.
577 * The first entry in the list is the least recently used.
578 * It contains ApplicationRecord objects. This list does NOT include
579 * any persistent application records (since we never want to exit them).
580 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800581 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 = new ArrayList<ProcessRecord>();
583
584 /**
585 * List of processes that should gc as soon as things are idle.
586 */
587 final ArrayList<ProcessRecord> mProcessesToGc
588 = new ArrayList<ProcessRecord>();
589
590 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800591 * This is the process holding what we currently consider to be
592 * the "home" activity.
593 */
594 private ProcessRecord mHomeProcess;
595
596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 * List of running activities, sorted by recent usage.
598 * The first entry in the list is the least recently used.
599 * It contains HistoryRecord objects.
600 */
601 private final ArrayList mLRUActivities = new ArrayList();
602
603 /**
604 * Set of PendingResultRecord objects that are currently active.
605 */
606 final HashSet mPendingResultRecords = new HashSet();
607
608 /**
609 * Set of IntentSenderRecord objects that are currently active.
610 */
611 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
612 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
613
614 /**
615 * Intent broadcast that we have tried to start, but are
616 * waiting for its application's process to be created. We only
617 * need one (instead of a list) because we always process broadcasts
618 * one at a time, so no others can be started while waiting for this
619 * one.
620 */
621 BroadcastRecord mPendingBroadcast = null;
622
623 /**
624 * Keeps track of all IIntentReceivers that have been registered for
625 * broadcasts. Hash keys are the receiver IBinder, hash value is
626 * a ReceiverList.
627 */
628 final HashMap mRegisteredReceivers = new HashMap();
629
630 /**
631 * Resolver for broadcast intents to registered receivers.
632 * Holds BroadcastFilter (subclass of IntentFilter).
633 */
634 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
635 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
636 @Override
637 protected boolean allowFilterResult(
638 BroadcastFilter filter, List<BroadcastFilter> dest) {
639 IBinder target = filter.receiverList.receiver.asBinder();
640 for (int i=dest.size()-1; i>=0; i--) {
641 if (dest.get(i).receiverList.receiver.asBinder() == target) {
642 return false;
643 }
644 }
645 return true;
646 }
647 };
648
649 /**
650 * State of all active sticky broadcasts. Keys are the action of the
651 * sticky Intent, values are an ArrayList of all broadcasted intents with
652 * that action (which should usually be one).
653 */
654 final HashMap<String, ArrayList<Intent>> mStickyBroadcasts =
655 new HashMap<String, ArrayList<Intent>>();
656
657 /**
658 * All currently running services.
659 */
660 final HashMap<ComponentName, ServiceRecord> mServices =
661 new HashMap<ComponentName, ServiceRecord>();
662
663 /**
664 * All currently running services indexed by the Intent used to start them.
665 */
666 final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent =
667 new HashMap<Intent.FilterComparison, ServiceRecord>();
668
669 /**
670 * All currently bound service connections. Keys are the IBinder of
671 * the client's IServiceConnection.
672 */
673 final HashMap<IBinder, ConnectionRecord> mServiceConnections
674 = new HashMap<IBinder, ConnectionRecord>();
675
676 /**
677 * List of services that we have been asked to start,
678 * but haven't yet been able to. It is used to hold start requests
679 * while waiting for their corresponding application thread to get
680 * going.
681 */
682 final ArrayList<ServiceRecord> mPendingServices
683 = new ArrayList<ServiceRecord>();
684
685 /**
686 * List of services that are scheduled to restart following a crash.
687 */
688 final ArrayList<ServiceRecord> mRestartingServices
689 = new ArrayList<ServiceRecord>();
690
691 /**
692 * List of services that are in the process of being stopped.
693 */
694 final ArrayList<ServiceRecord> mStoppingServices
695 = new ArrayList<ServiceRecord>();
696
697 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700698 * Backup/restore process management
699 */
700 String mBackupAppName = null;
701 BackupRecord mBackupTarget = null;
702
703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 * List of PendingThumbnailsRecord objects of clients who are still
705 * waiting to receive all of the thumbnails for a task.
706 */
707 final ArrayList mPendingThumbnails = new ArrayList();
708
709 /**
710 * List of HistoryRecord objects that have been finished and must
711 * still report back to a pending thumbnail receiver.
712 */
713 final ArrayList mCancelledThumbnails = new ArrayList();
714
715 /**
716 * All of the currently running global content providers. Keys are a
717 * string containing the provider name and values are a
718 * ContentProviderRecord object containing the data about it. Note
719 * that a single provider may be published under multiple names, so
720 * there may be multiple entries here for a single one in mProvidersByClass.
721 */
722 final HashMap mProvidersByName = new HashMap();
723
724 /**
725 * All of the currently running global content providers. Keys are a
726 * string containing the provider's implementation class and values are a
727 * ContentProviderRecord object containing the data about it.
728 */
729 final HashMap mProvidersByClass = new HashMap();
730
731 /**
732 * List of content providers who have clients waiting for them. The
733 * application is currently being launched and the provider will be
734 * removed from this list once it is published.
735 */
736 final ArrayList mLaunchingProviders = new ArrayList();
737
738 /**
739 * Global set of specific Uri permissions that have been granted.
740 */
741 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
742 = new SparseArray<HashMap<Uri, UriPermission>>();
743
744 /**
745 * Thread-local storage used to carry caller permissions over through
746 * indirect content-provider access.
747 * @see #ActivityManagerService.openContentUri()
748 */
749 private class Identity {
750 public int pid;
751 public int uid;
752
753 Identity(int _pid, int _uid) {
754 pid = _pid;
755 uid = _uid;
756 }
757 }
758 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
759
760 /**
761 * All information we have collected about the runtime performance of
762 * any user id that can impact battery performance.
763 */
764 final BatteryStatsService mBatteryStatsService;
765
766 /**
767 * information about component usage
768 */
769 final UsageStatsService mUsageStatsService;
770
771 /**
772 * Current configuration information. HistoryRecord objects are given
773 * a reference to this object to indicate which configuration they are
774 * currently running in, so this object must be kept immutable.
775 */
776 Configuration mConfiguration = new Configuration();
777
778 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800779 * Current sequencing integer of the configuration, for skipping old
780 * configurations.
781 */
782 int mConfigurationSeq = 0;
783
784 /**
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700785 * Set when we know we are going to be calling updateConfiguration()
786 * soon, so want to skip intermediate config checks.
787 */
788 boolean mConfigWillChange;
789
790 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700791 * Hardware-reported OpenGLES version.
792 */
793 final int GL_ES_VERSION;
794
795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 * List of initialization arguments to pass to all processes when binding applications to them.
797 * For example, references to the commonly used services.
798 */
799 HashMap<String, IBinder> mAppBindArgs;
800
801 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700802 * Temporary to avoid allocations. Protected by main lock.
803 */
804 final StringBuilder mStringBuilder = new StringBuilder(256);
805
806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 * Used to control how we initialize the service.
808 */
809 boolean mStartRunning = false;
810 ComponentName mTopComponent;
811 String mTopAction;
812 String mTopData;
813 boolean mSystemReady = false;
814 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700815 boolean mWaitingUpdate = false;
816 boolean mDidUpdate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 Context mContext;
819
820 int mFactoryTest;
821
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700822 boolean mCheckedForSetup;
823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700825 * The time at which we will allow normal application switches again,
826 * after a call to {@link #stopAppSwitches()}.
827 */
828 long mAppSwitchesAllowedTime;
829
830 /**
831 * This is set to true after the first switch after mAppSwitchesAllowedTime
832 * is set; any switches after that will clear the time.
833 */
834 boolean mDidAppSwitch;
835
836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 * Set while we are wanting to sleep, to prevent any
838 * activities from being started/resumed.
839 */
840 boolean mSleeping = false;
841
842 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700843 * Set if we are shutting down the system, similar to sleeping.
844 */
845 boolean mShuttingDown = false;
846
847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 * Set when the system is going to sleep, until we have
849 * successfully paused the current activity and released our wake lock.
850 * At that point the system is allowed to actually sleep.
851 */
852 PowerManager.WakeLock mGoingToSleep;
853
854 /**
855 * We don't want to allow the device to go to sleep while in the process
856 * of launching an activity. This is primarily to allow alarm intent
857 * receivers to launch an activity and get that to run before the device
858 * goes back to sleep.
859 */
860 PowerManager.WakeLock mLaunchingActivity;
861
862 /**
863 * Task identifier that activities are currently being started
864 * in. Incremented each time a new task is created.
865 * todo: Replace this with a TokenSpace class that generates non-repeating
866 * integers that won't wrap.
867 */
868 int mCurTask = 1;
869
870 /**
871 * Current sequence id for oom_adj computation traversal.
872 */
873 int mAdjSeq = 0;
874
875 /**
876 * Set to true if the ANDROID_SIMPLE_PROCESS_MANAGEMENT envvar
877 * is set, indicating the user wants processes started in such a way
878 * that they can use ANDROID_PROCESS_WRAPPER and know what will be
879 * running in each process (thus no pre-initialized process, etc).
880 */
881 boolean mSimpleProcessManagement = false;
882
883 /**
884 * System monitoring: number of processes that died since the last
885 * N procs were started.
886 */
887 int[] mProcDeaths = new int[20];
888
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700889 /**
890 * This is set if we had to do a delayed dexopt of an app before launching
891 * it, to increasing the ANR timeouts in that case.
892 */
893 boolean mDidDexOpt;
894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 String mDebugApp = null;
896 boolean mWaitForDebugger = false;
897 boolean mDebugTransient = false;
898 String mOrigDebugApp = null;
899 boolean mOrigWaitForDebugger = false;
900 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700901 IActivityController mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700903 final RemoteCallbackList<IActivityWatcher> mWatchers
904 = new RemoteCallbackList<IActivityWatcher>();
905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 /**
907 * Callback of last caller to {@link #requestPss}.
908 */
909 Runnable mRequestPssCallback;
910
911 /**
912 * Remaining processes for which we are waiting results from the last
913 * call to {@link #requestPss}.
914 */
915 final ArrayList<ProcessRecord> mRequestPssList
916 = new ArrayList<ProcessRecord>();
917
918 /**
919 * Runtime statistics collection thread. This object's lock is used to
920 * protect all related state.
921 */
922 final Thread mProcessStatsThread;
923
924 /**
925 * Used to collect process stats when showing not responding dialog.
926 * Protected by mProcessStatsThread.
927 */
928 final ProcessStats mProcessStats = new ProcessStats(
929 MONITOR_THREAD_CPU_USAGE);
930 long mLastCpuTime = 0;
931 long mLastWriteTime = 0;
932
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700933 long mInitialStartTime = 0;
934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 /**
936 * Set to true after the system has finished booting.
937 */
938 boolean mBooted = false;
939
940 int mProcessLimit = 0;
941
942 WindowManagerService mWindowManager;
943
944 static ActivityManagerService mSelf;
945 static ActivityThread mSystemThread;
946
947 private final class AppDeathRecipient implements IBinder.DeathRecipient {
948 final ProcessRecord mApp;
949 final int mPid;
950 final IApplicationThread mAppThread;
951
952 AppDeathRecipient(ProcessRecord app, int pid,
953 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800954 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 TAG, "New death recipient " + this
956 + " for thread " + thread.asBinder());
957 mApp = app;
958 mPid = pid;
959 mAppThread = thread;
960 }
961
962 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800963 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 TAG, "Death received in " + this
965 + " for thread " + mAppThread.asBinder());
966 removeRequestedPss(mApp);
967 synchronized(ActivityManagerService.this) {
968 appDiedLocked(mApp, mPid, mAppThread);
969 }
970 }
971 }
972
973 static final int SHOW_ERROR_MSG = 1;
974 static final int SHOW_NOT_RESPONDING_MSG = 2;
975 static final int SHOW_FACTORY_ERROR_MSG = 3;
976 static final int UPDATE_CONFIGURATION_MSG = 4;
977 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
978 static final int WAIT_FOR_DEBUGGER_MSG = 6;
979 static final int BROADCAST_INTENT_MSG = 7;
980 static final int BROADCAST_TIMEOUT_MSG = 8;
981 static final int PAUSE_TIMEOUT_MSG = 9;
982 static final int IDLE_TIMEOUT_MSG = 10;
983 static final int IDLE_NOW_MSG = 11;
984 static final int SERVICE_TIMEOUT_MSG = 12;
985 static final int UPDATE_TIME_ZONE = 13;
986 static final int SHOW_UID_ERROR_MSG = 14;
987 static final int IM_FEELING_LUCKY_MSG = 15;
988 static final int LAUNCH_TIMEOUT_MSG = 16;
989 static final int DESTROY_TIMEOUT_MSG = 17;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 static final int RESUME_TOP_ACTIVITY_MSG = 19;
991 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700992 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700993 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800994 static final int FINALIZE_PENDING_INTENT_MSG = 23;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995
996 AlertDialog mUidAlert;
997
998 final Handler mHandler = new Handler() {
999 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001000 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 //}
1002
1003 public void handleMessage(Message msg) {
1004 switch (msg.what) {
1005 case SHOW_ERROR_MSG: {
1006 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 synchronized (ActivityManagerService.this) {
1008 ProcessRecord proc = (ProcessRecord)data.get("app");
1009 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001010 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 return;
1012 }
1013 AppErrorResult res = (AppErrorResult) data.get("result");
Dianne Hackborn55280a92009-05-07 15:53:46 -07001014 if (!mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -08001015 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 d.show();
1017 proc.crashDialog = d;
1018 } else {
1019 // The device is asleep, so just pretend that the user
1020 // saw a crash dialog and hit "force quit".
1021 res.set(0);
1022 }
1023 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001024
1025 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 } break;
1027 case SHOW_NOT_RESPONDING_MSG: {
1028 synchronized (ActivityManagerService.this) {
1029 HashMap data = (HashMap) msg.obj;
1030 ProcessRecord proc = (ProcessRecord)data.get("app");
1031 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001032 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 return;
1034 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001035
1036 broadcastIntentLocked(null, null, new Intent("android.intent.action.ANR"),
1037 null, null, 0, null, null, null,
1038 false, false, MY_PID, Process.SYSTEM_UID);
1039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
1041 mContext, proc, (HistoryRecord)data.get("activity"));
1042 d.show();
1043 proc.anrDialog = d;
1044 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001045
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001046 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 } break;
1048 case SHOW_FACTORY_ERROR_MSG: {
1049 Dialog d = new FactoryErrorDialog(
1050 mContext, msg.getData().getCharSequence("msg"));
1051 d.show();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001052 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 } break;
1054 case UPDATE_CONFIGURATION_MSG: {
1055 final ContentResolver resolver = mContext.getContentResolver();
1056 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
1057 } break;
1058 case GC_BACKGROUND_PROCESSES_MSG: {
1059 synchronized (ActivityManagerService.this) {
1060 performAppGcsIfAppropriateLocked();
1061 }
1062 } break;
1063 case WAIT_FOR_DEBUGGER_MSG: {
1064 synchronized (ActivityManagerService.this) {
1065 ProcessRecord app = (ProcessRecord)msg.obj;
1066 if (msg.arg1 != 0) {
1067 if (!app.waitedForDebugger) {
1068 Dialog d = new AppWaitingForDebuggerDialog(
1069 ActivityManagerService.this,
1070 mContext, app);
1071 app.waitDialog = d;
1072 app.waitedForDebugger = true;
1073 d.show();
1074 }
1075 } else {
1076 if (app.waitDialog != null) {
1077 app.waitDialog.dismiss();
1078 app.waitDialog = null;
1079 }
1080 }
1081 }
1082 } break;
1083 case BROADCAST_INTENT_MSG: {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001084 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 TAG, "Received BROADCAST_INTENT_MSG");
1086 processNextBroadcast(true);
1087 } break;
1088 case BROADCAST_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001089 if (mDidDexOpt) {
1090 mDidDexOpt = false;
1091 Message nmsg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
1092 mHandler.sendMessageDelayed(nmsg, BROADCAST_TIMEOUT);
1093 return;
1094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 broadcastTimeout();
1096 } break;
1097 case PAUSE_TIMEOUT_MSG: {
1098 IBinder token = (IBinder)msg.obj;
1099 // We don't at this point know if the activity is fullscreen,
1100 // so we need to be conservative and assume it isn't.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001101 Slog.w(TAG, "Activity pause timeout for " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 activityPaused(token, null, true);
1103 } break;
1104 case IDLE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001105 if (mDidDexOpt) {
1106 mDidDexOpt = false;
1107 Message nmsg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
1108 nmsg.obj = msg.obj;
1109 mHandler.sendMessageDelayed(nmsg, IDLE_TIMEOUT);
1110 return;
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 // We don't at this point know if the activity is fullscreen,
1113 // so we need to be conservative and assume it isn't.
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001114 IBinder token = (IBinder)msg.obj;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001115 Slog.w(TAG, "Activity idle timeout for " + token);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001116 activityIdleInternal(token, true, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 } break;
1118 case DESTROY_TIMEOUT_MSG: {
1119 IBinder token = (IBinder)msg.obj;
1120 // We don't at this point know if the activity is fullscreen,
1121 // so we need to be conservative and assume it isn't.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001122 Slog.w(TAG, "Activity destroy timeout for " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 activityDestroyed(token);
1124 } break;
1125 case IDLE_NOW_MSG: {
1126 IBinder token = (IBinder)msg.obj;
Dianne Hackborne88846e2009-09-30 21:34:25 -07001127 activityIdle(token, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 } break;
1129 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001130 if (mDidDexOpt) {
1131 mDidDexOpt = false;
1132 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
1133 nmsg.obj = msg.obj;
1134 mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT);
1135 return;
1136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 serviceTimeout((ProcessRecord)msg.obj);
1138 } break;
1139 case UPDATE_TIME_ZONE: {
1140 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001141 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1142 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 if (r.thread != null) {
1144 try {
1145 r.thread.updateTimeZone();
1146 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001147 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149 }
1150 }
1151 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001152 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 case SHOW_UID_ERROR_MSG: {
1154 // XXX This is a temporary dialog, no need to localize.
1155 AlertDialog d = new BaseErrorDialog(mContext);
1156 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1157 d.setCancelable(false);
1158 d.setTitle("System UIDs Inconsistent");
1159 d.setMessage("UIDs on the system are inconsistent, you need to wipe your data partition or your device will be unstable.");
1160 d.setButton("I'm Feeling Lucky",
1161 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1162 mUidAlert = d;
1163 d.show();
1164 } break;
1165 case IM_FEELING_LUCKY_MSG: {
1166 if (mUidAlert != null) {
1167 mUidAlert.dismiss();
1168 mUidAlert = null;
1169 }
1170 } break;
1171 case LAUNCH_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001172 if (mDidDexOpt) {
1173 mDidDexOpt = false;
1174 Message nmsg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
1175 mHandler.sendMessageDelayed(nmsg, LAUNCH_TIMEOUT);
1176 return;
1177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 synchronized (ActivityManagerService.this) {
1179 if (mLaunchingActivity.isHeld()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001180 Slog.w(TAG, "Launch timeout has expired, giving up wake lock!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 mLaunchingActivity.release();
1182 }
1183 }
1184 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 case RESUME_TOP_ACTIVITY_MSG: {
1186 synchronized (ActivityManagerService.this) {
1187 resumeTopActivityLocked(null);
1188 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001189 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001191 if (mDidDexOpt) {
1192 mDidDexOpt = false;
1193 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1194 nmsg.obj = msg.obj;
1195 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1196 return;
1197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 ProcessRecord app = (ProcessRecord)msg.obj;
1199 synchronized (ActivityManagerService.this) {
1200 processStartTimedOutLocked(app);
1201 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001202 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001203 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1204 synchronized (ActivityManagerService.this) {
1205 doPendingActivityLaunchesLocked(true);
1206 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001207 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001208 case KILL_APPLICATION_MSG: {
1209 synchronized (ActivityManagerService.this) {
1210 int uid = msg.arg1;
1211 boolean restart = (msg.arg2 == 1);
1212 String pkg = (String) msg.obj;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001213 forceStopPackageLocked(pkg, uid, restart, false, true);
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001214 }
1215 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001216 case FINALIZE_PENDING_INTENT_MSG: {
1217 ((PendingIntentRecord)msg.obj).completeFinalize();
1218 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220 }
1221 };
1222
1223 public static void setSystemProcess() {
1224 try {
1225 ActivityManagerService m = mSelf;
1226
1227 ServiceManager.addService("activity", m);
1228 ServiceManager.addService("meminfo", new MemBinder(m));
1229 if (MONITOR_CPU_USAGE) {
1230 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 ServiceManager.addService("permission", new PermissionController(m));
1233
1234 ApplicationInfo info =
1235 mSelf.mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -07001236 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001237 mSystemThread.installSystemApplicationInfo(info);
1238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 synchronized (mSelf) {
1240 ProcessRecord app = mSelf.newProcessRecordLocked(
1241 mSystemThread.getApplicationThread(), info,
1242 info.processName);
1243 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001244 app.pid = MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 app.maxAdj = SYSTEM_ADJ;
1246 mSelf.mProcessNames.put(app.processName, app.info.uid, app);
1247 synchronized (mSelf.mPidsSelfLocked) {
1248 mSelf.mPidsSelfLocked.put(app.pid, app);
1249 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001250 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 }
1252 } catch (PackageManager.NameNotFoundException e) {
1253 throw new RuntimeException(
1254 "Unable to find android system package", e);
1255 }
1256 }
1257
1258 public void setWindowManager(WindowManagerService wm) {
1259 mWindowManager = wm;
1260 }
1261
1262 public static final Context main(int factoryTest) {
1263 AThread thr = new AThread();
1264 thr.start();
1265
1266 synchronized (thr) {
1267 while (thr.mService == null) {
1268 try {
1269 thr.wait();
1270 } catch (InterruptedException e) {
1271 }
1272 }
1273 }
1274
1275 ActivityManagerService m = thr.mService;
1276 mSelf = m;
1277 ActivityThread at = ActivityThread.systemMain();
1278 mSystemThread = at;
1279 Context context = at.getSystemContext();
1280 m.mContext = context;
1281 m.mFactoryTest = factoryTest;
1282 PowerManager pm =
1283 (PowerManager)context.getSystemService(Context.POWER_SERVICE);
1284 m.mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");
1285 m.mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Launch");
1286 m.mLaunchingActivity.setReferenceCounted(false);
1287
1288 m.mBatteryStatsService.publish(context);
1289 m.mUsageStatsService.publish(context);
1290
1291 synchronized (thr) {
1292 thr.mReady = true;
1293 thr.notifyAll();
1294 }
1295
1296 m.startRunning(null, null, null, null);
1297
1298 return context;
1299 }
1300
1301 public static ActivityManagerService self() {
1302 return mSelf;
1303 }
1304
1305 static class AThread extends Thread {
1306 ActivityManagerService mService;
1307 boolean mReady = false;
1308
1309 public AThread() {
1310 super("ActivityManager");
1311 }
1312
1313 public void run() {
1314 Looper.prepare();
1315
1316 android.os.Process.setThreadPriority(
1317 android.os.Process.THREAD_PRIORITY_FOREGROUND);
1318
1319 ActivityManagerService m = new ActivityManagerService();
1320
1321 synchronized (this) {
1322 mService = m;
1323 notifyAll();
1324 }
1325
1326 synchronized (this) {
1327 while (!mReady) {
1328 try {
1329 wait();
1330 } catch (InterruptedException e) {
1331 }
1332 }
1333 }
1334
1335 Looper.loop();
1336 }
1337 }
1338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 static class MemBinder extends Binder {
1340 ActivityManagerService mActivityManagerService;
1341 MemBinder(ActivityManagerService activityManagerService) {
1342 mActivityManagerService = activityManagerService;
1343 }
1344
1345 @Override
1346 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1347 ActivityManagerService service = mActivityManagerService;
1348 ArrayList<ProcessRecord> procs;
1349 synchronized (mActivityManagerService) {
1350 if (args != null && args.length > 0
1351 && args[0].charAt(0) != '-') {
1352 procs = new ArrayList<ProcessRecord>();
1353 int pid = -1;
1354 try {
1355 pid = Integer.parseInt(args[0]);
1356 } catch (NumberFormatException e) {
1357
1358 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001359 for (int i=service.mLruProcesses.size()-1; i>=0; i--) {
1360 ProcessRecord proc = service.mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 if (proc.pid == pid) {
1362 procs.add(proc);
1363 } else if (proc.processName.equals(args[0])) {
1364 procs.add(proc);
1365 }
1366 }
1367 if (procs.size() <= 0) {
1368 pw.println("No process found for: " + args[0]);
1369 return;
1370 }
1371 } else {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001372 procs = service.mLruProcesses;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 }
1374 }
1375 dumpApplicationMemoryUsage(fd, pw, procs, " ", args);
1376 }
1377 }
1378
1379 static class CpuBinder extends Binder {
1380 ActivityManagerService mActivityManagerService;
1381 CpuBinder(ActivityManagerService activityManagerService) {
1382 mActivityManagerService = activityManagerService;
1383 }
1384
1385 @Override
1386 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1387 synchronized (mActivityManagerService.mProcessStatsThread) {
1388 pw.print(mActivityManagerService.mProcessStats.printCurrentState());
1389 }
1390 }
1391 }
1392
1393 private ActivityManagerService() {
1394 String v = System.getenv("ANDROID_SIMPLE_PROCESS_MANAGEMENT");
1395 if (v != null && Integer.getInteger(v) != 0) {
1396 mSimpleProcessManagement = true;
1397 }
1398 v = System.getenv("ANDROID_DEBUG_APP");
1399 if (v != null) {
1400 mSimpleProcessManagement = true;
1401 }
1402
Joe Onorato8a9b2202010-02-26 18:56:32 -08001403 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 File dataDir = Environment.getDataDirectory();
1406 File systemDir = new File(dataDir, "system");
1407 systemDir.mkdirs();
1408 mBatteryStatsService = new BatteryStatsService(new File(
1409 systemDir, "batterystats.bin").toString());
1410 mBatteryStatsService.getActiveStatistics().readLocked();
1411 mBatteryStatsService.getActiveStatistics().writeLocked();
1412
1413 mUsageStatsService = new UsageStatsService( new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001414 systemDir, "usagestats").toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001415
Jack Palevichb90d28c2009-07-22 15:35:24 -07001416 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1417 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1418
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001419 mConfiguration.setToDefaults();
1420 mConfiguration.locale = Locale.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 mProcessStats.init();
1422
1423 // Add ourself to the Watchdog monitors.
1424 Watchdog.getInstance().addMonitor(this);
1425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 mProcessStatsThread = new Thread("ProcessStats") {
1427 public void run() {
1428 while (true) {
1429 try {
1430 try {
1431 synchronized(this) {
1432 final long now = SystemClock.uptimeMillis();
1433 long nextCpuDelay = (mLastCpuTime+MONITOR_CPU_MAX_TIME)-now;
1434 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001435 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 // + ", write delay=" + nextWriteDelay);
1437 if (nextWriteDelay < nextCpuDelay) {
1438 nextCpuDelay = nextWriteDelay;
1439 }
1440 if (nextCpuDelay > 0) {
1441 this.wait(nextCpuDelay);
1442 }
1443 }
1444 } catch (InterruptedException e) {
1445 }
1446
1447 updateCpuStatsNow();
1448 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452 }
1453 };
1454 mProcessStatsThread.start();
1455 }
1456
1457 @Override
1458 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1459 throws RemoteException {
1460 try {
1461 return super.onTransact(code, data, reply, flags);
1462 } catch (RuntimeException e) {
1463 // The activity manager only throws security exceptions, so let's
1464 // log all others.
1465 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001466 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 }
1468 throw e;
1469 }
1470 }
1471
1472 void updateCpuStats() {
1473 synchronized (mProcessStatsThread) {
1474 final long now = SystemClock.uptimeMillis();
1475 if (mLastCpuTime < (now-MONITOR_CPU_MIN_TIME)) {
1476 mProcessStatsThread.notify();
1477 }
1478 }
1479 }
1480
1481 void updateCpuStatsNow() {
1482 synchronized (mProcessStatsThread) {
1483 final long now = SystemClock.uptimeMillis();
1484 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 if (MONITOR_CPU_USAGE &&
1487 mLastCpuTime < (now-MONITOR_CPU_MIN_TIME)) {
1488 mLastCpuTime = now;
1489 haveNewCpuStats = true;
1490 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001491 //Slog.i(TAG, mProcessStats.printCurrentState());
1492 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 // + mProcessStats.getTotalCpuPercent() + "%");
1494
Joe Onorato8a9b2202010-02-26 18:56:32 -08001495 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 if ("true".equals(SystemProperties.get("events.cpu"))) {
1497 int user = mProcessStats.getLastUserTime();
1498 int system = mProcessStats.getLastSystemTime();
1499 int iowait = mProcessStats.getLastIoWaitTime();
1500 int irq = mProcessStats.getLastIrqTime();
1501 int softIrq = mProcessStats.getLastSoftIrqTime();
1502 int idle = mProcessStats.getLastIdleTime();
1503
1504 int total = user + system + iowait + irq + softIrq + idle;
1505 if (total == 0) total = 1;
1506
Doug Zongker2bec3d42009-12-04 12:52:44 -08001507 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 ((user+system+iowait+irq+softIrq) * 100) / total,
1509 (user * 100) / total,
1510 (system * 100) / total,
1511 (iowait * 100) / total,
1512 (irq * 100) / total,
1513 (softIrq * 100) / total);
1514 }
1515 }
1516
Amith Yamasanie43530a2009-08-21 13:11:37 -07001517 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001518 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001519 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 synchronized(mPidsSelfLocked) {
1521 if (haveNewCpuStats) {
1522 if (mBatteryStatsService.isOnBattery()) {
1523 final int N = mProcessStats.countWorkingStats();
1524 for (int i=0; i<N; i++) {
1525 ProcessStats.Stats st
1526 = mProcessStats.getWorkingStats(i);
1527 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
1528 if (pr != null) {
1529 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
1530 ps.addCpuTimeLocked(st.rel_utime, st.rel_stime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001531 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001532 } else {
1533 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001534 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001535 if (ps != null) {
1536 ps.addCpuTimeLocked(st.rel_utime, st.rel_stime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001537 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 }
1540 }
1541 }
1542 }
1543 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1546 mLastWriteTime = now;
1547 mBatteryStatsService.getActiveStatistics().writeLocked();
1548 }
1549 }
1550 }
1551 }
1552
1553 /**
1554 * Initialize the application bind args. These are passed to each
1555 * process when the bindApplication() IPC is sent to the process. They're
1556 * lazily setup to make sure the services are running when they're asked for.
1557 */
1558 private HashMap<String, IBinder> getCommonServicesLocked() {
1559 if (mAppBindArgs == null) {
1560 mAppBindArgs = new HashMap<String, IBinder>();
1561
1562 // Setup the application init args
1563 mAppBindArgs.put("package", ServiceManager.getService("package"));
1564 mAppBindArgs.put("window", ServiceManager.getService("window"));
1565 mAppBindArgs.put(Context.ALARM_SERVICE,
1566 ServiceManager.getService(Context.ALARM_SERVICE));
1567 }
1568 return mAppBindArgs;
1569 }
1570
1571 private final void setFocusedActivityLocked(HistoryRecord r) {
1572 if (mFocusedActivity != r) {
1573 mFocusedActivity = r;
1574 mWindowManager.setFocusedApp(r, true);
1575 }
1576 }
1577
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001578 private final void updateLruProcessLocked(ProcessRecord app,
1579 boolean oomAdj, boolean updateActivityTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001581 int lrui = mLruProcesses.indexOf(app);
1582 if (lrui >= 0) mLruProcesses.remove(lrui);
1583
1584 int i = mLruProcesses.size()-1;
1585 int skipTop = 0;
1586
1587 // compute the new weight for this process.
1588 if (updateActivityTime) {
1589 app.lastActivityTime = SystemClock.uptimeMillis();
1590 }
1591 if (app.activities.size() > 0) {
1592 // If this process has activities, we more strongly want to keep
1593 // it around.
1594 app.lruWeight = app.lastActivityTime;
1595 } else if (app.pubProviders.size() > 0) {
1596 // If this process contains content providers, we want to keep
1597 // it a little more strongly.
1598 app.lruWeight = app.lastActivityTime - CONTENT_APP_IDLE_OFFSET;
1599 // Also don't let it kick out the first few "real" hidden processes.
1600 skipTop = MIN_HIDDEN_APPS;
1601 } else {
1602 // If this process doesn't have activities, we less strongly
1603 // want to keep it around, and generally want to avoid getting
1604 // in front of any very recently used activities.
1605 app.lruWeight = app.lastActivityTime - EMPTY_APP_IDLE_OFFSET;
1606 // Also don't let it kick out the first few "real" hidden processes.
1607 skipTop = MIN_HIDDEN_APPS;
1608 }
1609 while (i >= 0) {
1610 ProcessRecord p = mLruProcesses.get(i);
1611 // If this app shouldn't be in front of the first N background
1612 // apps, then skip over that many that are currently hidden.
1613 if (skipTop > 0 && p.setAdj >= HIDDEN_APP_MIN_ADJ) {
1614 skipTop--;
1615 }
1616 if (p.lruWeight <= app.lruWeight){
1617 mLruProcesses.add(i+1, app);
1618 break;
1619 }
1620 i--;
1621 }
1622 if (i < 0) {
1623 mLruProcesses.add(0, app);
1624 }
1625
Joe Onorato8a9b2202010-02-26 18:56:32 -08001626 //Slog.i(TAG, "Putting proc to front: " + app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 if (oomAdj) {
1628 updateOomAdjLocked();
1629 }
1630 }
1631
1632 private final boolean updateLRUListLocked(HistoryRecord r) {
1633 final boolean hadit = mLRUActivities.remove(r);
1634 mLRUActivities.add(r);
1635 return hadit;
1636 }
1637
1638 private final HistoryRecord topRunningActivityLocked(HistoryRecord notTop) {
1639 int i = mHistory.size()-1;
1640 while (i >= 0) {
1641 HistoryRecord r = (HistoryRecord)mHistory.get(i);
1642 if (!r.finishing && r != notTop) {
1643 return r;
1644 }
1645 i--;
1646 }
1647 return null;
1648 }
1649
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001650 private final HistoryRecord topRunningNonDelayedActivityLocked(HistoryRecord notTop) {
1651 int i = mHistory.size()-1;
1652 while (i >= 0) {
1653 HistoryRecord r = (HistoryRecord)mHistory.get(i);
1654 if (!r.finishing && !r.delayedResume && r != notTop) {
1655 return r;
1656 }
1657 i--;
1658 }
1659 return null;
1660 }
1661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 /**
1663 * This is a simplified version of topRunningActivityLocked that provides a number of
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001664 * optional skip-over modes. It is intended for use with the ActivityController hook only.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 *
1666 * @param token If non-null, any history records matching this token will be skipped.
1667 * @param taskId If non-zero, we'll attempt to skip over records with the same task ID.
1668 *
1669 * @return Returns the HistoryRecord of the next activity on the stack.
1670 */
1671 private final HistoryRecord topRunningActivityLocked(IBinder token, int taskId) {
1672 int i = mHistory.size()-1;
1673 while (i >= 0) {
1674 HistoryRecord r = (HistoryRecord)mHistory.get(i);
1675 // Note: the taskId check depends on real taskId fields being non-zero
1676 if (!r.finishing && (token != r) && (taskId != r.task.taskId)) {
1677 return r;
1678 }
1679 i--;
1680 }
1681 return null;
1682 }
1683
1684 private final ProcessRecord getProcessRecordLocked(
1685 String processName, int uid) {
1686 if (uid == Process.SYSTEM_UID) {
1687 // The system gets to run in any process. If there are multiple
1688 // processes with the same uid, just pick the first (this
1689 // should never happen).
1690 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1691 processName);
1692 return procs != null ? procs.valueAt(0) : null;
1693 }
1694 ProcessRecord proc = mProcessNames.get(processName, uid);
1695 return proc;
1696 }
1697
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001698 private void ensurePackageDexOpt(String packageName) {
1699 IPackageManager pm = ActivityThread.getPackageManager();
1700 try {
1701 if (pm.performDexOpt(packageName)) {
1702 mDidDexOpt = true;
1703 }
1704 } catch (RemoteException e) {
1705 }
1706 }
1707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 private boolean isNextTransitionForward() {
1709 int transit = mWindowManager.getPendingAppTransition();
1710 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1711 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1712 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1713 }
1714
1715 private final boolean realStartActivityLocked(HistoryRecord r,
1716 ProcessRecord app, boolean andResume, boolean checkConfig)
1717 throws RemoteException {
1718
1719 r.startFreezingScreenLocked(app, 0);
1720 mWindowManager.setAppVisibility(r, true);
1721
1722 // Have the window manager re-evaluate the orientation of
1723 // the screen based on the new activity order. Note that
1724 // as a result of this, it can call back into the activity
1725 // manager with a new orientation. We don't care about that,
1726 // because the activity is not currently running so we are
1727 // just restarting it anyway.
1728 if (checkConfig) {
1729 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07001730 mConfiguration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 r.mayFreezeScreenLocked(app) ? r : null);
1732 updateConfigurationLocked(config, r);
1733 }
1734
1735 r.app = app;
1736
Joe Onorato8a9b2202010-02-26 18:56:32 -08001737 if (localLOGV) Slog.v(TAG, "Launching: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738
1739 int idx = app.activities.indexOf(r);
1740 if (idx < 0) {
1741 app.activities.add(r);
1742 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001743 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744
1745 try {
1746 if (app.thread == null) {
1747 throw new RemoteException();
1748 }
1749 List<ResultInfo> results = null;
1750 List<Intent> newIntents = null;
1751 if (andResume) {
1752 results = r.results;
1753 newIntents = r.newIntents;
1754 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001755 if (DEBUG_SWITCH) Slog.v(TAG, "Launching: " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001756 + " icicle=" + r.icicle
1757 + " with results=" + results + " newIntents=" + newIntents
1758 + " andResume=" + andResume);
1759 if (andResume) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08001760 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001761 System.identityHashCode(r),
1762 r.task.taskId, r.shortComponentName);
1763 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001764 if (r.isHomeActivity) {
1765 mHomeProcess = app;
1766 }
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001767 ensurePackageDexOpt(r.intent.getComponent().getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 app.thread.scheduleLaunchActivity(new Intent(r.intent), r,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001769 System.identityHashCode(r),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 r.info, r.icicle, results, newIntents, !andResume,
1771 isNextTransitionForward());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 } catch (RemoteException e) {
1773 if (r.launchFailed) {
1774 // This is the second time we failed -- finish activity
1775 // and give up.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001776 Slog.e(TAG, "Second failure launching "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 + r.intent.getComponent().flattenToShortString()
1778 + ", giving up", e);
1779 appDiedLocked(app, app.pid, app.thread);
1780 requestFinishActivityLocked(r, Activity.RESULT_CANCELED, null,
1781 "2nd-crash");
1782 return false;
1783 }
1784
1785 // This is the first time we failed -- restart process and
1786 // retry.
1787 app.activities.remove(r);
1788 throw e;
1789 }
1790
1791 r.launchFailed = false;
1792 if (updateLRUListLocked(r)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001793 Slog.w(TAG, "Activity " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 + " being launched, but already in LRU list");
1795 }
1796
1797 if (andResume) {
1798 // As part of the process of launching, ActivityThread also performs
1799 // a resume.
1800 r.state = ActivityState.RESUMED;
1801 r.icicle = null;
1802 r.haveState = false;
1803 r.stopped = false;
1804 mResumedActivity = r;
1805 r.task.touchActiveTime();
1806 completeResumeLocked(r);
1807 pauseIfSleepingLocked();
1808 } else {
1809 // This activity is not starting in the resumed state... which
1810 // should look like we asked it to pause+stop (but remain visible),
1811 // and it has done so and reported back the current icicle and
1812 // other state.
1813 r.state = ActivityState.STOPPED;
1814 r.stopped = true;
1815 }
1816
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07001817 // Launch the new version setup screen if needed. We do this -after-
1818 // launching the initial activity (that is, home), so that it can have
1819 // a chance to initialize itself while in the background, making the
1820 // switch back to it faster and look better.
1821 startSetupActivityLocked();
1822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 return true;
1824 }
1825
1826 private final void startSpecificActivityLocked(HistoryRecord r,
1827 boolean andResume, boolean checkConfig) {
1828 // Is this activity's application already running?
1829 ProcessRecord app = getProcessRecordLocked(r.processName,
1830 r.info.applicationInfo.uid);
1831
1832 if (r.startTime == 0) {
1833 r.startTime = SystemClock.uptimeMillis();
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07001834 if (mInitialStartTime == 0) {
1835 mInitialStartTime = r.startTime;
1836 }
1837 } else if (mInitialStartTime == 0) {
1838 mInitialStartTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 }
1840
1841 if (app != null && app.thread != null) {
1842 try {
1843 realStartActivityLocked(r, app, andResume, checkConfig);
1844 return;
1845 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001846 Slog.w(TAG, "Exception when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 + r.intent.getComponent().flattenToShortString(), e);
1848 }
1849
1850 // If a dead object exception was thrown -- fall through to
1851 // restart the application.
1852 }
1853
1854 startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001855 "activity", r.intent.getComponent(), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
1857
1858 private final ProcessRecord startProcessLocked(String processName,
1859 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001860 String hostingType, ComponentName hostingName, boolean allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 ProcessRecord app = getProcessRecordLocked(processName, info.uid);
1862 // We don't have to do anything more if:
1863 // (1) There is an existing application record; and
1864 // (2) The caller doesn't think it is dead, OR there is no thread
1865 // object attached to it so we know it couldn't have crashed; and
1866 // (3) There is a pid assigned to it, so it is either starting or
1867 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001868 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 + " app=" + app + " knownToBeDead=" + knownToBeDead
1870 + " thread=" + (app != null ? app.thread : null)
1871 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001872 if (app != null && app.pid > 0) {
1873 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001874 // We already have the app running, or are waiting for it to
1875 // come up (we have a pid but not yet its thread), so keep it.
Magnus Edlund7bb25812010-02-24 15:45:06 +01001876 return app;
1877 } else {
1878 // An application record is attached to a previous process,
1879 // clean it up now.
1880 handleAppDiedLocked(app, true);
1881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 String hostingNameStr = hostingName != null
1885 ? hostingName.flattenToShortString() : null;
1886
1887 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1888 // If we are in the background, then check to see if this process
1889 // is bad. If so, we will just silently fail.
1890 if (mBadProcesses.get(info.processName, info.uid) != null) {
1891 return null;
1892 }
1893 } else {
1894 // When the user is explicitly starting a process, then clear its
1895 // crash count so that we won't make it bad until they see at
1896 // least one crash dialog again, and make the process good again
1897 // if it had been bad.
1898 mProcessCrashTimes.remove(info.processName, info.uid);
1899 if (mBadProcesses.get(info.processName, info.uid) != null) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08001900 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 info.processName);
1902 mBadProcesses.remove(info.processName, info.uid);
1903 if (app != null) {
1904 app.bad = false;
1905 }
1906 }
1907 }
1908
1909 if (app == null) {
1910 app = newProcessRecordLocked(null, info, processName);
1911 mProcessNames.put(processName, info.uid, app);
1912 } else {
1913 // If this is a new package in the process, add the package to the list
1914 app.addPackage(info.packageName);
1915 }
1916
1917 // If the system is not ready yet, then hold off on starting this
1918 // process until it is.
1919 if (!mSystemReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001920 && !isAllowedWhileBooting(info)
1921 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 if (!mProcessesOnHold.contains(app)) {
1923 mProcessesOnHold.add(app);
1924 }
1925 return app;
1926 }
1927
1928 startProcessLocked(app, hostingType, hostingNameStr);
1929 return (app.pid != 0) ? app : null;
1930 }
1931
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001932 boolean isAllowedWhileBooting(ApplicationInfo ai) {
1933 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
1934 }
1935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 private final void startProcessLocked(ProcessRecord app,
1937 String hostingType, String hostingNameStr) {
1938 if (app.pid > 0 && app.pid != MY_PID) {
1939 synchronized (mPidsSelfLocked) {
1940 mPidsSelfLocked.remove(app.pid);
1941 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
1942 }
1943 app.pid = 0;
1944 }
1945
1946 mProcessesOnHold.remove(app);
1947
1948 updateCpuStats();
1949
1950 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
1951 mProcDeaths[0] = 0;
1952
1953 try {
1954 int uid = app.info.uid;
1955 int[] gids = null;
1956 try {
1957 gids = mContext.getPackageManager().getPackageGids(
1958 app.info.packageName);
1959 } catch (PackageManager.NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001960 Slog.w(TAG, "Unable to retrieve gids", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
1963 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
1964 && mTopComponent != null
1965 && app.processName.equals(mTopComponent.getPackageName())) {
1966 uid = 0;
1967 }
1968 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
1969 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
1970 uid = 0;
1971 }
1972 }
1973 int debugFlags = 0;
1974 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
1975 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
1976 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08001977 // Run the app in safe mode if its manifest requests so or the
1978 // system is booted in safe mode.
1979 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
1980 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08001981 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
1982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
1984 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
1985 }
1986 if ("1".equals(SystemProperties.get("debug.assert"))) {
1987 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
1988 }
1989 int pid = Process.start("android.app.ActivityThread",
1990 mSimpleProcessManagement ? app.processName : null, uid, uid,
1991 gids, debugFlags, null);
1992 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
1993 synchronized (bs) {
1994 if (bs.isOnBattery()) {
1995 app.batteryStats.incStartsLocked();
1996 }
1997 }
1998
Doug Zongker2bec3d42009-12-04 12:52:44 -08001999 EventLog.writeEvent(EventLogTags.AM_PROC_START, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 app.processName, hostingType,
2001 hostingNameStr != null ? hostingNameStr : "");
2002
2003 if (app.persistent) {
2004 Watchdog.getInstance().processStarted(app, app.processName, pid);
2005 }
2006
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002007 StringBuilder buf = mStringBuilder;
2008 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 buf.append("Start proc ");
2010 buf.append(app.processName);
2011 buf.append(" for ");
2012 buf.append(hostingType);
2013 if (hostingNameStr != null) {
2014 buf.append(" ");
2015 buf.append(hostingNameStr);
2016 }
2017 buf.append(": pid=");
2018 buf.append(pid);
2019 buf.append(" uid=");
2020 buf.append(uid);
2021 buf.append(" gids={");
2022 if (gids != null) {
2023 for (int gi=0; gi<gids.length; gi++) {
2024 if (gi != 0) buf.append(", ");
2025 buf.append(gids[gi]);
2026
2027 }
2028 }
2029 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08002030 Slog.i(TAG, buf.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 if (pid == 0 || pid == MY_PID) {
2032 // Processes are being emulated with threads.
2033 app.pid = MY_PID;
2034 app.removed = false;
2035 mStartingProcesses.add(app);
2036 } else if (pid > 0) {
2037 app.pid = pid;
2038 app.removed = false;
2039 synchronized (mPidsSelfLocked) {
2040 this.mPidsSelfLocked.put(pid, app);
2041 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
2042 msg.obj = app;
2043 mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT);
2044 }
2045 } else {
2046 app.pid = 0;
2047 RuntimeException e = new RuntimeException(
2048 "Failure starting process " + app.processName
2049 + ": returned pid=" + pid);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002050 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052 } catch (RuntimeException e) {
2053 // XXX do better error recovery.
2054 app.pid = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002055 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057 }
2058
2059 private final void startPausingLocked(boolean userLeaving, boolean uiSleeping) {
2060 if (mPausingActivity != null) {
2061 RuntimeException e = new RuntimeException();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002062 Slog.e(TAG, "Trying to pause when pause is already pending for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 + mPausingActivity, e);
2064 }
2065 HistoryRecord prev = mResumedActivity;
2066 if (prev == null) {
2067 RuntimeException e = new RuntimeException();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002068 Slog.e(TAG, "Trying to pause when nothing is resumed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 resumeTopActivityLocked(null);
2070 return;
2071 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002072 if (DEBUG_PAUSE) Slog.v(TAG, "Start pausing: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 mResumedActivity = null;
2074 mPausingActivity = prev;
2075 mLastPausedActivity = prev;
2076 prev.state = ActivityState.PAUSING;
2077 prev.task.touchActiveTime();
2078
2079 updateCpuStats();
2080
2081 if (prev.app != null && prev.app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002082 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending pause: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 try {
Doug Zongker2bec3d42009-12-04 12:52:44 -08002084 EventLog.writeEvent(EventLogTags.AM_PAUSE_ACTIVITY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 System.identityHashCode(prev),
2086 prev.shortComponentName);
2087 prev.app.thread.schedulePauseActivity(prev, prev.finishing, userLeaving,
2088 prev.configChangeFlags);
2089 updateUsageStats(prev, false);
2090 } catch (Exception e) {
2091 // Ignore exception, if process died other code will cleanup.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002092 Slog.w(TAG, "Exception thrown during pause", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 mPausingActivity = null;
2094 mLastPausedActivity = null;
2095 }
2096 } else {
2097 mPausingActivity = null;
2098 mLastPausedActivity = null;
2099 }
2100
2101 // If we are not going to sleep, we want to ensure the device is
2102 // awake until the next activity is started.
Dianne Hackborn55280a92009-05-07 15:53:46 -07002103 if (!mSleeping && !mShuttingDown) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 mLaunchingActivity.acquire();
2105 if (!mHandler.hasMessages(LAUNCH_TIMEOUT_MSG)) {
2106 // To be safe, don't allow the wake lock to be held for too long.
2107 Message msg = mHandler.obtainMessage(LAUNCH_TIMEOUT_MSG);
2108 mHandler.sendMessageDelayed(msg, LAUNCH_TIMEOUT);
2109 }
2110 }
2111
2112
2113 if (mPausingActivity != null) {
2114 // Have the window manager pause its key dispatching until the new
2115 // activity has started. If we're pausing the activity just because
2116 // the screen is being turned off and the UI is sleeping, don't interrupt
2117 // key dispatch; the same activity will pick it up again on wakeup.
2118 if (!uiSleeping) {
2119 prev.pauseKeyDispatchingLocked();
2120 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002121 if (DEBUG_PAUSE) Slog.v(TAG, "Key dispatch not paused for screen off");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123
2124 // Schedule a pause timeout in case the app doesn't respond.
2125 // We don't give it much time because this directly impacts the
2126 // responsiveness seen by the user.
2127 Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG);
2128 msg.obj = prev;
2129 mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002130 if (DEBUG_PAUSE) Slog.v(TAG, "Waiting for pause to complete...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 } else {
2132 // This activity failed to schedule the
2133 // pause, so just treat it as being paused now.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002134 if (DEBUG_PAUSE) Slog.v(TAG, "Activity not running, resuming next.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 resumeTopActivityLocked(null);
2136 }
2137 }
2138
2139 private final void completePauseLocked() {
2140 HistoryRecord prev = mPausingActivity;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002141 if (DEBUG_PAUSE) Slog.v(TAG, "Complete pause: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142
2143 if (prev != null) {
2144 if (prev.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002145 if (DEBUG_PAUSE) Slog.v(TAG, "Executing finish of activity: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 prev = finishCurrentActivityLocked(prev, FINISH_AFTER_VISIBLE);
2147 } else if (prev.app != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002148 if (DEBUG_PAUSE) Slog.v(TAG, "Enqueueing pending stop: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 if (prev.waitingVisible) {
2150 prev.waitingVisible = false;
2151 mWaitingVisibleActivities.remove(prev);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002152 if (DEBUG_SWITCH || DEBUG_PAUSE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 TAG, "Complete pause, no longer waiting: " + prev);
2154 }
2155 if (prev.configDestroy) {
2156 // The previous is being paused because the configuration
2157 // is changing, which means it is actually stopping...
2158 // To juggle the fact that we are also starting a new
2159 // instance right now, we need to first completely stop
2160 // the current instance before starting the new one.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002161 if (DEBUG_PAUSE) Slog.v(TAG, "Destroying after pause: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 destroyActivityLocked(prev, true);
2163 } else {
2164 mStoppingActivities.add(prev);
2165 if (mStoppingActivities.size() > 3) {
2166 // If we already have a few activities waiting to stop,
2167 // then give up on things going idle and start clearing
2168 // them out.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002169 if (DEBUG_PAUSE) Slog.v(TAG, "To many pending stops, forcing idle");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 Message msg = Message.obtain();
2171 msg.what = ActivityManagerService.IDLE_NOW_MSG;
2172 mHandler.sendMessage(msg);
2173 }
2174 }
2175 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002176 if (DEBUG_PAUSE) Slog.v(TAG, "App died during pause, not stopping: " + prev);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 prev = null;
2178 }
2179 mPausingActivity = null;
2180 }
2181
Dianne Hackborn55280a92009-05-07 15:53:46 -07002182 if (!mSleeping && !mShuttingDown) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 resumeTopActivityLocked(prev);
2184 } else {
2185 if (mGoingToSleep.isHeld()) {
2186 mGoingToSleep.release();
2187 }
Dianne Hackborn55280a92009-05-07 15:53:46 -07002188 if (mShuttingDown) {
2189 notifyAll();
2190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 }
2192
2193 if (prev != null) {
2194 prev.resumeKeyDispatchingLocked();
2195 }
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002196
2197 if (prev.app != null && prev.cpuTimeAtResume > 0 && mBatteryStatsService.isOnBattery()) {
2198 long diff = 0;
2199 synchronized (mProcessStatsThread) {
2200 diff = mProcessStats.getCpuTimeForPid(prev.app.pid) - prev.cpuTimeAtResume;
2201 }
2202 if (diff > 0) {
2203 BatteryStatsImpl bsi = mBatteryStatsService.getActiveStatistics();
2204 synchronized (bsi) {
2205 BatteryStatsImpl.Uid.Proc ps =
2206 bsi.getProcessStatsLocked(prev.info.applicationInfo.uid,
2207 prev.info.packageName);
2208 if (ps != null) {
2209 ps.addForegroundTimeLocked(diff);
2210 }
2211 }
2212 }
2213 }
2214 prev.cpuTimeAtResume = 0; // reset it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
2216
2217 /**
2218 * Once we know that we have asked an application to put an activity in
2219 * the resumed state (either by launching it or explicitly telling it),
2220 * this function updates the rest of our state to match that fact.
2221 */
2222 private final void completeResumeLocked(HistoryRecord next) {
2223 next.idle = false;
2224 next.results = null;
2225 next.newIntents = null;
2226
2227 // schedule an idle timeout in case the app doesn't do it for us.
2228 Message msg = mHandler.obtainMessage(IDLE_TIMEOUT_MSG);
2229 msg.obj = next;
2230 mHandler.sendMessageDelayed(msg, IDLE_TIMEOUT);
2231
2232 if (false) {
2233 // The activity was never told to pause, so just keep
2234 // things going as-is. To maintain our own state,
2235 // we need to emulate it coming back and saying it is
2236 // idle.
2237 msg = mHandler.obtainMessage(IDLE_NOW_MSG);
2238 msg.obj = next;
2239 mHandler.sendMessage(msg);
2240 }
2241
Dianne Hackborn1bcf5a82009-09-30 15:22:29 -07002242 reportResumedActivityLocked(next);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 next.thumbnail = null;
2245 setFocusedActivityLocked(next);
2246 next.resumeKeyDispatchingLocked();
2247 ensureActivitiesVisibleLocked(null, 0);
2248 mWindowManager.executeAppTransition();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002249 mNoAnimActivities.clear();
Amith Yamasanieaeb6632009-06-03 15:16:10 -07002250
2251 // Mark the point when the activity is resuming
2252 // TODO: To be more accurate, the mark should be before the onCreate,
2253 // not after the onResume. But for subsequent starts, onResume is fine.
2254 if (next.app != null) {
2255 synchronized (mProcessStatsThread) {
2256 next.cpuTimeAtResume = mProcessStats.getCpuTimeForPid(next.app.pid);
2257 }
2258 } else {
2259 next.cpuTimeAtResume = 0; // Couldn't get the cpu time of process
2260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 }
2262
2263 /**
2264 * Make sure that all activities that need to be visible (that is, they
2265 * currently can be seen by the user) actually are.
2266 */
2267 private final void ensureActivitiesVisibleLocked(HistoryRecord top,
2268 HistoryRecord starting, String onlyThisProcess, int configChanges) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002269 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002270 TAG, "ensureActivitiesVisible behind " + top
2271 + " configChanges=0x" + Integer.toHexString(configChanges));
2272
2273 // If the top activity is not fullscreen, then we need to
2274 // make sure any activities under it are now visible.
2275 final int count = mHistory.size();
2276 int i = count-1;
2277 while (mHistory.get(i) != top) {
2278 i--;
2279 }
2280 HistoryRecord r;
2281 boolean behindFullscreen = false;
2282 for (; i>=0; i--) {
2283 r = (HistoryRecord)mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002284 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 TAG, "Make visible? " + r + " finishing=" + r.finishing
2286 + " state=" + r.state);
2287 if (r.finishing) {
2288 continue;
2289 }
2290
2291 final boolean doThisProcess = onlyThisProcess == null
2292 || onlyThisProcess.equals(r.processName);
2293
2294 // First: if this is not the current activity being started, make
2295 // sure it matches the current configuration.
2296 if (r != starting && doThisProcess) {
2297 ensureActivityConfigurationLocked(r, 0);
2298 }
2299
2300 if (r.app == null || r.app.thread == null) {
2301 if (onlyThisProcess == null
2302 || onlyThisProcess.equals(r.processName)) {
2303 // This activity needs to be visible, but isn't even
2304 // running... get it started, but don't resume it
2305 // at this point.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002306 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 TAG, "Start and freeze screen for " + r);
2308 if (r != starting) {
2309 r.startFreezingScreenLocked(r.app, configChanges);
2310 }
2311 if (!r.visible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002312 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 TAG, "Starting and making visible: " + r);
2314 mWindowManager.setAppVisibility(r, true);
2315 }
2316 if (r != starting) {
2317 startSpecificActivityLocked(r, false, false);
2318 }
2319 }
2320
2321 } else if (r.visible) {
2322 // If this activity is already visible, then there is nothing
2323 // else to do here.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002324 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 TAG, "Skipping: already visible at " + r);
2326 r.stopFreezingScreenLocked(false);
2327
2328 } else if (onlyThisProcess == null) {
2329 // This activity is not currently visible, but is running.
2330 // Tell it to become visible.
2331 r.visible = true;
2332 if (r.state != ActivityState.RESUMED && r != starting) {
2333 // If this activity is paused, tell it
2334 // to now show its window.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002335 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 TAG, "Making visible and scheduling visibility: " + r);
2337 try {
2338 mWindowManager.setAppVisibility(r, true);
2339 r.app.thread.scheduleWindowVisibility(r, true);
2340 r.stopFreezingScreenLocked(false);
2341 } catch (Exception e) {
2342 // Just skip on any failure; we'll make it
2343 // visible when it next restarts.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002344 Slog.w(TAG, "Exception thrown making visibile: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 + r.intent.getComponent(), e);
2346 }
2347 }
2348 }
2349
2350 // Aggregate current change flags.
2351 configChanges |= r.configChangeFlags;
2352
2353 if (r.fullscreen) {
2354 // At this point, nothing else needs to be shown
Joe Onorato8a9b2202010-02-26 18:56:32 -08002355 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 TAG, "Stopping: fullscreen at " + r);
2357 behindFullscreen = true;
2358 i--;
2359 break;
2360 }
2361 }
2362
2363 // Now for any activities that aren't visible to the user, make
2364 // sure they no longer are keeping the screen frozen.
2365 while (i >= 0) {
2366 r = (HistoryRecord)mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002367 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 TAG, "Make invisible? " + r + " finishing=" + r.finishing
2369 + " state=" + r.state
2370 + " behindFullscreen=" + behindFullscreen);
2371 if (!r.finishing) {
2372 if (behindFullscreen) {
2373 if (r.visible) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002374 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 TAG, "Making invisible: " + r);
2376 r.visible = false;
2377 try {
2378 mWindowManager.setAppVisibility(r, false);
2379 if ((r.state == ActivityState.STOPPING
2380 || r.state == ActivityState.STOPPED)
2381 && r.app != null && r.app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002382 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 TAG, "Scheduling invisibility: " + r);
2384 r.app.thread.scheduleWindowVisibility(r, false);
2385 }
2386 } catch (Exception e) {
2387 // Just skip on any failure; we'll make it
2388 // visible when it next restarts.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002389 Slog.w(TAG, "Exception thrown making hidden: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 + r.intent.getComponent(), e);
2391 }
2392 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002393 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 TAG, "Already invisible: " + r);
2395 }
2396 } else if (r.fullscreen) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002397 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 TAG, "Now behindFullscreen: " + r);
2399 behindFullscreen = true;
2400 }
2401 }
2402 i--;
2403 }
2404 }
2405
2406 /**
2407 * Version of ensureActivitiesVisible that can easily be called anywhere.
2408 */
2409 private final void ensureActivitiesVisibleLocked(HistoryRecord starting,
2410 int configChanges) {
2411 HistoryRecord r = topRunningActivityLocked(null);
2412 if (r != null) {
2413 ensureActivitiesVisibleLocked(r, starting, null, configChanges);
2414 }
2415 }
2416
2417 private void updateUsageStats(HistoryRecord resumedComponent, boolean resumed) {
2418 if (resumed) {
2419 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
2420 } else {
2421 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
2422 }
2423 }
2424
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002425 private boolean startHomeActivityLocked() {
2426 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2427 && mTopAction == null) {
2428 // We are running in factory test mode, but unable to find
2429 // the factory test app, so just sit around displaying the
2430 // error message and don't try to start anything.
2431 return false;
2432 }
2433 Intent intent = new Intent(
2434 mTopAction,
2435 mTopData != null ? Uri.parse(mTopData) : null);
2436 intent.setComponent(mTopComponent);
2437 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
2438 intent.addCategory(Intent.CATEGORY_HOME);
2439 }
2440 ActivityInfo aInfo =
2441 intent.resolveActivityInfo(mContext.getPackageManager(),
2442 STOCK_PM_FLAGS);
2443 if (aInfo != null) {
2444 intent.setComponent(new ComponentName(
2445 aInfo.applicationInfo.packageName, aInfo.name));
2446 // Don't do this if the home app is currently being
2447 // instrumented.
2448 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
2449 aInfo.applicationInfo.uid);
2450 if (app == null || app.instrumentationClass == null) {
2451 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
2452 startActivityLocked(null, intent, null, null, 0, aInfo,
2453 null, null, 0, 0, 0, false, false);
2454 }
2455 }
2456
2457
2458 return true;
2459 }
2460
2461 /**
2462 * Starts the "new version setup screen" if appropriate.
2463 */
2464 private void startSetupActivityLocked() {
2465 // Only do this once per boot.
2466 if (mCheckedForSetup) {
2467 return;
2468 }
2469
2470 // We will show this screen if the current one is a different
2471 // version than the last one shown, and we are not running in
2472 // low-level factory test mode.
2473 final ContentResolver resolver = mContext.getContentResolver();
2474 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
2475 Settings.Secure.getInt(resolver,
2476 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
2477 mCheckedForSetup = true;
2478
2479 // See if we should be showing the platform update setup UI.
2480 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
2481 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
2482 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
2483
2484 // We don't allow third party apps to replace this.
2485 ResolveInfo ri = null;
2486 for (int i=0; ris != null && i<ris.size(); i++) {
2487 if ((ris.get(i).activityInfo.applicationInfo.flags
2488 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2489 ri = ris.get(i);
2490 break;
2491 }
2492 }
2493
2494 if (ri != null) {
2495 String vers = ri.activityInfo.metaData != null
2496 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
2497 : null;
2498 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
2499 vers = ri.activityInfo.applicationInfo.metaData.getString(
2500 Intent.METADATA_SETUP_VERSION);
2501 }
2502 String lastVers = Settings.Secure.getString(
2503 resolver, Settings.Secure.LAST_SETUP_SHOWN);
2504 if (vers != null && !vers.equals(lastVers)) {
2505 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2506 intent.setComponent(new ComponentName(
2507 ri.activityInfo.packageName, ri.activityInfo.name));
2508 startActivityLocked(null, intent, null, null, 0, ri.activityInfo,
2509 null, null, 0, 0, 0, false, false);
2510 }
2511 }
2512 }
2513 }
2514
Dianne Hackborn1bcf5a82009-09-30 15:22:29 -07002515 private void reportResumedActivityLocked(HistoryRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002516 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002517
2518 final int identHash = System.identityHashCode(r);
2519 updateUsageStats(r, true);
2520
2521 int i = mWatchers.beginBroadcast();
2522 while (i > 0) {
2523 i--;
2524 IActivityWatcher w = mWatchers.getBroadcastItem(i);
2525 if (w != null) {
2526 try {
2527 w.activityResuming(identHash);
2528 } catch (RemoteException e) {
2529 }
2530 }
2531 }
2532 mWatchers.finishBroadcast();
2533 }
2534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 /**
2536 * Ensure that the top activity in the stack is resumed.
2537 *
2538 * @param prev The previously resumed activity, for when in the process
2539 * of pausing; can be null to call from elsewhere.
2540 *
2541 * @return Returns true if something is being resumed, or false if
2542 * nothing happened.
2543 */
2544 private final boolean resumeTopActivityLocked(HistoryRecord prev) {
2545 // Find the first activity that is not finishing.
2546 HistoryRecord next = topRunningActivityLocked(null);
2547
2548 // Remember how we'll process this pause/resume situation, and ensure
2549 // that the state is reset however we wind up proceeding.
2550 final boolean userLeaving = mUserLeaving;
2551 mUserLeaving = false;
2552
2553 if (next == null) {
2554 // There are no more activities! Let's just start up the
2555 // Launcher...
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002556 return startHomeActivityLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
2558
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002559 next.delayedResume = false;
2560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 // If the top activity is the resumed one, nothing to do.
2562 if (mResumedActivity == next && next.state == ActivityState.RESUMED) {
2563 // Make sure we have executed any pending transitions, since there
2564 // should be nothing left to do at this point.
2565 mWindowManager.executeAppTransition();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002566 mNoAnimActivities.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 return false;
2568 }
2569
2570 // If we are sleeping, and there is no resumed activity, and the top
2571 // activity is paused, well that is the state we want.
Dianne Hackborn55280a92009-05-07 15:53:46 -07002572 if ((mSleeping || mShuttingDown)
2573 && mLastPausedActivity == next && next.state == ActivityState.PAUSED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 // Make sure we have executed any pending transitions, since there
2575 // should be nothing left to do at this point.
2576 mWindowManager.executeAppTransition();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002577 mNoAnimActivities.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 return false;
2579 }
2580
2581 // The activity may be waiting for stop, but that is no longer
2582 // appropriate for it.
2583 mStoppingActivities.remove(next);
2584 mWaitingVisibleActivities.remove(next);
2585
Joe Onorato8a9b2202010-02-26 18:56:32 -08002586 if (DEBUG_SWITCH) Slog.v(TAG, "Resuming " + next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587
2588 // If we are currently pausing an activity, then don't do anything
2589 // until that is done.
2590 if (mPausingActivity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002591 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: pausing=" + mPausingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 return false;
2593 }
2594
2595 // We need to start pausing the current activity so the top one
2596 // can be resumed...
2597 if (mResumedActivity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002598 if (DEBUG_SWITCH) Slog.v(TAG, "Skip resume: need to start pausing");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 startPausingLocked(userLeaving, false);
2600 return true;
2601 }
2602
2603 if (prev != null && prev != next) {
2604 if (!prev.waitingVisible && next != null && !next.nowVisible) {
2605 prev.waitingVisible = true;
2606 mWaitingVisibleActivities.add(prev);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002607 if (DEBUG_SWITCH) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 TAG, "Resuming top, waiting visible to hide: " + prev);
2609 } else {
2610 // The next activity is already visible, so hide the previous
2611 // activity's windows right now so we can show the new one ASAP.
2612 // We only do this if the previous is finishing, which should mean
2613 // it is on top of the one being resumed so hiding it quickly
2614 // is good. Otherwise, we want to do the normal route of allowing
2615 // the resumed activity to be shown so we can decide if the
2616 // previous should actually be hidden depending on whether the
2617 // new one is found to be full-screen or not.
2618 if (prev.finishing) {
2619 mWindowManager.setAppVisibility(prev, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002620 if (DEBUG_SWITCH) Slog.v(TAG, "Not waiting for visible to hide: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 + prev + ", waitingVisible="
2622 + (prev != null ? prev.waitingVisible : null)
2623 + ", nowVisible=" + next.nowVisible);
2624 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002625 if (DEBUG_SWITCH) Slog.v(TAG, "Previous already visible but still waiting to hide: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 + prev + ", waitingVisible="
2627 + (prev != null ? prev.waitingVisible : null)
2628 + ", nowVisible=" + next.nowVisible);
2629 }
2630 }
2631 }
2632
2633 // We are starting up the next activity, so tell the window manager
2634 // that the previous one will be hidden soon. This way it can know
2635 // to ignore it when computing the desired screen orientation.
2636 if (prev != null) {
2637 if (prev.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002638 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 "Prepare close transition: prev=" + prev);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002640 if (mNoAnimActivities.contains(prev)) {
2641 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
2642 } else {
2643 mWindowManager.prepareAppTransition(prev.task == next.task
2644 ? WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE
2645 : WindowManagerPolicy.TRANSIT_TASK_CLOSE);
2646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002647 mWindowManager.setAppWillBeHidden(prev);
2648 mWindowManager.setAppVisibility(prev, false);
2649 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002650 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 "Prepare open transition: prev=" + prev);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002652 if (mNoAnimActivities.contains(next)) {
2653 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
2654 } else {
2655 mWindowManager.prepareAppTransition(prev.task == next.task
2656 ? WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
2657 : WindowManagerPolicy.TRANSIT_TASK_OPEN);
2658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 }
2660 if (false) {
2661 mWindowManager.setAppWillBeHidden(prev);
2662 mWindowManager.setAppVisibility(prev, false);
2663 }
2664 } else if (mHistory.size() > 1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002665 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 "Prepare open transition: no previous");
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002667 if (mNoAnimActivities.contains(next)) {
2668 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
2669 } else {
2670 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN);
2671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 }
2673
2674 if (next.app != null && next.app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002675 if (DEBUG_SWITCH) Slog.v(TAG, "Resume running: " + next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676
2677 // This activity is now becoming visible.
2678 mWindowManager.setAppVisibility(next, true);
2679
2680 HistoryRecord lastResumedActivity = mResumedActivity;
2681 ActivityState lastState = next.state;
2682
2683 updateCpuStats();
2684
2685 next.state = ActivityState.RESUMED;
2686 mResumedActivity = next;
2687 next.task.touchActiveTime();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002688 updateLruProcessLocked(next.app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 updateLRUListLocked(next);
2690
2691 // Have the window manager re-evaluate the orientation of
2692 // the screen based on the new activity order.
Eric Fischerd4d04de2009-10-27 18:55:57 -07002693 boolean updated;
2694 synchronized (this) {
2695 Configuration config = mWindowManager.updateOrientationFromAppTokens(
2696 mConfiguration,
2697 next.mayFreezeScreenLocked(next.app) ? next : null);
2698 if (config != null) {
Eric Fischerd4d04de2009-10-27 18:55:57 -07002699 next.frozenBeforeDestroy = true;
2700 }
2701 updated = updateConfigurationLocked(config, next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002702 }
Eric Fischerd4d04de2009-10-27 18:55:57 -07002703 if (!updated) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 // The configuration update wasn't able to keep the existing
2705 // instance of the activity, and instead started a new one.
2706 // We should be all done, but let's just make sure our activity
2707 // is still at the top and schedule another run if something
2708 // weird happened.
2709 HistoryRecord nextNext = topRunningActivityLocked(null);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002710 if (DEBUG_SWITCH) Slog.i(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 "Activity config changed during resume: " + next
2712 + ", new next: " + nextNext);
2713 if (nextNext != next) {
2714 // Do over!
2715 mHandler.sendEmptyMessage(RESUME_TOP_ACTIVITY_MSG);
2716 }
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002717 setFocusedActivityLocked(next);
2718 ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 mWindowManager.executeAppTransition();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002720 mNoAnimActivities.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 return true;
2722 }
2723
2724 try {
2725 // Deliver all pending results.
2726 ArrayList a = next.results;
2727 if (a != null) {
2728 final int N = a.size();
2729 if (!next.finishing && N > 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002730 if (DEBUG_RESULTS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 TAG, "Delivering results to " + next
2732 + ": " + a);
2733 next.app.thread.scheduleSendResult(next, a);
2734 }
2735 }
2736
2737 if (next.newIntents != null) {
2738 next.app.thread.scheduleNewIntent(next.newIntents, next);
2739 }
2740
Doug Zongker2bec3d42009-12-04 12:52:44 -08002741 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 System.identityHashCode(next),
2743 next.task.taskId, next.shortComponentName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744
2745 next.app.thread.scheduleResumeActivity(next,
2746 isNextTransitionForward());
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 pauseIfSleepingLocked();
2749
2750 } catch (Exception e) {
2751 // Whoops, need to restart this activity!
2752 next.state = lastState;
2753 mResumedActivity = lastResumedActivity;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002754 Slog.i(TAG, "Restarting because process died: " + next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 if (!next.hasBeenLaunched) {
2756 next.hasBeenLaunched = true;
2757 } else {
2758 if (SHOW_APP_STARTING_ICON) {
2759 mWindowManager.setAppStartingWindow(
2760 next, next.packageName, next.theme,
2761 next.nonLocalizedLabel,
2762 next.labelRes, next.icon, null, true);
2763 }
2764 }
2765 startSpecificActivityLocked(next, true, false);
2766 return true;
2767 }
2768
2769 // From this point on, if something goes wrong there is no way
2770 // to recover the activity.
2771 try {
2772 next.visible = true;
2773 completeResumeLocked(next);
2774 } catch (Exception e) {
2775 // If any exception gets thrown, toss away this
2776 // activity and try the next one.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002777 Slog.w(TAG, "Exception thrown during resume of " + next, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 requestFinishActivityLocked(next, Activity.RESULT_CANCELED, null,
2779 "resume-exception");
2780 return true;
2781 }
2782
2783 // Didn't need to use the icicle, and it is now out of date.
2784 next.icicle = null;
2785 next.haveState = false;
2786 next.stopped = false;
2787
2788 } else {
2789 // Whoops, need to restart this activity!
2790 if (!next.hasBeenLaunched) {
2791 next.hasBeenLaunched = true;
2792 } else {
2793 if (SHOW_APP_STARTING_ICON) {
2794 mWindowManager.setAppStartingWindow(
2795 next, next.packageName, next.theme,
2796 next.nonLocalizedLabel,
2797 next.labelRes, next.icon, null, true);
2798 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002799 if (DEBUG_SWITCH) Slog.v(TAG, "Restarting: " + next);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
2801 startSpecificActivityLocked(next, true, true);
2802 }
2803
2804 return true;
2805 }
2806
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002807 private final void startActivityLocked(HistoryRecord r, boolean newTask,
2808 boolean doResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 final int NH = mHistory.size();
2810
2811 int addPos = -1;
2812
2813 if (!newTask) {
2814 // If starting in an existing task, find where that is...
2815 HistoryRecord next = null;
2816 boolean startIt = true;
2817 for (int i = NH-1; i >= 0; i--) {
2818 HistoryRecord p = (HistoryRecord)mHistory.get(i);
2819 if (p.finishing) {
2820 continue;
2821 }
2822 if (p.task == r.task) {
2823 // Here it is! Now, if this is not yet visible to the
2824 // user, then just add it without starting; it will
2825 // get started when the user navigates back to it.
2826 addPos = i+1;
2827 if (!startIt) {
2828 mHistory.add(addPos, r);
2829 r.inHistory = true;
2830 r.task.numActivities++;
2831 mWindowManager.addAppToken(addPos, r, r.task.taskId,
2832 r.info.screenOrientation, r.fullscreen);
2833 if (VALIDATE_TOKENS) {
2834 mWindowManager.validateAppTokens(mHistory);
2835 }
2836 return;
2837 }
2838 break;
2839 }
2840 if (p.fullscreen) {
2841 startIt = false;
2842 }
2843 next = p;
2844 }
2845 }
2846
2847 // Place a new activity at top of stack, so it is next to interact
2848 // with the user.
2849 if (addPos < 0) {
2850 addPos = mHistory.size();
2851 }
2852
2853 // If we are not placing the new activity frontmost, we do not want
2854 // to deliver the onUserLeaving callback to the actual frontmost
2855 // activity
2856 if (addPos < NH) {
2857 mUserLeaving = false;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002858 if (DEBUG_USER_LEAVING) Slog.v(TAG, "startActivity() behind front, mUserLeaving=false");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
2861 // Slot the activity into the history stack and proceed
2862 mHistory.add(addPos, r);
2863 r.inHistory = true;
2864 r.frontOfTask = newTask;
2865 r.task.numActivities++;
2866 if (NH > 0) {
2867 // We want to show the starting preview window if we are
2868 // switching to a new task, or the next activity's process is
2869 // not currently running.
2870 boolean showStartingIcon = newTask;
2871 ProcessRecord proc = r.app;
2872 if (proc == null) {
2873 proc = mProcessNames.get(r.processName, r.info.applicationInfo.uid);
2874 }
2875 if (proc == null || proc.thread == null) {
2876 showStartingIcon = true;
2877 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08002878 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 "Prepare open transition: starting " + r);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07002880 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
2881 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
2882 mNoAnimActivities.add(r);
2883 } else if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0) {
2884 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_TASK_OPEN);
2885 mNoAnimActivities.remove(r);
2886 } else {
2887 mWindowManager.prepareAppTransition(newTask
2888 ? WindowManagerPolicy.TRANSIT_TASK_OPEN
2889 : WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN);
2890 mNoAnimActivities.remove(r);
2891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 mWindowManager.addAppToken(
2893 addPos, r, r.task.taskId, r.info.screenOrientation, r.fullscreen);
2894 boolean doShow = true;
2895 if (newTask) {
2896 // Even though this activity is starting fresh, we still need
2897 // to reset it to make sure we apply affinities to move any
2898 // existing activities from other tasks in to it.
2899 // If the caller has requested that the target task be
2900 // reset, then do so.
2901 if ((r.intent.getFlags()
2902 &Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
2903 resetTaskIfNeededLocked(r, r);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002904 doShow = topRunningNonDelayedActivityLocked(null) == r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 }
2906 }
2907 if (SHOW_APP_STARTING_ICON && doShow) {
2908 // Figure out if we are transitioning from another activity that is
2909 // "has the same starting icon" as the next one. This allows the
2910 // window manager to keep the previous window it had previously
2911 // created, if it still had one.
2912 HistoryRecord prev = mResumedActivity;
2913 if (prev != null) {
2914 // We don't want to reuse the previous starting preview if:
2915 // (1) The current activity is in a different task.
2916 if (prev.task != r.task) prev = null;
2917 // (2) The current activity is already displayed.
2918 else if (prev.nowVisible) prev = null;
2919 }
2920 mWindowManager.setAppStartingWindow(
2921 r, r.packageName, r.theme, r.nonLocalizedLabel,
2922 r.labelRes, r.icon, prev, showStartingIcon);
2923 }
2924 } else {
2925 // If this is the first activity, don't do any fancy animations,
2926 // because there is nothing for it to animate on top of.
2927 mWindowManager.addAppToken(addPos, r, r.task.taskId,
2928 r.info.screenOrientation, r.fullscreen);
2929 }
2930 if (VALIDATE_TOKENS) {
2931 mWindowManager.validateAppTokens(mHistory);
2932 }
2933
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002934 if (doResume) {
2935 resumeTopActivityLocked(null);
2936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 }
2938
2939 /**
2940 * Perform clear operation as requested by
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002941 * {@link Intent#FLAG_ACTIVITY_CLEAR_TOP}: search from the top of the
2942 * stack to the given task, then look for
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 * an instance of that activity in the stack and, if found, finish all
2944 * activities on top of it and return the instance.
2945 *
2946 * @param newR Description of the new activity being started.
2947 * @return Returns the old activity that should be continue to be used,
2948 * or null if none was found.
2949 */
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002950 private final HistoryRecord performClearTaskLocked(int taskId,
Dianne Hackbornaa52f9a2009-08-25 16:01:15 -07002951 HistoryRecord newR, int launchFlags, boolean doClear) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 int i = mHistory.size();
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002953
2954 // First find the requested task.
2955 while (i > 0) {
2956 i--;
2957 HistoryRecord r = (HistoryRecord)mHistory.get(i);
2958 if (r.task.taskId == taskId) {
2959 i++;
2960 break;
2961 }
2962 }
2963
2964 // Now clear it.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 while (i > 0) {
2966 i--;
2967 HistoryRecord r = (HistoryRecord)mHistory.get(i);
2968 if (r.finishing) {
2969 continue;
2970 }
2971 if (r.task.taskId != taskId) {
2972 return null;
2973 }
2974 if (r.realActivity.equals(newR.realActivity)) {
2975 // Here it is! Now finish everything in front...
2976 HistoryRecord ret = r;
2977 if (doClear) {
2978 while (i < (mHistory.size()-1)) {
2979 i++;
2980 r = (HistoryRecord)mHistory.get(i);
2981 if (r.finishing) {
2982 continue;
2983 }
2984 if (finishActivityLocked(r, i, Activity.RESULT_CANCELED,
2985 null, "clear")) {
2986 i--;
2987 }
2988 }
2989 }
2990
2991 // Finally, if this is a normal launch mode (that is, not
2992 // expecting onNewIntent()), then we will finish the current
2993 // instance of the activity so a new fresh one can be started.
Dianne Hackbornaa52f9a2009-08-25 16:01:15 -07002994 if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
2995 && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 if (!ret.finishing) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07002997 int index = indexOfTokenLocked(ret);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 if (index >= 0) {
Dianne Hackbornf83c5552010-03-31 22:19:32 -07002999 finishActivityLocked(ret, index, Activity.RESULT_CANCELED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 null, "clear");
3001 }
3002 return null;
3003 }
3004 }
3005
3006 return ret;
3007 }
3008 }
3009
3010 return null;
3011 }
3012
3013 /**
3014 * Find the activity in the history stack within the given task. Returns
3015 * the index within the history at which it's found, or < 0 if not found.
3016 */
3017 private final int findActivityInHistoryLocked(HistoryRecord r, int task) {
3018 int i = mHistory.size();
3019 while (i > 0) {
3020 i--;
3021 HistoryRecord candidate = (HistoryRecord)mHistory.get(i);
3022 if (candidate.task.taskId != task) {
3023 break;
3024 }
3025 if (candidate.realActivity.equals(r.realActivity)) {
3026 return i;
3027 }
3028 }
3029
3030 return -1;
3031 }
3032
3033 /**
3034 * Reorder the history stack so that the activity at the given index is
3035 * brought to the front.
3036 */
3037 private final HistoryRecord moveActivityToFrontLocked(int where) {
3038 HistoryRecord newTop = (HistoryRecord)mHistory.remove(where);
3039 int top = mHistory.size();
3040 HistoryRecord oldTop = (HistoryRecord)mHistory.get(top-1);
3041 mHistory.add(top, newTop);
3042 oldTop.frontOfTask = false;
3043 newTop.frontOfTask = true;
3044 return newTop;
3045 }
3046
3047 /**
3048 * Deliver a new Intent to an existing activity, so that its onNewIntent()
3049 * method will be called at the proper time.
3050 */
3051 private final void deliverNewIntentLocked(HistoryRecord r, Intent intent) {
3052 boolean sent = false;
3053 if (r.state == ActivityState.RESUMED
3054 && r.app != null && r.app.thread != null) {
3055 try {
3056 ArrayList<Intent> ar = new ArrayList<Intent>();
3057 ar.add(new Intent(intent));
3058 r.app.thread.scheduleNewIntent(ar, r);
3059 sent = true;
3060 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003061 Slog.w(TAG, "Exception thrown sending new intent to " + r, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 }
3063 }
3064 if (!sent) {
3065 r.addNewIntentLocked(new Intent(intent));
3066 }
3067 }
3068
3069 private final void logStartActivity(int tag, HistoryRecord r,
3070 TaskRecord task) {
3071 EventLog.writeEvent(tag,
3072 System.identityHashCode(r), task.taskId,
3073 r.shortComponentName, r.intent.getAction(),
3074 r.intent.getType(), r.intent.getDataString(),
3075 r.intent.getFlags());
3076 }
3077
3078 private final int startActivityLocked(IApplicationThread caller,
3079 Intent intent, String resolvedType,
3080 Uri[] grantedUriPermissions,
3081 int grantedMode, ActivityInfo aInfo, IBinder resultTo,
3082 String resultWho, int requestCode,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003083 int callingPid, int callingUid, boolean onlyIfNeeded,
3084 boolean componentSpecified) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003085 Slog.i(TAG, "Starting activity: " + intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086
3087 HistoryRecord sourceRecord = null;
3088 HistoryRecord resultRecord = null;
3089 if (resultTo != null) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07003090 int index = indexOfTokenLocked(resultTo);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003091 if (DEBUG_RESULTS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 TAG, "Sending result to " + resultTo + " (index " + index + ")");
3093 if (index >= 0) {
3094 sourceRecord = (HistoryRecord)mHistory.get(index);
3095 if (requestCode >= 0 && !sourceRecord.finishing) {
3096 resultRecord = sourceRecord;
3097 }
3098 }
3099 }
3100
3101 int launchFlags = intent.getFlags();
3102
3103 if ((launchFlags&Intent.FLAG_ACTIVITY_FORWARD_RESULT) != 0
3104 && sourceRecord != null) {
3105 // Transfer the result target from the source activity to the new
3106 // one being started, including any failures.
3107 if (requestCode >= 0) {
3108 return START_FORWARD_AND_REQUEST_CONFLICT;
3109 }
3110 resultRecord = sourceRecord.resultTo;
3111 resultWho = sourceRecord.resultWho;
3112 requestCode = sourceRecord.requestCode;
3113 sourceRecord.resultTo = null;
3114 if (resultRecord != null) {
3115 resultRecord.removeResultsLocked(
3116 sourceRecord, resultWho, requestCode);
3117 }
3118 }
3119
3120 int err = START_SUCCESS;
3121
3122 if (intent.getComponent() == null) {
3123 // We couldn't find a class that can handle the given Intent.
3124 // That's the end of that!
3125 err = START_INTENT_NOT_RESOLVED;
3126 }
3127
3128 if (err == START_SUCCESS && aInfo == null) {
3129 // We couldn't find the specific class specified in the Intent.
3130 // Also the end of the line.
3131 err = START_CLASS_NOT_FOUND;
3132 }
3133
3134 ProcessRecord callerApp = null;
3135 if (err == START_SUCCESS && caller != null) {
3136 callerApp = getRecordForAppLocked(caller);
3137 if (callerApp != null) {
3138 callingPid = callerApp.pid;
3139 callingUid = callerApp.info.uid;
3140 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003141 Slog.w(TAG, "Unable to find app for caller " + caller
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 + " (pid=" + callingPid + ") when starting: "
3143 + intent.toString());
3144 err = START_PERMISSION_DENIED;
3145 }
3146 }
3147
3148 if (err != START_SUCCESS) {
3149 if (resultRecord != null) {
3150 sendActivityResultLocked(-1,
3151 resultRecord, resultWho, requestCode,
3152 Activity.RESULT_CANCELED, null);
3153 }
3154 return err;
3155 }
3156
3157 final int perm = checkComponentPermission(aInfo.permission, callingPid,
3158 callingUid, aInfo.exported ? -1 : aInfo.applicationInfo.uid);
3159 if (perm != PackageManager.PERMISSION_GRANTED) {
3160 if (resultRecord != null) {
3161 sendActivityResultLocked(-1,
3162 resultRecord, resultWho, requestCode,
3163 Activity.RESULT_CANCELED, null);
3164 }
3165 String msg = "Permission Denial: starting " + intent.toString()
3166 + " from " + callerApp + " (pid=" + callingPid
3167 + ", uid=" + callingUid + ")"
3168 + " requires " + aInfo.permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003169 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 throw new SecurityException(msg);
3171 }
3172
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003173 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 boolean abort = false;
3175 try {
3176 // The Intent we give to the watcher has the extra data
3177 // stripped off, since it can contain private information.
3178 Intent watchIntent = intent.cloneFilter();
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003179 abort = !mController.activityStarting(watchIntent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 aInfo.applicationInfo.packageName);
3181 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07003182 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 }
3184
3185 if (abort) {
3186 if (resultRecord != null) {
3187 sendActivityResultLocked(-1,
3188 resultRecord, resultWho, requestCode,
3189 Activity.RESULT_CANCELED, null);
3190 }
3191 // We pretend to the caller that it was really started, but
3192 // they will just get a cancel result.
3193 return START_SUCCESS;
3194 }
3195 }
3196
3197 HistoryRecord r = new HistoryRecord(this, callerApp, callingUid,
3198 intent, resolvedType, aInfo, mConfiguration,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003199 resultRecord, resultWho, requestCode, componentSpecified);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003201 if (mResumedActivity == null
3202 || mResumedActivity.info.applicationInfo.uid != callingUid) {
3203 if (!checkAppSwitchAllowedLocked(callingPid, callingUid, "Activity start")) {
3204 PendingActivityLaunch pal = new PendingActivityLaunch();
3205 pal.r = r;
3206 pal.sourceRecord = sourceRecord;
3207 pal.grantedUriPermissions = grantedUriPermissions;
3208 pal.grantedMode = grantedMode;
3209 pal.onlyIfNeeded = onlyIfNeeded;
3210 mPendingActivityLaunches.add(pal);
3211 return START_SWITCHES_CANCELED;
3212 }
3213 }
3214
3215 if (mDidAppSwitch) {
3216 // This is the second allowed switch since we stopped switches,
3217 // so now just generally allow switches. Use case: user presses
3218 // home (switches disabled, switch to home, mDidAppSwitch now true);
3219 // user taps a home icon (coming from home so allowed, we hit here
3220 // and now allow anyone to switch again).
3221 mAppSwitchesAllowedTime = 0;
3222 } else {
3223 mDidAppSwitch = true;
3224 }
3225
3226 doPendingActivityLaunchesLocked(false);
3227
3228 return startActivityUncheckedLocked(r, sourceRecord,
3229 grantedUriPermissions, grantedMode, onlyIfNeeded, true);
3230 }
3231
3232 private final void doPendingActivityLaunchesLocked(boolean doResume) {
3233 final int N = mPendingActivityLaunches.size();
3234 if (N <= 0) {
3235 return;
3236 }
3237 for (int i=0; i<N; i++) {
3238 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
3239 startActivityUncheckedLocked(pal.r, pal.sourceRecord,
3240 pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded,
3241 doResume && i == (N-1));
3242 }
3243 mPendingActivityLaunches.clear();
3244 }
3245
3246 private final int startActivityUncheckedLocked(HistoryRecord r,
3247 HistoryRecord sourceRecord, Uri[] grantedUriPermissions,
3248 int grantedMode, boolean onlyIfNeeded, boolean doResume) {
3249 final Intent intent = r.intent;
3250 final int callingUid = r.launchedFromUid;
3251
3252 int launchFlags = intent.getFlags();
3253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 // We'll invoke onUserLeaving before onPause only if the launching
3255 // activity did not explicitly state that this is an automated launch.
3256 mUserLeaving = (launchFlags&Intent.FLAG_ACTIVITY_NO_USER_ACTION) == 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003257 if (DEBUG_USER_LEAVING) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 "startActivity() => mUserLeaving=" + mUserLeaving);
3259
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003260 // If the caller has asked not to resume at this point, we make note
3261 // of this in the record so that we can skip it when trying to find
3262 // the top running activity.
3263 if (!doResume) {
3264 r.delayedResume = true;
3265 }
3266
3267 HistoryRecord notTop = (launchFlags&Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP)
3268 != 0 ? r : null;
3269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 // If the onlyIfNeeded flag is set, then we can do this if the activity
3271 // being launched is the same as the one making the call... or, as
3272 // a special case, if we do not know the caller then we count the
3273 // current top activity as the caller.
3274 if (onlyIfNeeded) {
3275 HistoryRecord checkedCaller = sourceRecord;
3276 if (checkedCaller == null) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003277 checkedCaller = topRunningNonDelayedActivityLocked(notTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 }
3279 if (!checkedCaller.realActivity.equals(r.realActivity)) {
3280 // Caller is not the same as launcher, so always needed.
3281 onlyIfNeeded = false;
3282 }
3283 }
3284
3285 if (grantedUriPermissions != null && callingUid > 0) {
3286 for (int i=0; i<grantedUriPermissions.length; i++) {
3287 grantUriPermissionLocked(callingUid, r.packageName,
3288 grantedUriPermissions[i], grantedMode, r);
3289 }
3290 }
3291
3292 grantUriPermissionFromIntentLocked(callingUid, r.packageName,
3293 intent, r);
3294
3295 if (sourceRecord == null) {
3296 // This activity is not being started from another... in this
3297 // case we -always- start a new task.
3298 if ((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003299 Slog.w(TAG, "startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 + intent);
3301 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
3302 }
3303 } else if (sourceRecord.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
3304 // The original activity who is starting us is running as a single
3305 // instance... this new activity it is starting must go on its
3306 // own task.
3307 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
3308 } else if (r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE
3309 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
3310 // The activity being started is a single instance... it always
3311 // gets launched into its own task.
3312 launchFlags |= Intent.FLAG_ACTIVITY_NEW_TASK;
3313 }
3314
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003315 if (r.resultTo != null && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 // For whatever reason this activity is being launched into a new
3317 // task... yet the caller has requested a result back. Well, that
3318 // is pretty messed up, so instead immediately send back a cancel
3319 // and let the new task continue launched as normal without a
3320 // dependency on its originator.
Joe Onorato8a9b2202010-02-26 18:56:32 -08003321 Slog.w(TAG, "Activity is launching as a new task, so cancelling activity result.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 sendActivityResultLocked(-1,
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003323 r.resultTo, r.resultWho, r.requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 Activity.RESULT_CANCELED, null);
3325 r.resultTo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 }
3327
3328 boolean addingToTask = false;
3329 if (((launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0 &&
3330 (launchFlags&Intent.FLAG_ACTIVITY_MULTIPLE_TASK) == 0)
3331 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
3332 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
3333 // If bring to front is requested, and no result is requested, and
3334 // we can find a task that was started with this same
3335 // component, then instead of launching bring that one to the front.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003336 if (r.resultTo == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 // See if there is a task to bring to the front. If this is
3338 // a SINGLE_INSTANCE activity, there can be one and only one
3339 // instance of it in the history, and it is always in its own
3340 // unique task, so we do a special search.
3341 HistoryRecord taskTop = r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE
3342 ? findTaskLocked(intent, r.info)
3343 : findActivityLocked(intent, r.info);
3344 if (taskTop != null) {
3345 if (taskTop.task.intent == null) {
3346 // This task was started because of movement of
3347 // the activity based on affinity... now that we
3348 // are actually launching it, we can assign the
3349 // base intent.
3350 taskTop.task.setIntent(intent, r.info);
3351 }
3352 // If the target task is not in the front, then we need
3353 // to bring it to the front... except... well, with
3354 // SINGLE_TASK_LAUNCH it's not entirely clear. We'd like
3355 // to have the same behavior as if a new instance was
3356 // being started, which means not bringing it to the front
3357 // if the caller is not itself in the front.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003358 HistoryRecord curTop = topRunningNonDelayedActivityLocked(notTop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (curTop.task != taskTop.task) {
3360 r.intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
3361 boolean callerAtFront = sourceRecord == null
3362 || curTop.task == sourceRecord.task;
3363 if (callerAtFront) {
3364 // We really do want to push this one into the
3365 // user's face, right now.
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07003366 moveTaskToFrontLocked(taskTop.task, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
3368 }
3369 // If the caller has requested that the target task be
3370 // reset, then do so.
3371 if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
3372 taskTop = resetTaskIfNeededLocked(taskTop, r);
3373 }
3374 if (onlyIfNeeded) {
3375 // We don't need to start a new activity, and
3376 // the client said not to do anything if that
3377 // is the case, so this is it! And for paranoia, make
3378 // sure we have correctly resumed the top activity.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003379 if (doResume) {
3380 resumeTopActivityLocked(null);
3381 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 return START_RETURN_INTENT_TO_CALLER;
3383 }
3384 if ((launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0
3385 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK
3386 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
3387 // In this situation we want to remove all activities
3388 // from the task up to the one being started. In most
3389 // cases this means we are resetting the task to its
3390 // initial state.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003391 HistoryRecord top = performClearTaskLocked(
Dianne Hackbornaa52f9a2009-08-25 16:01:15 -07003392 taskTop.task.taskId, r, launchFlags, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 if (top != null) {
3394 if (top.frontOfTask) {
3395 // Activity aliases may mean we use different
3396 // intents for the top activity, so make sure
3397 // the task now has the identity of the new
3398 // intent.
3399 top.task.setIntent(r.intent, r.info);
3400 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08003401 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 deliverNewIntentLocked(top, r.intent);
3403 } else {
3404 // A special case: we need to
3405 // start the activity because it is not currently
3406 // running, and the caller has asked to clear the
3407 // current task to have this activity at the top.
3408 addingToTask = true;
3409 // Now pretend like this activity is being started
3410 // by the top of its task, so it is put in the
3411 // right place.
3412 sourceRecord = taskTop;
3413 }
3414 } else if (r.realActivity.equals(taskTop.task.realActivity)) {
3415 // In this case the top activity on the task is the
3416 // same as the one being launched, so we take that
3417 // as a request to bring the task to the foreground.
3418 // If the top activity in the task is the root
3419 // activity, deliver this new intent to it if it
3420 // desires.
3421 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
3422 && taskTop.realActivity.equals(r.realActivity)) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003423 logStartActivity(EventLogTags.AM_NEW_INTENT, r, taskTop.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 if (taskTop.frontOfTask) {
3425 taskTop.task.setIntent(r.intent, r.info);
3426 }
3427 deliverNewIntentLocked(taskTop, r.intent);
3428 } else if (!r.intent.filterEquals(taskTop.task.intent)) {
3429 // In this case we are launching the root activity
3430 // of the task, but with a different intent. We
3431 // should start a new instance on top.
3432 addingToTask = true;
3433 sourceRecord = taskTop;
3434 }
3435 } else if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) == 0) {
3436 // In this case an activity is being launched in to an
3437 // existing task, without resetting that task. This
3438 // is typically the situation of launching an activity
3439 // from a notification or shortcut. We want to place
3440 // the new activity on top of the current task.
3441 addingToTask = true;
3442 sourceRecord = taskTop;
3443 } else if (!taskTop.task.rootWasReset) {
3444 // In this case we are launching in to an existing task
3445 // that has not yet been started from its front door.
3446 // The current task has been brought to the front.
3447 // Ideally, we'd probably like to place this new task
3448 // at the bottom of its stack, but that's a little hard
3449 // to do with the current organization of the code so
3450 // for now we'll just drop it.
3451 taskTop.task.setIntent(r.intent, r.info);
3452 }
3453 if (!addingToTask) {
3454 // We didn't do anything... but it was needed (a.k.a., client
3455 // don't use that intent!) And for paranoia, make
3456 // sure we have correctly resumed the top activity.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003457 if (doResume) {
3458 resumeTopActivityLocked(null);
3459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 return START_TASK_TO_FRONT;
3461 }
3462 }
3463 }
3464 }
3465
3466 //String uri = r.intent.toURI();
3467 //Intent intent2 = new Intent(uri);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003468 //Slog.i(TAG, "Given intent: " + r.intent);
3469 //Slog.i(TAG, "URI is: " + uri);
3470 //Slog.i(TAG, "To intent: " + intent2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471
3472 if (r.packageName != null) {
3473 // If the activity being launched is the same as the one currently
3474 // at the top, then we need to check if it should only be launched
3475 // once.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003476 HistoryRecord top = topRunningNonDelayedActivityLocked(notTop);
3477 if (top != null && r.resultTo == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 if (top.realActivity.equals(r.realActivity)) {
3479 if (top.app != null && top.app.thread != null) {
3480 if ((launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) != 0
3481 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP
3482 || r.launchMode == ActivityInfo.LAUNCH_SINGLE_TASK) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003483 logStartActivity(EventLogTags.AM_NEW_INTENT, top, top.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 // For paranoia, make sure we have correctly
3485 // resumed the top activity.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003486 if (doResume) {
3487 resumeTopActivityLocked(null);
3488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 if (onlyIfNeeded) {
3490 // We don't need to start a new activity, and
3491 // the client said not to do anything if that
3492 // is the case, so this is it!
3493 return START_RETURN_INTENT_TO_CALLER;
3494 }
3495 deliverNewIntentLocked(top, r.intent);
3496 return START_DELIVERED_TO_TOP;
3497 }
3498 }
3499 }
3500 }
3501
3502 } else {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003503 if (r.resultTo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 sendActivityResultLocked(-1,
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003505 r.resultTo, r.resultWho, r.requestCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 Activity.RESULT_CANCELED, null);
3507 }
3508 return START_CLASS_NOT_FOUND;
3509 }
3510
3511 boolean newTask = false;
3512
3513 // Should this be considered a new task?
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003514 if (r.resultTo == null && !addingToTask
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 && (launchFlags&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
3516 // todo: should do better management of integers.
3517 mCurTask++;
3518 if (mCurTask <= 0) {
3519 mCurTask = 1;
3520 }
3521 r.task = new TaskRecord(mCurTask, r.info, intent,
3522 (r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003523 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003524 + " in new task " + r.task);
3525 newTask = true;
Josh Bartel7f208742010-02-25 11:01:44 -06003526 addRecentTaskLocked(r.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527
3528 } else if (sourceRecord != null) {
3529 if (!addingToTask &&
3530 (launchFlags&Intent.FLAG_ACTIVITY_CLEAR_TOP) != 0) {
3531 // In this case, we are adding the activity to an existing
3532 // task, but the caller has asked to clear that task if the
3533 // activity is already running.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003534 HistoryRecord top = performClearTaskLocked(
Dianne Hackbornaa52f9a2009-08-25 16:01:15 -07003535 sourceRecord.task.taskId, r, launchFlags, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 if (top != null) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003537 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 deliverNewIntentLocked(top, r.intent);
3539 // For paranoia, make sure we have correctly
3540 // resumed the top activity.
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003541 if (doResume) {
3542 resumeTopActivityLocked(null);
3543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 return START_DELIVERED_TO_TOP;
3545 }
3546 } else if (!addingToTask &&
3547 (launchFlags&Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
3548 // In this case, we are launching an activity in our own task
3549 // that may already be running somewhere in the history, and
3550 // we want to shuffle it to the front of the stack if so.
3551 int where = findActivityInHistoryLocked(r, sourceRecord.task.taskId);
3552 if (where >= 0) {
3553 HistoryRecord top = moveActivityToFrontLocked(where);
Doug Zongker2bec3d42009-12-04 12:52:44 -08003554 logStartActivity(EventLogTags.AM_NEW_INTENT, r, top.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 deliverNewIntentLocked(top, r.intent);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003556 if (doResume) {
3557 resumeTopActivityLocked(null);
3558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 return START_DELIVERED_TO_TOP;
3560 }
3561 }
3562 // An existing activity is starting this new activity, so we want
3563 // to keep the new one in the same task as the one that is starting
3564 // it.
3565 r.task = sourceRecord.task;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003566 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 + " in existing task " + r.task);
3568
3569 } else {
3570 // This not being started from an existing activity, and not part
3571 // of a new task... just put it in the top task, though these days
3572 // this case should never happen.
3573 final int N = mHistory.size();
3574 HistoryRecord prev =
3575 N > 0 ? (HistoryRecord)mHistory.get(N-1) : null;
3576 r.task = prev != null
3577 ? prev.task
3578 : new TaskRecord(mCurTask, r.info, intent,
3579 (r.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08003580 if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 + " in new guessed " + r.task);
3582 }
3583 if (newTask) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003584 EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, r.task.taskId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08003586 logStartActivity(EventLogTags.AM_CREATE_ACTIVITY, r, r.task);
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003587 startActivityLocked(r, newTask, doResume);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 return START_SUCCESS;
3589 }
3590
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003591 void reportActivityLaunchedLocked(boolean timeout, HistoryRecord r,
3592 long thisTime, long totalTime) {
3593 for (int i=mWaitingActivityLaunched.size()-1; i>=0; i--) {
3594 WaitResult w = mWaitingActivityLaunched.get(i);
3595 w.timeout = timeout;
3596 if (r != null) {
3597 w.who = new ComponentName(r.info.packageName, r.info.name);
3598 }
3599 w.thisTime = thisTime;
3600 w.totalTime = totalTime;
3601 }
3602 notify();
3603 }
3604
3605 void reportActivityVisibleLocked(HistoryRecord r) {
3606 for (int i=mWaitingActivityVisible.size()-1; i>=0; i--) {
3607 WaitResult w = mWaitingActivityVisible.get(i);
3608 w.timeout = false;
3609 if (r != null) {
3610 w.who = new ComponentName(r.info.packageName, r.info.name);
3611 }
3612 w.totalTime = SystemClock.uptimeMillis() - w.thisTime;
3613 w.thisTime = w.totalTime;
3614 }
3615 notify();
3616 }
3617
3618 private final int startActivityMayWait(IApplicationThread caller,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
3620 int grantedMode, IBinder resultTo,
3621 String resultWho, int requestCode, boolean onlyIfNeeded,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003622 boolean debug, WaitResult outResult, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 // Refuse possible leaked file descriptors
3624 if (intent != null && intent.hasFileDescriptors()) {
3625 throw new IllegalArgumentException("File descriptors passed in Intent");
3626 }
3627
The Android Open Source Project4df24232009-03-05 14:34:35 -08003628 final boolean componentSpecified = intent.getComponent() != null;
3629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 // Don't modify the client's object!
3631 intent = new Intent(intent);
3632
3633 // Collect information about the target of the Intent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 ActivityInfo aInfo;
3635 try {
3636 ResolveInfo rInfo =
3637 ActivityThread.getPackageManager().resolveIntent(
3638 intent, resolvedType,
3639 PackageManager.MATCH_DEFAULT_ONLY
Dianne Hackborn1655be42009-05-08 14:29:01 -07003640 | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 aInfo = rInfo != null ? rInfo.activityInfo : null;
3642 } catch (RemoteException e) {
3643 aInfo = null;
3644 }
3645
3646 if (aInfo != null) {
3647 // Store the found target back into the intent, because now that
3648 // we have it we never want to do this again. For example, if the
3649 // user navigates back to this point in the history, we should
3650 // always restart the exact same activity.
3651 intent.setComponent(new ComponentName(
3652 aInfo.applicationInfo.packageName, aInfo.name));
3653
3654 // Don't debug things in the system process
3655 if (debug) {
3656 if (!aInfo.processName.equals("system")) {
3657 setDebugApp(aInfo.processName, true, false);
3658 }
3659 }
3660 }
3661
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003662 synchronized (this) {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08003663 int callingPid;
3664 int callingUid;
3665 if (caller == null) {
3666 callingPid = Binder.getCallingPid();
3667 callingUid = Binder.getCallingUid();
3668 } else {
3669 callingPid = callingUid = -1;
3670 }
Dianne Hackbornd49258f2010-03-26 00:44:29 -07003671
3672 mConfigWillChange = config != null && mConfiguration.diff(config) != 0;
Dianne Hackborne2522462010-03-29 18:41:30 -07003673 if (DEBUG_CONFIGURATION) Slog.v(TAG,
3674 "Starting activity when config will change = " + mConfigWillChange);
Dianne Hackbornd49258f2010-03-26 00:44:29 -07003675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornd49258f2010-03-26 00:44:29 -07003677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 int res = startActivityLocked(caller, intent, resolvedType,
3679 grantedUriPermissions, grantedMode, aInfo,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08003680 resultTo, resultWho, requestCode, callingPid, callingUid,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003681 onlyIfNeeded, componentSpecified);
Dianne Hackbornd49258f2010-03-26 00:44:29 -07003682
Dianne Hackborne2522462010-03-29 18:41:30 -07003683 if (mConfigWillChange) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003684 // If the caller also wants to switch to a new configuration,
3685 // do so now. This allows a clean switch, as we are waiting
3686 // for the current activity to pause (so we will not destroy
3687 // it), and have not yet started the next activity.
3688 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
3689 "updateConfiguration()");
Dianne Hackborne2522462010-03-29 18:41:30 -07003690 mConfigWillChange = false;
3691 if (DEBUG_CONFIGURATION) Slog.v(TAG,
3692 "Updating to new configuration after starting activity.");
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003693 updateConfigurationLocked(config, null);
3694 }
Dianne Hackbornd49258f2010-03-26 00:44:29 -07003695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 Binder.restoreCallingIdentity(origId);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003697
3698 if (outResult != null) {
3699 outResult.result = res;
3700 if (res == IActivityManager.START_SUCCESS) {
3701 mWaitingActivityLaunched.add(outResult);
3702 do {
3703 try {
3704 wait();
3705 } catch (InterruptedException e) {
3706 }
3707 } while (!outResult.timeout && outResult.who == null);
3708 } else if (res == IActivityManager.START_TASK_TO_FRONT) {
3709 HistoryRecord r = this.topRunningActivityLocked(null);
3710 if (r.nowVisible) {
3711 outResult.timeout = false;
3712 outResult.who = new ComponentName(r.info.packageName, r.info.name);
3713 outResult.totalTime = 0;
3714 outResult.thisTime = 0;
3715 } else {
3716 outResult.thisTime = SystemClock.uptimeMillis();
3717 mWaitingActivityVisible.add(outResult);
3718 do {
3719 try {
3720 wait();
3721 } catch (InterruptedException e) {
3722 }
3723 } while (!outResult.timeout && outResult.who == null);
3724 }
3725 }
3726 }
3727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003728 return res;
3729 }
3730 }
3731
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003732 public final int startActivity(IApplicationThread caller,
3733 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
3734 int grantedMode, IBinder resultTo,
3735 String resultWho, int requestCode, boolean onlyIfNeeded,
3736 boolean debug) {
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003737 return startActivityMayWait(caller, intent, resolvedType,
3738 grantedUriPermissions, grantedMode, resultTo, resultWho,
3739 requestCode, onlyIfNeeded, debug, null, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003740 }
3741
3742 public final WaitResult startActivityAndWait(IApplicationThread caller,
3743 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
3744 int grantedMode, IBinder resultTo,
3745 String resultWho, int requestCode, boolean onlyIfNeeded,
3746 boolean debug) {
3747 WaitResult res = new WaitResult();
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003748 startActivityMayWait(caller, intent, resolvedType,
3749 grantedUriPermissions, grantedMode, resultTo, resultWho,
3750 requestCode, onlyIfNeeded, debug, res, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003751 return res;
3752 }
3753
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07003754 public final int startActivityWithConfig(IApplicationThread caller,
3755 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
3756 int grantedMode, IBinder resultTo,
3757 String resultWho, int requestCode, boolean onlyIfNeeded,
3758 boolean debug, Configuration config) {
3759 return startActivityMayWait(caller, intent, resolvedType,
3760 grantedUriPermissions, grantedMode, resultTo, resultWho,
3761 requestCode, onlyIfNeeded, debug, null, config);
3762 }
3763
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08003764 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003765 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003766 IBinder resultTo, String resultWho, int requestCode,
3767 int flagsMask, int flagsValues) {
3768 // Refuse possible leaked file descriptors
3769 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
3770 throw new IllegalArgumentException("File descriptors passed in Intent");
3771 }
3772
3773 IIntentSender sender = intent.getTarget();
3774 if (!(sender instanceof PendingIntentRecord)) {
3775 throw new IllegalArgumentException("Bad PendingIntent object");
3776 }
3777
3778 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07003779
3780 synchronized (this) {
3781 // If this is coming from the currently resumed activity, it is
3782 // effectively saying that app switches are allowed at this point.
3783 if (mResumedActivity != null
3784 && mResumedActivity.info.applicationInfo.uid ==
3785 Binder.getCallingUid()) {
3786 mAppSwitchesAllowedTime = 0;
3787 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07003788 }
3789
3790 return pir.sendInner(0, fillInIntent, resolvedType,
3791 null, resultTo, resultWho, requestCode, flagsMask, flagsValues);
3792 }
3793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 public boolean startNextMatchingActivity(IBinder callingActivity,
3795 Intent intent) {
3796 // Refuse possible leaked file descriptors
3797 if (intent != null && intent.hasFileDescriptors() == true) {
3798 throw new IllegalArgumentException("File descriptors passed in Intent");
3799 }
3800
3801 synchronized (this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07003802 int index = indexOfTokenLocked(callingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 if (index < 0) {
3804 return false;
3805 }
3806 HistoryRecord r = (HistoryRecord)mHistory.get(index);
3807 if (r.app == null || r.app.thread == null) {
3808 // The caller is not running... d'oh!
3809 return false;
3810 }
3811 intent = new Intent(intent);
3812 // The caller is not allowed to change the data.
3813 intent.setDataAndType(r.intent.getData(), r.intent.getType());
3814 // And we are resetting to find the next component...
3815 intent.setComponent(null);
3816
3817 ActivityInfo aInfo = null;
3818 try {
3819 List<ResolveInfo> resolves =
3820 ActivityThread.getPackageManager().queryIntentActivities(
3821 intent, r.resolvedType,
Dianne Hackborn1655be42009-05-08 14:29:01 -07003822 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823
3824 // Look for the original activity in the list...
3825 final int N = resolves != null ? resolves.size() : 0;
3826 for (int i=0; i<N; i++) {
3827 ResolveInfo rInfo = resolves.get(i);
3828 if (rInfo.activityInfo.packageName.equals(r.packageName)
3829 && rInfo.activityInfo.name.equals(r.info.name)) {
3830 // We found the current one... the next matching is
3831 // after it.
3832 i++;
3833 if (i<N) {
3834 aInfo = resolves.get(i).activityInfo;
3835 }
3836 break;
3837 }
3838 }
3839 } catch (RemoteException e) {
3840 }
3841
3842 if (aInfo == null) {
3843 // Nobody who is next!
3844 return false;
3845 }
3846
3847 intent.setComponent(new ComponentName(
3848 aInfo.applicationInfo.packageName, aInfo.name));
3849 intent.setFlags(intent.getFlags()&~(
3850 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
3851 Intent.FLAG_ACTIVITY_CLEAR_TOP|
3852 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
3853 Intent.FLAG_ACTIVITY_NEW_TASK));
3854
3855 // Okay now we need to start the new activity, replacing the
3856 // currently running activity. This is a little tricky because
3857 // we want to start the new one as if the current one is finished,
3858 // but not finish the current one first so that there is no flicker.
3859 // And thus...
3860 final boolean wasFinishing = r.finishing;
3861 r.finishing = true;
3862
3863 // Propagate reply information over to the new activity.
3864 final HistoryRecord resultTo = r.resultTo;
3865 final String resultWho = r.resultWho;
3866 final int requestCode = r.requestCode;
3867 r.resultTo = null;
3868 if (resultTo != null) {
3869 resultTo.removeResultsLocked(r, resultWho, requestCode);
3870 }
3871
3872 final long origId = Binder.clearCallingIdentity();
3873 // XXX we are not dealing with propagating grantedUriPermissions...
3874 // those are not yet exposed to user code, so there is no need.
3875 int res = startActivityLocked(r.app.thread, intent,
3876 r.resolvedType, null, 0, aInfo, resultTo, resultWho,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003877 requestCode, -1, r.launchedFromUid, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 Binder.restoreCallingIdentity(origId);
3879
3880 r.finishing = wasFinishing;
3881 if (res != START_SUCCESS) {
3882 return false;
3883 }
3884 return true;
3885 }
3886 }
3887
Dianne Hackborn2d91af02009-07-16 13:34:33 -07003888 public final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 Intent intent, String resolvedType, IBinder resultTo,
3890 String resultWho, int requestCode, boolean onlyIfNeeded) {
Dianne Hackborn2d91af02009-07-16 13:34:33 -07003891
3892 // This is so super not safe, that only the system (or okay root)
3893 // can do it.
3894 final int callingUid = Binder.getCallingUid();
3895 if (callingUid != 0 && callingUid != Process.myUid()) {
3896 throw new SecurityException(
3897 "startActivityInPackage only available to the system");
3898 }
3899
The Android Open Source Project4df24232009-03-05 14:34:35 -08003900 final boolean componentSpecified = intent.getComponent() != null;
3901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 // Don't modify the client's object!
3903 intent = new Intent(intent);
3904
3905 // Collect information about the target of the Intent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 ActivityInfo aInfo;
3907 try {
3908 ResolveInfo rInfo =
3909 ActivityThread.getPackageManager().resolveIntent(
3910 intent, resolvedType,
Dianne Hackborn1655be42009-05-08 14:29:01 -07003911 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 aInfo = rInfo != null ? rInfo.activityInfo : null;
3913 } catch (RemoteException e) {
3914 aInfo = null;
3915 }
3916
3917 if (aInfo != null) {
3918 // Store the found target back into the intent, because now that
3919 // we have it we never want to do this again. For example, if the
3920 // user navigates back to this point in the history, we should
3921 // always restart the exact same activity.
3922 intent.setComponent(new ComponentName(
3923 aInfo.applicationInfo.packageName, aInfo.name));
3924 }
3925
3926 synchronized(this) {
3927 return startActivityLocked(null, intent, resolvedType,
3928 null, 0, aInfo, resultTo, resultWho, requestCode, -1, uid,
The Android Open Source Project4df24232009-03-05 14:34:35 -08003929 onlyIfNeeded, componentSpecified);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 }
3931 }
3932
Josh Bartel7f208742010-02-25 11:01:44 -06003933 private final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 // Remove any existing entries that are the same kind of task.
3935 int N = mRecentTasks.size();
3936 for (int i=0; i<N; i++) {
3937 TaskRecord tr = mRecentTasks.get(i);
3938 if ((task.affinity != null && task.affinity.equals(tr.affinity))
3939 || (task.intent != null && task.intent.filterEquals(tr.intent))) {
3940 mRecentTasks.remove(i);
3941 i--;
3942 N--;
3943 if (task.intent == null) {
3944 // If the new recent task we are adding is not fully
3945 // specified, then replace it with the existing recent task.
3946 task = tr;
3947 }
3948 }
3949 }
3950 if (N >= MAX_RECENT_TASKS) {
3951 mRecentTasks.remove(N-1);
3952 }
3953 mRecentTasks.add(0, task);
3954 }
3955
3956 public void setRequestedOrientation(IBinder token,
3957 int requestedOrientation) {
3958 synchronized (this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07003959 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 if (index < 0) {
3961 return;
3962 }
3963 HistoryRecord r = (HistoryRecord)mHistory.get(index);
3964 final long origId = Binder.clearCallingIdentity();
3965 mWindowManager.setAppOrientation(r, requestedOrientation);
3966 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07003967 mConfiguration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 r.mayFreezeScreenLocked(r.app) ? r : null);
3969 if (config != null) {
3970 r.frozenBeforeDestroy = true;
3971 if (!updateConfigurationLocked(config, r)) {
3972 resumeTopActivityLocked(null);
3973 }
3974 }
3975 Binder.restoreCallingIdentity(origId);
3976 }
3977 }
3978
3979 public int getRequestedOrientation(IBinder token) {
3980 synchronized (this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07003981 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 if (index < 0) {
3983 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
3984 }
3985 HistoryRecord r = (HistoryRecord)mHistory.get(index);
3986 return mWindowManager.getAppOrientation(r);
3987 }
3988 }
3989
3990 private final void stopActivityLocked(HistoryRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003991 if (DEBUG_SWITCH) Slog.d(TAG, "Stopping: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 if ((r.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_HISTORY) != 0
3993 || (r.info.flags&ActivityInfo.FLAG_NO_HISTORY) != 0) {
3994 if (!r.finishing) {
3995 requestFinishActivityLocked(r, Activity.RESULT_CANCELED, null,
3996 "no-history");
3997 }
3998 } else if (r.app != null && r.app.thread != null) {
3999 if (mFocusedActivity == r) {
4000 setFocusedActivityLocked(topRunningActivityLocked(null));
4001 }
4002 r.resumeKeyDispatchingLocked();
4003 try {
4004 r.stopped = false;
4005 r.state = ActivityState.STOPPING;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004006 if (DEBUG_VISBILITY) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 TAG, "Stopping visible=" + r.visible + " for " + r);
4008 if (!r.visible) {
4009 mWindowManager.setAppVisibility(r, false);
4010 }
4011 r.app.thread.scheduleStopActivity(r, r.visible, r.configChangeFlags);
4012 } catch (Exception e) {
4013 // Maybe just ignore exceptions here... if the process
4014 // has crashed, our death notification will clean things
4015 // up.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004016 Slog.w(TAG, "Exception thrown during pause", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 // Just in case, assume it to be stopped.
4018 r.stopped = true;
4019 r.state = ActivityState.STOPPED;
4020 if (r.configDestroy) {
4021 destroyActivityLocked(r, true);
4022 }
4023 }
4024 }
4025 }
4026
4027 /**
4028 * @return Returns true if the activity is being finished, false if for
4029 * some reason it is being left as-is.
4030 */
4031 private final boolean requestFinishActivityLocked(IBinder token, int resultCode,
4032 Intent resultData, String reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004033 if (DEBUG_RESULTS) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 TAG, "Finishing activity: token=" + token
4035 + ", result=" + resultCode + ", data=" + resultData);
4036
Dianne Hackborn75b03852009-06-12 15:43:26 -07004037 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004038 if (index < 0) {
4039 return false;
4040 }
4041 HistoryRecord r = (HistoryRecord)mHistory.get(index);
4042
4043 // Is this the last activity left?
4044 boolean lastActivity = true;
4045 for (int i=mHistory.size()-1; i>=0; i--) {
4046 HistoryRecord p = (HistoryRecord)mHistory.get(i);
4047 if (!p.finishing && p != r) {
4048 lastActivity = false;
4049 break;
4050 }
4051 }
4052
4053 // If this is the last activity, but it is the home activity, then
4054 // just don't finish it.
4055 if (lastActivity) {
4056 if (r.intent.hasCategory(Intent.CATEGORY_HOME)) {
4057 return false;
4058 }
4059 }
4060
4061 finishActivityLocked(r, index, resultCode, resultData, reason);
4062 return true;
4063 }
4064
4065 /**
4066 * @return Returns true if this activity has been removed from the history
4067 * list, or false if it is still in the list and will be removed later.
4068 */
4069 private final boolean finishActivityLocked(HistoryRecord r, int index,
4070 int resultCode, Intent resultData, String reason) {
4071 if (r.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004072 Slog.w(TAG, "Duplicate finish request for " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 return false;
4074 }
4075
4076 r.finishing = true;
Doug Zongker2bec3d42009-12-04 12:52:44 -08004077 EventLog.writeEvent(EventLogTags.AM_FINISH_ACTIVITY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 System.identityHashCode(r),
4079 r.task.taskId, r.shortComponentName, reason);
4080 r.task.numActivities--;
4081 if (r.frontOfTask && index < (mHistory.size()-1)) {
4082 HistoryRecord next = (HistoryRecord)mHistory.get(index+1);
4083 if (next.task == r.task) {
4084 next.frontOfTask = true;
4085 }
4086 }
4087
4088 r.pauseKeyDispatchingLocked();
4089 if (mFocusedActivity == r) {
4090 setFocusedActivityLocked(topRunningActivityLocked(null));
4091 }
4092
4093 // send the result
4094 HistoryRecord resultTo = r.resultTo;
4095 if (resultTo != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004096 if (DEBUG_RESULTS) Slog.v(TAG, "Adding result to " + resultTo
Chris Tate8a7dc172009-03-24 20:11:42 -07004097 + " who=" + r.resultWho + " req=" + r.requestCode
4098 + " res=" + resultCode + " data=" + resultData);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 if (r.info.applicationInfo.uid > 0) {
4100 grantUriPermissionFromIntentLocked(r.info.applicationInfo.uid,
4101 r.packageName, resultData, r);
4102 }
4103 resultTo.addResultLocked(r, r.resultWho, r.requestCode, resultCode,
4104 resultData);
4105 r.resultTo = null;
4106 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004107 else if (DEBUG_RESULTS) Slog.v(TAG, "No result destination from " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108
4109 // Make sure this HistoryRecord is not holding on to other resources,
4110 // because clients have remote IPC references to this object so we
4111 // can't assume that will go away and want to avoid circular IPC refs.
4112 r.results = null;
4113 r.pendingResults = null;
4114 r.newIntents = null;
4115 r.icicle = null;
4116
4117 if (mPendingThumbnails.size() > 0) {
4118 // There are clients waiting to receive thumbnails so, in case
4119 // this is an activity that someone is waiting for, add it
4120 // to the pending list so we can correctly update the clients.
4121 mCancelledThumbnails.add(r);
4122 }
4123
4124 if (mResumedActivity == r) {
4125 boolean endTask = index <= 0
4126 || ((HistoryRecord)mHistory.get(index-1)).task != r.task;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004127 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 "Prepare close transition: finishing " + r);
4129 mWindowManager.prepareAppTransition(endTask
4130 ? WindowManagerPolicy.TRANSIT_TASK_CLOSE
4131 : WindowManagerPolicy.TRANSIT_ACTIVITY_CLOSE);
4132
4133 // Tell window manager to prepare for this one to be removed.
4134 mWindowManager.setAppVisibility(r, false);
4135
4136 if (mPausingActivity == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004137 if (DEBUG_PAUSE) Slog.v(TAG, "Finish needs to pause: " + r);
4138 if (DEBUG_USER_LEAVING) Slog.v(TAG, "finish() => pause with userLeaving=false");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 startPausingLocked(false, false);
4140 }
4141
4142 } else if (r.state != ActivityState.PAUSING) {
4143 // If the activity is PAUSING, we will complete the finish once
4144 // it is done pausing; else we can just directly finish it here.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004145 if (DEBUG_PAUSE) Slog.v(TAG, "Finish not pausing: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 return finishCurrentActivityLocked(r, index,
4147 FINISH_AFTER_PAUSE) == null;
4148 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004149 if (DEBUG_PAUSE) Slog.v(TAG, "Finish waiting for pause of: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151
4152 return false;
4153 }
4154
4155 private static final int FINISH_IMMEDIATELY = 0;
4156 private static final int FINISH_AFTER_PAUSE = 1;
4157 private static final int FINISH_AFTER_VISIBLE = 2;
4158
4159 private final HistoryRecord finishCurrentActivityLocked(HistoryRecord r,
4160 int mode) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07004161 final int index = indexOfTokenLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 if (index < 0) {
4163 return null;
4164 }
4165
4166 return finishCurrentActivityLocked(r, index, mode);
4167 }
4168
4169 private final HistoryRecord finishCurrentActivityLocked(HistoryRecord r,
4170 int index, int mode) {
4171 // First things first: if this activity is currently visible,
4172 // and the resumed activity is not yet visible, then hold off on
4173 // finishing until the resumed one becomes visible.
4174 if (mode == FINISH_AFTER_VISIBLE && r.nowVisible) {
4175 if (!mStoppingActivities.contains(r)) {
4176 mStoppingActivities.add(r);
4177 if (mStoppingActivities.size() > 3) {
4178 // If we already have a few activities waiting to stop,
4179 // then give up on things going idle and start clearing
4180 // them out.
4181 Message msg = Message.obtain();
4182 msg.what = ActivityManagerService.IDLE_NOW_MSG;
4183 mHandler.sendMessage(msg);
4184 }
4185 }
4186 r.state = ActivityState.STOPPING;
4187 updateOomAdjLocked();
4188 return r;
4189 }
4190
4191 // make sure the record is cleaned out of other places.
4192 mStoppingActivities.remove(r);
4193 mWaitingVisibleActivities.remove(r);
4194 if (mResumedActivity == r) {
4195 mResumedActivity = null;
4196 }
4197 final ActivityState prevState = r.state;
4198 r.state = ActivityState.FINISHING;
4199
4200 if (mode == FINISH_IMMEDIATELY
4201 || prevState == ActivityState.STOPPED
4202 || prevState == ActivityState.INITIALIZING) {
4203 // If this activity is already stopped, we can just finish
4204 // it right now.
4205 return destroyActivityLocked(r, true) ? null : r;
4206 } else {
4207 // Need to go through the full pause cycle to get this
4208 // activity into the stopped state and then finish it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004209 if (localLOGV) Slog.v(TAG, "Enqueueing pending finish: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 mFinishingActivities.add(r);
4211 resumeTopActivityLocked(null);
4212 }
4213 return r;
4214 }
4215
4216 /**
4217 * This is the internal entry point for handling Activity.finish().
4218 *
4219 * @param token The Binder token referencing the Activity we want to finish.
4220 * @param resultCode Result code, if any, from this Activity.
4221 * @param resultData Result data (Intent), if any, from this Activity.
4222 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11004223 * @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 -08004224 */
4225 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
4226 // Refuse possible leaked file descriptors
4227 if (resultData != null && resultData.hasFileDescriptors() == true) {
4228 throw new IllegalArgumentException("File descriptors passed in Intent");
4229 }
4230
4231 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004232 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 // Find the first activity that is not finishing.
4234 HistoryRecord next = topRunningActivityLocked(token, 0);
4235 if (next != null) {
4236 // ask watcher if this is allowed
4237 boolean resumeOK = true;
4238 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004239 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004240 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004241 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 }
4243
4244 if (!resumeOK) {
4245 return false;
4246 }
4247 }
4248 }
4249 final long origId = Binder.clearCallingIdentity();
4250 boolean res = requestFinishActivityLocked(token, resultCode,
4251 resultData, "app-request");
4252 Binder.restoreCallingIdentity(origId);
4253 return res;
4254 }
4255 }
4256
4257 void sendActivityResultLocked(int callingUid, HistoryRecord r,
4258 String resultWho, int requestCode, int resultCode, Intent data) {
4259
4260 if (callingUid > 0) {
4261 grantUriPermissionFromIntentLocked(callingUid, r.packageName,
4262 data, r);
4263 }
4264
Joe Onorato8a9b2202010-02-26 18:56:32 -08004265 if (DEBUG_RESULTS) Slog.v(TAG, "Send activity result to " + r
The Android Open Source Project10592532009-03-18 17:39:46 -07004266 + " : who=" + resultWho + " req=" + requestCode
4267 + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004268 if (mResumedActivity == r && r.app != null && r.app.thread != null) {
4269 try {
4270 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
4271 list.add(new ResultInfo(resultWho, requestCode,
4272 resultCode, data));
4273 r.app.thread.scheduleSendResult(r, list);
4274 return;
4275 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004276 Slog.w(TAG, "Exception thrown sending result to " + r, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004277 }
4278 }
4279
4280 r.addResultLocked(null, resultWho, requestCode, resultCode, data);
4281 }
4282
4283 public final void finishSubActivity(IBinder token, String resultWho,
4284 int requestCode) {
4285 synchronized(this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07004286 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 if (index < 0) {
4288 return;
4289 }
4290 HistoryRecord self = (HistoryRecord)mHistory.get(index);
4291
4292 final long origId = Binder.clearCallingIdentity();
4293
4294 int i;
4295 for (i=mHistory.size()-1; i>=0; i--) {
4296 HistoryRecord r = (HistoryRecord)mHistory.get(i);
4297 if (r.resultTo == self && r.requestCode == requestCode) {
4298 if ((r.resultWho == null && resultWho == null) ||
4299 (r.resultWho != null && r.resultWho.equals(resultWho))) {
4300 finishActivityLocked(r, i,
4301 Activity.RESULT_CANCELED, null, "request-sub");
4302 }
4303 }
4304 }
4305
4306 Binder.restoreCallingIdentity(origId);
4307 }
4308 }
4309
Dianne Hackborn061d58a2010-03-12 15:07:06 -08004310 public boolean willActivityBeVisible(IBinder token) {
4311 synchronized(this) {
4312 int i;
4313 for (i=mHistory.size()-1; i>=0; i--) {
4314 HistoryRecord r = (HistoryRecord)mHistory.get(i);
4315 if (r == token) {
4316 return true;
4317 }
4318 if (r.fullscreen && !r.finishing) {
4319 return false;
4320 }
4321 }
4322 return true;
4323 }
4324 }
4325
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07004326 public void overridePendingTransition(IBinder token, String packageName,
4327 int enterAnim, int exitAnim) {
4328 synchronized(this) {
4329 int index = indexOfTokenLocked(token);
4330 if (index < 0) {
4331 return;
4332 }
4333 HistoryRecord self = (HistoryRecord)mHistory.get(index);
4334
4335 final long origId = Binder.clearCallingIdentity();
4336
4337 if (self.state == ActivityState.RESUMED
4338 || self.state == ActivityState.PAUSING) {
4339 mWindowManager.overridePendingAppTransition(packageName,
4340 enterAnim, exitAnim);
4341 }
4342
4343 Binder.restoreCallingIdentity(origId);
4344 }
4345 }
4346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 /**
4348 * Perform clean-up of service connections in an activity record.
4349 */
4350 private final void cleanUpActivityServicesLocked(HistoryRecord r) {
4351 // Throw away any services that have been bound by this activity.
4352 if (r.connections != null) {
4353 Iterator<ConnectionRecord> it = r.connections.iterator();
4354 while (it.hasNext()) {
4355 ConnectionRecord c = it.next();
4356 removeConnectionLocked(c, null, r);
4357 }
4358 r.connections = null;
4359 }
4360 }
4361
4362 /**
4363 * Perform the common clean-up of an activity record. This is called both
4364 * as part of destroyActivityLocked() (when destroying the client-side
4365 * representation) and cleaning things up as a result of its hosting
4366 * processing going away, in which case there is no remaining client-side
4367 * state to destroy so only the cleanup here is needed.
4368 */
4369 private final void cleanUpActivityLocked(HistoryRecord r, boolean cleanServices) {
4370 if (mResumedActivity == r) {
4371 mResumedActivity = null;
4372 }
4373 if (mFocusedActivity == r) {
4374 mFocusedActivity = null;
4375 }
4376
4377 r.configDestroy = false;
4378 r.frozenBeforeDestroy = false;
4379
4380 // Make sure this record is no longer in the pending finishes list.
4381 // This could happen, for example, if we are trimming activities
4382 // down to the max limit while they are still waiting to finish.
4383 mFinishingActivities.remove(r);
4384 mWaitingVisibleActivities.remove(r);
4385
4386 // Remove any pending results.
4387 if (r.finishing && r.pendingResults != null) {
4388 for (WeakReference<PendingIntentRecord> apr : r.pendingResults) {
4389 PendingIntentRecord rec = apr.get();
4390 if (rec != null) {
4391 cancelIntentSenderLocked(rec, false);
4392 }
4393 }
4394 r.pendingResults = null;
4395 }
4396
4397 if (cleanServices) {
4398 cleanUpActivityServicesLocked(r);
4399 }
4400
4401 if (mPendingThumbnails.size() > 0) {
4402 // There are clients waiting to receive thumbnails so, in case
4403 // this is an activity that someone is waiting for, add it
4404 // to the pending list so we can correctly update the clients.
4405 mCancelledThumbnails.add(r);
4406 }
4407
4408 // Get rid of any pending idle timeouts.
4409 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
4410 mHandler.removeMessages(IDLE_TIMEOUT_MSG, r);
4411 }
4412
4413 private final void removeActivityFromHistoryLocked(HistoryRecord r) {
4414 if (r.state != ActivityState.DESTROYED) {
4415 mHistory.remove(r);
4416 r.inHistory = false;
4417 r.state = ActivityState.DESTROYED;
4418 mWindowManager.removeAppToken(r);
4419 if (VALIDATE_TOKENS) {
4420 mWindowManager.validateAppTokens(mHistory);
4421 }
4422 cleanUpActivityServicesLocked(r);
4423 removeActivityUriPermissionsLocked(r);
4424 }
4425 }
4426
4427 /**
4428 * Destroy the current CLIENT SIDE instance of an activity. This may be
4429 * called both when actually finishing an activity, or when performing
4430 * a configuration switch where we destroy the current client-side object
4431 * but then create a new client-side object for this same HistoryRecord.
4432 */
4433 private final boolean destroyActivityLocked(HistoryRecord r,
4434 boolean removeFromApp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004435 if (DEBUG_SWITCH) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 TAG, "Removing activity: token=" + r
4437 + ", app=" + (r.app != null ? r.app.processName : "(null)"));
Doug Zongker2bec3d42009-12-04 12:52:44 -08004438 EventLog.writeEvent(EventLogTags.AM_DESTROY_ACTIVITY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 System.identityHashCode(r),
4440 r.task.taskId, r.shortComponentName);
4441
4442 boolean removedFromHistory = false;
4443
4444 cleanUpActivityLocked(r, false);
4445
Dianne Hackborn03abb812010-01-04 18:43:19 -08004446 final boolean hadApp = r.app != null;
4447
4448 if (hadApp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 if (removeFromApp) {
4450 int idx = r.app.activities.indexOf(r);
4451 if (idx >= 0) {
4452 r.app.activities.remove(idx);
4453 }
4454 if (r.persistent) {
4455 decPersistentCountLocked(r.app);
4456 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004457 if (r.app.activities.size() == 0) {
4458 // No longer have activities, so update location in
4459 // LRU list.
4460 updateLruProcessLocked(r.app, true, false);
4461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 }
4463
4464 boolean skipDestroy = false;
4465
4466 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004467 if (DEBUG_SWITCH) Slog.i(TAG, "Destroying: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 r.app.thread.scheduleDestroyActivity(r, r.finishing,
4469 r.configChangeFlags);
4470 } catch (Exception e) {
4471 // We can just ignore exceptions here... if the process
4472 // has crashed, our death notification will clean things
4473 // up.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004474 //Slog.w(TAG, "Exception thrown during finish", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 if (r.finishing) {
4476 removeActivityFromHistoryLocked(r);
4477 removedFromHistory = true;
4478 skipDestroy = true;
4479 }
4480 }
4481
4482 r.app = null;
4483 r.nowVisible = false;
4484
4485 if (r.finishing && !skipDestroy) {
4486 r.state = ActivityState.DESTROYING;
4487 Message msg = mHandler.obtainMessage(DESTROY_TIMEOUT_MSG);
4488 msg.obj = r;
4489 mHandler.sendMessageDelayed(msg, DESTROY_TIMEOUT);
4490 } else {
4491 r.state = ActivityState.DESTROYED;
4492 }
4493 } else {
4494 // remove this record from the history.
4495 if (r.finishing) {
4496 removeActivityFromHistoryLocked(r);
4497 removedFromHistory = true;
4498 } else {
4499 r.state = ActivityState.DESTROYED;
4500 }
4501 }
4502
4503 r.configChangeFlags = 0;
4504
Dianne Hackborn03abb812010-01-04 18:43:19 -08004505 if (!mLRUActivities.remove(r) && hadApp) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004506 Slog.w(TAG, "Activity " + r + " being finished, but not in LRU list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 }
4508
4509 return removedFromHistory;
4510 }
4511
Dianne Hackborn03abb812010-01-04 18:43:19 -08004512 private static void removeHistoryRecordsForAppLocked(ArrayList list, ProcessRecord app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 int i = list.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004514 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 TAG, "Removing app " + app + " from list " + list
4516 + " with " + i + " entries");
4517 while (i > 0) {
4518 i--;
4519 HistoryRecord r = (HistoryRecord)list.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004520 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 TAG, "Record #" + i + " " + r + ": app=" + r.app);
4522 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004523 if (localLOGV) Slog.v(TAG, "Removing this entry!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004524 list.remove(i);
4525 }
4526 }
4527 }
4528
4529 /**
4530 * Main function for removing an existing process from the activity manager
4531 * as a result of that process going away. Clears out all connections
4532 * to the process.
4533 */
4534 private final void handleAppDiedLocked(ProcessRecord app,
4535 boolean restarting) {
4536 cleanUpApplicationRecordLocked(app, restarting, -1);
4537 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004538 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004539 }
4540
4541 // Just in case...
4542 if (mPausingActivity != null && mPausingActivity.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004543 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " + mPausingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 mPausingActivity = null;
4545 }
4546 if (mLastPausedActivity != null && mLastPausedActivity.app == app) {
4547 mLastPausedActivity = null;
4548 }
4549
4550 // Remove this application's activities from active lists.
4551 removeHistoryRecordsForAppLocked(mLRUActivities, app);
4552 removeHistoryRecordsForAppLocked(mStoppingActivities, app);
4553 removeHistoryRecordsForAppLocked(mWaitingVisibleActivities, app);
4554 removeHistoryRecordsForAppLocked(mFinishingActivities, app);
4555
4556 boolean atTop = true;
4557 boolean hasVisibleActivities = false;
4558
4559 // Clean out the history list.
4560 int i = mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08004561 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 TAG, "Removing app " + app + " from history with " + i + " entries");
4563 while (i > 0) {
4564 i--;
4565 HistoryRecord r = (HistoryRecord)mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004566 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 TAG, "Record #" + i + " " + r + ": app=" + r.app);
4568 if (r.app == app) {
4569 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004570 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 TAG, "Removing this entry! frozen=" + r.haveState
4572 + " finishing=" + r.finishing);
4573 mHistory.remove(i);
4574
4575 r.inHistory = false;
4576 mWindowManager.removeAppToken(r);
4577 if (VALIDATE_TOKENS) {
4578 mWindowManager.validateAppTokens(mHistory);
4579 }
4580 removeActivityUriPermissionsLocked(r);
4581
4582 } else {
4583 // We have the current state for this activity, so
4584 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004585 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 TAG, "Keeping entry, setting app to null");
4587 if (r.visible) {
4588 hasVisibleActivities = true;
4589 }
4590 r.app = null;
4591 r.nowVisible = false;
4592 if (!r.haveState) {
4593 r.icicle = null;
4594 }
4595 }
4596
4597 cleanUpActivityLocked(r, true);
4598 r.state = ActivityState.STOPPED;
4599 }
4600 atTop = false;
4601 }
4602
4603 app.activities.clear();
4604
4605 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004606 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 + " running instrumentation " + app.instrumentationClass);
4608 Bundle info = new Bundle();
4609 info.putString("shortMsg", "Process crashed.");
4610 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
4611 }
4612
4613 if (!restarting) {
4614 if (!resumeTopActivityLocked(null)) {
4615 // If there was nothing to resume, and we are not already
4616 // restarting this process, but there is a visible activity that
4617 // is hosted by the process... then make sure all visible
4618 // activities are running, taking care of restarting this
4619 // process.
4620 if (hasVisibleActivities) {
4621 ensureActivitiesVisibleLocked(null, 0);
4622 }
4623 }
4624 }
4625 }
4626
4627 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
4628 IBinder threadBinder = thread.asBinder();
4629
4630 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004631 for (int i=mLruProcesses.size()-1; i>=0; i--) {
4632 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
4634 return i;
4635 }
4636 }
4637 return -1;
4638 }
4639
4640 private final ProcessRecord getRecordForAppLocked(
4641 IApplicationThread thread) {
4642 if (thread == null) {
4643 return null;
4644 }
4645
4646 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004647 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 }
4649
4650 private final void appDiedLocked(ProcessRecord app, int pid,
4651 IApplicationThread thread) {
4652
4653 mProcDeaths[0]++;
4654
Magnus Edlund7bb25812010-02-24 15:45:06 +01004655 // Clean up already done if the process has been re-started.
4656 if (app.pid == pid && app.thread != null &&
4657 app.thread.asBinder() == thread.asBinder()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004658 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 + ") has died.");
Doug Zongker2bec3d42009-12-04 12:52:44 -08004660 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08004661 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 TAG, "Dying app: " + app + ", pid: " + pid
4663 + ", thread: " + thread.asBinder());
4664 boolean doLowMem = app.instrumentationClass == null;
4665 handleAppDiedLocked(app, false);
4666
4667 if (doLowMem) {
4668 // If there are no longer any background processes running,
4669 // and the app that died was not running instrumentation,
4670 // then tell everyone we are now low on memory.
4671 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004672 for (int i=mLruProcesses.size()-1; i>=0; i--) {
4673 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 if (rec.thread != null && rec.setAdj >= HIDDEN_APP_MIN_ADJ) {
4675 haveBg = true;
4676 break;
4677 }
4678 }
4679
4680 if (!haveBg) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004681 Slog.i(TAG, "Low Memory: No more background processes.");
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004682 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004683 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08004684 for (int i=mLruProcesses.size()-1; i>=0; i--) {
4685 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07004686 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004687 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
4688 // The low memory report is overriding any current
4689 // state for a GC request. Make sure to do
4690 // visible/foreground processes first.
4691 if (rec.setAdj <= VISIBLE_APP_ADJ) {
4692 rec.lastRequestedGc = 0;
4693 } else {
4694 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004695 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004696 rec.reportLowMemory = true;
4697 rec.lastLowMemory = now;
4698 mProcessesToGc.remove(rec);
4699 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004700 }
4701 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07004702 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 }
4704 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01004705 } else if (app.pid != pid) {
4706 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004707 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01004708 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08004709 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08004710 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004711 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004712 + thread.asBinder());
4713 }
4714 }
4715
Dan Egnor42471dd2010-01-07 17:25:22 -08004716 /**
4717 * If a stack trace dump file is configured, dump process stack traces.
4718 * @param pids of dalvik VM processes to dump stack traces for
4719 * @return file containing stack traces, or null if no dump file is configured
4720 */
Dan Egnor9bdc94b2010-03-04 14:20:31 -08004721 public static File dumpStackTraces(ArrayList<Integer> pids) {
Dan Egnor42471dd2010-01-07 17:25:22 -08004722 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
4723 if (tracesPath == null || tracesPath.length() == 0) {
4724 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
Dan Egnor42471dd2010-01-07 17:25:22 -08004726
4727 File tracesFile = new File(tracesPath);
4728 try {
4729 File tracesDir = tracesFile.getParentFile();
4730 if (!tracesDir.exists()) tracesFile.mkdirs();
4731 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
4732
4733 if (tracesFile.exists()) tracesFile.delete();
4734 tracesFile.createNewFile();
4735 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
4736 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004737 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08004738 return null;
4739 }
4740
4741 // Use a FileObserver to detect when traces finish writing.
4742 // The order of traces is considered important to maintain for legibility.
4743 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
4744 public synchronized void onEvent(int event, String path) { notify(); }
4745 };
4746
4747 try {
4748 observer.startWatching();
4749 int num = pids.size();
4750 for (int i = 0; i < num; i++) {
4751 synchronized (observer) {
4752 Process.sendSignal(pids.get(i), Process.SIGNAL_QUIT);
4753 observer.wait(200); // Wait for write-close, give up after 200msec
4754 }
4755 }
4756 } catch (InterruptedException e) {
Joe Onorato5d3bea62010-03-01 13:44:29 -08004757 Log.wtf(TAG, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08004758 } finally {
4759 observer.stopWatching();
4760 }
4761
4762 return tracesFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 }
4764
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004765 final void appNotResponding(ProcessRecord app, HistoryRecord activity,
Dan Egnor42471dd2010-01-07 17:25:22 -08004766 HistoryRecord parent, final String annotation) {
Dan Egnor42471dd2010-01-07 17:25:22 -08004767 ArrayList<Integer> pids = new ArrayList<Integer>(20);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004768
4769 synchronized (this) {
4770 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
4771 if (mShuttingDown) {
4772 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
4773 return;
4774 } else if (app.notResponding) {
4775 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
4776 return;
4777 } else if (app.crashing) {
4778 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
4779 return;
4780 }
4781
4782 // In case we come through here for the same app before completing
4783 // this one, mark as anring now so we will bail out.
4784 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08004785
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004786 // Log the ANR to the event log.
4787 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
4788 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08004789
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004790 // Dump thread traces as quickly as we can, starting with "interesting" processes.
4791 pids.add(app.pid);
4792
4793 int parentPid = app.pid;
4794 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
4795 if (parentPid != app.pid) pids.add(parentPid);
4796
4797 if (MY_PID != app.pid && MY_PID != parentPid) pids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08004798
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004799 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
4800 ProcessRecord r = mLruProcesses.get(i);
4801 if (r != null && r.thread != null) {
4802 int pid = r.pid;
4803 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) pids.add(pid);
4804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 }
4806 }
4807
Dan Egnor42471dd2010-01-07 17:25:22 -08004808 File tracesFile = dumpStackTraces(pids);
4809
4810 // Log the ANR to the main log.
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07004811 StringBuilder info = mStringBuilder;
4812 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08004813 info.append("ANR in ").append(app.processName);
4814 if (activity != null && activity.shortComponentName != null) {
4815 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07004816 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08004817 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08004819 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 }
Dan Egnor42471dd2010-01-07 17:25:22 -08004821 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08004822 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824
Dan Egnor42471dd2010-01-07 17:25:22 -08004825 String cpuInfo = null;
4826 if (MONITOR_CPU_USAGE) {
4827 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004828 synchronized (mProcessStatsThread) {
4829 cpuInfo = mProcessStats.printCurrentState();
4830 }
Dan Egnor42471dd2010-01-07 17:25:22 -08004831 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 }
4833
Joe Onorato8a9b2202010-02-26 18:56:32 -08004834 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08004835 if (tracesFile == null) {
4836 // There is no trace file, so dump (only) the alleged culprit's threads to the log
4837 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
4838 }
4839
4840 addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null);
4841
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004842 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004843 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08004844 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
4845 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08004847 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
4848 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004849 }
4850 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004851 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
4853 }
4854
Dan Egnor42471dd2010-01-07 17:25:22 -08004855 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
4856 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
4857 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07004858
4859 synchronized (this) {
4860 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
4861 Process.killProcess(app.pid);
4862 return;
4863 }
4864
4865 // Set the app's notResponding state, and look up the errorReportReceiver
4866 makeAppNotRespondingLocked(app,
4867 activity != null ? activity.shortComponentName : null,
4868 annotation != null ? "ANR " + annotation : "ANR",
4869 info.toString());
4870
4871 // Bring up the infamous App Not Responding dialog
4872 Message msg = Message.obtain();
4873 HashMap map = new HashMap();
4874 msg.what = SHOW_NOT_RESPONDING_MSG;
4875 msg.obj = map;
4876 map.put("app", app);
4877 if (activity != null) {
4878 map.put("activity", activity);
4879 }
4880
4881 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08004882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 }
4884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004885 private final void decPersistentCountLocked(ProcessRecord app)
4886 {
4887 app.persistentActivities--;
4888 if (app.persistentActivities > 0) {
4889 // Still more of 'em...
4890 return;
4891 }
4892 if (app.persistent) {
4893 // Ah, but the application itself is persistent. Whatever!
4894 return;
4895 }
4896
4897 // App is no longer persistent... make sure it and the ones
4898 // following it in the LRU list have the correc oom_adj.
4899 updateOomAdjLocked();
4900 }
4901
4902 public void setPersistent(IBinder token, boolean isPersistent) {
4903 if (checkCallingPermission(android.Manifest.permission.PERSISTENT_ACTIVITY)
4904 != PackageManager.PERMISSION_GRANTED) {
4905 String msg = "Permission Denial: setPersistent() from pid="
4906 + Binder.getCallingPid()
4907 + ", uid=" + Binder.getCallingUid()
4908 + " requires " + android.Manifest.permission.PERSISTENT_ACTIVITY;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004909 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 throw new SecurityException(msg);
4911 }
4912
4913 synchronized(this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07004914 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915 if (index < 0) {
4916 return;
4917 }
4918 HistoryRecord r = (HistoryRecord)mHistory.get(index);
4919 ProcessRecord app = r.app;
4920
Joe Onorato8a9b2202010-02-26 18:56:32 -08004921 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004922 TAG, "Setting persistence " + isPersistent + ": " + r);
4923
4924 if (isPersistent) {
4925 if (r.persistent) {
4926 // Okay okay, I heard you already!
Joe Onorato8a9b2202010-02-26 18:56:32 -08004927 if (localLOGV) Slog.v(TAG, "Already persistent!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004928 return;
4929 }
4930 r.persistent = true;
4931 app.persistentActivities++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004932 if (localLOGV) Slog.v(TAG, "Num persistent now: " + app.persistentActivities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 if (app.persistentActivities > 1) {
4934 // We aren't the first...
Joe Onorato8a9b2202010-02-26 18:56:32 -08004935 if (localLOGV) Slog.v(TAG, "Not the first!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004936 return;
4937 }
4938 if (app.persistent) {
4939 // This would be redundant.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004940 if (localLOGV) Slog.v(TAG, "App is persistent!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 return;
4942 }
4943
4944 // App is now persistent... make sure it and the ones
4945 // following it now have the correct oom_adj.
4946 final long origId = Binder.clearCallingIdentity();
4947 updateOomAdjLocked();
4948 Binder.restoreCallingIdentity(origId);
4949
4950 } else {
4951 if (!r.persistent) {
4952 // Okay okay, I heard you already!
4953 return;
4954 }
4955 r.persistent = false;
4956 final long origId = Binder.clearCallingIdentity();
4957 decPersistentCountLocked(app);
4958 Binder.restoreCallingIdentity(origId);
4959
4960 }
4961 }
4962 }
4963
4964 public boolean clearApplicationUserData(final String packageName,
4965 final IPackageDataObserver observer) {
4966 int uid = Binder.getCallingUid();
4967 int pid = Binder.getCallingPid();
4968 long callingId = Binder.clearCallingIdentity();
4969 try {
4970 IPackageManager pm = ActivityThread.getPackageManager();
4971 int pkgUid = -1;
4972 synchronized(this) {
4973 try {
4974 pkgUid = pm.getPackageUid(packageName);
4975 } catch (RemoteException e) {
4976 }
4977 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004978 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 return false;
4980 }
4981 if (uid == pkgUid || checkComponentPermission(
4982 android.Manifest.permission.CLEAR_APP_USER_DATA,
4983 pid, uid, -1)
4984 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08004985 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 } else {
4987 throw new SecurityException(pid+" does not have permission:"+
4988 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
4989 "for process:"+packageName);
4990 }
4991 }
4992
4993 try {
4994 //clear application user data
4995 pm.clearApplicationUserData(packageName, observer);
4996 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
4997 Uri.fromParts("package", packageName, null));
4998 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Josh Bartel2ecce342010-02-25 10:55:48 -06004999 synchronized (this) {
5000 broadcastIntentLocked(null, null, intent,
5001 null, null, 0, null, null, null,
5002 false, false, MY_PID, Process.SYSTEM_UID);
5003 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 } catch (RemoteException e) {
5005 }
5006 } finally {
5007 Binder.restoreCallingIdentity(callingId);
5008 }
5009 return true;
5010 }
5011
Dianne Hackborn03abb812010-01-04 18:43:19 -08005012 public void killBackgroundProcesses(final String packageName) {
5013 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
5014 != PackageManager.PERMISSION_GRANTED &&
5015 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
5016 != PackageManager.PERMISSION_GRANTED) {
5017 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 + Binder.getCallingPid()
5019 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08005020 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005021 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005022 throw new SecurityException(msg);
5023 }
5024
5025 long callingId = Binder.clearCallingIdentity();
5026 try {
5027 IPackageManager pm = ActivityThread.getPackageManager();
5028 int pkgUid = -1;
5029 synchronized(this) {
5030 try {
5031 pkgUid = pm.getPackageUid(packageName);
5032 } catch (RemoteException e) {
5033 }
5034 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005035 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 return;
5037 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08005038 killPackageProcessesLocked(packageName, pkgUid,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005039 SECONDARY_SERVER_ADJ, false, true);
Dianne Hackborn03abb812010-01-04 18:43:19 -08005040 }
5041 } finally {
5042 Binder.restoreCallingIdentity(callingId);
5043 }
5044 }
5045
5046 public void forceStopPackage(final String packageName) {
5047 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
5048 != PackageManager.PERMISSION_GRANTED) {
5049 String msg = "Permission Denial: forceStopPackage() from pid="
5050 + Binder.getCallingPid()
5051 + ", uid=" + Binder.getCallingUid()
5052 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005053 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08005054 throw new SecurityException(msg);
5055 }
5056
5057 long callingId = Binder.clearCallingIdentity();
5058 try {
5059 IPackageManager pm = ActivityThread.getPackageManager();
5060 int pkgUid = -1;
5061 synchronized(this) {
5062 try {
5063 pkgUid = pm.getPackageUid(packageName);
5064 } catch (RemoteException e) {
5065 }
5066 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005067 Slog.w(TAG, "Invalid packageName: " + packageName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08005068 return;
5069 }
5070 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 }
5072 } finally {
5073 Binder.restoreCallingIdentity(callingId);
5074 }
5075 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07005076
5077 /*
5078 * The pkg name and uid have to be specified.
5079 * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
5080 */
5081 public void killApplicationWithUid(String pkg, int uid) {
5082 if (pkg == null) {
5083 return;
5084 }
5085 // Make sure the uid is valid.
5086 if (uid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005087 Slog.w(TAG, "Invalid uid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07005088 return;
5089 }
5090 int callerUid = Binder.getCallingUid();
5091 // Only the system server can kill an application
5092 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07005093 // Post an aysnc message to kill the application
5094 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
5095 msg.arg1 = uid;
5096 msg.arg2 = 0;
5097 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07005098 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07005099 } else {
5100 throw new SecurityException(callerUid + " cannot kill pkg: " +
5101 pkg);
5102 }
5103 }
5104
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07005105 public void closeSystemDialogs(String reason) {
5106 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
5107 if (reason != null) {
5108 intent.putExtra("reason", reason);
5109 }
5110
5111 final int uid = Binder.getCallingUid();
5112 final long origId = Binder.clearCallingIdentity();
5113 synchronized (this) {
5114 int i = mWatchers.beginBroadcast();
5115 while (i > 0) {
5116 i--;
5117 IActivityWatcher w = mWatchers.getBroadcastItem(i);
5118 if (w != null) {
5119 try {
5120 w.closingSystemDialogs(reason);
5121 } catch (RemoteException e) {
5122 }
5123 }
5124 }
5125 mWatchers.finishBroadcast();
5126
Dianne Hackbornffa42482009-09-23 22:20:11 -07005127 mWindowManager.closeSystemDialogs(reason);
5128
5129 for (i=mHistory.size()-1; i>=0; i--) {
5130 HistoryRecord r = (HistoryRecord)mHistory.get(i);
5131 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
5132 finishActivityLocked(r, i,
5133 Activity.RESULT_CANCELED, null, "close-sys");
5134 }
5135 }
5136
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07005137 broadcastIntentLocked(null, null, intent, null,
5138 null, 0, null, null, null, false, false, -1, uid);
5139 }
5140 Binder.restoreCallingIdentity(origId);
5141 }
5142
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07005143 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07005144 throws RemoteException {
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07005145 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
5146 for (int i=pids.length-1; i>=0; i--) {
5147 infos[i] = new Debug.MemoryInfo();
5148 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07005149 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07005150 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07005151 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07005152
5153 public void killApplicationProcess(String processName, int uid) {
5154 if (processName == null) {
5155 return;
5156 }
5157
5158 int callerUid = Binder.getCallingUid();
5159 // Only the system server can kill an application
5160 if (callerUid == Process.SYSTEM_UID) {
5161 synchronized (this) {
5162 ProcessRecord app = getProcessRecordLocked(processName, uid);
5163 if (app != null) {
5164 try {
5165 app.thread.scheduleSuicide();
5166 } catch (RemoteException e) {
5167 // If the other end already died, then our work here is done.
5168 }
5169 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005170 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07005171 + processName + " / " + uid);
5172 }
5173 }
5174 } else {
5175 throw new SecurityException(callerUid + " cannot kill app process: " +
5176 processName);
5177 }
5178 }
5179
Dianne Hackborn03abb812010-01-04 18:43:19 -08005180 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005181 forceStopPackageLocked(packageName, uid, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
5183 Uri.fromParts("package", packageName, null));
5184 intent.putExtra(Intent.EXTRA_UID, uid);
5185 broadcastIntentLocked(null, null, intent,
5186 null, null, 0, null, null, null,
5187 false, false, MY_PID, Process.SYSTEM_UID);
5188 }
5189
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005190 private final boolean killPackageProcessesLocked(String packageName, int uid,
5191 int minOomAdj, boolean callerWillRestart, boolean doit) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08005192 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193
Dianne Hackborn03abb812010-01-04 18:43:19 -08005194 // Remove all processes this package may have touched: all with the
5195 // same UID (except for the system or root user), and all whose name
5196 // matches the package name.
5197 final String procNamePrefix = packageName + ":";
5198 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
5199 final int NA = apps.size();
5200 for (int ia=0; ia<NA; ia++) {
5201 ProcessRecord app = apps.valueAt(ia);
5202 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005203 if (doit) {
5204 procs.add(app);
5205 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08005206 } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid)
5207 || app.processName.equals(packageName)
5208 || app.processName.startsWith(procNamePrefix)) {
5209 if (app.setAdj >= minOomAdj) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005210 if (!doit) {
5211 return true;
5212 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08005213 app.removed = true;
5214 procs.add(app);
5215 }
5216 }
5217 }
5218 }
5219
5220 int N = procs.size();
5221 for (int i=0; i<N; i++) {
5222 removeProcessLocked(procs.get(i), callerWillRestart);
5223 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005224 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08005225 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08005226
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005227 private final boolean forceStopPackageLocked(String name, int uid,
5228 boolean callerWillRestart, boolean purgeCache, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 int i, N;
5230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 if (uid < 0) {
5232 try {
5233 uid = ActivityThread.getPackageManager().getPackageUid(name);
5234 } catch (RemoteException e) {
5235 }
5236 }
5237
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005238 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005239 Slog.i(TAG, "Force stopping package " + name + " uid=" + uid);
Dianne Hackborn03abb812010-01-04 18:43:19 -08005240
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005241 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
5242 while (badApps.hasNext()) {
5243 SparseArray<Long> ba = badApps.next();
5244 if (ba.get(uid) != null) {
5245 badApps.remove();
5246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 }
5248 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005249
5250 boolean didSomething = killPackageProcessesLocked(name, uid, -100,
5251 callerWillRestart, doit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252
5253 for (i=mHistory.size()-1; i>=0; i--) {
5254 HistoryRecord r = (HistoryRecord)mHistory.get(i);
5255 if (r.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005256 if (!doit) {
5257 return true;
5258 }
5259 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005260 Slog.i(TAG, " Force finishing activity " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005261 if (r.app != null) {
5262 r.app.removed = true;
5263 }
5264 r.app = null;
5265 finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "uninstall");
5266 }
5267 }
5268
5269 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
5270 for (ServiceRecord service : mServices.values()) {
5271 if (service.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005272 if (!doit) {
5273 return true;
5274 }
5275 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005276 Slog.i(TAG, " Force stopping service " + service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 if (service.app != null) {
5278 service.app.removed = true;
5279 }
5280 service.app = null;
5281 services.add(service);
5282 }
5283 }
5284
5285 N = services.size();
5286 for (i=0; i<N; i++) {
5287 bringDownServiceLocked(services.get(i), true);
5288 }
5289
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005290 if (doit) {
5291 if (purgeCache) {
5292 AttributeCache ac = AttributeCache.instance();
5293 if (ac != null) {
5294 ac.removePackage(name);
5295 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08005296 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005297 resumeTopActivityLocked(null);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08005298 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005299
5300 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
5303 private final boolean removeProcessLocked(ProcessRecord app, boolean callerWillRestart) {
5304 final String name = app.processName;
5305 final int uid = app.info.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005306 if (DEBUG_PROCESSES) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 TAG, "Force removing process " + app + " (" + name
5308 + "/" + uid + ")");
5309
5310 mProcessNames.remove(name, uid);
5311 boolean needRestart = false;
5312 if (app.pid > 0 && app.pid != MY_PID) {
5313 int pid = app.pid;
5314 synchronized (mPidsSelfLocked) {
5315 mPidsSelfLocked.remove(pid);
5316 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
5317 }
5318 handleAppDiedLocked(app, true);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08005319 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 Process.killProcess(pid);
5321
5322 if (app.persistent) {
5323 if (!callerWillRestart) {
5324 addAppLocked(app.info);
5325 } else {
5326 needRestart = true;
5327 }
5328 }
5329 } else {
5330 mRemovedProcesses.add(app);
5331 }
5332
5333 return needRestart;
5334 }
5335
5336 private final void processStartTimedOutLocked(ProcessRecord app) {
5337 final int pid = app.pid;
5338 boolean gone = false;
5339 synchronized (mPidsSelfLocked) {
5340 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
5341 if (knownApp != null && knownApp.thread == null) {
5342 mPidsSelfLocked.remove(pid);
5343 gone = true;
5344 }
5345 }
5346
5347 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005348 Slog.w(TAG, "Process " + app + " failed to attach");
Doug Zongker2bec3d42009-12-04 12:52:44 -08005349 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005350 app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005351 mProcessNames.remove(app.processName, app.info.uid);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005352 // Take care of any launching providers waiting for this process.
5353 checkAppInLaunchingProvidersLocked(app, true);
5354 // Take care of any services that are waiting for the process.
5355 for (int i=0; i<mPendingServices.size(); i++) {
5356 ServiceRecord sr = mPendingServices.get(i);
5357 if (app.info.uid == sr.appInfo.uid
5358 && app.processName.equals(sr.processName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005359 Slog.w(TAG, "Forcing bringing down service: " + sr);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005360 mPendingServices.remove(i);
5361 i--;
5362 bringDownServiceLocked(sr, true);
5363 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005365 Process.killProcess(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07005366 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005367 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07005368 try {
5369 IBackupManager bm = IBackupManager.Stub.asInterface(
5370 ServiceManager.getService(Context.BACKUP_SERVICE));
5371 bm.agentDisconnected(app.info.packageName);
5372 } catch (RemoteException e) {
5373 // Can't happen; the backup manager is local
5374 }
5375 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005376 if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005377 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Dianne Hackbornf670ef72009-11-16 13:59:16 -08005378 mPendingBroadcast = null;
5379 scheduleBroadcastsLocked();
5380 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005382 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384 }
5385
5386 private final boolean attachApplicationLocked(IApplicationThread thread,
5387 int pid) {
5388
5389 // Find the application record that is being attached... either via
5390 // the pid if we are running in multiple processes, or just pull the
5391 // next app record if we are emulating process with anonymous threads.
5392 ProcessRecord app;
5393 if (pid != MY_PID && pid >= 0) {
5394 synchronized (mPidsSelfLocked) {
5395 app = mPidsSelfLocked.get(pid);
5396 }
5397 } else if (mStartingProcesses.size() > 0) {
5398 app = mStartingProcesses.remove(0);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07005399 app.setPid(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005400 } else {
5401 app = null;
5402 }
5403
5404 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005405 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08005407 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 if (pid > 0 && pid != MY_PID) {
5409 Process.killProcess(pid);
5410 } else {
5411 try {
5412 thread.scheduleExit();
5413 } catch (Exception e) {
5414 // Ignore exceptions.
5415 }
5416 }
5417 return false;
5418 }
5419
5420 // If this application record is still attached to a previous
5421 // process, clean it up now.
5422 if (app.thread != null) {
5423 handleAppDiedLocked(app, true);
5424 }
5425
5426 // Tell the process all about itself.
5427
Joe Onorato8a9b2202010-02-26 18:56:32 -08005428 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005429 TAG, "Binding process pid " + pid + " to record " + app);
5430
5431 String processName = app.processName;
5432 try {
5433 thread.asBinder().linkToDeath(new AppDeathRecipient(
5434 app, pid, thread), 0);
5435 } catch (RemoteException e) {
5436 app.resetPackageList();
5437 startProcessLocked(app, "link fail", processName);
5438 return false;
5439 }
5440
Doug Zongker2bec3d42009-12-04 12:52:44 -08005441 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442
5443 app.thread = thread;
5444 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08005445 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
5446 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 app.forcingToForeground = null;
5448 app.foregroundServices = false;
5449 app.debugging = false;
5450
5451 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
5452
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005453 boolean normalMode = mSystemReady || isAllowedWhileBooting(app.info);
5454 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005455
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005456 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005457 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005458 }
5459
Joe Onorato8a9b2202010-02-26 18:56:32 -08005460 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005461 TAG, "New app record " + app
5462 + " thread=" + thread.asBinder() + " pid=" + pid);
5463 try {
5464 int testMode = IApplicationThread.DEBUG_OFF;
5465 if (mDebugApp != null && mDebugApp.equals(processName)) {
5466 testMode = mWaitForDebugger
5467 ? IApplicationThread.DEBUG_WAIT
5468 : IApplicationThread.DEBUG_ON;
5469 app.debugging = true;
5470 if (mDebugTransient) {
5471 mDebugApp = mOrigDebugApp;
5472 mWaitForDebugger = mOrigWaitForDebugger;
5473 }
5474 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005475
Christopher Tate181fafa2009-05-14 11:12:14 -07005476 // If the app is being launched for restore or full backup, set it up specially
5477 boolean isRestrictedBackupMode = false;
5478 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
5479 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
5480 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
5481 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005482
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07005483 ensurePackageDexOpt(app.instrumentationInfo != null
5484 ? app.instrumentationInfo.packageName
5485 : app.info.packageName);
5486 if (app.instrumentationClass != null) {
5487 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07005488 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005489 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07005490 + processName + " with config " + mConfiguration);
Dianne Hackborn1655be42009-05-08 14:29:01 -07005491 thread.bindApplication(processName, app.instrumentationInfo != null
5492 ? app.instrumentationInfo : app.info, providers,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 app.instrumentationClass, app.instrumentationProfileFile,
5494 app.instrumentationArguments, app.instrumentationWatcher, testMode,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005495 isRestrictedBackupMode || !normalMode,
5496 mConfiguration, getCommonServicesLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08005497 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07005498 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 } catch (Exception e) {
5500 // todo: Yikes! What should we do? For now we will try to
5501 // start another process, but that could easily get us in
5502 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08005503 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504
5505 app.resetPackageList();
5506 startProcessLocked(app, "bind fail", processName);
5507 return false;
5508 }
5509
5510 // Remove this record from the list of starting applications.
5511 mPersistentStartingProcesses.remove(app);
5512 mProcessesOnHold.remove(app);
5513
5514 boolean badApp = false;
5515 boolean didSomething = false;
5516
5517 // See if the top visible activity is waiting to run in this process...
5518 HistoryRecord hr = topRunningActivityLocked(null);
5519 if (hr != null) {
5520 if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid
5521 && processName.equals(hr.processName)) {
5522 try {
5523 if (realStartActivityLocked(hr, app, true, true)) {
5524 didSomething = true;
5525 }
5526 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005527 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 + hr.intent.getComponent().flattenToShortString(), e);
5529 badApp = true;
5530 }
5531 } else {
5532 ensureActivitiesVisibleLocked(hr, null, processName, 0);
5533 }
5534 }
5535
5536 // Find any services that should be running in this process...
5537 if (!badApp && mPendingServices.size() > 0) {
5538 ServiceRecord sr = null;
5539 try {
5540 for (int i=0; i<mPendingServices.size(); i++) {
5541 sr = mPendingServices.get(i);
5542 if (app.info.uid != sr.appInfo.uid
5543 || !processName.equals(sr.processName)) {
5544 continue;
5545 }
5546
5547 mPendingServices.remove(i);
5548 i--;
5549 realStartServiceLocked(sr, app);
5550 didSomething = true;
5551 }
5552 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005553 Slog.w(TAG, "Exception in new application when starting service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005554 + sr.shortName, e);
5555 badApp = true;
5556 }
5557 }
5558
5559 // Check if the next broadcast receiver is in this process...
5560 BroadcastRecord br = mPendingBroadcast;
5561 if (!badApp && br != null && br.curApp == app) {
5562 try {
5563 mPendingBroadcast = null;
5564 processCurBroadcastLocked(br, app);
5565 didSomething = true;
5566 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005567 Slog.w(TAG, "Exception in new application when starting receiver "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 + br.curComponent.flattenToShortString(), e);
5569 badApp = true;
5570 logBroadcastReceiverDiscard(br);
5571 finishReceiverLocked(br.receiver, br.resultCode, br.resultData,
5572 br.resultExtras, br.resultAbort, true);
5573 scheduleBroadcastsLocked();
Magnus Edlund7bb25812010-02-24 15:45:06 +01005574 // We need to reset the state if we fails to start the receiver.
5575 br.state = BroadcastRecord.IDLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005576 }
5577 }
5578
Christopher Tate181fafa2009-05-14 11:12:14 -07005579 // Check whether the next backup agent is in this process...
5580 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005581 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07005582 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07005583 try {
5584 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, mBackupTarget.backupMode);
5585 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005586 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07005587 e.printStackTrace();
5588 }
5589 }
5590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005591 if (badApp) {
5592 // todo: Also need to kill application to deal with all
5593 // kinds of exceptions.
5594 handleAppDiedLocked(app, false);
5595 return false;
5596 }
5597
5598 if (!didSomething) {
5599 updateOomAdjLocked();
5600 }
5601
5602 return true;
5603 }
5604
5605 public final void attachApplication(IApplicationThread thread) {
5606 synchronized (this) {
5607 int callingPid = Binder.getCallingPid();
5608 final long origId = Binder.clearCallingIdentity();
5609 attachApplicationLocked(thread, callingPid);
5610 Binder.restoreCallingIdentity(origId);
5611 }
5612 }
5613
Dianne Hackborne88846e2009-09-30 21:34:25 -07005614 public final void activityIdle(IBinder token, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 final long origId = Binder.clearCallingIdentity();
Dianne Hackborne88846e2009-09-30 21:34:25 -07005616 activityIdleInternal(token, false, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 Binder.restoreCallingIdentity(origId);
5618 }
5619
5620 final ArrayList<HistoryRecord> processStoppingActivitiesLocked(
5621 boolean remove) {
5622 int N = mStoppingActivities.size();
5623 if (N <= 0) return null;
5624
5625 ArrayList<HistoryRecord> stops = null;
5626
5627 final boolean nowVisible = mResumedActivity != null
5628 && mResumedActivity.nowVisible
5629 && !mResumedActivity.waitingVisible;
5630 for (int i=0; i<N; i++) {
5631 HistoryRecord s = mStoppingActivities.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005632 if (localLOGV) Slog.v(TAG, "Stopping " + s + ": nowVisible="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 + nowVisible + " waitingVisible=" + s.waitingVisible
5634 + " finishing=" + s.finishing);
5635 if (s.waitingVisible && nowVisible) {
5636 mWaitingVisibleActivities.remove(s);
5637 s.waitingVisible = false;
5638 if (s.finishing) {
5639 // If this activity is finishing, it is sitting on top of
5640 // everyone else but we now know it is no longer needed...
5641 // so get rid of it. Otherwise, we need to go through the
5642 // normal flow and hide it once we determine that it is
5643 // hidden by the activities in front of it.
Joe Onorato8a9b2202010-02-26 18:56:32 -08005644 if (localLOGV) Slog.v(TAG, "Before stopping, can hide: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 mWindowManager.setAppVisibility(s, false);
5646 }
5647 }
5648 if (!s.waitingVisible && remove) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005649 if (localLOGV) Slog.v(TAG, "Ready to stop: " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005650 if (stops == null) {
5651 stops = new ArrayList<HistoryRecord>();
5652 }
5653 stops.add(s);
5654 mStoppingActivities.remove(i);
5655 N--;
5656 i--;
5657 }
5658 }
5659
5660 return stops;
5661 }
5662
5663 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08005664 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005665 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 mWindowManager.enableScreenAfterBoot();
5667 }
5668
Dianne Hackborne88846e2009-09-30 21:34:25 -07005669 final void activityIdleInternal(IBinder token, boolean fromTimeout,
5670 Configuration config) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005671 if (localLOGV) Slog.v(TAG, "Activity idle: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672
5673 ArrayList<HistoryRecord> stops = null;
5674 ArrayList<HistoryRecord> finishes = null;
5675 ArrayList<HistoryRecord> thumbnails = null;
5676 int NS = 0;
5677 int NF = 0;
5678 int NT = 0;
5679 IApplicationThread sendThumbnail = null;
5680 boolean booting = false;
5681 boolean enableScreen = false;
5682
5683 synchronized (this) {
5684 if (token != null) {
5685 mHandler.removeMessages(IDLE_TIMEOUT_MSG, token);
5686 }
5687
5688 // Get the activity record.
Dianne Hackborn75b03852009-06-12 15:43:26 -07005689 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005690 if (index >= 0) {
5691 HistoryRecord r = (HistoryRecord)mHistory.get(index);
5692
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08005693 if (fromTimeout) {
5694 reportActivityLaunchedLocked(fromTimeout, r, -1, -1);
5695 }
5696
Dianne Hackborne88846e2009-09-30 21:34:25 -07005697 // This is a hack to semi-deal with a race condition
5698 // in the client where it can be constructed with a
5699 // newer configuration from when we asked it to launch.
5700 // We'll update with whatever configuration it now says
5701 // it used to launch.
5702 if (config != null) {
5703 r.configuration = config;
5704 }
5705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 // No longer need to keep the device awake.
5707 if (mResumedActivity == r && mLaunchingActivity.isHeld()) {
5708 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
5709 mLaunchingActivity.release();
5710 }
5711
5712 // We are now idle. If someone is waiting for a thumbnail from
5713 // us, we can now deliver.
5714 r.idle = true;
5715 scheduleAppGcsLocked();
5716 if (r.thumbnailNeeded && r.app != null && r.app.thread != null) {
5717 sendThumbnail = r.app.thread;
5718 r.thumbnailNeeded = false;
5719 }
5720
5721 // If this activity is fullscreen, set up to hide those under it.
5722
Joe Onorato8a9b2202010-02-26 18:56:32 -08005723 if (DEBUG_VISBILITY) Slog.v(TAG, "Idle activity for " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 ensureActivitiesVisibleLocked(null, 0);
5725
Joe Onorato8a9b2202010-02-26 18:56:32 -08005726 //Slog.i(TAG, "IDLE: mBooted=" + mBooted + ", fromTimeout=" + fromTimeout);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 if (!mBooted && !fromTimeout) {
5728 mBooted = true;
5729 enableScreen = true;
5730 }
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08005731
5732 } else if (fromTimeout) {
5733 reportActivityLaunchedLocked(fromTimeout, null, -1, -1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 }
5735
5736 // Atomically retrieve all of the other things to do.
5737 stops = processStoppingActivitiesLocked(true);
5738 NS = stops != null ? stops.size() : 0;
5739 if ((NF=mFinishingActivities.size()) > 0) {
5740 finishes = new ArrayList<HistoryRecord>(mFinishingActivities);
5741 mFinishingActivities.clear();
5742 }
5743 if ((NT=mCancelledThumbnails.size()) > 0) {
5744 thumbnails = new ArrayList<HistoryRecord>(mCancelledThumbnails);
5745 mCancelledThumbnails.clear();
5746 }
5747
5748 booting = mBooting;
5749 mBooting = false;
5750 }
5751
5752 int i;
5753
5754 // Send thumbnail if requested.
5755 if (sendThumbnail != null) {
5756 try {
5757 sendThumbnail.requestThumbnail(token);
5758 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005759 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760 sendPendingThumbnail(null, token, null, null, true);
5761 }
5762 }
5763
5764 // Stop any activities that are scheduled to do so but have been
5765 // waiting for the next one to start.
5766 for (i=0; i<NS; i++) {
5767 HistoryRecord r = (HistoryRecord)stops.get(i);
5768 synchronized (this) {
5769 if (r.finishing) {
5770 finishCurrentActivityLocked(r, FINISH_IMMEDIATELY);
5771 } else {
5772 stopActivityLocked(r);
5773 }
5774 }
5775 }
5776
5777 // Finish any activities that are scheduled to do so but have been
5778 // waiting for the next one to start.
5779 for (i=0; i<NF; i++) {
5780 HistoryRecord r = (HistoryRecord)finishes.get(i);
5781 synchronized (this) {
5782 destroyActivityLocked(r, true);
5783 }
5784 }
5785
5786 // Report back to any thumbnail receivers.
5787 for (i=0; i<NT; i++) {
5788 HistoryRecord r = (HistoryRecord)thumbnails.get(i);
5789 sendPendingThumbnail(r, null, null, null, true);
5790 }
5791
5792 if (booting) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005793 finishBooting();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005794 }
5795
5796 trimApplications();
5797 //dump();
5798 //mWindowManager.dump();
5799
5800 if (enableScreen) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005801 enableScreenAfterBoot();
5802 }
5803 }
5804
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005805 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005806 IntentFilter pkgFilter = new IntentFilter();
5807 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
5808 pkgFilter.addDataScheme("package");
5809 mContext.registerReceiver(new BroadcastReceiver() {
5810 @Override
5811 public void onReceive(Context context, Intent intent) {
5812 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
5813 if (pkgs != null) {
5814 for (String pkg : pkgs) {
5815 if (forceStopPackageLocked(pkg, -1, false, false, false)) {
5816 setResultCode(Activity.RESULT_OK);
5817 return;
5818 }
5819 }
5820 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005821 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005822 }, pkgFilter);
5823
5824 synchronized (this) {
5825 // Ensure that any processes we had put on hold are now started
5826 // up.
5827 final int NP = mProcessesOnHold.size();
5828 if (NP > 0) {
5829 ArrayList<ProcessRecord> procs =
5830 new ArrayList<ProcessRecord>(mProcessesOnHold);
5831 for (int ip=0; ip<NP; ip++) {
5832 this.startProcessLocked(procs.get(ip), "on-hold", null);
5833 }
5834 }
5835
5836 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
5837 // Tell anyone interested that we are done booting!
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005838 broadcastIntentLocked(null, null,
5839 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
5840 null, null, 0, null, null,
5841 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
5842 false, false, MY_PID, Process.SYSTEM_UID);
5843 }
5844 }
5845 }
5846
5847 final void ensureBootCompleted() {
5848 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005849 boolean enableScreen;
5850 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005851 booting = mBooting;
5852 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005853 enableScreen = !mBooted;
5854 mBooted = true;
5855 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005856
5857 if (booting) {
5858 finishBooting();
5859 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005860
5861 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005862 enableScreenAfterBoot();
5863 }
5864 }
5865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 public final void activityPaused(IBinder token, Bundle icicle) {
5867 // Refuse possible leaked file descriptors
5868 if (icicle != null && icicle.hasFileDescriptors()) {
5869 throw new IllegalArgumentException("File descriptors passed in Bundle");
5870 }
5871
5872 final long origId = Binder.clearCallingIdentity();
5873 activityPaused(token, icicle, false);
5874 Binder.restoreCallingIdentity(origId);
5875 }
5876
5877 final void activityPaused(IBinder token, Bundle icicle, boolean timeout) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005878 if (DEBUG_PAUSE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005879 TAG, "Activity paused: token=" + token + ", icicle=" + icicle
5880 + ", timeout=" + timeout);
5881
5882 HistoryRecord r = null;
5883
5884 synchronized (this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07005885 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005886 if (index >= 0) {
5887 r = (HistoryRecord)mHistory.get(index);
5888 if (!timeout) {
5889 r.icicle = icicle;
5890 r.haveState = true;
5891 }
5892 mHandler.removeMessages(PAUSE_TIMEOUT_MSG, r);
5893 if (mPausingActivity == r) {
5894 r.state = ActivityState.PAUSED;
5895 completePauseLocked();
5896 } else {
Doug Zongker2bec3d42009-12-04 12:52:44 -08005897 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 System.identityHashCode(r), r.shortComponentName,
5899 mPausingActivity != null
5900 ? mPausingActivity.shortComponentName : "(none)");
5901 }
5902 }
5903 }
5904 }
5905
5906 public final void activityStopped(IBinder token, Bitmap thumbnail,
5907 CharSequence description) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005908 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 TAG, "Activity stopped: token=" + token);
5910
5911 HistoryRecord r = null;
5912
5913 final long origId = Binder.clearCallingIdentity();
5914
5915 synchronized (this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07005916 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005917 if (index >= 0) {
5918 r = (HistoryRecord)mHistory.get(index);
5919 r.thumbnail = thumbnail;
5920 r.description = description;
5921 r.stopped = true;
5922 r.state = ActivityState.STOPPED;
5923 if (!r.finishing) {
5924 if (r.configDestroy) {
5925 destroyActivityLocked(r, true);
5926 resumeTopActivityLocked(null);
5927 }
5928 }
5929 }
5930 }
5931
5932 if (r != null) {
5933 sendPendingThumbnail(r, null, null, null, false);
5934 }
5935
5936 trimApplications();
5937
5938 Binder.restoreCallingIdentity(origId);
5939 }
5940
5941 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005942 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 synchronized (this) {
5944 mHandler.removeMessages(DESTROY_TIMEOUT_MSG, token);
5945
Dianne Hackborn75b03852009-06-12 15:43:26 -07005946 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 if (index >= 0) {
5948 HistoryRecord r = (HistoryRecord)mHistory.get(index);
5949 if (r.state == ActivityState.DESTROYING) {
5950 final long origId = Binder.clearCallingIdentity();
5951 removeActivityFromHistoryLocked(r);
5952 Binder.restoreCallingIdentity(origId);
5953 }
5954 }
5955 }
5956 }
5957
5958 public String getCallingPackage(IBinder token) {
5959 synchronized (this) {
5960 HistoryRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07005961 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005962 }
5963 }
5964
5965 public ComponentName getCallingActivity(IBinder token) {
5966 synchronized (this) {
5967 HistoryRecord r = getCallingRecordLocked(token);
5968 return r != null ? r.intent.getComponent() : null;
5969 }
5970 }
5971
5972 private HistoryRecord getCallingRecordLocked(IBinder token) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07005973 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 if (index >= 0) {
5975 HistoryRecord r = (HistoryRecord)mHistory.get(index);
5976 if (r != null) {
5977 return r.resultTo;
5978 }
5979 }
5980 return null;
5981 }
5982
5983 public ComponentName getActivityClassForToken(IBinder token) {
5984 synchronized(this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07005985 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005986 if (index >= 0) {
5987 HistoryRecord r = (HistoryRecord)mHistory.get(index);
5988 return r.intent.getComponent();
5989 }
5990 return null;
5991 }
5992 }
5993
5994 public String getPackageForToken(IBinder token) {
5995 synchronized(this) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07005996 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005997 if (index >= 0) {
5998 HistoryRecord r = (HistoryRecord)mHistory.get(index);
5999 return r.packageName;
6000 }
6001 return null;
6002 }
6003 }
6004
6005 public IIntentSender getIntentSender(int type,
6006 String packageName, IBinder token, String resultWho,
6007 int requestCode, Intent intent, String resolvedType, int flags) {
6008 // Refuse possible leaked file descriptors
6009 if (intent != null && intent.hasFileDescriptors() == true) {
6010 throw new IllegalArgumentException("File descriptors passed in Intent");
6011 }
6012
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006013 if (type == INTENT_SENDER_BROADCAST) {
6014 if ((intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
6015 throw new IllegalArgumentException(
6016 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
6017 }
6018 }
6019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006020 synchronized(this) {
6021 int callingUid = Binder.getCallingUid();
6022 try {
6023 if (callingUid != 0 && callingUid != Process.SYSTEM_UID &&
6024 Process.supportsProcesses()) {
6025 int uid = ActivityThread.getPackageManager()
6026 .getPackageUid(packageName);
6027 if (uid != Binder.getCallingUid()) {
6028 String msg = "Permission Denial: getIntentSender() from pid="
6029 + Binder.getCallingPid()
6030 + ", uid=" + Binder.getCallingUid()
6031 + ", (need uid=" + uid + ")"
6032 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006033 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006034 throw new SecurityException(msg);
6035 }
6036 }
6037 } catch (RemoteException e) {
6038 throw new SecurityException(e);
6039 }
6040 HistoryRecord activity = null;
6041 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07006042 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006043 if (index < 0) {
6044 return null;
6045 }
6046 activity = (HistoryRecord)mHistory.get(index);
6047 if (activity.finishing) {
6048 return null;
6049 }
6050 }
6051
6052 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
6053 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
6054 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
6055 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
6056 |PendingIntent.FLAG_UPDATE_CURRENT);
6057
6058 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
6059 type, packageName, activity, resultWho,
6060 requestCode, intent, resolvedType, flags);
6061 WeakReference<PendingIntentRecord> ref;
6062 ref = mIntentSenderRecords.get(key);
6063 PendingIntentRecord rec = ref != null ? ref.get() : null;
6064 if (rec != null) {
6065 if (!cancelCurrent) {
6066 if (updateCurrent) {
6067 rec.key.requestIntent.replaceExtras(intent);
6068 }
6069 return rec;
6070 }
6071 rec.canceled = true;
6072 mIntentSenderRecords.remove(key);
6073 }
6074 if (noCreate) {
6075 return rec;
6076 }
6077 rec = new PendingIntentRecord(this, key, callingUid);
6078 mIntentSenderRecords.put(key, rec.ref);
6079 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
6080 if (activity.pendingResults == null) {
6081 activity.pendingResults
6082 = new HashSet<WeakReference<PendingIntentRecord>>();
6083 }
6084 activity.pendingResults.add(rec.ref);
6085 }
6086 return rec;
6087 }
6088 }
6089
6090 public void cancelIntentSender(IIntentSender sender) {
6091 if (!(sender instanceof PendingIntentRecord)) {
6092 return;
6093 }
6094 synchronized(this) {
6095 PendingIntentRecord rec = (PendingIntentRecord)sender;
6096 try {
6097 int uid = ActivityThread.getPackageManager()
6098 .getPackageUid(rec.key.packageName);
6099 if (uid != Binder.getCallingUid()) {
6100 String msg = "Permission Denial: cancelIntentSender() from pid="
6101 + Binder.getCallingPid()
6102 + ", uid=" + Binder.getCallingUid()
6103 + " is not allowed to cancel packges "
6104 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006105 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006106 throw new SecurityException(msg);
6107 }
6108 } catch (RemoteException e) {
6109 throw new SecurityException(e);
6110 }
6111 cancelIntentSenderLocked(rec, true);
6112 }
6113 }
6114
6115 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
6116 rec.canceled = true;
6117 mIntentSenderRecords.remove(rec.key);
6118 if (cleanActivity && rec.key.activity != null) {
6119 rec.key.activity.pendingResults.remove(rec.ref);
6120 }
6121 }
6122
6123 public String getPackageForIntentSender(IIntentSender pendingResult) {
6124 if (!(pendingResult instanceof PendingIntentRecord)) {
6125 return null;
6126 }
6127 synchronized(this) {
6128 try {
6129 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
6130 return res.key.packageName;
6131 } catch (ClassCastException e) {
6132 }
6133 }
6134 return null;
6135 }
6136
6137 public void setProcessLimit(int max) {
6138 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
6139 "setProcessLimit()");
6140 mProcessLimit = max;
6141 }
6142
6143 public int getProcessLimit() {
6144 return mProcessLimit;
6145 }
6146
6147 void foregroundTokenDied(ForegroundToken token) {
6148 synchronized (ActivityManagerService.this) {
6149 synchronized (mPidsSelfLocked) {
6150 ForegroundToken cur
6151 = mForegroundProcesses.get(token.pid);
6152 if (cur != token) {
6153 return;
6154 }
6155 mForegroundProcesses.remove(token.pid);
6156 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
6157 if (pr == null) {
6158 return;
6159 }
6160 pr.forcingToForeground = null;
6161 pr.foregroundServices = false;
6162 }
6163 updateOomAdjLocked();
6164 }
6165 }
6166
6167 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
6168 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
6169 "setProcessForeground()");
6170 synchronized(this) {
6171 boolean changed = false;
6172
6173 synchronized (mPidsSelfLocked) {
6174 ProcessRecord pr = mPidsSelfLocked.get(pid);
6175 if (pr == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006176 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 return;
6178 }
6179 ForegroundToken oldToken = mForegroundProcesses.get(pid);
6180 if (oldToken != null) {
6181 oldToken.token.unlinkToDeath(oldToken, 0);
6182 mForegroundProcesses.remove(pid);
6183 pr.forcingToForeground = null;
6184 changed = true;
6185 }
6186 if (isForeground && token != null) {
6187 ForegroundToken newToken = new ForegroundToken() {
6188 public void binderDied() {
6189 foregroundTokenDied(this);
6190 }
6191 };
6192 newToken.pid = pid;
6193 newToken.token = token;
6194 try {
6195 token.linkToDeath(newToken, 0);
6196 mForegroundProcesses.put(pid, newToken);
6197 pr.forcingToForeground = token;
6198 changed = true;
6199 } catch (RemoteException e) {
6200 // If the process died while doing this, we will later
6201 // do the cleanup with the process death link.
6202 }
6203 }
6204 }
6205
6206 if (changed) {
6207 updateOomAdjLocked();
6208 }
6209 }
6210 }
6211
6212 // =========================================================
6213 // PERMISSIONS
6214 // =========================================================
6215
6216 static class PermissionController extends IPermissionController.Stub {
6217 ActivityManagerService mActivityManagerService;
6218 PermissionController(ActivityManagerService activityManagerService) {
6219 mActivityManagerService = activityManagerService;
6220 }
6221
6222 public boolean checkPermission(String permission, int pid, int uid) {
6223 return mActivityManagerService.checkPermission(permission, pid,
6224 uid) == PackageManager.PERMISSION_GRANTED;
6225 }
6226 }
6227
6228 /**
6229 * This can be called with or without the global lock held.
6230 */
6231 int checkComponentPermission(String permission, int pid, int uid,
6232 int reqUid) {
6233 // We might be performing an operation on behalf of an indirect binder
6234 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
6235 // client identity accordingly before proceeding.
6236 Identity tlsIdentity = sCallerIdentity.get();
6237 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006238 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006239 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
6240 uid = tlsIdentity.uid;
6241 pid = tlsIdentity.pid;
6242 }
6243
6244 // Root, system server and our own process get to do everything.
6245 if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID ||
6246 !Process.supportsProcesses()) {
6247 return PackageManager.PERMISSION_GRANTED;
6248 }
6249 // If the target requires a specific UID, always fail for others.
6250 if (reqUid >= 0 && uid != reqUid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006251 Slog.w(TAG, "Permission denied: checkComponentPermission() reqUid=" + reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006252 return PackageManager.PERMISSION_DENIED;
6253 }
6254 if (permission == null) {
6255 return PackageManager.PERMISSION_GRANTED;
6256 }
6257 try {
6258 return ActivityThread.getPackageManager()
6259 .checkUidPermission(permission, uid);
6260 } catch (RemoteException e) {
6261 // Should never happen, but if it does... deny!
Joe Onorato8a9b2202010-02-26 18:56:32 -08006262 Slog.e(TAG, "PackageManager is dead?!?", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006263 }
6264 return PackageManager.PERMISSION_DENIED;
6265 }
6266
6267 /**
6268 * As the only public entry point for permissions checking, this method
6269 * can enforce the semantic that requesting a check on a null global
6270 * permission is automatically denied. (Internally a null permission
6271 * string is used when calling {@link #checkComponentPermission} in cases
6272 * when only uid-based security is needed.)
6273 *
6274 * This can be called with or without the global lock held.
6275 */
6276 public int checkPermission(String permission, int pid, int uid) {
6277 if (permission == null) {
6278 return PackageManager.PERMISSION_DENIED;
6279 }
6280 return checkComponentPermission(permission, pid, uid, -1);
6281 }
6282
6283 /**
6284 * Binder IPC calls go through the public entry point.
6285 * This can be called with or without the global lock held.
6286 */
6287 int checkCallingPermission(String permission) {
6288 return checkPermission(permission,
6289 Binder.getCallingPid(),
6290 Binder.getCallingUid());
6291 }
6292
6293 /**
6294 * This can be called with or without the global lock held.
6295 */
6296 void enforceCallingPermission(String permission, String func) {
6297 if (checkCallingPermission(permission)
6298 == PackageManager.PERMISSION_GRANTED) {
6299 return;
6300 }
6301
6302 String msg = "Permission Denial: " + func + " from pid="
6303 + Binder.getCallingPid()
6304 + ", uid=" + Binder.getCallingUid()
6305 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006306 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006307 throw new SecurityException(msg);
6308 }
6309
6310 private final boolean checkHoldingPermissionsLocked(IPackageManager pm,
6311 ProviderInfo pi, int uid, int modeFlags) {
6312 try {
6313 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
6314 if ((pi.readPermission != null) &&
6315 (pm.checkUidPermission(pi.readPermission, uid)
6316 != PackageManager.PERMISSION_GRANTED)) {
6317 return false;
6318 }
6319 }
6320 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
6321 if ((pi.writePermission != null) &&
6322 (pm.checkUidPermission(pi.writePermission, uid)
6323 != PackageManager.PERMISSION_GRANTED)) {
6324 return false;
6325 }
6326 }
6327 return true;
6328 } catch (RemoteException e) {
6329 return false;
6330 }
6331 }
6332
6333 private final boolean checkUriPermissionLocked(Uri uri, int uid,
6334 int modeFlags) {
6335 // Root gets to do everything.
6336 if (uid == 0 || !Process.supportsProcesses()) {
6337 return true;
6338 }
6339 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
6340 if (perms == null) return false;
6341 UriPermission perm = perms.get(uri);
6342 if (perm == null) return false;
6343 return (modeFlags&perm.modeFlags) == modeFlags;
6344 }
6345
6346 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
6347 // Another redirected-binder-call permissions check as in
6348 // {@link checkComponentPermission}.
6349 Identity tlsIdentity = sCallerIdentity.get();
6350 if (tlsIdentity != null) {
6351 uid = tlsIdentity.uid;
6352 pid = tlsIdentity.pid;
6353 }
6354
6355 // Our own process gets to do everything.
6356 if (pid == MY_PID) {
6357 return PackageManager.PERMISSION_GRANTED;
6358 }
6359 synchronized(this) {
6360 return checkUriPermissionLocked(uri, uid, modeFlags)
6361 ? PackageManager.PERMISSION_GRANTED
6362 : PackageManager.PERMISSION_DENIED;
6363 }
6364 }
6365
6366 private void grantUriPermissionLocked(int callingUid,
6367 String targetPkg, Uri uri, int modeFlags, HistoryRecord activity) {
6368 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
6369 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
6370 if (modeFlags == 0) {
6371 return;
6372 }
6373
Joe Onorato8a9b2202010-02-26 18:56:32 -08006374 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006375 "Requested grant " + targetPkg + " permission to " + uri);
6376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 final IPackageManager pm = ActivityThread.getPackageManager();
6378
6379 // If this is not a content: uri, we can't do anything with it.
6380 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006381 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006382 "Can't grant URI permission for non-content URI: " + uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006383 return;
6384 }
6385
6386 String name = uri.getAuthority();
6387 ProviderInfo pi = null;
6388 ContentProviderRecord cpr
6389 = (ContentProviderRecord)mProvidersByName.get(name);
6390 if (cpr != null) {
6391 pi = cpr.info;
6392 } else {
6393 try {
6394 pi = pm.resolveContentProvider(name,
6395 PackageManager.GET_URI_PERMISSION_PATTERNS);
6396 } catch (RemoteException ex) {
6397 }
6398 }
6399 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006400 Slog.w(TAG, "No content provider found for: " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 return;
6402 }
6403
6404 int targetUid;
6405 try {
6406 targetUid = pm.getPackageUid(targetPkg);
6407 if (targetUid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006408 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006409 "Can't grant URI permission no uid for: " + targetPkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006410 return;
6411 }
6412 } catch (RemoteException ex) {
6413 return;
6414 }
6415
6416 // First... does the target actually need this permission?
6417 if (checkHoldingPermissionsLocked(pm, pi, targetUid, modeFlags)) {
6418 // No need to grant the target this permission.
Joe Onorato8a9b2202010-02-26 18:56:32 -08006419 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006420 "Target " + targetPkg + " already has full permission to " + uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006421 return;
6422 }
6423
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006424 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006425 if (!pi.grantUriPermissions) {
6426 throw new SecurityException("Provider " + pi.packageName
6427 + "/" + pi.name
6428 + " does not allow granting of Uri permissions (uri "
6429 + uri + ")");
6430 }
6431 if (pi.uriPermissionPatterns != null) {
6432 final int N = pi.uriPermissionPatterns.length;
6433 boolean allowed = false;
6434 for (int i=0; i<N; i++) {
6435 if (pi.uriPermissionPatterns[i] != null
6436 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
6437 allowed = true;
6438 break;
6439 }
6440 }
6441 if (!allowed) {
6442 throw new SecurityException("Provider " + pi.packageName
6443 + "/" + pi.name
6444 + " does not allow granting of permission to path of Uri "
6445 + uri);
6446 }
6447 }
6448
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006449 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006450 // this uri?
6451 if (!checkHoldingPermissionsLocked(pm, pi, callingUid, modeFlags)) {
6452 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
6453 throw new SecurityException("Uid " + callingUid
6454 + " does not have permission to uri " + uri);
6455 }
6456 }
6457
6458 // Okay! So here we are: the caller has the assumed permission
6459 // to the uri, and the target doesn't. Let's now give this to
6460 // the target.
6461
Joe Onorato8a9b2202010-02-26 18:56:32 -08006462 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006463 "Granting " + targetPkg + " permission to " + uri);
6464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006465 HashMap<Uri, UriPermission> targetUris
6466 = mGrantedUriPermissions.get(targetUid);
6467 if (targetUris == null) {
6468 targetUris = new HashMap<Uri, UriPermission>();
6469 mGrantedUriPermissions.put(targetUid, targetUris);
6470 }
6471
6472 UriPermission perm = targetUris.get(uri);
6473 if (perm == null) {
6474 perm = new UriPermission(targetUid, uri);
6475 targetUris.put(uri, perm);
6476
6477 }
6478 perm.modeFlags |= modeFlags;
6479 if (activity == null) {
6480 perm.globalModeFlags |= modeFlags;
6481 } else if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
6482 perm.readActivities.add(activity);
6483 if (activity.readUriPermissions == null) {
6484 activity.readUriPermissions = new HashSet<UriPermission>();
6485 }
6486 activity.readUriPermissions.add(perm);
6487 } else if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
6488 perm.writeActivities.add(activity);
6489 if (activity.writeUriPermissions == null) {
6490 activity.writeUriPermissions = new HashSet<UriPermission>();
6491 }
6492 activity.writeUriPermissions.add(perm);
6493 }
6494 }
6495
6496 private void grantUriPermissionFromIntentLocked(int callingUid,
6497 String targetPkg, Intent intent, HistoryRecord activity) {
6498 if (intent == null) {
6499 return;
6500 }
6501 Uri data = intent.getData();
6502 if (data == null) {
6503 return;
6504 }
6505 grantUriPermissionLocked(callingUid, targetPkg, data,
6506 intent.getFlags(), activity);
6507 }
6508
6509 public void grantUriPermission(IApplicationThread caller, String targetPkg,
6510 Uri uri, int modeFlags) {
6511 synchronized(this) {
6512 final ProcessRecord r = getRecordForAppLocked(caller);
6513 if (r == null) {
6514 throw new SecurityException("Unable to find app for caller "
6515 + caller
6516 + " when granting permission to uri " + uri);
6517 }
6518 if (targetPkg == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006519 Slog.w(TAG, "grantUriPermission: null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006520 return;
6521 }
6522 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006523 Slog.w(TAG, "grantUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 return;
6525 }
6526
6527 grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags,
6528 null);
6529 }
6530 }
6531
6532 private void removeUriPermissionIfNeededLocked(UriPermission perm) {
6533 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
6534 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
6535 HashMap<Uri, UriPermission> perms
6536 = mGrantedUriPermissions.get(perm.uid);
6537 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006538 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006539 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006540 perms.remove(perm.uri);
6541 if (perms.size() == 0) {
6542 mGrantedUriPermissions.remove(perm.uid);
6543 }
6544 }
6545 }
6546 }
6547
6548 private void removeActivityUriPermissionsLocked(HistoryRecord activity) {
6549 if (activity.readUriPermissions != null) {
6550 for (UriPermission perm : activity.readUriPermissions) {
6551 perm.readActivities.remove(activity);
6552 if (perm.readActivities.size() == 0 && (perm.globalModeFlags
6553 &Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0) {
6554 perm.modeFlags &= ~Intent.FLAG_GRANT_READ_URI_PERMISSION;
6555 removeUriPermissionIfNeededLocked(perm);
6556 }
6557 }
6558 }
6559 if (activity.writeUriPermissions != null) {
6560 for (UriPermission perm : activity.writeUriPermissions) {
6561 perm.writeActivities.remove(activity);
6562 if (perm.writeActivities.size() == 0 && (perm.globalModeFlags
6563 &Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0) {
6564 perm.modeFlags &= ~Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
6565 removeUriPermissionIfNeededLocked(perm);
6566 }
6567 }
6568 }
6569 }
6570
6571 private void revokeUriPermissionLocked(int callingUid, Uri uri,
6572 int modeFlags) {
6573 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
6574 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
6575 if (modeFlags == 0) {
6576 return;
6577 }
6578
Joe Onorato8a9b2202010-02-26 18:56:32 -08006579 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006580 "Revoking all granted permissions to " + uri);
6581
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006582 final IPackageManager pm = ActivityThread.getPackageManager();
6583
6584 final String authority = uri.getAuthority();
6585 ProviderInfo pi = null;
6586 ContentProviderRecord cpr
6587 = (ContentProviderRecord)mProvidersByName.get(authority);
6588 if (cpr != null) {
6589 pi = cpr.info;
6590 } else {
6591 try {
6592 pi = pm.resolveContentProvider(authority,
6593 PackageManager.GET_URI_PERMISSION_PATTERNS);
6594 } catch (RemoteException ex) {
6595 }
6596 }
6597 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006598 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006599 return;
6600 }
6601
6602 // Does the caller have this permission on the URI?
6603 if (!checkHoldingPermissionsLocked(pm, pi, callingUid, modeFlags)) {
6604 // Right now, if you are not the original owner of the permission,
6605 // you are not allowed to revoke it.
6606 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
6607 throw new SecurityException("Uid " + callingUid
6608 + " does not have permission to uri " + uri);
6609 //}
6610 }
6611
6612 // Go through all of the permissions and remove any that match.
6613 final List<String> SEGMENTS = uri.getPathSegments();
6614 if (SEGMENTS != null) {
6615 final int NS = SEGMENTS.size();
6616 int N = mGrantedUriPermissions.size();
6617 for (int i=0; i<N; i++) {
6618 HashMap<Uri, UriPermission> perms
6619 = mGrantedUriPermissions.valueAt(i);
6620 Iterator<UriPermission> it = perms.values().iterator();
6621 toploop:
6622 while (it.hasNext()) {
6623 UriPermission perm = it.next();
6624 Uri targetUri = perm.uri;
6625 if (!authority.equals(targetUri.getAuthority())) {
6626 continue;
6627 }
6628 List<String> targetSegments = targetUri.getPathSegments();
6629 if (targetSegments == null) {
6630 continue;
6631 }
6632 if (targetSegments.size() < NS) {
6633 continue;
6634 }
6635 for (int j=0; j<NS; j++) {
6636 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
6637 continue toploop;
6638 }
6639 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006640 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08006641 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006642 perm.clearModes(modeFlags);
6643 if (perm.modeFlags == 0) {
6644 it.remove();
6645 }
6646 }
6647 if (perms.size() == 0) {
6648 mGrantedUriPermissions.remove(
6649 mGrantedUriPermissions.keyAt(i));
6650 N--;
6651 i--;
6652 }
6653 }
6654 }
6655 }
6656
6657 public void revokeUriPermission(IApplicationThread caller, Uri uri,
6658 int modeFlags) {
6659 synchronized(this) {
6660 final ProcessRecord r = getRecordForAppLocked(caller);
6661 if (r == null) {
6662 throw new SecurityException("Unable to find app for caller "
6663 + caller
6664 + " when revoking permission to uri " + uri);
6665 }
6666 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006667 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006668 return;
6669 }
6670
6671 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
6672 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
6673 if (modeFlags == 0) {
6674 return;
6675 }
6676
6677 final IPackageManager pm = ActivityThread.getPackageManager();
6678
6679 final String authority = uri.getAuthority();
6680 ProviderInfo pi = null;
6681 ContentProviderRecord cpr
6682 = (ContentProviderRecord)mProvidersByName.get(authority);
6683 if (cpr != null) {
6684 pi = cpr.info;
6685 } else {
6686 try {
6687 pi = pm.resolveContentProvider(authority,
6688 PackageManager.GET_URI_PERMISSION_PATTERNS);
6689 } catch (RemoteException ex) {
6690 }
6691 }
6692 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006693 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 return;
6695 }
6696
6697 revokeUriPermissionLocked(r.info.uid, uri, modeFlags);
6698 }
6699 }
6700
6701 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
6702 synchronized (this) {
6703 ProcessRecord app =
6704 who != null ? getRecordForAppLocked(who) : null;
6705 if (app == null) return;
6706
6707 Message msg = Message.obtain();
6708 msg.what = WAIT_FOR_DEBUGGER_MSG;
6709 msg.obj = app;
6710 msg.arg1 = waiting ? 1 : 0;
6711 mHandler.sendMessage(msg);
6712 }
6713 }
6714
6715 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
6716 outInfo.availMem = Process.getFreeMemory();
The Android Open Source Project4df24232009-03-05 14:34:35 -08006717 outInfo.threshold = HOME_APP_MEM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006718 outInfo.lowMemory = outInfo.availMem <
The Android Open Source Project4df24232009-03-05 14:34:35 -08006719 (HOME_APP_MEM + ((HIDDEN_APP_MEM-HOME_APP_MEM)/2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006720 }
6721
6722 // =========================================================
6723 // TASK MANAGEMENT
6724 // =========================================================
6725
6726 public List getTasks(int maxNum, int flags,
6727 IThumbnailReceiver receiver) {
6728 ArrayList list = new ArrayList();
6729
6730 PendingThumbnailsRecord pending = null;
6731 IApplicationThread topThumbnail = null;
6732 HistoryRecord topRecord = null;
6733
6734 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006735 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
6737 + ", receiver=" + receiver);
6738
6739 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
6740 != PackageManager.PERMISSION_GRANTED) {
6741 if (receiver != null) {
6742 // If the caller wants to wait for pending thumbnails,
6743 // it ain't gonna get them.
6744 try {
6745 receiver.finished();
6746 } catch (RemoteException ex) {
6747 }
6748 }
6749 String msg = "Permission Denial: getTasks() from pid="
6750 + Binder.getCallingPid()
6751 + ", uid=" + Binder.getCallingUid()
6752 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006753 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 throw new SecurityException(msg);
6755 }
6756
6757 int pos = mHistory.size()-1;
6758 HistoryRecord next =
6759 pos >= 0 ? (HistoryRecord)mHistory.get(pos) : null;
6760 HistoryRecord top = null;
6761 CharSequence topDescription = null;
6762 TaskRecord curTask = null;
6763 int numActivities = 0;
6764 int numRunning = 0;
6765 while (pos >= 0 && maxNum > 0) {
6766 final HistoryRecord r = next;
6767 pos--;
6768 next = pos >= 0 ? (HistoryRecord)mHistory.get(pos) : null;
6769
6770 // Initialize state for next task if needed.
6771 if (top == null ||
6772 (top.state == ActivityState.INITIALIZING
6773 && top.task == r.task)) {
6774 top = r;
6775 topDescription = r.description;
6776 curTask = r.task;
6777 numActivities = numRunning = 0;
6778 }
6779
6780 // Add 'r' into the current task.
6781 numActivities++;
6782 if (r.app != null && r.app.thread != null) {
6783 numRunning++;
6784 }
6785 if (topDescription == null) {
6786 topDescription = r.description;
6787 }
6788
Joe Onorato8a9b2202010-02-26 18:56:32 -08006789 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006790 TAG, r.intent.getComponent().flattenToShortString()
6791 + ": task=" + r.task);
6792
6793 // If the next one is a different task, generate a new
6794 // TaskInfo entry for what we have.
6795 if (next == null || next.task != curTask) {
6796 ActivityManager.RunningTaskInfo ci
6797 = new ActivityManager.RunningTaskInfo();
6798 ci.id = curTask.taskId;
6799 ci.baseActivity = r.intent.getComponent();
6800 ci.topActivity = top.intent.getComponent();
6801 ci.thumbnail = top.thumbnail;
6802 ci.description = topDescription;
6803 ci.numActivities = numActivities;
6804 ci.numRunning = numRunning;
6805 //System.out.println(
6806 // "#" + maxNum + ": " + " descr=" + ci.description);
6807 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006808 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006809 TAG, "State=" + top.state + "Idle=" + top.idle
6810 + " app=" + top.app
6811 + " thr=" + (top.app != null ? top.app.thread : null));
6812 if (top.state == ActivityState.RESUMED
6813 || top.state == ActivityState.PAUSING) {
6814 if (top.idle && top.app != null
6815 && top.app.thread != null) {
6816 topRecord = top;
6817 topThumbnail = top.app.thread;
6818 } else {
6819 top.thumbnailNeeded = true;
6820 }
6821 }
6822 if (pending == null) {
6823 pending = new PendingThumbnailsRecord(receiver);
6824 }
6825 pending.pendingRecords.add(top);
6826 }
6827 list.add(ci);
6828 maxNum--;
6829 top = null;
6830 }
6831 }
6832
6833 if (pending != null) {
6834 mPendingThumbnails.add(pending);
6835 }
6836 }
6837
Joe Onorato8a9b2202010-02-26 18:56:32 -08006838 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006839
6840 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006841 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006842 try {
6843 topThumbnail.requestThumbnail(topRecord);
6844 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006845 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 sendPendingThumbnail(null, topRecord, null, null, true);
6847 }
6848 }
6849
6850 if (pending == null && receiver != null) {
6851 // In this case all thumbnails were available and the client
6852 // is being asked to be told when the remaining ones come in...
6853 // which is unusually, since the top-most currently running
6854 // activity should never have a canned thumbnail! Oh well.
6855 try {
6856 receiver.finished();
6857 } catch (RemoteException ex) {
6858 }
6859 }
6860
6861 return list;
6862 }
6863
6864 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
6865 int flags) {
6866 synchronized (this) {
6867 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
6868 "getRecentTasks()");
6869
6870 final int N = mRecentTasks.size();
6871 ArrayList<ActivityManager.RecentTaskInfo> res
6872 = new ArrayList<ActivityManager.RecentTaskInfo>(
6873 maxNum < N ? maxNum : N);
6874 for (int i=0; i<N && maxNum > 0; i++) {
6875 TaskRecord tr = mRecentTasks.get(i);
6876 if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
6877 || (tr.intent == null)
6878 || ((tr.intent.getFlags()
6879 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
6880 ActivityManager.RecentTaskInfo rti
6881 = new ActivityManager.RecentTaskInfo();
6882 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
6883 rti.baseIntent = new Intent(
6884 tr.intent != null ? tr.intent : tr.affinityIntent);
6885 rti.origActivity = tr.origActivity;
6886 res.add(rti);
6887 maxNum--;
6888 }
6889 }
6890 return res;
6891 }
6892 }
6893
6894 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
6895 int j;
6896 TaskRecord startTask = ((HistoryRecord)mHistory.get(startIndex)).task;
6897 TaskRecord jt = startTask;
6898
6899 // First look backwards
6900 for (j=startIndex-1; j>=0; j--) {
6901 HistoryRecord r = (HistoryRecord)mHistory.get(j);
6902 if (r.task != jt) {
6903 jt = r.task;
6904 if (affinity.equals(jt.affinity)) {
6905 return j;
6906 }
6907 }
6908 }
6909
6910 // Now look forwards
6911 final int N = mHistory.size();
6912 jt = startTask;
6913 for (j=startIndex+1; j<N; j++) {
6914 HistoryRecord r = (HistoryRecord)mHistory.get(j);
6915 if (r.task != jt) {
6916 if (affinity.equals(jt.affinity)) {
6917 return j;
6918 }
6919 jt = r.task;
6920 }
6921 }
6922
6923 // Might it be at the top?
6924 if (affinity.equals(((HistoryRecord)mHistory.get(N-1)).task.affinity)) {
6925 return N-1;
6926 }
6927
6928 return -1;
6929 }
6930
6931 /**
6932 * Perform a reset of the given task, if needed as part of launching it.
6933 * Returns the new HistoryRecord at the top of the task.
6934 */
6935 private final HistoryRecord resetTaskIfNeededLocked(HistoryRecord taskTop,
6936 HistoryRecord newActivity) {
6937 boolean forceReset = (newActivity.info.flags
6938 &ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0;
6939 if (taskTop.task.getInactiveDuration() > ACTIVITY_INACTIVE_RESET_TIME) {
6940 if ((newActivity.info.flags
6941 &ActivityInfo.FLAG_ALWAYS_RETAIN_TASK_STATE) == 0) {
6942 forceReset = true;
6943 }
6944 }
6945
6946 final TaskRecord task = taskTop.task;
6947
6948 // We are going to move through the history list so that we can look
6949 // at each activity 'target' with 'below' either the interesting
6950 // activity immediately below it in the stack or null.
6951 HistoryRecord target = null;
6952 int targetI = 0;
6953 int taskTopI = -1;
6954 int replyChainEnd = -1;
6955 int lastReparentPos = -1;
6956 for (int i=mHistory.size()-1; i>=-1; i--) {
6957 HistoryRecord below = i >= 0 ? (HistoryRecord)mHistory.get(i) : null;
6958
6959 if (below != null && below.finishing) {
6960 continue;
6961 }
6962 if (target == null) {
6963 target = below;
6964 targetI = i;
6965 // If we were in the middle of a reply chain before this
6966 // task, it doesn't appear like the root of the chain wants
6967 // anything interesting, so drop it.
6968 replyChainEnd = -1;
6969 continue;
6970 }
6971
6972 final int flags = target.info.flags;
6973
6974 final boolean finishOnTaskLaunch =
6975 (flags&ActivityInfo.FLAG_FINISH_ON_TASK_LAUNCH) != 0;
6976 final boolean allowTaskReparenting =
6977 (flags&ActivityInfo.FLAG_ALLOW_TASK_REPARENTING) != 0;
6978
6979 if (target.task == task) {
6980 // We are inside of the task being reset... we'll either
6981 // finish this activity, push it out for another task,
6982 // or leave it as-is. We only do this
6983 // for activities that are not the root of the task (since
6984 // if we finish the root, we may no longer have the task!).
6985 if (taskTopI < 0) {
6986 taskTopI = targetI;
6987 }
6988 if (below != null && below.task == task) {
6989 final boolean clearWhenTaskReset =
6990 (target.intent.getFlags()
6991 &Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) != 0;
Ed Heyl73798232009-03-24 21:32:21 -07006992 if (!finishOnTaskLaunch && !clearWhenTaskReset && target.resultTo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 // If this activity is sending a reply to a previous
6994 // activity, we can't do anything with it now until
6995 // we reach the start of the reply chain.
6996 // XXX note that we are assuming the result is always
6997 // to the previous activity, which is almost always
6998 // the case but we really shouldn't count on.
6999 if (replyChainEnd < 0) {
7000 replyChainEnd = targetI;
7001 }
Ed Heyl73798232009-03-24 21:32:21 -07007002 } else if (!finishOnTaskLaunch && !clearWhenTaskReset && allowTaskReparenting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007003 && target.taskAffinity != null
7004 && !target.taskAffinity.equals(task.affinity)) {
7005 // If this activity has an affinity for another
7006 // task, then we need to move it out of here. We will
7007 // move it as far out of the way as possible, to the
7008 // bottom of the activity stack. This also keeps it
7009 // correctly ordered with any activities we previously
7010 // moved.
7011 HistoryRecord p = (HistoryRecord)mHistory.get(0);
7012 if (target.taskAffinity != null
7013 && target.taskAffinity.equals(p.task.affinity)) {
7014 // If the activity currently at the bottom has the
7015 // same task affinity as the one we are moving,
7016 // then merge it into the same task.
7017 target.task = p.task;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007018 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007019 + " out to bottom task " + p.task);
7020 } else {
7021 mCurTask++;
7022 if (mCurTask <= 0) {
7023 mCurTask = 1;
7024 }
7025 target.task = new TaskRecord(mCurTask, target.info, null,
7026 (target.info.flags&ActivityInfo.FLAG_CLEAR_TASK_ON_LAUNCH) != 0);
7027 target.task.affinityIntent = target.intent;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007028 if (DEBUG_TASKS) Slog.v(TAG, "Start pushing activity " + target
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 + " out to new task " + target.task);
7030 }
7031 mWindowManager.setAppGroupId(target, task.taskId);
7032 if (replyChainEnd < 0) {
7033 replyChainEnd = targetI;
7034 }
7035 int dstPos = 0;
7036 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
7037 p = (HistoryRecord)mHistory.get(srcPos);
7038 if (p.finishing) {
7039 continue;
7040 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007041 if (DEBUG_TASKS) Slog.v(TAG, "Pushing next activity " + p
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007042 + " out to target's task " + target.task);
7043 task.numActivities--;
7044 p.task = target.task;
7045 target.task.numActivities++;
7046 mHistory.remove(srcPos);
7047 mHistory.add(dstPos, p);
7048 mWindowManager.moveAppToken(dstPos, p);
7049 mWindowManager.setAppGroupId(p, p.task.taskId);
7050 dstPos++;
7051 if (VALIDATE_TOKENS) {
7052 mWindowManager.validateAppTokens(mHistory);
7053 }
7054 i++;
7055 }
7056 if (taskTop == p) {
7057 taskTop = below;
7058 }
7059 if (taskTopI == replyChainEnd) {
7060 taskTopI = -1;
7061 }
7062 replyChainEnd = -1;
Josh Bartel7f208742010-02-25 11:01:44 -06007063 addRecentTaskLocked(target.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 } else if (forceReset || finishOnTaskLaunch
7065 || clearWhenTaskReset) {
7066 // If the activity should just be removed -- either
7067 // because it asks for it, or the task should be
7068 // cleared -- then finish it and anything that is
7069 // part of its reply chain.
7070 if (clearWhenTaskReset) {
7071 // In this case, we want to finish this activity
7072 // and everything above it, so be sneaky and pretend
7073 // like these are all in the reply chain.
7074 replyChainEnd = targetI+1;
7075 while (replyChainEnd < mHistory.size() &&
7076 ((HistoryRecord)mHistory.get(
7077 replyChainEnd)).task == task) {
7078 replyChainEnd++;
7079 }
7080 replyChainEnd--;
7081 } else if (replyChainEnd < 0) {
7082 replyChainEnd = targetI;
7083 }
7084 HistoryRecord p = null;
7085 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
7086 p = (HistoryRecord)mHistory.get(srcPos);
7087 if (p.finishing) {
7088 continue;
7089 }
7090 if (finishActivityLocked(p, srcPos,
7091 Activity.RESULT_CANCELED, null, "reset")) {
7092 replyChainEnd--;
7093 srcPos--;
7094 }
7095 }
7096 if (taskTop == p) {
7097 taskTop = below;
7098 }
7099 if (taskTopI == replyChainEnd) {
7100 taskTopI = -1;
7101 }
7102 replyChainEnd = -1;
7103 } else {
7104 // If we were in the middle of a chain, well the
7105 // activity that started it all doesn't want anything
7106 // special, so leave it all as-is.
7107 replyChainEnd = -1;
7108 }
7109 } else {
7110 // Reached the bottom of the task -- any reply chain
7111 // should be left as-is.
7112 replyChainEnd = -1;
7113 }
7114
7115 } else if (target.resultTo != null) {
7116 // If this activity is sending a reply to a previous
7117 // activity, we can't do anything with it now until
7118 // we reach the start of the reply chain.
7119 // XXX note that we are assuming the result is always
7120 // to the previous activity, which is almost always
7121 // the case but we really shouldn't count on.
7122 if (replyChainEnd < 0) {
7123 replyChainEnd = targetI;
7124 }
7125
7126 } else if (taskTopI >= 0 && allowTaskReparenting
7127 && task.affinity != null
7128 && task.affinity.equals(target.taskAffinity)) {
7129 // We are inside of another task... if this activity has
7130 // an affinity for our task, then either remove it if we are
7131 // clearing or move it over to our task. Note that
7132 // we currently punt on the case where we are resetting a
7133 // task that is not at the top but who has activities above
7134 // with an affinity to it... this is really not a normal
7135 // case, and we will need to later pull that task to the front
7136 // and usually at that point we will do the reset and pick
7137 // up those remaining activities. (This only happens if
7138 // someone starts an activity in a new task from an activity
7139 // in a task that is not currently on top.)
7140 if (forceReset || finishOnTaskLaunch) {
7141 if (replyChainEnd < 0) {
7142 replyChainEnd = targetI;
7143 }
7144 HistoryRecord p = null;
7145 for (int srcPos=targetI; srcPos<=replyChainEnd; srcPos++) {
7146 p = (HistoryRecord)mHistory.get(srcPos);
7147 if (p.finishing) {
7148 continue;
7149 }
7150 if (finishActivityLocked(p, srcPos,
7151 Activity.RESULT_CANCELED, null, "reset")) {
7152 taskTopI--;
7153 lastReparentPos--;
7154 replyChainEnd--;
7155 srcPos--;
7156 }
7157 }
7158 replyChainEnd = -1;
7159 } else {
7160 if (replyChainEnd < 0) {
7161 replyChainEnd = targetI;
7162 }
7163 for (int srcPos=replyChainEnd; srcPos>=targetI; srcPos--) {
7164 HistoryRecord p = (HistoryRecord)mHistory.get(srcPos);
7165 if (p.finishing) {
7166 continue;
7167 }
7168 if (lastReparentPos < 0) {
7169 lastReparentPos = taskTopI;
7170 taskTop = p;
7171 } else {
7172 lastReparentPos--;
7173 }
7174 mHistory.remove(srcPos);
7175 p.task.numActivities--;
7176 p.task = task;
7177 mHistory.add(lastReparentPos, p);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007178 if (DEBUG_TASKS) Slog.v(TAG, "Pulling activity " + p
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007179 + " in to resetting task " + task);
7180 task.numActivities++;
7181 mWindowManager.moveAppToken(lastReparentPos, p);
7182 mWindowManager.setAppGroupId(p, p.task.taskId);
7183 if (VALIDATE_TOKENS) {
7184 mWindowManager.validateAppTokens(mHistory);
7185 }
7186 }
7187 replyChainEnd = -1;
7188
7189 // Now we've moved it in to place... but what if this is
7190 // a singleTop activity and we have put it on top of another
7191 // instance of the same activity? Then we drop the instance
7192 // below so it remains singleTop.
7193 if (target.info.launchMode == ActivityInfo.LAUNCH_SINGLE_TOP) {
7194 for (int j=lastReparentPos-1; j>=0; j--) {
7195 HistoryRecord p = (HistoryRecord)mHistory.get(j);
7196 if (p.finishing) {
7197 continue;
7198 }
7199 if (p.intent.getComponent().equals(target.intent.getComponent())) {
7200 if (finishActivityLocked(p, j,
7201 Activity.RESULT_CANCELED, null, "replace")) {
7202 taskTopI--;
7203 lastReparentPos--;
7204 }
7205 }
7206 }
7207 }
7208 }
7209 }
7210
7211 target = below;
7212 targetI = i;
7213 }
7214
7215 return taskTop;
7216 }
7217
7218 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007219 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007220 */
7221 public void moveTaskToFront(int task) {
7222 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
7223 "moveTaskToFront()");
7224
7225 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007226 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
7227 Binder.getCallingUid(), "Task to front")) {
7228 return;
7229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 final long origId = Binder.clearCallingIdentity();
7231 try {
7232 int N = mRecentTasks.size();
7233 for (int i=0; i<N; i++) {
7234 TaskRecord tr = mRecentTasks.get(i);
7235 if (tr.taskId == task) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007236 moveTaskToFrontLocked(tr, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 return;
7238 }
7239 }
7240 for (int i=mHistory.size()-1; i>=0; i--) {
7241 HistoryRecord hr = (HistoryRecord)mHistory.get(i);
7242 if (hr.task.taskId == task) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007243 moveTaskToFrontLocked(hr.task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007244 return;
7245 }
7246 }
7247 } finally {
7248 Binder.restoreCallingIdentity(origId);
7249 }
7250 }
7251 }
7252
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007253 private final void moveTaskToFrontLocked(TaskRecord tr, HistoryRecord reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007254 if (DEBUG_SWITCH) Slog.v(TAG, "moveTaskToFront: " + tr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007255
7256 final int task = tr.taskId;
7257 int top = mHistory.size()-1;
7258
7259 if (top < 0 || ((HistoryRecord)mHistory.get(top)).task.taskId == task) {
7260 // nothing to do!
7261 return;
7262 }
7263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007264 ArrayList moved = new ArrayList();
7265
7266 // Applying the affinities may have removed entries from the history,
7267 // so get the size again.
7268 top = mHistory.size()-1;
7269 int pos = top;
7270
7271 // Shift all activities with this task up to the top
7272 // of the stack, keeping them in the same internal order.
7273 while (pos >= 0) {
7274 HistoryRecord r = (HistoryRecord)mHistory.get(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007275 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 TAG, "At " + pos + " ckp " + r.task + ": " + r);
7277 boolean first = true;
7278 if (r.task.taskId == task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007279 if (localLOGV) Slog.v(TAG, "Removing and adding at " + top);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007280 mHistory.remove(pos);
7281 mHistory.add(top, r);
7282 moved.add(0, r);
7283 top--;
7284 if (first) {
Josh Bartel7f208742010-02-25 11:01:44 -06007285 addRecentTaskLocked(r.task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007286 first = false;
7287 }
7288 }
7289 pos--;
7290 }
7291
Joe Onorato8a9b2202010-02-26 18:56:32 -08007292 if (DEBUG_TRANSITION) Slog.v(TAG,
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007293 "Prepare to front transition: task=" + tr);
7294 if (reason != null &&
7295 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
7296 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
7297 HistoryRecord r = topRunningActivityLocked(null);
7298 if (r != null) {
7299 mNoAnimActivities.add(r);
7300 }
7301 } else {
7302 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_TASK_TO_FRONT);
7303 }
7304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007305 mWindowManager.moveAppTokensToTop(moved);
7306 if (VALIDATE_TOKENS) {
7307 mWindowManager.validateAppTokens(mHistory);
7308 }
7309
Josh Bartel7f208742010-02-25 11:01:44 -06007310 finishTaskMoveLocked(task);
Doug Zongker2bec3d42009-12-04 12:52:44 -08007311 EventLog.writeEvent(EventLogTags.AM_TASK_TO_FRONT, task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007312 }
7313
Josh Bartel7f208742010-02-25 11:01:44 -06007314 private final void finishTaskMoveLocked(int task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 resumeTopActivityLocked(null);
7316 }
7317
7318 public void moveTaskToBack(int task) {
7319 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
7320 "moveTaskToBack()");
7321
7322 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007323 if (mResumedActivity != null && mResumedActivity.task.taskId == task) {
7324 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
7325 Binder.getCallingUid(), "Task to back")) {
7326 return;
7327 }
7328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007329 final long origId = Binder.clearCallingIdentity();
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007330 moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007331 Binder.restoreCallingIdentity(origId);
7332 }
7333 }
7334
7335 /**
7336 * Moves an activity, and all of the other activities within the same task, to the bottom
7337 * of the history stack. The activity's order within the task is unchanged.
7338 *
7339 * @param token A reference to the activity we wish to move
7340 * @param nonRoot If false then this only works if the activity is the root
7341 * of a task; if true it will work for any activity in a task.
7342 * @return Returns true if the move completed, false if not.
7343 */
7344 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
7345 synchronized(this) {
7346 final long origId = Binder.clearCallingIdentity();
7347 int taskId = getTaskForActivityLocked(token, !nonRoot);
7348 if (taskId >= 0) {
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007349 return moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007350 }
7351 Binder.restoreCallingIdentity(origId);
7352 }
7353 return false;
7354 }
7355
7356 /**
7357 * Worker method for rearranging history stack. Implements the function of moving all
7358 * activities for a specific task (gathering them if disjoint) into a single group at the
7359 * bottom of the stack.
7360 *
7361 * If a watcher is installed, the action is preflighted and the watcher has an opportunity
7362 * to premeptively cancel the move.
7363 *
7364 * @param task The taskId to collect and move to the bottom.
7365 * @return Returns true if the move completed, false if not.
7366 */
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007367 private final boolean moveTaskToBackLocked(int task, HistoryRecord reason) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007368 Slog.i(TAG, "moveTaskToBack: " + task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007369
7370 // If we have a watcher, preflight the move before committing to it. First check
7371 // for *other* available tasks, but if none are available, then try again allowing the
7372 // current task to be selected.
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007373 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374 HistoryRecord next = topRunningActivityLocked(null, task);
7375 if (next == null) {
7376 next = topRunningActivityLocked(null, 0);
7377 }
7378 if (next != null) {
7379 // ask watcher if this is allowed
7380 boolean moveOK = true;
7381 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007382 moveOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007383 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007384 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007385 }
7386 if (!moveOK) {
7387 return false;
7388 }
7389 }
7390 }
7391
7392 ArrayList moved = new ArrayList();
7393
Joe Onorato8a9b2202010-02-26 18:56:32 -08007394 if (DEBUG_TRANSITION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007395 "Prepare to back transition: task=" + task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396
7397 final int N = mHistory.size();
7398 int bottom = 0;
7399 int pos = 0;
7400
7401 // Shift all activities with this task down to the bottom
7402 // of the stack, keeping them in the same internal order.
7403 while (pos < N) {
7404 HistoryRecord r = (HistoryRecord)mHistory.get(pos);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007405 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 TAG, "At " + pos + " ckp " + r.task + ": " + r);
7407 if (r.task.taskId == task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007408 if (localLOGV) Slog.v(TAG, "Removing and adding at " + (N-1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007409 mHistory.remove(pos);
7410 mHistory.add(bottom, r);
7411 moved.add(r);
7412 bottom++;
7413 }
7414 pos++;
7415 }
7416
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007417 if (reason != null &&
7418 (reason.intent.getFlags()&Intent.FLAG_ACTIVITY_NO_ANIMATION) != 0) {
7419 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_NONE);
7420 HistoryRecord r = topRunningActivityLocked(null);
7421 if (r != null) {
7422 mNoAnimActivities.add(r);
7423 }
7424 } else {
Suchi Amalapurapuc9568e32009-11-05 18:51:16 -08007425 mWindowManager.prepareAppTransition(WindowManagerPolicy.TRANSIT_TASK_TO_BACK);
Dianne Hackbornbfe319e2009-09-21 00:34:05 -07007426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007427 mWindowManager.moveAppTokensToBottom(moved);
7428 if (VALIDATE_TOKENS) {
7429 mWindowManager.validateAppTokens(mHistory);
7430 }
7431
Josh Bartel7f208742010-02-25 11:01:44 -06007432 finishTaskMoveLocked(task);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007433 return true;
7434 }
7435
7436 public void moveTaskBackwards(int task) {
7437 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
7438 "moveTaskBackwards()");
7439
7440 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07007441 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
7442 Binder.getCallingUid(), "Task backwards")) {
7443 return;
7444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007445 final long origId = Binder.clearCallingIdentity();
7446 moveTaskBackwardsLocked(task);
7447 Binder.restoreCallingIdentity(origId);
7448 }
7449 }
7450
7451 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007452 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 }
7454
7455 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
7456 synchronized(this) {
7457 return getTaskForActivityLocked(token, onlyRoot);
7458 }
7459 }
7460
7461 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
7462 final int N = mHistory.size();
7463 TaskRecord lastTask = null;
7464 for (int i=0; i<N; i++) {
7465 HistoryRecord r = (HistoryRecord)mHistory.get(i);
7466 if (r == token) {
7467 if (!onlyRoot || lastTask != r.task) {
7468 return r.task.taskId;
7469 }
7470 return -1;
7471 }
7472 lastTask = r.task;
7473 }
7474
7475 return -1;
7476 }
7477
7478 /**
7479 * Returns the top activity in any existing task matching the given
7480 * Intent. Returns null if no such task is found.
7481 */
7482 private HistoryRecord findTaskLocked(Intent intent, ActivityInfo info) {
7483 ComponentName cls = intent.getComponent();
7484 if (info.targetActivity != null) {
7485 cls = new ComponentName(info.packageName, info.targetActivity);
7486 }
7487
7488 TaskRecord cp = null;
7489
7490 final int N = mHistory.size();
7491 for (int i=(N-1); i>=0; i--) {
7492 HistoryRecord r = (HistoryRecord)mHistory.get(i);
7493 if (!r.finishing && r.task != cp
7494 && r.launchMode != ActivityInfo.LAUNCH_SINGLE_INSTANCE) {
7495 cp = r.task;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007496 //Slog.i(TAG, "Comparing existing cls=" + r.task.intent.getComponent().flattenToShortString()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007497 // + "/aff=" + r.task.affinity + " to new cls="
7498 // + intent.getComponent().flattenToShortString() + "/aff=" + taskAffinity);
7499 if (r.task.affinity != null) {
7500 if (r.task.affinity.equals(info.taskAffinity)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007501 //Slog.i(TAG, "Found matching affinity!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007502 return r;
7503 }
7504 } else if (r.task.intent != null
7505 && r.task.intent.getComponent().equals(cls)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007506 //Slog.i(TAG, "Found matching class!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007507 //dump();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007508 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509 return r;
7510 } else if (r.task.affinityIntent != null
7511 && r.task.affinityIntent.getComponent().equals(cls)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007512 //Slog.i(TAG, "Found matching class!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 //dump();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007514 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 return r;
7516 }
7517 }
7518 }
7519
7520 return null;
7521 }
7522
7523 /**
7524 * Returns the first activity (starting from the top of the stack) that
7525 * is the same as the given activity. Returns null if no such activity
7526 * is found.
7527 */
7528 private HistoryRecord findActivityLocked(Intent intent, ActivityInfo info) {
7529 ComponentName cls = intent.getComponent();
7530 if (info.targetActivity != null) {
7531 cls = new ComponentName(info.packageName, info.targetActivity);
7532 }
7533
7534 final int N = mHistory.size();
7535 for (int i=(N-1); i>=0; i--) {
7536 HistoryRecord r = (HistoryRecord)mHistory.get(i);
7537 if (!r.finishing) {
7538 if (r.intent.getComponent().equals(cls)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007539 //Slog.i(TAG, "Found matching class!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007540 //dump();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007541 //Slog.i(TAG, "For Intent " + intent + " bringing to top: " + r.intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007542 return r;
7543 }
7544 }
7545 }
7546
7547 return null;
7548 }
7549
7550 public void finishOtherInstances(IBinder token, ComponentName className) {
7551 synchronized(this) {
7552 final long origId = Binder.clearCallingIdentity();
7553
7554 int N = mHistory.size();
7555 TaskRecord lastTask = null;
7556 for (int i=0; i<N; i++) {
7557 HistoryRecord r = (HistoryRecord)mHistory.get(i);
7558 if (r.realActivity.equals(className)
7559 && r != token && lastTask != r.task) {
7560 if (finishActivityLocked(r, i, Activity.RESULT_CANCELED,
7561 null, "others")) {
7562 i--;
7563 N--;
7564 }
7565 }
7566 lastTask = r.task;
7567 }
7568
7569 Binder.restoreCallingIdentity(origId);
7570 }
7571 }
7572
7573 // =========================================================
7574 // THUMBNAILS
7575 // =========================================================
7576
7577 public void reportThumbnail(IBinder token,
7578 Bitmap thumbnail, CharSequence description) {
7579 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
7580 final long origId = Binder.clearCallingIdentity();
7581 sendPendingThumbnail(null, token, thumbnail, description, true);
7582 Binder.restoreCallingIdentity(origId);
7583 }
7584
7585 final void sendPendingThumbnail(HistoryRecord r, IBinder token,
7586 Bitmap thumbnail, CharSequence description, boolean always) {
7587 TaskRecord task = null;
7588 ArrayList receivers = null;
7589
7590 //System.out.println("Send pending thumbnail: " + r);
7591
7592 synchronized(this) {
7593 if (r == null) {
Dianne Hackborn75b03852009-06-12 15:43:26 -07007594 int index = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007595 if (index < 0) {
7596 return;
7597 }
7598 r = (HistoryRecord)mHistory.get(index);
7599 }
7600 if (thumbnail == null) {
7601 thumbnail = r.thumbnail;
7602 description = r.description;
7603 }
7604 if (thumbnail == null && !always) {
7605 // If there is no thumbnail, and this entry is not actually
7606 // going away, then abort for now and pick up the next
7607 // thumbnail we get.
7608 return;
7609 }
7610 task = r.task;
7611
7612 int N = mPendingThumbnails.size();
7613 int i=0;
7614 while (i<N) {
7615 PendingThumbnailsRecord pr =
7616 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
7617 //System.out.println("Looking in " + pr.pendingRecords);
7618 if (pr.pendingRecords.remove(r)) {
7619 if (receivers == null) {
7620 receivers = new ArrayList();
7621 }
7622 receivers.add(pr);
7623 if (pr.pendingRecords.size() == 0) {
7624 pr.finished = true;
7625 mPendingThumbnails.remove(i);
7626 N--;
7627 continue;
7628 }
7629 }
7630 i++;
7631 }
7632 }
7633
7634 if (receivers != null) {
7635 final int N = receivers.size();
7636 for (int i=0; i<N; i++) {
7637 try {
7638 PendingThumbnailsRecord pr =
7639 (PendingThumbnailsRecord)receivers.get(i);
7640 pr.receiver.newThumbnail(
7641 task != null ? task.taskId : -1, thumbnail, description);
7642 if (pr.finished) {
7643 pr.receiver.finished();
7644 }
7645 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007646 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007647 }
7648 }
7649 }
7650 }
7651
7652 // =========================================================
7653 // CONTENT PROVIDERS
7654 // =========================================================
7655
7656 private final List generateApplicationProvidersLocked(ProcessRecord app) {
7657 List providers = null;
7658 try {
7659 providers = ActivityThread.getPackageManager().
7660 queryContentProviders(app.processName, app.info.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07007661 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007662 } catch (RemoteException ex) {
7663 }
7664 if (providers != null) {
7665 final int N = providers.size();
7666 for (int i=0; i<N; i++) {
7667 ProviderInfo cpi =
7668 (ProviderInfo)providers.get(i);
7669 ContentProviderRecord cpr =
7670 (ContentProviderRecord)mProvidersByClass.get(cpi.name);
7671 if (cpr == null) {
7672 cpr = new ContentProviderRecord(cpi, app.info);
7673 mProvidersByClass.put(cpi.name, cpr);
7674 }
7675 app.pubProviders.put(cpi.name, cpr);
7676 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07007677 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 }
7679 }
7680 return providers;
7681 }
7682
7683 private final String checkContentProviderPermissionLocked(
7684 ProviderInfo cpi, ProcessRecord r, int mode) {
7685 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
7686 final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid();
7687 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
7688 cpi.exported ? -1 : cpi.applicationInfo.uid)
7689 == PackageManager.PERMISSION_GRANTED
7690 && mode == ParcelFileDescriptor.MODE_READ_ONLY || mode == -1) {
7691 return null;
7692 }
7693 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
7694 cpi.exported ? -1 : cpi.applicationInfo.uid)
7695 == PackageManager.PERMISSION_GRANTED) {
7696 return null;
7697 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07007698
7699 PathPermission[] pps = cpi.pathPermissions;
7700 if (pps != null) {
7701 int i = pps.length;
7702 while (i > 0) {
7703 i--;
7704 PathPermission pp = pps[i];
7705 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
7706 cpi.exported ? -1 : cpi.applicationInfo.uid)
7707 == PackageManager.PERMISSION_GRANTED
7708 && mode == ParcelFileDescriptor.MODE_READ_ONLY || mode == -1) {
7709 return null;
7710 }
7711 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
7712 cpi.exported ? -1 : cpi.applicationInfo.uid)
7713 == PackageManager.PERMISSION_GRANTED) {
7714 return null;
7715 }
7716 }
7717 }
7718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 String msg = "Permission Denial: opening provider " + cpi.name
7720 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
7721 + ", uid=" + callingUid + ") requires "
7722 + cpi.readPermission + " or " + cpi.writePermission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007723 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724 return msg;
7725 }
7726
7727 private final ContentProviderHolder getContentProviderImpl(
7728 IApplicationThread caller, String name) {
7729 ContentProviderRecord cpr;
7730 ProviderInfo cpi = null;
7731
7732 synchronized(this) {
7733 ProcessRecord r = null;
7734 if (caller != null) {
7735 r = getRecordForAppLocked(caller);
7736 if (r == null) {
7737 throw new SecurityException(
7738 "Unable to find app for caller " + caller
7739 + " (pid=" + Binder.getCallingPid()
7740 + ") when getting content provider " + name);
7741 }
7742 }
7743
7744 // First check if this content provider has been published...
7745 cpr = (ContentProviderRecord)mProvidersByName.get(name);
7746 if (cpr != null) {
7747 cpi = cpr.info;
7748 if (checkContentProviderPermissionLocked(cpi, r, -1) != null) {
7749 return new ContentProviderHolder(cpi,
7750 cpi.readPermission != null
7751 ? cpi.readPermission : cpi.writePermission);
7752 }
7753
7754 if (r != null && cpr.canRunHere(r)) {
7755 // This provider has been published or is in the process
7756 // of being published... but it is also allowed to run
7757 // in the caller's process, so don't make a connection
7758 // and just let the caller instantiate its own instance.
7759 if (cpr.provider != null) {
7760 // don't give caller the provider object, it needs
7761 // to make its own.
7762 cpr = new ContentProviderRecord(cpr);
7763 }
7764 return cpr;
7765 }
7766
7767 final long origId = Binder.clearCallingIdentity();
7768
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007769 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 // return it right away.
7771 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007772 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007773 "Adding provider requested by "
7774 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07007775 + cpr.info.processName);
7776 Integer cnt = r.conProviders.get(cpr);
7777 if (cnt == null) {
7778 r.conProviders.put(cpr, new Integer(1));
7779 } else {
7780 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
7781 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007782 cpr.clients.add(r);
7783 } else {
7784 cpr.externals++;
7785 }
7786
7787 if (cpr.app != null) {
7788 updateOomAdjLocked(cpr.app);
7789 }
7790
7791 Binder.restoreCallingIdentity(origId);
7792
7793 } else {
7794 try {
7795 cpi = ActivityThread.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07007796 resolveContentProvider(name,
7797 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007798 } catch (RemoteException ex) {
7799 }
7800 if (cpi == null) {
7801 return null;
7802 }
7803
7804 if (checkContentProviderPermissionLocked(cpi, r, -1) != null) {
7805 return new ContentProviderHolder(cpi,
7806 cpi.readPermission != null
7807 ? cpi.readPermission : cpi.writePermission);
7808 }
7809
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08007810 if (!mSystemReady && !mDidUpdate && !mWaitingUpdate
7811 && !cpi.processName.equals("system")) {
7812 // If this content provider does not run in the system
7813 // process, and the system is not yet ready to run other
7814 // processes, then fail fast instead of hanging.
7815 throw new IllegalArgumentException(
7816 "Attempt to launch content provider before system ready");
7817 }
7818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 cpr = (ContentProviderRecord)mProvidersByClass.get(cpi.name);
7820 final boolean firstClass = cpr == null;
7821 if (firstClass) {
7822 try {
7823 ApplicationInfo ai =
7824 ActivityThread.getPackageManager().
7825 getApplicationInfo(
7826 cpi.applicationInfo.packageName,
Dianne Hackborn1655be42009-05-08 14:29:01 -07007827 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007829 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007830 + cpi.name);
7831 return null;
7832 }
7833 cpr = new ContentProviderRecord(cpi, ai);
7834 } catch (RemoteException ex) {
7835 // pm is in same process, this will never happen.
7836 }
7837 }
7838
7839 if (r != null && cpr.canRunHere(r)) {
7840 // If this is a multiprocess provider, then just return its
7841 // info and allow the caller to instantiate it. Only do
7842 // this if the provider is the same user as the caller's
7843 // process, or can run as root (so can be in any process).
7844 return cpr;
7845 }
7846
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007847 if (DEBUG_PROVIDER) {
7848 RuntimeException e = new RuntimeException("here");
Joe Onorato8a9b2202010-02-26 18:56:32 -08007849 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007850 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007851 }
7852
7853 // This is single process, and our app is now connecting to it.
7854 // See if we are already in the process of launching this
7855 // provider.
7856 final int N = mLaunchingProviders.size();
7857 int i;
7858 for (i=0; i<N; i++) {
7859 if (mLaunchingProviders.get(i) == cpr) {
7860 break;
7861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 }
7863
7864 // If the provider is not already being launched, then get it
7865 // started.
7866 if (i >= N) {
7867 final long origId = Binder.clearCallingIdentity();
7868 ProcessRecord proc = startProcessLocked(cpi.processName,
7869 cpr.appInfo, false, 0, "content provider",
7870 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07007871 cpi.name), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007873 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 + cpi.applicationInfo.packageName + "/"
7875 + cpi.applicationInfo.uid + " for provider "
7876 + name + ": process is bad");
7877 return null;
7878 }
7879 cpr.launchingApp = proc;
7880 mLaunchingProviders.add(cpr);
7881 Binder.restoreCallingIdentity(origId);
7882 }
7883
7884 // Make sure the provider is published (the same provider class
7885 // may be published under multiple names).
7886 if (firstClass) {
7887 mProvidersByClass.put(cpi.name, cpr);
7888 }
7889 mProvidersByName.put(name, cpr);
7890
7891 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007892 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007893 "Adding provider requested by "
7894 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07007895 + cpr.info.processName);
7896 Integer cnt = r.conProviders.get(cpr);
7897 if (cnt == null) {
7898 r.conProviders.put(cpr, new Integer(1));
7899 } else {
7900 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
7901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 cpr.clients.add(r);
7903 } else {
7904 cpr.externals++;
7905 }
7906 }
7907 }
7908
7909 // Wait for the provider to be published...
7910 synchronized (cpr) {
7911 while (cpr.provider == null) {
7912 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007913 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914 + cpi.applicationInfo.packageName + "/"
7915 + cpi.applicationInfo.uid + " for provider "
7916 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08007917 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 cpi.applicationInfo.packageName,
7919 cpi.applicationInfo.uid, name);
7920 return null;
7921 }
7922 try {
7923 cpr.wait();
7924 } catch (InterruptedException ex) {
7925 }
7926 }
7927 }
7928 return cpr;
7929 }
7930
7931 public final ContentProviderHolder getContentProvider(
7932 IApplicationThread caller, String name) {
7933 if (caller == null) {
7934 String msg = "null IApplicationThread when getting content provider "
7935 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08007936 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007937 throw new SecurityException(msg);
7938 }
7939
7940 return getContentProviderImpl(caller, name);
7941 }
7942
7943 private ContentProviderHolder getContentProviderExternal(String name) {
7944 return getContentProviderImpl(null, name);
7945 }
7946
7947 /**
7948 * Drop a content provider from a ProcessRecord's bookkeeping
7949 * @param cpr
7950 */
7951 public void removeContentProvider(IApplicationThread caller, String name) {
7952 synchronized (this) {
7953 ContentProviderRecord cpr = (ContentProviderRecord)mProvidersByName.get(name);
7954 if(cpr == null) {
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007955 // remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08007956 if (DEBUG_PROVIDER) Slog.v(TAG, name +
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007957 " provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007958 return;
7959 }
7960 final ProcessRecord r = getRecordForAppLocked(caller);
7961 if (r == null) {
7962 throw new SecurityException(
7963 "Unable to find app for caller " + caller +
7964 " when removing content provider " + name);
7965 }
7966 //update content provider record entry info
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007967 ContentProviderRecord localCpr = (ContentProviderRecord)
7968 mProvidersByClass.get(cpr.info.name);
Joe Onorato8a9b2202010-02-26 18:56:32 -08007969 if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007970 + r.info.processName + " from process "
7971 + localCpr.appInfo.processName);
7972 if (localCpr.app == r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 //should not happen. taken care of as a local provider
Joe Onorato8a9b2202010-02-26 18:56:32 -08007974 Slog.w(TAG, "removeContentProvider called on local provider: "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07007975 + cpr.info.name + " in process " + r.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976 return;
7977 } else {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07007978 Integer cnt = r.conProviders.get(localCpr);
7979 if (cnt == null || cnt.intValue() <= 1) {
7980 localCpr.clients.remove(r);
7981 r.conProviders.remove(localCpr);
7982 } else {
7983 r.conProviders.put(localCpr, new Integer(cnt.intValue()-1));
7984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007985 }
7986 updateOomAdjLocked();
7987 }
7988 }
7989
7990 private void removeContentProviderExternal(String name) {
7991 synchronized (this) {
7992 ContentProviderRecord cpr = (ContentProviderRecord)mProvidersByName.get(name);
7993 if(cpr == null) {
7994 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08007995 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007996 return;
7997 }
7998
7999 //update content provider record entry info
8000 ContentProviderRecord localCpr = (ContentProviderRecord) mProvidersByClass.get(cpr.info.name);
8001 localCpr.externals--;
8002 if (localCpr.externals < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008003 Slog.e(TAG, "Externals < 0 for content provider " + localCpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 }
8005 updateOomAdjLocked();
8006 }
8007 }
8008
8009 public final void publishContentProviders(IApplicationThread caller,
8010 List<ContentProviderHolder> providers) {
8011 if (providers == null) {
8012 return;
8013 }
8014
8015 synchronized(this) {
8016 final ProcessRecord r = getRecordForAppLocked(caller);
8017 if (r == null) {
8018 throw new SecurityException(
8019 "Unable to find app for caller " + caller
8020 + " (pid=" + Binder.getCallingPid()
8021 + ") when publishing content providers");
8022 }
8023
8024 final long origId = Binder.clearCallingIdentity();
8025
8026 final int N = providers.size();
8027 for (int i=0; i<N; i++) {
8028 ContentProviderHolder src = providers.get(i);
8029 if (src == null || src.info == null || src.provider == null) {
8030 continue;
8031 }
8032 ContentProviderRecord dst =
8033 (ContentProviderRecord)r.pubProviders.get(src.info.name);
8034 if (dst != null) {
8035 mProvidersByClass.put(dst.info.name, dst);
8036 String names[] = dst.info.authority.split(";");
8037 for (int j = 0; j < names.length; j++) {
8038 mProvidersByName.put(names[j], dst);
8039 }
8040
8041 int NL = mLaunchingProviders.size();
8042 int j;
8043 for (j=0; j<NL; j++) {
8044 if (mLaunchingProviders.get(j) == dst) {
8045 mLaunchingProviders.remove(j);
8046 j--;
8047 NL--;
8048 }
8049 }
8050 synchronized (dst) {
8051 dst.provider = src.provider;
8052 dst.app = r;
8053 dst.notifyAll();
8054 }
8055 updateOomAdjLocked(r);
8056 }
8057 }
8058
8059 Binder.restoreCallingIdentity(origId);
8060 }
8061 }
8062
8063 public static final void installSystemProviders() {
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08008064 List providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06008065 synchronized (mSelf) {
8066 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
8067 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08008068 if (providers != null) {
8069 for (int i=providers.size()-1; i>=0; i--) {
8070 ProviderInfo pi = (ProviderInfo)providers.get(i);
8071 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
8072 Slog.w(TAG, "Not installing system proc provider " + pi.name
8073 + ": not system .apk");
8074 providers.remove(i);
8075 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08008076 }
8077 }
8078 }
Josh Bartel2ecce342010-02-25 10:55:48 -06008079 if (providers != null) {
8080 mSystemThread.installSystemProviders(providers);
8081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 }
8083
8084 // =========================================================
8085 // GLOBAL MANAGEMENT
8086 // =========================================================
8087
8088 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
8089 ApplicationInfo info, String customProcess) {
8090 String proc = customProcess != null ? customProcess : info.processName;
8091 BatteryStatsImpl.Uid.Proc ps = null;
8092 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
8093 synchronized (stats) {
8094 ps = stats.getProcessStatsLocked(info.uid, proc);
8095 }
8096 return new ProcessRecord(ps, thread, info, proc);
8097 }
8098
8099 final ProcessRecord addAppLocked(ApplicationInfo info) {
8100 ProcessRecord app = getProcessRecordLocked(info.processName, info.uid);
8101
8102 if (app == null) {
8103 app = newProcessRecordLocked(null, info, null);
8104 mProcessNames.put(info.processName, info.uid, app);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008105 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008106 }
8107
8108 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
8109 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
8110 app.persistent = true;
8111 app.maxAdj = CORE_SERVER_ADJ;
8112 }
8113 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
8114 mPersistentStartingProcesses.add(app);
8115 startProcessLocked(app, "added application", app.processName);
8116 }
8117
8118 return app;
8119 }
8120
8121 public void unhandledBack() {
8122 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
8123 "unhandledBack()");
8124
8125 synchronized(this) {
8126 int count = mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08008127 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 TAG, "Performing unhandledBack(): stack size = " + count);
8129 if (count > 1) {
8130 final long origId = Binder.clearCallingIdentity();
8131 finishActivityLocked((HistoryRecord)mHistory.get(count-1),
8132 count-1, Activity.RESULT_CANCELED, null, "unhandled-back");
8133 Binder.restoreCallingIdentity(origId);
8134 }
8135 }
8136 }
8137
8138 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
8139 String name = uri.getAuthority();
8140 ContentProviderHolder cph = getContentProviderExternal(name);
8141 ParcelFileDescriptor pfd = null;
8142 if (cph != null) {
8143 // We record the binder invoker's uid in thread-local storage before
8144 // going to the content provider to open the file. Later, in the code
8145 // that handles all permissions checks, we look for this uid and use
8146 // that rather than the Activity Manager's own uid. The effect is that
8147 // we do the check against the caller's permissions even though it looks
8148 // to the content provider like the Activity Manager itself is making
8149 // the request.
8150 sCallerIdentity.set(new Identity(
8151 Binder.getCallingPid(), Binder.getCallingUid()));
8152 try {
8153 pfd = cph.provider.openFile(uri, "r");
8154 } catch (FileNotFoundException e) {
8155 // do nothing; pfd will be returned null
8156 } finally {
8157 // Ensure that whatever happens, we clean up the identity state
8158 sCallerIdentity.remove();
8159 }
8160
8161 // We've got the fd now, so we're done with the provider.
8162 removeContentProviderExternal(name);
8163 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008164 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008165 }
8166 return pfd;
8167 }
8168
8169 public void goingToSleep() {
8170 synchronized(this) {
8171 mSleeping = true;
8172 mWindowManager.setEventDispatching(false);
8173
8174 if (mResumedActivity != null) {
8175 pauseIfSleepingLocked();
8176 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008177 Slog.w(TAG, "goingToSleep with no resumed activity!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 }
8179 }
8180 }
8181
Dianne Hackborn55280a92009-05-07 15:53:46 -07008182 public boolean shutdown(int timeout) {
8183 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
8184 != PackageManager.PERMISSION_GRANTED) {
8185 throw new SecurityException("Requires permission "
8186 + android.Manifest.permission.SHUTDOWN);
8187 }
8188
8189 boolean timedout = false;
8190
8191 synchronized(this) {
8192 mShuttingDown = true;
8193 mWindowManager.setEventDispatching(false);
8194
8195 if (mResumedActivity != null) {
8196 pauseIfSleepingLocked();
8197 final long endTime = System.currentTimeMillis() + timeout;
8198 while (mResumedActivity != null || mPausingActivity != null) {
8199 long delay = endTime - System.currentTimeMillis();
8200 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008201 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07008202 timedout = true;
8203 break;
8204 }
8205 try {
8206 this.wait();
8207 } catch (InterruptedException e) {
8208 }
8209 }
8210 }
8211 }
8212
8213 mUsageStatsService.shutdown();
8214 mBatteryStatsService.shutdown();
8215
8216 return timedout;
8217 }
8218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 void pauseIfSleepingLocked() {
Dianne Hackborn55280a92009-05-07 15:53:46 -07008220 if (mSleeping || mShuttingDown) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 if (!mGoingToSleep.isHeld()) {
8222 mGoingToSleep.acquire();
8223 if (mLaunchingActivity.isHeld()) {
8224 mLaunchingActivity.release();
8225 mHandler.removeMessages(LAUNCH_TIMEOUT_MSG);
8226 }
8227 }
8228
8229 // If we are not currently pausing an activity, get the current
8230 // one to pause. If we are pausing one, we will just let that stuff
8231 // run and release the wake lock when all done.
8232 if (mPausingActivity == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008233 if (DEBUG_PAUSE) Slog.v(TAG, "Sleep needs to pause...");
8234 if (DEBUG_USER_LEAVING) Slog.v(TAG, "Sleep => pause with userLeaving=false");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 startPausingLocked(false, true);
8236 }
8237 }
8238 }
8239
8240 public void wakingUp() {
8241 synchronized(this) {
8242 if (mGoingToSleep.isHeld()) {
8243 mGoingToSleep.release();
8244 }
8245 mWindowManager.setEventDispatching(true);
8246 mSleeping = false;
8247 resumeTopActivityLocked(null);
8248 }
8249 }
8250
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07008251 public void stopAppSwitches() {
8252 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
8253 != PackageManager.PERMISSION_GRANTED) {
8254 throw new SecurityException("Requires permission "
8255 + android.Manifest.permission.STOP_APP_SWITCHES);
8256 }
8257
8258 synchronized(this) {
8259 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
8260 + APP_SWITCH_DELAY_TIME;
8261 mDidAppSwitch = false;
8262 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
8263 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
8264 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
8265 }
8266 }
8267
8268 public void resumeAppSwitches() {
8269 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
8270 != PackageManager.PERMISSION_GRANTED) {
8271 throw new SecurityException("Requires permission "
8272 + android.Manifest.permission.STOP_APP_SWITCHES);
8273 }
8274
8275 synchronized(this) {
8276 // Note that we don't execute any pending app switches... we will
8277 // let those wait until either the timeout, or the next start
8278 // activity request.
8279 mAppSwitchesAllowedTime = 0;
8280 }
8281 }
8282
8283 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
8284 String name) {
8285 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
8286 return true;
8287 }
8288
8289 final int perm = checkComponentPermission(
8290 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
8291 callingUid, -1);
8292 if (perm == PackageManager.PERMISSION_GRANTED) {
8293 return true;
8294 }
8295
Joe Onorato8a9b2202010-02-26 18:56:32 -08008296 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07008297 return false;
8298 }
8299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 public void setDebugApp(String packageName, boolean waitForDebugger,
8301 boolean persistent) {
8302 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
8303 "setDebugApp()");
8304
8305 // Note that this is not really thread safe if there are multiple
8306 // callers into it at the same time, but that's not a situation we
8307 // care about.
8308 if (persistent) {
8309 final ContentResolver resolver = mContext.getContentResolver();
8310 Settings.System.putString(
8311 resolver, Settings.System.DEBUG_APP,
8312 packageName);
8313 Settings.System.putInt(
8314 resolver, Settings.System.WAIT_FOR_DEBUGGER,
8315 waitForDebugger ? 1 : 0);
8316 }
8317
8318 synchronized (this) {
8319 if (!persistent) {
8320 mOrigDebugApp = mDebugApp;
8321 mOrigWaitForDebugger = mWaitForDebugger;
8322 }
8323 mDebugApp = packageName;
8324 mWaitForDebugger = waitForDebugger;
8325 mDebugTransient = !persistent;
8326 if (packageName != null) {
8327 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08008328 forceStopPackageLocked(packageName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 Binder.restoreCallingIdentity(origId);
8330 }
8331 }
8332 }
8333
8334 public void setAlwaysFinish(boolean enabled) {
8335 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
8336 "setAlwaysFinish()");
8337
8338 Settings.System.putInt(
8339 mContext.getContentResolver(),
8340 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
8341
8342 synchronized (this) {
8343 mAlwaysFinishActivities = enabled;
8344 }
8345 }
8346
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008347 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008349 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008351 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008352 }
8353 }
8354
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08008355 public boolean isUserAMonkey() {
8356 // For now the fact that there is a controller implies
8357 // we have a monkey.
8358 synchronized (this) {
8359 return mController != null;
8360 }
8361 }
8362
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008363 public void registerActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06008364 synchronized (this) {
8365 mWatchers.register(watcher);
8366 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008367 }
8368
8369 public void unregisterActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06008370 synchronized (this) {
8371 mWatchers.unregister(watcher);
8372 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07008373 }
8374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 public final void enterSafeMode() {
8376 synchronized(this) {
8377 // It only makes sense to do this before the system is ready
8378 // and started launching other packages.
8379 if (!mSystemReady) {
8380 try {
8381 ActivityThread.getPackageManager().enterSafeMode();
8382 } catch (RemoteException e) {
8383 }
8384
8385 View v = LayoutInflater.from(mContext).inflate(
8386 com.android.internal.R.layout.safe_mode, null);
8387 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
8388 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
8389 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
8390 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
8391 lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
8392 lp.format = v.getBackground().getOpacity();
8393 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
8394 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
8395 ((WindowManager)mContext.getSystemService(
8396 Context.WINDOW_SERVICE)).addView(v, lp);
8397 }
8398 }
8399 }
8400
8401 public void noteWakeupAlarm(IIntentSender sender) {
8402 if (!(sender instanceof PendingIntentRecord)) {
8403 return;
8404 }
8405 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
8406 synchronized (stats) {
8407 if (mBatteryStatsService.isOnBattery()) {
8408 mBatteryStatsService.enforceCallingPermission();
8409 PendingIntentRecord rec = (PendingIntentRecord)sender;
8410 int MY_UID = Binder.getCallingUid();
8411 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
8412 BatteryStatsImpl.Uid.Pkg pkg =
8413 stats.getPackageStatsLocked(uid, rec.key.packageName);
8414 pkg.incWakeupsLocked();
8415 }
8416 }
8417 }
8418
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008419 public boolean killPids(int[] pids, String pReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008421 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008423 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008424 // XXX Note: don't acquire main activity lock here, because the window
8425 // manager calls in with its locks held.
8426
8427 boolean killed = false;
8428 synchronized (mPidsSelfLocked) {
8429 int[] types = new int[pids.length];
8430 int worstType = 0;
8431 for (int i=0; i<pids.length; i++) {
8432 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
8433 if (proc != null) {
8434 int type = proc.setAdj;
8435 types[i] = type;
8436 if (type > worstType) {
8437 worstType = type;
8438 }
8439 }
8440 }
8441
8442 // If the worse oom_adj is somewhere in the hidden proc LRU range,
8443 // then constrain it so we will kill all hidden procs.
8444 if (worstType < EMPTY_APP_ADJ && worstType > HIDDEN_APP_MIN_ADJ) {
8445 worstType = HIDDEN_APP_MIN_ADJ;
8446 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008447 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 for (int i=0; i<pids.length; i++) {
8449 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
8450 if (proc == null) {
8451 continue;
8452 }
8453 int adj = proc.setAdj;
8454 if (adj >= worstType) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008455 Slog.w(TAG, "Killing " + reason + " : " + proc + " (adj "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008456 + adj + ")");
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07008457 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
8458 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 killed = true;
8460 Process.killProcess(pids[i]);
8461 }
8462 }
8463 }
8464 return killed;
8465 }
8466
8467 public void reportPss(IApplicationThread caller, int pss) {
8468 Watchdog.PssRequestor req;
8469 String name;
8470 ProcessRecord callerApp;
8471 synchronized (this) {
8472 if (caller == null) {
8473 return;
8474 }
8475 callerApp = getRecordForAppLocked(caller);
8476 if (callerApp == null) {
8477 return;
8478 }
8479 callerApp.lastPss = pss;
8480 req = callerApp;
8481 name = callerApp.processName;
8482 }
8483 Watchdog.getInstance().reportPss(req, name, pss);
8484 if (!callerApp.persistent) {
8485 removeRequestedPss(callerApp);
8486 }
8487 }
8488
8489 public void requestPss(Runnable completeCallback) {
8490 ArrayList<ProcessRecord> procs;
8491 synchronized (this) {
8492 mRequestPssCallback = completeCallback;
8493 mRequestPssList.clear();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008494 for (int i=mLruProcesses.size()-1; i>=0; i--) {
8495 ProcessRecord proc = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 if (!proc.persistent) {
8497 mRequestPssList.add(proc);
8498 }
8499 }
8500 procs = new ArrayList<ProcessRecord>(mRequestPssList);
8501 }
8502
8503 int oldPri = Process.getThreadPriority(Process.myTid());
8504 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
8505 for (int i=procs.size()-1; i>=0; i--) {
8506 ProcessRecord proc = procs.get(i);
8507 proc.lastPss = 0;
8508 proc.requestPss();
8509 }
8510 Process.setThreadPriority(oldPri);
8511 }
8512
8513 void removeRequestedPss(ProcessRecord proc) {
8514 Runnable callback = null;
8515 synchronized (this) {
8516 if (mRequestPssList.remove(proc)) {
8517 if (mRequestPssList.size() == 0) {
8518 callback = mRequestPssCallback;
8519 mRequestPssCallback = null;
8520 }
8521 }
8522 }
8523
8524 if (callback != null) {
8525 callback.run();
8526 }
8527 }
8528
8529 public void collectPss(Watchdog.PssStats stats) {
8530 stats.mEmptyPss = 0;
8531 stats.mEmptyCount = 0;
8532 stats.mBackgroundPss = 0;
8533 stats.mBackgroundCount = 0;
8534 stats.mServicePss = 0;
8535 stats.mServiceCount = 0;
8536 stats.mVisiblePss = 0;
8537 stats.mVisibleCount = 0;
8538 stats.mForegroundPss = 0;
8539 stats.mForegroundCount = 0;
8540 stats.mNoPssCount = 0;
8541 synchronized (this) {
8542 int i;
8543 int NPD = mProcDeaths.length < stats.mProcDeaths.length
8544 ? mProcDeaths.length : stats.mProcDeaths.length;
8545 int aggr = 0;
8546 for (i=0; i<NPD; i++) {
8547 aggr += mProcDeaths[i];
8548 stats.mProcDeaths[i] = aggr;
8549 }
8550 while (i<stats.mProcDeaths.length) {
8551 stats.mProcDeaths[i] = 0;
8552 i++;
8553 }
8554
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008555 for (i=mLruProcesses.size()-1; i>=0; i--) {
8556 ProcessRecord proc = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 if (proc.persistent) {
8558 continue;
8559 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008560 //Slog.i(TAG, "Proc " + proc + ": pss=" + proc.lastPss);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008561 if (proc.lastPss == 0) {
8562 stats.mNoPssCount++;
8563 continue;
8564 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008565 if (proc.setAdj >= HIDDEN_APP_MIN_ADJ) {
8566 if (proc.empty) {
8567 stats.mEmptyPss += proc.lastPss;
8568 stats.mEmptyCount++;
8569 } else {
8570 stats.mBackgroundPss += proc.lastPss;
8571 stats.mBackgroundCount++;
8572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 } else if (proc.setAdj >= VISIBLE_APP_ADJ) {
8574 stats.mVisiblePss += proc.lastPss;
8575 stats.mVisibleCount++;
8576 } else {
8577 stats.mForegroundPss += proc.lastPss;
8578 stats.mForegroundCount++;
8579 }
8580 }
8581 }
8582 }
8583
8584 public final void startRunning(String pkg, String cls, String action,
8585 String data) {
8586 synchronized(this) {
8587 if (mStartRunning) {
8588 return;
8589 }
8590 mStartRunning = true;
8591 mTopComponent = pkg != null && cls != null
8592 ? new ComponentName(pkg, cls) : null;
8593 mTopAction = action != null ? action : Intent.ACTION_MAIN;
8594 mTopData = data;
8595 if (!mSystemReady) {
8596 return;
8597 }
8598 }
8599
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07008600 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008601 }
8602
8603 private void retrieveSettings() {
8604 final ContentResolver resolver = mContext.getContentResolver();
8605 String debugApp = Settings.System.getString(
8606 resolver, Settings.System.DEBUG_APP);
8607 boolean waitForDebugger = Settings.System.getInt(
8608 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
8609 boolean alwaysFinishActivities = Settings.System.getInt(
8610 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
8611
8612 Configuration configuration = new Configuration();
8613 Settings.System.getConfiguration(resolver, configuration);
8614
8615 synchronized (this) {
8616 mDebugApp = mOrigDebugApp = debugApp;
8617 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
8618 mAlwaysFinishActivities = alwaysFinishActivities;
8619 // This happens before any activities are started, so we can
8620 // change mConfiguration in-place.
8621 mConfiguration.updateFrom(configuration);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08008622 mConfigurationSeq = mConfiguration.seq = 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008623 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 }
8625 }
8626
8627 public boolean testIsSystemReady() {
8628 // no need to synchronize(this) just to read & return the value
8629 return mSystemReady;
8630 }
8631
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07008632 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 // In the simulator, startRunning will never have been called, which
8634 // normally sets a few crucial variables. Do it here instead.
8635 if (!Process.supportsProcesses()) {
8636 mStartRunning = true;
8637 mTopAction = Intent.ACTION_MAIN;
8638 }
8639
8640 synchronized(this) {
8641 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07008642 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 return;
8644 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008645
8646 // Check to see if there are any update receivers to run.
8647 if (!mDidUpdate) {
8648 if (mWaitingUpdate) {
8649 return;
8650 }
8651 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
8652 List<ResolveInfo> ris = null;
8653 try {
8654 ris = ActivityThread.getPackageManager().queryIntentReceivers(
8655 intent, null, 0);
8656 } catch (RemoteException e) {
8657 }
8658 if (ris != null) {
8659 for (int i=ris.size()-1; i>=0; i--) {
8660 if ((ris.get(i).activityInfo.applicationInfo.flags
8661 &ApplicationInfo.FLAG_SYSTEM) == 0) {
8662 ris.remove(i);
8663 }
8664 }
8665 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
8666 for (int i=0; i<ris.size(); i++) {
8667 ActivityInfo ai = ris.get(i).activityInfo;
8668 intent.setComponent(new ComponentName(ai.packageName, ai.name));
8669 IIntentReceiver finisher = null;
Dianne Hackbornd6847842010-01-12 18:14:19 -08008670 if (i == ris.size()-1) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008671 finisher = new IIntentReceiver.Stub() {
8672 public void performReceive(Intent intent, int resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -07008673 String data, Bundle extras, boolean ordered,
8674 boolean sticky)
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008675 throws RemoteException {
8676 synchronized (ActivityManagerService.this) {
8677 mDidUpdate = true;
8678 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07008679 systemReady(goingCallback);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008680 }
8681 };
8682 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08008683 Slog.i(TAG, "Sending system update to: " + intent.getComponent());
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008684 broadcastIntentLocked(null, null, intent, null, finisher,
8685 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackbornd6847842010-01-12 18:14:19 -08008686 if (finisher != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008687 mWaitingUpdate = true;
8688 }
8689 }
8690 }
8691 if (mWaitingUpdate) {
8692 return;
8693 }
8694 mDidUpdate = true;
8695 }
8696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697 mSystemReady = true;
8698 if (!mStartRunning) {
8699 return;
8700 }
8701 }
8702
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008703 ArrayList<ProcessRecord> procsToKill = null;
8704 synchronized(mPidsSelfLocked) {
8705 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
8706 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
8707 if (!isAllowedWhileBooting(proc.info)){
8708 if (procsToKill == null) {
8709 procsToKill = new ArrayList<ProcessRecord>();
8710 }
8711 procsToKill.add(proc);
8712 }
8713 }
8714 }
8715
8716 if (procsToKill != null) {
8717 synchronized(this) {
8718 for (int i=procsToKill.size()-1; i>=0; i--) {
8719 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008720 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008721 removeProcessLocked(proc, true);
8722 }
8723 }
8724 }
8725
Joe Onorato8a9b2202010-02-26 18:56:32 -08008726 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08008727 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008728 SystemClock.uptimeMillis());
8729
8730 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008731 // Make sure we have no pre-ready processes sitting around.
8732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
8734 ResolveInfo ri = mContext.getPackageManager()
8735 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07008736 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008737 CharSequence errorMsg = null;
8738 if (ri != null) {
8739 ActivityInfo ai = ri.activityInfo;
8740 ApplicationInfo app = ai.applicationInfo;
8741 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
8742 mTopAction = Intent.ACTION_FACTORY_TEST;
8743 mTopData = null;
8744 mTopComponent = new ComponentName(app.packageName,
8745 ai.name);
8746 } else {
8747 errorMsg = mContext.getResources().getText(
8748 com.android.internal.R.string.factorytest_not_system);
8749 }
8750 } else {
8751 errorMsg = mContext.getResources().getText(
8752 com.android.internal.R.string.factorytest_no_action);
8753 }
8754 if (errorMsg != null) {
8755 mTopAction = null;
8756 mTopData = null;
8757 mTopComponent = null;
8758 Message msg = Message.obtain();
8759 msg.what = SHOW_FACTORY_ERROR_MSG;
8760 msg.getData().putCharSequence("msg", errorMsg);
8761 mHandler.sendMessage(msg);
8762 }
8763 }
8764 }
8765
8766 retrieveSettings();
8767
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07008768 if (goingCallback != null) goingCallback.run();
8769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008770 synchronized (this) {
8771 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
8772 try {
8773 List apps = ActivityThread.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07008774 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008775 if (apps != null) {
8776 int N = apps.size();
8777 int i;
8778 for (i=0; i<N; i++) {
8779 ApplicationInfo info
8780 = (ApplicationInfo)apps.get(i);
8781 if (info != null &&
8782 !info.packageName.equals("android")) {
8783 addAppLocked(info);
8784 }
8785 }
8786 }
8787 } catch (RemoteException ex) {
8788 // pm is in same process, this will never happen.
8789 }
8790 }
8791
Dianne Hackborn9acc0302009-08-25 00:27:12 -07008792 // Start up initial activity.
8793 mBooting = true;
8794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 try {
8796 if (ActivityThread.getPackageManager().hasSystemUidErrors()) {
8797 Message msg = Message.obtain();
8798 msg.what = SHOW_UID_ERROR_MSG;
8799 mHandler.sendMessage(msg);
8800 }
8801 } catch (RemoteException e) {
8802 }
8803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008804 resumeTopActivityLocked(null);
8805 }
8806 }
8807
Dan Egnorb7f03672009-12-09 16:22:32 -08008808 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08008809 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008810 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08008811 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08008812 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008813 startAppProblemLocked(app);
8814 app.stopFreezingAllLocked();
8815 return handleAppCrashLocked(app);
8816 }
8817
Dan Egnorb7f03672009-12-09 16:22:32 -08008818 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08008819 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08008821 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08008822 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
8823 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 startAppProblemLocked(app);
8825 app.stopFreezingAllLocked();
8826 }
8827
8828 /**
8829 * Generate a process error record, suitable for attachment to a ProcessRecord.
8830 *
8831 * @param app The ProcessRecord in which the error occurred.
8832 * @param condition Crashing, Application Not Responding, etc. Values are defined in
8833 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08008834 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008835 * @param shortMsg Short message describing the crash.
8836 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08008837 * @param stackTrace Full crash stack trace, may be null.
8838 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008839 * @return Returns a fully-formed AppErrorStateInfo record.
8840 */
8841 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08008842 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08008844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008845 report.condition = condition;
8846 report.processName = app.processName;
8847 report.pid = app.pid;
8848 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08008849 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 report.shortMsg = shortMsg;
8851 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08008852 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853
8854 return report;
8855 }
8856
Dan Egnor42471dd2010-01-07 17:25:22 -08008857 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008858 synchronized (this) {
8859 app.crashing = false;
8860 app.crashingReport = null;
8861 app.notResponding = false;
8862 app.notRespondingReport = null;
8863 if (app.anrDialog == fromDialog) {
8864 app.anrDialog = null;
8865 }
8866 if (app.waitDialog == fromDialog) {
8867 app.waitDialog = null;
8868 }
8869 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08008870 handleAppCrashLocked(app);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008871 Slog.i(ActivityManagerService.TAG, "Killing process "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 + app.processName
8873 + " (pid=" + app.pid + ") at user's request");
8874 Process.killProcess(app.pid);
8875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008876 }
8877 }
Dan Egnor42471dd2010-01-07 17:25:22 -08008878
Dan Egnorb7f03672009-12-09 16:22:32 -08008879 private boolean handleAppCrashLocked(ProcessRecord app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008880 long now = SystemClock.uptimeMillis();
8881
8882 Long crashTime = mProcessCrashTimes.get(app.info.processName,
8883 app.info.uid);
8884 if (crashTime != null && now < crashTime+MIN_CRASH_INTERVAL) {
8885 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08008886 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008887 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08008888 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 app.info.processName, app.info.uid);
8890 killServicesLocked(app, false);
8891 for (int i=mHistory.size()-1; i>=0; i--) {
8892 HistoryRecord r = (HistoryRecord)mHistory.get(i);
8893 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008894 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 + r.intent.getComponent().flattenToShortString());
8896 finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed");
8897 }
8898 }
8899 if (!app.persistent) {
8900 // We don't want to start this process again until the user
8901 // explicitly does so... but for persistent process, we really
8902 // need to keep it running. If a persistent process is actually
8903 // repeatedly crashing, then badness for everyone.
Doug Zongker2bec3d42009-12-04 12:52:44 -08008904 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 app.info.processName);
8906 mBadProcesses.put(app.info.processName, app.info.uid, now);
8907 app.bad = true;
8908 mProcessCrashTimes.remove(app.info.processName, app.info.uid);
8909 app.removed = true;
8910 removeProcessLocked(app, false);
8911 return false;
8912 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -07008913 } else {
8914 HistoryRecord r = topRunningActivityLocked(null);
8915 if (r.app == app) {
8916 // If the top running activity is from this crashing
8917 // process, then terminate it to avoid getting in a loop.
8918 Slog.w(TAG, " Force finishing activity "
8919 + r.intent.getComponent().flattenToShortString());
8920 int index = indexOfTokenLocked(r);
8921 finishActivityLocked(r, index,
8922 Activity.RESULT_CANCELED, null, "crashed");
8923 // Also terminate an activities below it that aren't yet
8924 // stopped, to avoid a situation where one will get
8925 // re-start our crashing activity once it gets resumed again.
8926 index--;
8927 if (index >= 0) {
8928 r = (HistoryRecord)mHistory.get(index);
8929 if (r.state == ActivityState.RESUMED
8930 || r.state == ActivityState.PAUSING
8931 || r.state == ActivityState.PAUSED) {
8932 if (!r.isHomeActivity) {
8933 Slog.w(TAG, " Force finishing activity "
8934 + r.intent.getComponent().flattenToShortString());
8935 finishActivityLocked(r, index,
8936 Activity.RESULT_CANCELED, null, "crashed");
8937 }
8938 }
8939 }
8940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008941 }
8942
8943 // Bump up the crash count of any services currently running in the proc.
8944 if (app.services.size() != 0) {
8945 // Any services running in the application need to be placed
8946 // back in the pending list.
8947 Iterator it = app.services.iterator();
8948 while (it.hasNext()) {
8949 ServiceRecord sr = (ServiceRecord)it.next();
8950 sr.crashCount++;
8951 }
8952 }
8953
8954 mProcessCrashTimes.put(app.info.processName, app.info.uid, now);
8955 return true;
8956 }
8957
8958 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08008959 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
8960 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 skipCurrentReceiverLocked(app);
8962 }
8963
8964 void skipCurrentReceiverLocked(ProcessRecord app) {
8965 boolean reschedule = false;
8966 BroadcastRecord r = app.curReceiver;
8967 if (r != null) {
8968 // The current broadcast is waiting for this app's receiver
8969 // to be finished. Looks like that's not going to happen, so
8970 // let the broadcast continue.
8971 logBroadcastReceiverDiscard(r);
8972 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
8973 r.resultExtras, r.resultAbort, true);
8974 reschedule = true;
8975 }
8976 r = mPendingBroadcast;
8977 if (r != null && r.curApp == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008978 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979 "skip & discard pending app " + r);
8980 logBroadcastReceiverDiscard(r);
8981 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
8982 r.resultExtras, r.resultAbort, true);
8983 reschedule = true;
8984 }
8985 if (reschedule) {
8986 scheduleBroadcastsLocked();
8987 }
8988 }
8989
Dan Egnor60d87622009-12-16 16:32:58 -08008990 /**
8991 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
8992 * The application process will exit immediately after this call returns.
8993 * @param app object of the crashing app, null for the system server
8994 * @param crashInfo describing the exception
8995 */
8996 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
8997 ProcessRecord r = findAppProcess(app);
8998
8999 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
9000 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08009001 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08009002 crashInfo.exceptionClassName,
9003 crashInfo.exceptionMessage,
9004 crashInfo.throwFileName,
9005 crashInfo.throwLineNumber);
9006
Dan Egnor42471dd2010-01-07 17:25:22 -08009007 addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08009008
9009 crashApplication(r, crashInfo);
9010 }
9011
9012 /**
9013 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
9014 * @param app object of the crashing app, null for the system server
9015 * @param tag reported by the caller
9016 * @param crashInfo describing the context of the error
9017 * @return true if the process should exit immediately (WTF is fatal)
9018 */
9019 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08009020 ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnor60d87622009-12-16 16:32:58 -08009021 ProcessRecord r = findAppProcess(app);
9022
9023 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
9024 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08009025 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08009026 tag, crashInfo.exceptionMessage);
9027
Dan Egnor42471dd2010-01-07 17:25:22 -08009028 addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08009029
Doug Zongker43866e02010-01-07 12:09:54 -08009030 if (Settings.Secure.getInt(mContext.getContentResolver(),
9031 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08009032 crashApplication(r, crashInfo);
9033 return true;
9034 } else {
9035 return false;
9036 }
9037 }
9038
9039 /**
9040 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
9041 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
9042 */
9043 private ProcessRecord findAppProcess(IBinder app) {
9044 if (app == null) {
9045 return null;
9046 }
9047
9048 synchronized (this) {
9049 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
9050 final int NA = apps.size();
9051 for (int ia=0; ia<NA; ia++) {
9052 ProcessRecord p = apps.valueAt(ia);
9053 if (p.thread != null && p.thread.asBinder() == app) {
9054 return p;
9055 }
9056 }
9057 }
9058
Joe Onorato8a9b2202010-02-26 18:56:32 -08009059 Slog.w(TAG, "Can't find mystery application: " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08009060 return null;
9061 }
9062 }
9063
9064 /**
Dan Egnor42471dd2010-01-07 17:25:22 -08009065 * Write a description of an error (crash, WTF, ANR) to the drop box.
Dan Egnor60d87622009-12-16 16:32:58 -08009066 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
Dan Egnor42471dd2010-01-07 17:25:22 -08009067 * @param process which caused the error, null means the system server
9068 * @param activity which triggered the error, null if unknown
9069 * @param parent activity related to the error, null if unknown
9070 * @param subject line related to the error, null if absent
9071 * @param report in long form describing the error, null if absent
9072 * @param logFile to include in the report, null if none
9073 * @param crashInfo giving an application stack trace, null if absent
Dan Egnor60d87622009-12-16 16:32:58 -08009074 */
Dan Egnor9bdc94b2010-03-04 14:20:31 -08009075 public void addErrorToDropBox(String eventType,
Dan Egnora455d192010-03-12 08:52:28 -08009076 ProcessRecord process, HistoryRecord activity, HistoryRecord parent, String subject,
9077 final String report, final File logFile,
9078 final ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnor9bdc94b2010-03-04 14:20:31 -08009079 // NOTE -- this must never acquire the ActivityManagerService lock,
9080 // otherwise the watchdog may be prevented from resetting the system.
9081
Dan Egnora455d192010-03-12 08:52:28 -08009082 String prefix;
Dan Egnor42471dd2010-01-07 17:25:22 -08009083 if (process == null || process.pid == MY_PID) {
Dan Egnora455d192010-03-12 08:52:28 -08009084 prefix = "system_server_";
Dan Egnor42471dd2010-01-07 17:25:22 -08009085 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Dan Egnora455d192010-03-12 08:52:28 -08009086 prefix = "system_app_";
Dan Egnor60d87622009-12-16 16:32:58 -08009087 } else {
Dan Egnora455d192010-03-12 08:52:28 -08009088 prefix = "data_app_";
Dan Egnor60d87622009-12-16 16:32:58 -08009089 }
9090
Dan Egnora455d192010-03-12 08:52:28 -08009091 final String dropboxTag = prefix + eventType;
9092 final DropBoxManager dbox = (DropBoxManager)
9093 mContext.getSystemService(Context.DROPBOX_SERVICE);
9094
9095 // Exit early if the dropbox isn't configured to accept this report type.
9096 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
9097
9098 final StringBuilder sb = new StringBuilder(1024);
9099 if (process == null || process.pid == MY_PID) {
9100 sb.append("Process: system_server\n");
9101 } else {
9102 sb.append("Process: ").append(process.processName).append("\n");
9103 }
9104 if (process != null) {
9105 int flags = process.info.flags;
9106 IPackageManager pm = ActivityThread.getPackageManager();
9107 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
9108 for (String pkg : process.pkgList) {
9109 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08009110 try {
Dan Egnora455d192010-03-12 08:52:28 -08009111 PackageInfo pi = pm.getPackageInfo(pkg, 0);
9112 if (pi != null) {
9113 sb.append(" v").append(pi.versionCode);
9114 if (pi.versionName != null) {
9115 sb.append(" (").append(pi.versionName).append(")");
9116 }
9117 }
9118 } catch (RemoteException e) {
9119 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08009120 }
Dan Egnora455d192010-03-12 08:52:28 -08009121 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08009122 }
Dan Egnora455d192010-03-12 08:52:28 -08009123 }
9124 if (activity != null) {
9125 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
9126 }
9127 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
9128 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
9129 }
9130 if (parent != null && parent != activity) {
9131 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
9132 }
9133 if (subject != null) {
9134 sb.append("Subject: ").append(subject).append("\n");
9135 }
9136 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
9137 sb.append("\n");
9138
9139 // Do the rest in a worker thread to avoid blocking the caller on I/O
9140 // (After this point, we shouldn't access AMS internal data structures.)
9141 Thread worker = new Thread("Error dump: " + dropboxTag) {
9142 @Override
9143 public void run() {
9144 if (report != null) {
9145 sb.append(report);
9146 }
9147 if (logFile != null) {
9148 try {
9149 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
9150 } catch (IOException e) {
9151 Slog.e(TAG, "Error reading " + logFile, e);
9152 }
9153 }
9154 if (crashInfo != null && crashInfo.stackTrace != null) {
9155 sb.append(crashInfo.stackTrace);
9156 }
9157
9158 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
9159 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
9160 if (lines > 0) {
9161 sb.append("\n");
9162
9163 // Merge several logcat streams, and take the last N lines
9164 InputStreamReader input = null;
9165 try {
9166 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
9167 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
9168 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
9169
9170 try { logcat.getOutputStream().close(); } catch (IOException e) {}
9171 try { logcat.getErrorStream().close(); } catch (IOException e) {}
9172 input = new InputStreamReader(logcat.getInputStream());
9173
9174 int num;
9175 char[] buf = new char[8192];
9176 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
9177 } catch (IOException e) {
9178 Slog.e(TAG, "Error running logcat", e);
9179 } finally {
9180 if (input != null) try { input.close(); } catch (IOException e) {}
9181 }
9182 }
9183
9184 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08009185 }
Dan Egnora455d192010-03-12 08:52:28 -08009186 };
9187
9188 if (process == null || process.pid == MY_PID) {
9189 worker.run(); // We may be about to die -- need to run this synchronously
9190 } else {
9191 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08009192 }
9193 }
9194
9195 /**
9196 * Bring up the "unexpected error" dialog box for a crashing app.
9197 * Deal with edge cases (intercepts from instrumented applications,
9198 * ActivityController, error intent receivers, that sort of thing).
9199 * @param r the application crashing
9200 * @param crashInfo describing the failure
9201 */
9202 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08009203 long timeMillis = System.currentTimeMillis();
9204 String shortMsg = crashInfo.exceptionClassName;
9205 String longMsg = crashInfo.exceptionMessage;
9206 String stackTrace = crashInfo.stackTrace;
9207 if (shortMsg != null && longMsg != null) {
9208 longMsg = shortMsg + ": " + longMsg;
9209 } else if (shortMsg != null) {
9210 longMsg = shortMsg;
9211 }
9212
Dan Egnor60d87622009-12-16 16:32:58 -08009213 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009214 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07009215 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 try {
9217 String name = r != null ? r.processName : null;
9218 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08009219 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08009220 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009221 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 + " at watcher's request");
9223 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08009224 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009225 }
9226 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07009227 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 }
9229 }
9230
9231 final long origId = Binder.clearCallingIdentity();
9232
9233 // If this process is running instrumentation, finish it.
9234 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009235 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08009237 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
9238 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 Bundle info = new Bundle();
9240 info.putString("shortMsg", shortMsg);
9241 info.putString("longMsg", longMsg);
9242 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
9243 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08009244 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009245 }
9246
Dan Egnor60d87622009-12-16 16:32:58 -08009247 // If we can't identify the process or it's already exceeded its crash quota,
9248 // quit right away without showing a crash dialog.
9249 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009250 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08009251 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009252 }
9253
9254 Message msg = Message.obtain();
9255 msg.what = SHOW_ERROR_MSG;
9256 HashMap data = new HashMap();
9257 data.put("result", result);
9258 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 msg.obj = data;
9260 mHandler.sendMessage(msg);
9261
9262 Binder.restoreCallingIdentity(origId);
9263 }
9264
9265 int res = result.get();
9266
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009267 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009268 synchronized (this) {
9269 if (r != null) {
9270 mProcessCrashTimes.put(r.info.processName, r.info.uid,
9271 SystemClock.uptimeMillis());
9272 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009273 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08009274 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009275 }
9276 }
9277
9278 if (appErrorIntent != null) {
9279 try {
9280 mContext.startActivity(appErrorIntent);
9281 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009282 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009284 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 }
Dan Egnorb7f03672009-12-09 16:22:32 -08009286
9287 Intent createAppErrorIntentLocked(ProcessRecord r,
9288 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
9289 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009290 if (report == null) {
9291 return null;
9292 }
9293 Intent result = new Intent(Intent.ACTION_APP_ERROR);
9294 result.setComponent(r.errorReportReceiver);
9295 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
9296 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
9297 return result;
9298 }
9299
Dan Egnorb7f03672009-12-09 16:22:32 -08009300 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
9301 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009302 if (r.errorReportReceiver == null) {
9303 return null;
9304 }
9305
9306 if (!r.crashing && !r.notResponding) {
9307 return null;
9308 }
9309
Dan Egnorb7f03672009-12-09 16:22:32 -08009310 ApplicationErrorReport report = new ApplicationErrorReport();
9311 report.packageName = r.info.packageName;
9312 report.installerPackageName = r.errorReportReceiver.getPackageName();
9313 report.processName = r.processName;
9314 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01009315 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009316
Dan Egnorb7f03672009-12-09 16:22:32 -08009317 if (r.crashing) {
9318 report.type = ApplicationErrorReport.TYPE_CRASH;
9319 report.crashInfo = crashInfo;
9320 } else if (r.notResponding) {
9321 report.type = ApplicationErrorReport.TYPE_ANR;
9322 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009323
Dan Egnorb7f03672009-12-09 16:22:32 -08009324 report.anrInfo.activity = r.notRespondingReport.tag;
9325 report.anrInfo.cause = r.notRespondingReport.shortMsg;
9326 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009327 }
9328
Dan Egnorb7f03672009-12-09 16:22:32 -08009329 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02009330 }
9331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
9333 // assume our apps are happy - lazy create the list
9334 List<ActivityManager.ProcessErrorStateInfo> errList = null;
9335
9336 synchronized (this) {
9337
9338 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08009339 for (int i=mLruProcesses.size()-1; i>=0; i--) {
9340 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009341 if ((app.thread != null) && (app.crashing || app.notResponding)) {
9342 // This one's in trouble, so we'll generate a report for it
9343 // crashes are higher priority (in case there's a crash *and* an anr)
9344 ActivityManager.ProcessErrorStateInfo report = null;
9345 if (app.crashing) {
9346 report = app.crashingReport;
9347 } else if (app.notResponding) {
9348 report = app.notRespondingReport;
9349 }
9350
9351 if (report != null) {
9352 if (errList == null) {
9353 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
9354 }
9355 errList.add(report);
9356 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009357 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009358 " crashing = " + app.crashing +
9359 " notResponding = " + app.notResponding);
9360 }
9361 }
9362 }
9363 }
9364
9365 return errList;
9366 }
9367
9368 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
9369 // Lazy instantiation of list
9370 List<ActivityManager.RunningAppProcessInfo> runList = null;
9371 synchronized (this) {
9372 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08009373 for (int i=mLruProcesses.size()-1; i>=0; i--) {
9374 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009375 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
9376 // Generate process state info for running application
9377 ActivityManager.RunningAppProcessInfo currApp =
9378 new ActivityManager.RunningAppProcessInfo(app.processName,
9379 app.pid, app.getPackageList());
Dianne Hackborneb034652009-09-07 00:49:58 -07009380 currApp.uid = app.info.uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009381 int adj = app.curAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08009382 if (adj >= EMPTY_APP_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY;
9384 } else if (adj >= HIDDEN_APP_MIN_ADJ) {
9385 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
The Android Open Source Project4df24232009-03-05 14:34:35 -08009386 currApp.lru = adj - HIDDEN_APP_MIN_ADJ + 1;
9387 } else if (adj >= HOME_APP_ADJ) {
9388 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
9389 currApp.lru = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009390 } else if (adj >= SECONDARY_SERVER_ADJ) {
9391 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
9392 } else if (adj >= VISIBLE_APP_ADJ) {
9393 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
9394 } else {
9395 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
9396 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07009397 currApp.importanceReasonCode = app.adjTypeCode;
9398 if (app.adjSource instanceof ProcessRecord) {
9399 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
9400 } else if (app.adjSource instanceof HistoryRecord) {
9401 HistoryRecord r = (HistoryRecord)app.adjSource;
9402 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
9403 }
9404 if (app.adjTarget instanceof ComponentName) {
9405 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
9406 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009407 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009408 // + " lru=" + currApp.lru);
9409 if (runList == null) {
9410 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
9411 }
9412 runList.add(currApp);
9413 }
9414 }
9415 }
9416 return runList;
9417 }
9418
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07009419 public List<ApplicationInfo> getRunningExternalApplications() {
9420 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
9421 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
9422 if (runningApps != null && runningApps.size() > 0) {
9423 Set<String> extList = new HashSet<String>();
9424 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
9425 if (app.pkgList != null) {
9426 for (String pkg : app.pkgList) {
9427 extList.add(pkg);
9428 }
9429 }
9430 }
9431 IPackageManager pm = ActivityThread.getPackageManager();
9432 for (String pkg : extList) {
9433 try {
9434 ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
9435 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
9436 retList.add(info);
9437 }
9438 } catch (RemoteException e) {
9439 }
9440 }
9441 }
9442 return retList;
9443 }
9444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 @Override
9446 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009447 if (checkCallingPermission(android.Manifest.permission.DUMP)
9448 != PackageManager.PERMISSION_GRANTED) {
9449 pw.println("Permission Denial: can't dump ActivityManager from from pid="
9450 + Binder.getCallingPid()
9451 + ", uid=" + Binder.getCallingUid()
9452 + " without permission "
9453 + android.Manifest.permission.DUMP);
9454 return;
9455 }
9456
9457 boolean dumpAll = false;
9458
9459 int opti = 0;
9460 while (opti < args.length) {
9461 String opt = args[opti];
9462 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
9463 break;
9464 }
9465 opti++;
9466 if ("-a".equals(opt)) {
9467 dumpAll = true;
9468 } else if ("-h".equals(opt)) {
9469 pw.println("Activity manager dump options:");
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07009470 pw.println(" [-a] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009471 pw.println(" cmd may be one of:");
9472 pw.println(" activities: activity stack state");
9473 pw.println(" broadcasts: broadcast state");
9474 pw.println(" intents: pending intent state");
9475 pw.println(" processes: process state");
9476 pw.println(" providers: content provider state");
9477 pw.println(" services: service state");
9478 pw.println(" service [name]: service client-side state");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009480 } else {
9481 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009483 }
9484
9485 // Is the caller requesting to dump a particular piece of data?
9486 if (opti < args.length) {
9487 String cmd = args[opti];
9488 opti++;
9489 if ("activities".equals(cmd) || "a".equals(cmd)) {
9490 synchronized (this) {
9491 dumpActivitiesLocked(fd, pw, args, opti, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009492 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009493 return;
9494 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
9495 synchronized (this) {
9496 dumpBroadcastsLocked(fd, pw, args, opti, true);
9497 }
9498 return;
9499 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
9500 synchronized (this) {
9501 dumpPendingIntentsLocked(fd, pw, args, opti, true);
9502 }
9503 return;
9504 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
9505 synchronized (this) {
9506 dumpProcessesLocked(fd, pw, args, opti, true);
9507 }
9508 return;
9509 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
9510 synchronized (this) {
9511 dumpProvidersLocked(fd, pw, args, opti, true);
9512 }
9513 return;
9514 } else if ("service".equals(cmd)) {
9515 dumpService(fd, pw, args, opti, true);
9516 return;
9517 } else if ("services".equals(cmd) || "s".equals(cmd)) {
9518 synchronized (this) {
9519 dumpServicesLocked(fd, pw, args, opti, true);
9520 }
9521 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009522 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009523 }
9524
9525 // No piece of data specified, dump everything.
9526 synchronized (this) {
9527 boolean needSep;
9528 if (dumpAll) {
9529 pw.println("Providers in Current Activity Manager State:");
9530 }
9531 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll);
9532 if (needSep) {
9533 pw.println(" ");
9534 }
9535 if (dumpAll) {
9536 pw.println("-------------------------------------------------------------------------------");
9537 pw.println("Broadcasts in Current Activity Manager State:");
9538 }
9539 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll);
9540 if (needSep) {
9541 pw.println(" ");
9542 }
9543 if (dumpAll) {
9544 pw.println("-------------------------------------------------------------------------------");
9545 pw.println("Services in Current Activity Manager State:");
9546 }
9547 needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll);
9548 if (needSep) {
9549 pw.println(" ");
9550 }
9551 if (dumpAll) {
9552 pw.println("-------------------------------------------------------------------------------");
9553 pw.println("PendingIntents in Current Activity Manager State:");
9554 }
9555 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll);
9556 if (needSep) {
9557 pw.println(" ");
9558 }
9559 if (dumpAll) {
9560 pw.println("-------------------------------------------------------------------------------");
9561 pw.println("Activities in Current Activity Manager State:");
9562 }
9563 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, !dumpAll);
9564 if (needSep) {
9565 pw.println(" ");
9566 }
9567 if (dumpAll) {
9568 pw.println("-------------------------------------------------------------------------------");
9569 pw.println("Processes in Current Activity Manager State:");
9570 }
9571 dumpProcessesLocked(fd, pw, args, opti, dumpAll);
9572 }
9573 }
9574
9575 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9576 int opti, boolean dumpAll, boolean needHeader) {
9577 if (needHeader) {
9578 pw.println(" Activity stack:");
9579 }
9580 dumpHistoryList(pw, mHistory, " ", "Hist", true);
9581 pw.println(" ");
9582 pw.println(" Running activities (most recent first):");
9583 dumpHistoryList(pw, mLRUActivities, " ", "Run", false);
9584 if (mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009586 pw.println(" Activities waiting for another to become visible:");
9587 dumpHistoryList(pw, mWaitingVisibleActivities, " ", "Wait", false);
9588 }
9589 if (mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009590 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009591 pw.println(" Activities waiting to stop:");
9592 dumpHistoryList(pw, mStoppingActivities, " ", "Stop", false);
9593 }
9594 if (mFinishingActivities.size() > 0) {
9595 pw.println(" ");
9596 pw.println(" Activities waiting to finish:");
9597 dumpHistoryList(pw, mFinishingActivities, " ", "Fin", false);
9598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009599
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009600 pw.println(" ");
9601 pw.println(" mPausingActivity: " + mPausingActivity);
9602 pw.println(" mResumedActivity: " + mResumedActivity);
9603 pw.println(" mFocusedActivity: " + mFocusedActivity);
9604 pw.println(" mLastPausedActivity: " + mLastPausedActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009605
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009606 if (dumpAll && mRecentTasks.size() > 0) {
9607 pw.println(" ");
9608 pw.println("Recent tasks in Current Activity Manager State:");
9609
9610 final int N = mRecentTasks.size();
9611 for (int i=0; i<N; i++) {
9612 TaskRecord tr = mRecentTasks.get(i);
9613 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
9614 pw.println(tr);
9615 mRecentTasks.get(i).dump(pw, " ");
9616 }
9617 }
9618
9619 pw.println(" ");
9620 pw.println(" mCurTask: " + mCurTask);
9621
9622 return true;
9623 }
9624
9625 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9626 int opti, boolean dumpAll) {
9627 boolean needSep = false;
9628 int numPers = 0;
9629
9630 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
9632 final int NA = procs.size();
9633 for (int ia=0; ia<NA; ia++) {
9634 if (!needSep) {
9635 pw.println(" All known processes:");
9636 needSep = true;
9637 }
9638 ProcessRecord r = procs.valueAt(ia);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009639 pw.print(r.persistent ? " *PERS*" : " *APP*");
9640 pw.print(" UID "); pw.print(procs.keyAt(ia));
9641 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 r.dump(pw, " ");
9643 if (r.persistent) {
9644 numPers++;
9645 }
9646 }
9647 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009648 }
9649
9650 if (mLruProcesses.size() > 0) {
9651 if (needSep) pw.println(" ");
9652 needSep = true;
9653 pw.println(" Running processes (most recent first):");
9654 dumpProcessList(pw, this, mLruProcesses, " ",
9655 "App ", "PERS", true);
9656 needSep = true;
9657 }
9658
9659 synchronized (mPidsSelfLocked) {
9660 if (mPidsSelfLocked.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 if (needSep) pw.println(" ");
9662 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009663 pw.println(" PID mappings:");
9664 for (int i=0; i<mPidsSelfLocked.size(); i++) {
9665 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
9666 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009667 }
9668 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009669 }
9670
9671 if (mForegroundProcesses.size() > 0) {
9672 if (needSep) pw.println(" ");
9673 needSep = true;
9674 pw.println(" Foreground Processes:");
9675 for (int i=0; i<mForegroundProcesses.size(); i++) {
9676 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
9677 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009678 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009679 }
9680
9681 if (mPersistentStartingProcesses.size() > 0) {
9682 if (needSep) pw.println(" ");
9683 needSep = true;
9684 pw.println(" Persisent processes that are starting:");
9685 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
9686 "Starting Norm", "Restarting PERS", false);
9687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009688
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009689 if (mStartingProcesses.size() > 0) {
9690 if (needSep) pw.println(" ");
9691 needSep = true;
9692 pw.println(" Processes that are starting:");
9693 dumpProcessList(pw, this, mStartingProcesses, " ",
9694 "Starting Norm", "Starting PERS", false);
9695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009697 if (mRemovedProcesses.size() > 0) {
9698 if (needSep) pw.println(" ");
9699 needSep = true;
9700 pw.println(" Processes that are being removed:");
9701 dumpProcessList(pw, this, mRemovedProcesses, " ",
9702 "Removed Norm", "Removed PERS", false);
9703 }
9704
9705 if (mProcessesOnHold.size() > 0) {
9706 if (needSep) pw.println(" ");
9707 needSep = true;
9708 pw.println(" Processes that are on old until the system is ready:");
9709 dumpProcessList(pw, this, mProcessesOnHold, " ",
9710 "OnHold Norm", "OnHold PERS", false);
9711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009713 if (mProcessesToGc.size() > 0) {
9714 if (needSep) pw.println(" ");
9715 needSep = true;
9716 pw.println(" Processes that are waiting to GC:");
9717 long now = SystemClock.uptimeMillis();
9718 for (int i=0; i<mProcessesToGc.size(); i++) {
9719 ProcessRecord proc = mProcessesToGc.get(i);
9720 pw.print(" Process "); pw.println(proc);
9721 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
9722 pw.print(", last gced=");
9723 pw.print(now-proc.lastRequestedGc);
9724 pw.print(" ms ago, last lowMem=");
9725 pw.print(now-proc.lastLowMemory);
9726 pw.println(" ms ago");
9727
9728 }
9729 }
9730
9731 if (mProcessCrashTimes.getMap().size() > 0) {
9732 if (needSep) pw.println(" ");
9733 needSep = true;
9734 pw.println(" Time since processes crashed:");
9735 long now = SystemClock.uptimeMillis();
9736 for (Map.Entry<String, SparseArray<Long>> procs
9737 : mProcessCrashTimes.getMap().entrySet()) {
9738 SparseArray<Long> uids = procs.getValue();
9739 final int N = uids.size();
9740 for (int i=0; i<N; i++) {
9741 pw.print(" Process "); pw.print(procs.getKey());
9742 pw.print(" uid "); pw.print(uids.keyAt(i));
9743 pw.print(": last crashed ");
9744 pw.print((now-uids.valueAt(i)));
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009745 pw.println(" ms ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009746 }
9747 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009750 if (mBadProcesses.getMap().size() > 0) {
9751 if (needSep) pw.println(" ");
9752 needSep = true;
9753 pw.println(" Bad processes:");
9754 for (Map.Entry<String, SparseArray<Long>> procs
9755 : mBadProcesses.getMap().entrySet()) {
9756 SparseArray<Long> uids = procs.getValue();
9757 final int N = uids.size();
9758 for (int i=0; i<N; i++) {
9759 pw.print(" Bad process "); pw.print(procs.getKey());
9760 pw.print(" uid "); pw.print(uids.keyAt(i));
9761 pw.print(": crashed at time ");
9762 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763 }
9764 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009767 pw.println(" ");
9768 pw.println(" mHomeProcess: " + mHomeProcess);
9769 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackbornd49258f2010-03-26 00:44:29 -07009770 pw.println(" mConfigWillChange: " + mConfigWillChange);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009771 pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown);
9772 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
9773 || mOrigWaitForDebugger) {
9774 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
9775 + " mDebugTransient=" + mDebugTransient
9776 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
9777 }
9778 if (mAlwaysFinishActivities || mController != null) {
9779 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
9780 + " mController=" + mController);
9781 }
9782 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009784 pw.println(" mStartRunning=" + mStartRunning
9785 + " mSystemReady=" + mSystemReady
9786 + " mBooting=" + mBooting
9787 + " mBooted=" + mBooted
9788 + " mFactoryTest=" + mFactoryTest);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009789 pw.println(" mGoingToSleep=" + mGoingToSleep);
9790 pw.println(" mLaunchingActivity=" + mLaunchingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009791 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009792
9793 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 }
9795
9796 /**
9797 * There are three ways to call this:
9798 * - no service specified: dump all the services
9799 * - a flattened component name that matched an existing service was specified as the
9800 * first arg: dump that one service
9801 * - the first arg isn't the flattened component name of an existing service:
9802 * dump all services whose component contains the first arg as a substring
9803 */
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009804 protected void dumpService(FileDescriptor fd, PrintWriter pw, String[] args,
9805 int opti, boolean dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 String[] newArgs;
9807 String componentNameString;
9808 ServiceRecord r;
Kenny Root3619b9ab2010-02-13 10:05:42 -08009809 if (opti >= args.length) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009810 componentNameString = null;
9811 newArgs = EMPTY_STRING_ARRAY;
9812 r = null;
9813 } else {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009814 componentNameString = args[opti];
9815 opti++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009816 ComponentName componentName = ComponentName.unflattenFromString(componentNameString);
9817 r = componentName != null ? mServices.get(componentName) : null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009818 newArgs = new String[args.length - opti];
9819 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009820 }
9821
9822 if (r != null) {
9823 dumpService(fd, pw, r, newArgs);
9824 } else {
9825 for (ServiceRecord r1 : mServices.values()) {
9826 if (componentNameString == null
9827 || r1.name.flattenToString().contains(componentNameString)) {
9828 dumpService(fd, pw, r1, newArgs);
9829 }
9830 }
9831 }
9832 }
9833
9834 /**
9835 * Invokes IApplicationThread.dumpService() on the thread of the specified service if
9836 * there is a thread associated with the service.
9837 */
9838 private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args) {
9839 pw.println(" Service " + r.name.flattenToString());
9840 if (r.app != null && r.app.thread != null) {
9841 try {
9842 // flush anything that is already in the PrintWriter since the thread is going
9843 // to write to the file descriptor directly
9844 pw.flush();
9845 r.app.thread.dumpService(fd, r, args);
9846 pw.print("\n");
9847 } catch (RemoteException e) {
9848 pw.println("got a RemoteException while dumping the service");
9849 }
9850 }
9851 }
9852
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009853 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9854 int opti, boolean dumpAll) {
9855 boolean needSep = false;
9856
9857 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009858 if (mRegisteredReceivers.size() > 0) {
9859 pw.println(" ");
9860 pw.println(" Registered Receivers:");
9861 Iterator it = mRegisteredReceivers.values().iterator();
9862 while (it.hasNext()) {
9863 ReceiverList r = (ReceiverList)it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009864 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 r.dump(pw, " ");
9866 }
9867 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 pw.println(" ");
9870 pw.println("Receiver Resolver Table:");
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07009871 mReceiverResolver.dump(pw, null, " ", null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009872 needSep = true;
9873 }
9874
9875 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
9876 || mPendingBroadcast != null) {
9877 if (mParallelBroadcasts.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009879 pw.println(" Active broadcasts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009881 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
9882 pw.println(" Broadcast #" + i + ":");
9883 mParallelBroadcasts.get(i).dump(pw, " ");
9884 }
9885 if (mOrderedBroadcasts.size() > 0) {
9886 pw.println(" ");
9887 pw.println(" Active serialized broadcasts:");
9888 }
9889 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
9890 pw.println(" Serialized Broadcast #" + i + ":");
9891 mOrderedBroadcasts.get(i).dump(pw, " ");
9892 }
9893 pw.println(" ");
9894 pw.println(" Pending broadcast:");
9895 if (mPendingBroadcast != null) {
9896 mPendingBroadcast.dump(pw, " ");
9897 } else {
9898 pw.println(" (null)");
9899 }
9900 needSep = true;
9901 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009903 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 pw.println(" ");
Dianne Hackborn12527f92009-11-11 17:39:50 -08009905 pw.println(" Historical broadcasts:");
9906 for (int i=0; i<MAX_BROADCAST_HISTORY; i++) {
9907 BroadcastRecord r = mBroadcastHistory[i];
9908 if (r == null) {
9909 break;
9910 }
9911 pw.println(" Historical Broadcast #" + i + ":");
9912 r.dump(pw, " ");
9913 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009914 needSep = true;
9915 }
9916
9917 if (mStickyBroadcasts != null) {
Dianne Hackborn12527f92009-11-11 17:39:50 -08009918 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009919 pw.println(" Sticky broadcasts:");
9920 StringBuilder sb = new StringBuilder(128);
9921 for (Map.Entry<String, ArrayList<Intent>> ent
9922 : mStickyBroadcasts.entrySet()) {
9923 pw.print(" * Sticky action "); pw.print(ent.getKey());
9924 pw.println(":");
9925 ArrayList<Intent> intents = ent.getValue();
9926 final int N = intents.size();
9927 for (int i=0; i<N; i++) {
9928 sb.setLength(0);
9929 sb.append(" Intent: ");
9930 intents.get(i).toShortString(sb, true, false);
9931 pw.println(sb.toString());
9932 Bundle bundle = intents.get(i).getExtras();
9933 if (bundle != null) {
9934 pw.print(" ");
9935 pw.println(bundle.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 }
9937 }
9938 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009939 needSep = true;
9940 }
9941
9942 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009944 pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009945 pw.println(" mHandler:");
9946 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009947 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009949
9950 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009951 }
9952
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009953 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
9954 int opti, boolean dumpAll) {
9955 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009957 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 if (mServices.size() > 0) {
9959 pw.println(" Active services:");
9960 Iterator<ServiceRecord> it = mServices.values().iterator();
9961 while (it.hasNext()) {
9962 ServiceRecord r = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07009963 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 r.dump(pw, " ");
9965 }
9966 needSep = true;
9967 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009969
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009970 if (mPendingServices.size() > 0) {
9971 if (needSep) pw.println(" ");
9972 pw.println(" Pending services:");
9973 for (int i=0; i<mPendingServices.size(); i++) {
9974 ServiceRecord r = mPendingServices.get(i);
9975 pw.print(" * Pending "); pw.println(r);
9976 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009978 needSep = true;
9979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009980
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009981 if (mRestartingServices.size() > 0) {
9982 if (needSep) pw.println(" ");
9983 pw.println(" Restarting services:");
9984 for (int i=0; i<mRestartingServices.size(); i++) {
9985 ServiceRecord r = mRestartingServices.get(i);
9986 pw.print(" * Restarting "); pw.println(r);
9987 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009989 needSep = true;
9990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009991
Dianne Hackbornc59411b2009-12-21 20:10:14 -08009992 if (mStoppingServices.size() > 0) {
9993 if (needSep) pw.println(" ");
9994 pw.println(" Stopping services:");
9995 for (int i=0; i<mStoppingServices.size(); i++) {
9996 ServiceRecord r = mStoppingServices.get(i);
9997 pw.print(" * Stopping "); pw.println(r);
9998 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009999 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010000 needSep = true;
10001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010003 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010004 if (mServiceConnections.size() > 0) {
10005 if (needSep) pw.println(" ");
10006 pw.println(" Connection bindings to services:");
10007 Iterator<ConnectionRecord> it
10008 = mServiceConnections.values().iterator();
10009 while (it.hasNext()) {
10010 ConnectionRecord r = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010011 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010012 r.dump(pw, " ");
10013 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010014 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 }
10016 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010017
10018 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 }
10020
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010021 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
10022 int opti, boolean dumpAll) {
10023 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010024
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010025 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010026 if (mProvidersByClass.size() > 0) {
10027 if (needSep) pw.println(" ");
10028 pw.println(" Published content providers (by class):");
10029 Iterator it = mProvidersByClass.entrySet().iterator();
10030 while (it.hasNext()) {
10031 Map.Entry e = (Map.Entry)it.next();
10032 ContentProviderRecord r = (ContentProviderRecord)e.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010033 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 r.dump(pw, " ");
10035 }
10036 needSep = true;
10037 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010038
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010039 if (mProvidersByName.size() > 0) {
10040 pw.println(" ");
10041 pw.println(" Authority to provider mappings:");
10042 Iterator it = mProvidersByName.entrySet().iterator();
10043 while (it.hasNext()) {
10044 Map.Entry e = (Map.Entry)it.next();
10045 ContentProviderRecord r = (ContentProviderRecord)e.getValue();
10046 pw.print(" "); pw.print(e.getKey()); pw.print(": ");
10047 pw.println(r);
10048 }
10049 needSep = true;
10050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010052
10053 if (mLaunchingProviders.size() > 0) {
10054 if (needSep) pw.println(" ");
10055 pw.println(" Launching content providers:");
10056 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
10057 pw.print(" Launching #"); pw.print(i); pw.print(": ");
10058 pw.println(mLaunchingProviders.get(i));
10059 }
10060 needSep = true;
10061 }
10062
10063 if (mGrantedUriPermissions.size() > 0) {
10064 pw.println();
10065 pw.println("Granted Uri Permissions:");
10066 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
10067 int uid = mGrantedUriPermissions.keyAt(i);
10068 HashMap<Uri, UriPermission> perms
10069 = mGrantedUriPermissions.valueAt(i);
10070 pw.print(" * UID "); pw.print(uid);
10071 pw.println(" holds:");
10072 for (UriPermission perm : perms.values()) {
10073 pw.print(" "); pw.println(perm);
10074 perm.dump(pw, " ");
10075 }
10076 }
10077 needSep = true;
10078 }
10079
10080 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 }
10082
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010083 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
10084 int opti, boolean dumpAll) {
10085 boolean needSep = false;
10086
10087 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088 if (this.mIntentSenderRecords.size() > 0) {
10089 Iterator<WeakReference<PendingIntentRecord>> it
10090 = mIntentSenderRecords.values().iterator();
10091 while (it.hasNext()) {
10092 WeakReference<PendingIntentRecord> ref = it.next();
10093 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010094 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 if (rec != null) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010096 pw.print(" * "); pw.println(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 rec.dump(pw, " ");
10098 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010099 pw.print(" * "); pw.print(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010100 }
10101 }
10102 }
10103 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -080010104
10105 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010106 }
10107
10108 private static final void dumpHistoryList(PrintWriter pw, List list,
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010109 String prefix, String label, boolean complete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110 TaskRecord lastTask = null;
10111 for (int i=list.size()-1; i>=0; i--) {
10112 HistoryRecord r = (HistoryRecord)list.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010113 final boolean full = complete || !r.inHistory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 if (lastTask != r.task) {
10115 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010116 pw.print(prefix);
10117 pw.print(full ? "* " : " ");
10118 pw.println(lastTask);
10119 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010120 lastTask.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -070010123 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
10124 pw.print(" #"); pw.print(i); pw.print(": ");
10125 pw.println(r);
10126 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010127 r.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 }
10130 }
10131
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010132 private static String buildOomTag(String prefix, String space, int val, int base) {
10133 if (val == base) {
10134 if (space == null) return prefix;
10135 return prefix + " ";
10136 }
10137 return prefix + "+" + Integer.toString(val-base);
10138 }
10139
10140 private static final int dumpProcessList(PrintWriter pw,
10141 ActivityManagerService service, List list,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010142 String prefix, String normalLabel, String persistentLabel,
10143 boolean inclOomAdj) {
10144 int numPers = 0;
10145 for (int i=list.size()-1; i>=0; i--) {
10146 ProcessRecord r = (ProcessRecord)list.get(i);
10147 if (false) {
10148 pw.println(prefix + (r.persistent ? persistentLabel : normalLabel)
10149 + " #" + i + ":");
10150 r.dump(pw, prefix + " ");
10151 } else if (inclOomAdj) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010152 String oomAdj;
10153 if (r.setAdj >= EMPTY_APP_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010154 oomAdj = buildOomTag("empty", null, r.setAdj, EMPTY_APP_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010155 } else if (r.setAdj >= HIDDEN_APP_MIN_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010156 oomAdj = buildOomTag("bak", " ", r.setAdj, HIDDEN_APP_MIN_ADJ);
10157 } else if (r.setAdj >= HOME_APP_ADJ) {
10158 oomAdj = buildOomTag("home ", null, r.setAdj, HOME_APP_ADJ);
10159 } else if (r.setAdj >= SECONDARY_SERVER_ADJ) {
10160 oomAdj = buildOomTag("svc", " ", r.setAdj, SECONDARY_SERVER_ADJ);
10161 } else if (r.setAdj >= BACKUP_APP_ADJ) {
10162 oomAdj = buildOomTag("bckup", null, r.setAdj, BACKUP_APP_ADJ);
10163 } else if (r.setAdj >= VISIBLE_APP_ADJ) {
10164 oomAdj = buildOomTag("vis ", null, r.setAdj, VISIBLE_APP_ADJ);
10165 } else if (r.setAdj >= FOREGROUND_APP_ADJ) {
10166 oomAdj = buildOomTag("fore ", null, r.setAdj, FOREGROUND_APP_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010167 } else if (r.setAdj >= CORE_SERVER_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010168 oomAdj = buildOomTag("core ", null, r.setAdj, CORE_SERVER_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010169 } else if (r.setAdj >= SYSTEM_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010170 oomAdj = buildOomTag("sys ", null, r.setAdj, SYSTEM_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010171 } else {
10172 oomAdj = Integer.toString(r.setAdj);
10173 }
10174 String schedGroup;
10175 switch (r.setSchedGroup) {
10176 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
10177 schedGroup = "B";
10178 break;
10179 case Process.THREAD_GROUP_DEFAULT:
10180 schedGroup = "F";
10181 break;
10182 default:
10183 schedGroup = Integer.toString(r.setSchedGroup);
10184 break;
10185 }
10186 pw.println(String.format("%s%s #%2d: adj=%s/%s %s (%s)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 prefix, (r.persistent ? persistentLabel : normalLabel),
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010188 i, oomAdj, schedGroup, r.toShortString(), r.adjType));
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010189 if (r.adjSource != null || r.adjTarget != null) {
10190 pw.println(prefix + " " + r.adjTarget
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010191 + "<=" + r.adjSource);
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193 } else {
10194 pw.println(String.format("%s%s #%2d: %s",
10195 prefix, (r.persistent ? persistentLabel : normalLabel),
10196 i, r.toString()));
10197 }
10198 if (r.persistent) {
10199 numPers++;
10200 }
10201 }
10202 return numPers;
10203 }
10204
10205 private static final void dumpApplicationMemoryUsage(FileDescriptor fd,
10206 PrintWriter pw, List list, String prefix, String[] args) {
Dianne Hackborn6447ca32009-04-07 19:50:08 -070010207 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 long uptime = SystemClock.uptimeMillis();
10209 long realtime = SystemClock.elapsedRealtime();
10210
10211 if (isCheckinRequest) {
10212 // short checkin version
10213 pw.println(uptime + "," + realtime);
10214 pw.flush();
10215 } else {
10216 pw.println("Applications Memory Usage (kB):");
10217 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
10218 }
10219 for (int i = list.size() - 1 ; i >= 0 ; i--) {
10220 ProcessRecord r = (ProcessRecord)list.get(i);
10221 if (r.thread != null) {
10222 if (!isCheckinRequest) {
10223 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
10224 pw.flush();
10225 }
10226 try {
10227 r.thread.asBinder().dump(fd, args);
10228 } catch (RemoteException e) {
10229 if (!isCheckinRequest) {
10230 pw.println("Got RemoteException!");
10231 pw.flush();
10232 }
10233 }
10234 }
10235 }
10236 }
10237
10238 /**
10239 * Searches array of arguments for the specified string
10240 * @param args array of argument strings
10241 * @param value value to search for
10242 * @return true if the value is contained in the array
10243 */
10244 private static boolean scanArgs(String[] args, String value) {
10245 if (args != null) {
10246 for (String arg : args) {
10247 if (value.equals(arg)) {
10248 return true;
10249 }
10250 }
10251 }
10252 return false;
10253 }
10254
Dianne Hackborn75b03852009-06-12 15:43:26 -070010255 private final int indexOfTokenLocked(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 int count = mHistory.size();
10257
10258 // convert the token to an entry in the history.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010259 int index = -1;
10260 for (int i=count-1; i>=0; i--) {
10261 Object o = mHistory.get(i);
10262 if (o == token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010263 index = i;
10264 break;
10265 }
10266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267
10268 return index;
10269 }
10270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010271 private final void killServicesLocked(ProcessRecord app,
10272 boolean allowRestart) {
10273 // Report disconnected services.
10274 if (false) {
10275 // XXX we are letting the client link to the service for
10276 // death notifications.
10277 if (app.services.size() > 0) {
10278 Iterator it = app.services.iterator();
10279 while (it.hasNext()) {
10280 ServiceRecord r = (ServiceRecord)it.next();
10281 if (r.connections.size() > 0) {
10282 Iterator<ConnectionRecord> jt
10283 = r.connections.values().iterator();
10284 while (jt.hasNext()) {
10285 ConnectionRecord c = jt.next();
10286 if (c.binding.client != app) {
10287 try {
10288 //c.conn.connected(r.className, null);
10289 } catch (Exception e) {
10290 // todo: this should be asynchronous!
Joe Onorato8a9b2202010-02-26 18:56:32 -080010291 Slog.w(TAG, "Exception thrown disconnected servce "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010292 + r.shortName
10293 + " from app " + app.processName, e);
10294 }
10295 }
10296 }
10297 }
10298 }
10299 }
10300 }
10301
10302 // Clean up any connections this application has to other services.
10303 if (app.connections.size() > 0) {
10304 Iterator<ConnectionRecord> it = app.connections.iterator();
10305 while (it.hasNext()) {
10306 ConnectionRecord r = it.next();
10307 removeConnectionLocked(r, app, null);
10308 }
10309 }
10310 app.connections.clear();
10311
10312 if (app.services.size() != 0) {
10313 // Any services running in the application need to be placed
10314 // back in the pending list.
10315 Iterator it = app.services.iterator();
10316 while (it.hasNext()) {
10317 ServiceRecord sr = (ServiceRecord)it.next();
10318 synchronized (sr.stats.getBatteryStats()) {
10319 sr.stats.stopLaunchedLocked();
10320 }
10321 sr.app = null;
10322 sr.executeNesting = 0;
10323 mStoppingServices.remove(sr);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010324
10325 boolean hasClients = sr.bindings.size() > 0;
10326 if (hasClients) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010327 Iterator<IntentBindRecord> bindings
10328 = sr.bindings.values().iterator();
10329 while (bindings.hasNext()) {
10330 IntentBindRecord b = bindings.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010331 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010332 + ": shouldUnbind=" + b.hasBound);
10333 b.binder = null;
10334 b.requested = b.received = b.hasBound = false;
10335 }
10336 }
10337
10338 if (sr.crashCount >= 2) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010339 Slog.w(TAG, "Service crashed " + sr.crashCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010340 + " times, stopping: " + sr);
Doug Zongker2bec3d42009-12-04 12:52:44 -080010341 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 sr.crashCount, sr.shortName, app.pid);
10343 bringDownServiceLocked(sr, true);
10344 } else if (!allowRestart) {
10345 bringDownServiceLocked(sr, true);
10346 } else {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010347 boolean canceled = scheduleServiceRestartLocked(sr, true);
10348
10349 // Should the service remain running? Note that in the
10350 // extreme case of so many attempts to deliver a command
10351 // that it failed, that we also will stop it here.
10352 if (sr.startRequested && (sr.stopIfKilled || canceled)) {
10353 if (sr.pendingStarts.size() == 0) {
10354 sr.startRequested = false;
10355 if (!hasClients) {
10356 // Whoops, no reason to restart!
10357 bringDownServiceLocked(sr, true);
10358 }
10359 }
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361 }
10362 }
10363
10364 if (!allowRestart) {
10365 app.services.clear();
10366 }
10367 }
10368
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010369 // Make sure we have no more records on the stopping list.
10370 int i = mStoppingServices.size();
10371 while (i > 0) {
10372 i--;
10373 ServiceRecord sr = mStoppingServices.get(i);
10374 if (sr.app == app) {
10375 mStoppingServices.remove(i);
10376 }
10377 }
10378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 app.executingServices.clear();
10380 }
10381
10382 private final void removeDyingProviderLocked(ProcessRecord proc,
10383 ContentProviderRecord cpr) {
10384 synchronized (cpr) {
10385 cpr.launchingApp = null;
10386 cpr.notifyAll();
10387 }
10388
10389 mProvidersByClass.remove(cpr.info.name);
10390 String names[] = cpr.info.authority.split(";");
10391 for (int j = 0; j < names.length; j++) {
10392 mProvidersByName.remove(names[j]);
10393 }
10394
10395 Iterator<ProcessRecord> cit = cpr.clients.iterator();
10396 while (cit.hasNext()) {
10397 ProcessRecord capp = cit.next();
10398 if (!capp.persistent && capp.thread != null
10399 && capp.pid != 0
10400 && capp.pid != MY_PID) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010401 Slog.i(TAG, "Killing app " + capp.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010402 + " (pid " + capp.pid
10403 + ") because provider " + cpr.info.name
10404 + " is in dying process " + proc.processName);
10405 Process.killProcess(capp.pid);
10406 }
10407 }
10408
10409 mLaunchingProviders.remove(cpr);
10410 }
10411
10412 /**
10413 * Main code for cleaning up a process when it has gone away. This is
10414 * called both as a result of the process dying, or directly when stopping
10415 * a process when running in single process mode.
10416 */
10417 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
10418 boolean restarting, int index) {
10419 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010420 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 }
10422
Dianne Hackborn36124872009-10-08 16:22:03 -070010423 mProcessesToGc.remove(app);
10424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010425 // Dismiss any open dialogs.
10426 if (app.crashDialog != null) {
10427 app.crashDialog.dismiss();
10428 app.crashDialog = null;
10429 }
10430 if (app.anrDialog != null) {
10431 app.anrDialog.dismiss();
10432 app.anrDialog = null;
10433 }
10434 if (app.waitDialog != null) {
10435 app.waitDialog.dismiss();
10436 app.waitDialog = null;
10437 }
10438
10439 app.crashing = false;
10440 app.notResponding = false;
10441
10442 app.resetPackageList();
10443 app.thread = null;
10444 app.forcingToForeground = null;
10445 app.foregroundServices = false;
10446
10447 killServicesLocked(app, true);
10448
10449 boolean restart = false;
10450
10451 int NL = mLaunchingProviders.size();
10452
10453 // Remove published content providers.
10454 if (!app.pubProviders.isEmpty()) {
10455 Iterator it = app.pubProviders.values().iterator();
10456 while (it.hasNext()) {
10457 ContentProviderRecord cpr = (ContentProviderRecord)it.next();
10458 cpr.provider = null;
10459 cpr.app = null;
10460
10461 // See if someone is waiting for this provider... in which
10462 // case we don't remove it, but just let it restart.
10463 int i = 0;
10464 if (!app.bad) {
10465 for (; i<NL; i++) {
10466 if (mLaunchingProviders.get(i) == cpr) {
10467 restart = true;
10468 break;
10469 }
10470 }
10471 } else {
10472 i = NL;
10473 }
10474
10475 if (i >= NL) {
10476 removeDyingProviderLocked(app, cpr);
10477 NL = mLaunchingProviders.size();
10478 }
10479 }
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 Hackborn0c3154d2009-10-06 17:18:05 -070010490 Iterator it = app.conProviders.keySet().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491 while (it.hasNext()) {
10492 ContentProviderRecord cpr = (ContentProviderRecord)it.next();
10493 cpr.clients.remove(app);
10494 }
10495 app.conProviders.clear();
10496 }
10497
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010498 // At this point there may be remaining entries in mLaunchingProviders
10499 // where we were the only one waiting, so they are no longer of use.
10500 // Look for these and clean up if found.
10501 // XXX Commented out for now. Trying to figure out a way to reproduce
10502 // the actual situation to identify what is actually going on.
10503 if (false) {
10504 for (int i=0; i<NL; i++) {
10505 ContentProviderRecord cpr = (ContentProviderRecord)
10506 mLaunchingProviders.get(i);
10507 if (cpr.clients.size() <= 0 && cpr.externals <= 0) {
10508 synchronized (cpr) {
10509 cpr.launchingApp = null;
10510 cpr.notifyAll();
10511 }
10512 }
10513 }
10514 }
10515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 skipCurrentReceiverLocked(app);
10517
10518 // Unregister any receivers.
10519 if (app.receivers.size() > 0) {
10520 Iterator<ReceiverList> it = app.receivers.iterator();
10521 while (it.hasNext()) {
10522 removeReceiverLocked(it.next());
10523 }
10524 app.receivers.clear();
10525 }
10526
Christopher Tate181fafa2009-05-14 11:12:14 -070010527 // If the app is undergoing backup, tell the backup manager about it
10528 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010529 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -070010530 try {
10531 IBackupManager bm = IBackupManager.Stub.asInterface(
10532 ServiceManager.getService(Context.BACKUP_SERVICE));
10533 bm.agentDisconnected(app.info.packageName);
10534 } catch (RemoteException e) {
10535 // can't happen; backup manager is local
10536 }
10537 }
10538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010539 // If the caller is restarting this app, then leave it in its
10540 // current lists and let the caller take care of it.
10541 if (restarting) {
10542 return;
10543 }
10544
10545 if (!app.persistent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010546 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 "Removing non-persistent process during cleanup: " + app);
10548 mProcessNames.remove(app.processName, app.info.uid);
10549 } else if (!app.removed) {
10550 // This app is persistent, so we need to keep its record around.
10551 // If it is not already on the pending app list, add it there
10552 // and start a new process for it.
10553 app.thread = null;
10554 app.forcingToForeground = null;
10555 app.foregroundServices = false;
10556 if (mPersistentStartingProcesses.indexOf(app) < 0) {
10557 mPersistentStartingProcesses.add(app);
10558 restart = true;
10559 }
10560 }
10561 mProcessesOnHold.remove(app);
10562
The Android Open Source Project4df24232009-03-05 14:34:35 -080010563 if (app == mHomeProcess) {
10564 mHomeProcess = null;
10565 }
10566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010567 if (restart) {
10568 // We have components that still need to be running in the
10569 // process, so re-launch it.
10570 mProcessNames.put(app.processName, app.info.uid, app);
10571 startProcessLocked(app, "restart", app.processName);
10572 } else if (app.pid > 0 && app.pid != MY_PID) {
10573 // Goodbye!
10574 synchronized (mPidsSelfLocked) {
10575 mPidsSelfLocked.remove(app.pid);
10576 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
10577 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -070010578 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 }
10580 }
10581
Dianne Hackbornf670ef72009-11-16 13:59:16 -080010582 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
10583 // Look through the content providers we are waiting to have launched,
10584 // and if any run in this process then either schedule a restart of
10585 // the process or kill the client waiting for it if this process has
10586 // gone bad.
10587 int NL = mLaunchingProviders.size();
10588 boolean restart = false;
10589 for (int i=0; i<NL; i++) {
10590 ContentProviderRecord cpr = (ContentProviderRecord)
10591 mLaunchingProviders.get(i);
10592 if (cpr.launchingApp == app) {
10593 if (!alwaysBad && !app.bad) {
10594 restart = true;
10595 } else {
10596 removeDyingProviderLocked(app, cpr);
10597 NL = mLaunchingProviders.size();
10598 }
10599 }
10600 }
10601 return restart;
10602 }
10603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 // =========================================================
10605 // SERVICES
10606 // =========================================================
10607
10608 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) {
10609 ActivityManager.RunningServiceInfo info =
10610 new ActivityManager.RunningServiceInfo();
10611 info.service = r.name;
10612 if (r.app != null) {
10613 info.pid = r.app.pid;
10614 }
Dianne Hackborn3025ef32009-08-31 21:31:47 -070010615 info.uid = r.appInfo.uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616 info.process = r.processName;
10617 info.foreground = r.isForeground;
10618 info.activeSince = r.createTime;
10619 info.started = r.startRequested;
10620 info.clientCount = r.connections.size();
10621 info.crashCount = r.crashCount;
10622 info.lastActivityTime = r.lastActivity;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070010623 if (r.isForeground) {
10624 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND;
10625 }
10626 if (r.startRequested) {
10627 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED;
10628 }
Dan Egnor42471dd2010-01-07 17:25:22 -080010629 if (r.app != null && r.app.pid == MY_PID) {
Dianne Hackborn3025ef32009-08-31 21:31:47 -070010630 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS;
10631 }
10632 if (r.app != null && r.app.persistent) {
10633 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS;
10634 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010635 for (ConnectionRecord conn : r.connections.values()) {
10636 if (conn.clientLabel != 0) {
10637 info.clientPackage = conn.binding.client.info.packageName;
10638 info.clientLabel = conn.clientLabel;
10639 break;
10640 }
10641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 return info;
10643 }
10644
10645 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
10646 int flags) {
10647 synchronized (this) {
10648 ArrayList<ActivityManager.RunningServiceInfo> res
10649 = new ArrayList<ActivityManager.RunningServiceInfo>();
10650
10651 if (mServices.size() > 0) {
10652 Iterator<ServiceRecord> it = mServices.values().iterator();
10653 while (it.hasNext() && res.size() < maxNum) {
10654 res.add(makeRunningServiceInfoLocked(it.next()));
10655 }
10656 }
10657
10658 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) {
10659 ServiceRecord r = mRestartingServices.get(i);
10660 ActivityManager.RunningServiceInfo info =
10661 makeRunningServiceInfoLocked(r);
10662 info.restarting = r.nextRestartTime;
10663 res.add(info);
10664 }
10665
10666 return res;
10667 }
10668 }
10669
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010670 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
10671 synchronized (this) {
10672 ServiceRecord r = mServices.get(name);
10673 if (r != null) {
10674 for (ConnectionRecord conn : r.connections.values()) {
10675 if (conn.clientIntent != null) {
10676 return conn.clientIntent;
10677 }
10678 }
10679 }
10680 }
10681 return null;
10682 }
10683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010684 private final ServiceRecord findServiceLocked(ComponentName name,
10685 IBinder token) {
10686 ServiceRecord r = mServices.get(name);
10687 return r == token ? r : null;
10688 }
10689
10690 private final class ServiceLookupResult {
10691 final ServiceRecord record;
10692 final String permission;
10693
10694 ServiceLookupResult(ServiceRecord _record, String _permission) {
10695 record = _record;
10696 permission = _permission;
10697 }
10698 };
10699
10700 private ServiceLookupResult findServiceLocked(Intent service,
10701 String resolvedType) {
10702 ServiceRecord r = null;
10703 if (service.getComponent() != null) {
10704 r = mServices.get(service.getComponent());
10705 }
10706 if (r == null) {
10707 Intent.FilterComparison filter = new Intent.FilterComparison(service);
10708 r = mServicesByIntent.get(filter);
10709 }
10710
10711 if (r == null) {
10712 try {
10713 ResolveInfo rInfo =
10714 ActivityThread.getPackageManager().resolveService(
10715 service, resolvedType, 0);
10716 ServiceInfo sInfo =
10717 rInfo != null ? rInfo.serviceInfo : null;
10718 if (sInfo == null) {
10719 return null;
10720 }
10721
10722 ComponentName name = new ComponentName(
10723 sInfo.applicationInfo.packageName, sInfo.name);
10724 r = mServices.get(name);
10725 } catch (RemoteException ex) {
10726 // pm is in same process, this will never happen.
10727 }
10728 }
10729 if (r != null) {
10730 int callingPid = Binder.getCallingPid();
10731 int callingUid = Binder.getCallingUid();
10732 if (checkComponentPermission(r.permission,
10733 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
10734 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010735 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 + " from pid=" + callingPid
10737 + ", uid=" + callingUid
10738 + " requires " + r.permission);
10739 return new ServiceLookupResult(null, r.permission);
10740 }
10741 return new ServiceLookupResult(r, null);
10742 }
10743 return null;
10744 }
10745
10746 private class ServiceRestarter implements Runnable {
10747 private ServiceRecord mService;
10748
10749 void setService(ServiceRecord service) {
10750 mService = service;
10751 }
10752
10753 public void run() {
10754 synchronized(ActivityManagerService.this) {
10755 performServiceRestartLocked(mService);
10756 }
10757 }
10758 }
10759
10760 private ServiceLookupResult retrieveServiceLocked(Intent service,
10761 String resolvedType, int callingPid, int callingUid) {
10762 ServiceRecord r = null;
10763 if (service.getComponent() != null) {
10764 r = mServices.get(service.getComponent());
10765 }
10766 Intent.FilterComparison filter = new Intent.FilterComparison(service);
10767 r = mServicesByIntent.get(filter);
10768 if (r == null) {
10769 try {
10770 ResolveInfo rInfo =
10771 ActivityThread.getPackageManager().resolveService(
Dianne Hackborn1655be42009-05-08 14:29:01 -070010772 service, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010773 ServiceInfo sInfo =
10774 rInfo != null ? rInfo.serviceInfo : null;
10775 if (sInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010776 Slog.w(TAG, "Unable to start service " + service +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010777 ": not found");
10778 return null;
10779 }
10780
10781 ComponentName name = new ComponentName(
10782 sInfo.applicationInfo.packageName, sInfo.name);
10783 r = mServices.get(name);
10784 if (r == null) {
10785 filter = new Intent.FilterComparison(service.cloneFilter());
10786 ServiceRestarter res = new ServiceRestarter();
10787 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
10788 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
10789 synchronized (stats) {
10790 ss = stats.getServiceStatsLocked(
10791 sInfo.applicationInfo.uid, sInfo.packageName,
10792 sInfo.name);
10793 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -080010794 r = new ServiceRecord(this, ss, name, filter, sInfo, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 res.setService(r);
10796 mServices.put(name, r);
10797 mServicesByIntent.put(filter, r);
10798
10799 // Make sure this component isn't in the pending list.
10800 int N = mPendingServices.size();
10801 for (int i=0; i<N; i++) {
10802 ServiceRecord pr = mPendingServices.get(i);
10803 if (pr.name.equals(name)) {
10804 mPendingServices.remove(i);
10805 i--;
10806 N--;
10807 }
10808 }
10809 }
10810 } catch (RemoteException ex) {
10811 // pm is in same process, this will never happen.
10812 }
10813 }
10814 if (r != null) {
10815 if (checkComponentPermission(r.permission,
10816 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
10817 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010818 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010819 + " from pid=" + Binder.getCallingPid()
10820 + ", uid=" + Binder.getCallingUid()
10821 + " requires " + r.permission);
10822 return new ServiceLookupResult(null, r.permission);
10823 }
10824 return new ServiceLookupResult(r, null);
10825 }
10826 return null;
10827 }
10828
10829 private final void bumpServiceExecutingLocked(ServiceRecord r) {
10830 long now = SystemClock.uptimeMillis();
10831 if (r.executeNesting == 0 && r.app != null) {
10832 if (r.app.executingServices.size() == 0) {
10833 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
10834 msg.obj = r.app;
10835 mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT);
10836 }
10837 r.app.executingServices.add(r);
10838 }
10839 r.executeNesting++;
10840 r.executingStart = now;
10841 }
10842
10843 private final void sendServiceArgsLocked(ServiceRecord r,
10844 boolean oomAdjusted) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010845 final int N = r.pendingStarts.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010846 if (N == 0) {
10847 return;
10848 }
10849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 int i = 0;
10851 while (i < N) {
10852 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010853 ServiceRecord.StartItem si = r.pendingStarts.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010854 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to service: "
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010855 + r.name + " " + r.intent + " args=" + si.intent);
Dianne Hackbornfed534e2009-09-23 00:42:12 -070010856 if (si.intent == null && N > 1) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010857 // If somehow we got a dummy start at the front, then
10858 // just drop it here.
10859 i++;
10860 continue;
10861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 bumpServiceExecutingLocked(r);
10863 if (!oomAdjusted) {
10864 oomAdjusted = true;
10865 updateOomAdjLocked(r.app);
10866 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010867 int flags = 0;
10868 if (si.deliveryCount > 0) {
10869 flags |= Service.START_FLAG_RETRY;
10870 }
10871 if (si.doneExecutingCount > 0) {
10872 flags |= Service.START_FLAG_REDELIVERY;
10873 }
10874 r.app.thread.scheduleServiceArgs(r, si.id, flags, si.intent);
10875 si.deliveredTime = SystemClock.uptimeMillis();
10876 r.deliveredStarts.add(si);
10877 si.deliveryCount++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 i++;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010879 } catch (RemoteException e) {
10880 // Remote process gone... we'll let the normal cleanup take
10881 // care of this.
10882 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010884 Slog.w(TAG, "Unexpected exception", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010885 break;
10886 }
10887 }
10888 if (i == N) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010889 r.pendingStarts.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010890 } else {
10891 while (i > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010892 i--;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010893 r.pendingStarts.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 }
10895 }
10896 }
10897
10898 private final boolean requestServiceBindingLocked(ServiceRecord r,
10899 IntentBindRecord i, boolean rebind) {
10900 if (r.app == null || r.app.thread == null) {
10901 // If service is not currently running, can't yet bind.
10902 return false;
10903 }
10904 if ((!i.requested || rebind) && i.apps.size() > 0) {
10905 try {
10906 bumpServiceExecutingLocked(r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010907 if (DEBUG_SERVICE) Slog.v(TAG, "Connecting binding " + i
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 + ": shouldUnbind=" + i.hasBound);
10909 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind);
10910 if (!rebind) {
10911 i.requested = true;
10912 }
10913 i.hasBound = true;
10914 i.doRebind = false;
10915 } catch (RemoteException e) {
10916 return false;
10917 }
10918 }
10919 return true;
10920 }
10921
10922 private final void requestServiceBindingsLocked(ServiceRecord r) {
10923 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator();
10924 while (bindings.hasNext()) {
10925 IntentBindRecord i = bindings.next();
10926 if (!requestServiceBindingLocked(r, i, false)) {
10927 break;
10928 }
10929 }
10930 }
10931
10932 private final void realStartServiceLocked(ServiceRecord r,
10933 ProcessRecord app) throws RemoteException {
10934 if (app.thread == null) {
10935 throw new RemoteException();
10936 }
10937
10938 r.app = app;
The Android Open Source Project10592532009-03-18 17:39:46 -070010939 r.restartTime = r.lastActivity = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010940
10941 app.services.add(r);
10942 bumpServiceExecutingLocked(r);
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010943 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010944
10945 boolean created = false;
10946 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010947 if (DEBUG_SERVICE) Slog.v(TAG, "Scheduling start service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010948 + r.name + " " + r.intent);
Dianne Hackborna33e3f72009-09-29 17:28:24 -070010949 mStringBuilder.setLength(0);
10950 r.intent.getIntent().toShortString(mStringBuilder, false, true);
Doug Zongker2bec3d42009-12-04 12:52:44 -080010951 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 System.identityHashCode(r), r.shortName,
Dianne Hackborna33e3f72009-09-29 17:28:24 -070010953 mStringBuilder.toString(), r.app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010954 synchronized (r.stats.getBatteryStats()) {
10955 r.stats.startLaunchedLocked();
10956 }
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -070010957 ensurePackageDexOpt(r.serviceInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 app.thread.scheduleCreateService(r, r.serviceInfo);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070010959 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 created = true;
10961 } finally {
10962 if (!created) {
10963 app.services.remove(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010964 scheduleServiceRestartLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 }
10966 }
10967
10968 requestServiceBindingsLocked(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010969
10970 // If the service is in the started state, and there are no
10971 // pending arguments, then fake up one so its onStartCommand() will
10972 // be called.
10973 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {
10974 r.lastStartId++;
10975 if (r.lastStartId < 1) {
10976 r.lastStartId = 1;
10977 }
10978 r.pendingStarts.add(new ServiceRecord.StartItem(r.lastStartId, null));
10979 }
10980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010981 sendServiceArgsLocked(r, true);
10982 }
10983
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010984 private final boolean scheduleServiceRestartLocked(ServiceRecord r,
10985 boolean allowCancel) {
10986 boolean canceled = false;
10987
Dianne Hackbornfd12af42009-08-27 00:44:33 -070010988 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010989 long minDuration = SERVICE_RESTART_DURATION;
Dianne Hackborn6ccd2af2009-08-27 12:26:44 -070010990 long resetTime = SERVICE_RESET_RUN_DURATION;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010991
10992 // Any delivered but not yet finished starts should be put back
10993 // on the pending list.
10994 final int N = r.deliveredStarts.size();
10995 if (N > 0) {
10996 for (int i=N-1; i>=0; i--) {
10997 ServiceRecord.StartItem si = r.deliveredStarts.get(i);
10998 if (si.intent == null) {
10999 // We'll generate this again if needed.
11000 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT
11001 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) {
11002 r.pendingStarts.add(0, si);
11003 long dur = SystemClock.uptimeMillis() - si.deliveredTime;
11004 dur *= 2;
11005 if (minDuration < dur) minDuration = dur;
11006 if (resetTime < dur) resetTime = dur;
11007 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011008 Slog.w(TAG, "Canceling start item " + si.intent + " in service "
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011009 + r.name);
11010 canceled = true;
11011 }
11012 }
11013 r.deliveredStarts.clear();
11014 }
11015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 r.totalRestartCount++;
11017 if (r.restartDelay == 0) {
11018 r.restartCount++;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011019 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011020 } else {
11021 // If it has been a "reasonably long time" since the service
11022 // was started, then reset our restart duration back to
11023 // the beginning, so we don't infinitely increase the duration
11024 // on a service that just occasionally gets killed (which is
11025 // a normal case, due to process being killed to reclaim memory).
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011026 if (now > (r.restartTime+resetTime)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011027 r.restartCount = 1;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011028 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011029 } else {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011030 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011031 if (r.restartDelay < minDuration) {
11032 r.restartDelay = minDuration;
11033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011034 }
11035 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011036
11037 r.nextRestartTime = now + r.restartDelay;
11038
11039 // Make sure that we don't end up restarting a bunch of services
11040 // all at the same time.
11041 boolean repeat;
11042 do {
11043 repeat = false;
11044 for (int i=mRestartingServices.size()-1; i>=0; i--) {
11045 ServiceRecord r2 = mRestartingServices.get(i);
11046 if (r2 != r && r.nextRestartTime
11047 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN)
11048 && r.nextRestartTime
11049 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) {
11050 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN;
11051 r.restartDelay = r.nextRestartTime - now;
11052 repeat = true;
11053 break;
11054 }
11055 }
11056 } while (repeat);
11057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011058 if (!mRestartingServices.contains(r)) {
11059 mRestartingServices.add(r);
11060 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011061
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011062 r.cancelNotification();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 mHandler.removeCallbacks(r.restarter);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011065 mHandler.postAtTime(r.restarter, r.nextRestartTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011067 Slog.w(TAG, "Scheduling restart of crashed service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068 + r.shortName + " in " + r.restartDelay + "ms");
Doug Zongker2bec3d42009-12-04 12:52:44 -080011069 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 r.shortName, r.restartDelay);
11071
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011072 return canceled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011073 }
11074
11075 final void performServiceRestartLocked(ServiceRecord r) {
11076 if (!mRestartingServices.contains(r)) {
11077 return;
11078 }
11079 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true);
11080 }
11081
11082 private final boolean unscheduleServiceRestartLocked(ServiceRecord r) {
11083 if (r.restartDelay == 0) {
11084 return false;
11085 }
11086 r.resetRestartCounter();
11087 mRestartingServices.remove(r);
11088 mHandler.removeCallbacks(r.restarter);
11089 return true;
11090 }
11091
11092 private final boolean bringUpServiceLocked(ServiceRecord r,
11093 int intentFlags, boolean whileRestarting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011094 //Slog.i(TAG, "Bring up service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011095 //r.dump(" ");
11096
Dianne Hackborn36124872009-10-08 16:22:03 -070011097 if (r.app != null && r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011098 sendServiceArgsLocked(r, false);
11099 return true;
11100 }
11101
11102 if (!whileRestarting && r.restartDelay > 0) {
11103 // If waiting for a restart, then do nothing.
11104 return true;
11105 }
11106
Joe Onorato8a9b2202010-02-26 18:56:32 -080011107 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 + " " + r.intent);
11109
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011110 // We are now bringing the service up, so no longer in the
11111 // restarting state.
11112 mRestartingServices.remove(r);
11113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011114 final String appName = r.processName;
11115 ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid);
11116 if (app != null && app.thread != null) {
11117 try {
11118 realStartServiceLocked(r, app);
11119 return true;
11120 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011121 Slog.w(TAG, "Exception when starting service " + r.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011122 }
11123
11124 // If a dead object exception was thrown -- fall through to
11125 // restart the application.
11126 }
11127
Dianne Hackborn36124872009-10-08 16:22:03 -070011128 // Not running -- get it started, and enqueue this service record
11129 // to be executed when the app comes up.
11130 if (startProcessLocked(appName, r.appInfo, true, intentFlags,
11131 "service", r.name, false) == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011132 Slog.w(TAG, "Unable to launch app "
Dianne Hackborn36124872009-10-08 16:22:03 -070011133 + r.appInfo.packageName + "/"
11134 + r.appInfo.uid + " for service "
11135 + r.intent.getIntent() + ": process is bad");
11136 bringDownServiceLocked(r, true);
11137 return false;
11138 }
11139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 if (!mPendingServices.contains(r)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011141 mPendingServices.add(r);
11142 }
Dianne Hackborn36124872009-10-08 16:22:03 -070011143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 return true;
11145 }
11146
11147 private final void bringDownServiceLocked(ServiceRecord r, boolean force) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011148 //Slog.i(TAG, "Bring down service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011149 //r.dump(" ");
11150
11151 // Does it still need to run?
11152 if (!force && r.startRequested) {
11153 return;
11154 }
11155 if (r.connections.size() > 0) {
11156 if (!force) {
11157 // XXX should probably keep a count of the number of auto-create
11158 // connections directly in the service.
11159 Iterator<ConnectionRecord> it = r.connections.values().iterator();
11160 while (it.hasNext()) {
11161 ConnectionRecord cr = it.next();
11162 if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) {
11163 return;
11164 }
11165 }
11166 }
11167
11168 // Report to all of the connections that the service is no longer
11169 // available.
11170 Iterator<ConnectionRecord> it = r.connections.values().iterator();
11171 while (it.hasNext()) {
11172 ConnectionRecord c = it.next();
11173 try {
11174 // todo: shouldn't be a synchronous call!
11175 c.conn.connected(r.name, null);
11176 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011177 Slog.w(TAG, "Failure disconnecting service " + r.name +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011178 " to connection " + c.conn.asBinder() +
11179 " (in " + c.binding.client.processName + ")", e);
11180 }
11181 }
11182 }
11183
11184 // Tell the service that it has been unbound.
11185 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) {
11186 Iterator<IntentBindRecord> it = r.bindings.values().iterator();
11187 while (it.hasNext()) {
11188 IntentBindRecord ibr = it.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011189 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011190 + ": hasBound=" + ibr.hasBound);
11191 if (r.app != null && r.app.thread != null && ibr.hasBound) {
11192 try {
11193 bumpServiceExecutingLocked(r);
11194 updateOomAdjLocked(r.app);
11195 ibr.hasBound = false;
11196 r.app.thread.scheduleUnbindService(r,
11197 ibr.intent.getIntent());
11198 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011199 Slog.w(TAG, "Exception when unbinding service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011200 + r.shortName, e);
11201 serviceDoneExecutingLocked(r, true);
11202 }
11203 }
11204 }
11205 }
11206
Joe Onorato8a9b2202010-02-26 18:56:32 -080011207 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208 + " " + r.intent);
Doug Zongker2bec3d42009-12-04 12:52:44 -080011209 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011210 System.identityHashCode(r), r.shortName,
11211 (r.app != null) ? r.app.pid : -1);
11212
11213 mServices.remove(r.name);
11214 mServicesByIntent.remove(r.intent);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011215 if (localLOGV) Slog.v(TAG, "BRING DOWN SERVICE: " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 r.totalRestartCount = 0;
11217 unscheduleServiceRestartLocked(r);
11218
11219 // Also make sure it is not on the pending list.
11220 int N = mPendingServices.size();
11221 for (int i=0; i<N; i++) {
11222 if (mPendingServices.get(i) == r) {
11223 mPendingServices.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011224 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011225 TAG, "Removed pending service: " + r.shortName);
11226 i--;
11227 N--;
11228 }
11229 }
11230
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011231 r.cancelNotification();
11232 r.isForeground = false;
11233 r.foregroundId = 0;
11234 r.foregroundNoti = null;
11235
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011236 // Clear start entries.
11237 r.deliveredStarts.clear();
11238 r.pendingStarts.clear();
11239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011240 if (r.app != null) {
11241 synchronized (r.stats.getBatteryStats()) {
11242 r.stats.stopLaunchedLocked();
11243 }
11244 r.app.services.remove(r);
11245 if (r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011247 if (DEBUG_SERVICE) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -070011248 "Stopping service: " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011249 bumpServiceExecutingLocked(r);
11250 mStoppingServices.add(r);
11251 updateOomAdjLocked(r.app);
11252 r.app.thread.scheduleStopService(r);
11253 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011254 Slog.w(TAG, "Exception when stopping service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 + r.shortName, e);
11256 serviceDoneExecutingLocked(r, true);
11257 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011258 updateServiceForegroundLocked(r.app, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011260 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 TAG, "Removed service that has no process: " + r.shortName);
11262 }
11263 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011264 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 TAG, "Removed service that is not running: " + r.shortName);
11266 }
11267 }
11268
11269 ComponentName startServiceLocked(IApplicationThread caller,
11270 Intent service, String resolvedType,
11271 int callingPid, int callingUid) {
11272 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011273 if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 + " type=" + resolvedType + " args=" + service.getExtras());
11275
11276 if (caller != null) {
11277 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11278 if (callerApp == null) {
11279 throw new SecurityException(
11280 "Unable to find app for caller " + caller
11281 + " (pid=" + Binder.getCallingPid()
11282 + ") when starting service " + service);
11283 }
11284 }
11285
11286 ServiceLookupResult res =
11287 retrieveServiceLocked(service, resolvedType,
11288 callingPid, callingUid);
11289 if (res == null) {
11290 return null;
11291 }
11292 if (res.record == null) {
11293 return new ComponentName("!", res.permission != null
11294 ? res.permission : "private to package");
11295 }
11296 ServiceRecord r = res.record;
11297 if (unscheduleServiceRestartLocked(r)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011298 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011299 + r.shortName);
11300 }
11301 r.startRequested = true;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011302 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 r.lastStartId++;
11304 if (r.lastStartId < 1) {
11305 r.lastStartId = 1;
11306 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011307 r.pendingStarts.add(new ServiceRecord.StartItem(r.lastStartId, service));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011308 r.lastActivity = SystemClock.uptimeMillis();
11309 synchronized (r.stats.getBatteryStats()) {
11310 r.stats.startRunningLocked();
11311 }
11312 if (!bringUpServiceLocked(r, service.getFlags(), false)) {
11313 return new ComponentName("!", "Service process is bad");
11314 }
11315 return r.name;
11316 }
11317 }
11318
11319 public ComponentName startService(IApplicationThread caller, Intent service,
11320 String resolvedType) {
11321 // Refuse possible leaked file descriptors
11322 if (service != null && service.hasFileDescriptors() == true) {
11323 throw new IllegalArgumentException("File descriptors passed in Intent");
11324 }
11325
11326 synchronized(this) {
11327 final int callingPid = Binder.getCallingPid();
11328 final int callingUid = Binder.getCallingUid();
11329 final long origId = Binder.clearCallingIdentity();
11330 ComponentName res = startServiceLocked(caller, service,
11331 resolvedType, callingPid, callingUid);
11332 Binder.restoreCallingIdentity(origId);
11333 return res;
11334 }
11335 }
11336
11337 ComponentName startServiceInPackage(int uid,
11338 Intent service, String resolvedType) {
11339 synchronized(this) {
11340 final long origId = Binder.clearCallingIdentity();
11341 ComponentName res = startServiceLocked(null, service,
11342 resolvedType, -1, uid);
11343 Binder.restoreCallingIdentity(origId);
11344 return res;
11345 }
11346 }
11347
11348 public int stopService(IApplicationThread caller, Intent service,
11349 String resolvedType) {
11350 // Refuse possible leaked file descriptors
11351 if (service != null && service.hasFileDescriptors() == true) {
11352 throw new IllegalArgumentException("File descriptors passed in Intent");
11353 }
11354
11355 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011356 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011357 + " type=" + resolvedType);
11358
11359 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11360 if (caller != null && callerApp == null) {
11361 throw new SecurityException(
11362 "Unable to find app for caller " + caller
11363 + " (pid=" + Binder.getCallingPid()
11364 + ") when stopping service " + service);
11365 }
11366
11367 // If this service is active, make sure it is stopped.
11368 ServiceLookupResult r = findServiceLocked(service, resolvedType);
11369 if (r != null) {
11370 if (r.record != null) {
11371 synchronized (r.record.stats.getBatteryStats()) {
11372 r.record.stats.stopRunningLocked();
11373 }
11374 r.record.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011375 r.record.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011376 final long origId = Binder.clearCallingIdentity();
11377 bringDownServiceLocked(r.record, false);
11378 Binder.restoreCallingIdentity(origId);
11379 return 1;
11380 }
11381 return -1;
11382 }
11383 }
11384
11385 return 0;
11386 }
11387
11388 public IBinder peekService(Intent service, String resolvedType) {
11389 // Refuse possible leaked file descriptors
11390 if (service != null && service.hasFileDescriptors() == true) {
11391 throw new IllegalArgumentException("File descriptors passed in Intent");
11392 }
11393
11394 IBinder ret = null;
11395
11396 synchronized(this) {
11397 ServiceLookupResult r = findServiceLocked(service, resolvedType);
11398
11399 if (r != null) {
11400 // r.record is null if findServiceLocked() failed the caller permission check
11401 if (r.record == null) {
11402 throw new SecurityException(
11403 "Permission Denial: Accessing service " + r.record.name
11404 + " from pid=" + Binder.getCallingPid()
11405 + ", uid=" + Binder.getCallingUid()
11406 + " requires " + r.permission);
11407 }
11408 IntentBindRecord ib = r.record.bindings.get(r.record.intent);
11409 if (ib != null) {
11410 ret = ib.binder;
11411 }
11412 }
11413 }
11414
11415 return ret;
11416 }
11417
11418 public boolean stopServiceToken(ComponentName className, IBinder token,
11419 int startId) {
11420 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011421 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011422 + " " + token + " startId=" + startId);
11423 ServiceRecord r = findServiceLocked(className, token);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011424 if (r != null) {
11425 if (startId >= 0) {
11426 // Asked to only stop if done with all work. Note that
11427 // to avoid leaks, we will take this as dropping all
11428 // start items up to and including this one.
11429 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
11430 if (si != null) {
11431 while (r.deliveredStarts.size() > 0) {
11432 if (r.deliveredStarts.remove(0) == si) {
11433 break;
11434 }
11435 }
11436 }
11437
11438 if (r.lastStartId != startId) {
11439 return false;
11440 }
11441
11442 if (r.deliveredStarts.size() > 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011443 Slog.w(TAG, "stopServiceToken startId " + startId
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011444 + " is last, but have " + r.deliveredStarts.size()
11445 + " remaining args");
11446 }
11447 }
11448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 synchronized (r.stats.getBatteryStats()) {
11450 r.stats.stopRunningLocked();
11451 r.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011452 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011453 }
11454 final long origId = Binder.clearCallingIdentity();
11455 bringDownServiceLocked(r, false);
11456 Binder.restoreCallingIdentity(origId);
11457 return true;
11458 }
11459 }
11460 return false;
11461 }
11462
11463 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011464 int id, Notification notification, boolean removeNotification) {
11465 final long origId = Binder.clearCallingIdentity();
11466 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011467 synchronized(this) {
11468 ServiceRecord r = findServiceLocked(className, token);
11469 if (r != null) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011470 if (id != 0) {
11471 if (notification == null) {
11472 throw new IllegalArgumentException("null notification");
11473 }
11474 if (r.foregroundId != id) {
11475 r.cancelNotification();
11476 r.foregroundId = id;
11477 }
11478 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
11479 r.foregroundNoti = notification;
11480 r.isForeground = true;
11481 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011482 if (r.app != null) {
11483 updateServiceForegroundLocked(r.app, true);
11484 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011485 } else {
11486 if (r.isForeground) {
11487 r.isForeground = false;
11488 if (r.app != null) {
11489 updateServiceForegroundLocked(r.app, true);
11490 }
11491 }
11492 if (removeNotification) {
11493 r.cancelNotification();
11494 r.foregroundId = 0;
11495 r.foregroundNoti = null;
11496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011497 }
11498 }
11499 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070011500 } finally {
11501 Binder.restoreCallingIdentity(origId);
11502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011503 }
11504
11505 public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) {
11506 boolean anyForeground = false;
11507 for (ServiceRecord sr : (HashSet<ServiceRecord>)proc.services) {
11508 if (sr.isForeground) {
11509 anyForeground = true;
11510 break;
11511 }
11512 }
11513 if (anyForeground != proc.foregroundServices) {
11514 proc.foregroundServices = anyForeground;
11515 if (oomAdj) {
11516 updateOomAdjLocked();
11517 }
11518 }
11519 }
11520
11521 public int bindService(IApplicationThread caller, IBinder token,
11522 Intent service, String resolvedType,
11523 IServiceConnection connection, int flags) {
11524 // Refuse possible leaked file descriptors
11525 if (service != null && service.hasFileDescriptors() == true) {
11526 throw new IllegalArgumentException("File descriptors passed in Intent");
11527 }
11528
11529 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011530 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011531 + " type=" + resolvedType + " conn=" + connection.asBinder()
11532 + " flags=0x" + Integer.toHexString(flags));
11533 final ProcessRecord callerApp = getRecordForAppLocked(caller);
11534 if (callerApp == null) {
11535 throw new SecurityException(
11536 "Unable to find app for caller " + caller
11537 + " (pid=" + Binder.getCallingPid()
11538 + ") when binding service " + service);
11539 }
11540
11541 HistoryRecord activity = null;
11542 if (token != null) {
Dianne Hackborn75b03852009-06-12 15:43:26 -070011543 int aindex = indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544 if (aindex < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011545 Slog.w(TAG, "Binding with unknown activity: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011546 return 0;
11547 }
11548 activity = (HistoryRecord)mHistory.get(aindex);
11549 }
11550
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011551 int clientLabel = 0;
11552 PendingIntent clientIntent = null;
11553
11554 if (callerApp.info.uid == Process.SYSTEM_UID) {
11555 // Hacky kind of thing -- allow system stuff to tell us
11556 // what they are, so we can report this elsewhere for
11557 // others to know why certain services are running.
11558 try {
11559 clientIntent = (PendingIntent)service.getParcelableExtra(
11560 Intent.EXTRA_CLIENT_INTENT);
11561 } catch (RuntimeException e) {
11562 }
11563 if (clientIntent != null) {
11564 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0);
11565 if (clientLabel != 0) {
11566 // There are no useful extras in the intent, trash them.
11567 // System code calling with this stuff just needs to know
11568 // this will happen.
11569 service = service.cloneFilter();
11570 }
11571 }
11572 }
11573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011574 ServiceLookupResult res =
11575 retrieveServiceLocked(service, resolvedType,
11576 Binder.getCallingPid(), Binder.getCallingUid());
11577 if (res == null) {
11578 return 0;
11579 }
11580 if (res.record == null) {
11581 return -1;
11582 }
11583 ServiceRecord s = res.record;
11584
11585 final long origId = Binder.clearCallingIdentity();
11586
11587 if (unscheduleServiceRestartLocked(s)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011588 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011589 + s.shortName);
11590 }
11591
11592 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
11593 ConnectionRecord c = new ConnectionRecord(b, activity,
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011594 connection, flags, clientLabel, clientIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011595
11596 IBinder binder = connection.asBinder();
11597 s.connections.put(binder, c);
11598 b.connections.add(c);
11599 if (activity != null) {
11600 if (activity.connections == null) {
11601 activity.connections = new HashSet<ConnectionRecord>();
11602 }
11603 activity.connections.add(c);
11604 }
11605 b.client.connections.add(c);
11606 mServiceConnections.put(binder, c);
11607
11608 if ((flags&Context.BIND_AUTO_CREATE) != 0) {
11609 s.lastActivity = SystemClock.uptimeMillis();
11610 if (!bringUpServiceLocked(s, service.getFlags(), false)) {
11611 return 0;
11612 }
11613 }
11614
11615 if (s.app != null) {
11616 // This could have made the service more important.
11617 updateOomAdjLocked(s.app);
11618 }
11619
Joe Onorato8a9b2202010-02-26 18:56:32 -080011620 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011621 + ": received=" + b.intent.received
11622 + " apps=" + b.intent.apps.size()
11623 + " doRebind=" + b.intent.doRebind);
11624
11625 if (s.app != null && b.intent.received) {
11626 // Service is already running, so we can immediately
11627 // publish the connection.
11628 try {
11629 c.conn.connected(s.name, b.intent.binder);
11630 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011631 Slog.w(TAG, "Failure sending service " + s.shortName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011632 + " to connection " + c.conn.asBinder()
11633 + " (in " + c.binding.client.processName + ")", e);
11634 }
11635
11636 // If this is the first app connected back to this binding,
11637 // and the service had previously asked to be told when
11638 // rebound, then do so.
11639 if (b.intent.apps.size() == 1 && b.intent.doRebind) {
11640 requestServiceBindingLocked(s, b.intent, true);
11641 }
11642 } else if (!b.intent.requested) {
11643 requestServiceBindingLocked(s, b.intent, false);
11644 }
11645
11646 Binder.restoreCallingIdentity(origId);
11647 }
11648
11649 return 1;
11650 }
11651
11652 private void removeConnectionLocked(
11653 ConnectionRecord c, ProcessRecord skipApp, HistoryRecord skipAct) {
11654 IBinder binder = c.conn.asBinder();
11655 AppBindRecord b = c.binding;
11656 ServiceRecord s = b.service;
11657 s.connections.remove(binder);
11658 b.connections.remove(c);
11659 if (c.activity != null && c.activity != skipAct) {
11660 if (c.activity.connections != null) {
11661 c.activity.connections.remove(c);
11662 }
11663 }
11664 if (b.client != skipApp) {
11665 b.client.connections.remove(c);
11666 }
11667 mServiceConnections.remove(binder);
11668
11669 if (b.connections.size() == 0) {
11670 b.intent.apps.remove(b.client);
11671 }
11672
Joe Onorato8a9b2202010-02-26 18:56:32 -080011673 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011674 + ": shouldUnbind=" + b.intent.hasBound);
11675 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0
11676 && b.intent.hasBound) {
11677 try {
11678 bumpServiceExecutingLocked(s);
11679 updateOomAdjLocked(s.app);
11680 b.intent.hasBound = false;
11681 // Assume the client doesn't want to know about a rebind;
11682 // we will deal with that later if it asks for one.
11683 b.intent.doRebind = false;
11684 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent());
11685 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011686 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011687 serviceDoneExecutingLocked(s, true);
11688 }
11689 }
11690
11691 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
11692 bringDownServiceLocked(s, false);
11693 }
11694 }
11695
11696 public boolean unbindService(IServiceConnection connection) {
11697 synchronized (this) {
11698 IBinder binder = connection.asBinder();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011699 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011700 ConnectionRecord r = mServiceConnections.get(binder);
11701 if (r == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011702 Slog.w(TAG, "Unbind failed: could not find connection for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011703 + connection.asBinder());
11704 return false;
11705 }
11706
11707 final long origId = Binder.clearCallingIdentity();
11708
11709 removeConnectionLocked(r, null, null);
11710
11711 if (r.binding.service.app != null) {
11712 // This could have made the service less important.
11713 updateOomAdjLocked(r.binding.service.app);
11714 }
11715
11716 Binder.restoreCallingIdentity(origId);
11717 }
11718
11719 return true;
11720 }
11721
11722 public void publishService(IBinder token, Intent intent, IBinder service) {
11723 // Refuse possible leaked file descriptors
11724 if (intent != null && intent.hasFileDescriptors() == true) {
11725 throw new IllegalArgumentException("File descriptors passed in Intent");
11726 }
11727
11728 synchronized(this) {
11729 if (!(token instanceof ServiceRecord)) {
11730 throw new IllegalArgumentException("Invalid service token");
11731 }
11732 ServiceRecord r = (ServiceRecord)token;
11733
11734 final long origId = Binder.clearCallingIdentity();
11735
Joe Onorato8a9b2202010-02-26 18:56:32 -080011736 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING SERVICE " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011737 + " " + intent + ": " + service);
11738 if (r != null) {
11739 Intent.FilterComparison filter
11740 = new Intent.FilterComparison(intent);
11741 IntentBindRecord b = r.bindings.get(filter);
11742 if (b != null && !b.received) {
11743 b.binder = service;
11744 b.requested = true;
11745 b.received = true;
11746 if (r.connections.size() > 0) {
11747 Iterator<ConnectionRecord> it
11748 = r.connections.values().iterator();
11749 while (it.hasNext()) {
11750 ConnectionRecord c = it.next();
11751 if (!filter.equals(c.binding.intent.intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011752 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 TAG, "Not publishing to: " + c);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011754 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 TAG, "Bound intent: " + c.binding.intent.intent);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011756 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 TAG, "Published intent: " + intent);
11758 continue;
11759 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080011760 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011761 try {
11762 c.conn.connected(r.name, service);
11763 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011764 Slog.w(TAG, "Failure sending service " + r.name +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011765 " to connection " + c.conn.asBinder() +
11766 " (in " + c.binding.client.processName + ")", e);
11767 }
11768 }
11769 }
11770 }
11771
11772 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
11773
11774 Binder.restoreCallingIdentity(origId);
11775 }
11776 }
11777 }
11778
11779 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
11780 // Refuse possible leaked file descriptors
11781 if (intent != null && intent.hasFileDescriptors() == true) {
11782 throw new IllegalArgumentException("File descriptors passed in Intent");
11783 }
11784
11785 synchronized(this) {
11786 if (!(token instanceof ServiceRecord)) {
11787 throw new IllegalArgumentException("Invalid service token");
11788 }
11789 ServiceRecord r = (ServiceRecord)token;
11790
11791 final long origId = Binder.clearCallingIdentity();
11792
11793 if (r != null) {
11794 Intent.FilterComparison filter
11795 = new Intent.FilterComparison(intent);
11796 IntentBindRecord b = r.bindings.get(filter);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011797 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011798 + " at " + b + ": apps="
11799 + (b != null ? b.apps.size() : 0));
11800 if (b != null) {
11801 if (b.apps.size() > 0) {
11802 // Applications have already bound since the last
11803 // unbind, so just rebind right here.
11804 requestServiceBindingLocked(r, b, true);
11805 } else {
11806 // Note to tell the service the next time there is
11807 // a new client.
11808 b.doRebind = true;
11809 }
11810 }
11811
11812 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
11813
11814 Binder.restoreCallingIdentity(origId);
11815 }
11816 }
11817 }
11818
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011819 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011820 synchronized(this) {
11821 if (!(token instanceof ServiceRecord)) {
11822 throw new IllegalArgumentException("Invalid service token");
11823 }
11824 ServiceRecord r = (ServiceRecord)token;
11825 boolean inStopping = mStoppingServices.contains(token);
11826 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011827 if (DEBUG_SERVICE) Slog.v(TAG, "DONE EXECUTING SERVICE " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011828 + ": nesting=" + r.executeNesting
11829 + ", inStopping=" + inStopping);
11830 if (r != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011831 Slog.w(TAG, "Done executing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011832 + " with incorrect token: given " + token
11833 + ", expected " + r);
11834 return;
11835 }
11836
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070011837 if (type == 1) {
11838 // This is a call from a service start... take care of
11839 // book-keeping.
11840 r.callStart = true;
11841 switch (res) {
11842 case Service.START_STICKY_COMPATIBILITY:
11843 case Service.START_STICKY: {
11844 // We are done with the associated start arguments.
11845 r.findDeliveredStart(startId, true);
11846 // Don't stop if killed.
11847 r.stopIfKilled = false;
11848 break;
11849 }
11850 case Service.START_NOT_STICKY: {
11851 // We are done with the associated start arguments.
11852 r.findDeliveredStart(startId, true);
11853 if (r.lastStartId == startId) {
11854 // There is no more work, and this service
11855 // doesn't want to hang around if killed.
11856 r.stopIfKilled = true;
11857 }
11858 break;
11859 }
11860 case Service.START_REDELIVER_INTENT: {
11861 // We'll keep this item until they explicitly
11862 // call stop for it, but keep track of the fact
11863 // that it was delivered.
11864 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
11865 if (si != null) {
11866 si.deliveryCount = 0;
11867 si.doneExecutingCount++;
11868 // Don't stop if killed.
11869 r.stopIfKilled = true;
11870 }
11871 break;
11872 }
11873 default:
11874 throw new IllegalArgumentException(
11875 "Unknown service start result: " + res);
11876 }
11877 if (res == Service.START_STICKY_COMPATIBILITY) {
11878 r.callStart = false;
11879 }
11880 }
11881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882 final long origId = Binder.clearCallingIdentity();
11883 serviceDoneExecutingLocked(r, inStopping);
11884 Binder.restoreCallingIdentity(origId);
11885 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011886 Slog.w(TAG, "Done executing unknown service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011887 + " with token " + token);
11888 }
11889 }
11890 }
11891
11892 public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) {
11893 r.executeNesting--;
11894 if (r.executeNesting <= 0 && r.app != null) {
11895 r.app.executingServices.remove(r);
11896 if (r.app.executingServices.size() == 0) {
11897 mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app);
11898 }
11899 if (inStopping) {
11900 mStoppingServices.remove(r);
11901 }
11902 updateOomAdjLocked(r.app);
11903 }
11904 }
11905
11906 void serviceTimeout(ProcessRecord proc) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011907 String anrMessage = null;
11908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011909 synchronized(this) {
11910 if (proc.executingServices.size() == 0 || proc.thread == null) {
11911 return;
11912 }
11913 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT;
11914 Iterator<ServiceRecord> it = proc.executingServices.iterator();
11915 ServiceRecord timeout = null;
11916 long nextTime = 0;
11917 while (it.hasNext()) {
11918 ServiceRecord sr = it.next();
11919 if (sr.executingStart < maxTime) {
11920 timeout = sr;
11921 break;
11922 }
11923 if (sr.executingStart > nextTime) {
11924 nextTime = sr.executingStart;
11925 }
11926 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011927 if (timeout != null && mLruProcesses.contains(proc)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011928 Slog.w(TAG, "Timeout executing service: " + timeout);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011929 anrMessage = "Executing service " + timeout.shortName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011930 } else {
11931 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
11932 msg.obj = proc;
11933 mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT);
11934 }
11935 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011936
11937 if (anrMessage != null) {
11938 appNotResponding(proc, null, null, anrMessage);
11939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011940 }
11941
11942 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -070011943 // BACKUP AND RESTORE
11944 // =========================================================
11945
11946 // Cause the target app to be launched if necessary and its backup agent
11947 // instantiated. The backup agent will invoke backupAgentCreated() on the
11948 // activity manager to announce its creation.
11949 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011950 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070011951 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
11952
11953 synchronized(this) {
11954 // !!! TODO: currently no check here that we're already bound
11955 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
11956 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
11957 synchronized (stats) {
11958 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
11959 }
11960
11961 BackupRecord r = new BackupRecord(ss, app, backupMode);
11962 ComponentName hostingName = new ComponentName(app.packageName, app.backupAgentName);
11963 // startProcessLocked() returns existing proc's record if it's already running
11964 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011965 false, 0, "backup", hostingName, false);
Christopher Tate181fafa2009-05-14 11:12:14 -070011966 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011967 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -070011968 return false;
11969 }
11970
11971 r.app = proc;
11972 mBackupTarget = r;
11973 mBackupAppName = app.packageName;
11974
Christopher Tate6fa95972009-06-05 18:43:55 -070011975 // Try not to kill the process during backup
11976 updateOomAdjLocked(proc);
11977
Christopher Tate181fafa2009-05-14 11:12:14 -070011978 // If the process is already attached, schedule the creation of the backup agent now.
11979 // If it is not yet live, this will be done when it attaches to the framework.
11980 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011981 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -070011982 try {
11983 proc.thread.scheduleCreateBackupAgent(app, backupMode);
11984 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -070011985 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -070011986 }
11987 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011988 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -070011989 }
11990 // Invariants: at this point, the target app process exists and the application
11991 // is either already running or in the process of coming up. mBackupTarget and
11992 // mBackupAppName describe the app, so that when it binds back to the AM we
11993 // know that it's scheduled for a backup-agent operation.
11994 }
11995
11996 return true;
11997 }
11998
11999 // A backup agent has just come up
12000 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012001 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -070012002 + " = " + agent);
12003
12004 synchronized(this) {
12005 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012006 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -070012007 return;
12008 }
12009
Christopher Tate043dadc2009-06-02 16:11:00 -070012010 long oldIdent = Binder.clearCallingIdentity();
Christopher Tate181fafa2009-05-14 11:12:14 -070012011 try {
12012 IBackupManager bm = IBackupManager.Stub.asInterface(
12013 ServiceManager.getService(Context.BACKUP_SERVICE));
12014 bm.agentConnected(agentPackageName, agent);
12015 } catch (RemoteException e) {
12016 // can't happen; the backup manager service is local
12017 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012018 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
Christopher Tate181fafa2009-05-14 11:12:14 -070012019 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -070012020 } finally {
12021 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -070012022 }
12023 }
12024 }
12025
12026 // done with this agent
12027 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012028 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -070012029 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012030 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -070012031 return;
12032 }
Christopher Tate181fafa2009-05-14 11:12:14 -070012033
12034 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -070012035 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012036 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -070012037 return;
12038 }
12039
Christopher Tate181fafa2009-05-14 11:12:14 -070012040 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012041 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -070012042 return;
12043 }
12044
Christopher Tate6fa95972009-06-05 18:43:55 -070012045 ProcessRecord proc = mBackupTarget.app;
12046 mBackupTarget = null;
12047 mBackupAppName = null;
12048
12049 // Not backing this app up any more; reset its OOM adjustment
12050 updateOomAdjLocked(proc);
12051
Christopher Tatec7b31e32009-06-10 15:49:30 -070012052 // If the app crashed during backup, 'thread' will be null here
12053 if (proc.thread != null) {
12054 try {
12055 proc.thread.scheduleDestroyBackupAgent(appInfo);
12056 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012057 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -070012058 e.printStackTrace();
12059 }
Christopher Tate181fafa2009-05-14 11:12:14 -070012060 }
Christopher Tate181fafa2009-05-14 11:12:14 -070012061 }
12062 }
12063 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012064 // BROADCASTS
12065 // =========================================================
12066
Josh Bartel7f208742010-02-25 11:01:44 -060012067 private final List getStickiesLocked(String action, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012068 List cur) {
12069 final ContentResolver resolver = mContext.getContentResolver();
12070 final ArrayList<Intent> list = mStickyBroadcasts.get(action);
12071 if (list == null) {
12072 return cur;
12073 }
12074 int N = list.size();
12075 for (int i=0; i<N; i++) {
12076 Intent intent = list.get(i);
12077 if (filter.match(resolver, intent, true, TAG) >= 0) {
12078 if (cur == null) {
12079 cur = new ArrayList<Intent>();
12080 }
12081 cur.add(intent);
12082 }
12083 }
12084 return cur;
12085 }
12086
12087 private final void scheduleBroadcastsLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012088 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012089 + mBroadcastsScheduled);
12090
12091 if (mBroadcastsScheduled) {
12092 return;
12093 }
12094 mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG);
12095 mBroadcastsScheduled = true;
12096 }
12097
12098 public Intent registerReceiver(IApplicationThread caller,
12099 IIntentReceiver receiver, IntentFilter filter, String permission) {
12100 synchronized(this) {
12101 ProcessRecord callerApp = null;
12102 if (caller != null) {
12103 callerApp = getRecordForAppLocked(caller);
12104 if (callerApp == null) {
12105 throw new SecurityException(
12106 "Unable to find app for caller " + caller
12107 + " (pid=" + Binder.getCallingPid()
12108 + ") when registering receiver " + receiver);
12109 }
12110 }
12111
12112 List allSticky = null;
12113
12114 // Look for any matching sticky broadcasts...
12115 Iterator actions = filter.actionsIterator();
12116 if (actions != null) {
12117 while (actions.hasNext()) {
12118 String action = (String)actions.next();
Josh Bartel7f208742010-02-25 11:01:44 -060012119 allSticky = getStickiesLocked(action, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012120 }
12121 } else {
Josh Bartel7f208742010-02-25 11:01:44 -060012122 allSticky = getStickiesLocked(null, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 }
12124
12125 // The first sticky in the list is returned directly back to
12126 // the client.
12127 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
12128
Joe Onorato8a9b2202010-02-26 18:56:32 -080012129 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012130 + ": " + sticky);
12131
12132 if (receiver == null) {
12133 return sticky;
12134 }
12135
12136 ReceiverList rl
12137 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
12138 if (rl == null) {
12139 rl = new ReceiverList(this, callerApp,
12140 Binder.getCallingPid(),
12141 Binder.getCallingUid(), receiver);
12142 if (rl.app != null) {
12143 rl.app.receivers.add(rl);
12144 } else {
12145 try {
12146 receiver.asBinder().linkToDeath(rl, 0);
12147 } catch (RemoteException e) {
12148 return sticky;
12149 }
12150 rl.linkedToDeath = true;
12151 }
12152 mRegisteredReceivers.put(receiver.asBinder(), rl);
12153 }
12154 BroadcastFilter bf = new BroadcastFilter(filter, rl, permission);
12155 rl.add(bf);
12156 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012157 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 }
12159 mReceiverResolver.addFilter(bf);
12160
12161 // Enqueue broadcasts for all existing stickies that match
12162 // this filter.
12163 if (allSticky != null) {
12164 ArrayList receivers = new ArrayList();
12165 receivers.add(bf);
12166
12167 int N = allSticky.size();
12168 for (int i=0; i<N; i++) {
12169 Intent intent = (Intent)allSticky.get(i);
12170 BroadcastRecord r = new BroadcastRecord(intent, null,
12171 null, -1, -1, null, receivers, null, 0, null, null,
Dianne Hackborn12527f92009-11-11 17:39:50 -080012172 false, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 if (mParallelBroadcasts.size() == 0) {
12174 scheduleBroadcastsLocked();
12175 }
12176 mParallelBroadcasts.add(r);
12177 }
12178 }
12179
12180 return sticky;
12181 }
12182 }
12183
12184 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012185 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186
12187 boolean doNext = false;
12188
12189 synchronized(this) {
12190 ReceiverList rl
12191 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
12192 if (rl != null) {
12193 if (rl.curBroadcast != null) {
12194 BroadcastRecord r = rl.curBroadcast;
12195 doNext = finishReceiverLocked(
12196 receiver.asBinder(), r.resultCode, r.resultData,
12197 r.resultExtras, r.resultAbort, true);
12198 }
12199
12200 if (rl.app != null) {
12201 rl.app.receivers.remove(rl);
12202 }
12203 removeReceiverLocked(rl);
12204 if (rl.linkedToDeath) {
12205 rl.linkedToDeath = false;
12206 rl.receiver.asBinder().unlinkToDeath(rl, 0);
12207 }
12208 }
12209 }
12210
12211 if (!doNext) {
12212 return;
12213 }
12214
12215 final long origId = Binder.clearCallingIdentity();
12216 processNextBroadcast(false);
12217 trimApplications();
12218 Binder.restoreCallingIdentity(origId);
12219 }
12220
12221 void removeReceiverLocked(ReceiverList rl) {
12222 mRegisteredReceivers.remove(rl.receiver.asBinder());
12223 int N = rl.size();
12224 for (int i=0; i<N; i++) {
12225 mReceiverResolver.removeFilter(rl.get(i));
12226 }
12227 }
12228
12229 private final int broadcastIntentLocked(ProcessRecord callerApp,
12230 String callerPackage, Intent intent, String resolvedType,
12231 IIntentReceiver resultTo, int resultCode, String resultData,
12232 Bundle map, String requiredPermission,
12233 boolean ordered, boolean sticky, int callingPid, int callingUid) {
12234 intent = new Intent(intent);
12235
Joe Onorato8a9b2202010-02-26 18:56:32 -080012236 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012237 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
12238 + " ordered=" + ordered);
12239 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012240 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012241 }
12242
12243 // Handle special intents: if this broadcast is from the package
12244 // manager about a package being removed, we need to remove all of
12245 // its activities from the history stack.
12246 final boolean uidRemoved = intent.ACTION_UID_REMOVED.equals(
12247 intent.getAction());
12248 if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
12249 || intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080012250 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012251 || uidRemoved) {
12252 if (checkComponentPermission(
12253 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
12254 callingPid, callingUid, -1)
12255 == PackageManager.PERMISSION_GRANTED) {
12256 if (uidRemoved) {
12257 final Bundle intentExtras = intent.getExtras();
12258 final int uid = intentExtras != null
12259 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
12260 if (uid >= 0) {
12261 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
12262 synchronized (bs) {
12263 bs.removeUidStatsLocked(uid);
12264 }
12265 }
12266 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012267 // If resources are unvailble just force stop all
12268 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080012269 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012270 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
12271 if (list != null && (list.length > 0)) {
12272 for (String pkg : list) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080012273 forceStopPackageLocked(pkg, -1, false, true, true);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012274 }
12275 }
12276 } else {
12277 Uri data = intent.getData();
12278 String ssp;
12279 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
12280 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
12281 forceStopPackageLocked(ssp,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080012282 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true);
Dianne Hackbornde7faf62009-06-30 13:27:30 -070012283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 }
12285 }
12286 }
12287 } else {
12288 String msg = "Permission Denial: " + intent.getAction()
12289 + " broadcast from " + callerPackage + " (pid=" + callingPid
12290 + ", uid=" + callingUid + ")"
12291 + " requires "
12292 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012293 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012294 throw new SecurityException(msg);
12295 }
12296 }
12297
12298 /*
12299 * If this is the time zone changed action, queue up a message that will reset the timezone
12300 * of all currently running processes. This message will get queued up before the broadcast
12301 * happens.
12302 */
12303 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
12304 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
12305 }
12306
Dianne Hackborn854060af2009-07-09 18:14:31 -070012307 /*
12308 * Prevent non-system code (defined here to be non-persistent
12309 * processes) from sending protected broadcasts.
12310 */
12311 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
12312 || callingUid == Process.SHELL_UID || callingUid == 0) {
12313 // Always okay.
12314 } else if (callerApp == null || !callerApp.persistent) {
12315 try {
12316 if (ActivityThread.getPackageManager().isProtectedBroadcast(
12317 intent.getAction())) {
12318 String msg = "Permission Denial: not allowed to send broadcast "
12319 + intent.getAction() + " from pid="
12320 + callingPid + ", uid=" + callingUid;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012321 Slog.w(TAG, msg);
Dianne Hackborn854060af2009-07-09 18:14:31 -070012322 throw new SecurityException(msg);
12323 }
12324 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012325 Slog.w(TAG, "Remote exception", e);
Dianne Hackborn854060af2009-07-09 18:14:31 -070012326 return BROADCAST_SUCCESS;
12327 }
12328 }
12329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012330 // Add to the sticky list if requested.
12331 if (sticky) {
12332 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
12333 callingPid, callingUid)
12334 != PackageManager.PERMISSION_GRANTED) {
12335 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
12336 + callingPid + ", uid=" + callingUid
12337 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012338 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012339 throw new SecurityException(msg);
12340 }
12341 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012342 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012343 + " and enforce permission " + requiredPermission);
12344 return BROADCAST_STICKY_CANT_HAVE_PERMISSION;
12345 }
12346 if (intent.getComponent() != null) {
12347 throw new SecurityException(
12348 "Sticky broadcasts can't target a specific component");
12349 }
12350 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
12351 if (list == null) {
12352 list = new ArrayList<Intent>();
12353 mStickyBroadcasts.put(intent.getAction(), list);
12354 }
12355 int N = list.size();
12356 int i;
12357 for (i=0; i<N; i++) {
12358 if (intent.filterEquals(list.get(i))) {
12359 // This sticky already exists, replace it.
12360 list.set(i, new Intent(intent));
12361 break;
12362 }
12363 }
12364 if (i >= N) {
12365 list.add(new Intent(intent));
12366 }
12367 }
12368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012369 // Figure out who all will receive this broadcast.
12370 List receivers = null;
12371 List<BroadcastFilter> registeredReceivers = null;
12372 try {
12373 if (intent.getComponent() != null) {
12374 // Broadcast is going to one specific receiver class...
12375 ActivityInfo ai = ActivityThread.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -070012376 getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012377 if (ai != null) {
12378 receivers = new ArrayList();
12379 ResolveInfo ri = new ResolveInfo();
12380 ri.activityInfo = ai;
12381 receivers.add(ri);
12382 }
12383 } else {
12384 // Need to resolve the intent to interested receivers...
12385 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
12386 == 0) {
12387 receivers =
12388 ActivityThread.getPackageManager().queryIntentReceivers(
Dianne Hackborn1655be42009-05-08 14:29:01 -070012389 intent, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012390 }
Mihai Preda074edef2009-05-18 17:13:31 +020012391 registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012392 }
12393 } catch (RemoteException ex) {
12394 // pm is in same process, this will never happen.
12395 }
12396
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012397 final boolean replacePending =
12398 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
12399
Joe Onorato8a9b2202010-02-26 18:56:32 -080012400 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012401 + " replacePending=" + replacePending);
12402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012403 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
12404 if (!ordered && NR > 0) {
12405 // If we are not serializing this broadcast, then send the
12406 // registered receivers separately so they don't wait for the
12407 // components to be launched.
12408 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
12409 callerPackage, callingPid, callingUid, requiredPermission,
12410 registeredReceivers, resultTo, resultCode, resultData, map,
Dianne Hackborn12527f92009-11-11 17:39:50 -080012411 ordered, sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012412 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012413 TAG, "Enqueueing parallel broadcast " + r
12414 + ": prev had " + mParallelBroadcasts.size());
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012415 boolean replaced = false;
12416 if (replacePending) {
12417 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
12418 if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012419 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012420 "***** DROPPING PARALLEL: " + intent);
12421 mParallelBroadcasts.set(i, r);
12422 replaced = true;
12423 break;
12424 }
12425 }
12426 }
12427 if (!replaced) {
12428 mParallelBroadcasts.add(r);
12429 scheduleBroadcastsLocked();
12430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012431 registeredReceivers = null;
12432 NR = 0;
12433 }
12434
12435 // Merge into one list.
12436 int ir = 0;
12437 if (receivers != null) {
12438 // A special case for PACKAGE_ADDED: do not allow the package
12439 // being added to see this broadcast. This prevents them from
12440 // using this as a back door to get run as soon as they are
12441 // installed. Maybe in the future we want to have a special install
12442 // broadcast or such for apps, but we'd like to deliberately make
12443 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012444 String skipPackages[] = null;
12445 if (intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
12446 || intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
12447 || intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
12448 Uri data = intent.getData();
12449 if (data != null) {
12450 String pkgName = data.getSchemeSpecificPart();
12451 if (pkgName != null) {
12452 skipPackages = new String[] { pkgName };
12453 }
12454 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080012455 } else if (intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012456 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -070012457 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080012458 if (skipPackages != null && (skipPackages.length > 0)) {
12459 for (String skipPackage : skipPackages) {
12460 if (skipPackage != null) {
12461 int NT = receivers.size();
12462 for (int it=0; it<NT; it++) {
12463 ResolveInfo curt = (ResolveInfo)receivers.get(it);
12464 if (curt.activityInfo.packageName.equals(skipPackage)) {
12465 receivers.remove(it);
12466 it--;
12467 NT--;
12468 }
12469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012470 }
12471 }
12472 }
12473
12474 int NT = receivers != null ? receivers.size() : 0;
12475 int it = 0;
12476 ResolveInfo curt = null;
12477 BroadcastFilter curr = null;
12478 while (it < NT && ir < NR) {
12479 if (curt == null) {
12480 curt = (ResolveInfo)receivers.get(it);
12481 }
12482 if (curr == null) {
12483 curr = registeredReceivers.get(ir);
12484 }
12485 if (curr.getPriority() >= curt.priority) {
12486 // Insert this broadcast record into the final list.
12487 receivers.add(it, curr);
12488 ir++;
12489 curr = null;
12490 it++;
12491 NT++;
12492 } else {
12493 // Skip to the next ResolveInfo in the final list.
12494 it++;
12495 curt = null;
12496 }
12497 }
12498 }
12499 while (ir < NR) {
12500 if (receivers == null) {
12501 receivers = new ArrayList();
12502 }
12503 receivers.add(registeredReceivers.get(ir));
12504 ir++;
12505 }
12506
12507 if ((receivers != null && receivers.size() > 0)
12508 || resultTo != null) {
12509 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
12510 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -080012511 receivers, resultTo, resultCode, resultData, map, ordered,
12512 sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012513 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 TAG, "Enqueueing ordered broadcast " + r
12515 + ": prev had " + mOrderedBroadcasts.size());
12516 if (DEBUG_BROADCAST) {
12517 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012518 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012520 boolean replaced = false;
12521 if (replacePending) {
12522 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
12523 if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012524 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080012525 "***** DROPPING ORDERED: " + intent);
12526 mOrderedBroadcasts.set(i, r);
12527 replaced = true;
12528 break;
12529 }
12530 }
12531 }
12532 if (!replaced) {
12533 mOrderedBroadcasts.add(r);
12534 scheduleBroadcastsLocked();
12535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 }
12537
12538 return BROADCAST_SUCCESS;
12539 }
12540
12541 public final int broadcastIntent(IApplicationThread caller,
12542 Intent intent, String resolvedType, IIntentReceiver resultTo,
12543 int resultCode, String resultData, Bundle map,
12544 String requiredPermission, boolean serialized, boolean sticky) {
12545 // Refuse possible leaked file descriptors
12546 if (intent != null && intent.hasFileDescriptors() == true) {
12547 throw new IllegalArgumentException("File descriptors passed in Intent");
12548 }
12549
12550 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -070012551 int flags = intent.getFlags();
12552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012553 if (!mSystemReady) {
12554 // if the caller really truly claims to know what they're doing, go
12555 // ahead and allow the broadcast without launching any receivers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012556 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
12557 intent = new Intent(intent);
12558 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
12559 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0){
Joe Onorato8a9b2202010-02-26 18:56:32 -080012560 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012561 + " before boot completion");
12562 throw new IllegalStateException("Cannot broadcast before boot completed");
12563 }
12564 }
12565
Dianne Hackborn9acc0302009-08-25 00:27:12 -070012566 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
12567 throw new IllegalArgumentException(
12568 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
12569 }
12570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012571 final ProcessRecord callerApp = getRecordForAppLocked(caller);
12572 final int callingPid = Binder.getCallingPid();
12573 final int callingUid = Binder.getCallingUid();
12574 final long origId = Binder.clearCallingIdentity();
12575 int res = broadcastIntentLocked(callerApp,
12576 callerApp != null ? callerApp.info.packageName : null,
12577 intent, resolvedType, resultTo,
12578 resultCode, resultData, map, requiredPermission, serialized,
12579 sticky, callingPid, callingUid);
12580 Binder.restoreCallingIdentity(origId);
12581 return res;
12582 }
12583 }
12584
12585 int broadcastIntentInPackage(String packageName, int uid,
12586 Intent intent, String resolvedType, IIntentReceiver resultTo,
12587 int resultCode, String resultData, Bundle map,
12588 String requiredPermission, boolean serialized, boolean sticky) {
12589 synchronized(this) {
12590 final long origId = Binder.clearCallingIdentity();
12591 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
12592 resultTo, resultCode, resultData, map, requiredPermission,
12593 serialized, sticky, -1, uid);
12594 Binder.restoreCallingIdentity(origId);
12595 return res;
12596 }
12597 }
12598
12599 public final void unbroadcastIntent(IApplicationThread caller,
12600 Intent intent) {
12601 // Refuse possible leaked file descriptors
12602 if (intent != null && intent.hasFileDescriptors() == true) {
12603 throw new IllegalArgumentException("File descriptors passed in Intent");
12604 }
12605
12606 synchronized(this) {
12607 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
12608 != PackageManager.PERMISSION_GRANTED) {
12609 String msg = "Permission Denial: unbroadcastIntent() from pid="
12610 + Binder.getCallingPid()
12611 + ", uid=" + Binder.getCallingUid()
12612 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012613 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012614 throw new SecurityException(msg);
12615 }
12616 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
12617 if (list != null) {
12618 int N = list.size();
12619 int i;
12620 for (i=0; i<N; i++) {
12621 if (intent.filterEquals(list.get(i))) {
12622 list.remove(i);
12623 break;
12624 }
12625 }
12626 }
12627 }
12628 }
12629
12630 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
12631 String resultData, Bundle resultExtras, boolean resultAbort,
12632 boolean explicit) {
12633 if (mOrderedBroadcasts.size() == 0) {
12634 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012635 Slog.w(TAG, "finishReceiver called but no pending broadcasts");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012636 }
12637 return false;
12638 }
12639 BroadcastRecord r = mOrderedBroadcasts.get(0);
12640 if (r.receiver == null) {
12641 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012642 Slog.w(TAG, "finishReceiver called but none active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012643 }
12644 return false;
12645 }
12646 if (r.receiver != receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012647 Slog.w(TAG, "finishReceiver called but active receiver is different");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012648 return false;
12649 }
12650 int state = r.state;
12651 r.state = r.IDLE;
12652 if (state == r.IDLE) {
12653 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012654 Slog.w(TAG, "finishReceiver called but state is IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012655 }
12656 }
12657 r.receiver = null;
12658 r.intent.setComponent(null);
12659 if (r.curApp != null) {
12660 r.curApp.curReceiver = null;
12661 }
12662 if (r.curFilter != null) {
12663 r.curFilter.receiverList.curBroadcast = null;
12664 }
12665 r.curFilter = null;
12666 r.curApp = null;
12667 r.curComponent = null;
12668 r.curReceiver = null;
12669 mPendingBroadcast = null;
12670
12671 r.resultCode = resultCode;
12672 r.resultData = resultData;
12673 r.resultExtras = resultExtras;
12674 r.resultAbort = resultAbort;
12675
12676 // We will process the next receiver right now if this is finishing
12677 // an app receiver (which is always asynchronous) or after we have
12678 // come back from calling a receiver.
12679 return state == BroadcastRecord.APP_RECEIVE
12680 || state == BroadcastRecord.CALL_DONE_RECEIVE;
12681 }
12682
12683 public void finishReceiver(IBinder who, int resultCode, String resultData,
12684 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012685 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012686
12687 // Refuse possible leaked file descriptors
12688 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
12689 throw new IllegalArgumentException("File descriptors passed in Bundle");
12690 }
12691
12692 boolean doNext;
12693
12694 final long origId = Binder.clearCallingIdentity();
12695
12696 synchronized(this) {
12697 doNext = finishReceiverLocked(
12698 who, resultCode, resultData, resultExtras, resultAbort, true);
12699 }
12700
12701 if (doNext) {
12702 processNextBroadcast(false);
12703 }
12704 trimApplications();
12705
12706 Binder.restoreCallingIdentity(origId);
12707 }
12708
12709 private final void logBroadcastReceiverDiscard(BroadcastRecord r) {
12710 if (r.nextReceiver > 0) {
12711 Object curReceiver = r.receivers.get(r.nextReceiver-1);
12712 if (curReceiver instanceof BroadcastFilter) {
12713 BroadcastFilter bf = (BroadcastFilter) curReceiver;
Doug Zongker2bec3d42009-12-04 12:52:44 -080012714 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012715 System.identityHashCode(r),
12716 r.intent.getAction(),
12717 r.nextReceiver - 1,
12718 System.identityHashCode(bf));
12719 } else {
Doug Zongker2bec3d42009-12-04 12:52:44 -080012720 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721 System.identityHashCode(r),
12722 r.intent.getAction(),
12723 r.nextReceiver - 1,
12724 ((ResolveInfo)curReceiver).toString());
12725 }
12726 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012727 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012728 + r);
Doug Zongker2bec3d42009-12-04 12:52:44 -080012729 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012730 System.identityHashCode(r),
12731 r.intent.getAction(),
12732 r.nextReceiver,
12733 "NONE");
12734 }
12735 }
12736
12737 private final void broadcastTimeout() {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070012738 ProcessRecord app = null;
12739 String anrMessage = null;
12740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012741 synchronized (this) {
12742 if (mOrderedBroadcasts.size() == 0) {
12743 return;
12744 }
12745 long now = SystemClock.uptimeMillis();
12746 BroadcastRecord r = mOrderedBroadcasts.get(0);
Dianne Hackborn12527f92009-11-11 17:39:50 -080012747 if ((r.receiverTime+BROADCAST_TIMEOUT) > now) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012748 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012749 "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
Dianne Hackborn12527f92009-11-11 17:39:50 -080012750 + (r.receiverTime + BROADCAST_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012751 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
Dianne Hackborn12527f92009-11-11 17:39:50 -080012752 mHandler.sendMessageAtTime(msg, r.receiverTime+BROADCAST_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012753 return;
12754 }
12755
Joe Onorato8a9b2202010-02-26 18:56:32 -080012756 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -080012757 r.receiverTime = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012758 r.anrCount++;
12759
12760 // Current receiver has passed its expiration date.
12761 if (r.nextReceiver <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012762 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012763 return;
12764 }
12765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 Object curReceiver = r.receivers.get(r.nextReceiver-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012767 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012768 logBroadcastReceiverDiscard(r);
12769 if (curReceiver instanceof BroadcastFilter) {
12770 BroadcastFilter bf = (BroadcastFilter)curReceiver;
12771 if (bf.receiverList.pid != 0
12772 && bf.receiverList.pid != MY_PID) {
12773 synchronized (this.mPidsSelfLocked) {
12774 app = this.mPidsSelfLocked.get(
12775 bf.receiverList.pid);
12776 }
12777 }
12778 } else {
12779 app = r.curApp;
12780 }
12781
12782 if (app != null) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070012783 anrMessage = "Broadcast of " + r.intent.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012784 }
12785
12786 if (mPendingBroadcast == r) {
12787 mPendingBroadcast = null;
12788 }
12789
12790 // Move on to the next receiver.
12791 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
12792 r.resultExtras, r.resultAbort, true);
12793 scheduleBroadcastsLocked();
12794 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -070012795
12796 if (anrMessage != null) {
12797 appNotResponding(app, null, null, anrMessage);
12798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012799 }
12800
12801 private final void processCurBroadcastLocked(BroadcastRecord r,
12802 ProcessRecord app) throws RemoteException {
12803 if (app.thread == null) {
12804 throw new RemoteException();
12805 }
12806 r.receiver = app.thread.asBinder();
12807 r.curApp = app;
12808 app.curReceiver = r;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012809 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012810
12811 // Tell the application to launch this receiver.
12812 r.intent.setComponent(r.curComponent);
12813
12814 boolean started = false;
12815 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012816 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012817 "Delivering to component " + r.curComponent
12818 + ": " + r);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -070012819 ensurePackageDexOpt(r.intent.getComponent().getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012820 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
12821 r.resultCode, r.resultData, r.resultExtras, r.ordered);
12822 started = true;
12823 } finally {
12824 if (!started) {
12825 r.receiver = null;
12826 r.curApp = null;
12827 app.curReceiver = null;
12828 }
12829 }
12830
12831 }
12832
12833 static void performReceive(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070012834 Intent intent, int resultCode, String data, Bundle extras,
12835 boolean ordered, boolean sticky) throws RemoteException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 if (app != null && app.thread != null) {
12837 // If we have an app thread, do the call through that so it is
12838 // correctly ordered with other one-way calls.
12839 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070012840 data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012841 } else {
Dianne Hackborn68d881c2009-10-05 13:58:17 -070012842 receiver.performReceive(intent, resultCode, data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012843 }
12844 }
12845
12846 private final void deliverToRegisteredReceiver(BroadcastRecord r,
12847 BroadcastFilter filter, boolean ordered) {
12848 boolean skip = false;
12849 if (filter.requiredPermission != null) {
12850 int perm = checkComponentPermission(filter.requiredPermission,
12851 r.callingPid, r.callingUid, -1);
12852 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012853 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012854 + r.intent.toString()
12855 + " from " + r.callerPackage + " (pid="
12856 + r.callingPid + ", uid=" + r.callingUid + ")"
12857 + " requires " + filter.requiredPermission
12858 + " due to registered receiver " + filter);
12859 skip = true;
12860 }
12861 }
12862 if (r.requiredPermission != null) {
12863 int perm = checkComponentPermission(r.requiredPermission,
12864 filter.receiverList.pid, filter.receiverList.uid, -1);
12865 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012866 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012867 + r.intent.toString()
12868 + " to " + filter.receiverList.app
12869 + " (pid=" + filter.receiverList.pid
12870 + ", uid=" + filter.receiverList.uid + ")"
12871 + " requires " + r.requiredPermission
12872 + " due to sender " + r.callerPackage
12873 + " (uid " + r.callingUid + ")");
12874 skip = true;
12875 }
12876 }
12877
12878 if (!skip) {
12879 // If this is not being sent as an ordered broadcast, then we
12880 // don't want to touch the fields that keep track of the current
12881 // state of ordered broadcasts.
12882 if (ordered) {
12883 r.receiver = filter.receiverList.receiver.asBinder();
12884 r.curFilter = filter;
12885 filter.receiverList.curBroadcast = r;
12886 r.state = BroadcastRecord.CALL_IN_RECEIVE;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012887 if (filter.receiverList.app != null) {
12888 // Bump hosting application to no longer be in background
12889 // scheduling class. Note that we can't do that if there
12890 // isn't an app... but we can only be in that case for
12891 // things that directly call the IActivityManager API, which
12892 // are already core system stuff so don't matter for this.
12893 r.curApp = filter.receiverList.app;
12894 filter.receiverList.app.curReceiver = r;
12895 updateOomAdjLocked();
12896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012897 }
12898 try {
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012899 if (DEBUG_BROADCAST_LIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012900 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012901 Slog.i(TAG, "Delivering to " + filter.receiverList.app
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012902 + " (seq=" + seq + "): " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012903 }
12904 performReceive(filter.receiverList.app, filter.receiverList.receiver,
12905 new Intent(r.intent), r.resultCode,
Dianne Hackborn12527f92009-11-11 17:39:50 -080012906 r.resultData, r.resultExtras, r.ordered, r.initialSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012907 if (ordered) {
12908 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
12909 }
12910 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012911 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012912 if (ordered) {
12913 r.receiver = null;
12914 r.curFilter = null;
12915 filter.receiverList.curBroadcast = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012916 if (filter.receiverList.app != null) {
12917 filter.receiverList.app.curReceiver = null;
12918 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012919 }
12920 }
12921 }
12922 }
12923
Dianne Hackborn12527f92009-11-11 17:39:50 -080012924 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
12925 if (r.callingUid < 0) {
12926 // This was from a registerReceiver() call; ignore it.
12927 return;
12928 }
12929 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
12930 MAX_BROADCAST_HISTORY-1);
12931 r.finishTime = SystemClock.uptimeMillis();
12932 mBroadcastHistory[0] = r;
12933 }
12934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012935 private final void processNextBroadcast(boolean fromMsg) {
12936 synchronized(this) {
12937 BroadcastRecord r;
12938
Joe Onorato8a9b2202010-02-26 18:56:32 -080012939 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012940 + mParallelBroadcasts.size() + " broadcasts, "
12941 + mOrderedBroadcasts.size() + " serialized broadcasts");
12942
12943 updateCpuStats();
12944
12945 if (fromMsg) {
12946 mBroadcastsScheduled = false;
12947 }
12948
12949 // First, deliver any non-serialized broadcasts right away.
12950 while (mParallelBroadcasts.size() > 0) {
12951 r = mParallelBroadcasts.remove(0);
Dianne Hackborn12527f92009-11-11 17:39:50 -080012952 r.dispatchTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012953 final int N = r.receivers.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -080012954 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012955 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012956 for (int i=0; i<N; i++) {
12957 Object target = r.receivers.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012958 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012959 "Delivering non-serialized to registered "
12960 + target + ": " + r);
12961 deliverToRegisteredReceiver(r, (BroadcastFilter)target, false);
12962 }
Dianne Hackborn12527f92009-11-11 17:39:50 -080012963 addBroadcastToHistoryLocked(r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012964 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012965 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012966 }
12967
12968 // Now take care of the next serialized one...
12969
12970 // If we are waiting for a process to come up to handle the next
12971 // broadcast, then do nothing at this point. Just in case, we
12972 // check that the process we're waiting for still exists.
12973 if (mPendingBroadcast != null) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070012974 if (DEBUG_BROADCAST_LIGHT) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012975 Slog.v(TAG, "processNextBroadcast: waiting for "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070012976 + mPendingBroadcast.curApp);
12977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012978
12979 boolean isDead;
12980 synchronized (mPidsSelfLocked) {
12981 isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null);
12982 }
12983 if (!isDead) {
12984 // It's still alive, so keep waiting
12985 return;
12986 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012987 Slog.w(TAG, "pending app " + mPendingBroadcast.curApp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012988 + " died before responding to broadcast");
12989 mPendingBroadcast = null;
12990 }
12991 }
12992
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012993 boolean looped = false;
12994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012995 do {
12996 if (mOrderedBroadcasts.size() == 0) {
12997 // No more broadcasts pending, so all done!
12998 scheduleAppGcsLocked();
Dianne Hackborn82f3f002009-06-16 18:49:05 -070012999 if (looped) {
13000 // If we had finished the last ordered broadcast, then
13001 // make sure all processes have correct oom and sched
13002 // adjustments.
13003 updateOomAdjLocked();
13004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013005 return;
13006 }
13007 r = mOrderedBroadcasts.get(0);
13008 boolean forceReceive = false;
13009
13010 // Ensure that even if something goes awry with the timeout
13011 // detection, we catch "hung" broadcasts here, discard them,
13012 // and continue to make progress.
13013 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
13014 long now = SystemClock.uptimeMillis();
13015 if (r.dispatchTime > 0) {
13016 if ((numReceivers > 0) &&
13017 (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013018 Slog.w(TAG, "Hung broadcast discarded after timeout failure:"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013019 + " now=" + now
13020 + " dispatchTime=" + r.dispatchTime
Dianne Hackborn12527f92009-11-11 17:39:50 -080013021 + " startTime=" + r.receiverTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013022 + " intent=" + r.intent
13023 + " numReceivers=" + numReceivers
13024 + " nextReceiver=" + r.nextReceiver
13025 + " state=" + r.state);
13026 broadcastTimeout(); // forcibly finish this broadcast
13027 forceReceive = true;
13028 r.state = BroadcastRecord.IDLE;
13029 }
13030 }
13031
13032 if (r.state != BroadcastRecord.IDLE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013033 if (DEBUG_BROADCAST) Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013034 "processNextBroadcast() called when not idle (state="
13035 + r.state + ")");
13036 return;
13037 }
13038
13039 if (r.receivers == null || r.nextReceiver >= numReceivers
13040 || r.resultAbort || forceReceive) {
13041 // No more receivers for this broadcast! Send the final
13042 // result if requested...
13043 if (r.resultTo != null) {
13044 try {
13045 if (DEBUG_BROADCAST) {
13046 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080013047 Slog.i(TAG, "Finishing broadcast " + r.intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013048 + " seq=" + seq + " app=" + r.callerApp);
13049 }
13050 performReceive(r.callerApp, r.resultTo,
13051 new Intent(r.intent), r.resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070013052 r.resultData, r.resultExtras, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013053 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013054 Slog.w(TAG, "Failure sending broadcast result of " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013055 }
13056 }
13057
Joe Onorato8a9b2202010-02-26 18:56:32 -080013058 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013059 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG);
13060
Joe Onorato8a9b2202010-02-26 18:56:32 -080013061 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070013062 + r);
13063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013064 // ... and on to the next...
Dianne Hackborn12527f92009-11-11 17:39:50 -080013065 addBroadcastToHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013066 mOrderedBroadcasts.remove(0);
13067 r = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070013068 looped = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 continue;
13070 }
13071 } while (r == null);
13072
13073 // Get the next receiver...
13074 int recIdx = r.nextReceiver++;
13075
13076 // Keep track of when this receiver started, and make sure there
13077 // is a timeout message pending to kill it if need be.
Dianne Hackborn12527f92009-11-11 17:39:50 -080013078 r.receiverTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013079 if (recIdx == 0) {
Dianne Hackborn12527f92009-11-11 17:39:50 -080013080 r.dispatchTime = r.receiverTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013081
Joe Onorato8a9b2202010-02-26 18:56:32 -080013082 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070013083 + r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080013084 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013085 "Submitting BROADCAST_TIMEOUT_MSG for "
Dianne Hackborn12527f92009-11-11 17:39:50 -080013086 + (r.receiverTime + BROADCAST_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
Dianne Hackborn12527f92009-11-11 17:39:50 -080013088 mHandler.sendMessageAtTime(msg, r.receiverTime+BROADCAST_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013089 }
13090
13091 Object nextReceiver = r.receivers.get(recIdx);
13092 if (nextReceiver instanceof BroadcastFilter) {
13093 // Simple case: this is a registered receiver who gets
13094 // a direct call.
13095 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Joe Onorato8a9b2202010-02-26 18:56:32 -080013096 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013097 "Delivering serialized to registered "
13098 + filter + ": " + r);
13099 deliverToRegisteredReceiver(r, filter, r.ordered);
13100 if (r.receiver == null || !r.ordered) {
13101 // The receiver has already finished, so schedule to
13102 // process the next one.
13103 r.state = BroadcastRecord.IDLE;
13104 scheduleBroadcastsLocked();
13105 }
13106 return;
13107 }
13108
13109 // Hard case: need to instantiate the receiver, possibly
13110 // starting its application process to host it.
13111
13112 ResolveInfo info =
13113 (ResolveInfo)nextReceiver;
13114
13115 boolean skip = false;
13116 int perm = checkComponentPermission(info.activityInfo.permission,
13117 r.callingPid, r.callingUid,
13118 info.activityInfo.exported
13119 ? -1 : info.activityInfo.applicationInfo.uid);
13120 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013121 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013122 + r.intent.toString()
13123 + " from " + r.callerPackage + " (pid=" + r.callingPid
13124 + ", uid=" + r.callingUid + ")"
13125 + " requires " + info.activityInfo.permission
13126 + " due to receiver " + info.activityInfo.packageName
13127 + "/" + info.activityInfo.name);
13128 skip = true;
13129 }
13130 if (r.callingUid != Process.SYSTEM_UID &&
13131 r.requiredPermission != null) {
13132 try {
13133 perm = ActivityThread.getPackageManager().
13134 checkPermission(r.requiredPermission,
13135 info.activityInfo.applicationInfo.packageName);
13136 } catch (RemoteException e) {
13137 perm = PackageManager.PERMISSION_DENIED;
13138 }
13139 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013140 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013141 + r.intent + " to "
13142 + info.activityInfo.applicationInfo.packageName
13143 + " requires " + r.requiredPermission
13144 + " due to sender " + r.callerPackage
13145 + " (uid " + r.callingUid + ")");
13146 skip = true;
13147 }
13148 }
13149 if (r.curApp != null && r.curApp.crashing) {
13150 // If the target process is crashing, just skip it.
13151 skip = true;
13152 }
13153
13154 if (skip) {
13155 r.receiver = null;
13156 r.curFilter = null;
13157 r.state = BroadcastRecord.IDLE;
13158 scheduleBroadcastsLocked();
13159 return;
13160 }
13161
13162 r.state = BroadcastRecord.APP_RECEIVE;
13163 String targetProcess = info.activityInfo.processName;
13164 r.curComponent = new ComponentName(
13165 info.activityInfo.applicationInfo.packageName,
13166 info.activityInfo.name);
13167 r.curReceiver = info.activityInfo;
13168
13169 // Is this receiver's application already running?
13170 ProcessRecord app = getProcessRecordLocked(targetProcess,
13171 info.activityInfo.applicationInfo.uid);
13172 if (app != null && app.thread != null) {
13173 try {
13174 processCurBroadcastLocked(r, app);
13175 return;
13176 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013177 Slog.w(TAG, "Exception when sending broadcast to "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013178 + r.curComponent, e);
13179 }
13180
13181 // If a dead object exception was thrown -- fall through to
13182 // restart the application.
13183 }
13184
Dianne Hackborn9acc0302009-08-25 00:27:12 -070013185 // Not running -- get it started, to be executed when the app comes up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013186 if ((r.curApp=startProcessLocked(targetProcess,
13187 info.activityInfo.applicationInfo, true,
13188 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
Dianne Hackborn9acc0302009-08-25 00:27:12 -070013189 "broadcast", r.curComponent,
13190 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0))
13191 == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013192 // Ah, this recipient is unavailable. Finish it if necessary,
13193 // and mark the broadcast record as ready for the next.
Joe Onorato8a9b2202010-02-26 18:56:32 -080013194 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013195 + info.activityInfo.applicationInfo.packageName + "/"
13196 + info.activityInfo.applicationInfo.uid + " for broadcast "
13197 + r.intent + ": process is bad");
13198 logBroadcastReceiverDiscard(r);
13199 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
13200 r.resultExtras, r.resultAbort, true);
13201 scheduleBroadcastsLocked();
13202 r.state = BroadcastRecord.IDLE;
13203 return;
13204 }
13205
13206 mPendingBroadcast = r;
13207 }
13208 }
13209
13210 // =========================================================
13211 // INSTRUMENTATION
13212 // =========================================================
13213
13214 public boolean startInstrumentation(ComponentName className,
13215 String profileFile, int flags, Bundle arguments,
13216 IInstrumentationWatcher watcher) {
13217 // Refuse possible leaked file descriptors
13218 if (arguments != null && arguments.hasFileDescriptors()) {
13219 throw new IllegalArgumentException("File descriptors passed in Bundle");
13220 }
13221
13222 synchronized(this) {
13223 InstrumentationInfo ii = null;
13224 ApplicationInfo ai = null;
13225 try {
13226 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070013227 className, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013228 ai = mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070013229 ii.targetPackage, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013230 } catch (PackageManager.NameNotFoundException e) {
13231 }
13232 if (ii == null) {
13233 reportStartInstrumentationFailure(watcher, className,
13234 "Unable to find instrumentation info for: " + className);
13235 return false;
13236 }
13237 if (ai == null) {
13238 reportStartInstrumentationFailure(watcher, className,
13239 "Unable to find instrumentation target package: " + ii.targetPackage);
13240 return false;
13241 }
13242
13243 int match = mContext.getPackageManager().checkSignatures(
13244 ii.targetPackage, ii.packageName);
13245 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
13246 String msg = "Permission Denial: starting instrumentation "
13247 + className + " from pid="
13248 + Binder.getCallingPid()
13249 + ", uid=" + Binder.getCallingPid()
13250 + " not allowed because package " + ii.packageName
13251 + " does not have a signature matching the target "
13252 + ii.targetPackage;
13253 reportStartInstrumentationFailure(watcher, className, msg);
13254 throw new SecurityException(msg);
13255 }
13256
13257 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080013258 forceStopPackageLocked(ii.targetPackage, -1, true, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013259 ProcessRecord app = addAppLocked(ai);
13260 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070013261 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013262 app.instrumentationProfileFile = profileFile;
13263 app.instrumentationArguments = arguments;
13264 app.instrumentationWatcher = watcher;
13265 app.instrumentationResultClass = className;
13266 Binder.restoreCallingIdentity(origId);
13267 }
13268
13269 return true;
13270 }
13271
13272 /**
13273 * Report errors that occur while attempting to start Instrumentation. Always writes the
13274 * error to the logs, but if somebody is watching, send the report there too. This enables
13275 * the "am" command to report errors with more information.
13276 *
13277 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
13278 * @param cn The component name of the instrumentation.
13279 * @param report The error report.
13280 */
13281 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
13282 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013283 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013284 try {
13285 if (watcher != null) {
13286 Bundle results = new Bundle();
13287 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
13288 results.putString("Error", report);
13289 watcher.instrumentationStatus(cn, -1, results);
13290 }
13291 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013292 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013293 }
13294 }
13295
13296 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
13297 if (app.instrumentationWatcher != null) {
13298 try {
13299 // NOTE: IInstrumentationWatcher *must* be oneway here
13300 app.instrumentationWatcher.instrumentationFinished(
13301 app.instrumentationClass,
13302 resultCode,
13303 results);
13304 } catch (RemoteException e) {
13305 }
13306 }
13307 app.instrumentationWatcher = null;
13308 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070013309 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013310 app.instrumentationProfileFile = null;
13311 app.instrumentationArguments = null;
13312
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080013313 forceStopPackageLocked(app.processName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013314 }
13315
13316 public void finishInstrumentation(IApplicationThread target,
13317 int resultCode, Bundle results) {
13318 // Refuse possible leaked file descriptors
13319 if (results != null && results.hasFileDescriptors()) {
13320 throw new IllegalArgumentException("File descriptors passed in Intent");
13321 }
13322
13323 synchronized(this) {
13324 ProcessRecord app = getRecordForAppLocked(target);
13325 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013326 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013327 return;
13328 }
13329 final long origId = Binder.clearCallingIdentity();
13330 finishInstrumentationLocked(app, resultCode, results);
13331 Binder.restoreCallingIdentity(origId);
13332 }
13333 }
13334
13335 // =========================================================
13336 // CONFIGURATION
13337 // =========================================================
13338
13339 public ConfigurationInfo getDeviceConfigurationInfo() {
13340 ConfigurationInfo config = new ConfigurationInfo();
13341 synchronized (this) {
13342 config.reqTouchScreen = mConfiguration.touchscreen;
13343 config.reqKeyboardType = mConfiguration.keyboard;
13344 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070013345 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
13346 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013347 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
13348 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070013349 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
13350 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013351 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
13352 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070013353 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013354 }
13355 return config;
13356 }
13357
13358 public Configuration getConfiguration() {
13359 Configuration ci;
13360 synchronized(this) {
13361 ci = new Configuration(mConfiguration);
13362 }
13363 return ci;
13364 }
13365
13366 public void updateConfiguration(Configuration values) {
13367 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
13368 "updateConfiguration()");
13369
13370 synchronized(this) {
13371 if (values == null && mWindowManager != null) {
13372 // sentinel: fetch the current configuration from the window manager
13373 values = mWindowManager.computeNewConfiguration();
13374 }
13375
13376 final long origId = Binder.clearCallingIdentity();
13377 updateConfigurationLocked(values, null);
13378 Binder.restoreCallingIdentity(origId);
13379 }
13380 }
13381
13382 /**
13383 * Do either or both things: (1) change the current configuration, and (2)
13384 * make sure the given activity is running with the (now) current
13385 * configuration. Returns true if the activity has been left running, or
13386 * false if <var>starting</var> is being destroyed to match the new
13387 * configuration.
13388 */
13389 public boolean updateConfigurationLocked(Configuration values,
13390 HistoryRecord starting) {
13391 int changes = 0;
13392
13393 boolean kept = true;
13394
13395 if (values != null) {
13396 Configuration newConfig = new Configuration(mConfiguration);
13397 changes = newConfig.updateFrom(values);
13398 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013399 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013400 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013401 }
13402
Doug Zongker2bec3d42009-12-04 12:52:44 -080013403 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013404
13405 if (values.locale != null) {
13406 saveLocaleLocked(values.locale,
13407 !values.locale.equals(mConfiguration.locale),
13408 values.userSetLocale);
13409 }
13410
Dianne Hackborne36d6e22010-02-17 19:46:25 -080013411 mConfigurationSeq++;
13412 if (mConfigurationSeq <= 0) {
13413 mConfigurationSeq = 1;
13414 }
13415 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013416 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080013417 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080013418
13419 AttributeCache ac = AttributeCache.instance();
13420 if (ac != null) {
13421 ac.updateConfiguration(mConfiguration);
13422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013423
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080013424 if (Settings.System.hasInterestingConfigurationChanges(changes)) {
13425 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
13426 msg.obj = new Configuration(mConfiguration);
13427 mHandler.sendMessage(msg);
13428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013429
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013430 for (int i=mLruProcesses.size()-1; i>=0; i--) {
13431 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013432 try {
13433 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013434 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013435 + app.processName + " new config " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013436 app.thread.scheduleConfigurationChanged(mConfiguration);
13437 }
13438 } catch (Exception e) {
13439 }
13440 }
13441 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080013442 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
13443 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013444 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
13445 null, false, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080013446 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
13447 broadcastIntentLocked(null, null,
13448 new Intent(Intent.ACTION_LOCALE_CHANGED),
13449 null, null, 0, null, null,
13450 null, false, false, MY_PID, Process.SYSTEM_UID);
13451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013452 }
13453 }
13454
13455 if (changes != 0 && starting == null) {
13456 // If the configuration changed, and the caller is not already
13457 // in the process of starting an activity, then find the top
13458 // activity to check if its configuration needs to change.
13459 starting = topRunningActivityLocked(null);
13460 }
13461
13462 if (starting != null) {
13463 kept = ensureActivityConfigurationLocked(starting, changes);
13464 if (kept) {
13465 // If this didn't result in the starting activity being
13466 // destroyed, then we need to make sure at this point that all
13467 // other activities are made visible.
Joe Onorato8a9b2202010-02-26 18:56:32 -080013468 if (DEBUG_SWITCH) Slog.i(TAG, "Config didn't destroy " + starting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013469 + ", ensuring others are correct.");
13470 ensureActivitiesVisibleLocked(starting, changes);
13471 }
13472 }
13473
Dianne Hackborne36d6e22010-02-17 19:46:25 -080013474 if (values != null && mWindowManager != null) {
13475 mWindowManager.setNewConfiguration(mConfiguration);
13476 }
13477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013478 return kept;
13479 }
13480
13481 private final boolean relaunchActivityLocked(HistoryRecord r,
13482 int changes, boolean andResume) {
13483 List<ResultInfo> results = null;
13484 List<Intent> newIntents = null;
13485 if (andResume) {
13486 results = r.results;
13487 newIntents = r.newIntents;
13488 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080013489 if (DEBUG_SWITCH) Slog.v(TAG, "Relaunching: " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013490 + " with results=" + results + " newIntents=" + newIntents
13491 + " andResume=" + andResume);
Doug Zongker2bec3d42009-12-04 12:52:44 -080013492 EventLog.writeEvent(andResume ? EventLogTags.AM_RELAUNCH_RESUME_ACTIVITY
13493 : EventLogTags.AM_RELAUNCH_ACTIVITY, System.identityHashCode(r),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013494 r.task.taskId, r.shortComponentName);
13495
13496 r.startFreezingScreenLocked(r.app, 0);
13497
13498 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013499 if (DEBUG_SWITCH) Slog.i(TAG, "Switch is restarting resumed " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013500 r.app.thread.scheduleRelaunchActivity(r, results, newIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -080013501 changes, !andResume, mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013502 // Note: don't need to call pauseIfSleepingLocked() here, because
13503 // the caller will only pass in 'andResume' if this activity is
13504 // currently resumed, which implies we aren't sleeping.
13505 } catch (RemoteException e) {
13506 return false;
13507 }
13508
13509 if (andResume) {
13510 r.results = null;
13511 r.newIntents = null;
Dianne Hackborn1bcf5a82009-09-30 15:22:29 -070013512 reportResumedActivityLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013513 }
13514
13515 return true;
13516 }
13517
13518 /**
13519 * Make sure the given activity matches the current configuration. Returns
13520 * false if the activity had to be destroyed. Returns true if the
13521 * configuration is the same, or the activity will remain running as-is
13522 * for whatever reason. Ensures the HistoryRecord is updated with the
13523 * correct configuration and all other bookkeeping is handled.
13524 */
13525 private final boolean ensureActivityConfigurationLocked(HistoryRecord r,
13526 int globalChanges) {
Dianne Hackborne2522462010-03-29 18:41:30 -070013527 if (mConfigWillChange) {
Dianne Hackbornd49258f2010-03-26 00:44:29 -070013528 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
13529 "Skipping config check (will change): " + r);
13530 return true;
13531 }
13532
Joe Onorato8a9b2202010-02-26 18:56:32 -080013533 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013534 "Ensuring correct configuration: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013535
13536 // Short circuit: if the two configurations are the exact same
13537 // object (the common case), then there is nothing to do.
13538 Configuration newConfig = mConfiguration;
13539 if (r.configuration == newConfig) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013540 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013541 "Configuration unchanged in " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013542 return true;
13543 }
13544
13545 // We don't worry about activities that are finishing.
13546 if (r.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013547 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013548 "Configuration doesn't matter in finishing " + r);
13549 r.stopFreezingScreenLocked(false);
13550 return true;
13551 }
13552
13553 // Okay we now are going to make this activity have the new config.
13554 // But then we need to figure out how it needs to deal with that.
13555 Configuration oldConfig = r.configuration;
13556 r.configuration = newConfig;
13557
13558 // If the activity isn't currently running, just leave the new
13559 // configuration and it will pick that up next time it starts.
13560 if (r.app == null || r.app.thread == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013561 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013562 "Configuration doesn't matter not running " + r);
13563 r.stopFreezingScreenLocked(false);
13564 return true;
13565 }
13566
13567 // If the activity isn't persistent, there is a chance we will
13568 // need to restart it.
13569 if (!r.persistent) {
13570
13571 // Figure out what has changed between the two configurations.
13572 int changes = oldConfig.diff(newConfig);
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013573 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013574 Slog.v(TAG, "Checking to restart " + r.info.name + ": changed=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013575 + Integer.toHexString(changes) + ", handles=0x"
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013576 + Integer.toHexString(r.info.configChanges)
13577 + ", newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013578 }
13579 if ((changes&(~r.info.configChanges)) != 0) {
13580 // Aha, the activity isn't handling the change, so DIE DIE DIE.
13581 r.configChangeFlags |= changes;
13582 r.startFreezingScreenLocked(r.app, globalChanges);
13583 if (r.app == null || r.app.thread == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013584 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013585 "Switch is destroying non-running " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013586 destroyActivityLocked(r, true);
13587 } else if (r.state == ActivityState.PAUSING) {
13588 // A little annoying: we are waiting for this activity to
13589 // finish pausing. Let's not do anything now, but just
13590 // flag that it needs to be restarted when done pausing.
Joe Onorato8a9b2202010-02-26 18:56:32 -080013591 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013592 "Switch is skipping already pausing " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013593 r.configDestroy = true;
13594 return true;
13595 } else if (r.state == ActivityState.RESUMED) {
13596 // Try to optimize this case: the configuration is changing
13597 // and we need to restart the top, resumed activity.
13598 // Instead of doing the normal handshaking, just say
13599 // "restart!".
Joe Onorato8a9b2202010-02-26 18:56:32 -080013600 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013601 "Switch is restarting resumed " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013602 relaunchActivityLocked(r, r.configChangeFlags, true);
13603 r.configChangeFlags = 0;
13604 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013605 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG,
Dianne Hackborndc6b6352009-09-30 14:20:09 -070013606 "Switch is restarting non-resumed " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013607 relaunchActivityLocked(r, r.configChangeFlags, false);
13608 r.configChangeFlags = 0;
13609 }
13610
13611 // All done... tell the caller we weren't able to keep this
13612 // activity around.
13613 return false;
13614 }
13615 }
13616
13617 // Default case: the activity can handle this new configuration, so
13618 // hand it over. Note that we don't need to give it the new
13619 // configuration, since we always send configuration changes to all
13620 // process when they happen so it can just use whatever configuration
13621 // it last got.
13622 if (r.app != null && r.app.thread != null) {
13623 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013624 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending new config to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013625 r.app.thread.scheduleActivityConfigurationChanged(r);
13626 } catch (RemoteException e) {
13627 // If process died, whatever.
13628 }
13629 }
13630 r.stopFreezingScreenLocked(false);
13631
13632 return true;
13633 }
13634
13635 /**
13636 * Save the locale. You must be inside a synchronized (this) block.
13637 */
13638 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
13639 if(isDiff) {
13640 SystemProperties.set("user.language", l.getLanguage());
13641 SystemProperties.set("user.region", l.getCountry());
13642 }
13643
13644 if(isPersist) {
13645 SystemProperties.set("persist.sys.language", l.getLanguage());
13646 SystemProperties.set("persist.sys.country", l.getCountry());
13647 SystemProperties.set("persist.sys.localevar", l.getVariant());
13648 }
13649 }
13650
13651 // =========================================================
13652 // LIFETIME MANAGEMENT
13653 // =========================================================
13654
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013655 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
13656 ProcessRecord TOP_APP, boolean recursed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013657 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013658 // This adjustment has already been computed. If we are calling
13659 // from the top, we may have already computed our adjustment with
13660 // an earlier hidden adjustment that isn't really for us... if
13661 // so, use the new hidden adjustment.
13662 if (!recursed && app.hidden) {
13663 app.curAdj = hiddenAdj;
13664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013665 return app.curAdj;
13666 }
13667
13668 if (app.thread == null) {
13669 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013670 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013671 return (app.curAdj=EMPTY_APP_ADJ);
13672 }
13673
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013674 if (app.maxAdj <= FOREGROUND_APP_ADJ) {
13675 // The max adjustment doesn't allow this app to be anything
13676 // below foreground, so it is not worth doing work for it.
13677 app.adjType = "fixed";
13678 app.adjSeq = mAdjSeq;
13679 app.curRawAdj = app.maxAdj;
13680 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
13681 return (app.curAdj=app.maxAdj);
13682 }
13683
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070013684 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013685 app.adjSource = null;
13686 app.adjTarget = null;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013687 app.empty = false;
13688 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013689
The Android Open Source Project4df24232009-03-05 14:34:35 -080013690 // Determine the importance of the process, starting with most
13691 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013692 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013693 int schedGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013694 int N;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013695 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013696 // The last app on the list is the foreground app.
13697 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013698 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013699 app.adjType = "top-activity";
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013700 } else if (app.instrumentationClass != null) {
13701 // Don't want to kill running instrumentation.
13702 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013703 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013704 app.adjType = "instrumentation";
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013705 } else if (app.persistentActivities > 0) {
13706 // Special persistent activities... shouldn't be used these days.
13707 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013708 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013709 app.adjType = "persistent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013710 } else if (app.curReceiver != null ||
13711 (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) {
13712 // An app that is currently receiving a broadcast also
13713 // counts as being in the foreground.
13714 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013715 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013716 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013717 } else if (app.executingServices.size() > 0) {
13718 // An app that is currently executing a service callback also
13719 // counts as being in the foreground.
13720 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013721 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013722 app.adjType = "exec-service";
13723 } else if (app.foregroundServices) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013724 // The user is aware of this app, so make it visible.
13725 adj = VISIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013726 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013727 app.adjType = "foreground-service";
13728 } else if (app.forcingToForeground != null) {
13729 // The user is aware of this app, so make it visible.
13730 adj = VISIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013731 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013732 app.adjType = "force-foreground";
13733 app.adjSource = app.forcingToForeground;
The Android Open Source Project4df24232009-03-05 14:34:35 -080013734 } else if (app == mHomeProcess) {
13735 // This process is hosting what we currently consider to be the
13736 // home app, so we don't want to let it go into the background.
13737 adj = HOME_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013738 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013739 app.adjType = "home";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013740 } else if ((N=app.activities.size()) != 0) {
13741 // This app is in the background with paused activities.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013742 app.hidden = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013743 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013744 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013745 app.adjType = "bg-activities";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013746 N = app.activities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013747 for (int j=0; j<N; j++) {
13748 if (((HistoryRecord)app.activities.get(j)).visible) {
13749 // This app has a visible activity!
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013750 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013751 adj = VISIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013752 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013753 app.adjType = "visible";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013754 break;
13755 }
13756 }
13757 } else {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013758 // A very not-needed process. If this is lower in the lru list,
13759 // we will push it in to the empty bucket.
13760 app.hidden = true;
13761 app.empty = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013762 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013763 adj = hiddenAdj;
13764 app.adjType = "bg-empty";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013765 }
13766
Joe Onorato8a9b2202010-02-26 18:56:32 -080013767 //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj);
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013768
The Android Open Source Project4df24232009-03-05 14:34:35 -080013769 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 // there are applications dependent on our services or providers, but
13771 // this gives us a baseline and makes sure we don't get into an
13772 // infinite recursion.
13773 app.adjSeq = mAdjSeq;
13774 app.curRawAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013775
Christopher Tate6fa95972009-06-05 18:43:55 -070013776 if (mBackupTarget != null && app == mBackupTarget.app) {
13777 // If possible we want to avoid killing apps while they're being backed up
13778 if (adj > BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013779 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Christopher Tate6fa95972009-06-05 18:43:55 -070013780 adj = BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013781 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013782 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070013783 }
13784 }
13785
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013786 if (app.services.size() != 0 && (adj > FOREGROUND_APP_ADJ
13787 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013788 final long now = SystemClock.uptimeMillis();
13789 // This process is more important if the top activity is
13790 // bound to the service.
13791 Iterator jt = app.services.iterator();
13792 while (jt.hasNext() && adj > FOREGROUND_APP_ADJ) {
13793 ServiceRecord s = (ServiceRecord)jt.next();
13794 if (s.startRequested) {
13795 if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) {
13796 // This service has seen some activity within
13797 // recent memory, so we will keep its process ahead
13798 // of the background processes.
13799 if (adj > SECONDARY_SERVER_ADJ) {
13800 adj = SECONDARY_SERVER_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013801 app.adjType = "started-services";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013802 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013803 }
13804 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080013805 // If we have let the service slide into the background
13806 // state, still have some text describing what it is doing
13807 // even though the service no longer has an impact.
13808 if (adj > SECONDARY_SERVER_ADJ) {
13809 app.adjType = "started-bg-services";
13810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013811 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013812 if (s.connections.size() > 0 && (adj > FOREGROUND_APP_ADJ
13813 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013814 Iterator<ConnectionRecord> kt
13815 = s.connections.values().iterator();
13816 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
13817 // XXX should compute this based on the max of
13818 // all connected clients.
13819 ConnectionRecord cr = kt.next();
The Android Open Source Project10592532009-03-18 17:39:46 -070013820 if (cr.binding.client == app) {
13821 // Binding to ourself is not interesting.
13822 continue;
13823 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013824 if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) {
13825 ProcessRecord client = cr.binding.client;
13826 int myHiddenAdj = hiddenAdj;
13827 if (myHiddenAdj > client.hiddenAdj) {
13828 if (client.hiddenAdj > VISIBLE_APP_ADJ) {
13829 myHiddenAdj = client.hiddenAdj;
13830 } else {
13831 myHiddenAdj = VISIBLE_APP_ADJ;
13832 }
13833 }
13834 int clientAdj = computeOomAdjLocked(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013835 client, myHiddenAdj, TOP_APP, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013836 if (adj > clientAdj) {
13837 adj = clientAdj > VISIBLE_APP_ADJ
13838 ? clientAdj : VISIBLE_APP_ADJ;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013839 if (!client.hidden) {
13840 app.hidden = false;
13841 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013842 app.adjType = "service";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070013843 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
13844 .REASON_SERVICE_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013845 app.adjSource = cr.binding.client;
13846 app.adjTarget = s.serviceInfo.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013847 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013848 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
13849 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
13850 schedGroup = Process.THREAD_GROUP_DEFAULT;
13851 }
13852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013853 }
13854 HistoryRecord a = cr.activity;
13855 //if (a != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080013856 // Slog.i(TAG, "Connection to " + a ": state=" + a.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013857 //}
13858 if (a != null && adj > FOREGROUND_APP_ADJ &&
13859 (a.state == ActivityState.RESUMED
13860 || a.state == ActivityState.PAUSING)) {
13861 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013862 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013863 app.hidden = false;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013864 app.adjType = "service";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070013865 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
13866 .REASON_SERVICE_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013867 app.adjSource = a;
13868 app.adjTarget = s.serviceInfo.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013869 }
13870 }
13871 }
13872 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070013873
13874 // Finally, f this process has active services running in it, we
13875 // would like to avoid killing it unless it would prevent the current
13876 // application from running. By default we put the process in
13877 // with the rest of the background processes; as we scan through
13878 // its services we may bump it up from there.
13879 if (adj > hiddenAdj) {
13880 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013881 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070013882 app.adjType = "bg-services";
13883 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013884 }
13885
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013886 if (app.pubProviders.size() != 0 && (adj > FOREGROUND_APP_ADJ
13887 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013888 Iterator jt = app.pubProviders.values().iterator();
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013889 while (jt.hasNext() && (adj > FOREGROUND_APP_ADJ
13890 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013891 ContentProviderRecord cpr = (ContentProviderRecord)jt.next();
13892 if (cpr.clients.size() != 0) {
13893 Iterator<ProcessRecord> kt = cpr.clients.iterator();
13894 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
13895 ProcessRecord client = kt.next();
The Android Open Source Project10592532009-03-18 17:39:46 -070013896 if (client == app) {
13897 // Being our own client is not interesting.
13898 continue;
13899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013900 int myHiddenAdj = hiddenAdj;
13901 if (myHiddenAdj > client.hiddenAdj) {
13902 if (client.hiddenAdj > FOREGROUND_APP_ADJ) {
13903 myHiddenAdj = client.hiddenAdj;
13904 } else {
13905 myHiddenAdj = FOREGROUND_APP_ADJ;
13906 }
13907 }
13908 int clientAdj = computeOomAdjLocked(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013909 client, myHiddenAdj, TOP_APP, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910 if (adj > clientAdj) {
13911 adj = clientAdj > FOREGROUND_APP_ADJ
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013912 ? clientAdj : FOREGROUND_APP_ADJ;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013913 if (!client.hidden) {
13914 app.hidden = false;
13915 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013916 app.adjType = "provider";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070013917 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
13918 .REASON_PROVIDER_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013919 app.adjSource = client;
13920 app.adjTarget = cpr.info.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013921 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013922 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
13923 schedGroup = Process.THREAD_GROUP_DEFAULT;
13924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013925 }
13926 }
13927 // If the provider has external (non-framework) process
13928 // dependencies, ensure that its adjustment is at least
13929 // FOREGROUND_APP_ADJ.
13930 if (cpr.externals != 0) {
13931 if (adj > FOREGROUND_APP_ADJ) {
13932 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013933 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080013934 app.hidden = false;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070013935 app.adjType = "provider";
13936 app.adjTarget = cpr.info.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013937 }
13938 }
13939 }
13940 }
13941
13942 app.curRawAdj = adj;
13943
Joe Onorato8a9b2202010-02-26 18:56:32 -080013944 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013945 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
13946 if (adj > app.maxAdj) {
13947 adj = app.maxAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013948 if (app.maxAdj <= VISIBLE_APP_ADJ) {
13949 schedGroup = Process.THREAD_GROUP_DEFAULT;
13950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013951 }
13952
13953 app.curAdj = adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080013954 app.curSchedGroup = schedGroup;
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070013955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013956 return adj;
13957 }
13958
13959 /**
13960 * Ask a given process to GC right now.
13961 */
13962 final void performAppGcLocked(ProcessRecord app) {
13963 try {
13964 app.lastRequestedGc = SystemClock.uptimeMillis();
13965 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070013966 if (app.reportLowMemory) {
13967 app.reportLowMemory = false;
13968 app.thread.scheduleLowMemory();
13969 } else {
13970 app.thread.processInBackground();
13971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013972 }
13973 } catch (Exception e) {
13974 // whatever.
13975 }
13976 }
13977
13978 /**
13979 * Returns true if things are idle enough to perform GCs.
13980 */
Josh Bartel7f208742010-02-25 11:01:44 -060013981 private final boolean canGcNowLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013982 return mParallelBroadcasts.size() == 0
13983 && mOrderedBroadcasts.size() == 0
13984 && (mSleeping || (mResumedActivity != null &&
13985 mResumedActivity.idle));
13986 }
13987
13988 /**
13989 * Perform GCs on all processes that are waiting for it, but only
13990 * if things are idle.
13991 */
13992 final void performAppGcsLocked() {
13993 final int N = mProcessesToGc.size();
13994 if (N <= 0) {
13995 return;
13996 }
Josh Bartel7f208742010-02-25 11:01:44 -060013997 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013998 while (mProcessesToGc.size() > 0) {
13999 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070014000 if (proc.curRawAdj > VISIBLE_APP_ADJ || proc.reportLowMemory) {
14001 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
14002 <= SystemClock.uptimeMillis()) {
14003 // To avoid spamming the system, we will GC processes one
14004 // at a time, waiting a few seconds between each.
14005 performAppGcLocked(proc);
14006 scheduleAppGcsLocked();
14007 return;
14008 } else {
14009 // It hasn't been long enough since we last GCed this
14010 // process... put it in the list to wait for its time.
14011 addProcessToGcListLocked(proc);
14012 break;
14013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014014 }
14015 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070014016
14017 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 }
14019 }
14020
14021 /**
14022 * If all looks good, perform GCs on all processes waiting for them.
14023 */
14024 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060014025 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014026 performAppGcsLocked();
14027 return;
14028 }
14029 // Still not idle, wait some more.
14030 scheduleAppGcsLocked();
14031 }
14032
14033 /**
14034 * Schedule the execution of all pending app GCs.
14035 */
14036 final void scheduleAppGcsLocked() {
14037 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070014038
14039 if (mProcessesToGc.size() > 0) {
14040 // Schedule a GC for the time to the next process.
14041 ProcessRecord proc = mProcessesToGc.get(0);
14042 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
14043
14044 long when = mProcessesToGc.get(0).lastRequestedGc + GC_MIN_INTERVAL;
14045 long now = SystemClock.uptimeMillis();
14046 if (when < (now+GC_TIMEOUT)) {
14047 when = now + GC_TIMEOUT;
14048 }
14049 mHandler.sendMessageAtTime(msg, when);
14050 }
14051 }
14052
14053 /**
14054 * Add a process to the array of processes waiting to be GCed. Keeps the
14055 * list in sorted order by the last GC time. The process can't already be
14056 * on the list.
14057 */
14058 final void addProcessToGcListLocked(ProcessRecord proc) {
14059 boolean added = false;
14060 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
14061 if (mProcessesToGc.get(i).lastRequestedGc <
14062 proc.lastRequestedGc) {
14063 added = true;
14064 mProcessesToGc.add(i+1, proc);
14065 break;
14066 }
14067 }
14068 if (!added) {
14069 mProcessesToGc.add(0, proc);
14070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014071 }
14072
14073 /**
14074 * Set up to ask a process to GC itself. This will either do it
14075 * immediately, or put it on the list of processes to gc the next
14076 * time things are idle.
14077 */
14078 final void scheduleAppGcLocked(ProcessRecord app) {
14079 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070014080 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014081 return;
14082 }
14083 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070014084 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014085 scheduleAppGcsLocked();
14086 }
14087 }
14088
14089 private final boolean updateOomAdjLocked(
14090 ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) {
14091 app.hiddenAdj = hiddenAdj;
14092
14093 if (app.thread == null) {
14094 return true;
14095 }
14096
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014097 int adj = computeOomAdjLocked(app, hiddenAdj, TOP_APP, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014098
Dianne Hackborn09c916b2009-12-08 14:50:51 -080014099 if ((app.pid != 0 && app.pid != MY_PID) || Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014100 if (app.curRawAdj != app.setRawAdj) {
14101 if (app.curRawAdj > FOREGROUND_APP_ADJ
14102 && app.setRawAdj <= FOREGROUND_APP_ADJ) {
14103 // If this app is transitioning from foreground to
14104 // non-foreground, have it do a gc.
14105 scheduleAppGcLocked(app);
14106 } else if (app.curRawAdj >= HIDDEN_APP_MIN_ADJ
14107 && app.setRawAdj < HIDDEN_APP_MIN_ADJ) {
14108 // Likewise do a gc when an app is moving in to the
14109 // background (such as a service stopping).
14110 scheduleAppGcLocked(app);
14111 }
14112 app.setRawAdj = app.curRawAdj;
14113 }
14114 if (adj != app.setAdj) {
14115 if (Process.setOomAdj(app.pid, adj)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080014116 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014117 TAG, "Set app " + app.processName +
14118 " oom adj to " + adj);
14119 app.setAdj = adj;
14120 } else {
14121 return false;
14122 }
14123 }
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070014124 if (app.setSchedGroup != app.curSchedGroup) {
14125 app.setSchedGroup = app.curSchedGroup;
Joe Onorato8a9b2202010-02-26 18:56:32 -080014126 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070014127 "Setting process group of " + app.processName
14128 + " to " + app.curSchedGroup);
14129 if (true) {
San Mehat9438de22009-06-10 09:11:28 -070014130 long oldId = Binder.clearCallingIdentity();
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070014131 try {
14132 Process.setProcessGroup(app.pid, app.curSchedGroup);
14133 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080014134 Slog.w(TAG, "Failed setting process group of " + app.pid
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070014135 + " to " + app.curSchedGroup);
San Mehat9438de22009-06-10 09:11:28 -070014136 e.printStackTrace();
14137 } finally {
14138 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070014139 }
14140 }
14141 if (false) {
14142 if (app.thread != null) {
14143 try {
14144 app.thread.setSchedulingGroup(app.curSchedGroup);
14145 } catch (RemoteException e) {
14146 }
14147 }
14148 }
14149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014150 }
14151
14152 return true;
14153 }
14154
14155 private final HistoryRecord resumedAppLocked() {
14156 HistoryRecord resumedActivity = mResumedActivity;
14157 if (resumedActivity == null || resumedActivity.app == null) {
14158 resumedActivity = mPausingActivity;
14159 if (resumedActivity == null || resumedActivity.app == null) {
14160 resumedActivity = topRunningActivityLocked(null);
14161 }
14162 }
14163 return resumedActivity;
14164 }
14165
14166 private final boolean updateOomAdjLocked(ProcessRecord app) {
14167 final HistoryRecord TOP_ACT = resumedAppLocked();
14168 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
14169 int curAdj = app.curAdj;
14170 final boolean wasHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
14171 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
14172
14173 mAdjSeq++;
14174
14175 final boolean res = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP);
14176 if (res) {
14177 final boolean nowHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
14178 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
14179 if (nowHidden != wasHidden) {
14180 // Changed to/from hidden state, so apps after it in the LRU
14181 // list may also be changed.
14182 updateOomAdjLocked();
14183 }
14184 }
14185 return res;
14186 }
14187
14188 private final boolean updateOomAdjLocked() {
14189 boolean didOomAdj = true;
14190 final HistoryRecord TOP_ACT = resumedAppLocked();
14191 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
14192
14193 if (false) {
14194 RuntimeException e = new RuntimeException();
14195 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080014196 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014197 }
14198
14199 mAdjSeq++;
14200
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080014201 // Let's determine how many processes we have running vs.
14202 // how many slots we have for background processes; we may want
14203 // to put multiple processes in a slot of there are enough of
14204 // them.
14205 int numSlots = HIDDEN_APP_MAX_ADJ - HIDDEN_APP_MIN_ADJ + 1;
14206 int factor = (mLruProcesses.size()-4)/numSlots;
14207 if (factor < 1) factor = 1;
14208 int step = 0;
14209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014210 // First try updating the OOM adjustment for each of the
14211 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014212 int i = mLruProcesses.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014213 int curHiddenAdj = HIDDEN_APP_MIN_ADJ;
14214 while (i > 0) {
14215 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014216 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080014217 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014218 if (updateOomAdjLocked(app, curHiddenAdj, TOP_APP)) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014219 if (curHiddenAdj < EMPTY_APP_ADJ
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220 && app.curAdj == curHiddenAdj) {
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080014221 step++;
14222 if (step >= factor) {
14223 step = 0;
14224 curHiddenAdj++;
14225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014226 }
14227 } else {
14228 didOomAdj = false;
14229 }
14230 }
14231
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014232 // If we return false, we will fall back on killing processes to
14233 // have a fixed limit. Do this if a limit has been requested; else
14234 // only return false if one of the adjustments failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014235 return ENFORCE_PROCESS_LIMIT || mProcessLimit > 0 ? false : didOomAdj;
14236 }
14237
14238 private final void trimApplications() {
14239 synchronized (this) {
14240 int i;
14241
14242 // First remove any unused application processes whose package
14243 // has been removed.
14244 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
14245 final ProcessRecord app = mRemovedProcesses.get(i);
14246 if (app.activities.size() == 0
14247 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080014248 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014249 TAG, "Exiting empty application process "
14250 + app.processName + " ("
14251 + (app.thread != null ? app.thread.asBinder() : null)
14252 + ")\n");
14253 if (app.pid > 0 && app.pid != MY_PID) {
14254 Process.killProcess(app.pid);
14255 } else {
14256 try {
14257 app.thread.scheduleExit();
14258 } catch (Exception e) {
14259 // Ignore exceptions.
14260 }
14261 }
14262 cleanUpApplicationRecordLocked(app, false, -1);
14263 mRemovedProcesses.remove(i);
14264
14265 if (app.persistent) {
14266 if (app.persistent) {
14267 addAppLocked(app.info);
14268 }
14269 }
14270 }
14271 }
14272
14273 // Now try updating the OOM adjustment for each of the
14274 // application processes based on their current state.
14275 // If the setOomAdj() API is not supported, then go with our
14276 // back-up plan...
14277 if (!updateOomAdjLocked()) {
14278
14279 // Count how many processes are running services.
14280 int numServiceProcs = 0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014281 for (i=mLruProcesses.size()-1; i>=0; i--) {
14282 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014283
14284 if (app.persistent || app.services.size() != 0
14285 || app.curReceiver != null
14286 || app.persistentActivities > 0) {
14287 // Don't count processes holding services against our
14288 // maximum process count.
Joe Onorato8a9b2202010-02-26 18:56:32 -080014289 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014290 TAG, "Not trimming app " + app + " with services: "
14291 + app.services);
14292 numServiceProcs++;
14293 }
14294 }
14295
14296 int curMaxProcs = mProcessLimit;
14297 if (curMaxProcs <= 0) curMaxProcs = MAX_PROCESSES;
14298 if (mAlwaysFinishActivities) {
14299 curMaxProcs = 1;
14300 }
14301 curMaxProcs += numServiceProcs;
14302
14303 // Quit as many processes as we can to get down to the desired
14304 // process count. First remove any processes that no longer
14305 // have activites running in them.
14306 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014307 i<mLruProcesses.size()
14308 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014309 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014310 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014311 // Quit an application only if it is not currently
14312 // running any activities.
14313 if (!app.persistent && app.activities.size() == 0
14314 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080014315 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014316 TAG, "Exiting empty application process "
14317 + app.processName + " ("
14318 + (app.thread != null ? app.thread.asBinder() : null)
14319 + ")\n");
14320 if (app.pid > 0 && app.pid != MY_PID) {
14321 Process.killProcess(app.pid);
14322 } else {
14323 try {
14324 app.thread.scheduleExit();
14325 } catch (Exception e) {
14326 // Ignore exceptions.
14327 }
14328 }
14329 // todo: For now we assume the application is not buggy
14330 // or evil, and will quit as a result of our request.
14331 // Eventually we need to drive this off of the death
14332 // notification, and kill the process if it takes too long.
14333 cleanUpApplicationRecordLocked(app, false, i);
14334 i--;
14335 }
14336 }
14337
14338 // If we still have too many processes, now from the least
14339 // recently used process we start finishing activities.
Joe Onorato8a9b2202010-02-26 18:56:32 -080014340 if (Config.LOGV) Slog.v(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014341 TAG, "*** NOW HAVE " + mLruProcesses.size() +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014342 " of " + curMaxProcs + " processes");
14343 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014344 i<mLruProcesses.size()
14345 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014346 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014347 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014348 // Quit the application only if we have a state saved for
14349 // all of its activities.
14350 boolean canQuit = !app.persistent && app.curReceiver == null
14351 && app.services.size() == 0
14352 && app.persistentActivities == 0;
14353 int NUMA = app.activities.size();
14354 int j;
Joe Onorato8a9b2202010-02-26 18:56:32 -080014355 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014356 TAG, "Looking to quit " + app.processName);
14357 for (j=0; j<NUMA && canQuit; j++) {
14358 HistoryRecord r = (HistoryRecord)app.activities.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -080014359 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014360 TAG, " " + r.intent.getComponent().flattenToShortString()
14361 + ": frozen=" + r.haveState + ", visible=" + r.visible);
14362 canQuit = (r.haveState || !r.stateNotNeeded)
14363 && !r.visible && r.stopped;
14364 }
14365 if (canQuit) {
14366 // Finish all of the activities, and then the app itself.
14367 for (j=0; j<NUMA; j++) {
14368 HistoryRecord r = (HistoryRecord)app.activities.get(j);
14369 if (!r.finishing) {
14370 destroyActivityLocked(r, false);
14371 }
14372 r.resultTo = null;
14373 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080014374 Slog.i(TAG, "Exiting application process "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014375 + app.processName + " ("
14376 + (app.thread != null ? app.thread.asBinder() : null)
14377 + ")\n");
14378 if (app.pid > 0 && app.pid != MY_PID) {
14379 Process.killProcess(app.pid);
14380 } else {
14381 try {
14382 app.thread.scheduleExit();
14383 } catch (Exception e) {
14384 // Ignore exceptions.
14385 }
14386 }
14387 // todo: For now we assume the application is not buggy
14388 // or evil, and will quit as a result of our request.
14389 // Eventually we need to drive this off of the death
14390 // notification, and kill the process if it takes too long.
14391 cleanUpApplicationRecordLocked(app, false, i);
14392 i--;
14393 //dump();
14394 }
14395 }
14396
14397 }
14398
14399 int curMaxActivities = MAX_ACTIVITIES;
14400 if (mAlwaysFinishActivities) {
14401 curMaxActivities = 1;
14402 }
14403
14404 // Finally, if there are too many activities now running, try to
14405 // finish as many as we can to get back down to the limit.
14406 for ( i=0;
14407 i<mLRUActivities.size()
14408 && mLRUActivities.size() > curMaxActivities;
14409 i++) {
14410 final HistoryRecord r
14411 = (HistoryRecord)mLRUActivities.get(i);
14412
14413 // We can finish this one if we have its icicle saved and
14414 // it is not persistent.
14415 if ((r.haveState || !r.stateNotNeeded) && !r.visible
14416 && r.stopped && !r.persistent && !r.finishing) {
14417 final int origSize = mLRUActivities.size();
14418 destroyActivityLocked(r, true);
14419
14420 // This will remove it from the LRU list, so keep
14421 // our index at the same value. Note that this check to
14422 // see if the size changes is just paranoia -- if
14423 // something unexpected happens, we don't want to end up
14424 // in an infinite loop.
14425 if (origSize > mLRUActivities.size()) {
14426 i--;
14427 }
14428 }
14429 }
14430 }
14431 }
14432
14433 /** This method sends the specified signal to each of the persistent apps */
14434 public void signalPersistentProcesses(int sig) throws RemoteException {
14435 if (sig != Process.SIGNAL_USR1) {
14436 throw new SecurityException("Only SIGNAL_USR1 is allowed");
14437 }
14438
14439 synchronized (this) {
14440 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
14441 != PackageManager.PERMISSION_GRANTED) {
14442 throw new SecurityException("Requires permission "
14443 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
14444 }
14445
Dianne Hackborndd71fc82009-12-16 19:24:32 -080014446 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
14447 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014448 if (r.thread != null && r.persistent) {
14449 Process.sendSignal(r.pid, sig);
14450 }
14451 }
14452 }
14453 }
14454
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014455 public boolean profileControl(String process, boolean start,
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014456 String path, ParcelFileDescriptor fd) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014457
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014458 try {
14459 synchronized (this) {
14460 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
14461 // its own permission.
14462 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
14463 != PackageManager.PERMISSION_GRANTED) {
14464 throw new SecurityException("Requires permission "
14465 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014466 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014467
14468 if (start && fd == null) {
14469 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014470 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014471
14472 ProcessRecord proc = null;
14473 try {
14474 int pid = Integer.parseInt(process);
14475 synchronized (mPidsSelfLocked) {
14476 proc = mPidsSelfLocked.get(pid);
14477 }
14478 } catch (NumberFormatException e) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014479 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014480
14481 if (proc == null) {
14482 HashMap<String, SparseArray<ProcessRecord>> all
14483 = mProcessNames.getMap();
14484 SparseArray<ProcessRecord> procs = all.get(process);
14485 if (procs != null && procs.size() > 0) {
14486 proc = procs.valueAt(0);
14487 }
14488 }
14489
14490 if (proc == null || proc.thread == null) {
14491 throw new IllegalArgumentException("Unknown process: " + process);
14492 }
14493
14494 boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0"));
14495 if (isSecure) {
14496 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
14497 throw new SecurityException("Process not debuggable: " + proc);
14498 }
14499 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014500
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014501 proc.thread.profilerControl(start, path, fd);
14502 fd = null;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014503 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070014504 }
14505 } catch (RemoteException e) {
14506 throw new IllegalStateException("Process disappeared");
14507 } finally {
14508 if (fd != null) {
14509 try {
14510 fd.close();
14511 } catch (IOException e) {
14512 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080014513 }
14514 }
14515 }
14516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014517 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
14518 public void monitor() {
14519 synchronized (this) { }
14520 }
14521}