blob: 3c8fb017e9a3b14c4cab455ec5612ea96189ad9b [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
Dianne Hackborn860755f2010-06-03 18:47:52 -070019import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.os.BatteryStatsImpl;
Dianne Hackbornde7faf62009-06-30 13:27:30 -070021import com.android.server.AttributeCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import com.android.server.IntentResolver;
23import com.android.server.ProcessMap;
24import com.android.server.ProcessStats;
25import com.android.server.SystemServer;
26import com.android.server.Watchdog;
27import com.android.server.WindowManagerService;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070028import com.android.server.am.ActivityStack.ActivityState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Dianne Hackborndd71fc82009-12-16 19:24:32 -080030import dalvik.system.Zygote;
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.app.Activity;
33import android.app.ActivityManager;
34import android.app.ActivityManagerNative;
35import android.app.ActivityThread;
36import android.app.AlertDialog;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070037import android.app.AppGlobals;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020038import android.app.ApplicationErrorReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.app.Dialog;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070040import android.app.IActivityController;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.app.IActivityWatcher;
42import android.app.IApplicationThread;
43import android.app.IInstrumentationWatcher;
Dianne Hackborn860755f2010-06-03 18:47:52 -070044import android.app.INotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.app.IServiceConnection;
46import android.app.IThumbnailReceiver;
47import android.app.Instrumentation;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070048import android.app.Notification;
Dianne Hackborn860755f2010-06-03 18:47:52 -070049import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.app.PendingIntent;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070051import android.app.Service;
Christopher Tate45281862010-03-05 15:46:30 -080052import android.app.backup.IBackupManager;
Jacek Surazskif5b9c722009-05-18 12:09:59 +020053import android.content.ActivityNotFoundException;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080054import android.content.BroadcastReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.content.ComponentName;
56import android.content.ContentResolver;
57import android.content.Context;
Christian Mehlmauer7664e202010-07-20 08:46:17 +020058import android.content.DialogInterface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.content.Intent;
60import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070061import android.content.IIntentReceiver;
62import android.content.IIntentSender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070063import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.content.pm.ActivityInfo;
65import android.content.pm.ApplicationInfo;
66import android.content.pm.ConfigurationInfo;
67import android.content.pm.IPackageDataObserver;
68import android.content.pm.IPackageManager;
69import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080070import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.content.pm.PackageManager;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070072import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.content.pm.ProviderInfo;
74import android.content.pm.ResolveInfo;
75import android.content.pm.ServiceInfo;
Dianne Hackborn860755f2010-06-03 18:47:52 -070076import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.content.res.Configuration;
78import android.graphics.Bitmap;
Robert Greenwalt434203a2010-10-11 16:00:27 -070079import android.net.Proxy;
80import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.net.Uri;
82import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080083import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080084import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070085import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080086import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080088import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.os.FileUtils;
90import android.os.Handler;
91import android.os.IBinder;
92import android.os.IPermissionController;
93import android.os.Looper;
94import android.os.Message;
95import android.os.Parcel;
96import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070098import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.RemoteException;
100import android.os.ServiceManager;
Brad Fitzpatrick46d42382010-06-11 13:57:58 -0700101import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.os.SystemClock;
103import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.util.Config;
106import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800107import android.util.Slog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800108import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.util.PrintWriterPrinter;
110import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700111import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.Gravity;
113import android.view.LayoutInflater;
114import android.view.View;
115import android.view.WindowManager;
116import android.view.WindowManagerPolicy;
117
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700118import java.io.BufferedInputStream;
119import java.io.BufferedOutputStream;
120import java.io.DataInputStream;
121import java.io.DataOutputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122import java.io.File;
123import java.io.FileDescriptor;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700124import java.io.FileInputStream;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import java.io.FileNotFoundException;
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700126import java.io.FileOutputStream;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200127import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800128import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import java.io.PrintWriter;
130import java.lang.IllegalStateException;
131import java.lang.ref.WeakReference;
132import java.util.ArrayList;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700133import java.util.Collections;
134import java.util.Comparator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.HashMap;
136import java.util.HashSet;
137import java.util.Iterator;
138import java.util.List;
139import java.util.Locale;
140import java.util.Map;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700141import java.util.Set;
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700142import java.util.concurrent.atomic.AtomicBoolean;
143import java.util.concurrent.atomic.AtomicLong;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700145public final class ActivityManagerService extends ActivityManagerNative
146 implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 static final String TAG = "ActivityManager";
148 static final boolean DEBUG = false;
149 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
150 static final boolean DEBUG_SWITCH = localLOGV || false;
151 static final boolean DEBUG_TASKS = localLOGV || false;
152 static final boolean DEBUG_PAUSE = localLOGV || false;
153 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
154 static final boolean DEBUG_TRANSITION = localLOGV || false;
155 static final boolean DEBUG_BROADCAST = localLOGV || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700156 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 static final boolean DEBUG_SERVICE = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700158 static final boolean DEBUG_SERVICE_EXECUTING = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 static final boolean DEBUG_VISBILITY = localLOGV || false;
160 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700161 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800162 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700164 static final boolean DEBUG_RESULTS = localLOGV || false;
Christopher Tate436344a2009-09-30 16:17:37 -0700165 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700166 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700167 static final boolean DEBUG_POWER = localLOGV || false;
168 static final boolean DEBUG_POWER_QUICK = DEBUG_POWER || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 static final boolean VALIDATE_TOKENS = false;
170 static final boolean SHOW_ACTIVITY_START_TIME = true;
171
172 // Control over CPU and battery monitoring.
173 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
174 static final boolean MONITOR_CPU_USAGE = true;
175 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
176 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
177 static final boolean MONITOR_THREAD_CPU_USAGE = false;
178
Dianne Hackborn1655be42009-05-08 14:29:01 -0700179 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700180 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private static final String SYSTEM_SECURE = "ro.secure";
183
184 // This is the maximum number of application processes we would like
185 // to have running. Due to the asynchronous nature of things, we can
186 // temporarily go beyond this limit.
187 static final int MAX_PROCESSES = 2;
188
189 // Set to false to leave processes running indefinitely, relying on
190 // the kernel killing them as resources are required.
191 static final boolean ENFORCE_PROCESS_LIMIT = false;
192
193 // This is the maximum number of activities that we would like to have
194 // running at a given time.
195 static final int MAX_ACTIVITIES = 20;
196
197 // Maximum number of recent tasks that we can remember.
198 static final int MAX_RECENT_TASKS = 20;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700199
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700200 // Amount of time after a call to stopAppSwitches() during which we will
201 // prevent further untrusted switches from happening.
202 static final long APP_SWITCH_DELAY_TIME = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
204 // How long we wait for a launched process to attach to the activity manager
205 // before we decide it's never going to come up for real.
206 static final int PROC_START_TIMEOUT = 10*1000;
207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 // 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
Dianne Hackborn287952c2010-09-22 22:34:31 -0700214 // The rate at which we check for apps using excessive power -- 15 mins.
215 static final int POWER_CHECK_DELAY = (DEBUG_POWER_QUICK ? 2 : 15) * 60*1000;
216
217 // The minimum sample duration we will allow before deciding we have
218 // enough data on wake locks to start killing things.
219 static final int WAKE_LOCK_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
220
221 // The minimum sample duration we will allow before deciding we have
222 // enough data on CPU usage to start killing things.
223 static final int CPU_MIN_CHECK_DURATION = (DEBUG_POWER_QUICK ? 1 : 5) * 60*1000;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // How long we allow a receiver to run before giving up on it.
226 static final int BROADCAST_TIMEOUT = 10*1000;
227
228 // How long we wait for a service to finish executing.
229 static final int SERVICE_TIMEOUT = 20*1000;
230
231 // How long a service needs to be running until restarting its process
232 // is no longer considered to be a relaunch of the service.
233 static final int SERVICE_RESTART_DURATION = 5*1000;
234
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700235 // How long a service needs to be running until it will start back at
236 // SERVICE_RESTART_DURATION after being killed.
237 static final int SERVICE_RESET_RUN_DURATION = 60*1000;
238
239 // Multiplying factor to increase restart duration time by, for each time
240 // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
241 static final int SERVICE_RESTART_DURATION_FACTOR = 4;
242
243 // The minimum amount of time between restarting services that we allow.
244 // That is, when multiple services are restarting, we won't allow each
245 // to restart less than this amount of time from the last one.
246 static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 // Maximum amount of time for there to be no activity on a service before
249 // we consider it non-essential and allow its process to go on the
250 // LRU background list.
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700251 static final int MAX_SERVICE_INACTIVITY = 30*60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 // How long we wait until we timeout on key dispatching.
254 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
255
256 // The minimum time we allow between crashes, for us to consider this
257 // application to be bad and stop and its services and reject broadcasts.
258 static final int MIN_CRASH_INTERVAL = 60*1000;
259
260 // How long we wait until we timeout on key dispatching during instrumentation.
261 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
262
263 // OOM adjustments for processes in various states:
264
265 // This is a process without anything currently running in it. Definitely
266 // the first to go! Value set in system/rootdir/init.rc on startup.
267 // This value is initalized in the constructor, careful when refering to
268 // this static variable externally.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800269 static final int EMPTY_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270
271 // This is a process only hosting activities that are not visible,
272 // so it can be killed without any disruption. Value set in
273 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800274 static final int HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 static int HIDDEN_APP_MIN_ADJ;
276
The Android Open Source Project4df24232009-03-05 14:34:35 -0800277 // This is a process holding the home application -- we want to try
278 // avoiding killing it, even if it would normally be in the background,
279 // because the user interacts with it so much.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800280 static final int HOME_APP_ADJ;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800281
Christopher Tate6fa95972009-06-05 18:43:55 -0700282 // This is a process currently hosting a backup operation. Killing it
283 // is not entirely fatal but is generally a bad idea.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800284 static final int BACKUP_APP_ADJ;
Christopher Tate6fa95972009-06-05 18:43:55 -0700285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 // This is a process holding a secondary server -- killing it will not
287 // have much of an impact as far as the user is concerned. Value set in
288 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800289 static final int SECONDARY_SERVER_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700291 // This is a process with a heavy-weight application. It is in the
292 // background, but we want to try to avoid killing it. Value set in
293 // system/rootdir/init.rc on startup.
294 static final int HEAVY_WEIGHT_APP_ADJ;
295
296 // This is a process only hosting components that are perceptible to the
297 // user, and we really want to avoid killing them, but they are not
298 // immediately visible. An example is background music playback. Value set in
299 // system/rootdir/init.rc on startup.
300 static final int PERCEPTIBLE_APP_ADJ;
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 // This is a process only hosting activities that are visible to the
303 // user, so we'd prefer they don't disappear. Value set in
304 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800305 static final int VISIBLE_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
307 // This is the process running the current foreground app. We'd really
308 // rather not kill it! Value set in system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800309 static final int FOREGROUND_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310
311 // This is a process running a core server, such as telephony. Definitely
312 // don't want to kill it, but doing so is not completely fatal.
313 static final int CORE_SERVER_ADJ = -12;
314
315 // The system process runs at the default adjustment.
316 static final int SYSTEM_ADJ = -16;
317
318 // Memory pages are 4K.
319 static final int PAGE_SIZE = 4*1024;
320
321 // Corresponding memory levels for above adjustments.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800322 static final int EMPTY_APP_MEM;
323 static final int HIDDEN_APP_MEM;
324 static final int HOME_APP_MEM;
325 static final int BACKUP_APP_MEM;
326 static final int SECONDARY_SERVER_MEM;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700327 static final int HEAVY_WEIGHT_APP_MEM;
328 static final int PERCEPTIBLE_APP_MEM;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800329 static final int VISIBLE_APP_MEM;
330 static final int FOREGROUND_APP_MEM;
331
332 // The minimum number of hidden apps we want to be able to keep around,
333 // without empty apps being able to push them out of memory.
334 static final int MIN_HIDDEN_APPS = 2;
335
Dianne Hackborn8633e682010-04-22 16:03:41 -0700336 // The maximum number of hidden processes we will keep around before
337 // killing them; this is just a control to not let us go too crazy with
338 // keeping around processes on devices with large amounts of RAM.
339 static final int MAX_HIDDEN_APPS = 15;
340
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800341 // We put empty content processes after any hidden processes that have
Dianne Hackborn906497c2010-05-10 15:57:38 -0700342 // been idle for less than 15 seconds.
343 static final long CONTENT_APP_IDLE_OFFSET = 15*1000;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800344
345 // We put empty content processes after any hidden processes that have
Dianne Hackborn906497c2010-05-10 15:57:38 -0700346 // been idle for less than 120 seconds.
347 static final long EMPTY_APP_IDLE_OFFSET = 120*1000;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800348
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700349 static int getIntProp(String name, boolean allowZero) {
350 String str = SystemProperties.get(name);
351 if (str == null) {
352 throw new IllegalArgumentException("Property not defined: " + name);
353 }
354 int val = Integer.valueOf(str);
355 if (val == 0 && !allowZero) {
356 throw new IllegalArgumentException("Property must not be zero: " + name);
357 }
358 return val;
359 }
360
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800361 static {
362 // These values are set in system/rootdir/init.rc on startup.
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700363 FOREGROUND_APP_ADJ = getIntProp("ro.FOREGROUND_APP_ADJ", true);
364 VISIBLE_APP_ADJ = getIntProp("ro.VISIBLE_APP_ADJ", true);
365 PERCEPTIBLE_APP_ADJ = getIntProp("ro.PERCEPTIBLE_APP_ADJ", true);
366 HEAVY_WEIGHT_APP_ADJ = getIntProp("ro.HEAVY_WEIGHT_APP_ADJ", true);
367 SECONDARY_SERVER_ADJ = getIntProp("ro.SECONDARY_SERVER_ADJ", true);
368 BACKUP_APP_ADJ = getIntProp("ro.BACKUP_APP_ADJ", true);
369 HOME_APP_ADJ = getIntProp("ro.HOME_APP_ADJ", true);
370 HIDDEN_APP_MIN_ADJ = getIntProp("ro.HIDDEN_APP_MIN_ADJ", true);
371 EMPTY_APP_ADJ = getIntProp("ro.EMPTY_APP_ADJ", true);
372 // These days we use the last empty slot for hidden apps as well.
373 HIDDEN_APP_MAX_ADJ = EMPTY_APP_ADJ;
374 FOREGROUND_APP_MEM = getIntProp("ro.FOREGROUND_APP_MEM", false)*PAGE_SIZE;
375 VISIBLE_APP_MEM = getIntProp("ro.VISIBLE_APP_MEM", false)*PAGE_SIZE;
376 PERCEPTIBLE_APP_MEM = getIntProp("ro.PERCEPTIBLE_APP_MEM", false)*PAGE_SIZE;
377 HEAVY_WEIGHT_APP_MEM = getIntProp("ro.HEAVY_WEIGHT_APP_MEM", false)*PAGE_SIZE;
378 SECONDARY_SERVER_MEM = getIntProp("ro.SECONDARY_SERVER_MEM", false)*PAGE_SIZE;
379 BACKUP_APP_MEM = getIntProp("ro.BACKUP_APP_MEM", false)*PAGE_SIZE;
380 HOME_APP_MEM = getIntProp("ro.HOME_APP_MEM", false)*PAGE_SIZE;
381 HIDDEN_APP_MEM = getIntProp("ro.HIDDEN_APP_MEM", false)*PAGE_SIZE;
382 EMPTY_APP_MEM = getIntProp("ro.EMPTY_APP_MEM", false)*PAGE_SIZE;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384
Dan Egnor42471dd2010-01-07 17:25:22 -0800385 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
387 static final String[] EMPTY_STRING_ARRAY = new String[0];
388
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700389 public ActivityStack mMainStack;
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700392 * Description of a request to start a new activity, which has been held
393 * due to app switches being disabled.
394 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700395 static class PendingActivityLaunch {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700396 ActivityRecord r;
397 ActivityRecord sourceRecord;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700398 Uri[] grantedUriPermissions;
399 int grantedMode;
400 boolean onlyIfNeeded;
401 }
402
403 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
404 = new ArrayList<PendingActivityLaunch>();
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 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 * Activity we have told the window manager to have key focus.
437 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700438 ActivityRecord mFocusedActivity = null;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700439 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 * List of intents that were used to start the most recent tasks.
441 */
442 final ArrayList<TaskRecord> mRecentTasks
443 = new ArrayList<TaskRecord>();
444
445 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 * All of the applications we currently have running organized by name.
447 * The keys are strings of the application package name (as
448 * returned by the package manager), and the keys are ApplicationRecord
449 * objects.
450 */
451 final ProcessMap<ProcessRecord> mProcessNames
452 = new ProcessMap<ProcessRecord>();
453
454 /**
Dianne Hackborn860755f2010-06-03 18:47:52 -0700455 * The currently running heavy-weight process, if any.
456 */
457 ProcessRecord mHeavyWeightProcess = null;
458
459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 * The last time that various processes have crashed.
461 */
462 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
463
464 /**
465 * Set of applications that we consider to be bad, and will reject
466 * incoming broadcasts from (which the user has no control over).
467 * Processes are added to this set when they have crashed twice within
468 * a minimum amount of time; they are removed from it when they are
469 * later restarted (hopefully due to some user action). The value is the
470 * time it was added to the list.
471 */
472 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
473
474 /**
475 * All of the processes we currently have running organized by pid.
476 * The keys are the pid running the application.
477 *
478 * <p>NOTE: This object is protected by its own lock, NOT the global
479 * activity manager lock!
480 */
481 final SparseArray<ProcessRecord> mPidsSelfLocked
482 = new SparseArray<ProcessRecord>();
483
484 /**
485 * All of the processes that have been forced to be foreground. The key
486 * is the pid of the caller who requested it (we hold a death
487 * link on it).
488 */
489 abstract class ForegroundToken implements IBinder.DeathRecipient {
490 int pid;
491 IBinder token;
492 }
493 final SparseArray<ForegroundToken> mForegroundProcesses
494 = new SparseArray<ForegroundToken>();
495
496 /**
497 * List of records for processes that someone had tried to start before the
498 * system was ready. We don't start them at that point, but ensure they
499 * are started by the time booting is complete.
500 */
501 final ArrayList<ProcessRecord> mProcessesOnHold
502 = new ArrayList<ProcessRecord>();
503
504 /**
505 * List of records for processes that we have started and are waiting
506 * for them to call back. This is really only needed when running in
507 * single processes mode, in which case we do not have a unique pid for
508 * each process.
509 */
510 final ArrayList<ProcessRecord> mStartingProcesses
511 = new ArrayList<ProcessRecord>();
512
513 /**
514 * List of persistent applications that are in the process
515 * of being started.
516 */
517 final ArrayList<ProcessRecord> mPersistentStartingProcesses
518 = new ArrayList<ProcessRecord>();
519
520 /**
521 * Processes that are being forcibly torn down.
522 */
523 final ArrayList<ProcessRecord> mRemovedProcesses
524 = new ArrayList<ProcessRecord>();
525
526 /**
527 * List of running applications, sorted by recent usage.
528 * The first entry in the list is the least recently used.
529 * It contains ApplicationRecord objects. This list does NOT include
530 * any persistent application records (since we never want to exit them).
531 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800532 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 = new ArrayList<ProcessRecord>();
534
535 /**
536 * List of processes that should gc as soon as things are idle.
537 */
538 final ArrayList<ProcessRecord> mProcessesToGc
539 = new ArrayList<ProcessRecord>();
540
541 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800542 * This is the process holding what we currently consider to be
543 * the "home" activity.
544 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700545 ProcessRecord mHomeProcess;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800546
547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 * Set of PendingResultRecord objects that are currently active.
549 */
550 final HashSet mPendingResultRecords = new HashSet();
551
552 /**
553 * Set of IntentSenderRecord objects that are currently active.
554 */
555 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
556 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
557
558 /**
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -0800559 * Fingerprints (hashCode()) of stack traces that we've
Brad Fitzpatrick143666f2010-06-14 12:40:21 -0700560 * already logged DropBox entries for. Guarded by itself. If
561 * something (rogue user app) forces this over
562 * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
563 */
564 private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
565 private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
566
567 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -0700568 * Strict Mode background batched logging state.
569 *
570 * The string buffer is guarded by itself, and its lock is also
571 * used to determine if another batched write is already
572 * in-flight.
573 */
574 private final StringBuilder mStrictModeBuffer = new StringBuilder();
575
576 /**
Jeff Brown4d94a762010-09-23 11:33:28 -0700577 * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler.
578 */
579 private boolean mPendingBroadcastTimeoutMessage;
580
581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * Intent broadcast that we have tried to start, but are
583 * waiting for its application's process to be created. We only
584 * need one (instead of a list) because we always process broadcasts
585 * one at a time, so no others can be started while waiting for this
586 * one.
587 */
588 BroadcastRecord mPendingBroadcast = null;
589
590 /**
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700591 * The receiver index that is pending, to restart the broadcast if needed.
592 */
593 int mPendingBroadcastRecvIndex;
594
595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * Keeps track of all IIntentReceivers that have been registered for
597 * broadcasts. Hash keys are the receiver IBinder, hash value is
598 * a ReceiverList.
599 */
600 final HashMap mRegisteredReceivers = new HashMap();
601
602 /**
603 * Resolver for broadcast intents to registered receivers.
604 * Holds BroadcastFilter (subclass of IntentFilter).
605 */
606 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
607 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
608 @Override
609 protected boolean allowFilterResult(
610 BroadcastFilter filter, List<BroadcastFilter> dest) {
611 IBinder target = filter.receiverList.receiver.asBinder();
612 for (int i=dest.size()-1; i>=0; i--) {
613 if (dest.get(i).receiverList.receiver.asBinder() == target) {
614 return false;
615 }
616 }
617 return true;
618 }
619 };
620
621 /**
622 * State of all active sticky broadcasts. Keys are the action of the
623 * sticky Intent, values are an ArrayList of all broadcasted intents with
624 * that action (which should usually be one).
625 */
626 final HashMap<String, ArrayList<Intent>> mStickyBroadcasts =
627 new HashMap<String, ArrayList<Intent>>();
628
629 /**
630 * All currently running services.
631 */
632 final HashMap<ComponentName, ServiceRecord> mServices =
633 new HashMap<ComponentName, ServiceRecord>();
634
635 /**
636 * All currently running services indexed by the Intent used to start them.
637 */
638 final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent =
639 new HashMap<Intent.FilterComparison, ServiceRecord>();
640
641 /**
642 * All currently bound service connections. Keys are the IBinder of
643 * the client's IServiceConnection.
644 */
Dianne Hackborn43d9ac82010-08-25 15:06:25 -0700645 final HashMap<IBinder, ArrayList<ConnectionRecord>> mServiceConnections
646 = new HashMap<IBinder, ArrayList<ConnectionRecord>>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
648 /**
649 * List of services that we have been asked to start,
650 * but haven't yet been able to. It is used to hold start requests
651 * while waiting for their corresponding application thread to get
652 * going.
653 */
654 final ArrayList<ServiceRecord> mPendingServices
655 = new ArrayList<ServiceRecord>();
656
657 /**
658 * List of services that are scheduled to restart following a crash.
659 */
660 final ArrayList<ServiceRecord> mRestartingServices
661 = new ArrayList<ServiceRecord>();
662
663 /**
664 * List of services that are in the process of being stopped.
665 */
666 final ArrayList<ServiceRecord> mStoppingServices
667 = new ArrayList<ServiceRecord>();
668
669 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700670 * Backup/restore process management
671 */
672 String mBackupAppName = null;
673 BackupRecord mBackupTarget = null;
674
675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 * List of PendingThumbnailsRecord objects of clients who are still
677 * waiting to receive all of the thumbnails for a task.
678 */
679 final ArrayList mPendingThumbnails = new ArrayList();
680
681 /**
682 * List of HistoryRecord objects that have been finished and must
683 * still report back to a pending thumbnail receiver.
684 */
685 final ArrayList mCancelledThumbnails = new ArrayList();
686
687 /**
688 * All of the currently running global content providers. Keys are a
689 * string containing the provider name and values are a
690 * ContentProviderRecord object containing the data about it. Note
691 * that a single provider may be published under multiple names, so
692 * there may be multiple entries here for a single one in mProvidersByClass.
693 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700694 final HashMap<String, ContentProviderRecord> mProvidersByName
695 = new HashMap<String, ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 /**
698 * All of the currently running global content providers. Keys are a
699 * string containing the provider's implementation class and values are a
700 * ContentProviderRecord object containing the data about it.
701 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700702 final HashMap<String, ContentProviderRecord> mProvidersByClass
703 = new HashMap<String, ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704
705 /**
706 * List of content providers who have clients waiting for them. The
707 * application is currently being launched and the provider will be
708 * removed from this list once it is published.
709 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700710 final ArrayList<ContentProviderRecord> mLaunchingProviders
711 = new ArrayList<ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
713 /**
714 * Global set of specific Uri permissions that have been granted.
715 */
716 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
717 = new SparseArray<HashMap<Uri, UriPermission>>();
718
719 /**
720 * Thread-local storage used to carry caller permissions over through
721 * indirect content-provider access.
722 * @see #ActivityManagerService.openContentUri()
723 */
724 private class Identity {
725 public int pid;
726 public int uid;
727
728 Identity(int _pid, int _uid) {
729 pid = _pid;
730 uid = _uid;
731 }
732 }
733 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
734
735 /**
736 * All information we have collected about the runtime performance of
737 * any user id that can impact battery performance.
738 */
739 final BatteryStatsService mBatteryStatsService;
740
741 /**
742 * information about component usage
743 */
744 final UsageStatsService mUsageStatsService;
745
746 /**
747 * Current configuration information. HistoryRecord objects are given
748 * a reference to this object to indicate which configuration they are
749 * currently running in, so this object must be kept immutable.
750 */
751 Configuration mConfiguration = new Configuration();
752
753 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800754 * Current sequencing integer of the configuration, for skipping old
755 * configurations.
756 */
757 int mConfigurationSeq = 0;
758
759 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700760 * Hardware-reported OpenGLES version.
761 */
762 final int GL_ES_VERSION;
763
764 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 * List of initialization arguments to pass to all processes when binding applications to them.
766 * For example, references to the commonly used services.
767 */
768 HashMap<String, IBinder> mAppBindArgs;
769
770 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700771 * Temporary to avoid allocations. Protected by main lock.
772 */
773 final StringBuilder mStringBuilder = new StringBuilder(256);
774
775 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 * Used to control how we initialize the service.
777 */
778 boolean mStartRunning = false;
779 ComponentName mTopComponent;
780 String mTopAction;
781 String mTopData;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -0700782 boolean mProcessesReady = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 boolean mSystemReady = false;
784 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700785 boolean mWaitingUpdate = false;
786 boolean mDidUpdate = false;
Dianne Hackborn0d903a82010-09-07 23:51:03 -0700787 boolean mOnBattery = false;
Dianne Hackborn0dad3642010-09-09 21:25:35 -0700788 boolean mLaunchWarningShown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789
790 Context mContext;
791
792 int mFactoryTest;
793
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700794 boolean mCheckedForSetup;
795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700797 * The time at which we will allow normal application switches again,
798 * after a call to {@link #stopAppSwitches()}.
799 */
800 long mAppSwitchesAllowedTime;
801
802 /**
803 * This is set to true after the first switch after mAppSwitchesAllowedTime
804 * is set; any switches after that will clear the time.
805 */
806 boolean mDidAppSwitch;
807
808 /**
Dianne Hackborn287952c2010-09-22 22:34:31 -0700809 * Last time (in realtime) at which we checked for power usage.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700810 */
Dianne Hackborn287952c2010-09-22 22:34:31 -0700811 long mLastPowerCheckRealtime;
812
813 /**
814 * Last time (in uptime) at which we checked for power usage.
815 */
816 long mLastPowerCheckUptime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700817
818 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 * Set while we are wanting to sleep, to prevent any
820 * activities from being started/resumed.
821 */
822 boolean mSleeping = false;
823
824 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700825 * Set if we are shutting down the system, similar to sleeping.
826 */
827 boolean mShuttingDown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828
829 /**
830 * Task identifier that activities are currently being started
831 * in. Incremented each time a new task is created.
832 * todo: Replace this with a TokenSpace class that generates non-repeating
833 * integers that won't wrap.
834 */
835 int mCurTask = 1;
836
837 /**
838 * Current sequence id for oom_adj computation traversal.
839 */
840 int mAdjSeq = 0;
841
842 /**
Dianne Hackborn906497c2010-05-10 15:57:38 -0700843 * Current sequence id for process LRU updating.
844 */
845 int mLruSeq = 0;
846
847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 * Set to true if the ANDROID_SIMPLE_PROCESS_MANAGEMENT envvar
849 * is set, indicating the user wants processes started in such a way
850 * that they can use ANDROID_PROCESS_WRAPPER and know what will be
851 * running in each process (thus no pre-initialized process, etc).
852 */
853 boolean mSimpleProcessManagement = false;
854
855 /**
856 * System monitoring: number of processes that died since the last
857 * N procs were started.
858 */
859 int[] mProcDeaths = new int[20];
860
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700861 /**
862 * This is set if we had to do a delayed dexopt of an app before launching
863 * it, to increasing the ANR timeouts in that case.
864 */
865 boolean mDidDexOpt;
866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 String mDebugApp = null;
868 boolean mWaitForDebugger = false;
869 boolean mDebugTransient = false;
870 String mOrigDebugApp = null;
871 boolean mOrigWaitForDebugger = false;
872 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700873 IActivityController mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700875 final RemoteCallbackList<IActivityWatcher> mWatchers
876 = new RemoteCallbackList<IActivityWatcher>();
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 /**
879 * Callback of last caller to {@link #requestPss}.
880 */
881 Runnable mRequestPssCallback;
882
883 /**
884 * Remaining processes for which we are waiting results from the last
885 * call to {@link #requestPss}.
886 */
887 final ArrayList<ProcessRecord> mRequestPssList
888 = new ArrayList<ProcessRecord>();
889
890 /**
891 * Runtime statistics collection thread. This object's lock is used to
892 * protect all related state.
893 */
894 final Thread mProcessStatsThread;
895
896 /**
897 * Used to collect process stats when showing not responding dialog.
898 * Protected by mProcessStatsThread.
899 */
900 final ProcessStats mProcessStats = new ProcessStats(
901 MONITOR_THREAD_CPU_USAGE);
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700902 final AtomicLong mLastCpuTime = new AtomicLong(0);
903 final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 long mLastWriteTime = 0;
906
907 /**
908 * Set to true after the system has finished booting.
909 */
910 boolean mBooted = false;
911
912 int mProcessLimit = 0;
913
914 WindowManagerService mWindowManager;
915
916 static ActivityManagerService mSelf;
917 static ActivityThread mSystemThread;
918
919 private final class AppDeathRecipient implements IBinder.DeathRecipient {
920 final ProcessRecord mApp;
921 final int mPid;
922 final IApplicationThread mAppThread;
923
924 AppDeathRecipient(ProcessRecord app, int pid,
925 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800926 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 TAG, "New death recipient " + this
928 + " for thread " + thread.asBinder());
929 mApp = app;
930 mPid = pid;
931 mAppThread = thread;
932 }
933
934 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800935 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 TAG, "Death received in " + this
937 + " for thread " + mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 synchronized(ActivityManagerService.this) {
939 appDiedLocked(mApp, mPid, mAppThread);
940 }
941 }
942 }
943
944 static final int SHOW_ERROR_MSG = 1;
945 static final int SHOW_NOT_RESPONDING_MSG = 2;
946 static final int SHOW_FACTORY_ERROR_MSG = 3;
947 static final int UPDATE_CONFIGURATION_MSG = 4;
948 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
949 static final int WAIT_FOR_DEBUGGER_MSG = 6;
950 static final int BROADCAST_INTENT_MSG = 7;
951 static final int BROADCAST_TIMEOUT_MSG = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 static final int SERVICE_TIMEOUT_MSG = 12;
953 static final int UPDATE_TIME_ZONE = 13;
954 static final int SHOW_UID_ERROR_MSG = 14;
955 static final int IM_FEELING_LUCKY_MSG = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700957 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700958 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800959 static final int FINALIZE_PENDING_INTENT_MSG = 23;
Dianne Hackborn860755f2010-06-03 18:47:52 -0700960 static final int POST_HEAVY_NOTIFICATION_MSG = 24;
961 static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700962 static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700963 static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700964 static final int CLEAR_DNS_CACHE = 28;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700965 static final int UPDATE_HTTP_PROXY = 29;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966
967 AlertDialog mUidAlert;
968
969 final Handler mHandler = new Handler() {
970 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800971 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 //}
973
974 public void handleMessage(Message msg) {
975 switch (msg.what) {
976 case SHOW_ERROR_MSG: {
977 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 synchronized (ActivityManagerService.this) {
979 ProcessRecord proc = (ProcessRecord)data.get("app");
980 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800981 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 return;
983 }
984 AppErrorResult res = (AppErrorResult) data.get("result");
Dianne Hackborn55280a92009-05-07 15:53:46 -0700985 if (!mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -0800986 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 d.show();
988 proc.crashDialog = d;
989 } else {
990 // The device is asleep, so just pretend that the user
991 // saw a crash dialog and hit "force quit".
992 res.set(0);
993 }
994 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700995
996 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 } break;
998 case SHOW_NOT_RESPONDING_MSG: {
999 synchronized (ActivityManagerService.this) {
1000 HashMap data = (HashMap) msg.obj;
1001 ProcessRecord proc = (ProcessRecord)data.get("app");
1002 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001003 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 return;
1005 }
The Android Open Source Project4df24232009-03-05 14:34:35 -08001006
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001007 Intent intent = new Intent("android.intent.action.ANR");
1008 if (!mProcessesReady) {
1009 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1010 }
1011 broadcastIntentLocked(null, null, intent,
The Android Open Source Project4df24232009-03-05 14:34:35 -08001012 null, null, 0, null, null, null,
1013 false, false, MY_PID, Process.SYSTEM_UID);
1014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001016 mContext, proc, (ActivityRecord)data.get("activity"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 d.show();
1018 proc.anrDialog = d;
1019 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001020
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001021 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 } break;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07001023 case SHOW_STRICT_MODE_VIOLATION_MSG: {
1024 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
1025 synchronized (ActivityManagerService.this) {
1026 ProcessRecord proc = (ProcessRecord) data.get("app");
1027 if (proc == null) {
1028 Slog.e(TAG, "App not found when showing strict mode dialog.");
1029 break;
1030 }
1031 if (proc.crashDialog != null) {
1032 Slog.e(TAG, "App already has strict mode dialog: " + proc);
1033 return;
1034 }
1035 AppErrorResult res = (AppErrorResult) data.get("result");
1036 if (!mSleeping && !mShuttingDown) {
1037 Dialog d = new StrictModeViolationDialog(mContext, res, proc);
1038 d.show();
1039 proc.crashDialog = d;
1040 } else {
1041 // The device is asleep, so just pretend that the user
1042 // saw a crash dialog and hit "force quit".
1043 res.set(0);
1044 }
1045 }
1046 ensureBootCompleted();
1047 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 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: {
Jeff Brown4d94a762010-09-23 11:33:28 -07001089 synchronized (ActivityManagerService.this) {
1090 broadcastTimeoutLocked(true);
Jeff Hamiltonacf84742010-05-25 22:10:18 -05001091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001094 if (mDidDexOpt) {
1095 mDidDexOpt = false;
1096 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
1097 nmsg.obj = msg.obj;
1098 mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT);
1099 return;
1100 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 serviceTimeout((ProcessRecord)msg.obj);
1102 } break;
1103 case UPDATE_TIME_ZONE: {
1104 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001105 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1106 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 if (r.thread != null) {
1108 try {
1109 r.thread.updateTimeZone();
1110 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001111 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113 }
1114 }
1115 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001116 } break;
Robert Greenwalt03595d02010-11-02 14:08:23 -07001117 case CLEAR_DNS_CACHE: {
1118 synchronized (ActivityManagerService.this) {
1119 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1120 ProcessRecord r = mLruProcesses.get(i);
1121 if (r.thread != null) {
1122 try {
1123 r.thread.clearDnsCache();
1124 } catch (RemoteException ex) {
1125 Slog.w(TAG, "Failed to clear dns cache for: " + r.info.processName);
1126 }
1127 }
1128 }
1129 }
1130 } break;
Robert Greenwalt434203a2010-10-11 16:00:27 -07001131 case UPDATE_HTTP_PROXY: {
1132 ProxyProperties proxy = (ProxyProperties)msg.obj;
1133 String host = "";
1134 String port = "";
1135 String exclList = "";
1136 if (proxy != null) {
1137 host = proxy.getHost();
1138 port = Integer.toString(proxy.getPort());
1139 exclList = proxy.getExclusionList();
1140 }
1141 synchronized (ActivityManagerService.this) {
1142 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1143 ProcessRecord r = mLruProcesses.get(i);
1144 if (r.thread != null) {
1145 try {
1146 r.thread.setHttpProxy(host, port, exclList);
1147 } catch (RemoteException ex) {
1148 Slog.w(TAG, "Failed to update http proxy for: " +
1149 r.info.processName);
1150 }
1151 }
1152 }
1153 }
1154 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 case SHOW_UID_ERROR_MSG: {
1156 // XXX This is a temporary dialog, no need to localize.
1157 AlertDialog d = new BaseErrorDialog(mContext);
1158 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1159 d.setCancelable(false);
1160 d.setTitle("System UIDs Inconsistent");
1161 d.setMessage("UIDs on the system are inconsistent, you need to wipe your data partition or your device will be unstable.");
Christian Mehlmauer7664e202010-07-20 08:46:17 +02001162 d.setButton(DialogInterface.BUTTON_POSITIVE, "I'm Feeling Lucky",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1164 mUidAlert = d;
1165 d.show();
1166 } break;
1167 case IM_FEELING_LUCKY_MSG: {
1168 if (mUidAlert != null) {
1169 mUidAlert.dismiss();
1170 mUidAlert = null;
1171 }
1172 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001174 if (mDidDexOpt) {
1175 mDidDexOpt = false;
1176 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1177 nmsg.obj = msg.obj;
1178 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1179 return;
1180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 ProcessRecord app = (ProcessRecord)msg.obj;
1182 synchronized (ActivityManagerService.this) {
1183 processStartTimedOutLocked(app);
1184 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001185 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001186 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1187 synchronized (ActivityManagerService.this) {
1188 doPendingActivityLaunchesLocked(true);
1189 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001190 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001191 case KILL_APPLICATION_MSG: {
1192 synchronized (ActivityManagerService.this) {
1193 int uid = msg.arg1;
1194 boolean restart = (msg.arg2 == 1);
1195 String pkg = (String) msg.obj;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001196 forceStopPackageLocked(pkg, uid, restart, false, true);
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001197 }
1198 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001199 case FINALIZE_PENDING_INTENT_MSG: {
1200 ((PendingIntentRecord)msg.obj).completeFinalize();
1201 } break;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001202 case POST_HEAVY_NOTIFICATION_MSG: {
1203 INotificationManager inm = NotificationManager.getService();
1204 if (inm == null) {
1205 return;
1206 }
1207
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001208 ActivityRecord root = (ActivityRecord)msg.obj;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001209 ProcessRecord process = root.app;
1210 if (process == null) {
1211 return;
1212 }
1213
1214 try {
1215 Context context = mContext.createPackageContext(process.info.packageName, 0);
1216 String text = mContext.getString(R.string.heavy_weight_notification,
1217 context.getApplicationInfo().loadLabel(context.getPackageManager()));
1218 Notification notification = new Notification();
1219 notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1220 notification.when = 0;
1221 notification.flags = Notification.FLAG_ONGOING_EVENT;
1222 notification.tickerText = text;
1223 notification.defaults = 0; // please be quiet
1224 notification.sound = null;
1225 notification.vibrate = null;
1226 notification.setLatestEventInfo(context, text,
1227 mContext.getText(R.string.heavy_weight_notification_detail),
1228 PendingIntent.getActivity(mContext, 0, root.intent,
1229 PendingIntent.FLAG_CANCEL_CURRENT));
1230
1231 try {
1232 int[] outId = new int[1];
1233 inm.enqueueNotification("android", R.string.heavy_weight_notification,
1234 notification, outId);
1235 } catch (RuntimeException e) {
1236 Slog.w(ActivityManagerService.TAG,
1237 "Error showing notification for heavy-weight app", e);
1238 } catch (RemoteException e) {
1239 }
1240 } catch (NameNotFoundException e) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07001241 Slog.w(TAG, "Unable to create context for heavy notification", e);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001242 }
1243 } break;
1244 case CANCEL_HEAVY_NOTIFICATION_MSG: {
1245 INotificationManager inm = NotificationManager.getService();
1246 if (inm == null) {
1247 return;
1248 }
1249 try {
1250 inm.cancelNotification("android",
1251 R.string.heavy_weight_notification);
1252 } catch (RuntimeException e) {
1253 Slog.w(ActivityManagerService.TAG,
1254 "Error canceling notification for service", e);
1255 } catch (RemoteException e) {
1256 }
1257 } break;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001258 case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1259 synchronized (ActivityManagerService.this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001260 checkExcessivePowerUsageLocked(true);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001261 removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001262 Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1263 sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001264 }
1265 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 }
1267 }
1268 };
1269
1270 public static void setSystemProcess() {
1271 try {
1272 ActivityManagerService m = mSelf;
1273
1274 ServiceManager.addService("activity", m);
1275 ServiceManager.addService("meminfo", new MemBinder(m));
1276 if (MONITOR_CPU_USAGE) {
1277 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 ServiceManager.addService("permission", new PermissionController(m));
1280
1281 ApplicationInfo info =
1282 mSelf.mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -07001283 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001284 mSystemThread.installSystemApplicationInfo(info);
1285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 synchronized (mSelf) {
1287 ProcessRecord app = mSelf.newProcessRecordLocked(
1288 mSystemThread.getApplicationThread(), info,
1289 info.processName);
1290 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001291 app.pid = MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 app.maxAdj = SYSTEM_ADJ;
1293 mSelf.mProcessNames.put(app.processName, app.info.uid, app);
1294 synchronized (mSelf.mPidsSelfLocked) {
1295 mSelf.mPidsSelfLocked.put(app.pid, app);
1296 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001297 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 } catch (PackageManager.NameNotFoundException e) {
1300 throw new RuntimeException(
1301 "Unable to find android system package", e);
1302 }
1303 }
1304
1305 public void setWindowManager(WindowManagerService wm) {
1306 mWindowManager = wm;
1307 }
1308
1309 public static final Context main(int factoryTest) {
1310 AThread thr = new AThread();
1311 thr.start();
1312
1313 synchronized (thr) {
1314 while (thr.mService == null) {
1315 try {
1316 thr.wait();
1317 } catch (InterruptedException e) {
1318 }
1319 }
1320 }
1321
1322 ActivityManagerService m = thr.mService;
1323 mSelf = m;
1324 ActivityThread at = ActivityThread.systemMain();
1325 mSystemThread = at;
1326 Context context = at.getSystemContext();
Dianne Hackborn247fe742011-01-08 17:25:57 -08001327 context.setTheme(android.R.style.Theme_Holo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 m.mContext = context;
1329 m.mFactoryTest = factoryTest;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001330 m.mMainStack = new ActivityStack(m, context, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 m.mBatteryStatsService.publish(context);
1333 m.mUsageStatsService.publish(context);
1334
1335 synchronized (thr) {
1336 thr.mReady = true;
1337 thr.notifyAll();
1338 }
1339
1340 m.startRunning(null, null, null, null);
1341
1342 return context;
1343 }
1344
1345 public static ActivityManagerService self() {
1346 return mSelf;
1347 }
1348
1349 static class AThread extends Thread {
1350 ActivityManagerService mService;
1351 boolean mReady = false;
1352
1353 public AThread() {
1354 super("ActivityManager");
1355 }
1356
1357 public void run() {
1358 Looper.prepare();
1359
1360 android.os.Process.setThreadPriority(
1361 android.os.Process.THREAD_PRIORITY_FOREGROUND);
Christopher Tate160edb32010-06-30 17:46:30 -07001362 android.os.Process.setCanSelfBackground(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
1364 ActivityManagerService m = new ActivityManagerService();
1365
1366 synchronized (this) {
1367 mService = m;
1368 notifyAll();
1369 }
1370
1371 synchronized (this) {
1372 while (!mReady) {
1373 try {
1374 wait();
1375 } catch (InterruptedException e) {
1376 }
1377 }
1378 }
1379
Brad Fitzpatrickec062f62010-11-03 09:56:54 -07001380 // For debug builds, log event loop stalls to dropbox for analysis.
1381 if (StrictMode.conditionallyEnableDebugLogging()) {
1382 Slog.i(TAG, "Enabled StrictMode logging for AThread's Looper");
1383 }
1384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 Looper.loop();
1386 }
1387 }
1388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 static class MemBinder extends Binder {
1390 ActivityManagerService mActivityManagerService;
1391 MemBinder(ActivityManagerService activityManagerService) {
1392 mActivityManagerService = activityManagerService;
1393 }
1394
1395 @Override
1396 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1397 ActivityManagerService service = mActivityManagerService;
1398 ArrayList<ProcessRecord> procs;
1399 synchronized (mActivityManagerService) {
1400 if (args != null && args.length > 0
1401 && args[0].charAt(0) != '-') {
1402 procs = new ArrayList<ProcessRecord>();
1403 int pid = -1;
1404 try {
1405 pid = Integer.parseInt(args[0]);
1406 } catch (NumberFormatException e) {
1407
1408 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001409 for (int i=service.mLruProcesses.size()-1; i>=0; i--) {
1410 ProcessRecord proc = service.mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 if (proc.pid == pid) {
1412 procs.add(proc);
1413 } else if (proc.processName.equals(args[0])) {
1414 procs.add(proc);
1415 }
1416 }
1417 if (procs.size() <= 0) {
1418 pw.println("No process found for: " + args[0]);
1419 return;
1420 }
1421 } else {
Dianne Hackborn472ad872010-04-07 17:31:48 -07001422 procs = new ArrayList<ProcessRecord>(service.mLruProcesses);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 }
1424 }
1425 dumpApplicationMemoryUsage(fd, pw, procs, " ", args);
1426 }
1427 }
1428
1429 static class CpuBinder extends Binder {
1430 ActivityManagerService mActivityManagerService;
1431 CpuBinder(ActivityManagerService activityManagerService) {
1432 mActivityManagerService = activityManagerService;
1433 }
1434
1435 @Override
1436 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1437 synchronized (mActivityManagerService.mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07001438 pw.print(mActivityManagerService.mProcessStats.printCurrentLoad());
1439 pw.print(mActivityManagerService.mProcessStats.printCurrentState(
1440 SystemClock.uptimeMillis()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442 }
1443 }
1444
1445 private ActivityManagerService() {
1446 String v = System.getenv("ANDROID_SIMPLE_PROCESS_MANAGEMENT");
1447 if (v != null && Integer.getInteger(v) != 0) {
1448 mSimpleProcessManagement = true;
1449 }
1450 v = System.getenv("ANDROID_DEBUG_APP");
1451 if (v != null) {
1452 mSimpleProcessManagement = true;
1453 }
1454
Joe Onorato8a9b2202010-02-26 18:56:32 -08001455 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 File dataDir = Environment.getDataDirectory();
1458 File systemDir = new File(dataDir, "system");
1459 systemDir.mkdirs();
1460 mBatteryStatsService = new BatteryStatsService(new File(
1461 systemDir, "batterystats.bin").toString());
1462 mBatteryStatsService.getActiveStatistics().readLocked();
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001463 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
Dianne Hackborn287952c2010-09-22 22:34:31 -07001464 mOnBattery = DEBUG_POWER ? true
1465 : mBatteryStatsService.getActiveStatistics().getIsOnBattery();
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001466 mBatteryStatsService.getActiveStatistics().setCallback(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001468 mUsageStatsService = new UsageStatsService(new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001469 systemDir, "usagestats").toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470
Jack Palevichb90d28c2009-07-22 15:35:24 -07001471 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1472 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1473
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001474 mConfiguration.setToDefaults();
1475 mConfiguration.locale = Locale.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 mProcessStats.init();
1477
1478 // Add ourself to the Watchdog monitors.
1479 Watchdog.getInstance().addMonitor(this);
1480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 mProcessStatsThread = new Thread("ProcessStats") {
1482 public void run() {
1483 while (true) {
1484 try {
1485 try {
1486 synchronized(this) {
1487 final long now = SystemClock.uptimeMillis();
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001488 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001490 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 // + ", write delay=" + nextWriteDelay);
1492 if (nextWriteDelay < nextCpuDelay) {
1493 nextCpuDelay = nextWriteDelay;
1494 }
1495 if (nextCpuDelay > 0) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001496 mProcessStatsMutexFree.set(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 this.wait(nextCpuDelay);
1498 }
1499 }
1500 } catch (InterruptedException e) {
1501 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 updateCpuStatsNow();
1503 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001504 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506 }
1507 }
1508 };
1509 mProcessStatsThread.start();
1510 }
1511
1512 @Override
1513 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1514 throws RemoteException {
1515 try {
1516 return super.onTransact(code, data, reply, flags);
1517 } catch (RuntimeException e) {
1518 // The activity manager only throws security exceptions, so let's
1519 // log all others.
1520 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001521 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523 throw e;
1524 }
1525 }
1526
1527 void updateCpuStats() {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001528 final long now = SystemClock.uptimeMillis();
1529 if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
1530 return;
1531 }
1532 if (mProcessStatsMutexFree.compareAndSet(true, false)) {
1533 synchronized (mProcessStatsThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 mProcessStatsThread.notify();
1535 }
1536 }
1537 }
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 void updateCpuStatsNow() {
1540 synchronized (mProcessStatsThread) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001541 mProcessStatsMutexFree.set(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 final long now = SystemClock.uptimeMillis();
1543 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 if (MONITOR_CPU_USAGE &&
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001546 mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
1547 mLastCpuTime.set(now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 haveNewCpuStats = true;
1549 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001550 //Slog.i(TAG, mProcessStats.printCurrentState());
1551 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 // + mProcessStats.getTotalCpuPercent() + "%");
1553
Joe Onorato8a9b2202010-02-26 18:56:32 -08001554 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 if ("true".equals(SystemProperties.get("events.cpu"))) {
1556 int user = mProcessStats.getLastUserTime();
1557 int system = mProcessStats.getLastSystemTime();
1558 int iowait = mProcessStats.getLastIoWaitTime();
1559 int irq = mProcessStats.getLastIrqTime();
1560 int softIrq = mProcessStats.getLastSoftIrqTime();
1561 int idle = mProcessStats.getLastIdleTime();
1562
1563 int total = user + system + iowait + irq + softIrq + idle;
1564 if (total == 0) total = 1;
1565
Doug Zongker2bec3d42009-12-04 12:52:44 -08001566 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 ((user+system+iowait+irq+softIrq) * 100) / total,
1568 (user * 100) / total,
1569 (system * 100) / total,
1570 (iowait * 100) / total,
1571 (irq * 100) / total,
1572 (softIrq * 100) / total);
1573 }
1574 }
1575
Amith Yamasanie43530a2009-08-21 13:11:37 -07001576 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001577 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001578 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 synchronized(mPidsSelfLocked) {
1580 if (haveNewCpuStats) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001581 if (mOnBattery) {
1582 int perc = bstats.startAddingCpuLocked();
1583 int totalUTime = 0;
1584 int totalSTime = 0;
Dianne Hackborn287952c2010-09-22 22:34:31 -07001585 final int N = mProcessStats.countStats();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 for (int i=0; i<N; i++) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001587 ProcessStats.Stats st = mProcessStats.getStats(i);
1588 if (!st.working) {
1589 continue;
1590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001592 int otherUTime = (st.rel_utime*perc)/100;
1593 int otherSTime = (st.rel_stime*perc)/100;
1594 totalUTime += otherUTime;
1595 totalSTime += otherSTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 if (pr != null) {
1597 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001598 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1599 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001600 ps.addSpeedStepTimes(cpuSpeedTimes);
Dianne Hackborn287952c2010-09-22 22:34:31 -07001601 pr.curCpuTime += (st.rel_utime+st.rel_stime) * 10;
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001602 } else {
1603 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001604 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001605 if (ps != null) {
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001606 ps.addCpuTimeLocked(st.rel_utime-otherUTime,
1607 st.rel_stime-otherSTime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001608 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 }
1611 }
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001612 bstats.finishAddingCpuLocked(perc, totalUTime,
1613 totalSTime, cpuSpeedTimes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 }
1615 }
1616 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1619 mLastWriteTime = now;
Dianne Hackbornce2ef762010-09-20 11:39:14 -07001620 mBatteryStatsService.getActiveStatistics().writeAsyncLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 }
1622 }
1623 }
1624 }
1625
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001626 @Override
1627 public void batteryNeedsCpuUpdate() {
1628 updateCpuStatsNow();
1629 }
1630
1631 @Override
1632 public void batteryPowerChanged(boolean onBattery) {
1633 // When plugging in, update the CPU stats first before changing
1634 // the plug state.
1635 updateCpuStatsNow();
1636 synchronized (this) {
1637 synchronized(mPidsSelfLocked) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001638 mOnBattery = DEBUG_POWER ? true : onBattery;
Dianne Hackborn0d903a82010-09-07 23:51:03 -07001639 }
1640 }
1641 }
1642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 /**
1644 * Initialize the application bind args. These are passed to each
1645 * process when the bindApplication() IPC is sent to the process. They're
1646 * lazily setup to make sure the services are running when they're asked for.
1647 */
1648 private HashMap<String, IBinder> getCommonServicesLocked() {
1649 if (mAppBindArgs == null) {
1650 mAppBindArgs = new HashMap<String, IBinder>();
1651
1652 // Setup the application init args
1653 mAppBindArgs.put("package", ServiceManager.getService("package"));
1654 mAppBindArgs.put("window", ServiceManager.getService("window"));
1655 mAppBindArgs.put(Context.ALARM_SERVICE,
1656 ServiceManager.getService(Context.ALARM_SERVICE));
1657 }
1658 return mAppBindArgs;
1659 }
1660
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001661 final void setFocusedActivityLocked(ActivityRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if (mFocusedActivity != r) {
1663 mFocusedActivity = r;
1664 mWindowManager.setFocusedApp(r, true);
1665 }
1666 }
1667
Dianne Hackborn906497c2010-05-10 15:57:38 -07001668 private final void updateLruProcessInternalLocked(ProcessRecord app,
1669 boolean oomAdj, boolean updateActivityTime, int bestPos) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001671 int lrui = mLruProcesses.indexOf(app);
1672 if (lrui >= 0) mLruProcesses.remove(lrui);
1673
1674 int i = mLruProcesses.size()-1;
1675 int skipTop = 0;
1676
Dianne Hackborn906497c2010-05-10 15:57:38 -07001677 app.lruSeq = mLruSeq;
1678
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001679 // compute the new weight for this process.
1680 if (updateActivityTime) {
1681 app.lastActivityTime = SystemClock.uptimeMillis();
1682 }
1683 if (app.activities.size() > 0) {
1684 // If this process has activities, we more strongly want to keep
1685 // it around.
1686 app.lruWeight = app.lastActivityTime;
1687 } else if (app.pubProviders.size() > 0) {
1688 // If this process contains content providers, we want to keep
1689 // it a little more strongly.
1690 app.lruWeight = app.lastActivityTime - CONTENT_APP_IDLE_OFFSET;
1691 // Also don't let it kick out the first few "real" hidden processes.
1692 skipTop = MIN_HIDDEN_APPS;
1693 } else {
1694 // If this process doesn't have activities, we less strongly
1695 // want to keep it around, and generally want to avoid getting
1696 // in front of any very recently used activities.
1697 app.lruWeight = app.lastActivityTime - EMPTY_APP_IDLE_OFFSET;
1698 // Also don't let it kick out the first few "real" hidden processes.
1699 skipTop = MIN_HIDDEN_APPS;
1700 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001701
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001702 while (i >= 0) {
1703 ProcessRecord p = mLruProcesses.get(i);
1704 // If this app shouldn't be in front of the first N background
1705 // apps, then skip over that many that are currently hidden.
1706 if (skipTop > 0 && p.setAdj >= HIDDEN_APP_MIN_ADJ) {
1707 skipTop--;
1708 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001709 if (p.lruWeight <= app.lruWeight || i < bestPos) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001710 mLruProcesses.add(i+1, app);
1711 break;
1712 }
1713 i--;
1714 }
1715 if (i < 0) {
1716 mLruProcesses.add(0, app);
1717 }
1718
Dianne Hackborn906497c2010-05-10 15:57:38 -07001719 // If the app is currently using a content provider or service,
1720 // bump those processes as well.
1721 if (app.connections.size() > 0) {
1722 for (ConnectionRecord cr : app.connections) {
1723 if (cr.binding != null && cr.binding.service != null
1724 && cr.binding.service.app != null
1725 && cr.binding.service.app.lruSeq != mLruSeq) {
1726 updateLruProcessInternalLocked(cr.binding.service.app, oomAdj,
1727 updateActivityTime, i+1);
1728 }
1729 }
1730 }
1731 if (app.conProviders.size() > 0) {
1732 for (ContentProviderRecord cpr : app.conProviders.keySet()) {
1733 if (cpr.app != null && cpr.app.lruSeq != mLruSeq) {
1734 updateLruProcessInternalLocked(cpr.app, oomAdj,
1735 updateActivityTime, i+1);
1736 }
1737 }
1738 }
1739
Joe Onorato8a9b2202010-02-26 18:56:32 -08001740 //Slog.i(TAG, "Putting proc to front: " + app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 if (oomAdj) {
1742 updateOomAdjLocked();
1743 }
1744 }
1745
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001746 final void updateLruProcessLocked(ProcessRecord app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001747 boolean oomAdj, boolean updateActivityTime) {
1748 mLruSeq++;
1749 updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0);
1750 }
1751
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001752 final ProcessRecord getProcessRecordLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 String processName, int uid) {
1754 if (uid == Process.SYSTEM_UID) {
1755 // The system gets to run in any process. If there are multiple
1756 // processes with the same uid, just pick the first (this
1757 // should never happen).
1758 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1759 processName);
1760 return procs != null ? procs.valueAt(0) : null;
1761 }
1762 ProcessRecord proc = mProcessNames.get(processName, uid);
1763 return proc;
1764 }
1765
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001766 void ensurePackageDexOpt(String packageName) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001767 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001768 try {
1769 if (pm.performDexOpt(packageName)) {
1770 mDidDexOpt = true;
1771 }
1772 } catch (RemoteException e) {
1773 }
1774 }
1775
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001776 boolean isNextTransitionForward() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 int transit = mWindowManager.getPendingAppTransition();
1778 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1779 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1780 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1781 }
1782
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001783 final ProcessRecord startProcessLocked(String processName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001785 String hostingType, ComponentName hostingName, boolean allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 ProcessRecord app = getProcessRecordLocked(processName, info.uid);
1787 // We don't have to do anything more if:
1788 // (1) There is an existing application record; and
1789 // (2) The caller doesn't think it is dead, OR there is no thread
1790 // object attached to it so we know it couldn't have crashed; and
1791 // (3) There is a pid assigned to it, so it is either starting or
1792 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001793 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 + " app=" + app + " knownToBeDead=" + knownToBeDead
1795 + " thread=" + (app != null ? app.thread : null)
1796 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001797 if (app != null && app.pid > 0) {
1798 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001799 // We already have the app running, or are waiting for it to
1800 // come up (we have a pid but not yet its thread), so keep it.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001801 if (DEBUG_PROCESSES) Slog.v(TAG, "App already running: " + app);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001802 return app;
1803 } else {
1804 // An application record is attached to a previous process,
1805 // clean it up now.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001806 if (DEBUG_PROCESSES) Slog.v(TAG, "App died: " + app);
Magnus Edlund7bb25812010-02-24 15:45:06 +01001807 handleAppDiedLocked(app, true);
1808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 String hostingNameStr = hostingName != null
1812 ? hostingName.flattenToShortString() : null;
1813
1814 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1815 // If we are in the background, then check to see if this process
1816 // is bad. If so, we will just silently fail.
1817 if (mBadProcesses.get(info.processName, info.uid) != null) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001818 if (DEBUG_PROCESSES) Slog.v(TAG, "Bad process: " + info.uid
1819 + "/" + info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 return null;
1821 }
1822 } else {
1823 // When the user is explicitly starting a process, then clear its
1824 // crash count so that we won't make it bad until they see at
1825 // least one crash dialog again, and make the process good again
1826 // if it had been bad.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001827 if (DEBUG_PROCESSES) Slog.v(TAG, "Clearing bad process: " + info.uid
1828 + "/" + info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 mProcessCrashTimes.remove(info.processName, info.uid);
1830 if (mBadProcesses.get(info.processName, info.uid) != null) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08001831 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 info.processName);
1833 mBadProcesses.remove(info.processName, info.uid);
1834 if (app != null) {
1835 app.bad = false;
1836 }
1837 }
1838 }
1839
1840 if (app == null) {
1841 app = newProcessRecordLocked(null, info, processName);
1842 mProcessNames.put(processName, info.uid, app);
1843 } else {
1844 // If this is a new package in the process, add the package to the list
1845 app.addPackage(info.packageName);
1846 }
1847
1848 // If the system is not ready yet, then hold off on starting this
1849 // process until it is.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001850 if (!mProcessesReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001851 && !isAllowedWhileBooting(info)
1852 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 if (!mProcessesOnHold.contains(app)) {
1854 mProcessesOnHold.add(app);
1855 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001856 if (DEBUG_PROCESSES) Slog.v(TAG, "System not ready, putting on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 return app;
1858 }
1859
1860 startProcessLocked(app, hostingType, hostingNameStr);
1861 return (app.pid != 0) ? app : null;
1862 }
1863
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001864 boolean isAllowedWhileBooting(ApplicationInfo ai) {
1865 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
1866 }
1867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 private final void startProcessLocked(ProcessRecord app,
1869 String hostingType, String hostingNameStr) {
1870 if (app.pid > 0 && app.pid != MY_PID) {
1871 synchronized (mPidsSelfLocked) {
1872 mPidsSelfLocked.remove(app.pid);
1873 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
1874 }
1875 app.pid = 0;
1876 }
1877
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07001878 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
1879 "startProcessLocked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 mProcessesOnHold.remove(app);
1881
1882 updateCpuStats();
1883
1884 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
1885 mProcDeaths[0] = 0;
1886
1887 try {
1888 int uid = app.info.uid;
1889 int[] gids = null;
1890 try {
1891 gids = mContext.getPackageManager().getPackageGids(
1892 app.info.packageName);
1893 } catch (PackageManager.NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001894 Slog.w(TAG, "Unable to retrieve gids", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
1896 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
1897 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
1898 && mTopComponent != null
1899 && app.processName.equals(mTopComponent.getPackageName())) {
1900 uid = 0;
1901 }
1902 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
1903 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
1904 uid = 0;
1905 }
1906 }
1907 int debugFlags = 0;
1908 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
1909 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
1910 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08001911 // Run the app in safe mode if its manifest requests so or the
1912 // system is booted in safe mode.
1913 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
1914 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08001915 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
1916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
1918 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
1919 }
1920 if ("1".equals(SystemProperties.get("debug.assert"))) {
1921 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
1922 }
1923 int pid = Process.start("android.app.ActivityThread",
1924 mSimpleProcessManagement ? app.processName : null, uid, uid,
1925 gids, debugFlags, null);
1926 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
1927 synchronized (bs) {
1928 if (bs.isOnBattery()) {
1929 app.batteryStats.incStartsLocked();
1930 }
1931 }
1932
Doug Zongker2bec3d42009-12-04 12:52:44 -08001933 EventLog.writeEvent(EventLogTags.AM_PROC_START, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 app.processName, hostingType,
1935 hostingNameStr != null ? hostingNameStr : "");
1936
1937 if (app.persistent) {
Christopher Tatec27181c2010-06-30 14:41:09 -07001938 Watchdog.getInstance().processStarted(app.processName, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
1940
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07001941 StringBuilder buf = mStringBuilder;
1942 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 buf.append("Start proc ");
1944 buf.append(app.processName);
1945 buf.append(" for ");
1946 buf.append(hostingType);
1947 if (hostingNameStr != null) {
1948 buf.append(" ");
1949 buf.append(hostingNameStr);
1950 }
1951 buf.append(": pid=");
1952 buf.append(pid);
1953 buf.append(" uid=");
1954 buf.append(uid);
1955 buf.append(" gids={");
1956 if (gids != null) {
1957 for (int gi=0; gi<gids.length; gi++) {
1958 if (gi != 0) buf.append(", ");
1959 buf.append(gids[gi]);
1960
1961 }
1962 }
1963 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08001964 Slog.i(TAG, buf.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 if (pid == 0 || pid == MY_PID) {
1966 // Processes are being emulated with threads.
1967 app.pid = MY_PID;
1968 app.removed = false;
1969 mStartingProcesses.add(app);
1970 } else if (pid > 0) {
1971 app.pid = pid;
1972 app.removed = false;
1973 synchronized (mPidsSelfLocked) {
1974 this.mPidsSelfLocked.put(pid, app);
1975 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1976 msg.obj = app;
1977 mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT);
1978 }
1979 } else {
1980 app.pid = 0;
1981 RuntimeException e = new RuntimeException(
1982 "Failure starting process " + app.processName
1983 + ": returned pid=" + pid);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001984 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 }
1986 } catch (RuntimeException e) {
1987 // XXX do better error recovery.
1988 app.pid = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001989 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
1991 }
1992
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993 void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 if (resumed) {
1995 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
1996 } else {
1997 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
1998 }
1999 }
2000
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002001 boolean startHomeActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002002 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
2003 && mTopAction == null) {
2004 // We are running in factory test mode, but unable to find
2005 // the factory test app, so just sit around displaying the
2006 // error message and don't try to start anything.
2007 return false;
2008 }
2009 Intent intent = new Intent(
2010 mTopAction,
2011 mTopData != null ? Uri.parse(mTopData) : null);
2012 intent.setComponent(mTopComponent);
2013 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
2014 intent.addCategory(Intent.CATEGORY_HOME);
2015 }
2016 ActivityInfo aInfo =
2017 intent.resolveActivityInfo(mContext.getPackageManager(),
2018 STOCK_PM_FLAGS);
2019 if (aInfo != null) {
2020 intent.setComponent(new ComponentName(
2021 aInfo.applicationInfo.packageName, aInfo.name));
2022 // Don't do this if the home app is currently being
2023 // instrumented.
2024 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
2025 aInfo.applicationInfo.uid);
2026 if (app == null || app.instrumentationClass == null) {
2027 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002028 mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002029 null, null, 0, 0, 0, false, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002030 }
2031 }
2032
2033
2034 return true;
2035 }
2036
2037 /**
2038 * Starts the "new version setup screen" if appropriate.
2039 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002040 void startSetupActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002041 // Only do this once per boot.
2042 if (mCheckedForSetup) {
2043 return;
2044 }
2045
2046 // We will show this screen if the current one is a different
2047 // version than the last one shown, and we are not running in
2048 // low-level factory test mode.
2049 final ContentResolver resolver = mContext.getContentResolver();
2050 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
2051 Settings.Secure.getInt(resolver,
2052 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
2053 mCheckedForSetup = true;
2054
2055 // See if we should be showing the platform update setup UI.
2056 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
2057 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
2058 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
2059
2060 // We don't allow third party apps to replace this.
2061 ResolveInfo ri = null;
2062 for (int i=0; ris != null && i<ris.size(); i++) {
2063 if ((ris.get(i).activityInfo.applicationInfo.flags
2064 & ApplicationInfo.FLAG_SYSTEM) != 0) {
2065 ri = ris.get(i);
2066 break;
2067 }
2068 }
2069
2070 if (ri != null) {
2071 String vers = ri.activityInfo.metaData != null
2072 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
2073 : null;
2074 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
2075 vers = ri.activityInfo.applicationInfo.metaData.getString(
2076 Intent.METADATA_SETUP_VERSION);
2077 }
2078 String lastVers = Settings.Secure.getString(
2079 resolver, Settings.Secure.LAST_SETUP_SHOWN);
2080 if (vers != null && !vers.equals(lastVers)) {
2081 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2082 intent.setComponent(new ComponentName(
2083 ri.activityInfo.packageName, ri.activityInfo.name));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002084 mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002085 null, null, 0, 0, 0, false, false, null);
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07002086 }
2087 }
2088 }
2089 }
2090
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002091 void reportResumedActivityLocked(ActivityRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002092 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002093
2094 final int identHash = System.identityHashCode(r);
2095 updateUsageStats(r, true);
2096
2097 int i = mWatchers.beginBroadcast();
2098 while (i > 0) {
2099 i--;
2100 IActivityWatcher w = mWatchers.getBroadcastItem(i);
2101 if (w != null) {
2102 try {
2103 w.activityResuming(identHash);
2104 } catch (RemoteException e) {
2105 }
2106 }
2107 }
2108 mWatchers.finishBroadcast();
2109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002111 final void doPendingActivityLaunchesLocked(boolean doResume) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002112 final int N = mPendingActivityLaunches.size();
2113 if (N <= 0) {
2114 return;
2115 }
2116 for (int i=0; i<N; i++) {
2117 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002118 mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord,
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07002119 pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded,
2120 doResume && i == (N-1));
2121 }
2122 mPendingActivityLaunches.clear();
2123 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002124
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002125 public final int startActivity(IApplicationThread caller,
2126 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2127 int grantedMode, IBinder resultTo,
2128 String resultWho, int requestCode, boolean onlyIfNeeded,
2129 boolean debug) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002130 return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002131 grantedUriPermissions, grantedMode, resultTo, resultWho,
2132 requestCode, onlyIfNeeded, debug, null, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002133 }
2134
2135 public final WaitResult startActivityAndWait(IApplicationThread caller,
2136 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2137 int grantedMode, IBinder resultTo,
2138 String resultWho, int requestCode, boolean onlyIfNeeded,
2139 boolean debug) {
2140 WaitResult res = new WaitResult();
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002141 mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002142 grantedUriPermissions, grantedMode, resultTo, resultWho,
2143 requestCode, onlyIfNeeded, debug, res, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002144 return res;
2145 }
2146
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002147 public final int startActivityWithConfig(IApplicationThread caller,
2148 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2149 int grantedMode, IBinder resultTo,
2150 String resultWho, int requestCode, boolean onlyIfNeeded,
2151 boolean debug, Configuration config) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002152 return mMainStack.startActivityMayWait(caller, -1, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002153 grantedUriPermissions, grantedMode, resultTo, resultWho,
2154 requestCode, onlyIfNeeded, debug, null, config);
2155 }
2156
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002157 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002158 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002159 IBinder resultTo, String resultWho, int requestCode,
2160 int flagsMask, int flagsValues) {
2161 // Refuse possible leaked file descriptors
2162 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
2163 throw new IllegalArgumentException("File descriptors passed in Intent");
2164 }
2165
2166 IIntentSender sender = intent.getTarget();
2167 if (!(sender instanceof PendingIntentRecord)) {
2168 throw new IllegalArgumentException("Bad PendingIntent object");
2169 }
2170
2171 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002172
2173 synchronized (this) {
2174 // If this is coming from the currently resumed activity, it is
2175 // effectively saying that app switches are allowed at this point.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002176 if (mMainStack.mResumedActivity != null
2177 && mMainStack.mResumedActivity.info.applicationInfo.uid ==
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002178 Binder.getCallingUid()) {
2179 mAppSwitchesAllowedTime = 0;
2180 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002181 }
2182
2183 return pir.sendInner(0, fillInIntent, resolvedType,
2184 null, resultTo, resultWho, requestCode, flagsMask, flagsValues);
2185 }
2186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 public boolean startNextMatchingActivity(IBinder callingActivity,
2188 Intent intent) {
2189 // Refuse possible leaked file descriptors
2190 if (intent != null && intent.hasFileDescriptors() == true) {
2191 throw new IllegalArgumentException("File descriptors passed in Intent");
2192 }
2193
2194 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 int index = mMainStack.indexOfTokenLocked(callingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 if (index < 0) {
2197 return false;
2198 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002199 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 if (r.app == null || r.app.thread == null) {
2201 // The caller is not running... d'oh!
2202 return false;
2203 }
2204 intent = new Intent(intent);
2205 // The caller is not allowed to change the data.
2206 intent.setDataAndType(r.intent.getData(), r.intent.getType());
2207 // And we are resetting to find the next component...
2208 intent.setComponent(null);
2209
2210 ActivityInfo aInfo = null;
2211 try {
2212 List<ResolveInfo> resolves =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002213 AppGlobals.getPackageManager().queryIntentActivities(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 intent, r.resolvedType,
Dianne Hackborn1655be42009-05-08 14:29:01 -07002215 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216
2217 // Look for the original activity in the list...
2218 final int N = resolves != null ? resolves.size() : 0;
2219 for (int i=0; i<N; i++) {
2220 ResolveInfo rInfo = resolves.get(i);
2221 if (rInfo.activityInfo.packageName.equals(r.packageName)
2222 && rInfo.activityInfo.name.equals(r.info.name)) {
2223 // We found the current one... the next matching is
2224 // after it.
2225 i++;
2226 if (i<N) {
2227 aInfo = resolves.get(i).activityInfo;
2228 }
2229 break;
2230 }
2231 }
2232 } catch (RemoteException e) {
2233 }
2234
2235 if (aInfo == null) {
2236 // Nobody who is next!
2237 return false;
2238 }
2239
2240 intent.setComponent(new ComponentName(
2241 aInfo.applicationInfo.packageName, aInfo.name));
2242 intent.setFlags(intent.getFlags()&~(
2243 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
2244 Intent.FLAG_ACTIVITY_CLEAR_TOP|
2245 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
2246 Intent.FLAG_ACTIVITY_NEW_TASK));
2247
2248 // Okay now we need to start the new activity, replacing the
2249 // currently running activity. This is a little tricky because
2250 // we want to start the new one as if the current one is finished,
2251 // but not finish the current one first so that there is no flicker.
2252 // And thus...
2253 final boolean wasFinishing = r.finishing;
2254 r.finishing = true;
2255
2256 // Propagate reply information over to the new activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002257 final ActivityRecord resultTo = r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 final String resultWho = r.resultWho;
2259 final int requestCode = r.requestCode;
2260 r.resultTo = null;
2261 if (resultTo != null) {
2262 resultTo.removeResultsLocked(r, resultWho, requestCode);
2263 }
2264
2265 final long origId = Binder.clearCallingIdentity();
2266 // XXX we are not dealing with propagating grantedUriPermissions...
2267 // those are not yet exposed to user code, so there is no need.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002268 int res = mMainStack.startActivityLocked(r.app.thread, intent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 r.resolvedType, null, 0, aInfo, resultTo, resultWho,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002270 requestCode, -1, r.launchedFromUid, false, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 Binder.restoreCallingIdentity(origId);
2272
2273 r.finishing = wasFinishing;
2274 if (res != START_SUCCESS) {
2275 return false;
2276 }
2277 return true;
2278 }
2279 }
2280
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002281 public final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 Intent intent, String resolvedType, IBinder resultTo,
2283 String resultWho, int requestCode, boolean onlyIfNeeded) {
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002284
2285 // This is so super not safe, that only the system (or okay root)
2286 // can do it.
2287 final int callingUid = Binder.getCallingUid();
2288 if (callingUid != 0 && callingUid != Process.myUid()) {
2289 throw new SecurityException(
2290 "startActivityInPackage only available to the system");
2291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002293 return mMainStack.startActivityMayWait(null, uid, intent, resolvedType,
2294 null, 0, resultTo, resultWho, requestCode, onlyIfNeeded, false, null, null);
2295 }
2296
2297 public final int startActivities(IApplicationThread caller,
2298 Intent[] intents, String[] resolvedTypes, IBinder resultTo) {
2299 return mMainStack.startActivities(caller, -1, intents, resolvedTypes, resultTo);
2300 }
2301
2302 public final int startActivitiesInPackage(int uid,
2303 Intent[] intents, String[] resolvedTypes, IBinder resultTo) {
2304
2305 // This is so super not safe, that only the system (or okay root)
2306 // can do it.
2307 final int callingUid = Binder.getCallingUid();
2308 if (callingUid != 0 && callingUid != Process.myUid()) {
2309 throw new SecurityException(
2310 "startActivityInPackage only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312
Dianne Hackborn621e17d2010-11-22 15:59:56 -08002313 return mMainStack.startActivities(null, uid, intents, resolvedTypes, resultTo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002316 final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 int N = mRecentTasks.size();
Dianne Hackborn7c0e75e2010-12-21 19:15:40 -08002318 // Quick case: check if the top-most recent task is the same.
2319 if (N > 0 && mRecentTasks.get(0) == task) {
2320 return;
2321 }
2322 // Remove any existing entries that are the same kind of task.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 for (int i=0; i<N; i++) {
2324 TaskRecord tr = mRecentTasks.get(i);
2325 if ((task.affinity != null && task.affinity.equals(tr.affinity))
2326 || (task.intent != null && task.intent.filterEquals(tr.intent))) {
2327 mRecentTasks.remove(i);
2328 i--;
2329 N--;
2330 if (task.intent == null) {
2331 // If the new recent task we are adding is not fully
2332 // specified, then replace it with the existing recent task.
2333 task = tr;
2334 }
2335 }
2336 }
2337 if (N >= MAX_RECENT_TASKS) {
2338 mRecentTasks.remove(N-1);
2339 }
2340 mRecentTasks.add(0, task);
2341 }
2342
2343 public void setRequestedOrientation(IBinder token,
2344 int requestedOrientation) {
2345 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002346 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 if (index < 0) {
2348 return;
2349 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002350 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 final long origId = Binder.clearCallingIdentity();
2352 mWindowManager.setAppOrientation(r, requestedOrientation);
2353 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002354 mConfiguration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002355 r.mayFreezeScreenLocked(r.app) ? r : null);
2356 if (config != null) {
2357 r.frozenBeforeDestroy = true;
2358 if (!updateConfigurationLocked(config, r)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002359 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 }
2361 }
2362 Binder.restoreCallingIdentity(origId);
2363 }
2364 }
2365
2366 public int getRequestedOrientation(IBinder token) {
2367 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002368 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 if (index < 0) {
2370 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2371 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002372 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 return mWindowManager.getAppOrientation(r);
2374 }
2375 }
2376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 /**
2378 * This is the internal entry point for handling Activity.finish().
2379 *
2380 * @param token The Binder token referencing the Activity we want to finish.
2381 * @param resultCode Result code, if any, from this Activity.
2382 * @param resultData Result data (Intent), if any, from this Activity.
2383 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11002384 * @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 -08002385 */
2386 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
2387 // Refuse possible leaked file descriptors
2388 if (resultData != null && resultData.hasFileDescriptors() == true) {
2389 throw new IllegalArgumentException("File descriptors passed in Intent");
2390 }
2391
2392 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002393 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 // Find the first activity that is not finishing.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002395 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 if (next != null) {
2397 // ask watcher if this is allowed
2398 boolean resumeOK = true;
2399 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002400 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002402 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
2405 if (!resumeOK) {
2406 return false;
2407 }
2408 }
2409 }
2410 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002411 boolean res = mMainStack.requestFinishActivityLocked(token, resultCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 resultData, "app-request");
2413 Binder.restoreCallingIdentity(origId);
2414 return res;
2415 }
2416 }
2417
Dianne Hackborn860755f2010-06-03 18:47:52 -07002418 public final void finishHeavyWeightApp() {
2419 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2420 != PackageManager.PERMISSION_GRANTED) {
2421 String msg = "Permission Denial: finishHeavyWeightApp() from pid="
2422 + Binder.getCallingPid()
2423 + ", uid=" + Binder.getCallingUid()
2424 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2425 Slog.w(TAG, msg);
2426 throw new SecurityException(msg);
2427 }
2428
2429 synchronized(this) {
2430 if (mHeavyWeightProcess == null) {
2431 return;
2432 }
2433
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002434 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
Dianne Hackborn860755f2010-06-03 18:47:52 -07002435 mHeavyWeightProcess.activities);
2436 for (int i=0; i<activities.size(); i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002437 ActivityRecord r = activities.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002438 if (!r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002439 int index = mMainStack.indexOfTokenLocked(r);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002440 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002441 mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED,
Dianne Hackborn860755f2010-06-03 18:47:52 -07002442 null, "finish-heavy");
2443 }
2444 }
2445 }
2446
2447 mHeavyWeightProcess = null;
2448 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
2449 }
2450 }
2451
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002452 public void crashApplication(int uid, int initialPid, String packageName,
2453 String message) {
2454 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2455 != PackageManager.PERMISSION_GRANTED) {
2456 String msg = "Permission Denial: crashApplication() from pid="
2457 + Binder.getCallingPid()
2458 + ", uid=" + Binder.getCallingUid()
2459 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2460 Slog.w(TAG, msg);
2461 throw new SecurityException(msg);
2462 }
2463
2464 synchronized(this) {
2465 ProcessRecord proc = null;
2466
2467 // Figure out which process to kill. We don't trust that initialPid
2468 // still has any relation to current pids, so must scan through the
2469 // list.
2470 synchronized (mPidsSelfLocked) {
2471 for (int i=0; i<mPidsSelfLocked.size(); i++) {
2472 ProcessRecord p = mPidsSelfLocked.valueAt(i);
2473 if (p.info.uid != uid) {
2474 continue;
2475 }
2476 if (p.pid == initialPid) {
2477 proc = p;
2478 break;
2479 }
2480 for (String str : p.pkgList) {
2481 if (str.equals(packageName)) {
2482 proc = p;
2483 }
2484 }
2485 }
2486 }
2487
2488 if (proc == null) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07002489 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002490 + " initialPid=" + initialPid
2491 + " packageName=" + packageName);
2492 return;
2493 }
2494
2495 if (proc.thread != null) {
Dianne Hackborn9f531192010-08-04 17:48:03 -07002496 if (proc.pid == Process.myPid()) {
2497 Log.w(TAG, "crashApplication: trying to crash self!");
2498 return;
2499 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002500 long ident = Binder.clearCallingIdentity();
2501 try {
2502 proc.thread.scheduleCrash(message);
2503 } catch (RemoteException e) {
2504 }
2505 Binder.restoreCallingIdentity(ident);
2506 }
2507 }
2508 }
2509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 public final void finishSubActivity(IBinder token, String resultWho,
2511 int requestCode) {
2512 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002513 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 if (index < 0) {
2515 return;
2516 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002517 ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518
2519 final long origId = Binder.clearCallingIdentity();
2520
2521 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002522 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2523 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 if (r.resultTo == self && r.requestCode == requestCode) {
2525 if ((r.resultWho == null && resultWho == null) ||
2526 (r.resultWho != null && r.resultWho.equals(resultWho))) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002527 mMainStack.finishActivityLocked(r, i,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 Activity.RESULT_CANCELED, null, "request-sub");
2529 }
2530 }
2531 }
2532
2533 Binder.restoreCallingIdentity(origId);
2534 }
2535 }
2536
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002537 public boolean willActivityBeVisible(IBinder token) {
2538 synchronized(this) {
2539 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2541 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002542 if (r == token) {
2543 return true;
2544 }
2545 if (r.fullscreen && !r.finishing) {
2546 return false;
2547 }
2548 }
2549 return true;
2550 }
2551 }
2552
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002553 public void overridePendingTransition(IBinder token, String packageName,
2554 int enterAnim, int exitAnim) {
2555 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002556 int index = mMainStack.indexOfTokenLocked(token);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002557 if (index < 0) {
2558 return;
2559 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002560 ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002561
2562 final long origId = Binder.clearCallingIdentity();
2563
2564 if (self.state == ActivityState.RESUMED
2565 || self.state == ActivityState.PAUSING) {
2566 mWindowManager.overridePendingAppTransition(packageName,
2567 enterAnim, exitAnim);
2568 }
2569
2570 Binder.restoreCallingIdentity(origId);
2571 }
2572 }
2573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 * Main function for removing an existing process from the activity manager
2576 * as a result of that process going away. Clears out all connections
2577 * to the process.
2578 */
2579 private final void handleAppDiedLocked(ProcessRecord app,
2580 boolean restarting) {
2581 cleanUpApplicationRecordLocked(app, restarting, -1);
2582 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002583 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585
2586 // Just in case...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002587 if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) {
2588 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity);
2589 mMainStack.mPausingActivity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002591 if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) {
2592 mMainStack.mLastPausedActivity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 }
2594
2595 // Remove this application's activities from active lists.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002596 mMainStack.removeHistoryRecordsForAppLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597
2598 boolean atTop = true;
2599 boolean hasVisibleActivities = false;
2600
2601 // Clean out the history list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002602 int i = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002603 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 TAG, "Removing app " + app + " from history with " + i + " entries");
2605 while (i > 0) {
2606 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002607 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002608 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2610 if (r.app == app) {
2611 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002612 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 TAG, "Removing this entry! frozen=" + r.haveState
2614 + " finishing=" + r.finishing);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002615 mMainStack.mHistory.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616
2617 r.inHistory = false;
2618 mWindowManager.removeAppToken(r);
2619 if (VALIDATE_TOKENS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002620 mWindowManager.validateAppTokens(mMainStack.mHistory);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002622 r.removeUriPermissionsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623
2624 } else {
2625 // We have the current state for this activity, so
2626 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002627 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 TAG, "Keeping entry, setting app to null");
2629 if (r.visible) {
2630 hasVisibleActivities = true;
2631 }
2632 r.app = null;
2633 r.nowVisible = false;
2634 if (!r.haveState) {
2635 r.icicle = null;
2636 }
2637 }
2638
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002639 r.stack.cleanUpActivityLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 r.state = ActivityState.STOPPED;
2641 }
2642 atTop = false;
2643 }
2644
2645 app.activities.clear();
2646
2647 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002648 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 + " running instrumentation " + app.instrumentationClass);
2650 Bundle info = new Bundle();
2651 info.putString("shortMsg", "Process crashed.");
2652 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
2653 }
2654
2655 if (!restarting) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002656 if (!mMainStack.resumeTopActivityLocked(null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 // If there was nothing to resume, and we are not already
2658 // restarting this process, but there is a visible activity that
2659 // is hosted by the process... then make sure all visible
2660 // activities are running, taking care of restarting this
2661 // process.
2662 if (hasVisibleActivities) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002663 mMainStack.ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 }
2665 }
2666 }
2667 }
2668
2669 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
2670 IBinder threadBinder = thread.asBinder();
2671
2672 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002673 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2674 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002675 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
2676 return i;
2677 }
2678 }
2679 return -1;
2680 }
2681
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002682 final ProcessRecord getRecordForAppLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 IApplicationThread thread) {
2684 if (thread == null) {
2685 return null;
2686 }
2687
2688 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002689 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 }
2691
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002692 final void appDiedLocked(ProcessRecord app, int pid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 IApplicationThread thread) {
2694
2695 mProcDeaths[0]++;
2696
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002697 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
2698 synchronized (stats) {
2699 stats.noteProcessDiedLocked(app.info.uid, pid);
2700 }
2701
Magnus Edlund7bb25812010-02-24 15:45:06 +01002702 // Clean up already done if the process has been re-started.
2703 if (app.pid == pid && app.thread != null &&
2704 app.thread.asBinder() == thread.asBinder()) {
Dianne Hackborn906497c2010-05-10 15:57:38 -07002705 if (!app.killedBackground) {
2706 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
2707 + ") has died.");
2708 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08002709 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002710 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 TAG, "Dying app: " + app + ", pid: " + pid
2712 + ", thread: " + thread.asBinder());
2713 boolean doLowMem = app.instrumentationClass == null;
2714 handleAppDiedLocked(app, false);
2715
2716 if (doLowMem) {
2717 // If there are no longer any background processes running,
2718 // and the app that died was not running instrumentation,
2719 // then tell everyone we are now low on memory.
2720 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002721 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2722 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 if (rec.thread != null && rec.setAdj >= HIDDEN_APP_MIN_ADJ) {
2724 haveBg = true;
2725 break;
2726 }
2727 }
2728
2729 if (!haveBg) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002730 Slog.i(TAG, "Low Memory: No more background processes.");
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002731 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002732 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002733 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2734 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07002735 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002736 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
2737 // The low memory report is overriding any current
2738 // state for a GC request. Make sure to do
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002739 // heavy/important/visible/foreground processes first.
2740 if (rec.setAdj <= HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002741 rec.lastRequestedGc = 0;
2742 } else {
2743 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002745 rec.reportLowMemory = true;
2746 rec.lastLowMemory = now;
2747 mProcessesToGc.remove(rec);
2748 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
2750 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002751 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
2753 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01002754 } else if (app.pid != pid) {
2755 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002756 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01002757 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08002758 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08002759 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002760 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 + thread.asBinder());
2762 }
2763 }
2764
Dan Egnor42471dd2010-01-07 17:25:22 -08002765 /**
2766 * If a stack trace dump file is configured, dump process stack traces.
Christopher Tate6ee412d2010-05-28 12:01:56 -07002767 * @param clearTraces causes the dump file to be erased prior to the new
2768 * traces being written, if true; when false, the new traces will be
2769 * appended to any existing file content.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002770 * @param firstPids of dalvik VM processes to dump stack traces for first
2771 * @param lastPids of dalvik VM processes to dump stack traces for last
Dan Egnor42471dd2010-01-07 17:25:22 -08002772 * @return file containing stack traces, or null if no dump file is configured
2773 */
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002774 public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids,
2775 ProcessStats processStats, SparseArray<Boolean> lastPids) {
Dan Egnor42471dd2010-01-07 17:25:22 -08002776 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
2777 if (tracesPath == null || tracesPath.length() == 0) {
2778 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002780
2781 File tracesFile = new File(tracesPath);
2782 try {
2783 File tracesDir = tracesFile.getParentFile();
2784 if (!tracesDir.exists()) tracesFile.mkdirs();
2785 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
2786
Christopher Tate6ee412d2010-05-28 12:01:56 -07002787 if (clearTraces && tracesFile.exists()) tracesFile.delete();
Dan Egnor42471dd2010-01-07 17:25:22 -08002788 tracesFile.createNewFile();
2789 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
2790 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002791 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08002792 return null;
2793 }
2794
2795 // Use a FileObserver to detect when traces finish writing.
2796 // The order of traces is considered important to maintain for legibility.
2797 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
2798 public synchronized void onEvent(int event, String path) { notify(); }
2799 };
2800
2801 try {
2802 observer.startWatching();
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002803
2804 // First collect all of the stacks of the most important pids.
2805 try {
2806 int num = firstPids.size();
2807 for (int i = 0; i < num; i++) {
2808 synchronized (observer) {
2809 Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
2810 observer.wait(200); // Wait for write-close, give up after 200msec
2811 }
2812 }
2813 } catch (InterruptedException e) {
2814 Log.wtf(TAG, e);
2815 }
2816
2817 // Next measure CPU usage.
2818 if (processStats != null) {
2819 processStats.init();
2820 System.gc();
2821 processStats.update();
2822 try {
2823 synchronized (processStats) {
2824 processStats.wait(500); // measure over 1/2 second.
2825 }
2826 } catch (InterruptedException e) {
2827 }
2828 processStats.update();
2829
2830 // We'll take the stack crawls of just the top apps using CPU.
2831 final int N = processStats.countWorkingStats();
2832 int numProcs = 0;
2833 for (int i=0; i<N && numProcs<5; i++) {
2834 ProcessStats.Stats stats = processStats.getWorkingStats(i);
2835 if (lastPids.indexOfKey(stats.pid) >= 0) {
2836 numProcs++;
2837 try {
2838 synchronized (observer) {
2839 Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
2840 observer.wait(200); // Wait for write-close, give up after 200msec
2841 }
2842 } catch (InterruptedException e) {
2843 Log.wtf(TAG, e);
2844 }
2845
2846 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002847 }
2848 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002849
2850 return tracesFile;
2851
Dan Egnor42471dd2010-01-07 17:25:22 -08002852 } finally {
2853 observer.stopWatching();
2854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 }
2856
Jeff Brown4d94a762010-09-23 11:33:28 -07002857 private final class AppNotResponding implements Runnable {
2858 private final ProcessRecord mApp;
2859 private final String mAnnotation;
2860
2861 public AppNotResponding(ProcessRecord app, String annotation) {
2862 mApp = app;
2863 mAnnotation = annotation;
2864 }
2865
2866 @Override
2867 public void run() {
2868 appNotResponding(mApp, null, null, mAnnotation);
2869 }
2870 }
2871
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002872 final void appNotResponding(ProcessRecord app, ActivityRecord activity,
2873 ActivityRecord parent, final String annotation) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002874 ArrayList<Integer> firstPids = new ArrayList<Integer>(5);
2875 SparseArray<Boolean> lastPids = new SparseArray<Boolean>(20);
2876
Dianne Hackborn287952c2010-09-22 22:34:31 -07002877 if (mController != null) {
2878 try {
2879 // 0 == continue, -1 = kill process immediately
2880 int res = mController.appEarlyNotResponding(app.processName, app.pid, annotation);
2881 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
2882 } catch (RemoteException e) {
2883 mController = null;
2884 }
2885 }
2886
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002887 long anrTime = SystemClock.uptimeMillis();
2888 if (MONITOR_CPU_USAGE) {
2889 updateCpuStatsNow();
2890 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002891
2892 synchronized (this) {
2893 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
2894 if (mShuttingDown) {
2895 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
2896 return;
2897 } else if (app.notResponding) {
2898 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
2899 return;
2900 } else if (app.crashing) {
2901 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
2902 return;
2903 }
2904
2905 // In case we come through here for the same app before completing
2906 // this one, mark as anring now so we will bail out.
2907 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08002908
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002909 // Log the ANR to the event log.
2910 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
2911 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08002912
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002913 // Dump thread traces as quickly as we can, starting with "interesting" processes.
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002914 firstPids.add(app.pid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002915
2916 int parentPid = app.pid;
2917 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002918 if (parentPid != app.pid) firstPids.add(parentPid);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002919
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002920 if (MY_PID != app.pid && MY_PID != parentPid) firstPids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08002921
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002922 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
2923 ProcessRecord r = mLruProcesses.get(i);
2924 if (r != null && r.thread != null) {
2925 int pid = r.pid;
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002926 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) {
2927 if (r.persistent) {
2928 firstPids.add(pid);
2929 } else {
2930 lastPids.put(pid, Boolean.TRUE);
2931 }
2932 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 }
2935 }
2936
Dan Egnor42471dd2010-01-07 17:25:22 -08002937 // Log the ANR to the main log.
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002938 StringBuilder info = mStringBuilder;
2939 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08002940 info.append("ANR in ").append(app.processName);
2941 if (activity != null && activity.shortComponentName != null) {
2942 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07002943 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08002944 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08002946 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002948 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08002949 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951
Dianne Hackborn287952c2010-09-22 22:34:31 -07002952 final ProcessStats processStats = new ProcessStats(true);
2953
2954 File tracesFile = dumpStackTraces(true, firstPids, processStats, lastPids);
2955
Dan Egnor42471dd2010-01-07 17:25:22 -08002956 String cpuInfo = null;
2957 if (MONITOR_CPU_USAGE) {
2958 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002959 synchronized (mProcessStatsThread) {
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002960 cpuInfo = mProcessStats.printCurrentState(anrTime);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002961 }
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002962 info.append(processStats.printCurrentLoad());
Dan Egnor42471dd2010-01-07 17:25:22 -08002963 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965
Dianne Hackborn6b1afeb2010-08-31 15:40:21 -07002966 info.append(processStats.printCurrentState(anrTime));
2967
Joe Onorato8a9b2202010-02-26 18:56:32 -08002968 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08002969 if (tracesFile == null) {
2970 // There is no trace file, so dump (only) the alleged culprit's threads to the log
2971 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
2972 }
2973
2974 addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null);
2975
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002976 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08002978 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
2979 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08002981 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
2982 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 }
2984 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002985 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 }
2987 }
2988
Dan Egnor42471dd2010-01-07 17:25:22 -08002989 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
2990 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
2991 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002992
2993 synchronized (this) {
2994 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
2995 Process.killProcess(app.pid);
2996 return;
2997 }
2998
2999 // Set the app's notResponding state, and look up the errorReportReceiver
3000 makeAppNotRespondingLocked(app,
3001 activity != null ? activity.shortComponentName : null,
3002 annotation != null ? "ANR " + annotation : "ANR",
3003 info.toString());
3004
3005 // Bring up the infamous App Not Responding dialog
3006 Message msg = Message.obtain();
3007 HashMap map = new HashMap();
3008 msg.what = SHOW_NOT_RESPONDING_MSG;
3009 msg.obj = map;
3010 map.put("app", app);
3011 if (activity != null) {
3012 map.put("activity", activity);
3013 }
3014
3015 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08003016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 }
3018
Dianne Hackborn0dad3642010-09-09 21:25:35 -07003019 final void showLaunchWarningLocked(final ActivityRecord cur, final ActivityRecord next) {
3020 if (!mLaunchWarningShown) {
3021 mLaunchWarningShown = true;
3022 mHandler.post(new Runnable() {
3023 @Override
3024 public void run() {
3025 synchronized (ActivityManagerService.this) {
3026 final Dialog d = new LaunchWarningWindow(mContext, cur, next);
3027 d.show();
3028 mHandler.postDelayed(new Runnable() {
3029 @Override
3030 public void run() {
3031 synchronized (ActivityManagerService.this) {
3032 d.dismiss();
3033 mLaunchWarningShown = false;
3034 }
3035 }
3036 }, 4000);
3037 }
3038 }
3039 });
3040 }
3041 }
3042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 public boolean clearApplicationUserData(final String packageName,
3044 final IPackageDataObserver observer) {
3045 int uid = Binder.getCallingUid();
3046 int pid = Binder.getCallingPid();
3047 long callingId = Binder.clearCallingIdentity();
3048 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003049 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 int pkgUid = -1;
3051 synchronized(this) {
3052 try {
3053 pkgUid = pm.getPackageUid(packageName);
3054 } catch (RemoteException e) {
3055 }
3056 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003057 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 return false;
3059 }
3060 if (uid == pkgUid || checkComponentPermission(
3061 android.Manifest.permission.CLEAR_APP_USER_DATA,
3062 pid, uid, -1)
3063 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003064 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 } else {
3066 throw new SecurityException(pid+" does not have permission:"+
3067 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
3068 "for process:"+packageName);
3069 }
3070 }
3071
3072 try {
3073 //clear application user data
3074 pm.clearApplicationUserData(packageName, observer);
3075 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
3076 Uri.fromParts("package", packageName, null));
3077 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003078 broadcastIntentInPackage("android", Process.SYSTEM_UID, intent,
3079 null, null, 0, null, null, null, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 } catch (RemoteException e) {
3081 }
3082 } finally {
3083 Binder.restoreCallingIdentity(callingId);
3084 }
3085 return true;
3086 }
3087
Dianne Hackborn03abb812010-01-04 18:43:19 -08003088 public void killBackgroundProcesses(final String packageName) {
3089 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
3090 != PackageManager.PERMISSION_GRANTED &&
3091 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
3092 != PackageManager.PERMISSION_GRANTED) {
3093 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 + Binder.getCallingPid()
3095 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08003096 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003097 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 throw new SecurityException(msg);
3099 }
3100
3101 long callingId = Binder.clearCallingIdentity();
3102 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003103 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 int pkgUid = -1;
3105 synchronized(this) {
3106 try {
3107 pkgUid = pm.getPackageUid(packageName);
3108 } catch (RemoteException e) {
3109 }
3110 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003111 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 return;
3113 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003114 killPackageProcessesLocked(packageName, pkgUid,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003115 SECONDARY_SERVER_ADJ, false, true);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003116 }
3117 } finally {
3118 Binder.restoreCallingIdentity(callingId);
3119 }
3120 }
3121
3122 public void forceStopPackage(final String packageName) {
3123 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
3124 != PackageManager.PERMISSION_GRANTED) {
3125 String msg = "Permission Denial: forceStopPackage() from pid="
3126 + Binder.getCallingPid()
3127 + ", uid=" + Binder.getCallingUid()
3128 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003129 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003130 throw new SecurityException(msg);
3131 }
3132
3133 long callingId = Binder.clearCallingIdentity();
3134 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003135 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn03abb812010-01-04 18:43:19 -08003136 int pkgUid = -1;
3137 synchronized(this) {
3138 try {
3139 pkgUid = pm.getPackageUid(packageName);
3140 } catch (RemoteException e) {
3141 }
3142 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003143 Slog.w(TAG, "Invalid packageName: " + packageName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003144 return;
3145 }
3146 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
3148 } finally {
3149 Binder.restoreCallingIdentity(callingId);
3150 }
3151 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003152
3153 /*
3154 * The pkg name and uid have to be specified.
3155 * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
3156 */
3157 public void killApplicationWithUid(String pkg, int uid) {
3158 if (pkg == null) {
3159 return;
3160 }
3161 // Make sure the uid is valid.
3162 if (uid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003163 Slog.w(TAG, "Invalid uid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003164 return;
3165 }
3166 int callerUid = Binder.getCallingUid();
3167 // Only the system server can kill an application
3168 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003169 // Post an aysnc message to kill the application
3170 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
3171 msg.arg1 = uid;
3172 msg.arg2 = 0;
3173 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07003174 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003175 } else {
3176 throw new SecurityException(callerUid + " cannot kill pkg: " +
3177 pkg);
3178 }
3179 }
3180
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003181 public void closeSystemDialogs(String reason) {
3182 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003183 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003184 if (reason != null) {
3185 intent.putExtra("reason", reason);
3186 }
3187
3188 final int uid = Binder.getCallingUid();
3189 final long origId = Binder.clearCallingIdentity();
3190 synchronized (this) {
3191 int i = mWatchers.beginBroadcast();
3192 while (i > 0) {
3193 i--;
3194 IActivityWatcher w = mWatchers.getBroadcastItem(i);
3195 if (w != null) {
3196 try {
3197 w.closingSystemDialogs(reason);
3198 } catch (RemoteException e) {
3199 }
3200 }
3201 }
3202 mWatchers.finishBroadcast();
3203
Dianne Hackbornffa42482009-09-23 22:20:11 -07003204 mWindowManager.closeSystemDialogs(reason);
3205
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003206 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
3207 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornffa42482009-09-23 22:20:11 -07003208 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003209 r.stack.finishActivityLocked(r, i,
Dianne Hackbornffa42482009-09-23 22:20:11 -07003210 Activity.RESULT_CANCELED, null, "close-sys");
3211 }
3212 }
3213
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003214 broadcastIntentLocked(null, null, intent, null,
3215 null, 0, null, null, null, false, false, -1, uid);
3216 }
3217 Binder.restoreCallingIdentity(origId);
3218 }
3219
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003220 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003221 throws RemoteException {
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003222 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
3223 for (int i=pids.length-1; i>=0; i--) {
3224 infos[i] = new Debug.MemoryInfo();
3225 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003226 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003227 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003228 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07003229
3230 public void killApplicationProcess(String processName, int uid) {
3231 if (processName == null) {
3232 return;
3233 }
3234
3235 int callerUid = Binder.getCallingUid();
3236 // Only the system server can kill an application
3237 if (callerUid == Process.SYSTEM_UID) {
3238 synchronized (this) {
3239 ProcessRecord app = getProcessRecordLocked(processName, uid);
3240 if (app != null) {
3241 try {
3242 app.thread.scheduleSuicide();
3243 } catch (RemoteException e) {
3244 // If the other end already died, then our work here is done.
3245 }
3246 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003247 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07003248 + processName + " / " + uid);
3249 }
3250 }
3251 } else {
3252 throw new SecurityException(callerUid + " cannot kill app process: " +
3253 processName);
3254 }
3255 }
3256
Dianne Hackborn03abb812010-01-04 18:43:19 -08003257 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003258 forceStopPackageLocked(packageName, uid, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
3260 Uri.fromParts("package", packageName, null));
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003261 if (!mProcessesReady) {
3262 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
3263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 intent.putExtra(Intent.EXTRA_UID, uid);
3265 broadcastIntentLocked(null, null, intent,
3266 null, null, 0, null, null, null,
3267 false, false, MY_PID, Process.SYSTEM_UID);
3268 }
3269
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003270 private final boolean killPackageProcessesLocked(String packageName, int uid,
3271 int minOomAdj, boolean callerWillRestart, boolean doit) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003272 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273
Dianne Hackborn03abb812010-01-04 18:43:19 -08003274 // Remove all processes this package may have touched: all with the
3275 // same UID (except for the system or root user), and all whose name
3276 // matches the package name.
3277 final String procNamePrefix = packageName + ":";
3278 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3279 final int NA = apps.size();
3280 for (int ia=0; ia<NA; ia++) {
3281 ProcessRecord app = apps.valueAt(ia);
3282 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003283 if (doit) {
3284 procs.add(app);
3285 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003286 } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid)
3287 || app.processName.equals(packageName)
3288 || app.processName.startsWith(procNamePrefix)) {
3289 if (app.setAdj >= minOomAdj) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003290 if (!doit) {
3291 return true;
3292 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003293 app.removed = true;
3294 procs.add(app);
3295 }
3296 }
3297 }
3298 }
3299
3300 int N = procs.size();
3301 for (int i=0; i<N; i++) {
3302 removeProcessLocked(procs.get(i), callerWillRestart);
3303 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003304 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003305 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003306
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003307 private final boolean forceStopPackageLocked(String name, int uid,
3308 boolean callerWillRestart, boolean purgeCache, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 int i, N;
3310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 if (uid < 0) {
3312 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003313 uid = AppGlobals.getPackageManager().getPackageUid(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 } catch (RemoteException e) {
3315 }
3316 }
3317
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003318 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003319 Slog.i(TAG, "Force stopping package " + name + " uid=" + uid);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003320
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003321 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
3322 while (badApps.hasNext()) {
3323 SparseArray<Long> ba = badApps.next();
3324 if (ba.get(uid) != null) {
3325 badApps.remove();
3326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 }
3328 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003329
3330 boolean didSomething = killPackageProcessesLocked(name, uid, -100,
3331 callerWillRestart, doit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003333 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
3334 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 if (r.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003336 if (!doit) {
3337 return true;
3338 }
3339 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003340 Slog.i(TAG, " Force finishing activity " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 if (r.app != null) {
3342 r.app.removed = true;
3343 }
3344 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003345 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "uninstall");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347 }
3348
3349 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
3350 for (ServiceRecord service : mServices.values()) {
3351 if (service.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003352 if (!doit) {
3353 return true;
3354 }
3355 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003356 Slog.i(TAG, " Force stopping service " + service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 if (service.app != null) {
3358 service.app.removed = true;
3359 }
3360 service.app = null;
3361 services.add(service);
3362 }
3363 }
3364
3365 N = services.size();
3366 for (i=0; i<N; i++) {
3367 bringDownServiceLocked(services.get(i), true);
3368 }
3369
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003370 if (doit) {
3371 if (purgeCache) {
3372 AttributeCache ac = AttributeCache.instance();
3373 if (ac != null) {
3374 ac.removePackage(name);
3375 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003376 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003377 mMainStack.resumeTopActivityLocked(null);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003378 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003379
3380 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382
3383 private final boolean removeProcessLocked(ProcessRecord app, boolean callerWillRestart) {
3384 final String name = app.processName;
3385 final int uid = app.info.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003386 if (DEBUG_PROCESSES) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 TAG, "Force removing process " + app + " (" + name
3388 + "/" + uid + ")");
3389
3390 mProcessNames.remove(name, uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003391 if (mHeavyWeightProcess == app) {
3392 mHeavyWeightProcess = null;
3393 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 boolean needRestart = false;
3396 if (app.pid > 0 && app.pid != MY_PID) {
3397 int pid = app.pid;
3398 synchronized (mPidsSelfLocked) {
3399 mPidsSelfLocked.remove(pid);
3400 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3401 }
3402 handleAppDiedLocked(app, true);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003403 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 Process.killProcess(pid);
3405
3406 if (app.persistent) {
3407 if (!callerWillRestart) {
3408 addAppLocked(app.info);
3409 } else {
3410 needRestart = true;
3411 }
3412 }
3413 } else {
3414 mRemovedProcesses.add(app);
3415 }
3416
3417 return needRestart;
3418 }
3419
3420 private final void processStartTimedOutLocked(ProcessRecord app) {
3421 final int pid = app.pid;
3422 boolean gone = false;
3423 synchronized (mPidsSelfLocked) {
3424 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
3425 if (knownApp != null && knownApp.thread == null) {
3426 mPidsSelfLocked.remove(pid);
3427 gone = true;
3428 }
3429 }
3430
3431 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003432 Slog.w(TAG, "Process " + app + " failed to attach");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003433 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003434 app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 mProcessNames.remove(app.processName, app.info.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003436 if (mHeavyWeightProcess == app) {
3437 mHeavyWeightProcess = null;
3438 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3439 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003440 // Take care of any launching providers waiting for this process.
3441 checkAppInLaunchingProvidersLocked(app, true);
3442 // Take care of any services that are waiting for the process.
3443 for (int i=0; i<mPendingServices.size(); i++) {
3444 ServiceRecord sr = mPendingServices.get(i);
3445 if (app.info.uid == sr.appInfo.uid
3446 && app.processName.equals(sr.processName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003447 Slog.w(TAG, "Forcing bringing down service: " + sr);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003448 mPendingServices.remove(i);
3449 i--;
3450 bringDownServiceLocked(sr, true);
3451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003453 Process.killProcess(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07003454 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003455 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07003456 try {
3457 IBackupManager bm = IBackupManager.Stub.asInterface(
3458 ServiceManager.getService(Context.BACKUP_SERVICE));
3459 bm.agentDisconnected(app.info.packageName);
3460 } catch (RemoteException e) {
3461 // Can't happen; the backup manager is local
3462 }
3463 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003464 if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003465 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003466 mPendingBroadcast.state = BroadcastRecord.IDLE;
3467 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003468 mPendingBroadcast = null;
3469 scheduleBroadcastsLocked();
3470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003472 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 }
3474 }
3475
3476 private final boolean attachApplicationLocked(IApplicationThread thread,
3477 int pid) {
3478
3479 // Find the application record that is being attached... either via
3480 // the pid if we are running in multiple processes, or just pull the
3481 // next app record if we are emulating process with anonymous threads.
3482 ProcessRecord app;
3483 if (pid != MY_PID && pid >= 0) {
3484 synchronized (mPidsSelfLocked) {
3485 app = mPidsSelfLocked.get(pid);
3486 }
3487 } else if (mStartingProcesses.size() > 0) {
3488 app = mStartingProcesses.remove(0);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003489 app.setPid(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 } else {
3491 app = null;
3492 }
3493
3494 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003495 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003497 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 if (pid > 0 && pid != MY_PID) {
3499 Process.killProcess(pid);
3500 } else {
3501 try {
3502 thread.scheduleExit();
3503 } catch (Exception e) {
3504 // Ignore exceptions.
3505 }
3506 }
3507 return false;
3508 }
3509
3510 // If this application record is still attached to a previous
3511 // process, clean it up now.
3512 if (app.thread != null) {
3513 handleAppDiedLocked(app, true);
3514 }
3515
3516 // Tell the process all about itself.
3517
Joe Onorato8a9b2202010-02-26 18:56:32 -08003518 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 TAG, "Binding process pid " + pid + " to record " + app);
3520
3521 String processName = app.processName;
3522 try {
3523 thread.asBinder().linkToDeath(new AppDeathRecipient(
3524 app, pid, thread), 0);
3525 } catch (RemoteException e) {
3526 app.resetPackageList();
3527 startProcessLocked(app, "link fail", processName);
3528 return false;
3529 }
3530
Doug Zongker2bec3d42009-12-04 12:52:44 -08003531 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532
3533 app.thread = thread;
3534 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08003535 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
3536 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 app.forcingToForeground = null;
3538 app.foregroundServices = false;
3539 app.debugging = false;
3540
3541 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3542
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003543 boolean normalMode = mProcessesReady || isAllowedWhileBooting(app.info);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003544 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003546 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003547 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003548 }
3549
Joe Onorato8a9b2202010-02-26 18:56:32 -08003550 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 TAG, "New app record " + app
3552 + " thread=" + thread.asBinder() + " pid=" + pid);
3553 try {
3554 int testMode = IApplicationThread.DEBUG_OFF;
3555 if (mDebugApp != null && mDebugApp.equals(processName)) {
3556 testMode = mWaitForDebugger
3557 ? IApplicationThread.DEBUG_WAIT
3558 : IApplicationThread.DEBUG_ON;
3559 app.debugging = true;
3560 if (mDebugTransient) {
3561 mDebugApp = mOrigDebugApp;
3562 mWaitForDebugger = mOrigWaitForDebugger;
3563 }
3564 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003565
Christopher Tate181fafa2009-05-14 11:12:14 -07003566 // If the app is being launched for restore or full backup, set it up specially
3567 boolean isRestrictedBackupMode = false;
3568 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
3569 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
3570 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
3571 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003572
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07003573 ensurePackageDexOpt(app.instrumentationInfo != null
3574 ? app.instrumentationInfo.packageName
3575 : app.info.packageName);
3576 if (app.instrumentationClass != null) {
3577 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003578 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003579 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003580 + processName + " with config " + mConfiguration);
Dianne Hackborn1655be42009-05-08 14:29:01 -07003581 thread.bindApplication(processName, app.instrumentationInfo != null
3582 ? app.instrumentationInfo : app.info, providers,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 app.instrumentationClass, app.instrumentationProfileFile,
3584 app.instrumentationArguments, app.instrumentationWatcher, testMode,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003585 isRestrictedBackupMode || !normalMode,
3586 mConfiguration, getCommonServicesLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003587 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003588 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003589 } catch (Exception e) {
3590 // todo: Yikes! What should we do? For now we will try to
3591 // start another process, but that could easily get us in
3592 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08003593 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594
3595 app.resetPackageList();
3596 startProcessLocked(app, "bind fail", processName);
3597 return false;
3598 }
3599
3600 // Remove this record from the list of starting applications.
3601 mPersistentStartingProcesses.remove(app);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003602 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
3603 "Attach application locked removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 mProcessesOnHold.remove(app);
3605
3606 boolean badApp = false;
3607 boolean didSomething = false;
3608
3609 // See if the top visible activity is waiting to run in this process...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003610 ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
Christopher Tate04c0af82010-06-07 18:35:20 -07003611 if (hr != null && normalMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid
3613 && processName.equals(hr.processName)) {
3614 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003615 if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 didSomething = true;
3617 }
3618 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003619 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 + hr.intent.getComponent().flattenToShortString(), e);
3621 badApp = true;
3622 }
3623 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003624 mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 }
3626 }
3627
3628 // Find any services that should be running in this process...
3629 if (!badApp && mPendingServices.size() > 0) {
3630 ServiceRecord sr = null;
3631 try {
3632 for (int i=0; i<mPendingServices.size(); i++) {
3633 sr = mPendingServices.get(i);
3634 if (app.info.uid != sr.appInfo.uid
3635 || !processName.equals(sr.processName)) {
3636 continue;
3637 }
3638
3639 mPendingServices.remove(i);
3640 i--;
3641 realStartServiceLocked(sr, app);
3642 didSomething = true;
3643 }
3644 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003645 Slog.w(TAG, "Exception in new application when starting service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 + sr.shortName, e);
3647 badApp = true;
3648 }
3649 }
3650
3651 // Check if the next broadcast receiver is in this process...
3652 BroadcastRecord br = mPendingBroadcast;
3653 if (!badApp && br != null && br.curApp == app) {
3654 try {
3655 mPendingBroadcast = null;
3656 processCurBroadcastLocked(br, app);
3657 didSomething = true;
3658 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003659 Slog.w(TAG, "Exception in new application when starting receiver "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 + br.curComponent.flattenToShortString(), e);
3661 badApp = true;
Jeff Brown4d94a762010-09-23 11:33:28 -07003662 logBroadcastReceiverDiscardLocked(br);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 finishReceiverLocked(br.receiver, br.resultCode, br.resultData,
3664 br.resultExtras, br.resultAbort, true);
3665 scheduleBroadcastsLocked();
Magnus Edlund7bb25812010-02-24 15:45:06 +01003666 // We need to reset the state if we fails to start the receiver.
3667 br.state = BroadcastRecord.IDLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 }
3669 }
3670
Christopher Tate181fafa2009-05-14 11:12:14 -07003671 // Check whether the next backup agent is in this process...
3672 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003673 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003674 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07003675 try {
3676 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, mBackupTarget.backupMode);
3677 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003678 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07003679 e.printStackTrace();
3680 }
3681 }
3682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 if (badApp) {
3684 // todo: Also need to kill application to deal with all
3685 // kinds of exceptions.
3686 handleAppDiedLocked(app, false);
3687 return false;
3688 }
3689
3690 if (!didSomething) {
3691 updateOomAdjLocked();
3692 }
3693
3694 return true;
3695 }
3696
3697 public final void attachApplication(IApplicationThread thread) {
3698 synchronized (this) {
3699 int callingPid = Binder.getCallingPid();
3700 final long origId = Binder.clearCallingIdentity();
3701 attachApplicationLocked(thread, callingPid);
3702 Binder.restoreCallingIdentity(origId);
3703 }
3704 }
3705
Dianne Hackborne88846e2009-09-30 21:34:25 -07003706 public final void activityIdle(IBinder token, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003708 mMainStack.activityIdleInternal(token, false, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 Binder.restoreCallingIdentity(origId);
3710 }
3711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003713 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003714 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 mWindowManager.enableScreenAfterBoot();
3716 }
3717
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003718 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003719 IntentFilter pkgFilter = new IntentFilter();
3720 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
3721 pkgFilter.addDataScheme("package");
3722 mContext.registerReceiver(new BroadcastReceiver() {
3723 @Override
3724 public void onReceive(Context context, Intent intent) {
3725 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
3726 if (pkgs != null) {
3727 for (String pkg : pkgs) {
Vairavan Srinivasan61f07652010-07-22 13:36:40 -07003728 synchronized (ActivityManagerService.this) {
3729 if (forceStopPackageLocked(pkg, -1, false, false, false)) {
3730 setResultCode(Activity.RESULT_OK);
3731 return;
3732 }
3733 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003734 }
3735 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003736 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003737 }, pkgFilter);
3738
3739 synchronized (this) {
3740 // Ensure that any processes we had put on hold are now started
3741 // up.
3742 final int NP = mProcessesOnHold.size();
3743 if (NP > 0) {
3744 ArrayList<ProcessRecord> procs =
3745 new ArrayList<ProcessRecord>(mProcessesOnHold);
3746 for (int ip=0; ip<NP; ip++) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07003747 if (DEBUG_PROCESSES) Slog.v(TAG, "Starting process on hold: "
3748 + procs.get(ip));
3749 startProcessLocked(procs.get(ip), "on-hold", null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003750 }
3751 }
3752
3753 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003754 // Start looking for apps that are abusing wake locks.
3755 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07003756 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003757 // Tell anyone interested that we are done booting!
Dianne Hackbornf4c454b2010-08-11 12:47:41 -07003758 SystemProperties.set("sys.boot_completed", "1");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003759 broadcastIntentLocked(null, null,
3760 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
3761 null, null, 0, null, null,
3762 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
3763 false, false, MY_PID, Process.SYSTEM_UID);
3764 }
3765 }
3766 }
3767
3768 final void ensureBootCompleted() {
3769 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003770 boolean enableScreen;
3771 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003772 booting = mBooting;
3773 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003774 enableScreen = !mBooted;
3775 mBooted = true;
3776 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003777
3778 if (booting) {
3779 finishBooting();
3780 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003781
3782 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003783 enableScreenAfterBoot();
3784 }
3785 }
3786
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003787 public final void activityPaused(IBinder token) {
3788 final long origId = Binder.clearCallingIdentity();
3789 mMainStack.activityPaused(token, false);
3790 Binder.restoreCallingIdentity(origId);
3791 }
3792
3793 public final void activityStopped(IBinder token, Bundle icicle, Bitmap thumbnail,
3794 CharSequence description) {
3795 if (localLOGV) Slog.v(
3796 TAG, "Activity stopped: token=" + token);
3797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 // Refuse possible leaked file descriptors
3799 if (icicle != null && icicle.hasFileDescriptors()) {
3800 throw new IllegalArgumentException("File descriptors passed in Bundle");
3801 }
3802
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003803 ActivityRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804
3805 final long origId = Binder.clearCallingIdentity();
3806
3807 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003808 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003810 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003811 r.icicle = icicle;
3812 r.haveState = true;
3813 if (thumbnail != null) {
3814 r.thumbnail = thumbnail;
Dianne Hackbornd2835932010-12-13 16:28:46 -08003815 if (r.task != null) {
3816 r.task.lastThumbnail = r.thumbnail;
3817 }
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 r.description = description;
Dianne Hackbornd2835932010-12-13 16:28:46 -08003820 if (r.task != null) {
3821 r.task.lastDescription = r.description;
3822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 r.stopped = true;
3824 r.state = ActivityState.STOPPED;
3825 if (!r.finishing) {
3826 if (r.configDestroy) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003827 r.stack.destroyActivityLocked(r, true);
3828 r.stack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 }
3830 }
3831 }
3832 }
3833
3834 if (r != null) {
3835 sendPendingThumbnail(r, null, null, null, false);
3836 }
3837
3838 trimApplications();
3839
3840 Binder.restoreCallingIdentity(origId);
3841 }
3842
3843 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003844 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003845 mMainStack.activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 }
3847
3848 public String getCallingPackage(IBinder token) {
3849 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003850 ActivityRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07003851 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853 }
3854
3855 public ComponentName getCallingActivity(IBinder token) {
3856 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003857 ActivityRecord r = getCallingRecordLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003858 return r != null ? r.intent.getComponent() : null;
3859 }
3860 }
3861
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003862 private ActivityRecord getCallingRecordLocked(IBinder token) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003863 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003865 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 if (r != null) {
3867 return r.resultTo;
3868 }
3869 }
3870 return null;
3871 }
3872
3873 public ComponentName getActivityClassForToken(IBinder token) {
3874 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003875 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003877 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 return r.intent.getComponent();
3879 }
3880 return null;
3881 }
3882 }
3883
3884 public String getPackageForToken(IBinder token) {
3885 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003886 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003888 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 return r.packageName;
3890 }
3891 return null;
3892 }
3893 }
3894
3895 public IIntentSender getIntentSender(int type,
3896 String packageName, IBinder token, String resultWho,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003897 int requestCode, Intent[] intents, String[] resolvedTypes, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 // Refuse possible leaked file descriptors
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003899 if (intents != null) {
3900 if (intents.length < 1) {
3901 throw new IllegalArgumentException("Intents array length must be >= 1");
3902 }
3903 for (int i=0; i<intents.length; i++) {
3904 Intent intent = intents[i];
3905 if (intent == null) {
3906 throw new IllegalArgumentException("Null intent at index " + i);
3907 }
3908 if (intent.hasFileDescriptors()) {
3909 throw new IllegalArgumentException("File descriptors passed in Intent");
3910 }
3911 if (type == INTENT_SENDER_BROADCAST &&
3912 (intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
3913 throw new IllegalArgumentException(
3914 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
3915 }
3916 intents[i] = new Intent(intent);
3917 }
3918 if (resolvedTypes != null && resolvedTypes.length != intents.length) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003919 throw new IllegalArgumentException(
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003920 "Intent array length does not match resolvedTypes length");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003921 }
3922 }
3923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924 synchronized(this) {
3925 int callingUid = Binder.getCallingUid();
3926 try {
3927 if (callingUid != 0 && callingUid != Process.SYSTEM_UID &&
3928 Process.supportsProcesses()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003929 int uid = AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930 .getPackageUid(packageName);
3931 if (uid != Binder.getCallingUid()) {
3932 String msg = "Permission Denial: getIntentSender() from pid="
3933 + Binder.getCallingPid()
3934 + ", uid=" + Binder.getCallingUid()
3935 + ", (need uid=" + uid + ")"
3936 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003937 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 throw new SecurityException(msg);
3939 }
3940 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003941
3942 return getIntentSenderLocked(type, packageName, callingUid,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003943 token, resultWho, requestCode, intents, resolvedTypes, flags);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 } catch (RemoteException e) {
3946 throw new SecurityException(e);
3947 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003948 }
3949 }
3950
3951 IIntentSender getIntentSenderLocked(int type,
3952 String packageName, int callingUid, IBinder token, String resultWho,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003953 int requestCode, Intent[] intents, String[] resolvedTypes, int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003954 ActivityRecord activity = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07003955 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003956 int index = mMainStack.indexOfTokenLocked(token);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003957 if (index < 0) {
3958 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003959 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003960 activity = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003961 if (activity.finishing) {
3962 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003964 }
3965
3966 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
3967 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
3968 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
3969 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
3970 |PendingIntent.FLAG_UPDATE_CURRENT);
3971
3972 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
3973 type, packageName, activity, resultWho,
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003974 requestCode, intents, resolvedTypes, flags);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003975 WeakReference<PendingIntentRecord> ref;
3976 ref = mIntentSenderRecords.get(key);
3977 PendingIntentRecord rec = ref != null ? ref.get() : null;
3978 if (rec != null) {
3979 if (!cancelCurrent) {
3980 if (updateCurrent) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08003981 if (rec.key.requestIntent != null) {
3982 rec.key.requestIntent.replaceExtras(intents != null ? intents[0] : null);
3983 }
3984 if (intents != null) {
3985 intents[intents.length-1] = rec.key.requestIntent;
3986 rec.key.allIntents = intents;
3987 rec.key.allResolvedTypes = resolvedTypes;
3988 } else {
3989 rec.key.allIntents = null;
3990 rec.key.allResolvedTypes = null;
3991 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 return rec;
3994 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003995 rec.canceled = true;
3996 mIntentSenderRecords.remove(key);
3997 }
3998 if (noCreate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 return rec;
4000 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07004001 rec = new PendingIntentRecord(this, key, callingUid);
4002 mIntentSenderRecords.put(key, rec.ref);
4003 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
4004 if (activity.pendingResults == null) {
4005 activity.pendingResults
4006 = new HashSet<WeakReference<PendingIntentRecord>>();
4007 }
4008 activity.pendingResults.add(rec.ref);
4009 }
4010 return rec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 }
4012
4013 public void cancelIntentSender(IIntentSender sender) {
4014 if (!(sender instanceof PendingIntentRecord)) {
4015 return;
4016 }
4017 synchronized(this) {
4018 PendingIntentRecord rec = (PendingIntentRecord)sender;
4019 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004020 int uid = AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 .getPackageUid(rec.key.packageName);
4022 if (uid != Binder.getCallingUid()) {
4023 String msg = "Permission Denial: cancelIntentSender() from pid="
4024 + Binder.getCallingPid()
4025 + ", uid=" + Binder.getCallingUid()
4026 + " is not allowed to cancel packges "
4027 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004028 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 throw new SecurityException(msg);
4030 }
4031 } catch (RemoteException e) {
4032 throw new SecurityException(e);
4033 }
4034 cancelIntentSenderLocked(rec, true);
4035 }
4036 }
4037
4038 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
4039 rec.canceled = true;
4040 mIntentSenderRecords.remove(rec.key);
4041 if (cleanActivity && rec.key.activity != null) {
4042 rec.key.activity.pendingResults.remove(rec.ref);
4043 }
4044 }
4045
4046 public String getPackageForIntentSender(IIntentSender pendingResult) {
4047 if (!(pendingResult instanceof PendingIntentRecord)) {
4048 return null;
4049 }
Brad Fitzpatrickb213d102010-04-19 11:58:52 -07004050 try {
4051 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
4052 return res.key.packageName;
4053 } catch (ClassCastException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055 return null;
4056 }
4057
4058 public void setProcessLimit(int max) {
4059 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4060 "setProcessLimit()");
4061 mProcessLimit = max;
4062 }
4063
4064 public int getProcessLimit() {
4065 return mProcessLimit;
4066 }
4067
4068 void foregroundTokenDied(ForegroundToken token) {
4069 synchronized (ActivityManagerService.this) {
4070 synchronized (mPidsSelfLocked) {
4071 ForegroundToken cur
4072 = mForegroundProcesses.get(token.pid);
4073 if (cur != token) {
4074 return;
4075 }
4076 mForegroundProcesses.remove(token.pid);
4077 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
4078 if (pr == null) {
4079 return;
4080 }
4081 pr.forcingToForeground = null;
4082 pr.foregroundServices = false;
4083 }
4084 updateOomAdjLocked();
4085 }
4086 }
4087
4088 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
4089 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
4090 "setProcessForeground()");
4091 synchronized(this) {
4092 boolean changed = false;
4093
4094 synchronized (mPidsSelfLocked) {
4095 ProcessRecord pr = mPidsSelfLocked.get(pid);
4096 if (pr == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004097 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 return;
4099 }
4100 ForegroundToken oldToken = mForegroundProcesses.get(pid);
4101 if (oldToken != null) {
4102 oldToken.token.unlinkToDeath(oldToken, 0);
4103 mForegroundProcesses.remove(pid);
4104 pr.forcingToForeground = null;
4105 changed = true;
4106 }
4107 if (isForeground && token != null) {
4108 ForegroundToken newToken = new ForegroundToken() {
4109 public void binderDied() {
4110 foregroundTokenDied(this);
4111 }
4112 };
4113 newToken.pid = pid;
4114 newToken.token = token;
4115 try {
4116 token.linkToDeath(newToken, 0);
4117 mForegroundProcesses.put(pid, newToken);
4118 pr.forcingToForeground = token;
4119 changed = true;
4120 } catch (RemoteException e) {
4121 // If the process died while doing this, we will later
4122 // do the cleanup with the process death link.
4123 }
4124 }
4125 }
4126
4127 if (changed) {
4128 updateOomAdjLocked();
4129 }
4130 }
4131 }
4132
4133 // =========================================================
4134 // PERMISSIONS
4135 // =========================================================
4136
4137 static class PermissionController extends IPermissionController.Stub {
4138 ActivityManagerService mActivityManagerService;
4139 PermissionController(ActivityManagerService activityManagerService) {
4140 mActivityManagerService = activityManagerService;
4141 }
4142
4143 public boolean checkPermission(String permission, int pid, int uid) {
4144 return mActivityManagerService.checkPermission(permission, pid,
4145 uid) == PackageManager.PERMISSION_GRANTED;
4146 }
4147 }
4148
4149 /**
4150 * This can be called with or without the global lock held.
4151 */
4152 int checkComponentPermission(String permission, int pid, int uid,
4153 int reqUid) {
4154 // We might be performing an operation on behalf of an indirect binder
4155 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
4156 // client identity accordingly before proceeding.
4157 Identity tlsIdentity = sCallerIdentity.get();
4158 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004159 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
4161 uid = tlsIdentity.uid;
4162 pid = tlsIdentity.pid;
4163 }
4164
4165 // Root, system server and our own process get to do everything.
4166 if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID ||
4167 !Process.supportsProcesses()) {
4168 return PackageManager.PERMISSION_GRANTED;
4169 }
4170 // If the target requires a specific UID, always fail for others.
4171 if (reqUid >= 0 && uid != reqUid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004172 Slog.w(TAG, "Permission denied: checkComponentPermission() reqUid=" + reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 return PackageManager.PERMISSION_DENIED;
4174 }
4175 if (permission == null) {
4176 return PackageManager.PERMISSION_GRANTED;
4177 }
4178 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004179 return AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 .checkUidPermission(permission, uid);
4181 } catch (RemoteException e) {
4182 // Should never happen, but if it does... deny!
Joe Onorato8a9b2202010-02-26 18:56:32 -08004183 Slog.e(TAG, "PackageManager is dead?!?", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 }
4185 return PackageManager.PERMISSION_DENIED;
4186 }
4187
4188 /**
4189 * As the only public entry point for permissions checking, this method
4190 * can enforce the semantic that requesting a check on a null global
4191 * permission is automatically denied. (Internally a null permission
4192 * string is used when calling {@link #checkComponentPermission} in cases
4193 * when only uid-based security is needed.)
4194 *
4195 * This can be called with or without the global lock held.
4196 */
4197 public int checkPermission(String permission, int pid, int uid) {
4198 if (permission == null) {
4199 return PackageManager.PERMISSION_DENIED;
4200 }
4201 return checkComponentPermission(permission, pid, uid, -1);
4202 }
4203
4204 /**
4205 * Binder IPC calls go through the public entry point.
4206 * This can be called with or without the global lock held.
4207 */
4208 int checkCallingPermission(String permission) {
4209 return checkPermission(permission,
4210 Binder.getCallingPid(),
4211 Binder.getCallingUid());
4212 }
4213
4214 /**
4215 * This can be called with or without the global lock held.
4216 */
4217 void enforceCallingPermission(String permission, String func) {
4218 if (checkCallingPermission(permission)
4219 == PackageManager.PERMISSION_GRANTED) {
4220 return;
4221 }
4222
4223 String msg = "Permission Denial: " + func + " from pid="
4224 + Binder.getCallingPid()
4225 + ", uid=" + Binder.getCallingUid()
4226 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004227 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 throw new SecurityException(msg);
4229 }
4230
4231 private final boolean checkHoldingPermissionsLocked(IPackageManager pm,
Dianne Hackborn48058e82010-09-27 16:53:23 -07004232 ProviderInfo pi, Uri uri, int uid, int modeFlags) {
4233 boolean readPerm = (modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) == 0;
4234 boolean writePerm = (modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) == 0;
4235 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4236 "checkHoldingPermissionsLocked: uri=" + uri + " uid=" + uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 try {
Dianne Hackborn48058e82010-09-27 16:53:23 -07004238 // Is the component private from the target uid?
4239 final boolean prv = !pi.exported && pi.applicationInfo.uid != uid;
4240
4241 // Acceptable if the there is no read permission needed from the
4242 // target or the target is holding the read permission.
4243 if (!readPerm) {
4244 if ((!prv && pi.readPermission == null) ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 (pm.checkUidPermission(pi.readPermission, uid)
Dianne Hackborn48058e82010-09-27 16:53:23 -07004246 == PackageManager.PERMISSION_GRANTED)) {
4247 readPerm = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 }
4249 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004250
4251 // Acceptable if the there is no write permission needed from the
4252 // target or the target is holding the read permission.
4253 if (!writePerm) {
4254 if (!prv && (pi.writePermission == null) ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 (pm.checkUidPermission(pi.writePermission, uid)
Dianne Hackborn48058e82010-09-27 16:53:23 -07004256 == PackageManager.PERMISSION_GRANTED)) {
4257 writePerm = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 }
4259 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004260
4261 // Acceptable if there is a path permission matching the URI that
4262 // the target holds the permission on.
4263 PathPermission[] pps = pi.pathPermissions;
4264 if (pps != null && (!readPerm || !writePerm)) {
4265 final String path = uri.getPath();
4266 int i = pps.length;
4267 while (i > 0 && (!readPerm || !writePerm)) {
4268 i--;
4269 PathPermission pp = pps[i];
4270 if (!readPerm) {
4271 final String pprperm = pp.getReadPermission();
4272 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking read perm for "
4273 + pprperm + " for " + pp.getPath()
4274 + ": match=" + pp.match(path)
4275 + " check=" + pm.checkUidPermission(pprperm, uid));
4276 if (pprperm != null && pp.match(path) &&
4277 (pm.checkUidPermission(pprperm, uid)
4278 == PackageManager.PERMISSION_GRANTED)) {
4279 readPerm = true;
4280 }
4281 }
4282 if (!writePerm) {
4283 final String ppwperm = pp.getWritePermission();
4284 if (DEBUG_URI_PERMISSION) Slog.v(TAG, "Checking write perm "
4285 + ppwperm + " for " + pp.getPath()
4286 + ": match=" + pp.match(path)
4287 + " check=" + pm.checkUidPermission(ppwperm, uid));
4288 if (ppwperm != null && pp.match(path) &&
4289 (pm.checkUidPermission(ppwperm, uid)
4290 == PackageManager.PERMISSION_GRANTED)) {
4291 writePerm = true;
4292 }
4293 }
4294 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296 } catch (RemoteException e) {
4297 return false;
4298 }
Dianne Hackborn48058e82010-09-27 16:53:23 -07004299
4300 return readPerm && writePerm;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302
4303 private final boolean checkUriPermissionLocked(Uri uri, int uid,
4304 int modeFlags) {
4305 // Root gets to do everything.
4306 if (uid == 0 || !Process.supportsProcesses()) {
4307 return true;
4308 }
4309 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
4310 if (perms == null) return false;
4311 UriPermission perm = perms.get(uri);
4312 if (perm == null) return false;
4313 return (modeFlags&perm.modeFlags) == modeFlags;
4314 }
4315
4316 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
4317 // Another redirected-binder-call permissions check as in
4318 // {@link checkComponentPermission}.
4319 Identity tlsIdentity = sCallerIdentity.get();
4320 if (tlsIdentity != null) {
4321 uid = tlsIdentity.uid;
4322 pid = tlsIdentity.pid;
4323 }
4324
4325 // Our own process gets to do everything.
4326 if (pid == MY_PID) {
4327 return PackageManager.PERMISSION_GRANTED;
4328 }
4329 synchronized(this) {
4330 return checkUriPermissionLocked(uri, uid, modeFlags)
4331 ? PackageManager.PERMISSION_GRANTED
4332 : PackageManager.PERMISSION_DENIED;
4333 }
4334 }
4335
Dianne Hackborn39792d22010-08-19 18:01:52 -07004336 /**
4337 * Check if the targetPkg can be granted permission to access uri by
4338 * the callingUid using the given modeFlags. Throws a security exception
4339 * if callingUid is not allowed to do this. Returns the uid of the target
4340 * if the URI permission grant should be performed; returns -1 if it is not
4341 * needed (for example targetPkg already has permission to access the URI).
4342 */
4343 int checkGrantUriPermissionLocked(int callingUid, String targetPkg,
4344 Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4346 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4347 if (modeFlags == 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004348 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 }
4350
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004351 if (targetPkg != null) {
4352 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4353 "Checking grant " + targetPkg + " permission to " + uri);
4354 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004355
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004356 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357
4358 // If this is not a content: uri, we can't do anything with it.
4359 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004360 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004361 "Can't grant URI permission for non-content URI: " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004362 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 }
4364
4365 String name = uri.getAuthority();
4366 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004367 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 if (cpr != null) {
4369 pi = cpr.info;
4370 } else {
4371 try {
4372 pi = pm.resolveContentProvider(name,
4373 PackageManager.GET_URI_PERMISSION_PATTERNS);
4374 } catch (RemoteException ex) {
4375 }
4376 }
4377 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004378 Slog.w(TAG, "No content provider found for: " + name);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004379 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 }
4381
4382 int targetUid;
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004383 if (targetPkg != null) {
4384 try {
4385 targetUid = pm.getPackageUid(targetPkg);
4386 if (targetUid < 0) {
4387 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4388 "Can't grant URI permission no uid for: " + targetPkg);
4389 return -1;
4390 }
4391 } catch (RemoteException ex) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004392 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 }
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004394 } else {
4395 targetUid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 }
4397
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004398 if (targetUid >= 0) {
4399 // First... does the target actually need this permission?
4400 if (checkHoldingPermissionsLocked(pm, pi, uri, targetUid, modeFlags)) {
4401 // No need to grant the target this permission.
4402 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
4403 "Target " + targetPkg + " already has full permission to " + uri);
4404 return -1;
4405 }
4406 } else {
4407 // First... there is no target package, so can anyone access it?
4408 boolean allowed = pi.exported;
4409 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
4410 if (pi.readPermission != null) {
4411 allowed = false;
4412 }
4413 }
4414 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
4415 if (pi.writePermission != null) {
4416 allowed = false;
4417 }
4418 }
4419 if (allowed) {
4420 return -1;
4421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
4423
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004424 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 if (!pi.grantUriPermissions) {
4426 throw new SecurityException("Provider " + pi.packageName
4427 + "/" + pi.name
4428 + " does not allow granting of Uri permissions (uri "
4429 + uri + ")");
4430 }
4431 if (pi.uriPermissionPatterns != null) {
4432 final int N = pi.uriPermissionPatterns.length;
4433 boolean allowed = false;
4434 for (int i=0; i<N; i++) {
4435 if (pi.uriPermissionPatterns[i] != null
4436 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
4437 allowed = true;
4438 break;
4439 }
4440 }
4441 if (!allowed) {
4442 throw new SecurityException("Provider " + pi.packageName
4443 + "/" + pi.name
4444 + " does not allow granting of permission to path of Uri "
4445 + uri);
4446 }
4447 }
4448
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004449 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 // this uri?
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004451 if (callingUid != Process.myUid()) {
4452 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
4453 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
4454 throw new SecurityException("Uid " + callingUid
4455 + " does not have permission to uri " + uri);
4456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 }
4458 }
4459
Dianne Hackborn39792d22010-08-19 18:01:52 -07004460 return targetUid;
4461 }
4462
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004463 public int checkGrantUriPermission(int callingUid, String targetPkg,
4464 Uri uri, int modeFlags) {
4465 synchronized(this) {
4466 return checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags);
4467 }
4468 }
4469
Dianne Hackborn39792d22010-08-19 18:01:52 -07004470 void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg,
4471 Uri uri, int modeFlags, UriPermissionOwner owner) {
4472 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4473 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4474 if (modeFlags == 0) {
4475 return;
4476 }
4477
4478 // So here we are: the caller has the assumed permission
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 // to the uri, and the target doesn't. Let's now give this to
4480 // the target.
4481
Joe Onorato8a9b2202010-02-26 18:56:32 -08004482 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07004483 "Granting " + targetPkg + "/" + targetUid + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 HashMap<Uri, UriPermission> targetUris
4486 = mGrantedUriPermissions.get(targetUid);
4487 if (targetUris == null) {
4488 targetUris = new HashMap<Uri, UriPermission>();
4489 mGrantedUriPermissions.put(targetUid, targetUris);
4490 }
4491
4492 UriPermission perm = targetUris.get(uri);
4493 if (perm == null) {
4494 perm = new UriPermission(targetUid, uri);
4495 targetUris.put(uri, perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 perm.modeFlags |= modeFlags;
Dianne Hackborn39792d22010-08-19 18:01:52 -07004499 if (owner == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 perm.globalModeFlags |= modeFlags;
4501 } else if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004502 perm.readOwners.add(owner);
4503 owner.addReadPermission(perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 } else if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004505 perm.writeOwners.add(owner);
4506 owner.addWritePermission(perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 }
4508 }
4509
Dianne Hackborn39792d22010-08-19 18:01:52 -07004510 void grantUriPermissionLocked(int callingUid,
4511 String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) {
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004512 if (targetPkg == null) {
4513 throw new NullPointerException("targetPkg");
4514 }
4515
Dianne Hackborn39792d22010-08-19 18:01:52 -07004516 int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags);
4517 if (targetUid < 0) {
4518 return;
4519 }
4520
4521 grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner);
4522 }
4523
4524 /**
4525 * Like checkGrantUriPermissionLocked, but takes an Intent.
4526 */
4527 int checkGrantUriPermissionFromIntentLocked(int callingUid,
4528 String targetPkg, Intent intent) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07004529 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07004530 "Checking URI perm to " + (intent != null ? intent.getData() : null)
Dianne Hackbornb424b632010-08-18 15:59:05 -07004531 + " from " + intent + "; flags=0x"
4532 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
4533
Dianne Hackborn90f4aaf2010-09-27 14:58:44 -07004534 if (targetPkg == null) {
4535 throw new NullPointerException("targetPkg");
4536 }
4537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 if (intent == null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004539 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 }
4541 Uri data = intent.getData();
4542 if (data == null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004543 return -1;
4544 }
4545 return checkGrantUriPermissionLocked(callingUid, targetPkg, data,
4546 intent.getFlags());
4547 }
4548
4549 /**
4550 * Like grantUriPermissionUncheckedLocked, but takes an Intent.
4551 */
4552 void grantUriPermissionUncheckedFromIntentLocked(int targetUid,
4553 String targetPkg, Intent intent, UriPermissionOwner owner) {
4554 grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(),
4555 intent.getFlags(), owner);
4556 }
4557
4558 void grantUriPermissionFromIntentLocked(int callingUid,
4559 String targetPkg, Intent intent, UriPermissionOwner owner) {
4560 int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent);
4561 if (targetUid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 return;
4563 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07004564
4565 grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 }
4567
4568 public void grantUriPermission(IApplicationThread caller, String targetPkg,
4569 Uri uri, int modeFlags) {
4570 synchronized(this) {
4571 final ProcessRecord r = getRecordForAppLocked(caller);
4572 if (r == null) {
4573 throw new SecurityException("Unable to find app for caller "
4574 + caller
4575 + " when granting permission to uri " + uri);
4576 }
4577 if (targetPkg == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07004578 throw new IllegalArgumentException("null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
4580 if (uri == null) {
Dianne Hackborn7e269642010-08-25 19:50:20 -07004581 throw new IllegalArgumentException("null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 }
4583
4584 grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags,
4585 null);
4586 }
4587 }
4588
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004589 void removeUriPermissionIfNeededLocked(UriPermission perm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
4591 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
4592 HashMap<Uri, UriPermission> perms
4593 = mGrantedUriPermissions.get(perm.uid);
4594 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004595 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004596 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 perms.remove(perm.uri);
4598 if (perms.size() == 0) {
4599 mGrantedUriPermissions.remove(perm.uid);
4600 }
4601 }
4602 }
4603 }
4604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 private void revokeUriPermissionLocked(int callingUid, Uri uri,
4606 int modeFlags) {
4607 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4608 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4609 if (modeFlags == 0) {
4610 return;
4611 }
4612
Joe Onorato8a9b2202010-02-26 18:56:32 -08004613 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004614 "Revoking all granted permissions to " + uri);
4615
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004616 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617
4618 final String authority = uri.getAuthority();
4619 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004620 ContentProviderRecord cpr = mProvidersByName.get(authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 if (cpr != null) {
4622 pi = cpr.info;
4623 } else {
4624 try {
4625 pi = pm.resolveContentProvider(authority,
4626 PackageManager.GET_URI_PERMISSION_PATTERNS);
4627 } catch (RemoteException ex) {
4628 }
4629 }
4630 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004631 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 return;
4633 }
4634
4635 // Does the caller have this permission on the URI?
Dianne Hackborn48058e82010-09-27 16:53:23 -07004636 if (!checkHoldingPermissionsLocked(pm, pi, uri, callingUid, modeFlags)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 // Right now, if you are not the original owner of the permission,
4638 // you are not allowed to revoke it.
4639 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
4640 throw new SecurityException("Uid " + callingUid
4641 + " does not have permission to uri " + uri);
4642 //}
4643 }
4644
4645 // Go through all of the permissions and remove any that match.
4646 final List<String> SEGMENTS = uri.getPathSegments();
4647 if (SEGMENTS != null) {
4648 final int NS = SEGMENTS.size();
4649 int N = mGrantedUriPermissions.size();
4650 for (int i=0; i<N; i++) {
4651 HashMap<Uri, UriPermission> perms
4652 = mGrantedUriPermissions.valueAt(i);
4653 Iterator<UriPermission> it = perms.values().iterator();
4654 toploop:
4655 while (it.hasNext()) {
4656 UriPermission perm = it.next();
4657 Uri targetUri = perm.uri;
4658 if (!authority.equals(targetUri.getAuthority())) {
4659 continue;
4660 }
4661 List<String> targetSegments = targetUri.getPathSegments();
4662 if (targetSegments == null) {
4663 continue;
4664 }
4665 if (targetSegments.size() < NS) {
4666 continue;
4667 }
4668 for (int j=0; j<NS; j++) {
4669 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
4670 continue toploop;
4671 }
4672 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004673 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004674 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 perm.clearModes(modeFlags);
4676 if (perm.modeFlags == 0) {
4677 it.remove();
4678 }
4679 }
4680 if (perms.size() == 0) {
4681 mGrantedUriPermissions.remove(
4682 mGrantedUriPermissions.keyAt(i));
4683 N--;
4684 i--;
4685 }
4686 }
4687 }
4688 }
4689
4690 public void revokeUriPermission(IApplicationThread caller, Uri uri,
4691 int modeFlags) {
4692 synchronized(this) {
4693 final ProcessRecord r = getRecordForAppLocked(caller);
4694 if (r == null) {
4695 throw new SecurityException("Unable to find app for caller "
4696 + caller
4697 + " when revoking permission to uri " + uri);
4698 }
4699 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004700 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004701 return;
4702 }
4703
4704 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4705 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4706 if (modeFlags == 0) {
4707 return;
4708 }
4709
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004710 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711
4712 final String authority = uri.getAuthority();
4713 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004714 ContentProviderRecord cpr = mProvidersByName.get(authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 if (cpr != null) {
4716 pi = cpr.info;
4717 } else {
4718 try {
4719 pi = pm.resolveContentProvider(authority,
4720 PackageManager.GET_URI_PERMISSION_PATTERNS);
4721 } catch (RemoteException ex) {
4722 }
4723 }
4724 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004725 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 return;
4727 }
4728
4729 revokeUriPermissionLocked(r.info.uid, uri, modeFlags);
4730 }
4731 }
4732
Dianne Hackborn7e269642010-08-25 19:50:20 -07004733 @Override
4734 public IBinder newUriPermissionOwner(String name) {
4735 synchronized(this) {
4736 UriPermissionOwner owner = new UriPermissionOwner(this, name);
4737 return owner.getExternalTokenLocked();
4738 }
4739 }
4740
4741 @Override
4742 public void grantUriPermissionFromOwner(IBinder token, int fromUid, String targetPkg,
4743 Uri uri, int modeFlags) {
4744 synchronized(this) {
4745 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
4746 if (owner == null) {
4747 throw new IllegalArgumentException("Unknown owner: " + token);
4748 }
4749 if (fromUid != Binder.getCallingUid()) {
4750 if (Binder.getCallingUid() != Process.myUid()) {
4751 // Only system code can grant URI permissions on behalf
4752 // of other users.
4753 throw new SecurityException("nice try");
4754 }
4755 }
4756 if (targetPkg == null) {
4757 throw new IllegalArgumentException("null target");
4758 }
4759 if (uri == null) {
4760 throw new IllegalArgumentException("null uri");
4761 }
4762
4763 grantUriPermissionLocked(fromUid, targetPkg, uri, modeFlags, owner);
4764 }
4765 }
4766
4767 @Override
4768 public void revokeUriPermissionFromOwner(IBinder token, Uri uri, int mode) {
4769 synchronized(this) {
4770 UriPermissionOwner owner = UriPermissionOwner.fromExternalToken(token);
4771 if (owner == null) {
4772 throw new IllegalArgumentException("Unknown owner: " + token);
4773 }
4774
4775 if (uri == null) {
4776 owner.removeUriPermissionsLocked(mode);
4777 } else {
4778 owner.removeUriPermissionLocked(uri, mode);
4779 }
4780 }
4781 }
4782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
4784 synchronized (this) {
4785 ProcessRecord app =
4786 who != null ? getRecordForAppLocked(who) : null;
4787 if (app == null) return;
4788
4789 Message msg = Message.obtain();
4790 msg.what = WAIT_FOR_DEBUGGER_MSG;
4791 msg.obj = app;
4792 msg.arg1 = waiting ? 1 : 0;
4793 mHandler.sendMessage(msg);
4794 }
4795 }
4796
4797 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
4798 outInfo.availMem = Process.getFreeMemory();
The Android Open Source Project4df24232009-03-05 14:34:35 -08004799 outInfo.threshold = HOME_APP_MEM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 outInfo.lowMemory = outInfo.availMem <
The Android Open Source Project4df24232009-03-05 14:34:35 -08004801 (HOME_APP_MEM + ((HIDDEN_APP_MEM-HOME_APP_MEM)/2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 }
4803
4804 // =========================================================
4805 // TASK MANAGEMENT
4806 // =========================================================
4807
4808 public List getTasks(int maxNum, int flags,
4809 IThumbnailReceiver receiver) {
4810 ArrayList list = new ArrayList();
4811
4812 PendingThumbnailsRecord pending = null;
4813 IApplicationThread topThumbnail = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004814 ActivityRecord topRecord = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815
4816 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004817 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
4819 + ", receiver=" + receiver);
4820
4821 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
4822 != PackageManager.PERMISSION_GRANTED) {
4823 if (receiver != null) {
4824 // If the caller wants to wait for pending thumbnails,
4825 // it ain't gonna get them.
4826 try {
4827 receiver.finished();
4828 } catch (RemoteException ex) {
4829 }
4830 }
4831 String msg = "Permission Denial: getTasks() from pid="
4832 + Binder.getCallingPid()
4833 + ", uid=" + Binder.getCallingUid()
4834 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004835 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 throw new SecurityException(msg);
4837 }
4838
Dianne Hackbornd2835932010-12-13 16:28:46 -08004839 final boolean canReadFb = (flags&ActivityManager.TASKS_GET_THUMBNAILS) != 0
4840 && checkCallingPermission(
4841 android.Manifest.permission.READ_FRAME_BUFFER)
4842 == PackageManager.PERMISSION_GRANTED;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004843
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004844 int pos = mMainStack.mHistory.size()-1;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004845 ActivityRecord next =
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004846 pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004847 ActivityRecord top = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 CharSequence topDescription = null;
4849 TaskRecord curTask = null;
4850 int numActivities = 0;
4851 int numRunning = 0;
4852 while (pos >= 0 && maxNum > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004853 final ActivityRecord r = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 pos--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004855 next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004856
4857 // Initialize state for next task if needed.
4858 if (top == null ||
4859 (top.state == ActivityState.INITIALIZING
4860 && top.task == r.task)) {
4861 top = r;
4862 topDescription = r.description;
4863 curTask = r.task;
4864 numActivities = numRunning = 0;
4865 }
4866
4867 // Add 'r' into the current task.
4868 numActivities++;
4869 if (r.app != null && r.app.thread != null) {
4870 numRunning++;
4871 }
4872 if (topDescription == null) {
4873 topDescription = r.description;
4874 }
4875
Joe Onorato8a9b2202010-02-26 18:56:32 -08004876 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 TAG, r.intent.getComponent().flattenToShortString()
4878 + ": task=" + r.task);
4879
4880 // If the next one is a different task, generate a new
4881 // TaskInfo entry for what we have.
4882 if (next == null || next.task != curTask) {
4883 ActivityManager.RunningTaskInfo ci
4884 = new ActivityManager.RunningTaskInfo();
4885 ci.id = curTask.taskId;
4886 ci.baseActivity = r.intent.getComponent();
4887 ci.topActivity = top.intent.getComponent();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004888 if (canReadFb) {
4889 if (top.thumbnail != null) {
4890 ci.thumbnail = top.thumbnail;
4891 } else if (top.state == ActivityState.RESUMED) {
Dianne Hackbornd2835932010-12-13 16:28:46 -08004892 ci.thumbnail = top.stack.screenshotActivities(top);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004893 }
4894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 ci.description = topDescription;
4896 ci.numActivities = numActivities;
4897 ci.numRunning = numRunning;
4898 //System.out.println(
4899 // "#" + maxNum + ": " + " descr=" + ci.description);
4900 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004901 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 TAG, "State=" + top.state + "Idle=" + top.idle
4903 + " app=" + top.app
4904 + " thr=" + (top.app != null ? top.app.thread : null));
4905 if (top.state == ActivityState.RESUMED
4906 || top.state == ActivityState.PAUSING) {
4907 if (top.idle && top.app != null
4908 && top.app.thread != null) {
4909 topRecord = top;
4910 topThumbnail = top.app.thread;
4911 } else {
4912 top.thumbnailNeeded = true;
4913 }
4914 }
4915 if (pending == null) {
4916 pending = new PendingThumbnailsRecord(receiver);
4917 }
4918 pending.pendingRecords.add(top);
4919 }
4920 list.add(ci);
4921 maxNum--;
4922 top = null;
4923 }
4924 }
4925
4926 if (pending != null) {
4927 mPendingThumbnails.add(pending);
4928 }
4929 }
4930
Joe Onorato8a9b2202010-02-26 18:56:32 -08004931 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932
4933 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004934 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 try {
4936 topThumbnail.requestThumbnail(topRecord);
4937 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004938 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004939 sendPendingThumbnail(null, topRecord, null, null, true);
4940 }
4941 }
4942
4943 if (pending == null && receiver != null) {
4944 // In this case all thumbnails were available and the client
4945 // is being asked to be told when the remaining ones come in...
4946 // which is unusually, since the top-most currently running
4947 // activity should never have a canned thumbnail! Oh well.
4948 try {
4949 receiver.finished();
4950 } catch (RemoteException ex) {
4951 }
4952 }
4953
4954 return list;
4955 }
4956
4957 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
4958 int flags) {
4959 synchronized (this) {
4960 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
4961 "getRecentTasks()");
4962
Dianne Hackbornd2835932010-12-13 16:28:46 -08004963 final boolean canReadFb = (flags&ActivityManager.TASKS_GET_THUMBNAILS) != 0
4964 && checkCallingPermission(
4965 android.Manifest.permission.READ_FRAME_BUFFER)
4966 == PackageManager.PERMISSION_GRANTED;
4967
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004968 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07004969
Dianne Hackbornd2835932010-12-13 16:28:46 -08004970 ActivityRecord resumed = mMainStack.mResumedActivity;
4971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004972 final int N = mRecentTasks.size();
4973 ArrayList<ActivityManager.RecentTaskInfo> res
4974 = new ArrayList<ActivityManager.RecentTaskInfo>(
4975 maxNum < N ? maxNum : N);
4976 for (int i=0; i<N && maxNum > 0; i++) {
4977 TaskRecord tr = mRecentTasks.get(i);
4978 if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
4979 || (tr.intent == null)
4980 || ((tr.intent.getFlags()
4981 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
4982 ActivityManager.RecentTaskInfo rti
4983 = new ActivityManager.RecentTaskInfo();
4984 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
4985 rti.baseIntent = new Intent(
4986 tr.intent != null ? tr.intent : tr.affinityIntent);
4987 rti.origActivity = tr.origActivity;
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07004988
Dianne Hackbornd2835932010-12-13 16:28:46 -08004989 if (canReadFb) {
4990 if (resumed != null && resumed.task == tr) {
4991 rti.thumbnail = resumed.stack.screenshotActivities(resumed);
4992 } else {
4993 rti.thumbnail = tr.lastThumbnail;
4994 }
4995 }
4996 rti.description = tr.lastDescription;
4997
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07004998 if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) {
4999 // Check whether this activity is currently available.
5000 try {
5001 if (rti.origActivity != null) {
5002 if (pm.getActivityInfo(rti.origActivity, 0) == null) {
5003 continue;
5004 }
5005 } else if (rti.baseIntent != null) {
5006 if (pm.queryIntentActivities(rti.baseIntent,
5007 null, 0) == null) {
5008 continue;
5009 }
5010 }
5011 } catch (RemoteException e) {
5012 // Will never happen.
5013 }
5014 }
5015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 res.add(rti);
5017 maxNum--;
5018 }
5019 }
5020 return res;
5021 }
5022 }
5023
5024 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
5025 int j;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005026 TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005027 TaskRecord jt = startTask;
5028
5029 // First look backwards
5030 for (j=startIndex-1; j>=0; j--) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005031 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 if (r.task != jt) {
5033 jt = r.task;
5034 if (affinity.equals(jt.affinity)) {
5035 return j;
5036 }
5037 }
5038 }
5039
5040 // Now look forwards
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005041 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005042 jt = startTask;
5043 for (j=startIndex+1; j<N; j++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005044 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 if (r.task != jt) {
5046 if (affinity.equals(jt.affinity)) {
5047 return j;
5048 }
5049 jt = r.task;
5050 }
5051 }
5052
5053 // Might it be at the top?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005054 if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 return N-1;
5056 }
5057
5058 return -1;
5059 }
5060
5061 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005062 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005063 */
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005064 public void moveTaskToFront(int task, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005065 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5066 "moveTaskToFront()");
5067
5068 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005069 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5070 Binder.getCallingUid(), "Task to front")) {
5071 return;
5072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073 final long origId = Binder.clearCallingIdentity();
5074 try {
5075 int N = mRecentTasks.size();
5076 for (int i=0; i<N; i++) {
5077 TaskRecord tr = mRecentTasks.get(i);
5078 if (tr.taskId == task) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005079 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5080 // Caller wants the home activity moved with it. To accomplish this,
5081 // we'll just move the home task to the top first.
5082 mMainStack.moveHomeToFrontLocked();
5083 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005084 mMainStack.moveTaskToFrontLocked(tr, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 return;
5086 }
5087 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005088 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
5089 ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 if (hr.task.taskId == task) {
Dianne Hackborn621e17d2010-11-22 15:59:56 -08005091 if ((flags&ActivityManager.MOVE_TASK_WITH_HOME) != 0) {
5092 // Caller wants the home activity moved with it. To accomplish this,
5093 // we'll just move the home task to the top first.
5094 mMainStack.moveHomeToFrontLocked();
5095 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005096 mMainStack.moveTaskToFrontLocked(hr.task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 return;
5098 }
5099 }
5100 } finally {
5101 Binder.restoreCallingIdentity(origId);
5102 }
5103 }
5104 }
5105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 public void moveTaskToBack(int task) {
5107 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5108 "moveTaskToBack()");
5109
5110 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005111 if (mMainStack.mResumedActivity != null
5112 && mMainStack.mResumedActivity.task.taskId == task) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005113 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5114 Binder.getCallingUid(), "Task to back")) {
5115 return;
5116 }
5117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005119 mMainStack.moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 Binder.restoreCallingIdentity(origId);
5121 }
5122 }
5123
5124 /**
5125 * Moves an activity, and all of the other activities within the same task, to the bottom
5126 * of the history stack. The activity's order within the task is unchanged.
5127 *
5128 * @param token A reference to the activity we wish to move
5129 * @param nonRoot If false then this only works if the activity is the root
5130 * of a task; if true it will work for any activity in a task.
5131 * @return Returns true if the move completed, false if not.
5132 */
5133 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
5134 synchronized(this) {
5135 final long origId = Binder.clearCallingIdentity();
5136 int taskId = getTaskForActivityLocked(token, !nonRoot);
5137 if (taskId >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005138 return mMainStack.moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005139 }
5140 Binder.restoreCallingIdentity(origId);
5141 }
5142 return false;
5143 }
5144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 public void moveTaskBackwards(int task) {
5146 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
5147 "moveTaskBackwards()");
5148
5149 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005150 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
5151 Binder.getCallingUid(), "Task backwards")) {
5152 return;
5153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 final long origId = Binder.clearCallingIdentity();
5155 moveTaskBackwardsLocked(task);
5156 Binder.restoreCallingIdentity(origId);
5157 }
5158 }
5159
5160 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005161 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
5163
5164 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
5165 synchronized(this) {
5166 return getTaskForActivityLocked(token, onlyRoot);
5167 }
5168 }
5169
5170 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005171 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 TaskRecord lastTask = null;
5173 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005174 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 if (r == token) {
5176 if (!onlyRoot || lastTask != r.task) {
5177 return r.task.taskId;
5178 }
5179 return -1;
5180 }
5181 lastTask = r.task;
5182 }
5183
5184 return -1;
5185 }
5186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005187 public void finishOtherInstances(IBinder token, ComponentName className) {
5188 synchronized(this) {
5189 final long origId = Binder.clearCallingIdentity();
5190
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005191 int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 TaskRecord lastTask = null;
5193 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005194 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 if (r.realActivity.equals(className)
5196 && r != token && lastTask != r.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005197 if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 null, "others")) {
5199 i--;
5200 N--;
5201 }
5202 }
5203 lastTask = r.task;
5204 }
5205
5206 Binder.restoreCallingIdentity(origId);
5207 }
5208 }
5209
5210 // =========================================================
5211 // THUMBNAILS
5212 // =========================================================
5213
5214 public void reportThumbnail(IBinder token,
5215 Bitmap thumbnail, CharSequence description) {
5216 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
5217 final long origId = Binder.clearCallingIdentity();
5218 sendPendingThumbnail(null, token, thumbnail, description, true);
5219 Binder.restoreCallingIdentity(origId);
5220 }
5221
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005222 final void sendPendingThumbnail(ActivityRecord r, IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 Bitmap thumbnail, CharSequence description, boolean always) {
5224 TaskRecord task = null;
5225 ArrayList receivers = null;
5226
5227 //System.out.println("Send pending thumbnail: " + r);
5228
5229 synchronized(this) {
5230 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005231 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 if (index < 0) {
5233 return;
5234 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005235 r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 }
5237 if (thumbnail == null) {
5238 thumbnail = r.thumbnail;
5239 description = r.description;
5240 }
5241 if (thumbnail == null && !always) {
5242 // If there is no thumbnail, and this entry is not actually
5243 // going away, then abort for now and pick up the next
5244 // thumbnail we get.
5245 return;
5246 }
5247 task = r.task;
5248
5249 int N = mPendingThumbnails.size();
5250 int i=0;
5251 while (i<N) {
5252 PendingThumbnailsRecord pr =
5253 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
5254 //System.out.println("Looking in " + pr.pendingRecords);
5255 if (pr.pendingRecords.remove(r)) {
5256 if (receivers == null) {
5257 receivers = new ArrayList();
5258 }
5259 receivers.add(pr);
5260 if (pr.pendingRecords.size() == 0) {
5261 pr.finished = true;
5262 mPendingThumbnails.remove(i);
5263 N--;
5264 continue;
5265 }
5266 }
5267 i++;
5268 }
5269 }
5270
5271 if (receivers != null) {
5272 final int N = receivers.size();
5273 for (int i=0; i<N; i++) {
5274 try {
5275 PendingThumbnailsRecord pr =
5276 (PendingThumbnailsRecord)receivers.get(i);
5277 pr.receiver.newThumbnail(
5278 task != null ? task.taskId : -1, thumbnail, description);
5279 if (pr.finished) {
5280 pr.receiver.finished();
5281 }
5282 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005283 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 }
5285 }
5286 }
5287 }
5288
5289 // =========================================================
5290 // CONTENT PROVIDERS
5291 // =========================================================
5292
5293 private final List generateApplicationProvidersLocked(ProcessRecord app) {
5294 List providers = null;
5295 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005296 providers = AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 queryContentProviders(app.processName, app.info.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07005298 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 } catch (RemoteException ex) {
5300 }
5301 if (providers != null) {
5302 final int N = providers.size();
5303 for (int i=0; i<N; i++) {
5304 ProviderInfo cpi =
5305 (ProviderInfo)providers.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07005306 ContentProviderRecord cpr = mProvidersByClass.get(cpi.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 if (cpr == null) {
5308 cpr = new ContentProviderRecord(cpi, app.info);
5309 mProvidersByClass.put(cpi.name, cpr);
5310 }
5311 app.pubProviders.put(cpi.name, cpr);
5312 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07005313 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
5315 }
5316 return providers;
5317 }
5318
5319 private final String checkContentProviderPermissionLocked(
Dianne Hackbornb424b632010-08-18 15:59:05 -07005320 ProviderInfo cpi, ProcessRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005321 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
5322 final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid();
5323 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
5324 cpi.exported ? -1 : cpi.applicationInfo.uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -07005325 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 return null;
5327 }
5328 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
5329 cpi.exported ? -1 : cpi.applicationInfo.uid)
5330 == PackageManager.PERMISSION_GRANTED) {
5331 return null;
5332 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07005333
5334 PathPermission[] pps = cpi.pathPermissions;
5335 if (pps != null) {
5336 int i = pps.length;
5337 while (i > 0) {
5338 i--;
5339 PathPermission pp = pps[i];
5340 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
5341 cpi.exported ? -1 : cpi.applicationInfo.uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -07005342 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07005343 return null;
5344 }
5345 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
5346 cpi.exported ? -1 : cpi.applicationInfo.uid)
5347 == PackageManager.PERMISSION_GRANTED) {
5348 return null;
5349 }
5350 }
5351 }
5352
Dianne Hackbornb424b632010-08-18 15:59:05 -07005353 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
5354 if (perms != null) {
5355 for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) {
5356 if (uri.getKey().getAuthority().equals(cpi.authority)) {
5357 return null;
5358 }
5359 }
5360 }
5361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 String msg = "Permission Denial: opening provider " + cpi.name
5363 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
5364 + ", uid=" + callingUid + ") requires "
5365 + cpi.readPermission + " or " + cpi.writePermission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005366 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005367 return msg;
5368 }
5369
5370 private final ContentProviderHolder getContentProviderImpl(
5371 IApplicationThread caller, String name) {
5372 ContentProviderRecord cpr;
5373 ProviderInfo cpi = null;
5374
5375 synchronized(this) {
5376 ProcessRecord r = null;
5377 if (caller != null) {
5378 r = getRecordForAppLocked(caller);
5379 if (r == null) {
5380 throw new SecurityException(
5381 "Unable to find app for caller " + caller
5382 + " (pid=" + Binder.getCallingPid()
5383 + ") when getting content provider " + name);
5384 }
5385 }
5386
5387 // First check if this content provider has been published...
Dianne Hackborn860755f2010-06-03 18:47:52 -07005388 cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389 if (cpr != null) {
5390 cpi = cpr.info;
Dianne Hackbornb424b632010-08-18 15:59:05 -07005391 String msg;
5392 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
5393 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005394 }
5395
5396 if (r != null && cpr.canRunHere(r)) {
5397 // This provider has been published or is in the process
5398 // of being published... but it is also allowed to run
5399 // in the caller's process, so don't make a connection
5400 // and just let the caller instantiate its own instance.
5401 if (cpr.provider != null) {
5402 // don't give caller the provider object, it needs
5403 // to make its own.
5404 cpr = new ContentProviderRecord(cpr);
5405 }
5406 return cpr;
5407 }
5408
5409 final long origId = Binder.clearCallingIdentity();
5410
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005411 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 // return it right away.
5413 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005414 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005415 "Adding provider requested by "
5416 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005417 + cpr.info.processName);
5418 Integer cnt = r.conProviders.get(cpr);
5419 if (cnt == null) {
5420 r.conProviders.put(cpr, new Integer(1));
5421 } else {
5422 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
5423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 cpr.clients.add(r);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005425 if (cpr.app != null && r.setAdj <= PERCEPTIBLE_APP_ADJ) {
5426 // If this is a perceptible app accessing the provider,
Dianne Hackborn906497c2010-05-10 15:57:38 -07005427 // make sure to count it as being accessed and thus
5428 // back up on the LRU list. This is good because
5429 // content providers are often expensive to start.
5430 updateLruProcessLocked(cpr.app, false, true);
5431 }
Dianne Hackborn6f86c0e2010-05-11 14:20:52 -07005432 } else {
5433 cpr.externals++;
5434 }
5435
5436 if (cpr.app != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 updateOomAdjLocked(cpr.app);
5438 }
5439
5440 Binder.restoreCallingIdentity(origId);
5441
5442 } else {
5443 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005444 cpi = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07005445 resolveContentProvider(name,
5446 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 } catch (RemoteException ex) {
5448 }
5449 if (cpi == null) {
5450 return null;
5451 }
5452
Dianne Hackbornb424b632010-08-18 15:59:05 -07005453 String msg;
5454 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
5455 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 }
5457
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07005458 if (!mProcessesReady && !mDidUpdate && !mWaitingUpdate
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08005459 && !cpi.processName.equals("system")) {
5460 // If this content provider does not run in the system
5461 // process, and the system is not yet ready to run other
5462 // processes, then fail fast instead of hanging.
5463 throw new IllegalArgumentException(
5464 "Attempt to launch content provider before system ready");
5465 }
5466
Dianne Hackborn860755f2010-06-03 18:47:52 -07005467 cpr = mProvidersByClass.get(cpi.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 final boolean firstClass = cpr == null;
5469 if (firstClass) {
5470 try {
5471 ApplicationInfo ai =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005472 AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005473 getApplicationInfo(
5474 cpi.applicationInfo.packageName,
Dianne Hackborn1655be42009-05-08 14:29:01 -07005475 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005477 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 + cpi.name);
5479 return null;
5480 }
5481 cpr = new ContentProviderRecord(cpi, ai);
5482 } catch (RemoteException ex) {
5483 // pm is in same process, this will never happen.
5484 }
5485 }
5486
5487 if (r != null && cpr.canRunHere(r)) {
5488 // If this is a multiprocess provider, then just return its
5489 // info and allow the caller to instantiate it. Only do
5490 // this if the provider is the same user as the caller's
5491 // process, or can run as root (so can be in any process).
5492 return cpr;
5493 }
5494
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005495 if (DEBUG_PROVIDER) {
5496 RuntimeException e = new RuntimeException("here");
Joe Onorato8a9b2202010-02-26 18:56:32 -08005497 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005498 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 }
5500
5501 // This is single process, and our app is now connecting to it.
5502 // See if we are already in the process of launching this
5503 // provider.
5504 final int N = mLaunchingProviders.size();
5505 int i;
5506 for (i=0; i<N; i++) {
5507 if (mLaunchingProviders.get(i) == cpr) {
5508 break;
5509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 }
5511
5512 // If the provider is not already being launched, then get it
5513 // started.
5514 if (i >= N) {
5515 final long origId = Binder.clearCallingIdentity();
5516 ProcessRecord proc = startProcessLocked(cpi.processName,
5517 cpr.appInfo, false, 0, "content provider",
5518 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005519 cpi.name), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005521 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 + cpi.applicationInfo.packageName + "/"
5523 + cpi.applicationInfo.uid + " for provider "
5524 + name + ": process is bad");
5525 return null;
5526 }
5527 cpr.launchingApp = proc;
5528 mLaunchingProviders.add(cpr);
5529 Binder.restoreCallingIdentity(origId);
5530 }
5531
5532 // Make sure the provider is published (the same provider class
5533 // may be published under multiple names).
5534 if (firstClass) {
5535 mProvidersByClass.put(cpi.name, cpr);
5536 }
5537 mProvidersByName.put(name, cpr);
5538
5539 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005540 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005541 "Adding provider requested by "
5542 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005543 + cpr.info.processName);
5544 Integer cnt = r.conProviders.get(cpr);
5545 if (cnt == null) {
5546 r.conProviders.put(cpr, new Integer(1));
5547 } else {
5548 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
5549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 cpr.clients.add(r);
5551 } else {
5552 cpr.externals++;
5553 }
5554 }
5555 }
5556
5557 // Wait for the provider to be published...
5558 synchronized (cpr) {
5559 while (cpr.provider == null) {
5560 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005561 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 + cpi.applicationInfo.packageName + "/"
5563 + cpi.applicationInfo.uid + " for provider "
5564 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08005565 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 cpi.applicationInfo.packageName,
5567 cpi.applicationInfo.uid, name);
5568 return null;
5569 }
5570 try {
5571 cpr.wait();
5572 } catch (InterruptedException ex) {
5573 }
5574 }
5575 }
5576 return cpr;
5577 }
5578
5579 public final ContentProviderHolder getContentProvider(
5580 IApplicationThread caller, String name) {
5581 if (caller == null) {
5582 String msg = "null IApplicationThread when getting content provider "
5583 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005584 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 throw new SecurityException(msg);
5586 }
5587
5588 return getContentProviderImpl(caller, name);
5589 }
5590
5591 private ContentProviderHolder getContentProviderExternal(String name) {
5592 return getContentProviderImpl(null, name);
5593 }
5594
5595 /**
5596 * Drop a content provider from a ProcessRecord's bookkeeping
5597 * @param cpr
5598 */
5599 public void removeContentProvider(IApplicationThread caller, String name) {
5600 synchronized (this) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07005601 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 if(cpr == null) {
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005603 // remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08005604 if (DEBUG_PROVIDER) Slog.v(TAG, name +
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005605 " provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 return;
5607 }
5608 final ProcessRecord r = getRecordForAppLocked(caller);
5609 if (r == null) {
5610 throw new SecurityException(
5611 "Unable to find app for caller " + caller +
5612 " when removing content provider " + name);
5613 }
5614 //update content provider record entry info
Dianne Hackborn860755f2010-06-03 18:47:52 -07005615 ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005616 if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005617 + r.info.processName + " from process "
5618 + localCpr.appInfo.processName);
5619 if (localCpr.app == r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 //should not happen. taken care of as a local provider
Joe Onorato8a9b2202010-02-26 18:56:32 -08005621 Slog.w(TAG, "removeContentProvider called on local provider: "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005622 + cpr.info.name + " in process " + r.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 return;
5624 } else {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005625 Integer cnt = r.conProviders.get(localCpr);
5626 if (cnt == null || cnt.intValue() <= 1) {
5627 localCpr.clients.remove(r);
5628 r.conProviders.remove(localCpr);
5629 } else {
5630 r.conProviders.put(localCpr, new Integer(cnt.intValue()-1));
5631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633 updateOomAdjLocked();
5634 }
5635 }
5636
5637 private void removeContentProviderExternal(String name) {
5638 synchronized (this) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07005639 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 if(cpr == null) {
5641 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08005642 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 return;
5644 }
5645
5646 //update content provider record entry info
Dianne Hackborn860755f2010-06-03 18:47:52 -07005647 ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 localCpr.externals--;
5649 if (localCpr.externals < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005650 Slog.e(TAG, "Externals < 0 for content provider " + localCpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005651 }
5652 updateOomAdjLocked();
5653 }
5654 }
5655
5656 public final void publishContentProviders(IApplicationThread caller,
5657 List<ContentProviderHolder> providers) {
5658 if (providers == null) {
5659 return;
5660 }
5661
5662 synchronized(this) {
5663 final ProcessRecord r = getRecordForAppLocked(caller);
5664 if (r == null) {
5665 throw new SecurityException(
5666 "Unable to find app for caller " + caller
5667 + " (pid=" + Binder.getCallingPid()
5668 + ") when publishing content providers");
5669 }
5670
5671 final long origId = Binder.clearCallingIdentity();
5672
5673 final int N = providers.size();
5674 for (int i=0; i<N; i++) {
5675 ContentProviderHolder src = providers.get(i);
5676 if (src == null || src.info == null || src.provider == null) {
5677 continue;
5678 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07005679 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 if (dst != null) {
5681 mProvidersByClass.put(dst.info.name, dst);
5682 String names[] = dst.info.authority.split(";");
5683 for (int j = 0; j < names.length; j++) {
5684 mProvidersByName.put(names[j], dst);
5685 }
5686
5687 int NL = mLaunchingProviders.size();
5688 int j;
5689 for (j=0; j<NL; j++) {
5690 if (mLaunchingProviders.get(j) == dst) {
5691 mLaunchingProviders.remove(j);
5692 j--;
5693 NL--;
5694 }
5695 }
5696 synchronized (dst) {
5697 dst.provider = src.provider;
5698 dst.app = r;
5699 dst.notifyAll();
5700 }
5701 updateOomAdjLocked(r);
5702 }
5703 }
5704
5705 Binder.restoreCallingIdentity(origId);
5706 }
5707 }
5708
5709 public static final void installSystemProviders() {
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08005710 List providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06005711 synchronized (mSelf) {
5712 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
5713 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08005714 if (providers != null) {
5715 for (int i=providers.size()-1; i>=0; i--) {
5716 ProviderInfo pi = (ProviderInfo)providers.get(i);
5717 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
5718 Slog.w(TAG, "Not installing system proc provider " + pi.name
5719 + ": not system .apk");
5720 providers.remove(i);
5721 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08005722 }
5723 }
5724 }
Josh Bartel2ecce342010-02-25 10:55:48 -06005725 if (providers != null) {
5726 mSystemThread.installSystemProviders(providers);
5727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 }
5729
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005730 /**
5731 * Allows app to retrieve the MIME type of a URI without having permission
5732 * to access its content provider.
5733 *
5734 * CTS tests for this functionality can be run with "runtest cts-appsecurity".
5735 *
5736 * Test cases are at cts/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/
5737 * src/com/android/cts/usespermissiondiffcertapp/AccessPermissionWithDiffSigTest.java
5738 */
5739 public String getProviderMimeType(Uri uri) {
5740 final String name = uri.getAuthority();
5741 final long ident = Binder.clearCallingIdentity();
5742 ContentProviderHolder holder = null;
5743
5744 try {
5745 holder = getContentProviderExternal(name);
5746 if (holder != null) {
5747 return holder.provider.getType(uri);
5748 }
5749 } catch (RemoteException e) {
5750 Log.w(TAG, "Content provider dead retrieving " + uri, e);
5751 return null;
5752 } finally {
5753 if (holder != null) {
5754 removeContentProviderExternal(name);
5755 }
5756 Binder.restoreCallingIdentity(ident);
5757 }
5758
5759 return null;
5760 }
5761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 // =========================================================
5763 // GLOBAL MANAGEMENT
5764 // =========================================================
5765
5766 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
5767 ApplicationInfo info, String customProcess) {
5768 String proc = customProcess != null ? customProcess : info.processName;
5769 BatteryStatsImpl.Uid.Proc ps = null;
5770 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
5771 synchronized (stats) {
5772 ps = stats.getProcessStatsLocked(info.uid, proc);
5773 }
5774 return new ProcessRecord(ps, thread, info, proc);
5775 }
5776
5777 final ProcessRecord addAppLocked(ApplicationInfo info) {
5778 ProcessRecord app = getProcessRecordLocked(info.processName, info.uid);
5779
5780 if (app == null) {
5781 app = newProcessRecordLocked(null, info, null);
5782 mProcessNames.put(info.processName, info.uid, app);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08005783 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 }
5785
5786 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
5787 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
5788 app.persistent = true;
5789 app.maxAdj = CORE_SERVER_ADJ;
5790 }
5791 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
5792 mPersistentStartingProcesses.add(app);
5793 startProcessLocked(app, "added application", app.processName);
5794 }
5795
5796 return app;
5797 }
5798
5799 public void unhandledBack() {
5800 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
5801 "unhandledBack()");
5802
5803 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005804 int count = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08005805 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 TAG, "Performing unhandledBack(): stack size = " + count);
5807 if (count > 1) {
5808 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005809 mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 count-1, Activity.RESULT_CANCELED, null, "unhandled-back");
5811 Binder.restoreCallingIdentity(origId);
5812 }
5813 }
5814 }
5815
5816 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
5817 String name = uri.getAuthority();
5818 ContentProviderHolder cph = getContentProviderExternal(name);
5819 ParcelFileDescriptor pfd = null;
5820 if (cph != null) {
5821 // We record the binder invoker's uid in thread-local storage before
5822 // going to the content provider to open the file. Later, in the code
5823 // that handles all permissions checks, we look for this uid and use
5824 // that rather than the Activity Manager's own uid. The effect is that
5825 // we do the check against the caller's permissions even though it looks
5826 // to the content provider like the Activity Manager itself is making
5827 // the request.
5828 sCallerIdentity.set(new Identity(
5829 Binder.getCallingPid(), Binder.getCallingUid()));
5830 try {
5831 pfd = cph.provider.openFile(uri, "r");
5832 } catch (FileNotFoundException e) {
5833 // do nothing; pfd will be returned null
5834 } finally {
5835 // Ensure that whatever happens, we clean up the identity state
5836 sCallerIdentity.remove();
5837 }
5838
5839 // We've got the fd now, so we're done with the provider.
5840 removeContentProviderExternal(name);
5841 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005842 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 }
5844 return pfd;
5845 }
5846
5847 public void goingToSleep() {
5848 synchronized(this) {
5849 mSleeping = true;
5850 mWindowManager.setEventDispatching(false);
5851
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005852 if (mMainStack.mResumedActivity != null) {
5853 mMainStack.pauseIfSleepingLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005854 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005855 Slog.w(TAG, "goingToSleep with no resumed activity!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005856 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005857
5858 // Initialize the wake times of all processes.
Dianne Hackborn287952c2010-09-22 22:34:31 -07005859 checkExcessivePowerUsageLocked(false);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005860 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
5861 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn287952c2010-09-22 22:34:31 -07005862 mHandler.sendMessageDelayed(nmsg, POWER_CHECK_DELAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 }
5864 }
5865
Dianne Hackborn55280a92009-05-07 15:53:46 -07005866 public boolean shutdown(int timeout) {
5867 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
5868 != PackageManager.PERMISSION_GRANTED) {
5869 throw new SecurityException("Requires permission "
5870 + android.Manifest.permission.SHUTDOWN);
5871 }
5872
5873 boolean timedout = false;
5874
5875 synchronized(this) {
5876 mShuttingDown = true;
5877 mWindowManager.setEventDispatching(false);
5878
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005879 if (mMainStack.mResumedActivity != null) {
5880 mMainStack.pauseIfSleepingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07005881 final long endTime = System.currentTimeMillis() + timeout;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005882 while (mMainStack.mResumedActivity != null
5883 || mMainStack.mPausingActivity != null) {
Dianne Hackborn55280a92009-05-07 15:53:46 -07005884 long delay = endTime - System.currentTimeMillis();
5885 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005886 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07005887 timedout = true;
5888 break;
5889 }
5890 try {
5891 this.wait();
5892 } catch (InterruptedException e) {
5893 }
5894 }
5895 }
5896 }
5897
5898 mUsageStatsService.shutdown();
5899 mBatteryStatsService.shutdown();
5900
5901 return timedout;
5902 }
5903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 public void wakingUp() {
5905 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005906 if (mMainStack.mGoingToSleep.isHeld()) {
5907 mMainStack.mGoingToSleep.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 }
5909 mWindowManager.setEventDispatching(true);
5910 mSleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005911 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 }
5913 }
5914
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005915 public void stopAppSwitches() {
5916 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
5917 != PackageManager.PERMISSION_GRANTED) {
5918 throw new SecurityException("Requires permission "
5919 + android.Manifest.permission.STOP_APP_SWITCHES);
5920 }
5921
5922 synchronized(this) {
5923 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
5924 + APP_SWITCH_DELAY_TIME;
5925 mDidAppSwitch = false;
5926 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
5927 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
5928 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
5929 }
5930 }
5931
5932 public void resumeAppSwitches() {
5933 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
5934 != PackageManager.PERMISSION_GRANTED) {
5935 throw new SecurityException("Requires permission "
5936 + android.Manifest.permission.STOP_APP_SWITCHES);
5937 }
5938
5939 synchronized(this) {
5940 // Note that we don't execute any pending app switches... we will
5941 // let those wait until either the timeout, or the next start
5942 // activity request.
5943 mAppSwitchesAllowedTime = 0;
5944 }
5945 }
5946
5947 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
5948 String name) {
5949 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
5950 return true;
5951 }
5952
5953 final int perm = checkComponentPermission(
5954 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
5955 callingUid, -1);
5956 if (perm == PackageManager.PERMISSION_GRANTED) {
5957 return true;
5958 }
5959
Joe Onorato8a9b2202010-02-26 18:56:32 -08005960 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005961 return false;
5962 }
5963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005964 public void setDebugApp(String packageName, boolean waitForDebugger,
5965 boolean persistent) {
5966 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
5967 "setDebugApp()");
5968
5969 // Note that this is not really thread safe if there are multiple
5970 // callers into it at the same time, but that's not a situation we
5971 // care about.
5972 if (persistent) {
5973 final ContentResolver resolver = mContext.getContentResolver();
5974 Settings.System.putString(
5975 resolver, Settings.System.DEBUG_APP,
5976 packageName);
5977 Settings.System.putInt(
5978 resolver, Settings.System.WAIT_FOR_DEBUGGER,
5979 waitForDebugger ? 1 : 0);
5980 }
5981
5982 synchronized (this) {
5983 if (!persistent) {
5984 mOrigDebugApp = mDebugApp;
5985 mOrigWaitForDebugger = mWaitForDebugger;
5986 }
5987 mDebugApp = packageName;
5988 mWaitForDebugger = waitForDebugger;
5989 mDebugTransient = !persistent;
5990 if (packageName != null) {
5991 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005992 forceStopPackageLocked(packageName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005993 Binder.restoreCallingIdentity(origId);
5994 }
5995 }
5996 }
5997
5998 public void setAlwaysFinish(boolean enabled) {
5999 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
6000 "setAlwaysFinish()");
6001
6002 Settings.System.putInt(
6003 mContext.getContentResolver(),
6004 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
6005
6006 synchronized (this) {
6007 mAlwaysFinishActivities = enabled;
6008 }
6009 }
6010
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006011 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006013 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006015 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006016 }
6017 }
6018
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08006019 public boolean isUserAMonkey() {
6020 // For now the fact that there is a controller implies
6021 // we have a monkey.
6022 synchronized (this) {
6023 return mController != null;
6024 }
6025 }
6026
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006027 public void registerActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06006028 synchronized (this) {
6029 mWatchers.register(watcher);
6030 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006031 }
6032
6033 public void unregisterActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06006034 synchronized (this) {
6035 mWatchers.unregister(watcher);
6036 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006037 }
6038
Daniel Sandler69a48172010-06-23 16:29:36 -04006039 public void setImmersive(IBinder token, boolean immersive) {
6040 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006041 int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1;
Daniel Sandler69a48172010-06-23 16:29:36 -04006042 if (index < 0) {
6043 throw new IllegalArgumentException();
6044 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006045 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
Daniel Sandler69a48172010-06-23 16:29:36 -04006046 r.immersive = immersive;
6047 }
6048 }
6049
6050 public boolean isImmersive(IBinder token) {
6051 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006052 int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1;
Daniel Sandler69a48172010-06-23 16:29:36 -04006053 if (index < 0) {
6054 throw new IllegalArgumentException();
6055 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006056 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
Daniel Sandler69a48172010-06-23 16:29:36 -04006057 return r.immersive;
6058 }
6059 }
6060
6061 public boolean isTopActivityImmersive() {
6062 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006063 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Daniel Sandler69a48172010-06-23 16:29:36 -04006064 return (r != null) ? r.immersive : false;
6065 }
6066 }
6067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006068 public final void enterSafeMode() {
6069 synchronized(this) {
6070 // It only makes sense to do this before the system is ready
6071 // and started launching other packages.
6072 if (!mSystemReady) {
6073 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006074 AppGlobals.getPackageManager().enterSafeMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 } catch (RemoteException e) {
6076 }
6077
6078 View v = LayoutInflater.from(mContext).inflate(
6079 com.android.internal.R.layout.safe_mode, null);
6080 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Jeff Brown3b2b3542010-10-15 00:54:27 -07006081 lp.type = WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006082 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
6083 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
6084 lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
6085 lp.format = v.getBackground().getOpacity();
6086 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
6087 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
6088 ((WindowManager)mContext.getSystemService(
6089 Context.WINDOW_SERVICE)).addView(v, lp);
6090 }
6091 }
6092 }
6093
6094 public void noteWakeupAlarm(IIntentSender sender) {
6095 if (!(sender instanceof PendingIntentRecord)) {
6096 return;
6097 }
6098 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
6099 synchronized (stats) {
6100 if (mBatteryStatsService.isOnBattery()) {
6101 mBatteryStatsService.enforceCallingPermission();
6102 PendingIntentRecord rec = (PendingIntentRecord)sender;
6103 int MY_UID = Binder.getCallingUid();
6104 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
6105 BatteryStatsImpl.Uid.Pkg pkg =
6106 stats.getPackageStatsLocked(uid, rec.key.packageName);
6107 pkg.incWakeupsLocked();
6108 }
6109 }
6110 }
6111
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07006112 public boolean killPids(int[] pids, String pReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006113 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07006114 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006115 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07006116 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006117 // XXX Note: don't acquire main activity lock here, because the window
6118 // manager calls in with its locks held.
6119
6120 boolean killed = false;
6121 synchronized (mPidsSelfLocked) {
6122 int[] types = new int[pids.length];
6123 int worstType = 0;
6124 for (int i=0; i<pids.length; i++) {
6125 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
6126 if (proc != null) {
6127 int type = proc.setAdj;
6128 types[i] = type;
6129 if (type > worstType) {
6130 worstType = type;
6131 }
6132 }
6133 }
6134
6135 // If the worse oom_adj is somewhere in the hidden proc LRU range,
6136 // then constrain it so we will kill all hidden procs.
6137 if (worstType < EMPTY_APP_ADJ && worstType > HIDDEN_APP_MIN_ADJ) {
6138 worstType = HIDDEN_APP_MIN_ADJ;
6139 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07006140 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 for (int i=0; i<pids.length; i++) {
6142 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
6143 if (proc == null) {
6144 continue;
6145 }
6146 int adj = proc.setAdj;
Dianne Hackborn906497c2010-05-10 15:57:38 -07006147 if (adj >= worstType && !proc.killedBackground) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07006148 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07006149 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
6150 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006151 killed = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -07006152 proc.killedBackground = true;
6153 Process.killProcessQuiet(pids[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006154 }
6155 }
6156 }
6157 return killed;
6158 }
6159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 public final void startRunning(String pkg, String cls, String action,
6161 String data) {
6162 synchronized(this) {
6163 if (mStartRunning) {
6164 return;
6165 }
6166 mStartRunning = true;
6167 mTopComponent = pkg != null && cls != null
6168 ? new ComponentName(pkg, cls) : null;
6169 mTopAction = action != null ? action : Intent.ACTION_MAIN;
6170 mTopData = data;
6171 if (!mSystemReady) {
6172 return;
6173 }
6174 }
6175
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07006176 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 }
6178
6179 private void retrieveSettings() {
6180 final ContentResolver resolver = mContext.getContentResolver();
6181 String debugApp = Settings.System.getString(
6182 resolver, Settings.System.DEBUG_APP);
6183 boolean waitForDebugger = Settings.System.getInt(
6184 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
6185 boolean alwaysFinishActivities = Settings.System.getInt(
6186 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
6187
6188 Configuration configuration = new Configuration();
6189 Settings.System.getConfiguration(resolver, configuration);
6190
6191 synchronized (this) {
6192 mDebugApp = mOrigDebugApp = debugApp;
6193 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
6194 mAlwaysFinishActivities = alwaysFinishActivities;
6195 // This happens before any activities are started, so we can
6196 // change mConfiguration in-place.
6197 mConfiguration.updateFrom(configuration);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006198 mConfigurationSeq = mConfiguration.seq = 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -08006199 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006200 }
6201 }
6202
6203 public boolean testIsSystemReady() {
6204 // no need to synchronize(this) just to read & return the value
6205 return mSystemReady;
6206 }
6207
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006208 private static File getCalledPreBootReceiversFile() {
6209 File dataDir = Environment.getDataDirectory();
6210 File systemDir = new File(dataDir, "system");
6211 File fname = new File(systemDir, "called_pre_boots.dat");
6212 return fname;
6213 }
6214
6215 private static ArrayList<ComponentName> readLastDonePreBootReceivers() {
6216 ArrayList<ComponentName> lastDoneReceivers = new ArrayList<ComponentName>();
6217 File file = getCalledPreBootReceiversFile();
6218 FileInputStream fis = null;
6219 try {
6220 fis = new FileInputStream(file);
6221 DataInputStream dis = new DataInputStream(new BufferedInputStream(fis, 2048));
6222 int vers = dis.readInt();
6223 String codename = dis.readUTF();
6224 if (vers == android.os.Build.VERSION.SDK_INT
6225 && codename.equals(android.os.Build.VERSION.CODENAME)) {
6226 int num = dis.readInt();
6227 while (num > 0) {
6228 num--;
6229 String pkg = dis.readUTF();
6230 String cls = dis.readUTF();
6231 lastDoneReceivers.add(new ComponentName(pkg, cls));
6232 }
6233 }
6234 } catch (FileNotFoundException e) {
6235 } catch (IOException e) {
6236 Slog.w(TAG, "Failure reading last done pre-boot receivers", e);
6237 } finally {
6238 if (fis != null) {
6239 try {
6240 fis.close();
6241 } catch (IOException e) {
6242 }
6243 }
6244 }
6245 return lastDoneReceivers;
6246 }
6247
6248 private static void writeLastDonePreBootReceivers(ArrayList<ComponentName> list) {
6249 File file = getCalledPreBootReceiversFile();
6250 FileOutputStream fos = null;
6251 DataOutputStream dos = null;
6252 try {
6253 Slog.i(TAG, "Writing new set of last done pre-boot receivers...");
6254 fos = new FileOutputStream(file);
6255 dos = new DataOutputStream(new BufferedOutputStream(fos, 2048));
6256 dos.writeInt(android.os.Build.VERSION.SDK_INT);
6257 dos.writeUTF(android.os.Build.VERSION.CODENAME);
6258 dos.writeInt(list.size());
6259 for (int i=0; i<list.size(); i++) {
6260 dos.writeUTF(list.get(i).getPackageName());
6261 dos.writeUTF(list.get(i).getClassName());
6262 }
6263 } catch (IOException e) {
6264 Slog.w(TAG, "Failure writing last done pre-boot receivers", e);
6265 file.delete();
6266 } finally {
Dianne Hackborn8bdf5932010-10-15 12:54:40 -07006267 FileUtils.sync(fos);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006268 if (dos != null) {
6269 try {
6270 dos.close();
6271 } catch (IOException e) {
6272 // TODO Auto-generated catch block
6273 e.printStackTrace();
6274 }
6275 }
6276 }
6277 }
6278
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07006279 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 // In the simulator, startRunning will never have been called, which
6281 // normally sets a few crucial variables. Do it here instead.
6282 if (!Process.supportsProcesses()) {
6283 mStartRunning = true;
6284 mTopAction = Intent.ACTION_MAIN;
6285 }
6286
6287 synchronized(this) {
6288 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07006289 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 return;
6291 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006292
6293 // Check to see if there are any update receivers to run.
6294 if (!mDidUpdate) {
6295 if (mWaitingUpdate) {
6296 return;
6297 }
6298 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
6299 List<ResolveInfo> ris = null;
6300 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006301 ris = AppGlobals.getPackageManager().queryIntentReceivers(
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006302 intent, null, 0);
6303 } catch (RemoteException e) {
6304 }
6305 if (ris != null) {
6306 for (int i=ris.size()-1; i>=0; i--) {
6307 if ((ris.get(i).activityInfo.applicationInfo.flags
6308 &ApplicationInfo.FLAG_SYSTEM) == 0) {
6309 ris.remove(i);
6310 }
6311 }
6312 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006313
6314 ArrayList<ComponentName> lastDoneReceivers = readLastDonePreBootReceivers();
6315
6316 final ArrayList<ComponentName> doneReceivers = new ArrayList<ComponentName>();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006317 for (int i=0; i<ris.size(); i++) {
6318 ActivityInfo ai = ris.get(i).activityInfo;
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006319 ComponentName comp = new ComponentName(ai.packageName, ai.name);
6320 if (lastDoneReceivers.contains(comp)) {
6321 ris.remove(i);
6322 i--;
6323 }
6324 }
6325
6326 for (int i=0; i<ris.size(); i++) {
6327 ActivityInfo ai = ris.get(i).activityInfo;
6328 ComponentName comp = new ComponentName(ai.packageName, ai.name);
6329 doneReceivers.add(comp);
6330 intent.setComponent(comp);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006331 IIntentReceiver finisher = null;
Dianne Hackbornd6847842010-01-12 18:14:19 -08006332 if (i == ris.size()-1) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006333 finisher = new IIntentReceiver.Stub() {
6334 public void performReceive(Intent intent, int resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -07006335 String data, Bundle extras, boolean ordered,
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006336 boolean sticky) {
6337 // The raw IIntentReceiver interface is called
6338 // with the AM lock held, so redispatch to
6339 // execute our code without the lock.
6340 mHandler.post(new Runnable() {
6341 public void run() {
6342 synchronized (ActivityManagerService.this) {
6343 mDidUpdate = true;
6344 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006345 writeLastDonePreBootReceivers(doneReceivers);
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -07006346 systemReady(goingCallback);
6347 }
6348 });
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006349 }
6350 };
6351 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006352 Slog.i(TAG, "Sending system update to: " + intent.getComponent());
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006353 broadcastIntentLocked(null, null, intent, null, finisher,
6354 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackbornd6847842010-01-12 18:14:19 -08006355 if (finisher != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006356 mWaitingUpdate = true;
6357 }
6358 }
6359 }
6360 if (mWaitingUpdate) {
6361 return;
6362 }
6363 mDidUpdate = true;
6364 }
6365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006366 mSystemReady = true;
6367 if (!mStartRunning) {
6368 return;
6369 }
6370 }
6371
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006372 ArrayList<ProcessRecord> procsToKill = null;
6373 synchronized(mPidsSelfLocked) {
6374 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
6375 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
6376 if (!isAllowedWhileBooting(proc.info)){
6377 if (procsToKill == null) {
6378 procsToKill = new ArrayList<ProcessRecord>();
6379 }
6380 procsToKill.add(proc);
6381 }
6382 }
6383 }
6384
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006385 synchronized(this) {
6386 if (procsToKill != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006387 for (int i=procsToKill.size()-1; i>=0; i--) {
6388 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08006389 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006390 removeProcessLocked(proc, true);
6391 }
6392 }
Dianne Hackborncef65ee2010-09-30 18:27:22 -07006393
6394 // Now that we have cleaned up any update processes, we
6395 // are ready to start launching real processes and know that
6396 // we won't trample on them any more.
6397 mProcessesReady = true;
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006398 }
6399
Joe Onorato8a9b2202010-02-26 18:56:32 -08006400 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006401 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006402 SystemClock.uptimeMillis());
6403
6404 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006405 // Make sure we have no pre-ready processes sitting around.
6406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
6408 ResolveInfo ri = mContext.getPackageManager()
6409 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07006410 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006411 CharSequence errorMsg = null;
6412 if (ri != null) {
6413 ActivityInfo ai = ri.activityInfo;
6414 ApplicationInfo app = ai.applicationInfo;
6415 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
6416 mTopAction = Intent.ACTION_FACTORY_TEST;
6417 mTopData = null;
6418 mTopComponent = new ComponentName(app.packageName,
6419 ai.name);
6420 } else {
6421 errorMsg = mContext.getResources().getText(
6422 com.android.internal.R.string.factorytest_not_system);
6423 }
6424 } else {
6425 errorMsg = mContext.getResources().getText(
6426 com.android.internal.R.string.factorytest_no_action);
6427 }
6428 if (errorMsg != null) {
6429 mTopAction = null;
6430 mTopData = null;
6431 mTopComponent = null;
6432 Message msg = Message.obtain();
6433 msg.what = SHOW_FACTORY_ERROR_MSG;
6434 msg.getData().putCharSequence("msg", errorMsg);
6435 mHandler.sendMessage(msg);
6436 }
6437 }
6438 }
6439
6440 retrieveSettings();
6441
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07006442 if (goingCallback != null) goingCallback.run();
6443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006444 synchronized (this) {
6445 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
6446 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006447 List apps = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07006448 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006449 if (apps != null) {
6450 int N = apps.size();
6451 int i;
6452 for (i=0; i<N; i++) {
6453 ApplicationInfo info
6454 = (ApplicationInfo)apps.get(i);
6455 if (info != null &&
6456 !info.packageName.equals("android")) {
6457 addAppLocked(info);
6458 }
6459 }
6460 }
6461 } catch (RemoteException ex) {
6462 // pm is in same process, this will never happen.
6463 }
6464 }
6465
Dianne Hackborn9acc0302009-08-25 00:27:12 -07006466 // Start up initial activity.
6467 mBooting = true;
6468
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006469 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006470 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006471 Message msg = Message.obtain();
6472 msg.what = SHOW_UID_ERROR_MSG;
6473 mHandler.sendMessage(msg);
6474 }
6475 } catch (RemoteException e) {
6476 }
6477
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006478 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006479 }
6480 }
6481
Dan Egnorb7f03672009-12-09 16:22:32 -08006482 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08006483 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006484 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08006485 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08006486 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006487 startAppProblemLocked(app);
6488 app.stopFreezingAllLocked();
6489 return handleAppCrashLocked(app);
6490 }
6491
Dan Egnorb7f03672009-12-09 16:22:32 -08006492 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08006493 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08006495 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08006496 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
6497 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 startAppProblemLocked(app);
6499 app.stopFreezingAllLocked();
6500 }
6501
6502 /**
6503 * Generate a process error record, suitable for attachment to a ProcessRecord.
6504 *
6505 * @param app The ProcessRecord in which the error occurred.
6506 * @param condition Crashing, Application Not Responding, etc. Values are defined in
6507 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08006508 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006509 * @param shortMsg Short message describing the crash.
6510 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08006511 * @param stackTrace Full crash stack trace, may be null.
6512 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006513 * @return Returns a fully-formed AppErrorStateInfo record.
6514 */
6515 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08006516 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006517 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08006518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006519 report.condition = condition;
6520 report.processName = app.processName;
6521 report.pid = app.pid;
6522 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08006523 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 report.shortMsg = shortMsg;
6525 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08006526 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527
6528 return report;
6529 }
6530
Dan Egnor42471dd2010-01-07 17:25:22 -08006531 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 synchronized (this) {
6533 app.crashing = false;
6534 app.crashingReport = null;
6535 app.notResponding = false;
6536 app.notRespondingReport = null;
6537 if (app.anrDialog == fromDialog) {
6538 app.anrDialog = null;
6539 }
6540 if (app.waitDialog == fromDialog) {
6541 app.waitDialog = null;
6542 }
6543 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08006544 handleAppCrashLocked(app);
Dianne Hackborn8633e682010-04-22 16:03:41 -07006545 Slog.i(ActivityManagerService.TAG, "Killing "
6546 + app.processName + " (pid=" + app.pid + "): user's request");
6547 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
6548 app.processName, app.setAdj, "user's request after error");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 Process.killProcess(app.pid);
6550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 }
6552 }
Dan Egnor42471dd2010-01-07 17:25:22 -08006553
Dan Egnorb7f03672009-12-09 16:22:32 -08006554 private boolean handleAppCrashLocked(ProcessRecord app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 long now = SystemClock.uptimeMillis();
6556
6557 Long crashTime = mProcessCrashTimes.get(app.info.processName,
6558 app.info.uid);
6559 if (crashTime != null && now < crashTime+MIN_CRASH_INTERVAL) {
6560 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08006561 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006562 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006563 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006564 app.info.processName, app.info.uid);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006565 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
6566 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006568 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006570 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006571 }
6572 }
6573 if (!app.persistent) {
Dianne Hackborn070783f2010-12-29 16:46:28 -08006574 // Don't let services in this process be restarted and potentially
6575 // annoy the user repeatedly. Unless it is persistent, since those
6576 // processes run critical code.
6577 killServicesLocked(app, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006578 // We don't want to start this process again until the user
6579 // explicitly does so... but for persistent process, we really
6580 // need to keep it running. If a persistent process is actually
6581 // repeatedly crashing, then badness for everyone.
Doug Zongker2bec3d42009-12-04 12:52:44 -08006582 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 app.info.processName);
6584 mBadProcesses.put(app.info.processName, app.info.uid, now);
6585 app.bad = true;
6586 mProcessCrashTimes.remove(app.info.processName, app.info.uid);
6587 app.removed = true;
6588 removeProcessLocked(app, false);
6589 return false;
6590 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006591 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006592 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006593 if (r.app == app) {
6594 // If the top running activity is from this crashing
6595 // process, then terminate it to avoid getting in a loop.
6596 Slog.w(TAG, " Force finishing activity "
6597 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006598 int index = mMainStack.indexOfTokenLocked(r);
6599 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006600 Activity.RESULT_CANCELED, null, "crashed");
Dianne Hackborn070783f2010-12-29 16:46:28 -08006601 // Also terminate any activities below it that aren't yet
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006602 // stopped, to avoid a situation where one will get
6603 // re-start our crashing activity once it gets resumed again.
6604 index--;
6605 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006606 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006607 if (r.state == ActivityState.RESUMED
6608 || r.state == ActivityState.PAUSING
6609 || r.state == ActivityState.PAUSED) {
6610 if (!r.isHomeActivity) {
6611 Slog.w(TAG, " Force finishing activity "
6612 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006613 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006614 Activity.RESULT_CANCELED, null, "crashed");
6615 }
6616 }
6617 }
6618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 }
6620
6621 // Bump up the crash count of any services currently running in the proc.
6622 if (app.services.size() != 0) {
6623 // Any services running in the application need to be placed
6624 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07006625 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07006627 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 sr.crashCount++;
6629 }
6630 }
Mattias Larssona4fd0072010-06-22 22:37:03 +02006631
6632 // If the crashing process is what we consider to be the "home process" and it has been
6633 // replaced by a third-party app, clear the package preferred activities from packages
6634 // with a home activity running in the process to prevent a repeatedly crashing app
6635 // from blocking the user to manually clear the list.
6636 if (app == mHomeProcess && mHomeProcess.activities.size() > 0
6637 && (mHomeProcess.info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
6638 Iterator it = mHomeProcess.activities.iterator();
6639 while (it.hasNext()) {
Jean-Baptiste Queru5ea89f72010-07-30 09:30:31 -07006640 ActivityRecord r = (ActivityRecord)it.next();
Mattias Larssona4fd0072010-06-22 22:37:03 +02006641 if (r.isHomeActivity) {
6642 Log.i(TAG, "Clearing package preferred activities from " + r.packageName);
6643 try {
6644 ActivityThread.getPackageManager()
6645 .clearPackagePreferredActivities(r.packageName);
6646 } catch (RemoteException c) {
6647 // pm is in same process, this will never happen.
6648 }
6649 }
6650 }
6651 }
6652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 mProcessCrashTimes.put(app.info.processName, app.info.uid, now);
6654 return true;
6655 }
6656
6657 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08006658 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
6659 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006660 skipCurrentReceiverLocked(app);
6661 }
6662
6663 void skipCurrentReceiverLocked(ProcessRecord app) {
6664 boolean reschedule = false;
6665 BroadcastRecord r = app.curReceiver;
6666 if (r != null) {
6667 // The current broadcast is waiting for this app's receiver
6668 // to be finished. Looks like that's not going to happen, so
6669 // let the broadcast continue.
Jeff Brown4d94a762010-09-23 11:33:28 -07006670 logBroadcastReceiverDiscardLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006671 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
6672 r.resultExtras, r.resultAbort, true);
6673 reschedule = true;
6674 }
6675 r = mPendingBroadcast;
6676 if (r != null && r.curApp == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006677 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 "skip & discard pending app " + r);
Jeff Brown4d94a762010-09-23 11:33:28 -07006679 logBroadcastReceiverDiscardLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006680 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
6681 r.resultExtras, r.resultAbort, true);
6682 reschedule = true;
6683 }
6684 if (reschedule) {
6685 scheduleBroadcastsLocked();
6686 }
6687 }
6688
Dan Egnor60d87622009-12-16 16:32:58 -08006689 /**
6690 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
6691 * The application process will exit immediately after this call returns.
6692 * @param app object of the crashing app, null for the system server
6693 * @param crashInfo describing the exception
6694 */
6695 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
6696 ProcessRecord r = findAppProcess(app);
6697
6698 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
6699 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08006700 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08006701 crashInfo.exceptionClassName,
6702 crashInfo.exceptionMessage,
6703 crashInfo.throwFileName,
6704 crashInfo.throwLineNumber);
6705
Dan Egnor42471dd2010-01-07 17:25:22 -08006706 addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08006707
6708 crashApplication(r, crashInfo);
6709 }
6710
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006711 public void handleApplicationStrictModeViolation(
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006712 IBinder app,
6713 int violationMask,
6714 StrictMode.ViolationInfo info) {
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006715 ProcessRecord r = findAppProcess(app);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006716
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006717 if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
Brad Fitzpatrickf3d86be2010-11-23 10:31:52 -08006718 Integer stackFingerprint = info.hashCode();
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006719 boolean logIt = true;
6720 synchronized (mAlreadyLoggedViolatedStacks) {
6721 if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
6722 logIt = false;
6723 // TODO: sub-sample into EventLog for these, with
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006724 // the info.durationMillis? Then we'd get
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006725 // the relative pain numbers, without logging all
6726 // the stack traces repeatedly. We'd want to do
6727 // likewise in the client code, which also does
6728 // dup suppression, before the Binder call.
6729 } else {
6730 if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
6731 mAlreadyLoggedViolatedStacks.clear();
6732 }
6733 mAlreadyLoggedViolatedStacks.add(stackFingerprint);
6734 }
6735 }
6736 if (logIt) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006737 logStrictModeViolationToDropBox(r, info);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006738 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006739 }
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006740
6741 if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
6742 AppErrorResult result = new AppErrorResult();
6743 synchronized (this) {
6744 final long origId = Binder.clearCallingIdentity();
6745
6746 Message msg = Message.obtain();
6747 msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
6748 HashMap<String, Object> data = new HashMap<String, Object>();
6749 data.put("result", result);
6750 data.put("app", r);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006751 data.put("violationMask", violationMask);
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006752 data.put("info", info);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006753 msg.obj = data;
6754 mHandler.sendMessage(msg);
6755
6756 Binder.restoreCallingIdentity(origId);
6757 }
6758 int res = result.get();
Dianne Hackbornb424b632010-08-18 15:59:05 -07006759 Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006760 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006761 }
6762
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006763 // Depending on the policy in effect, there could be a bunch of
6764 // these in quick succession so we try to batch these together to
6765 // minimize disk writes, number of dropbox entries, and maximize
6766 // compression, by having more fewer, larger records.
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006767 private void logStrictModeViolationToDropBox(
6768 ProcessRecord process,
6769 StrictMode.ViolationInfo info) {
6770 if (info == null) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006771 return;
6772 }
6773 final boolean isSystemApp = process == null ||
6774 (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
6775 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
6776 final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
6777 final DropBoxManager dbox = (DropBoxManager)
6778 mContext.getSystemService(Context.DROPBOX_SERVICE);
6779
6780 // Exit early if the dropbox isn't configured to accept this report type.
6781 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
6782
6783 boolean bufferWasEmpty;
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006784 boolean needsFlush;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006785 final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
6786 synchronized (sb) {
6787 bufferWasEmpty = sb.length() == 0;
6788 appendDropBoxProcessHeaders(process, sb);
6789 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
6790 sb.append("System-App: ").append(isSystemApp).append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006791 sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
6792 if (info.violationNumThisLoop != 0) {
6793 sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
6794 }
Brad Fitzpatrick599ca292010-10-22 14:47:03 -07006795 if (info.numAnimationsRunning != 0) {
6796 sb.append("Animations-Running: ").append(info.numAnimationsRunning).append("\n");
6797 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07006798 if (info.broadcastIntentAction != null) {
6799 sb.append("Broadcast-Intent-Action: ").append(info.broadcastIntentAction).append("\n");
6800 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08006801 if (info.durationMillis != -1) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006802 sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006803 }
Brad Fitzpatricke7520d82010-11-10 18:08:36 -08006804 if (info.tags != null) {
6805 for (String tag : info.tags) {
6806 sb.append("Span-Tag: ").append(tag).append("\n");
6807 }
6808 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006809 sb.append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006810 if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
6811 sb.append(info.crashInfo.stackTrace);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006812 }
6813 sb.append("\n");
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006814
6815 // Only buffer up to ~64k. Various logging bits truncate
6816 // things at 128k.
6817 needsFlush = (sb.length() > 64 * 1024);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006818 }
6819
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006820 // Flush immediately if the buffer's grown too large, or this
6821 // is a non-system app. Non-system apps are isolated with a
6822 // different tag & policy and not batched.
6823 //
6824 // Batching is useful during internal testing with
6825 // StrictMode settings turned up high. Without batching,
6826 // thousands of separate files could be created on boot.
6827 if (!isSystemApp || needsFlush) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006828 new Thread("Error dump: " + dropboxTag) {
6829 @Override
6830 public void run() {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006831 String report;
6832 synchronized (sb) {
6833 report = sb.toString();
6834 sb.delete(0, sb.length());
6835 sb.trimToSize();
6836 }
6837 if (report.length() != 0) {
6838 dbox.addText(dropboxTag, report);
6839 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006840 }
6841 }.start();
6842 return;
6843 }
6844
6845 // System app batching:
6846 if (!bufferWasEmpty) {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006847 // An existing dropbox-writing thread is outstanding, so
6848 // we don't need to start it up. The existing thread will
6849 // catch the buffer appends we just did.
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006850 return;
6851 }
6852
6853 // Worker thread to both batch writes and to avoid blocking the caller on I/O.
6854 // (After this point, we shouldn't access AMS internal data structures.)
6855 new Thread("Error dump: " + dropboxTag) {
6856 @Override
6857 public void run() {
6858 // 5 second sleep to let stacks arrive and be batched together
6859 try {
6860 Thread.sleep(5000); // 5 seconds
6861 } catch (InterruptedException e) {}
6862
6863 String errorReport;
6864 synchronized (mStrictModeBuffer) {
6865 errorReport = mStrictModeBuffer.toString();
6866 if (errorReport.length() == 0) {
6867 return;
6868 }
6869 mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
6870 mStrictModeBuffer.trimToSize();
6871 }
6872 dbox.addText(dropboxTag, errorReport);
6873 }
6874 }.start();
6875 }
6876
Dan Egnor60d87622009-12-16 16:32:58 -08006877 /**
6878 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
6879 * @param app object of the crashing app, null for the system server
6880 * @param tag reported by the caller
6881 * @param crashInfo describing the context of the error
6882 * @return true if the process should exit immediately (WTF is fatal)
6883 */
6884 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08006885 ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnor60d87622009-12-16 16:32:58 -08006886 ProcessRecord r = findAppProcess(app);
6887
6888 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
6889 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08006890 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08006891 tag, crashInfo.exceptionMessage);
6892
Dan Egnor42471dd2010-01-07 17:25:22 -08006893 addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08006894
Doug Zongker43866e02010-01-07 12:09:54 -08006895 if (Settings.Secure.getInt(mContext.getContentResolver(),
6896 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08006897 crashApplication(r, crashInfo);
6898 return true;
6899 } else {
6900 return false;
6901 }
6902 }
6903
6904 /**
6905 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
6906 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
6907 */
6908 private ProcessRecord findAppProcess(IBinder app) {
6909 if (app == null) {
6910 return null;
6911 }
6912
6913 synchronized (this) {
6914 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
6915 final int NA = apps.size();
6916 for (int ia=0; ia<NA; ia++) {
6917 ProcessRecord p = apps.valueAt(ia);
6918 if (p.thread != null && p.thread.asBinder() == app) {
6919 return p;
6920 }
6921 }
6922 }
6923
Joe Onorato8a9b2202010-02-26 18:56:32 -08006924 Slog.w(TAG, "Can't find mystery application: " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08006925 return null;
6926 }
6927 }
6928
6929 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006930 * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
6931 * to append various headers to the dropbox log text.
Dan Egnor60d87622009-12-16 16:32:58 -08006932 */
Brad Fitzpatrick1e02d362010-09-10 09:19:50 -07006933 private void appendDropBoxProcessHeaders(ProcessRecord process, StringBuilder sb) {
6934 // Note: ProcessRecord 'process' is guarded by the service
6935 // instance. (notably process.pkgList, which could otherwise change
6936 // concurrently during execution of this method)
6937 synchronized (this) {
6938 if (process == null || process.pid == MY_PID) {
6939 sb.append("Process: system_server\n");
6940 } else {
6941 sb.append("Process: ").append(process.processName).append("\n");
6942 }
6943 if (process == null) {
6944 return;
6945 }
Dan Egnora455d192010-03-12 08:52:28 -08006946 int flags = process.info.flags;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006947 IPackageManager pm = AppGlobals.getPackageManager();
Dan Egnora455d192010-03-12 08:52:28 -08006948 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
6949 for (String pkg : process.pkgList) {
6950 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08006951 try {
Dan Egnora455d192010-03-12 08:52:28 -08006952 PackageInfo pi = pm.getPackageInfo(pkg, 0);
6953 if (pi != null) {
6954 sb.append(" v").append(pi.versionCode);
6955 if (pi.versionName != null) {
6956 sb.append(" (").append(pi.versionName).append(")");
6957 }
6958 }
6959 } catch (RemoteException e) {
6960 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08006961 }
Dan Egnora455d192010-03-12 08:52:28 -08006962 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08006963 }
Dan Egnora455d192010-03-12 08:52:28 -08006964 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006965 }
6966
6967 private static String processClass(ProcessRecord process) {
6968 if (process == null || process.pid == MY_PID) {
6969 return "system_server";
6970 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
6971 return "system_app";
6972 } else {
6973 return "data_app";
6974 }
6975 }
6976
6977 /**
6978 * Write a description of an error (crash, WTF, ANR) to the drop box.
6979 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
6980 * @param process which caused the error, null means the system server
6981 * @param activity which triggered the error, null if unknown
6982 * @param parent activity related to the error, null if unknown
6983 * @param subject line related to the error, null if absent
6984 * @param report in long form describing the error, null if absent
6985 * @param logFile to include in the report, null if none
6986 * @param crashInfo giving an application stack trace, null if absent
6987 */
6988 public void addErrorToDropBox(String eventType,
6989 ProcessRecord process, ActivityRecord activity, ActivityRecord parent, String subject,
6990 final String report, final File logFile,
6991 final ApplicationErrorReport.CrashInfo crashInfo) {
6992 // NOTE -- this must never acquire the ActivityManagerService lock,
6993 // otherwise the watchdog may be prevented from resetting the system.
6994
6995 final String dropboxTag = processClass(process) + "_" + eventType;
6996 final DropBoxManager dbox = (DropBoxManager)
6997 mContext.getSystemService(Context.DROPBOX_SERVICE);
6998
6999 // Exit early if the dropbox isn't configured to accept this report type.
7000 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
7001
7002 final StringBuilder sb = new StringBuilder(1024);
7003 appendDropBoxProcessHeaders(process, sb);
Dan Egnora455d192010-03-12 08:52:28 -08007004 if (activity != null) {
7005 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
7006 }
7007 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
7008 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
7009 }
7010 if (parent != null && parent != activity) {
7011 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
7012 }
7013 if (subject != null) {
7014 sb.append("Subject: ").append(subject).append("\n");
7015 }
7016 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
Christian Lindeberg03d2ca62010-09-28 14:52:20 +02007017 if (Debug.isDebuggerConnected()) {
7018 sb.append("Debugger: Connected\n");
7019 }
Dan Egnora455d192010-03-12 08:52:28 -08007020 sb.append("\n");
7021
7022 // Do the rest in a worker thread to avoid blocking the caller on I/O
7023 // (After this point, we shouldn't access AMS internal data structures.)
7024 Thread worker = new Thread("Error dump: " + dropboxTag) {
7025 @Override
7026 public void run() {
7027 if (report != null) {
7028 sb.append(report);
7029 }
7030 if (logFile != null) {
7031 try {
7032 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
7033 } catch (IOException e) {
7034 Slog.e(TAG, "Error reading " + logFile, e);
7035 }
7036 }
7037 if (crashInfo != null && crashInfo.stackTrace != null) {
7038 sb.append(crashInfo.stackTrace);
7039 }
7040
7041 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
7042 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
7043 if (lines > 0) {
7044 sb.append("\n");
7045
7046 // Merge several logcat streams, and take the last N lines
7047 InputStreamReader input = null;
7048 try {
7049 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
7050 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
7051 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
7052
7053 try { logcat.getOutputStream().close(); } catch (IOException e) {}
7054 try { logcat.getErrorStream().close(); } catch (IOException e) {}
7055 input = new InputStreamReader(logcat.getInputStream());
7056
7057 int num;
7058 char[] buf = new char[8192];
7059 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
7060 } catch (IOException e) {
7061 Slog.e(TAG, "Error running logcat", e);
7062 } finally {
7063 if (input != null) try { input.close(); } catch (IOException e) {}
7064 }
7065 }
7066
7067 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08007068 }
Dan Egnora455d192010-03-12 08:52:28 -08007069 };
7070
7071 if (process == null || process.pid == MY_PID) {
7072 worker.run(); // We may be about to die -- need to run this synchronously
7073 } else {
7074 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08007075 }
7076 }
7077
7078 /**
7079 * Bring up the "unexpected error" dialog box for a crashing app.
7080 * Deal with edge cases (intercepts from instrumented applications,
7081 * ActivityController, error intent receivers, that sort of thing).
7082 * @param r the application crashing
7083 * @param crashInfo describing the failure
7084 */
7085 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08007086 long timeMillis = System.currentTimeMillis();
7087 String shortMsg = crashInfo.exceptionClassName;
7088 String longMsg = crashInfo.exceptionMessage;
7089 String stackTrace = crashInfo.stackTrace;
7090 if (shortMsg != null && longMsg != null) {
7091 longMsg = shortMsg + ": " + longMsg;
7092 } else if (shortMsg != null) {
7093 longMsg = shortMsg;
7094 }
7095
Dan Egnor60d87622009-12-16 16:32:58 -08007096 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007098 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007099 try {
7100 String name = r != null ? r.processName : null;
7101 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08007102 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08007103 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007104 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 + " at watcher's request");
7106 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08007107 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 }
7109 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07007110 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 }
7112 }
7113
7114 final long origId = Binder.clearCallingIdentity();
7115
7116 // If this process is running instrumentation, finish it.
7117 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007118 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007119 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08007120 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
7121 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007122 Bundle info = new Bundle();
7123 info.putString("shortMsg", shortMsg);
7124 info.putString("longMsg", longMsg);
7125 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
7126 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08007127 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007128 }
7129
Dan Egnor60d87622009-12-16 16:32:58 -08007130 // If we can't identify the process or it's already exceeded its crash quota,
7131 // quit right away without showing a crash dialog.
7132 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08007134 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 }
7136
7137 Message msg = Message.obtain();
7138 msg.what = SHOW_ERROR_MSG;
7139 HashMap data = new HashMap();
7140 data.put("result", result);
7141 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007142 msg.obj = data;
7143 mHandler.sendMessage(msg);
7144
7145 Binder.restoreCallingIdentity(origId);
7146 }
7147
7148 int res = result.get();
7149
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007150 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007151 synchronized (this) {
7152 if (r != null) {
7153 mProcessCrashTimes.put(r.info.processName, r.info.uid,
7154 SystemClock.uptimeMillis());
7155 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007156 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08007157 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007158 }
7159 }
7160
7161 if (appErrorIntent != null) {
7162 try {
7163 mContext.startActivity(appErrorIntent);
7164 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007165 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007168 }
Dan Egnorb7f03672009-12-09 16:22:32 -08007169
7170 Intent createAppErrorIntentLocked(ProcessRecord r,
7171 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
7172 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007173 if (report == null) {
7174 return null;
7175 }
7176 Intent result = new Intent(Intent.ACTION_APP_ERROR);
7177 result.setComponent(r.errorReportReceiver);
7178 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
7179 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
7180 return result;
7181 }
7182
Dan Egnorb7f03672009-12-09 16:22:32 -08007183 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
7184 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007185 if (r.errorReportReceiver == null) {
7186 return null;
7187 }
7188
7189 if (!r.crashing && !r.notResponding) {
7190 return null;
7191 }
7192
Dan Egnorb7f03672009-12-09 16:22:32 -08007193 ApplicationErrorReport report = new ApplicationErrorReport();
7194 report.packageName = r.info.packageName;
7195 report.installerPackageName = r.errorReportReceiver.getPackageName();
7196 report.processName = r.processName;
7197 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01007198 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007199
Dan Egnorb7f03672009-12-09 16:22:32 -08007200 if (r.crashing) {
7201 report.type = ApplicationErrorReport.TYPE_CRASH;
7202 report.crashInfo = crashInfo;
7203 } else if (r.notResponding) {
7204 report.type = ApplicationErrorReport.TYPE_ANR;
7205 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007206
Dan Egnorb7f03672009-12-09 16:22:32 -08007207 report.anrInfo.activity = r.notRespondingReport.tag;
7208 report.anrInfo.cause = r.notRespondingReport.shortMsg;
7209 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007210 }
7211
Dan Egnorb7f03672009-12-09 16:22:32 -08007212 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02007213 }
7214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007215 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
7216 // assume our apps are happy - lazy create the list
7217 List<ActivityManager.ProcessErrorStateInfo> errList = null;
7218
7219 synchronized (this) {
7220
7221 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007222 for (int i=mLruProcesses.size()-1; i>=0; i--) {
7223 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007224 if ((app.thread != null) && (app.crashing || app.notResponding)) {
7225 // This one's in trouble, so we'll generate a report for it
7226 // crashes are higher priority (in case there's a crash *and* an anr)
7227 ActivityManager.ProcessErrorStateInfo report = null;
7228 if (app.crashing) {
7229 report = app.crashingReport;
7230 } else if (app.notResponding) {
7231 report = app.notRespondingReport;
7232 }
7233
7234 if (report != null) {
7235 if (errList == null) {
7236 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
7237 }
7238 errList.add(report);
7239 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007240 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 " crashing = " + app.crashing +
7242 " notResponding = " + app.notResponding);
7243 }
7244 }
7245 }
7246 }
7247
7248 return errList;
7249 }
7250
7251 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
7252 // Lazy instantiation of list
7253 List<ActivityManager.RunningAppProcessInfo> runList = null;
7254 synchronized (this) {
7255 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007256 for (int i=mLruProcesses.size()-1; i>=0; i--) {
7257 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007258 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
7259 // Generate process state info for running application
7260 ActivityManager.RunningAppProcessInfo currApp =
7261 new ActivityManager.RunningAppProcessInfo(app.processName,
7262 app.pid, app.getPackageList());
Dianne Hackborneb034652009-09-07 00:49:58 -07007263 currApp.uid = app.info.uid;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07007264 if (mHeavyWeightProcess == app) {
Dianne Hackborn482566e2010-09-03 12:51:28 -07007265 currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_CANT_SAVE_STATE;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07007266 }
Dianne Hackborn42499172010-10-15 18:45:07 -07007267 if (app.persistent) {
7268 currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_PERSISTENT;
7269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 int adj = app.curAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007271 if (adj >= EMPTY_APP_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY;
7273 } else if (adj >= HIDDEN_APP_MIN_ADJ) {
7274 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
The Android Open Source Project4df24232009-03-05 14:34:35 -08007275 currApp.lru = adj - HIDDEN_APP_MIN_ADJ + 1;
7276 } else if (adj >= HOME_APP_ADJ) {
7277 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
7278 currApp.lru = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007279 } else if (adj >= SECONDARY_SERVER_ADJ) {
7280 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007281 } else if (adj >= HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackborn5383f502010-10-22 12:59:20 -07007282 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007283 } else if (adj >= PERCEPTIBLE_APP_ADJ) {
7284 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 } else if (adj >= VISIBLE_APP_ADJ) {
7286 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
7287 } else {
7288 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
7289 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07007290 currApp.importanceReasonCode = app.adjTypeCode;
7291 if (app.adjSource instanceof ProcessRecord) {
7292 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007293 } else if (app.adjSource instanceof ActivityRecord) {
7294 ActivityRecord r = (ActivityRecord)app.adjSource;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07007295 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
7296 }
7297 if (app.adjTarget instanceof ComponentName) {
7298 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
7299 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08007300 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007301 // + " lru=" + currApp.lru);
7302 if (runList == null) {
7303 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
7304 }
7305 runList.add(currApp);
7306 }
7307 }
7308 }
7309 return runList;
7310 }
7311
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07007312 public List<ApplicationInfo> getRunningExternalApplications() {
7313 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
7314 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
7315 if (runningApps != null && runningApps.size() > 0) {
7316 Set<String> extList = new HashSet<String>();
7317 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
7318 if (app.pkgList != null) {
7319 for (String pkg : app.pkgList) {
7320 extList.add(pkg);
7321 }
7322 }
7323 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007324 IPackageManager pm = AppGlobals.getPackageManager();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07007325 for (String pkg : extList) {
7326 try {
7327 ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
7328 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
7329 retList.add(info);
7330 }
7331 } catch (RemoteException e) {
7332 }
7333 }
7334 }
7335 return retList;
7336 }
7337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007338 @Override
7339 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007340 if (checkCallingPermission(android.Manifest.permission.DUMP)
7341 != PackageManager.PERMISSION_GRANTED) {
7342 pw.println("Permission Denial: can't dump ActivityManager from from pid="
7343 + Binder.getCallingPid()
7344 + ", uid=" + Binder.getCallingUid()
7345 + " without permission "
7346 + android.Manifest.permission.DUMP);
7347 return;
7348 }
7349
7350 boolean dumpAll = false;
7351
7352 int opti = 0;
7353 while (opti < args.length) {
7354 String opt = args[opti];
7355 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
7356 break;
7357 }
7358 opti++;
7359 if ("-a".equals(opt)) {
7360 dumpAll = true;
7361 } else if ("-h".equals(opt)) {
7362 pw.println("Activity manager dump options:");
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07007363 pw.println(" [-a] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007364 pw.println(" cmd may be one of:");
Dianne Hackborn287952c2010-09-22 22:34:31 -07007365 pw.println(" a[ctivities]: activity stack state");
7366 pw.println(" b[roadcasts]: broadcast state");
7367 pw.println(" i[ntents]: pending intent state");
7368 pw.println(" p[rocesses]: process state");
7369 pw.println(" o[om]: out of memory management");
7370 pw.println(" prov[iders]: content provider state");
7371 pw.println(" s[ervices]: service state");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007372 pw.println(" service [name]: service client-side state");
Dianne Hackborn30d71892010-12-11 10:37:55 -08007373 pw.println(" cmd may also be a component name (com.foo/.myApp),");
7374 pw.println(" a partial substring in a component name, or an");
7375 pw.println(" ActivityRecord hex object identifier.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007377 } else {
7378 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007379 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007380 }
7381
7382 // Is the caller requesting to dump a particular piece of data?
7383 if (opti < args.length) {
7384 String cmd = args[opti];
7385 opti++;
7386 if ("activities".equals(cmd) || "a".equals(cmd)) {
7387 synchronized (this) {
7388 dumpActivitiesLocked(fd, pw, args, opti, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007390 return;
7391 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
7392 synchronized (this) {
7393 dumpBroadcastsLocked(fd, pw, args, opti, true);
7394 }
7395 return;
7396 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
7397 synchronized (this) {
7398 dumpPendingIntentsLocked(fd, pw, args, opti, true);
7399 }
7400 return;
7401 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
7402 synchronized (this) {
7403 dumpProcessesLocked(fd, pw, args, opti, true);
7404 }
7405 return;
Dianne Hackborn287952c2010-09-22 22:34:31 -07007406 } else if ("oom".equals(cmd) || "o".equals(cmd)) {
7407 synchronized (this) {
7408 dumpOomLocked(fd, pw, args, opti, true);
7409 }
7410 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007411 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
7412 synchronized (this) {
7413 dumpProvidersLocked(fd, pw, args, opti, true);
7414 }
7415 return;
7416 } else if ("service".equals(cmd)) {
Joe Onoratoeb95b082010-10-21 14:54:19 -04007417 dumpService(fd, pw, args, opti);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007418 return;
7419 } else if ("services".equals(cmd) || "s".equals(cmd)) {
7420 synchronized (this) {
7421 dumpServicesLocked(fd, pw, args, opti, true);
7422 }
7423 return;
Dianne Hackborn625ac272010-09-17 18:29:22 -07007424 } else {
7425 // Dumping a single activity?
7426 if (dumpActivity(fd, pw, cmd, args, opti, dumpAll)) {
7427 return;
7428 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08007429 pw.println("Bad activity command, or no activities match: " + cmd);
7430 pw.println("Use -h for help.");
7431 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007432 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007433 }
7434
7435 // No piece of data specified, dump everything.
7436 synchronized (this) {
7437 boolean needSep;
7438 if (dumpAll) {
7439 pw.println("Providers in Current Activity Manager State:");
7440 }
7441 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll);
7442 if (needSep) {
7443 pw.println(" ");
7444 }
7445 if (dumpAll) {
7446 pw.println("-------------------------------------------------------------------------------");
7447 pw.println("Broadcasts in Current Activity Manager State:");
7448 }
7449 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll);
7450 if (needSep) {
7451 pw.println(" ");
7452 }
7453 if (dumpAll) {
7454 pw.println("-------------------------------------------------------------------------------");
7455 pw.println("Services in Current Activity Manager State:");
7456 }
7457 needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll);
7458 if (needSep) {
7459 pw.println(" ");
7460 }
7461 if (dumpAll) {
7462 pw.println("-------------------------------------------------------------------------------");
7463 pw.println("PendingIntents in Current Activity Manager State:");
7464 }
7465 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll);
7466 if (needSep) {
7467 pw.println(" ");
7468 }
7469 if (dumpAll) {
7470 pw.println("-------------------------------------------------------------------------------");
7471 pw.println("Activities in Current Activity Manager State:");
7472 }
7473 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, !dumpAll);
7474 if (needSep) {
7475 pw.println(" ");
7476 }
7477 if (dumpAll) {
7478 pw.println("-------------------------------------------------------------------------------");
7479 pw.println("Processes in Current Activity Manager State:");
7480 }
7481 dumpProcessesLocked(fd, pw, args, opti, dumpAll);
7482 }
7483 }
7484
7485 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7486 int opti, boolean dumpAll, boolean needHeader) {
7487 if (needHeader) {
7488 pw.println(" Activity stack:");
7489 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007490 dumpHistoryList(pw, mMainStack.mHistory, " ", "Hist", true);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007491 pw.println(" ");
7492 pw.println(" Running activities (most recent first):");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007493 dumpHistoryList(pw, mMainStack.mLRUActivities, " ", "Run", false);
7494 if (mMainStack.mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007496 pw.println(" Activities waiting for another to become visible:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007497 dumpHistoryList(pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007498 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007499 if (mMainStack.mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007500 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007501 pw.println(" Activities waiting to stop:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007502 dumpHistoryList(pw, mMainStack.mStoppingActivities, " ", "Stop", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007503 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007504 if (mMainStack.mFinishingActivities.size() > 0) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007505 pw.println(" ");
7506 pw.println(" Activities waiting to finish:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007507 dumpHistoryList(pw, mMainStack.mFinishingActivities, " ", "Fin", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007509
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007510 pw.println(" ");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007511 pw.println(" mPausingActivity: " + mMainStack.mPausingActivity);
7512 pw.println(" mResumedActivity: " + mMainStack.mResumedActivity);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007513 pw.println(" mFocusedActivity: " + mFocusedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007514 pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007516 if (dumpAll && mRecentTasks.size() > 0) {
7517 pw.println(" ");
7518 pw.println("Recent tasks in Current Activity Manager State:");
7519
7520 final int N = mRecentTasks.size();
7521 for (int i=0; i<N; i++) {
7522 TaskRecord tr = mRecentTasks.get(i);
7523 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
7524 pw.println(tr);
7525 mRecentTasks.get(i).dump(pw, " ");
7526 }
7527 }
7528
7529 pw.println(" ");
7530 pw.println(" mCurTask: " + mCurTask);
7531
7532 return true;
7533 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07007534
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007535 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7536 int opti, boolean dumpAll) {
7537 boolean needSep = false;
7538 int numPers = 0;
7539
7540 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007541 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
7542 final int NA = procs.size();
7543 for (int ia=0; ia<NA; ia++) {
7544 if (!needSep) {
7545 pw.println(" All known processes:");
7546 needSep = true;
7547 }
7548 ProcessRecord r = procs.valueAt(ia);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007549 pw.print(r.persistent ? " *PERS*" : " *APP*");
7550 pw.print(" UID "); pw.print(procs.keyAt(ia));
7551 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007552 r.dump(pw, " ");
7553 if (r.persistent) {
7554 numPers++;
7555 }
7556 }
7557 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007558 }
7559
7560 if (mLruProcesses.size() > 0) {
7561 if (needSep) pw.println(" ");
7562 needSep = true;
7563 pw.println(" Running processes (most recent first):");
Dianne Hackborn287952c2010-09-22 22:34:31 -07007564 dumpProcessOomList(pw, this, mLruProcesses, " ",
7565 "Proc", "PERS", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007566 needSep = true;
7567 }
7568
7569 synchronized (mPidsSelfLocked) {
7570 if (mPidsSelfLocked.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007571 if (needSep) pw.println(" ");
7572 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007573 pw.println(" PID mappings:");
7574 for (int i=0; i<mPidsSelfLocked.size(); i++) {
7575 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
7576 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
7578 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007579 }
7580
7581 if (mForegroundProcesses.size() > 0) {
7582 if (needSep) pw.println(" ");
7583 needSep = true;
7584 pw.println(" Foreground Processes:");
7585 for (int i=0; i<mForegroundProcesses.size(); i++) {
7586 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
7587 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007589 }
7590
7591 if (mPersistentStartingProcesses.size() > 0) {
7592 if (needSep) pw.println(" ");
7593 needSep = true;
7594 pw.println(" Persisent processes that are starting:");
7595 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
Dianne Hackborn287952c2010-09-22 22:34:31 -07007596 "Starting Norm", "Restarting PERS");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007598
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007599 if (mStartingProcesses.size() > 0) {
7600 if (needSep) pw.println(" ");
7601 needSep = true;
7602 pw.println(" Processes that are starting:");
7603 dumpProcessList(pw, this, mStartingProcesses, " ",
Dianne Hackborn287952c2010-09-22 22:34:31 -07007604 "Starting Norm", "Starting PERS");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007606
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007607 if (mRemovedProcesses.size() > 0) {
7608 if (needSep) pw.println(" ");
7609 needSep = true;
7610 pw.println(" Processes that are being removed:");
7611 dumpProcessList(pw, this, mRemovedProcesses, " ",
Dianne Hackborn287952c2010-09-22 22:34:31 -07007612 "Removed Norm", "Removed PERS");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007613 }
7614
7615 if (mProcessesOnHold.size() > 0) {
7616 if (needSep) pw.println(" ");
7617 needSep = true;
7618 pw.println(" Processes that are on old until the system is ready:");
7619 dumpProcessList(pw, this, mProcessesOnHold, " ",
Dianne Hackborn287952c2010-09-22 22:34:31 -07007620 "OnHold Norm", "OnHold PERS");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007622
Dianne Hackborn287952c2010-09-22 22:34:31 -07007623 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007624
7625 if (mProcessCrashTimes.getMap().size() > 0) {
7626 if (needSep) pw.println(" ");
7627 needSep = true;
7628 pw.println(" Time since processes crashed:");
7629 long now = SystemClock.uptimeMillis();
7630 for (Map.Entry<String, SparseArray<Long>> procs
7631 : mProcessCrashTimes.getMap().entrySet()) {
7632 SparseArray<Long> uids = procs.getValue();
7633 final int N = uids.size();
7634 for (int i=0; i<N; i++) {
7635 pw.print(" Process "); pw.print(procs.getKey());
7636 pw.print(" uid "); pw.print(uids.keyAt(i));
7637 pw.print(": last crashed ");
7638 pw.print((now-uids.valueAt(i)));
Dianne Hackbornfd12af42009-08-27 00:44:33 -07007639 pw.println(" ms ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07007640 }
7641 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007642 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007643
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007644 if (mBadProcesses.getMap().size() > 0) {
7645 if (needSep) pw.println(" ");
7646 needSep = true;
7647 pw.println(" Bad processes:");
7648 for (Map.Entry<String, SparseArray<Long>> procs
7649 : mBadProcesses.getMap().entrySet()) {
7650 SparseArray<Long> uids = procs.getValue();
7651 final int N = uids.size();
7652 for (int i=0; i<N; i++) {
7653 pw.print(" Bad process "); pw.print(procs.getKey());
7654 pw.print(" uid "); pw.print(uids.keyAt(i));
7655 pw.print(": crashed at time ");
7656 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007657 }
7658 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007660
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007661 pw.println(" ");
7662 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackborn860755f2010-06-03 18:47:52 -07007663 if (mHeavyWeightProcess != null) {
7664 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
7665 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007666 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007667 pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007668 pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown);
7669 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
7670 || mOrigWaitForDebugger) {
7671 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
7672 + " mDebugTransient=" + mDebugTransient
7673 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
7674 }
7675 if (mAlwaysFinishActivities || mController != null) {
7676 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
7677 + " mController=" + mController);
7678 }
7679 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007680 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007681 pw.println(" mStartRunning=" + mStartRunning
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07007682 + " mProcessesReady=" + mProcessesReady
7683 + " mSystemReady=" + mSystemReady);
7684 pw.println(" mBooting=" + mBooting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007685 + " mBooted=" + mBooted
7686 + " mFactoryTest=" + mFactoryTest);
Dianne Hackborn287952c2010-09-22 22:34:31 -07007687 pw.print(" mLastPowerCheckRealtime=");
7688 TimeUtils.formatDuration(mLastPowerCheckRealtime, pw);
7689 pw.println("");
7690 pw.print(" mLastPowerCheckUptime=");
7691 TimeUtils.formatDuration(mLastPowerCheckUptime, pw);
7692 pw.println("");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007693 pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep);
7694 pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity);
Dianne Hackborn906497c2010-05-10 15:57:38 -07007695 pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007697
7698 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007699 }
7700
Dianne Hackborn287952c2010-09-22 22:34:31 -07007701 boolean dumpProcessesToGc(FileDescriptor fd, PrintWriter pw, String[] args,
7702 int opti, boolean needSep, boolean dumpAll) {
7703 if (mProcessesToGc.size() > 0) {
7704 if (needSep) pw.println(" ");
7705 needSep = true;
7706 pw.println(" Processes that are waiting to GC:");
7707 long now = SystemClock.uptimeMillis();
7708 for (int i=0; i<mProcessesToGc.size(); i++) {
7709 ProcessRecord proc = mProcessesToGc.get(i);
7710 pw.print(" Process "); pw.println(proc);
7711 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
7712 pw.print(", last gced=");
7713 pw.print(now-proc.lastRequestedGc);
7714 pw.print(" ms ago, last lowMem=");
7715 pw.print(now-proc.lastLowMemory);
7716 pw.println(" ms ago");
7717
7718 }
7719 }
7720 return needSep;
7721 }
7722
7723 boolean dumpOomLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7724 int opti, boolean dumpAll) {
7725 boolean needSep = false;
7726
7727 if (mLruProcesses.size() > 0) {
7728 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>(mLruProcesses);
7729
7730 Comparator<ProcessRecord> comparator = new Comparator<ProcessRecord>() {
7731 @Override
7732 public int compare(ProcessRecord object1, ProcessRecord object2) {
7733 if (object1.setAdj != object2.setAdj) {
7734 return object1.setAdj > object2.setAdj ? -1 : 1;
7735 }
7736 if (object1.setSchedGroup != object2.setSchedGroup) {
7737 return object1.setSchedGroup > object2.setSchedGroup ? -1 : 1;
7738 }
7739 if (object1.keeping != object2.keeping) {
7740 return object1.keeping ? -1 : 1;
7741 }
7742 if (object1.pid != object2.pid) {
7743 return object1.pid > object2.pid ? -1 : 1;
7744 }
7745 return 0;
7746 }
7747 };
7748
7749 Collections.sort(procs, comparator);
7750
7751 if (needSep) pw.println(" ");
7752 needSep = true;
7753 pw.println(" Process OOM control:");
7754 dumpProcessOomList(pw, this, procs, " ",
7755 "Proc", "PERS", true);
7756 needSep = true;
7757 }
7758
7759 needSep = dumpProcessesToGc(fd, pw, args, opti, needSep, dumpAll);
7760
7761 pw.println(" ");
7762 pw.println(" mHomeProcess: " + mHomeProcess);
7763 if (mHeavyWeightProcess != null) {
7764 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
7765 }
7766
7767 return true;
7768 }
7769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007770 /**
7771 * There are three ways to call this:
7772 * - no service specified: dump all the services
7773 * - a flattened component name that matched an existing service was specified as the
7774 * first arg: dump that one service
7775 * - the first arg isn't the flattened component name of an existing service:
7776 * dump all services whose component contains the first arg as a substring
7777 */
Joe Onoratoeb95b082010-10-21 14:54:19 -04007778 protected void dumpService(FileDescriptor fd, PrintWriter pw, String[] args, int opti) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 String[] newArgs;
7780 String componentNameString;
7781 ServiceRecord r;
Kenny Root3619b9ab2010-02-13 10:05:42 -08007782 if (opti >= args.length) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 componentNameString = null;
7784 newArgs = EMPTY_STRING_ARRAY;
7785 r = null;
7786 } else {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007787 componentNameString = args[opti];
7788 opti++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007789 ComponentName componentName = ComponentName.unflattenFromString(componentNameString);
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007790 synchronized (this) {
7791 r = componentName != null ? mServices.get(componentName) : null;
7792 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007793 newArgs = new String[args.length - opti];
7794 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007795 }
7796
7797 if (r != null) {
Joe Onoratoeb95b082010-10-21 14:54:19 -04007798 dumpService(fd, pw, r, newArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 } else {
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007800 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
7801 synchronized (this) {
7802 for (ServiceRecord r1 : mServices.values()) {
7803 if (componentNameString == null
7804 || r1.name.flattenToString().contains(componentNameString)) {
7805 services.add(r1);
7806 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007807 }
7808 }
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007809 for (int i=0; i<services.size(); i++) {
Joe Onoratoeb95b082010-10-21 14:54:19 -04007810 dumpService(fd, pw, services.get(i), newArgs);
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007812 }
7813 }
7814
7815 /**
7816 * Invokes IApplicationThread.dumpService() on the thread of the specified service if
7817 * there is a thread associated with the service.
7818 */
Joe Onoratoeb95b082010-10-21 14:54:19 -04007819 private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args) {
7820 pw.println("------------------------------------------------------------"
7821 + "-------------------");
7822 pw.println("APP SERVICE: " + r.name.flattenToString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007823 if (r.app != null && r.app.thread != null) {
7824 try {
7825 // flush anything that is already in the PrintWriter since the thread is going
7826 // to write to the file descriptor directly
7827 pw.flush();
7828 r.app.thread.dumpService(fd, r, args);
7829 pw.print("\n");
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007830 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 } catch (RemoteException e) {
7832 pw.println("got a RemoteException while dumping the service");
7833 }
7834 }
7835 }
7836
Dianne Hackborn625ac272010-09-17 18:29:22 -07007837 /**
7838 * There are three things that cmd can be:
7839 * - a flattened component name that matched an existing activity
7840 * - the cmd arg isn't the flattened component name of an existing activity:
7841 * dump all activity whose component contains the cmd as a substring
7842 * - A hex number of the ActivityRecord object instance.
7843 */
7844 protected boolean dumpActivity(FileDescriptor fd, PrintWriter pw, String name, String[] args,
7845 int opti, boolean dumpAll) {
7846 String[] newArgs;
7847 ComponentName componentName = ComponentName.unflattenFromString(name);
7848 int objectId = 0;
Dianne Hackborn30d71892010-12-11 10:37:55 -08007849 if (componentName == null) {
7850 // Not a '/' separated full component name; maybe an object ID?
7851 try {
7852 objectId = Integer.parseInt(name, 16);
7853 name = null;
7854 componentName = null;
7855 } catch (RuntimeException e) {
7856 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007857 }
7858 newArgs = new String[args.length - opti];
7859 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
7860
7861 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>();
7862 synchronized (this) {
7863 for (ActivityRecord r1 : (ArrayList<ActivityRecord>)mMainStack.mHistory) {
7864 if (componentName != null) {
7865 if (r1.intent.getComponent().equals(componentName)) {
7866 activities.add(r1);
7867 }
7868 } else if (name != null) {
7869 if (r1.intent.getComponent().flattenToString().contains(name)) {
7870 activities.add(r1);
7871 }
Dianne Hackborn30d71892010-12-11 10:37:55 -08007872 } else if (System.identityHashCode(r1) == objectId) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07007873 activities.add(r1);
7874 }
7875 }
7876 }
7877
7878 if (activities.size() <= 0) {
7879 return false;
7880 }
7881
Dianne Hackborn30d71892010-12-11 10:37:55 -08007882 TaskRecord lastTask = null;
7883 for (int i=activities.size()-1; i>=0; i--) {
7884 ActivityRecord r = (ActivityRecord)activities.get(i);
7885 if (lastTask != r.task) {
7886 lastTask = r.task;
7887 pw.print("* Task "); pw.print(lastTask.affinity);
7888 pw.print(" id="); pw.println(lastTask.taskId);
7889 if (dumpAll) {
7890 lastTask.dump(pw, " ");
7891 }
7892 }
7893 dumpActivity(" ", fd, pw, activities.get(i), newArgs, dumpAll);
Dianne Hackborn625ac272010-09-17 18:29:22 -07007894 }
7895 return true;
7896 }
7897
7898 /**
7899 * Invokes IApplicationThread.dumpActivity() on the thread of the specified activity if
7900 * there is a thread associated with the activity.
7901 */
Dianne Hackborn30d71892010-12-11 10:37:55 -08007902 private void dumpActivity(String prefix, FileDescriptor fd, PrintWriter pw,
7903 ActivityRecord r, String[] args, boolean dumpAll) {
7904 synchronized (this) {
7905 pw.print(prefix); pw.print("* Activity ");
7906 pw.print(Integer.toHexString(System.identityHashCode(r)));
7907 pw.print(" "); pw.print(r.shortComponentName); pw.print(" pid=");
7908 if (r.app != null) pw.println(r.app.pid);
7909 else pw.println("(not running)");
7910 if (dumpAll) {
7911 r.dump(pw, prefix + " ");
Dianne Hackborn625ac272010-09-17 18:29:22 -07007912 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07007913 }
7914 if (r.app != null && r.app.thread != null) {
7915 try {
7916 // flush anything that is already in the PrintWriter since the thread is going
7917 // to write to the file descriptor directly
7918 pw.flush();
Dianne Hackborn30d71892010-12-11 10:37:55 -08007919 r.app.thread.dumpActivity(fd, r, prefix + " ", args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07007920 pw.flush();
7921 } catch (RemoteException e) {
7922 pw.println("got a RemoteException while dumping the activity");
7923 }
7924 }
7925 }
7926
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007927 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7928 int opti, boolean dumpAll) {
7929 boolean needSep = false;
7930
7931 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 if (mRegisteredReceivers.size() > 0) {
7933 pw.println(" ");
7934 pw.println(" Registered Receivers:");
7935 Iterator it = mRegisteredReceivers.values().iterator();
7936 while (it.hasNext()) {
7937 ReceiverList r = (ReceiverList)it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007938 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007939 r.dump(pw, " ");
7940 }
7941 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007943 pw.println(" ");
7944 pw.println("Receiver Resolver Table:");
Dianne Hackborncef65ee2010-09-30 18:27:22 -07007945 mReceiverResolver.dump(pw, null, " ", null, false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007946 needSep = true;
7947 }
7948
7949 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
7950 || mPendingBroadcast != null) {
7951 if (mParallelBroadcasts.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007953 pw.println(" Active broadcasts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007954 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007955 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
7956 pw.println(" Broadcast #" + i + ":");
7957 mParallelBroadcasts.get(i).dump(pw, " ");
7958 }
7959 if (mOrderedBroadcasts.size() > 0) {
7960 pw.println(" ");
Dianne Hackborn399cccb2010-04-13 22:57:49 -07007961 pw.println(" Active ordered broadcasts:");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007962 }
7963 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
7964 pw.println(" Serialized Broadcast #" + i + ":");
7965 mOrderedBroadcasts.get(i).dump(pw, " ");
7966 }
7967 pw.println(" ");
7968 pw.println(" Pending broadcast:");
7969 if (mPendingBroadcast != null) {
7970 mPendingBroadcast.dump(pw, " ");
7971 } else {
7972 pw.println(" (null)");
7973 }
7974 needSep = true;
7975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007977 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 pw.println(" ");
Dianne Hackborn12527f92009-11-11 17:39:50 -08007979 pw.println(" Historical broadcasts:");
7980 for (int i=0; i<MAX_BROADCAST_HISTORY; i++) {
7981 BroadcastRecord r = mBroadcastHistory[i];
7982 if (r == null) {
7983 break;
7984 }
7985 pw.println(" Historical Broadcast #" + i + ":");
7986 r.dump(pw, " ");
7987 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007988 needSep = true;
7989 }
7990
7991 if (mStickyBroadcasts != null) {
Dianne Hackborn12527f92009-11-11 17:39:50 -08007992 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007993 pw.println(" Sticky broadcasts:");
7994 StringBuilder sb = new StringBuilder(128);
7995 for (Map.Entry<String, ArrayList<Intent>> ent
7996 : mStickyBroadcasts.entrySet()) {
7997 pw.print(" * Sticky action "); pw.print(ent.getKey());
7998 pw.println(":");
7999 ArrayList<Intent> intents = ent.getValue();
8000 final int N = intents.size();
8001 for (int i=0; i<N; i++) {
8002 sb.setLength(0);
8003 sb.append(" Intent: ");
8004 intents.get(i).toShortString(sb, true, false);
8005 pw.println(sb.toString());
8006 Bundle bundle = intents.get(i).getExtras();
8007 if (bundle != null) {
8008 pw.print(" ");
8009 pw.println(bundle.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 }
8011 }
8012 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008013 needSep = true;
8014 }
8015
8016 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008018 pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 pw.println(" mHandler:");
8020 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008021 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008023
8024 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008025 }
8026
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008027 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
8028 int opti, boolean dumpAll) {
8029 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008030
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008031 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008032 if (mServices.size() > 0) {
8033 pw.println(" Active services:");
8034 Iterator<ServiceRecord> it = mServices.values().iterator();
8035 while (it.hasNext()) {
8036 ServiceRecord r = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008037 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008038 r.dump(pw, " ");
8039 }
8040 needSep = true;
8041 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008044 if (mPendingServices.size() > 0) {
8045 if (needSep) pw.println(" ");
8046 pw.println(" Pending services:");
8047 for (int i=0; i<mPendingServices.size(); i++) {
8048 ServiceRecord r = mPendingServices.get(i);
8049 pw.print(" * Pending "); pw.println(r);
8050 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008051 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008052 needSep = true;
8053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008055 if (mRestartingServices.size() > 0) {
8056 if (needSep) pw.println(" ");
8057 pw.println(" Restarting services:");
8058 for (int i=0; i<mRestartingServices.size(); i++) {
8059 ServiceRecord r = mRestartingServices.get(i);
8060 pw.print(" * Restarting "); pw.println(r);
8061 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008062 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008063 needSep = true;
8064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008066 if (mStoppingServices.size() > 0) {
8067 if (needSep) pw.println(" ");
8068 pw.println(" Stopping services:");
8069 for (int i=0; i<mStoppingServices.size(); i++) {
8070 ServiceRecord r = mStoppingServices.get(i);
8071 pw.print(" * Stopping "); pw.println(r);
8072 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008073 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008074 needSep = true;
8075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008076
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008077 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 if (mServiceConnections.size() > 0) {
8079 if (needSep) pw.println(" ");
8080 pw.println(" Connection bindings to services:");
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008081 Iterator<ArrayList<ConnectionRecord>> it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008082 = mServiceConnections.values().iterator();
8083 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008084 ArrayList<ConnectionRecord> r = it.next();
8085 for (int i=0; i<r.size(); i++) {
8086 pw.print(" * "); pw.println(r.get(i));
8087 r.get(i).dump(pw, " ");
8088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008089 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008090 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008091 }
8092 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008093
8094 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008095 }
8096
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008097 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
8098 int opti, boolean dumpAll) {
8099 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008101 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008102 if (mProvidersByClass.size() > 0) {
8103 if (needSep) pw.println(" ");
8104 pw.println(" Published content providers (by class):");
Dianne Hackborn860755f2010-06-03 18:47:52 -07008105 Iterator<Map.Entry<String, ContentProviderRecord>> it
8106 = mProvidersByClass.entrySet().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008107 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008108 Map.Entry<String, ContentProviderRecord> e = it.next();
8109 ContentProviderRecord r = e.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008110 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 r.dump(pw, " ");
8112 }
8113 needSep = true;
8114 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008115
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008116 if (mProvidersByName.size() > 0) {
8117 pw.println(" ");
8118 pw.println(" Authority to provider mappings:");
Dianne Hackborn860755f2010-06-03 18:47:52 -07008119 Iterator<Map.Entry<String, ContentProviderRecord>> it
8120 = mProvidersByName.entrySet().iterator();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008121 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008122 Map.Entry<String, ContentProviderRecord> e = it.next();
8123 ContentProviderRecord r = e.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008124 pw.print(" "); pw.print(e.getKey()); pw.print(": ");
8125 pw.println(r);
8126 }
8127 needSep = true;
8128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008129 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008130
8131 if (mLaunchingProviders.size() > 0) {
8132 if (needSep) pw.println(" ");
8133 pw.println(" Launching content providers:");
8134 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
8135 pw.print(" Launching #"); pw.print(i); pw.print(": ");
8136 pw.println(mLaunchingProviders.get(i));
8137 }
8138 needSep = true;
8139 }
8140
8141 if (mGrantedUriPermissions.size() > 0) {
8142 pw.println();
8143 pw.println("Granted Uri Permissions:");
8144 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
8145 int uid = mGrantedUriPermissions.keyAt(i);
8146 HashMap<Uri, UriPermission> perms
8147 = mGrantedUriPermissions.valueAt(i);
8148 pw.print(" * UID "); pw.print(uid);
8149 pw.println(" holds:");
8150 for (UriPermission perm : perms.values()) {
8151 pw.print(" "); pw.println(perm);
8152 perm.dump(pw, " ");
8153 }
8154 }
8155 needSep = true;
8156 }
8157
8158 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008159 }
8160
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008161 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
8162 int opti, boolean dumpAll) {
8163 boolean needSep = false;
8164
8165 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008166 if (this.mIntentSenderRecords.size() > 0) {
8167 Iterator<WeakReference<PendingIntentRecord>> it
8168 = mIntentSenderRecords.values().iterator();
8169 while (it.hasNext()) {
8170 WeakReference<PendingIntentRecord> ref = it.next();
8171 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008172 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008173 if (rec != null) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008174 pw.print(" * "); pw.println(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008175 rec.dump(pw, " ");
8176 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008177 pw.print(" * "); pw.print(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 }
8179 }
8180 }
8181 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08008182
8183 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008184 }
8185
8186 private static final void dumpHistoryList(PrintWriter pw, List list,
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008187 String prefix, String label, boolean complete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008188 TaskRecord lastTask = null;
8189 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008190 ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008191 final boolean full = complete || !r.inHistory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 if (lastTask != r.task) {
8193 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008194 pw.print(prefix);
8195 pw.print(full ? "* " : " ");
8196 pw.println(lastTask);
8197 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008198 lastTask.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07008201 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
8202 pw.print(" #"); pw.print(i); pw.print(": ");
8203 pw.println(r);
8204 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008205 r.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 }
8208 }
8209
Dianne Hackborn09c916b2009-12-08 14:50:51 -08008210 private static String buildOomTag(String prefix, String space, int val, int base) {
8211 if (val == base) {
8212 if (space == null) return prefix;
8213 return prefix + " ";
8214 }
8215 return prefix + "+" + Integer.toString(val-base);
8216 }
8217
8218 private static final int dumpProcessList(PrintWriter pw,
8219 ActivityManagerService service, List list,
Dianne Hackborn287952c2010-09-22 22:34:31 -07008220 String prefix, String normalLabel, String persistentLabel) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008221 int numPers = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07008222 final int N = list.size()-1;
8223 for (int i=N; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 ProcessRecord r = (ProcessRecord)list.get(i);
Dianne Hackborn287952c2010-09-22 22:34:31 -07008225 pw.println(String.format("%s%s #%2d: %s",
8226 prefix, (r.persistent ? persistentLabel : normalLabel),
8227 i, r.toString()));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 if (r.persistent) {
8229 numPers++;
8230 }
8231 }
8232 return numPers;
8233 }
8234
Dianne Hackborn287952c2010-09-22 22:34:31 -07008235 private static final void dumpProcessOomList(PrintWriter pw,
8236 ActivityManagerService service, List<ProcessRecord> list,
8237 String prefix, String normalLabel, String persistentLabel,
8238 boolean inclDetails) {
8239
8240 final long curRealtime = SystemClock.elapsedRealtime();
8241 final long realtimeSince = curRealtime - service.mLastPowerCheckRealtime;
8242 final long curUptime = SystemClock.uptimeMillis();
8243 final long uptimeSince = curUptime - service.mLastPowerCheckUptime;
8244
8245 final int N = list.size()-1;
8246 for (int i=N; i>=0; i--) {
8247 ProcessRecord r = list.get(i);
8248 String oomAdj;
8249 if (r.setAdj >= EMPTY_APP_ADJ) {
8250 oomAdj = buildOomTag("empty", null, r.setAdj, EMPTY_APP_ADJ);
8251 } else if (r.setAdj >= HIDDEN_APP_MIN_ADJ) {
8252 oomAdj = buildOomTag("bak", " ", r.setAdj, HIDDEN_APP_MIN_ADJ);
8253 } else if (r.setAdj >= HOME_APP_ADJ) {
8254 oomAdj = buildOomTag("home ", null, r.setAdj, HOME_APP_ADJ);
8255 } else if (r.setAdj >= SECONDARY_SERVER_ADJ) {
8256 oomAdj = buildOomTag("svc", " ", r.setAdj, SECONDARY_SERVER_ADJ);
8257 } else if (r.setAdj >= BACKUP_APP_ADJ) {
8258 oomAdj = buildOomTag("bckup", null, r.setAdj, BACKUP_APP_ADJ);
8259 } else if (r.setAdj >= HEAVY_WEIGHT_APP_ADJ) {
8260 oomAdj = buildOomTag("hvy ", null, r.setAdj, HEAVY_WEIGHT_APP_ADJ);
8261 } else if (r.setAdj >= PERCEPTIBLE_APP_ADJ) {
8262 oomAdj = buildOomTag("prcp ", null, r.setAdj, PERCEPTIBLE_APP_ADJ);
8263 } else if (r.setAdj >= VISIBLE_APP_ADJ) {
8264 oomAdj = buildOomTag("vis ", null, r.setAdj, VISIBLE_APP_ADJ);
8265 } else if (r.setAdj >= FOREGROUND_APP_ADJ) {
8266 oomAdj = buildOomTag("fore ", null, r.setAdj, FOREGROUND_APP_ADJ);
8267 } else if (r.setAdj >= CORE_SERVER_ADJ) {
8268 oomAdj = buildOomTag("core ", null, r.setAdj, CORE_SERVER_ADJ);
8269 } else if (r.setAdj >= SYSTEM_ADJ) {
8270 oomAdj = buildOomTag("sys ", null, r.setAdj, SYSTEM_ADJ);
8271 } else {
8272 oomAdj = Integer.toString(r.setAdj);
8273 }
8274 String schedGroup;
8275 switch (r.setSchedGroup) {
8276 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
8277 schedGroup = "B";
8278 break;
8279 case Process.THREAD_GROUP_DEFAULT:
8280 schedGroup = "F";
8281 break;
8282 default:
8283 schedGroup = Integer.toString(r.setSchedGroup);
8284 break;
8285 }
8286 pw.println(String.format("%s%s #%2d: adj=%s/%s %s (%s)",
8287 prefix, (r.persistent ? persistentLabel : normalLabel),
8288 N-i, oomAdj, schedGroup, r.toShortString(), r.adjType));
8289 if (r.adjSource != null || r.adjTarget != null) {
8290 pw.print(prefix);
8291 pw.print(" ");
8292 if (r.adjTarget instanceof ComponentName) {
8293 pw.print(((ComponentName)r.adjTarget).flattenToShortString());
8294 } else if (r.adjTarget != null) {
8295 pw.print(r.adjTarget.toString());
8296 } else {
8297 pw.print("{null}");
8298 }
8299 pw.print("<=");
8300 if (r.adjSource instanceof ProcessRecord) {
8301 pw.print("Proc{");
8302 pw.print(((ProcessRecord)r.adjSource).toShortString());
8303 pw.println("}");
8304 } else if (r.adjSource != null) {
8305 pw.println(r.adjSource.toString());
8306 } else {
8307 pw.println("{null}");
8308 }
8309 }
8310 if (inclDetails) {
8311 pw.print(prefix);
8312 pw.print(" ");
8313 pw.print("oom: max="); pw.print(r.maxAdj);
8314 pw.print(" hidden="); pw.print(r.hiddenAdj);
8315 pw.print(" curRaw="); pw.print(r.curRawAdj);
8316 pw.print(" setRaw="); pw.print(r.setRawAdj);
8317 pw.print(" cur="); pw.print(r.curAdj);
8318 pw.print(" set="); pw.println(r.setAdj);
8319 pw.print(prefix);
8320 pw.print(" ");
8321 pw.print("keeping="); pw.print(r.keeping);
8322 pw.print(" hidden="); pw.print(r.hidden);
8323 pw.print(" empty="); pw.println(r.empty);
8324
8325 if (!r.keeping) {
8326 if (r.lastWakeTime != 0) {
8327 long wtime;
8328 BatteryStatsImpl stats = service.mBatteryStatsService.getActiveStatistics();
8329 synchronized (stats) {
8330 wtime = stats.getProcessWakeTime(r.info.uid,
8331 r.pid, curRealtime);
8332 }
8333 long timeUsed = wtime - r.lastWakeTime;
8334 pw.print(prefix);
8335 pw.print(" ");
8336 pw.print("keep awake over ");
8337 TimeUtils.formatDuration(realtimeSince, pw);
8338 pw.print(" used ");
8339 TimeUtils.formatDuration(timeUsed, pw);
8340 pw.print(" (");
8341 pw.print((timeUsed*100)/realtimeSince);
8342 pw.println("%)");
8343 }
8344 if (r.lastCpuTime != 0) {
8345 long timeUsed = r.curCpuTime - r.lastCpuTime;
8346 pw.print(prefix);
8347 pw.print(" ");
8348 pw.print("run cpu over ");
8349 TimeUtils.formatDuration(uptimeSince, pw);
8350 pw.print(" used ");
8351 TimeUtils.formatDuration(timeUsed, pw);
8352 pw.print(" (");
8353 pw.print((timeUsed*100)/uptimeSince);
8354 pw.println("%)");
8355 }
8356 }
8357 }
8358 }
8359 }
8360
Dianne Hackborn472ad872010-04-07 17:31:48 -07008361 static final void dumpApplicationMemoryUsage(FileDescriptor fd,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 PrintWriter pw, List list, String prefix, String[] args) {
Dianne Hackborn6447ca32009-04-07 19:50:08 -07008363 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008364 long uptime = SystemClock.uptimeMillis();
8365 long realtime = SystemClock.elapsedRealtime();
8366
8367 if (isCheckinRequest) {
8368 // short checkin version
8369 pw.println(uptime + "," + realtime);
8370 pw.flush();
8371 } else {
8372 pw.println("Applications Memory Usage (kB):");
8373 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
8374 }
8375 for (int i = list.size() - 1 ; i >= 0 ; i--) {
8376 ProcessRecord r = (ProcessRecord)list.get(i);
8377 if (r.thread != null) {
8378 if (!isCheckinRequest) {
8379 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
8380 pw.flush();
8381 }
8382 try {
8383 r.thread.asBinder().dump(fd, args);
8384 } catch (RemoteException e) {
8385 if (!isCheckinRequest) {
8386 pw.println("Got RemoteException!");
8387 pw.flush();
8388 }
8389 }
8390 }
8391 }
8392 }
8393
8394 /**
8395 * Searches array of arguments for the specified string
8396 * @param args array of argument strings
8397 * @param value value to search for
8398 * @return true if the value is contained in the array
8399 */
8400 private static boolean scanArgs(String[] args, String value) {
8401 if (args != null) {
8402 for (String arg : args) {
8403 if (value.equals(arg)) {
8404 return true;
8405 }
8406 }
8407 }
8408 return false;
8409 }
8410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 private final void killServicesLocked(ProcessRecord app,
8412 boolean allowRestart) {
8413 // Report disconnected services.
8414 if (false) {
8415 // XXX we are letting the client link to the service for
8416 // death notifications.
8417 if (app.services.size() > 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008418 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008420 ServiceRecord r = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008421 if (r.connections.size() > 0) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008422 Iterator<ArrayList<ConnectionRecord>> jt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 = r.connections.values().iterator();
8424 while (jt.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008425 ArrayList<ConnectionRecord> cl = jt.next();
8426 for (int i=0; i<cl.size(); i++) {
8427 ConnectionRecord c = cl.get(i);
8428 if (c.binding.client != app) {
8429 try {
8430 //c.conn.connected(r.className, null);
8431 } catch (Exception e) {
8432 // todo: this should be asynchronous!
8433 Slog.w(TAG, "Exception thrown disconnected servce "
8434 + r.shortName
8435 + " from app " + app.processName, e);
8436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 }
8438 }
8439 }
8440 }
8441 }
8442 }
8443 }
8444
8445 // Clean up any connections this application has to other services.
8446 if (app.connections.size() > 0) {
8447 Iterator<ConnectionRecord> it = app.connections.iterator();
8448 while (it.hasNext()) {
8449 ConnectionRecord r = it.next();
8450 removeConnectionLocked(r, app, null);
8451 }
8452 }
8453 app.connections.clear();
8454
8455 if (app.services.size() != 0) {
8456 // Any services running in the application need to be placed
8457 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07008458 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008459 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008460 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008461 synchronized (sr.stats.getBatteryStats()) {
8462 sr.stats.stopLaunchedLocked();
8463 }
8464 sr.app = null;
8465 sr.executeNesting = 0;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008466 if (mStoppingServices.remove(sr)) {
8467 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr);
8468 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008469
8470 boolean hasClients = sr.bindings.size() > 0;
8471 if (hasClients) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008472 Iterator<IntentBindRecord> bindings
8473 = sr.bindings.values().iterator();
8474 while (bindings.hasNext()) {
8475 IntentBindRecord b = bindings.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -08008476 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008477 + ": shouldUnbind=" + b.hasBound);
8478 b.binder = null;
8479 b.requested = b.received = b.hasBound = false;
8480 }
8481 }
8482
Dianne Hackborn070783f2010-12-29 16:46:28 -08008483 if (sr.crashCount >= 2 && (sr.serviceInfo.applicationInfo.flags
8484 &ApplicationInfo.FLAG_PERSISTENT) == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008485 Slog.w(TAG, "Service crashed " + sr.crashCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 + " times, stopping: " + sr);
Doug Zongker2bec3d42009-12-04 12:52:44 -08008487 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008488 sr.crashCount, sr.shortName, app.pid);
8489 bringDownServiceLocked(sr, true);
8490 } else if (!allowRestart) {
8491 bringDownServiceLocked(sr, true);
8492 } else {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008493 boolean canceled = scheduleServiceRestartLocked(sr, true);
8494
8495 // Should the service remain running? Note that in the
8496 // extreme case of so many attempts to deliver a command
8497 // that it failed, that we also will stop it here.
8498 if (sr.startRequested && (sr.stopIfKilled || canceled)) {
8499 if (sr.pendingStarts.size() == 0) {
8500 sr.startRequested = false;
8501 if (!hasClients) {
8502 // Whoops, no reason to restart!
8503 bringDownServiceLocked(sr, true);
8504 }
8505 }
8506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008507 }
8508 }
8509
8510 if (!allowRestart) {
8511 app.services.clear();
8512 }
8513 }
8514
Dianne Hackbornde42bb62009-08-05 12:26:15 -07008515 // Make sure we have no more records on the stopping list.
8516 int i = mStoppingServices.size();
8517 while (i > 0) {
8518 i--;
8519 ServiceRecord sr = mStoppingServices.get(i);
8520 if (sr.app == app) {
8521 mStoppingServices.remove(i);
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008522 if (DEBUG_SERVICE) Slog.v(TAG, "killServices remove stopping " + sr);
Dianne Hackbornde42bb62009-08-05 12:26:15 -07008523 }
8524 }
8525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 app.executingServices.clear();
8527 }
8528
8529 private final void removeDyingProviderLocked(ProcessRecord proc,
8530 ContentProviderRecord cpr) {
8531 synchronized (cpr) {
8532 cpr.launchingApp = null;
8533 cpr.notifyAll();
8534 }
8535
8536 mProvidersByClass.remove(cpr.info.name);
8537 String names[] = cpr.info.authority.split(";");
8538 for (int j = 0; j < names.length; j++) {
8539 mProvidersByName.remove(names[j]);
8540 }
8541
8542 Iterator<ProcessRecord> cit = cpr.clients.iterator();
8543 while (cit.hasNext()) {
8544 ProcessRecord capp = cit.next();
8545 if (!capp.persistent && capp.thread != null
8546 && capp.pid != 0
8547 && capp.pid != MY_PID) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07008548 Slog.i(TAG, "Kill " + capp.processName
8549 + " (pid " + capp.pid + "): provider " + cpr.info.name
8550 + " in dying process " + proc.processName);
8551 EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid,
8552 capp.processName, capp.setAdj, "dying provider " + proc.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 Process.killProcess(capp.pid);
8554 }
8555 }
8556
8557 mLaunchingProviders.remove(cpr);
8558 }
8559
8560 /**
8561 * Main code for cleaning up a process when it has gone away. This is
8562 * called both as a result of the process dying, or directly when stopping
8563 * a process when running in single process mode.
8564 */
8565 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
8566 boolean restarting, int index) {
8567 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008568 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008569 }
8570
Dianne Hackborn36124872009-10-08 16:22:03 -07008571 mProcessesToGc.remove(app);
8572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 // Dismiss any open dialogs.
8574 if (app.crashDialog != null) {
8575 app.crashDialog.dismiss();
8576 app.crashDialog = null;
8577 }
8578 if (app.anrDialog != null) {
8579 app.anrDialog.dismiss();
8580 app.anrDialog = null;
8581 }
8582 if (app.waitDialog != null) {
8583 app.waitDialog.dismiss();
8584 app.waitDialog = null;
8585 }
8586
8587 app.crashing = false;
8588 app.notResponding = false;
8589
8590 app.resetPackageList();
8591 app.thread = null;
8592 app.forcingToForeground = null;
8593 app.foregroundServices = false;
8594
8595 killServicesLocked(app, true);
8596
8597 boolean restart = false;
8598
8599 int NL = mLaunchingProviders.size();
8600
8601 // Remove published content providers.
8602 if (!app.pubProviders.isEmpty()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008603 Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008604 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008605 ContentProviderRecord cpr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008606 cpr.provider = null;
8607 cpr.app = null;
8608
8609 // See if someone is waiting for this provider... in which
8610 // case we don't remove it, but just let it restart.
8611 int i = 0;
8612 if (!app.bad) {
8613 for (; i<NL; i++) {
8614 if (mLaunchingProviders.get(i) == cpr) {
8615 restart = true;
8616 break;
8617 }
8618 }
8619 } else {
8620 i = NL;
8621 }
8622
8623 if (i >= NL) {
8624 removeDyingProviderLocked(app, cpr);
8625 NL = mLaunchingProviders.size();
8626 }
8627 }
8628 app.pubProviders.clear();
8629 }
8630
Dianne Hackbornf670ef72009-11-16 13:59:16 -08008631 // Take care of any launching providers waiting for this process.
8632 if (checkAppInLaunchingProvidersLocked(app, false)) {
8633 restart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008634 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08008635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 // Unregister from connected content providers.
8637 if (!app.conProviders.isEmpty()) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07008638 Iterator it = app.conProviders.keySet().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 while (it.hasNext()) {
8640 ContentProviderRecord cpr = (ContentProviderRecord)it.next();
8641 cpr.clients.remove(app);
8642 }
8643 app.conProviders.clear();
8644 }
8645
Dianne Hackbornde42bb62009-08-05 12:26:15 -07008646 // At this point there may be remaining entries in mLaunchingProviders
8647 // where we were the only one waiting, so they are no longer of use.
8648 // Look for these and clean up if found.
8649 // XXX Commented out for now. Trying to figure out a way to reproduce
8650 // the actual situation to identify what is actually going on.
8651 if (false) {
8652 for (int i=0; i<NL; i++) {
8653 ContentProviderRecord cpr = (ContentProviderRecord)
8654 mLaunchingProviders.get(i);
8655 if (cpr.clients.size() <= 0 && cpr.externals <= 0) {
8656 synchronized (cpr) {
8657 cpr.launchingApp = null;
8658 cpr.notifyAll();
8659 }
8660 }
8661 }
8662 }
8663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 skipCurrentReceiverLocked(app);
8665
8666 // Unregister any receivers.
8667 if (app.receivers.size() > 0) {
8668 Iterator<ReceiverList> it = app.receivers.iterator();
8669 while (it.hasNext()) {
8670 removeReceiverLocked(it.next());
8671 }
8672 app.receivers.clear();
8673 }
8674
Christopher Tate181fafa2009-05-14 11:12:14 -07008675 // If the app is undergoing backup, tell the backup manager about it
8676 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008677 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -07008678 try {
8679 IBackupManager bm = IBackupManager.Stub.asInterface(
8680 ServiceManager.getService(Context.BACKUP_SERVICE));
8681 bm.agentDisconnected(app.info.packageName);
8682 } catch (RemoteException e) {
8683 // can't happen; backup manager is local
8684 }
8685 }
8686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008687 // If the caller is restarting this app, then leave it in its
8688 // current lists and let the caller take care of it.
8689 if (restarting) {
8690 return;
8691 }
8692
8693 if (!app.persistent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008694 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008695 "Removing non-persistent process during cleanup: " + app);
8696 mProcessNames.remove(app.processName, app.info.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07008697 if (mHeavyWeightProcess == app) {
8698 mHeavyWeightProcess = null;
8699 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
8700 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008701 } else if (!app.removed) {
8702 // This app is persistent, so we need to keep its record around.
8703 // If it is not already on the pending app list, add it there
8704 // and start a new process for it.
8705 app.thread = null;
8706 app.forcingToForeground = null;
8707 app.foregroundServices = false;
8708 if (mPersistentStartingProcesses.indexOf(app) < 0) {
8709 mPersistentStartingProcesses.add(app);
8710 restart = true;
8711 }
8712 }
Dianne Hackborn8891fdc2010-09-20 20:44:46 -07008713 if (DEBUG_PROCESSES && mProcessesOnHold.contains(app)) Slog.v(TAG,
8714 "Clean-up removing on hold: " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 mProcessesOnHold.remove(app);
8716
The Android Open Source Project4df24232009-03-05 14:34:35 -08008717 if (app == mHomeProcess) {
8718 mHomeProcess = null;
8719 }
8720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008721 if (restart) {
8722 // We have components that still need to be running in the
8723 // process, so re-launch it.
8724 mProcessNames.put(app.processName, app.info.uid, app);
8725 startProcessLocked(app, "restart", app.processName);
8726 } else if (app.pid > 0 && app.pid != MY_PID) {
8727 // Goodbye!
8728 synchronized (mPidsSelfLocked) {
8729 mPidsSelfLocked.remove(app.pid);
8730 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
8731 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07008732 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008733 }
8734 }
8735
Dianne Hackbornf670ef72009-11-16 13:59:16 -08008736 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
8737 // Look through the content providers we are waiting to have launched,
8738 // and if any run in this process then either schedule a restart of
8739 // the process or kill the client waiting for it if this process has
8740 // gone bad.
8741 int NL = mLaunchingProviders.size();
8742 boolean restart = false;
8743 for (int i=0; i<NL; i++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07008744 ContentProviderRecord cpr = mLaunchingProviders.get(i);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08008745 if (cpr.launchingApp == app) {
8746 if (!alwaysBad && !app.bad) {
8747 restart = true;
8748 } else {
8749 removeDyingProviderLocked(app, cpr);
8750 NL = mLaunchingProviders.size();
8751 }
8752 }
8753 }
8754 return restart;
8755 }
8756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 // =========================================================
8758 // SERVICES
8759 // =========================================================
8760
8761 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) {
8762 ActivityManager.RunningServiceInfo info =
8763 new ActivityManager.RunningServiceInfo();
8764 info.service = r.name;
8765 if (r.app != null) {
8766 info.pid = r.app.pid;
8767 }
Dianne Hackborn3025ef32009-08-31 21:31:47 -07008768 info.uid = r.appInfo.uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008769 info.process = r.processName;
8770 info.foreground = r.isForeground;
8771 info.activeSince = r.createTime;
8772 info.started = r.startRequested;
8773 info.clientCount = r.connections.size();
8774 info.crashCount = r.crashCount;
8775 info.lastActivityTime = r.lastActivity;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07008776 if (r.isForeground) {
8777 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND;
8778 }
8779 if (r.startRequested) {
8780 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED;
8781 }
Dan Egnor42471dd2010-01-07 17:25:22 -08008782 if (r.app != null && r.app.pid == MY_PID) {
Dianne Hackborn3025ef32009-08-31 21:31:47 -07008783 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS;
8784 }
8785 if (r.app != null && r.app.persistent) {
8786 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS;
8787 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008788
8789 for (ArrayList<ConnectionRecord> connl : r.connections.values()) {
8790 for (int i=0; i<connl.size(); i++) {
8791 ConnectionRecord conn = connl.get(i);
8792 if (conn.clientLabel != 0) {
8793 info.clientPackage = conn.binding.client.info.packageName;
8794 info.clientLabel = conn.clientLabel;
8795 return info;
8796 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008797 }
8798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008799 return info;
8800 }
8801
8802 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
8803 int flags) {
8804 synchronized (this) {
8805 ArrayList<ActivityManager.RunningServiceInfo> res
8806 = new ArrayList<ActivityManager.RunningServiceInfo>();
8807
8808 if (mServices.size() > 0) {
8809 Iterator<ServiceRecord> it = mServices.values().iterator();
8810 while (it.hasNext() && res.size() < maxNum) {
8811 res.add(makeRunningServiceInfoLocked(it.next()));
8812 }
8813 }
8814
8815 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) {
8816 ServiceRecord r = mRestartingServices.get(i);
8817 ActivityManager.RunningServiceInfo info =
8818 makeRunningServiceInfoLocked(r);
8819 info.restarting = r.nextRestartTime;
8820 res.add(info);
8821 }
8822
8823 return res;
8824 }
8825 }
8826
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008827 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
8828 synchronized (this) {
8829 ServiceRecord r = mServices.get(name);
8830 if (r != null) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07008831 for (ArrayList<ConnectionRecord> conn : r.connections.values()) {
8832 for (int i=0; i<conn.size(); i++) {
8833 if (conn.get(i).clientIntent != null) {
8834 return conn.get(i).clientIntent;
8835 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008836 }
8837 }
8838 }
8839 }
8840 return null;
8841 }
8842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 private final ServiceRecord findServiceLocked(ComponentName name,
8844 IBinder token) {
8845 ServiceRecord r = mServices.get(name);
8846 return r == token ? r : null;
8847 }
8848
8849 private final class ServiceLookupResult {
8850 final ServiceRecord record;
8851 final String permission;
8852
8853 ServiceLookupResult(ServiceRecord _record, String _permission) {
8854 record = _record;
8855 permission = _permission;
8856 }
8857 };
8858
8859 private ServiceLookupResult findServiceLocked(Intent service,
8860 String resolvedType) {
8861 ServiceRecord r = null;
8862 if (service.getComponent() != null) {
8863 r = mServices.get(service.getComponent());
8864 }
8865 if (r == null) {
8866 Intent.FilterComparison filter = new Intent.FilterComparison(service);
8867 r = mServicesByIntent.get(filter);
8868 }
8869
8870 if (r == null) {
8871 try {
8872 ResolveInfo rInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008873 AppGlobals.getPackageManager().resolveService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008874 service, resolvedType, 0);
8875 ServiceInfo sInfo =
8876 rInfo != null ? rInfo.serviceInfo : null;
8877 if (sInfo == null) {
8878 return null;
8879 }
8880
8881 ComponentName name = new ComponentName(
8882 sInfo.applicationInfo.packageName, sInfo.name);
8883 r = mServices.get(name);
8884 } catch (RemoteException ex) {
8885 // pm is in same process, this will never happen.
8886 }
8887 }
8888 if (r != null) {
8889 int callingPid = Binder.getCallingPid();
8890 int callingUid = Binder.getCallingUid();
8891 if (checkComponentPermission(r.permission,
8892 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
8893 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008894 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895 + " from pid=" + callingPid
8896 + ", uid=" + callingUid
8897 + " requires " + r.permission);
8898 return new ServiceLookupResult(null, r.permission);
8899 }
8900 return new ServiceLookupResult(r, null);
8901 }
8902 return null;
8903 }
8904
8905 private class ServiceRestarter implements Runnable {
8906 private ServiceRecord mService;
8907
8908 void setService(ServiceRecord service) {
8909 mService = service;
8910 }
8911
8912 public void run() {
8913 synchronized(ActivityManagerService.this) {
8914 performServiceRestartLocked(mService);
8915 }
8916 }
8917 }
8918
8919 private ServiceLookupResult retrieveServiceLocked(Intent service,
8920 String resolvedType, int callingPid, int callingUid) {
8921 ServiceRecord r = null;
8922 if (service.getComponent() != null) {
8923 r = mServices.get(service.getComponent());
8924 }
8925 Intent.FilterComparison filter = new Intent.FilterComparison(service);
8926 r = mServicesByIntent.get(filter);
8927 if (r == null) {
8928 try {
8929 ResolveInfo rInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008930 AppGlobals.getPackageManager().resolveService(
Dianne Hackborn1655be42009-05-08 14:29:01 -07008931 service, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008932 ServiceInfo sInfo =
8933 rInfo != null ? rInfo.serviceInfo : null;
8934 if (sInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008935 Slog.w(TAG, "Unable to start service " + service +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008936 ": not found");
8937 return null;
8938 }
8939
8940 ComponentName name = new ComponentName(
8941 sInfo.applicationInfo.packageName, sInfo.name);
8942 r = mServices.get(name);
8943 if (r == null) {
8944 filter = new Intent.FilterComparison(service.cloneFilter());
8945 ServiceRestarter res = new ServiceRestarter();
8946 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
8947 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
8948 synchronized (stats) {
8949 ss = stats.getServiceStatsLocked(
8950 sInfo.applicationInfo.uid, sInfo.packageName,
8951 sInfo.name);
8952 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -08008953 r = new ServiceRecord(this, ss, name, filter, sInfo, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008954 res.setService(r);
8955 mServices.put(name, r);
8956 mServicesByIntent.put(filter, r);
8957
8958 // Make sure this component isn't in the pending list.
8959 int N = mPendingServices.size();
8960 for (int i=0; i<N; i++) {
8961 ServiceRecord pr = mPendingServices.get(i);
8962 if (pr.name.equals(name)) {
8963 mPendingServices.remove(i);
8964 i--;
8965 N--;
8966 }
8967 }
8968 }
8969 } catch (RemoteException ex) {
8970 // pm is in same process, this will never happen.
8971 }
8972 }
8973 if (r != null) {
8974 if (checkComponentPermission(r.permission,
8975 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
8976 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008977 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008978 + " from pid=" + Binder.getCallingPid()
8979 + ", uid=" + Binder.getCallingUid()
8980 + " requires " + r.permission);
8981 return new ServiceLookupResult(null, r.permission);
8982 }
8983 return new ServiceLookupResult(r, null);
8984 }
8985 return null;
8986 }
8987
Dianne Hackborn287952c2010-09-22 22:34:31 -07008988 private final void bumpServiceExecutingLocked(ServiceRecord r, String why) {
8989 if (DEBUG_SERVICE) Log.v(TAG, ">>> EXECUTING "
8990 + why + " of " + r + " in app " + r.app);
8991 else if (DEBUG_SERVICE_EXECUTING) Log.v(TAG, ">>> EXECUTING "
8992 + why + " of " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008993 long now = SystemClock.uptimeMillis();
8994 if (r.executeNesting == 0 && r.app != null) {
8995 if (r.app.executingServices.size() == 0) {
8996 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
8997 msg.obj = r.app;
8998 mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT);
8999 }
9000 r.app.executingServices.add(r);
9001 }
9002 r.executeNesting++;
9003 r.executingStart = now;
9004 }
9005
9006 private final void sendServiceArgsLocked(ServiceRecord r,
9007 boolean oomAdjusted) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009008 final int N = r.pendingStarts.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009009 if (N == 0) {
9010 return;
9011 }
9012
Dianne Hackborn39792d22010-08-19 18:01:52 -07009013 while (r.pendingStarts.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009014 try {
Dianne Hackborn39792d22010-08-19 18:01:52 -07009015 ServiceRecord.StartItem si = r.pendingStarts.remove(0);
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009016 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to: "
9017 + r + " " + r.intent + " args=" + si.intent);
Dianne Hackborn39792d22010-08-19 18:01:52 -07009018 if (si.intent == null) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009019 // If somehow we got a dummy start at the front, then
9020 // just drop it here.
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009021 continue;
9022 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07009023 si.deliveredTime = SystemClock.uptimeMillis();
9024 r.deliveredStarts.add(si);
9025 si.deliveryCount++;
9026 if (si.targetPermissionUid >= 0) {
9027 grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid,
Dianne Hackborn7e269642010-08-25 19:50:20 -07009028 r.packageName, si.intent, si.getUriPermissionsLocked());
Dianne Hackborn39792d22010-08-19 18:01:52 -07009029 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07009030 bumpServiceExecutingLocked(r, "start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 if (!oomAdjusted) {
9032 oomAdjusted = true;
9033 updateOomAdjLocked(r.app);
9034 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009035 int flags = 0;
9036 if (si.deliveryCount > 0) {
9037 flags |= Service.START_FLAG_RETRY;
9038 }
9039 if (si.doneExecutingCount > 0) {
9040 flags |= Service.START_FLAG_REDELIVERY;
9041 }
9042 r.app.thread.scheduleServiceArgs(r, si.id, flags, si.intent);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009043 } catch (RemoteException e) {
9044 // Remote process gone... we'll let the normal cleanup take
9045 // care of this.
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009046 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while scheduling start: " + r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009047 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009048 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009049 Slog.w(TAG, "Unexpected exception", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 break;
9051 }
9052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009053 }
9054
9055 private final boolean requestServiceBindingLocked(ServiceRecord r,
9056 IntentBindRecord i, boolean rebind) {
9057 if (r.app == null || r.app.thread == null) {
9058 // If service is not currently running, can't yet bind.
9059 return false;
9060 }
9061 if ((!i.requested || rebind) && i.apps.size() > 0) {
9062 try {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009063 bumpServiceExecutingLocked(r, "bind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind);
9065 if (!rebind) {
9066 i.requested = true;
9067 }
9068 i.hasBound = true;
9069 i.doRebind = false;
9070 } catch (RemoteException e) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009071 if (DEBUG_SERVICE) Slog.v(TAG, "Crashed while binding " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 return false;
9073 }
9074 }
9075 return true;
9076 }
9077
9078 private final void requestServiceBindingsLocked(ServiceRecord r) {
9079 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator();
9080 while (bindings.hasNext()) {
9081 IntentBindRecord i = bindings.next();
9082 if (!requestServiceBindingLocked(r, i, false)) {
9083 break;
9084 }
9085 }
9086 }
9087
9088 private final void realStartServiceLocked(ServiceRecord r,
9089 ProcessRecord app) throws RemoteException {
9090 if (app.thread == null) {
9091 throw new RemoteException();
9092 }
9093
9094 r.app = app;
The Android Open Source Project10592532009-03-18 17:39:46 -07009095 r.restartTime = r.lastActivity = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096
9097 app.services.add(r);
Dianne Hackborn287952c2010-09-22 22:34:31 -07009098 bumpServiceExecutingLocked(r, "create");
Dianne Hackborndd71fc82009-12-16 19:24:32 -08009099 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009100
9101 boolean created = false;
9102 try {
Dianne Hackborna33e3f72009-09-29 17:28:24 -07009103 mStringBuilder.setLength(0);
9104 r.intent.getIntent().toShortString(mStringBuilder, false, true);
Doug Zongker2bec3d42009-12-04 12:52:44 -08009105 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106 System.identityHashCode(r), r.shortName,
Dianne Hackborna33e3f72009-09-29 17:28:24 -07009107 mStringBuilder.toString(), r.app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 synchronized (r.stats.getBatteryStats()) {
9109 r.stats.startLaunchedLocked();
9110 }
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07009111 ensurePackageDexOpt(r.serviceInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 app.thread.scheduleCreateService(r, r.serviceInfo);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009113 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009114 created = true;
9115 } finally {
9116 if (!created) {
9117 app.services.remove(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009118 scheduleServiceRestartLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 }
9120 }
9121
9122 requestServiceBindingsLocked(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009123
9124 // If the service is in the started state, and there are no
9125 // pending arguments, then fake up one so its onStartCommand() will
9126 // be called.
9127 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {
9128 r.lastStartId++;
9129 if (r.lastStartId < 1) {
9130 r.lastStartId = 1;
9131 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07009132 r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId, null, -1));
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009133 }
9134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009135 sendServiceArgsLocked(r, true);
9136 }
9137
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009138 private final boolean scheduleServiceRestartLocked(ServiceRecord r,
9139 boolean allowCancel) {
9140 boolean canceled = false;
9141
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009142 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009143 long minDuration = SERVICE_RESTART_DURATION;
Dianne Hackborn6ccd2af2009-08-27 12:26:44 -07009144 long resetTime = SERVICE_RESET_RUN_DURATION;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009145
Dianne Hackborn070783f2010-12-29 16:46:28 -08009146 if ((r.serviceInfo.applicationInfo.flags
9147 &ApplicationInfo.FLAG_PERSISTENT) != 0) {
9148 minDuration /= 4;
9149 }
9150
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009151 // Any delivered but not yet finished starts should be put back
9152 // on the pending list.
9153 final int N = r.deliveredStarts.size();
9154 if (N > 0) {
9155 for (int i=N-1; i>=0; i--) {
9156 ServiceRecord.StartItem si = r.deliveredStarts.get(i);
Dianne Hackborn39792d22010-08-19 18:01:52 -07009157 si.removeUriPermissionsLocked();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009158 if (si.intent == null) {
9159 // We'll generate this again if needed.
9160 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT
9161 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) {
9162 r.pendingStarts.add(0, si);
9163 long dur = SystemClock.uptimeMillis() - si.deliveredTime;
9164 dur *= 2;
9165 if (minDuration < dur) minDuration = dur;
9166 if (resetTime < dur) resetTime = dur;
9167 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009168 Slog.w(TAG, "Canceling start item " + si.intent + " in service "
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009169 + r.name);
9170 canceled = true;
9171 }
9172 }
9173 r.deliveredStarts.clear();
9174 }
9175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 r.totalRestartCount++;
9177 if (r.restartDelay == 0) {
9178 r.restartCount++;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009179 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 } else {
9181 // If it has been a "reasonably long time" since the service
9182 // was started, then reset our restart duration back to
9183 // the beginning, so we don't infinitely increase the duration
9184 // on a service that just occasionally gets killed (which is
9185 // a normal case, due to process being killed to reclaim memory).
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009186 if (now > (r.restartTime+resetTime)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 r.restartCount = 1;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009188 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009189 } else {
Dianne Hackborn070783f2010-12-29 16:46:28 -08009190 if ((r.serviceInfo.applicationInfo.flags
9191 &ApplicationInfo.FLAG_PERSISTENT) != 0) {
9192 // Services in peristent processes will restart much more
9193 // quickly, since they are pretty important. (Think SystemUI).
9194 r.restartDelay += minDuration/2;
9195 } else {
9196 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;
9197 if (r.restartDelay < minDuration) {
9198 r.restartDelay = minDuration;
9199 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009201 }
9202 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009203
9204 r.nextRestartTime = now + r.restartDelay;
9205
9206 // Make sure that we don't end up restarting a bunch of services
9207 // all at the same time.
9208 boolean repeat;
9209 do {
9210 repeat = false;
9211 for (int i=mRestartingServices.size()-1; i>=0; i--) {
9212 ServiceRecord r2 = mRestartingServices.get(i);
9213 if (r2 != r && r.nextRestartTime
9214 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN)
9215 && r.nextRestartTime
9216 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) {
9217 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN;
9218 r.restartDelay = r.nextRestartTime - now;
9219 repeat = true;
9220 break;
9221 }
9222 }
9223 } while (repeat);
9224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009225 if (!mRestartingServices.contains(r)) {
9226 mRestartingServices.add(r);
9227 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009228
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009229 r.cancelNotification();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009231 mHandler.removeCallbacks(r.restarter);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07009232 mHandler.postAtTime(r.restarter, r.nextRestartTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009234 Slog.w(TAG, "Scheduling restart of crashed service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 + r.shortName + " in " + r.restartDelay + "ms");
Doug Zongker2bec3d42009-12-04 12:52:44 -08009236 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 r.shortName, r.restartDelay);
9238
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009239 return canceled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009240 }
9241
9242 final void performServiceRestartLocked(ServiceRecord r) {
9243 if (!mRestartingServices.contains(r)) {
9244 return;
9245 }
9246 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true);
9247 }
9248
9249 private final boolean unscheduleServiceRestartLocked(ServiceRecord r) {
9250 if (r.restartDelay == 0) {
9251 return false;
9252 }
9253 r.resetRestartCounter();
9254 mRestartingServices.remove(r);
9255 mHandler.removeCallbacks(r.restarter);
9256 return true;
9257 }
9258
9259 private final boolean bringUpServiceLocked(ServiceRecord r,
9260 int intentFlags, boolean whileRestarting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009261 //Slog.i(TAG, "Bring up service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262 //r.dump(" ");
9263
Dianne Hackborn36124872009-10-08 16:22:03 -07009264 if (r.app != null && r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 sendServiceArgsLocked(r, false);
9266 return true;
9267 }
9268
9269 if (!whileRestarting && r.restartDelay > 0) {
9270 // If waiting for a restart, then do nothing.
9271 return true;
9272 }
9273
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009274 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up " + r + " " + r.intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275
Dianne Hackbornde42bb62009-08-05 12:26:15 -07009276 // We are now bringing the service up, so no longer in the
9277 // restarting state.
9278 mRestartingServices.remove(r);
9279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280 final String appName = r.processName;
9281 ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid);
9282 if (app != null && app.thread != null) {
9283 try {
9284 realStartServiceLocked(r, app);
9285 return true;
9286 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009287 Slog.w(TAG, "Exception when starting service " + r.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009288 }
9289
9290 // If a dead object exception was thrown -- fall through to
9291 // restart the application.
9292 }
9293
Dianne Hackborn36124872009-10-08 16:22:03 -07009294 // Not running -- get it started, and enqueue this service record
9295 // to be executed when the app comes up.
9296 if (startProcessLocked(appName, r.appInfo, true, intentFlags,
9297 "service", r.name, false) == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009298 Slog.w(TAG, "Unable to launch app "
Dianne Hackborn36124872009-10-08 16:22:03 -07009299 + r.appInfo.packageName + "/"
9300 + r.appInfo.uid + " for service "
9301 + r.intent.getIntent() + ": process is bad");
9302 bringDownServiceLocked(r, true);
9303 return false;
9304 }
9305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 if (!mPendingServices.contains(r)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 mPendingServices.add(r);
9308 }
Dianne Hackborn36124872009-10-08 16:22:03 -07009309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009310 return true;
9311 }
9312
9313 private final void bringDownServiceLocked(ServiceRecord r, boolean force) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009314 //Slog.i(TAG, "Bring down service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009315 //r.dump(" ");
9316
9317 // Does it still need to run?
9318 if (!force && r.startRequested) {
9319 return;
9320 }
9321 if (r.connections.size() > 0) {
9322 if (!force) {
9323 // XXX should probably keep a count of the number of auto-create
9324 // connections directly in the service.
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009325 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009326 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009327 ArrayList<ConnectionRecord> cr = it.next();
9328 for (int i=0; i<cr.size(); i++) {
9329 if ((cr.get(i).flags&Context.BIND_AUTO_CREATE) != 0) {
9330 return;
9331 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009332 }
9333 }
9334 }
9335
9336 // Report to all of the connections that the service is no longer
9337 // available.
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009338 Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009340 ArrayList<ConnectionRecord> c = it.next();
9341 for (int i=0; i<c.size(); i++) {
9342 try {
9343 c.get(i).conn.connected(r.name, null);
9344 } catch (Exception e) {
9345 Slog.w(TAG, "Failure disconnecting service " + r.name +
9346 " to connection " + c.get(i).conn.asBinder() +
9347 " (in " + c.get(i).binding.client.processName + ")", e);
9348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009349 }
9350 }
9351 }
9352
9353 // Tell the service that it has been unbound.
9354 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) {
9355 Iterator<IntentBindRecord> it = r.bindings.values().iterator();
9356 while (it.hasNext()) {
9357 IntentBindRecord ibr = it.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -08009358 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 + ": hasBound=" + ibr.hasBound);
9360 if (r.app != null && r.app.thread != null && ibr.hasBound) {
9361 try {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009362 bumpServiceExecutingLocked(r, "bring down unbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009363 updateOomAdjLocked(r.app);
9364 ibr.hasBound = false;
9365 r.app.thread.scheduleUnbindService(r,
9366 ibr.intent.getIntent());
9367 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009368 Slog.w(TAG, "Exception when unbinding service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009369 + r.shortName, e);
9370 serviceDoneExecutingLocked(r, true);
9371 }
9372 }
9373 }
9374 }
9375
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009376 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down " + r + " " + r.intent);
Doug Zongker2bec3d42009-12-04 12:52:44 -08009377 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009378 System.identityHashCode(r), r.shortName,
9379 (r.app != null) ? r.app.pid : -1);
9380
9381 mServices.remove(r.name);
9382 mServicesByIntent.remove(r.intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 r.totalRestartCount = 0;
9384 unscheduleServiceRestartLocked(r);
9385
9386 // Also make sure it is not on the pending list.
9387 int N = mPendingServices.size();
9388 for (int i=0; i<N; i++) {
9389 if (mPendingServices.get(i) == r) {
9390 mPendingServices.remove(i);
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009391 if (DEBUG_SERVICE) Slog.v(TAG, "Removed pending: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392 i--;
9393 N--;
9394 }
9395 }
9396
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009397 r.cancelNotification();
9398 r.isForeground = false;
9399 r.foregroundId = 0;
9400 r.foregroundNoti = null;
9401
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009402 // Clear start entries.
Dianne Hackborn39792d22010-08-19 18:01:52 -07009403 r.clearDeliveredStartsLocked();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009404 r.pendingStarts.clear();
9405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009406 if (r.app != null) {
9407 synchronized (r.stats.getBatteryStats()) {
9408 r.stats.stopLaunchedLocked();
9409 }
9410 r.app.services.remove(r);
9411 if (r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009412 try {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009413 bumpServiceExecutingLocked(r, "stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 mStoppingServices.add(r);
9415 updateOomAdjLocked(r.app);
9416 r.app.thread.scheduleStopService(r);
9417 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009418 Slog.w(TAG, "Exception when stopping service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009419 + r.shortName, e);
9420 serviceDoneExecutingLocked(r, true);
9421 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009422 updateServiceForegroundLocked(r.app, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009423 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009424 if (DEBUG_SERVICE) Slog.v(
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009425 TAG, "Removed service that has no process: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009426 }
9427 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009428 if (DEBUG_SERVICE) Slog.v(
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009429 TAG, "Removed service that is not running: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 }
Vairavan Srinivasana207ce22010-12-23 13:51:48 -08009431
9432 if (r.bindings.size() > 0) {
9433 r.bindings.clear();
9434 }
9435
9436 if (r.restarter instanceof ServiceRestarter) {
9437 ((ServiceRestarter)r.restarter).setService(null);
9438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009439 }
9440
9441 ComponentName startServiceLocked(IApplicationThread caller,
9442 Intent service, String resolvedType,
9443 int callingPid, int callingUid) {
9444 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009445 if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009446 + " type=" + resolvedType + " args=" + service.getExtras());
9447
9448 if (caller != null) {
9449 final ProcessRecord callerApp = getRecordForAppLocked(caller);
9450 if (callerApp == null) {
9451 throw new SecurityException(
9452 "Unable to find app for caller " + caller
9453 + " (pid=" + Binder.getCallingPid()
9454 + ") when starting service " + service);
9455 }
9456 }
9457
9458 ServiceLookupResult res =
9459 retrieveServiceLocked(service, resolvedType,
9460 callingPid, callingUid);
9461 if (res == null) {
9462 return null;
9463 }
9464 if (res.record == null) {
9465 return new ComponentName("!", res.permission != null
9466 ? res.permission : "private to package");
9467 }
9468 ServiceRecord r = res.record;
Dianne Hackborn39792d22010-08-19 18:01:52 -07009469 int targetPermissionUid = checkGrantUriPermissionFromIntentLocked(
9470 callingUid, r.packageName, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009471 if (unscheduleServiceRestartLocked(r)) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009472 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 }
9474 r.startRequested = true;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009475 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 r.lastStartId++;
9477 if (r.lastStartId < 1) {
9478 r.lastStartId = 1;
9479 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07009480 r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId,
9481 service, targetPermissionUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009482 r.lastActivity = SystemClock.uptimeMillis();
9483 synchronized (r.stats.getBatteryStats()) {
9484 r.stats.startRunningLocked();
9485 }
9486 if (!bringUpServiceLocked(r, service.getFlags(), false)) {
9487 return new ComponentName("!", "Service process is bad");
9488 }
9489 return r.name;
9490 }
9491 }
9492
9493 public ComponentName startService(IApplicationThread caller, Intent service,
9494 String resolvedType) {
9495 // Refuse possible leaked file descriptors
9496 if (service != null && service.hasFileDescriptors() == true) {
9497 throw new IllegalArgumentException("File descriptors passed in Intent");
9498 }
9499
9500 synchronized(this) {
9501 final int callingPid = Binder.getCallingPid();
9502 final int callingUid = Binder.getCallingUid();
9503 final long origId = Binder.clearCallingIdentity();
9504 ComponentName res = startServiceLocked(caller, service,
9505 resolvedType, callingPid, callingUid);
9506 Binder.restoreCallingIdentity(origId);
9507 return res;
9508 }
9509 }
9510
9511 ComponentName startServiceInPackage(int uid,
9512 Intent service, String resolvedType) {
9513 synchronized(this) {
9514 final long origId = Binder.clearCallingIdentity();
9515 ComponentName res = startServiceLocked(null, service,
9516 resolvedType, -1, uid);
9517 Binder.restoreCallingIdentity(origId);
9518 return res;
9519 }
9520 }
9521
9522 public int stopService(IApplicationThread caller, Intent service,
9523 String resolvedType) {
9524 // Refuse possible leaked file descriptors
9525 if (service != null && service.hasFileDescriptors() == true) {
9526 throw new IllegalArgumentException("File descriptors passed in Intent");
9527 }
9528
9529 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009530 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009531 + " type=" + resolvedType);
9532
9533 final ProcessRecord callerApp = getRecordForAppLocked(caller);
9534 if (caller != null && callerApp == null) {
9535 throw new SecurityException(
9536 "Unable to find app for caller " + caller
9537 + " (pid=" + Binder.getCallingPid()
9538 + ") when stopping service " + service);
9539 }
9540
9541 // If this service is active, make sure it is stopped.
9542 ServiceLookupResult r = findServiceLocked(service, resolvedType);
9543 if (r != null) {
9544 if (r.record != null) {
9545 synchronized (r.record.stats.getBatteryStats()) {
9546 r.record.stats.stopRunningLocked();
9547 }
9548 r.record.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009549 r.record.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009550 final long origId = Binder.clearCallingIdentity();
9551 bringDownServiceLocked(r.record, false);
9552 Binder.restoreCallingIdentity(origId);
9553 return 1;
9554 }
9555 return -1;
9556 }
9557 }
9558
9559 return 0;
9560 }
9561
9562 public IBinder peekService(Intent service, String resolvedType) {
9563 // Refuse possible leaked file descriptors
9564 if (service != null && service.hasFileDescriptors() == true) {
9565 throw new IllegalArgumentException("File descriptors passed in Intent");
9566 }
9567
9568 IBinder ret = null;
9569
9570 synchronized(this) {
9571 ServiceLookupResult r = findServiceLocked(service, resolvedType);
9572
9573 if (r != null) {
9574 // r.record is null if findServiceLocked() failed the caller permission check
9575 if (r.record == null) {
9576 throw new SecurityException(
9577 "Permission Denial: Accessing service " + r.record.name
9578 + " from pid=" + Binder.getCallingPid()
9579 + ", uid=" + Binder.getCallingUid()
9580 + " requires " + r.permission);
9581 }
9582 IntentBindRecord ib = r.record.bindings.get(r.record.intent);
9583 if (ib != null) {
9584 ret = ib.binder;
9585 }
9586 }
9587 }
9588
9589 return ret;
9590 }
9591
9592 public boolean stopServiceToken(ComponentName className, IBinder token,
9593 int startId) {
9594 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009595 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009596 + " " + token + " startId=" + startId);
9597 ServiceRecord r = findServiceLocked(className, token);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009598 if (r != null) {
9599 if (startId >= 0) {
9600 // Asked to only stop if done with all work. Note that
9601 // to avoid leaks, we will take this as dropping all
9602 // start items up to and including this one.
9603 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
9604 if (si != null) {
9605 while (r.deliveredStarts.size() > 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07009606 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0);
9607 cur.removeUriPermissionsLocked();
9608 if (cur == si) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009609 break;
9610 }
9611 }
9612 }
9613
9614 if (r.lastStartId != startId) {
9615 return false;
9616 }
9617
9618 if (r.deliveredStarts.size() > 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009619 Slog.w(TAG, "stopServiceToken startId " + startId
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009620 + " is last, but have " + r.deliveredStarts.size()
9621 + " remaining args");
9622 }
9623 }
9624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 synchronized (r.stats.getBatteryStats()) {
9626 r.stats.stopRunningLocked();
9627 r.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009628 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629 }
9630 final long origId = Binder.clearCallingIdentity();
9631 bringDownServiceLocked(r, false);
9632 Binder.restoreCallingIdentity(origId);
9633 return true;
9634 }
9635 }
9636 return false;
9637 }
9638
9639 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009640 int id, Notification notification, boolean removeNotification) {
9641 final long origId = Binder.clearCallingIdentity();
9642 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009643 synchronized(this) {
9644 ServiceRecord r = findServiceLocked(className, token);
9645 if (r != null) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009646 if (id != 0) {
9647 if (notification == null) {
9648 throw new IllegalArgumentException("null notification");
9649 }
9650 if (r.foregroundId != id) {
9651 r.cancelNotification();
9652 r.foregroundId = id;
9653 }
9654 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
9655 r.foregroundNoti = notification;
9656 r.isForeground = true;
9657 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009658 if (r.app != null) {
9659 updateServiceForegroundLocked(r.app, true);
9660 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009661 } else {
9662 if (r.isForeground) {
9663 r.isForeground = false;
9664 if (r.app != null) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07009665 updateLruProcessLocked(r.app, false, true);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009666 updateServiceForegroundLocked(r.app, true);
9667 }
9668 }
9669 if (removeNotification) {
9670 r.cancelNotification();
9671 r.foregroundId = 0;
9672 r.foregroundNoti = null;
9673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 }
9675 }
9676 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07009677 } finally {
9678 Binder.restoreCallingIdentity(origId);
9679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 }
9681
9682 public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) {
9683 boolean anyForeground = false;
Dianne Hackborn860755f2010-06-03 18:47:52 -07009684 for (ServiceRecord sr : proc.services) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685 if (sr.isForeground) {
9686 anyForeground = true;
9687 break;
9688 }
9689 }
9690 if (anyForeground != proc.foregroundServices) {
9691 proc.foregroundServices = anyForeground;
9692 if (oomAdj) {
9693 updateOomAdjLocked();
9694 }
9695 }
9696 }
9697
9698 public int bindService(IApplicationThread caller, IBinder token,
9699 Intent service, String resolvedType,
9700 IServiceConnection connection, int flags) {
9701 // Refuse possible leaked file descriptors
9702 if (service != null && service.hasFileDescriptors() == true) {
9703 throw new IllegalArgumentException("File descriptors passed in Intent");
9704 }
9705
9706 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009707 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009708 + " type=" + resolvedType + " conn=" + connection.asBinder()
9709 + " flags=0x" + Integer.toHexString(flags));
9710 final ProcessRecord callerApp = getRecordForAppLocked(caller);
9711 if (callerApp == null) {
9712 throw new SecurityException(
9713 "Unable to find app for caller " + caller
9714 + " (pid=" + Binder.getCallingPid()
9715 + ") when binding service " + service);
9716 }
9717
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009718 ActivityRecord activity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009719 if (token != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009720 int aindex = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009721 if (aindex < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009722 Slog.w(TAG, "Binding with unknown activity: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 return 0;
9724 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009725 activity = (ActivityRecord)mMainStack.mHistory.get(aindex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 }
9727
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07009728 int clientLabel = 0;
9729 PendingIntent clientIntent = null;
9730
9731 if (callerApp.info.uid == Process.SYSTEM_UID) {
9732 // Hacky kind of thing -- allow system stuff to tell us
9733 // what they are, so we can report this elsewhere for
9734 // others to know why certain services are running.
9735 try {
9736 clientIntent = (PendingIntent)service.getParcelableExtra(
9737 Intent.EXTRA_CLIENT_INTENT);
9738 } catch (RuntimeException e) {
9739 }
9740 if (clientIntent != null) {
9741 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0);
9742 if (clientLabel != 0) {
9743 // There are no useful extras in the intent, trash them.
9744 // System code calling with this stuff just needs to know
9745 // this will happen.
9746 service = service.cloneFilter();
9747 }
9748 }
9749 }
9750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009751 ServiceLookupResult res =
9752 retrieveServiceLocked(service, resolvedType,
9753 Binder.getCallingPid(), Binder.getCallingUid());
9754 if (res == null) {
9755 return 0;
9756 }
9757 if (res.record == null) {
9758 return -1;
9759 }
9760 ServiceRecord s = res.record;
9761
9762 final long origId = Binder.clearCallingIdentity();
9763
9764 if (unscheduleServiceRestartLocked(s)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009765 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: "
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009766 + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009767 }
9768
9769 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
9770 ConnectionRecord c = new ConnectionRecord(b, activity,
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07009771 connection, flags, clientLabel, clientIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009772
9773 IBinder binder = connection.asBinder();
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009774 ArrayList<ConnectionRecord> clist = s.connections.get(binder);
9775 if (clist == null) {
9776 clist = new ArrayList<ConnectionRecord>();
9777 s.connections.put(binder, clist);
9778 }
9779 clist.add(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009780 b.connections.add(c);
9781 if (activity != null) {
9782 if (activity.connections == null) {
9783 activity.connections = new HashSet<ConnectionRecord>();
9784 }
9785 activity.connections.add(c);
9786 }
9787 b.client.connections.add(c);
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009788 clist = mServiceConnections.get(binder);
9789 if (clist == null) {
9790 clist = new ArrayList<ConnectionRecord>();
9791 mServiceConnections.put(binder, clist);
9792 }
9793 clist.add(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794
9795 if ((flags&Context.BIND_AUTO_CREATE) != 0) {
9796 s.lastActivity = SystemClock.uptimeMillis();
9797 if (!bringUpServiceLocked(s, service.getFlags(), false)) {
9798 return 0;
9799 }
9800 }
9801
9802 if (s.app != null) {
9803 // This could have made the service more important.
9804 updateOomAdjLocked(s.app);
9805 }
9806
Joe Onorato8a9b2202010-02-26 18:56:32 -08009807 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 + ": received=" + b.intent.received
9809 + " apps=" + b.intent.apps.size()
9810 + " doRebind=" + b.intent.doRebind);
9811
9812 if (s.app != null && b.intent.received) {
9813 // Service is already running, so we can immediately
9814 // publish the connection.
9815 try {
9816 c.conn.connected(s.name, b.intent.binder);
9817 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009818 Slog.w(TAG, "Failure sending service " + s.shortName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009819 + " to connection " + c.conn.asBinder()
9820 + " (in " + c.binding.client.processName + ")", e);
9821 }
9822
9823 // If this is the first app connected back to this binding,
9824 // and the service had previously asked to be told when
9825 // rebound, then do so.
9826 if (b.intent.apps.size() == 1 && b.intent.doRebind) {
9827 requestServiceBindingLocked(s, b.intent, true);
9828 }
9829 } else if (!b.intent.requested) {
9830 requestServiceBindingLocked(s, b.intent, false);
9831 }
9832
9833 Binder.restoreCallingIdentity(origId);
9834 }
9835
9836 return 1;
9837 }
9838
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009839 void removeConnectionLocked(
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009840 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 IBinder binder = c.conn.asBinder();
9842 AppBindRecord b = c.binding;
9843 ServiceRecord s = b.service;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009844 ArrayList<ConnectionRecord> clist = s.connections.get(binder);
9845 if (clist != null) {
9846 clist.remove(c);
9847 if (clist.size() == 0) {
9848 s.connections.remove(binder);
9849 }
9850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 b.connections.remove(c);
9852 if (c.activity != null && c.activity != skipAct) {
9853 if (c.activity.connections != null) {
9854 c.activity.connections.remove(c);
9855 }
9856 }
9857 if (b.client != skipApp) {
9858 b.client.connections.remove(c);
9859 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009860 clist = mServiceConnections.get(binder);
9861 if (clist != null) {
9862 clist.remove(c);
9863 if (clist.size() == 0) {
9864 mServiceConnections.remove(binder);
9865 }
9866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867
9868 if (b.connections.size() == 0) {
9869 b.intent.apps.remove(b.client);
9870 }
9871
Joe Onorato8a9b2202010-02-26 18:56:32 -08009872 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 + ": shouldUnbind=" + b.intent.hasBound);
9874 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0
9875 && b.intent.hasBound) {
9876 try {
Dianne Hackborn287952c2010-09-22 22:34:31 -07009877 bumpServiceExecutingLocked(s, "unbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 updateOomAdjLocked(s.app);
9879 b.intent.hasBound = false;
9880 // Assume the client doesn't want to know about a rebind;
9881 // we will deal with that later if it asks for one.
9882 b.intent.doRebind = false;
9883 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent());
9884 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009885 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 serviceDoneExecutingLocked(s, true);
9887 }
9888 }
9889
9890 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
9891 bringDownServiceLocked(s, false);
9892 }
9893 }
9894
9895 public boolean unbindService(IServiceConnection connection) {
9896 synchronized (this) {
9897 IBinder binder = connection.asBinder();
Joe Onorato8a9b2202010-02-26 18:56:32 -08009898 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder);
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009899 ArrayList<ConnectionRecord> clist = mServiceConnections.get(binder);
9900 if (clist == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009901 Slog.w(TAG, "Unbind failed: could not find connection for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 + connection.asBinder());
9903 return false;
9904 }
9905
9906 final long origId = Binder.clearCallingIdentity();
9907
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009908 while (clist.size() > 0) {
9909 ConnectionRecord r = clist.get(0);
9910 removeConnectionLocked(r, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009911
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009912 if (r.binding.service.app != null) {
9913 // This could have made the service less important.
9914 updateOomAdjLocked(r.binding.service.app);
9915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 }
9917
9918 Binder.restoreCallingIdentity(origId);
9919 }
9920
9921 return true;
9922 }
9923
9924 public void publishService(IBinder token, Intent intent, IBinder service) {
9925 // Refuse possible leaked file descriptors
9926 if (intent != null && intent.hasFileDescriptors() == true) {
9927 throw new IllegalArgumentException("File descriptors passed in Intent");
9928 }
9929
9930 synchronized(this) {
9931 if (!(token instanceof ServiceRecord)) {
9932 throw new IllegalArgumentException("Invalid service token");
9933 }
9934 ServiceRecord r = (ServiceRecord)token;
9935
9936 final long origId = Binder.clearCallingIdentity();
9937
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009938 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009939 + " " + intent + ": " + service);
9940 if (r != null) {
9941 Intent.FilterComparison filter
9942 = new Intent.FilterComparison(intent);
9943 IntentBindRecord b = r.bindings.get(filter);
9944 if (b != null && !b.received) {
9945 b.binder = service;
9946 b.requested = true;
9947 b.received = true;
9948 if (r.connections.size() > 0) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009949 Iterator<ArrayList<ConnectionRecord>> it
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950 = r.connections.values().iterator();
9951 while (it.hasNext()) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -07009952 ArrayList<ConnectionRecord> clist = it.next();
9953 for (int i=0; i<clist.size(); i++) {
9954 ConnectionRecord c = clist.get(i);
9955 if (!filter.equals(c.binding.intent.intent)) {
9956 if (DEBUG_SERVICE) Slog.v(
9957 TAG, "Not publishing to: " + c);
9958 if (DEBUG_SERVICE) Slog.v(
9959 TAG, "Bound intent: " + c.binding.intent.intent);
9960 if (DEBUG_SERVICE) Slog.v(
9961 TAG, "Published intent: " + intent);
9962 continue;
9963 }
9964 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c);
9965 try {
9966 c.conn.connected(r.name, service);
9967 } catch (Exception e) {
9968 Slog.w(TAG, "Failure sending service " + r.name +
9969 " to connection " + c.conn.asBinder() +
9970 " (in " + c.binding.client.processName + ")", e);
9971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009972 }
9973 }
9974 }
9975 }
9976
9977 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
9978
9979 Binder.restoreCallingIdentity(origId);
9980 }
9981 }
9982 }
9983
9984 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
9985 // Refuse possible leaked file descriptors
9986 if (intent != null && intent.hasFileDescriptors() == true) {
9987 throw new IllegalArgumentException("File descriptors passed in Intent");
9988 }
9989
9990 synchronized(this) {
9991 if (!(token instanceof ServiceRecord)) {
9992 throw new IllegalArgumentException("Invalid service token");
9993 }
9994 ServiceRecord r = (ServiceRecord)token;
9995
9996 final long origId = Binder.clearCallingIdentity();
9997
9998 if (r != null) {
9999 Intent.FilterComparison filter
10000 = new Intent.FilterComparison(intent);
10001 IntentBindRecord b = r.bindings.get(filter);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010002 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010003 + " at " + b + ": apps="
10004 + (b != null ? b.apps.size() : 0));
10005 if (b != null) {
10006 if (b.apps.size() > 0) {
10007 // Applications have already bound since the last
10008 // unbind, so just rebind right here.
10009 requestServiceBindingLocked(r, b, true);
10010 } else {
10011 // Note to tell the service the next time there is
10012 // a new client.
10013 b.doRebind = true;
10014 }
10015 }
10016
10017 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
10018
10019 Binder.restoreCallingIdentity(origId);
10020 }
10021 }
10022 }
10023
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010024 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010025 synchronized(this) {
10026 if (!(token instanceof ServiceRecord)) {
10027 throw new IllegalArgumentException("Invalid service token");
10028 }
10029 ServiceRecord r = (ServiceRecord)token;
10030 boolean inStopping = mStoppingServices.contains(token);
10031 if (r != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010032 if (r != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010033 Slog.w(TAG, "Done executing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010034 + " with incorrect token: given " + token
10035 + ", expected " + r);
10036 return;
10037 }
10038
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -070010039 if (type == 1) {
10040 // This is a call from a service start... take care of
10041 // book-keeping.
10042 r.callStart = true;
10043 switch (res) {
10044 case Service.START_STICKY_COMPATIBILITY:
10045 case Service.START_STICKY: {
10046 // We are done with the associated start arguments.
10047 r.findDeliveredStart(startId, true);
10048 // Don't stop if killed.
10049 r.stopIfKilled = false;
10050 break;
10051 }
10052 case Service.START_NOT_STICKY: {
10053 // We are done with the associated start arguments.
10054 r.findDeliveredStart(startId, true);
10055 if (r.lastStartId == startId) {
10056 // There is no more work, and this service
10057 // doesn't want to hang around if killed.
10058 r.stopIfKilled = true;
10059 }
10060 break;
10061 }
10062 case Service.START_REDELIVER_INTENT: {
10063 // We'll keep this item until they explicitly
10064 // call stop for it, but keep track of the fact
10065 // that it was delivered.
10066 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
10067 if (si != null) {
10068 si.deliveryCount = 0;
10069 si.doneExecutingCount++;
10070 // Don't stop if killed.
10071 r.stopIfKilled = true;
10072 }
10073 break;
10074 }
10075 default:
10076 throw new IllegalArgumentException(
10077 "Unknown service start result: " + res);
10078 }
10079 if (res == Service.START_STICKY_COMPATIBILITY) {
10080 r.callStart = false;
10081 }
10082 }
10083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010084 final long origId = Binder.clearCallingIdentity();
10085 serviceDoneExecutingLocked(r, inStopping);
10086 Binder.restoreCallingIdentity(origId);
10087 } else {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070010088 Slog.w(TAG, "Done executing unknown service from pid "
10089 + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010090 }
10091 }
10092 }
10093
10094 public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070010095 if (DEBUG_SERVICE) Slog.v(TAG, "<<< DONE EXECUTING " + r
10096 + ": nesting=" + r.executeNesting
10097 + ", inStopping=" + inStopping + ", app=" + r.app);
Dianne Hackborn287952c2010-09-22 22:34:31 -070010098 else if (DEBUG_SERVICE_EXECUTING) Slog.v(TAG, "<<< DONE EXECUTING " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010099 r.executeNesting--;
10100 if (r.executeNesting <= 0 && r.app != null) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070010101 if (DEBUG_SERVICE) Slog.v(TAG,
10102 "Nesting at 0 of " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010103 r.app.executingServices.remove(r);
10104 if (r.app.executingServices.size() == 0) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070010105 if (DEBUG_SERVICE || DEBUG_SERVICE_EXECUTING) Slog.v(TAG,
10106 "No more executingServices of " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010107 mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app);
10108 }
10109 if (inStopping) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070010110 if (DEBUG_SERVICE) Slog.v(TAG,
10111 "doneExecuting remove stopping " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010112 mStoppingServices.remove(r);
Mattias Petersson3996b412010-10-27 09:32:51 +020010113 r.bindings.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010114 }
10115 updateOomAdjLocked(r.app);
10116 }
10117 }
10118
10119 void serviceTimeout(ProcessRecord proc) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010120 String anrMessage = null;
10121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010122 synchronized(this) {
10123 if (proc.executingServices.size() == 0 || proc.thread == null) {
10124 return;
10125 }
10126 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT;
10127 Iterator<ServiceRecord> it = proc.executingServices.iterator();
10128 ServiceRecord timeout = null;
10129 long nextTime = 0;
10130 while (it.hasNext()) {
10131 ServiceRecord sr = it.next();
10132 if (sr.executingStart < maxTime) {
10133 timeout = sr;
10134 break;
10135 }
10136 if (sr.executingStart > nextTime) {
10137 nextTime = sr.executingStart;
10138 }
10139 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010140 if (timeout != null && mLruProcesses.contains(proc)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010141 Slog.w(TAG, "Timeout executing service: " + timeout);
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010142 anrMessage = "Executing service " + timeout.shortName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 } else {
10144 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
10145 msg.obj = proc;
10146 mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT);
10147 }
10148 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010149
10150 if (anrMessage != null) {
10151 appNotResponding(proc, null, null, anrMessage);
10152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010153 }
10154
10155 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -070010156 // BACKUP AND RESTORE
10157 // =========================================================
10158
10159 // Cause the target app to be launched if necessary and its backup agent
10160 // instantiated. The backup agent will invoke backupAgentCreated() on the
10161 // activity manager to announce its creation.
10162 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010163 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -070010164 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
10165
10166 synchronized(this) {
10167 // !!! TODO: currently no check here that we're already bound
10168 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
10169 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
10170 synchronized (stats) {
10171 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
10172 }
10173
10174 BackupRecord r = new BackupRecord(ss, app, backupMode);
10175 ComponentName hostingName = new ComponentName(app.packageName, app.backupAgentName);
10176 // startProcessLocked() returns existing proc's record if it's already running
10177 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborn9acc0302009-08-25 00:27:12 -070010178 false, 0, "backup", hostingName, false);
Christopher Tate181fafa2009-05-14 11:12:14 -070010179 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010180 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -070010181 return false;
10182 }
10183
10184 r.app = proc;
10185 mBackupTarget = r;
10186 mBackupAppName = app.packageName;
10187
Christopher Tate6fa95972009-06-05 18:43:55 -070010188 // Try not to kill the process during backup
10189 updateOomAdjLocked(proc);
10190
Christopher Tate181fafa2009-05-14 11:12:14 -070010191 // If the process is already attached, schedule the creation of the backup agent now.
10192 // If it is not yet live, this will be done when it attaches to the framework.
10193 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010194 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -070010195 try {
10196 proc.thread.scheduleCreateBackupAgent(app, backupMode);
10197 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -070010198 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -070010199 }
10200 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010201 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -070010202 }
10203 // Invariants: at this point, the target app process exists and the application
10204 // is either already running or in the process of coming up. mBackupTarget and
10205 // mBackupAppName describe the app, so that when it binds back to the AM we
10206 // know that it's scheduled for a backup-agent operation.
10207 }
10208
10209 return true;
10210 }
10211
10212 // A backup agent has just come up
10213 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010214 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -070010215 + " = " + agent);
10216
10217 synchronized(this) {
10218 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010219 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -070010220 return;
10221 }
Dianne Hackborn06740692010-09-22 22:46:21 -070010222 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010223
Dianne Hackborn06740692010-09-22 22:46:21 -070010224 long oldIdent = Binder.clearCallingIdentity();
10225 try {
10226 IBackupManager bm = IBackupManager.Stub.asInterface(
10227 ServiceManager.getService(Context.BACKUP_SERVICE));
10228 bm.agentConnected(agentPackageName, agent);
10229 } catch (RemoteException e) {
10230 // can't happen; the backup manager service is local
10231 } catch (Exception e) {
10232 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
10233 e.printStackTrace();
10234 } finally {
10235 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -070010236 }
10237 }
10238
10239 // done with this agent
10240 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010241 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -070010242 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010243 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -070010244 return;
10245 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010246
10247 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -070010248 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010249 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -070010250 return;
10251 }
10252
Christopher Tate181fafa2009-05-14 11:12:14 -070010253 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010254 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -070010255 return;
10256 }
10257
Christopher Tate6fa95972009-06-05 18:43:55 -070010258 ProcessRecord proc = mBackupTarget.app;
10259 mBackupTarget = null;
10260 mBackupAppName = null;
10261
10262 // Not backing this app up any more; reset its OOM adjustment
10263 updateOomAdjLocked(proc);
10264
Christopher Tatec7b31e32009-06-10 15:49:30 -070010265 // If the app crashed during backup, 'thread' will be null here
10266 if (proc.thread != null) {
10267 try {
10268 proc.thread.scheduleDestroyBackupAgent(appInfo);
10269 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010270 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -070010271 e.printStackTrace();
10272 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010273 }
Christopher Tate181fafa2009-05-14 11:12:14 -070010274 }
10275 }
10276 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 // BROADCASTS
10278 // =========================================================
10279
Josh Bartel7f208742010-02-25 11:01:44 -060010280 private final List getStickiesLocked(String action, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010281 List cur) {
10282 final ContentResolver resolver = mContext.getContentResolver();
10283 final ArrayList<Intent> list = mStickyBroadcasts.get(action);
10284 if (list == null) {
10285 return cur;
10286 }
10287 int N = list.size();
10288 for (int i=0; i<N; i++) {
10289 Intent intent = list.get(i);
10290 if (filter.match(resolver, intent, true, TAG) >= 0) {
10291 if (cur == null) {
10292 cur = new ArrayList<Intent>();
10293 }
10294 cur.add(intent);
10295 }
10296 }
10297 return cur;
10298 }
10299
10300 private final void scheduleBroadcastsLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010301 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 + mBroadcastsScheduled);
10303
10304 if (mBroadcastsScheduled) {
10305 return;
10306 }
10307 mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG);
10308 mBroadcastsScheduled = true;
10309 }
10310
10311 public Intent registerReceiver(IApplicationThread caller,
10312 IIntentReceiver receiver, IntentFilter filter, String permission) {
10313 synchronized(this) {
10314 ProcessRecord callerApp = null;
10315 if (caller != null) {
10316 callerApp = getRecordForAppLocked(caller);
10317 if (callerApp == null) {
10318 throw new SecurityException(
10319 "Unable to find app for caller " + caller
10320 + " (pid=" + Binder.getCallingPid()
10321 + ") when registering receiver " + receiver);
10322 }
10323 }
10324
10325 List allSticky = null;
10326
10327 // Look for any matching sticky broadcasts...
10328 Iterator actions = filter.actionsIterator();
10329 if (actions != null) {
10330 while (actions.hasNext()) {
10331 String action = (String)actions.next();
Josh Bartel7f208742010-02-25 11:01:44 -060010332 allSticky = getStickiesLocked(action, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010333 }
10334 } else {
Josh Bartel7f208742010-02-25 11:01:44 -060010335 allSticky = getStickiesLocked(null, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 }
10337
10338 // The first sticky in the list is returned directly back to
10339 // the client.
10340 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
10341
Joe Onorato8a9b2202010-02-26 18:56:32 -080010342 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 + ": " + sticky);
10344
10345 if (receiver == null) {
10346 return sticky;
10347 }
10348
10349 ReceiverList rl
10350 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
10351 if (rl == null) {
10352 rl = new ReceiverList(this, callerApp,
10353 Binder.getCallingPid(),
10354 Binder.getCallingUid(), receiver);
10355 if (rl.app != null) {
10356 rl.app.receivers.add(rl);
10357 } else {
10358 try {
10359 receiver.asBinder().linkToDeath(rl, 0);
10360 } catch (RemoteException e) {
10361 return sticky;
10362 }
10363 rl.linkedToDeath = true;
10364 }
10365 mRegisteredReceivers.put(receiver.asBinder(), rl);
10366 }
10367 BroadcastFilter bf = new BroadcastFilter(filter, rl, permission);
10368 rl.add(bf);
10369 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010370 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010371 }
10372 mReceiverResolver.addFilter(bf);
10373
10374 // Enqueue broadcasts for all existing stickies that match
10375 // this filter.
10376 if (allSticky != null) {
10377 ArrayList receivers = new ArrayList();
10378 receivers.add(bf);
10379
10380 int N = allSticky.size();
10381 for (int i=0; i<N; i++) {
10382 Intent intent = (Intent)allSticky.get(i);
10383 BroadcastRecord r = new BroadcastRecord(intent, null,
10384 null, -1, -1, null, receivers, null, 0, null, null,
Dianne Hackborn12527f92009-11-11 17:39:50 -080010385 false, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010386 if (mParallelBroadcasts.size() == 0) {
10387 scheduleBroadcastsLocked();
10388 }
10389 mParallelBroadcasts.add(r);
10390 }
10391 }
10392
10393 return sticky;
10394 }
10395 }
10396
10397 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010398 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010399
10400 boolean doNext = false;
10401
10402 synchronized(this) {
10403 ReceiverList rl
10404 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
10405 if (rl != null) {
10406 if (rl.curBroadcast != null) {
10407 BroadcastRecord r = rl.curBroadcast;
10408 doNext = finishReceiverLocked(
10409 receiver.asBinder(), r.resultCode, r.resultData,
10410 r.resultExtras, r.resultAbort, true);
10411 }
10412
10413 if (rl.app != null) {
10414 rl.app.receivers.remove(rl);
10415 }
10416 removeReceiverLocked(rl);
10417 if (rl.linkedToDeath) {
10418 rl.linkedToDeath = false;
10419 rl.receiver.asBinder().unlinkToDeath(rl, 0);
10420 }
10421 }
10422 }
10423
10424 if (!doNext) {
10425 return;
10426 }
10427
10428 final long origId = Binder.clearCallingIdentity();
10429 processNextBroadcast(false);
10430 trimApplications();
10431 Binder.restoreCallingIdentity(origId);
10432 }
10433
10434 void removeReceiverLocked(ReceiverList rl) {
10435 mRegisteredReceivers.remove(rl.receiver.asBinder());
10436 int N = rl.size();
10437 for (int i=0; i<N; i++) {
10438 mReceiverResolver.removeFilter(rl.get(i));
10439 }
10440 }
10441
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070010442 private final void sendPackageBroadcastLocked(int cmd, String[] packages) {
10443 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
10444 ProcessRecord r = mLruProcesses.get(i);
10445 if (r.thread != null) {
10446 try {
10447 r.thread.dispatchPackageBroadcast(cmd, packages);
10448 } catch (RemoteException ex) {
10449 }
10450 }
10451 }
10452 }
10453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010454 private final int broadcastIntentLocked(ProcessRecord callerApp,
10455 String callerPackage, Intent intent, String resolvedType,
10456 IIntentReceiver resultTo, int resultCode, String resultData,
10457 Bundle map, String requiredPermission,
10458 boolean ordered, boolean sticky, int callingPid, int callingUid) {
10459 intent = new Intent(intent);
10460
Joe Onorato8a9b2202010-02-26 18:56:32 -080010461 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
10463 + " ordered=" + ordered);
10464 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010465 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 }
10467
10468 // Handle special intents: if this broadcast is from the package
10469 // manager about a package being removed, we need to remove all of
10470 // its activities from the history stack.
10471 final boolean uidRemoved = intent.ACTION_UID_REMOVED.equals(
10472 intent.getAction());
10473 if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
10474 || intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080010475 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 || uidRemoved) {
10477 if (checkComponentPermission(
10478 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
10479 callingPid, callingUid, -1)
10480 == PackageManager.PERMISSION_GRANTED) {
10481 if (uidRemoved) {
10482 final Bundle intentExtras = intent.getExtras();
10483 final int uid = intentExtras != null
10484 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
10485 if (uid >= 0) {
10486 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
10487 synchronized (bs) {
10488 bs.removeUidStatsLocked(uid);
10489 }
10490 }
10491 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010492 // If resources are unvailble just force stop all
10493 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080010494 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010495 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
10496 if (list != null && (list.length > 0)) {
10497 for (String pkg : list) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080010498 forceStopPackageLocked(pkg, -1, false, true, true);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010499 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070010500 sendPackageBroadcastLocked(
10501 IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010502 }
10503 } else {
10504 Uri data = intent.getData();
10505 String ssp;
10506 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
10507 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
10508 forceStopPackageLocked(ssp,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080010509 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true);
Dianne Hackbornde7faf62009-06-30 13:27:30 -070010510 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -070010511 if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
10512 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
10513 new String[] {ssp});
10514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 }
10516 }
10517 }
10518 } else {
10519 String msg = "Permission Denial: " + intent.getAction()
10520 + " broadcast from " + callerPackage + " (pid=" + callingPid
10521 + ", uid=" + callingUid + ")"
10522 + " requires "
10523 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010524 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010525 throw new SecurityException(msg);
10526 }
10527 }
10528
10529 /*
10530 * If this is the time zone changed action, queue up a message that will reset the timezone
10531 * of all currently running processes. This message will get queued up before the broadcast
10532 * happens.
10533 */
10534 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
10535 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
10536 }
10537
Robert Greenwalt03595d02010-11-02 14:08:23 -070010538 if (intent.ACTION_CLEAR_DNS_CACHE.equals(intent.getAction())) {
10539 mHandler.sendEmptyMessage(CLEAR_DNS_CACHE);
10540 }
10541
Robert Greenwalt434203a2010-10-11 16:00:27 -070010542 if (Proxy.PROXY_CHANGE_ACTION.equals(intent.getAction())) {
10543 ProxyProperties proxy = intent.getParcelableExtra("proxy");
10544 mHandler.sendMessage(mHandler.obtainMessage(UPDATE_HTTP_PROXY, proxy));
10545 }
10546
Dianne Hackborn854060af2009-07-09 18:14:31 -070010547 /*
10548 * Prevent non-system code (defined here to be non-persistent
10549 * processes) from sending protected broadcasts.
10550 */
10551 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
10552 || callingUid == Process.SHELL_UID || callingUid == 0) {
10553 // Always okay.
10554 } else if (callerApp == null || !callerApp.persistent) {
10555 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070010556 if (AppGlobals.getPackageManager().isProtectedBroadcast(
Dianne Hackborn854060af2009-07-09 18:14:31 -070010557 intent.getAction())) {
10558 String msg = "Permission Denial: not allowed to send broadcast "
10559 + intent.getAction() + " from pid="
10560 + callingPid + ", uid=" + callingUid;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010561 Slog.w(TAG, msg);
Dianne Hackborn854060af2009-07-09 18:14:31 -070010562 throw new SecurityException(msg);
10563 }
10564 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010565 Slog.w(TAG, "Remote exception", e);
Dianne Hackborn854060af2009-07-09 18:14:31 -070010566 return BROADCAST_SUCCESS;
10567 }
10568 }
10569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010570 // Add to the sticky list if requested.
10571 if (sticky) {
10572 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
10573 callingPid, callingUid)
10574 != PackageManager.PERMISSION_GRANTED) {
10575 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
10576 + callingPid + ", uid=" + callingUid
10577 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010578 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010579 throw new SecurityException(msg);
10580 }
10581 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010582 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010583 + " and enforce permission " + requiredPermission);
10584 return BROADCAST_STICKY_CANT_HAVE_PERMISSION;
10585 }
10586 if (intent.getComponent() != null) {
10587 throw new SecurityException(
10588 "Sticky broadcasts can't target a specific component");
10589 }
10590 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
10591 if (list == null) {
10592 list = new ArrayList<Intent>();
10593 mStickyBroadcasts.put(intent.getAction(), list);
10594 }
10595 int N = list.size();
10596 int i;
10597 for (i=0; i<N; i++) {
10598 if (intent.filterEquals(list.get(i))) {
10599 // This sticky already exists, replace it.
10600 list.set(i, new Intent(intent));
10601 break;
10602 }
10603 }
10604 if (i >= N) {
10605 list.add(new Intent(intent));
10606 }
10607 }
10608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010609 // Figure out who all will receive this broadcast.
10610 List receivers = null;
10611 List<BroadcastFilter> registeredReceivers = null;
10612 try {
10613 if (intent.getComponent() != null) {
10614 // Broadcast is going to one specific receiver class...
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070010615 ActivityInfo ai = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -070010616 getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010617 if (ai != null) {
10618 receivers = new ArrayList();
10619 ResolveInfo ri = new ResolveInfo();
10620 ri.activityInfo = ai;
10621 receivers.add(ri);
10622 }
10623 } else {
10624 // Need to resolve the intent to interested receivers...
10625 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
10626 == 0) {
10627 receivers =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070010628 AppGlobals.getPackageManager().queryIntentReceivers(
Dianne Hackborn1655be42009-05-08 14:29:01 -070010629 intent, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010630 }
Mihai Preda074edef2009-05-18 17:13:31 +020010631 registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010632 }
10633 } catch (RemoteException ex) {
10634 // pm is in same process, this will never happen.
10635 }
10636
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010637 final boolean replacePending =
10638 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
10639
Joe Onorato8a9b2202010-02-26 18:56:32 -080010640 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010641 + " replacePending=" + replacePending);
10642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
10644 if (!ordered && NR > 0) {
10645 // If we are not serializing this broadcast, then send the
10646 // registered receivers separately so they don't wait for the
10647 // components to be launched.
10648 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
10649 callerPackage, callingPid, callingUid, requiredPermission,
10650 registeredReceivers, resultTo, resultCode, resultData, map,
Dianne Hackborn12527f92009-11-11 17:39:50 -080010651 ordered, sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010652 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 TAG, "Enqueueing parallel broadcast " + r
10654 + ": prev had " + mParallelBroadcasts.size());
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010655 boolean replaced = false;
10656 if (replacePending) {
10657 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
10658 if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010659 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010660 "***** DROPPING PARALLEL: " + intent);
10661 mParallelBroadcasts.set(i, r);
10662 replaced = true;
10663 break;
10664 }
10665 }
10666 }
10667 if (!replaced) {
10668 mParallelBroadcasts.add(r);
10669 scheduleBroadcastsLocked();
10670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 registeredReceivers = null;
10672 NR = 0;
10673 }
10674
10675 // Merge into one list.
10676 int ir = 0;
10677 if (receivers != null) {
10678 // A special case for PACKAGE_ADDED: do not allow the package
10679 // being added to see this broadcast. This prevents them from
10680 // using this as a back door to get run as soon as they are
10681 // installed. Maybe in the future we want to have a special install
10682 // broadcast or such for apps, but we'd like to deliberately make
10683 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010684 String skipPackages[] = null;
10685 if (intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
10686 || intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
10687 || intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
10688 Uri data = intent.getData();
10689 if (data != null) {
10690 String pkgName = data.getSchemeSpecificPart();
10691 if (pkgName != null) {
10692 skipPackages = new String[] { pkgName };
10693 }
10694 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -080010695 } else if (intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010696 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -070010697 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -080010698 if (skipPackages != null && (skipPackages.length > 0)) {
10699 for (String skipPackage : skipPackages) {
10700 if (skipPackage != null) {
10701 int NT = receivers.size();
10702 for (int it=0; it<NT; it++) {
10703 ResolveInfo curt = (ResolveInfo)receivers.get(it);
10704 if (curt.activityInfo.packageName.equals(skipPackage)) {
10705 receivers.remove(it);
10706 it--;
10707 NT--;
10708 }
10709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010710 }
10711 }
10712 }
10713
10714 int NT = receivers != null ? receivers.size() : 0;
10715 int it = 0;
10716 ResolveInfo curt = null;
10717 BroadcastFilter curr = null;
10718 while (it < NT && ir < NR) {
10719 if (curt == null) {
10720 curt = (ResolveInfo)receivers.get(it);
10721 }
10722 if (curr == null) {
10723 curr = registeredReceivers.get(ir);
10724 }
10725 if (curr.getPriority() >= curt.priority) {
10726 // Insert this broadcast record into the final list.
10727 receivers.add(it, curr);
10728 ir++;
10729 curr = null;
10730 it++;
10731 NT++;
10732 } else {
10733 // Skip to the next ResolveInfo in the final list.
10734 it++;
10735 curt = null;
10736 }
10737 }
10738 }
10739 while (ir < NR) {
10740 if (receivers == null) {
10741 receivers = new ArrayList();
10742 }
10743 receivers.add(registeredReceivers.get(ir));
10744 ir++;
10745 }
10746
10747 if ((receivers != null && receivers.size() > 0)
10748 || resultTo != null) {
10749 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
10750 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -080010751 receivers, resultTo, resultCode, resultData, map, ordered,
10752 sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010753 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010754 TAG, "Enqueueing ordered broadcast " + r
10755 + ": prev had " + mOrderedBroadcasts.size());
10756 if (DEBUG_BROADCAST) {
10757 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010758 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010759 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010760 boolean replaced = false;
10761 if (replacePending) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010762 for (int i=mOrderedBroadcasts.size()-1; i>0; i--) {
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010763 if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010764 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010765 "***** DROPPING ORDERED: " + intent);
10766 mOrderedBroadcasts.set(i, r);
10767 replaced = true;
10768 break;
10769 }
10770 }
10771 }
10772 if (!replaced) {
10773 mOrderedBroadcasts.add(r);
10774 scheduleBroadcastsLocked();
10775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010776 }
10777
10778 return BROADCAST_SUCCESS;
10779 }
10780
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010781 final Intent verifyBroadcastLocked(Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 // Refuse possible leaked file descriptors
10783 if (intent != null && intent.hasFileDescriptors() == true) {
10784 throw new IllegalArgumentException("File descriptors passed in Intent");
10785 }
10786
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010787 int flags = intent.getFlags();
10788
10789 if (!mProcessesReady) {
10790 // if the caller really truly claims to know what they're doing, go
10791 // ahead and allow the broadcast without launching any receivers
10792 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
10793 intent = new Intent(intent);
10794 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
10795 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) {
10796 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
10797 + " before boot completion");
10798 throw new IllegalStateException("Cannot broadcast before boot completed");
10799 }
10800 }
10801
10802 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
10803 throw new IllegalArgumentException(
10804 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
10805 }
10806
10807 return intent;
10808 }
10809
10810 public final int broadcastIntent(IApplicationThread caller,
10811 Intent intent, String resolvedType, IIntentReceiver resultTo,
10812 int resultCode, String resultData, Bundle map,
10813 String requiredPermission, boolean serialized, boolean sticky) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010814 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010815 intent = verifyBroadcastLocked(intent);
Dianne Hackborn9acc0302009-08-25 00:27:12 -070010816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010817 final ProcessRecord callerApp = getRecordForAppLocked(caller);
10818 final int callingPid = Binder.getCallingPid();
10819 final int callingUid = Binder.getCallingUid();
10820 final long origId = Binder.clearCallingIdentity();
10821 int res = broadcastIntentLocked(callerApp,
10822 callerApp != null ? callerApp.info.packageName : null,
10823 intent, resolvedType, resultTo,
10824 resultCode, resultData, map, requiredPermission, serialized,
10825 sticky, callingPid, callingUid);
10826 Binder.restoreCallingIdentity(origId);
10827 return res;
10828 }
10829 }
10830
10831 int broadcastIntentInPackage(String packageName, int uid,
10832 Intent intent, String resolvedType, IIntentReceiver resultTo,
10833 int resultCode, String resultData, Bundle map,
10834 String requiredPermission, boolean serialized, boolean sticky) {
10835 synchronized(this) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070010836 intent = verifyBroadcastLocked(intent);
10837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838 final long origId = Binder.clearCallingIdentity();
10839 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
10840 resultTo, resultCode, resultData, map, requiredPermission,
10841 serialized, sticky, -1, uid);
10842 Binder.restoreCallingIdentity(origId);
10843 return res;
10844 }
10845 }
10846
10847 public final void unbroadcastIntent(IApplicationThread caller,
10848 Intent intent) {
10849 // Refuse possible leaked file descriptors
10850 if (intent != null && intent.hasFileDescriptors() == true) {
10851 throw new IllegalArgumentException("File descriptors passed in Intent");
10852 }
10853
10854 synchronized(this) {
10855 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
10856 != PackageManager.PERMISSION_GRANTED) {
10857 String msg = "Permission Denial: unbroadcastIntent() from pid="
10858 + Binder.getCallingPid()
10859 + ", uid=" + Binder.getCallingUid()
10860 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010861 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010862 throw new SecurityException(msg);
10863 }
10864 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
10865 if (list != null) {
10866 int N = list.size();
10867 int i;
10868 for (i=0; i<N; i++) {
10869 if (intent.filterEquals(list.get(i))) {
10870 list.remove(i);
10871 break;
10872 }
10873 }
10874 }
10875 }
10876 }
10877
10878 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
10879 String resultData, Bundle resultExtras, boolean resultAbort,
10880 boolean explicit) {
10881 if (mOrderedBroadcasts.size() == 0) {
10882 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010883 Slog.w(TAG, "finishReceiver called but no pending broadcasts");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010884 }
10885 return false;
10886 }
10887 BroadcastRecord r = mOrderedBroadcasts.get(0);
10888 if (r.receiver == null) {
10889 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010890 Slog.w(TAG, "finishReceiver called but none active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010891 }
10892 return false;
10893 }
10894 if (r.receiver != receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010895 Slog.w(TAG, "finishReceiver called but active receiver is different");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 return false;
10897 }
10898 int state = r.state;
10899 r.state = r.IDLE;
10900 if (state == r.IDLE) {
10901 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010902 Slog.w(TAG, "finishReceiver called but state is IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010903 }
10904 }
10905 r.receiver = null;
10906 r.intent.setComponent(null);
10907 if (r.curApp != null) {
10908 r.curApp.curReceiver = null;
10909 }
10910 if (r.curFilter != null) {
10911 r.curFilter.receiverList.curBroadcast = null;
10912 }
10913 r.curFilter = null;
10914 r.curApp = null;
10915 r.curComponent = null;
10916 r.curReceiver = null;
10917 mPendingBroadcast = null;
10918
10919 r.resultCode = resultCode;
10920 r.resultData = resultData;
10921 r.resultExtras = resultExtras;
10922 r.resultAbort = resultAbort;
10923
10924 // We will process the next receiver right now if this is finishing
10925 // an app receiver (which is always asynchronous) or after we have
10926 // come back from calling a receiver.
10927 return state == BroadcastRecord.APP_RECEIVE
10928 || state == BroadcastRecord.CALL_DONE_RECEIVE;
10929 }
10930
10931 public void finishReceiver(IBinder who, int resultCode, String resultData,
10932 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010933 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010934
10935 // Refuse possible leaked file descriptors
10936 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
10937 throw new IllegalArgumentException("File descriptors passed in Bundle");
10938 }
10939
10940 boolean doNext;
10941
10942 final long origId = Binder.clearCallingIdentity();
10943
10944 synchronized(this) {
10945 doNext = finishReceiverLocked(
10946 who, resultCode, resultData, resultExtras, resultAbort, true);
10947 }
10948
10949 if (doNext) {
10950 processNextBroadcast(false);
10951 }
10952 trimApplications();
10953
10954 Binder.restoreCallingIdentity(origId);
10955 }
10956
Jeff Brown4d94a762010-09-23 11:33:28 -070010957 private final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010958 if (r.nextReceiver > 0) {
10959 Object curReceiver = r.receivers.get(r.nextReceiver-1);
10960 if (curReceiver instanceof BroadcastFilter) {
10961 BroadcastFilter bf = (BroadcastFilter) curReceiver;
Doug Zongker2bec3d42009-12-04 12:52:44 -080010962 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010963 System.identityHashCode(r),
10964 r.intent.getAction(),
10965 r.nextReceiver - 1,
10966 System.identityHashCode(bf));
10967 } else {
Doug Zongker2bec3d42009-12-04 12:52:44 -080010968 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 System.identityHashCode(r),
10970 r.intent.getAction(),
10971 r.nextReceiver - 1,
10972 ((ResolveInfo)curReceiver).toString());
10973 }
10974 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010975 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010976 + r);
Doug Zongker2bec3d42009-12-04 12:52:44 -080010977 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010978 System.identityHashCode(r),
10979 r.intent.getAction(),
10980 r.nextReceiver,
10981 "NONE");
10982 }
10983 }
10984
Jeff Brown4d94a762010-09-23 11:33:28 -070010985 private final void setBroadcastTimeoutLocked(long timeoutTime) {
10986 if (! mPendingBroadcastTimeoutMessage) {
10987 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
10988 mHandler.sendMessageAtTime(msg, timeoutTime);
10989 mPendingBroadcastTimeoutMessage = true;
10990 }
10991 }
10992
10993 private final void cancelBroadcastTimeoutLocked() {
10994 if (mPendingBroadcastTimeoutMessage) {
10995 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG);
10996 mPendingBroadcastTimeoutMessage = false;
10997 }
10998 }
10999
11000 private final void broadcastTimeoutLocked(boolean fromMsg) {
11001 if (fromMsg) {
11002 mPendingBroadcastTimeoutMessage = false;
11003 }
11004
11005 if (mOrderedBroadcasts.size() == 0) {
11006 return;
11007 }
11008
11009 long now = SystemClock.uptimeMillis();
11010 BroadcastRecord r = mOrderedBroadcasts.get(0);
11011 if (fromMsg) {
11012 if (mDidDexOpt) {
11013 // Delay timeouts until dexopt finishes.
11014 mDidDexOpt = false;
11015 long timeoutTime = SystemClock.uptimeMillis() + BROADCAST_TIMEOUT;
11016 setBroadcastTimeoutLocked(timeoutTime);
11017 return;
11018 }
11019 if (! mProcessesReady) {
11020 // Only process broadcast timeouts if the system is ready. That way
11021 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
11022 // to do heavy lifting for system up.
11023 return;
11024 }
11025
11026 long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT;
11027 if (timeoutTime > now) {
11028 // We can observe premature timeouts because we do not cancel and reset the
11029 // broadcast timeout message after each receiver finishes. Instead, we set up
11030 // an initial timeout then kick it down the road a little further as needed
11031 // when it expires.
11032 if (DEBUG_BROADCAST) Slog.v(TAG,
11033 "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
11034 + timeoutTime);
11035 setBroadcastTimeoutLocked(timeoutTime);
11036 return;
11037 }
11038 }
11039
11040 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver
11041 + ", started " + (now - r.receiverTime) + "ms ago");
11042 r.receiverTime = now;
11043 r.anrCount++;
11044
11045 // Current receiver has passed its expiration date.
11046 if (r.nextReceiver <= 0) {
11047 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
11048 return;
11049 }
11050
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011051 ProcessRecord app = null;
11052 String anrMessage = null;
11053
Jeff Brown4d94a762010-09-23 11:33:28 -070011054 Object curReceiver = r.receivers.get(r.nextReceiver-1);
11055 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
11056 logBroadcastReceiverDiscardLocked(r);
11057 if (curReceiver instanceof BroadcastFilter) {
11058 BroadcastFilter bf = (BroadcastFilter)curReceiver;
11059 if (bf.receiverList.pid != 0
11060 && bf.receiverList.pid != MY_PID) {
11061 synchronized (this.mPidsSelfLocked) {
11062 app = this.mPidsSelfLocked.get(
11063 bf.receiverList.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011065 }
Jeff Brown4d94a762010-09-23 11:33:28 -070011066 } else {
11067 app = r.curApp;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011068 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011069
Jeff Brown4d94a762010-09-23 11:33:28 -070011070 if (app != null) {
11071 anrMessage = "Broadcast of " + r.intent.toString();
11072 }
11073
11074 if (mPendingBroadcast == r) {
11075 mPendingBroadcast = null;
11076 }
11077
11078 // Move on to the next receiver.
11079 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
11080 r.resultExtras, r.resultAbort, true);
11081 scheduleBroadcastsLocked();
11082
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011083 if (anrMessage != null) {
Jeff Brown4d94a762010-09-23 11:33:28 -070011084 // Post the ANR to the handler since we do not want to process ANRs while
11085 // potentially holding our lock.
11086 mHandler.post(new AppNotResponding(app, anrMessage));
Dianne Hackbornad5499d2010-03-29 18:08:45 -070011087 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011088 }
11089
11090 private final void processCurBroadcastLocked(BroadcastRecord r,
11091 ProcessRecord app) throws RemoteException {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011092 if (DEBUG_BROADCAST) Slog.v(TAG,
11093 "Process cur broadcast " + r + " for app " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011094 if (app.thread == null) {
11095 throw new RemoteException();
11096 }
11097 r.receiver = app.thread.asBinder();
11098 r.curApp = app;
11099 app.curReceiver = r;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011100 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011101
11102 // Tell the application to launch this receiver.
11103 r.intent.setComponent(r.curComponent);
11104
11105 boolean started = false;
11106 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011107 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011108 "Delivering to component " + r.curComponent
11109 + ": " + r);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -070011110 ensurePackageDexOpt(r.intent.getComponent().getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011111 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
11112 r.resultCode, r.resultData, r.resultExtras, r.ordered);
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011113 if (DEBUG_BROADCAST) Slog.v(TAG,
11114 "Process cur broadcast " + r + " DELIVERED for app " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 started = true;
11116 } finally {
11117 if (!started) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011118 if (DEBUG_BROADCAST) Slog.v(TAG,
11119 "Process cur broadcast " + r + ": NOT STARTED!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011120 r.receiver = null;
11121 r.curApp = null;
11122 app.curReceiver = null;
11123 }
11124 }
11125
11126 }
11127
Jeff Brown4d94a762010-09-23 11:33:28 -070011128 static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070011129 Intent intent, int resultCode, String data, Bundle extras,
11130 boolean ordered, boolean sticky) throws RemoteException {
Jeff Brown4d94a762010-09-23 11:33:28 -070011131 // Send the intent to the receiver asynchronously using one-way binder calls.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 if (app != null && app.thread != null) {
11133 // If we have an app thread, do the call through that so it is
11134 // correctly ordered with other one-way calls.
11135 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070011136 data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011137 } else {
Dianne Hackborn68d881c2009-10-05 13:58:17 -070011138 receiver.performReceive(intent, resultCode, data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011139 }
11140 }
11141
Jeff Brown4d94a762010-09-23 11:33:28 -070011142 private final void deliverToRegisteredReceiverLocked(BroadcastRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011143 BroadcastFilter filter, boolean ordered) {
11144 boolean skip = false;
11145 if (filter.requiredPermission != null) {
11146 int perm = checkComponentPermission(filter.requiredPermission,
11147 r.callingPid, r.callingUid, -1);
11148 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011149 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 + r.intent.toString()
11151 + " from " + r.callerPackage + " (pid="
11152 + r.callingPid + ", uid=" + r.callingUid + ")"
11153 + " requires " + filter.requiredPermission
11154 + " due to registered receiver " + filter);
11155 skip = true;
11156 }
11157 }
11158 if (r.requiredPermission != null) {
11159 int perm = checkComponentPermission(r.requiredPermission,
11160 filter.receiverList.pid, filter.receiverList.uid, -1);
11161 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011162 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011163 + r.intent.toString()
11164 + " to " + filter.receiverList.app
11165 + " (pid=" + filter.receiverList.pid
11166 + ", uid=" + filter.receiverList.uid + ")"
11167 + " requires " + r.requiredPermission
11168 + " due to sender " + r.callerPackage
11169 + " (uid " + r.callingUid + ")");
11170 skip = true;
11171 }
11172 }
11173
11174 if (!skip) {
11175 // If this is not being sent as an ordered broadcast, then we
11176 // don't want to touch the fields that keep track of the current
11177 // state of ordered broadcasts.
11178 if (ordered) {
11179 r.receiver = filter.receiverList.receiver.asBinder();
11180 r.curFilter = filter;
11181 filter.receiverList.curBroadcast = r;
11182 r.state = BroadcastRecord.CALL_IN_RECEIVE;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011183 if (filter.receiverList.app != null) {
11184 // Bump hosting application to no longer be in background
11185 // scheduling class. Note that we can't do that if there
11186 // isn't an app... but we can only be in that case for
11187 // things that directly call the IActivityManager API, which
11188 // are already core system stuff so don't matter for this.
11189 r.curApp = filter.receiverList.app;
11190 filter.receiverList.app.curReceiver = r;
11191 updateOomAdjLocked();
11192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 }
11194 try {
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011195 if (DEBUG_BROADCAST_LIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 int seq = r.intent.getIntExtra("seq", -1);
Dianne Hackborn399cccb2010-04-13 22:57:49 -070011197 Slog.i(TAG, "Delivering to " + filter
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011198 + " (seq=" + seq + "): " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199 }
Jeff Brown4d94a762010-09-23 11:33:28 -070011200 performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011201 new Intent(r.intent), r.resultCode,
Dianne Hackborn12527f92009-11-11 17:39:50 -080011202 r.resultData, r.resultExtras, r.ordered, r.initialSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 if (ordered) {
11204 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
11205 }
11206 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011207 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011208 if (ordered) {
11209 r.receiver = null;
11210 r.curFilter = null;
11211 filter.receiverList.curBroadcast = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011212 if (filter.receiverList.app != null) {
11213 filter.receiverList.app.curReceiver = null;
11214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 }
11216 }
11217 }
11218 }
11219
Dianne Hackborn12527f92009-11-11 17:39:50 -080011220 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
11221 if (r.callingUid < 0) {
11222 // This was from a registerReceiver() call; ignore it.
11223 return;
11224 }
11225 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
11226 MAX_BROADCAST_HISTORY-1);
11227 r.finishTime = SystemClock.uptimeMillis();
11228 mBroadcastHistory[0] = r;
11229 }
11230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011231 private final void processNextBroadcast(boolean fromMsg) {
11232 synchronized(this) {
11233 BroadcastRecord r;
11234
Joe Onorato8a9b2202010-02-26 18:56:32 -080011235 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011236 + mParallelBroadcasts.size() + " broadcasts, "
Dianne Hackborn399cccb2010-04-13 22:57:49 -070011237 + mOrderedBroadcasts.size() + " ordered broadcasts");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011238
11239 updateCpuStats();
11240
11241 if (fromMsg) {
11242 mBroadcastsScheduled = false;
11243 }
11244
11245 // First, deliver any non-serialized broadcasts right away.
11246 while (mParallelBroadcasts.size() > 0) {
11247 r = mParallelBroadcasts.remove(0);
Dianne Hackborn12527f92009-11-11 17:39:50 -080011248 r.dispatchTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011249 final int N = r.receivers.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011250 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011251 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011252 for (int i=0; i<N; i++) {
11253 Object target = r.receivers.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011254 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn399cccb2010-04-13 22:57:49 -070011255 "Delivering non-ordered to registered "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 + target + ": " + r);
Jeff Brown4d94a762010-09-23 11:33:28 -070011257 deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 }
Dianne Hackborn12527f92009-11-11 17:39:50 -080011259 addBroadcastToHistoryLocked(r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011260 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011261 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 }
11263
11264 // Now take care of the next serialized one...
11265
11266 // If we are waiting for a process to come up to handle the next
11267 // broadcast, then do nothing at this point. Just in case, we
11268 // check that the process we're waiting for still exists.
11269 if (mPendingBroadcast != null) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070011270 if (DEBUG_BROADCAST_LIGHT) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011271 Slog.v(TAG, "processNextBroadcast: waiting for "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070011272 + mPendingBroadcast.curApp);
11273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274
11275 boolean isDead;
11276 synchronized (mPidsSelfLocked) {
11277 isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null);
11278 }
11279 if (!isDead) {
11280 // It's still alive, so keep waiting
11281 return;
11282 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011283 Slog.w(TAG, "pending app " + mPendingBroadcast.curApp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011284 + " died before responding to broadcast");
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011285 mPendingBroadcast.state = BroadcastRecord.IDLE;
11286 mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011287 mPendingBroadcast = null;
11288 }
11289 }
11290
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011291 boolean looped = false;
11292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011293 do {
11294 if (mOrderedBroadcasts.size() == 0) {
11295 // No more broadcasts pending, so all done!
11296 scheduleAppGcsLocked();
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011297 if (looped) {
11298 // If we had finished the last ordered broadcast, then
11299 // make sure all processes have correct oom and sched
11300 // adjustments.
11301 updateOomAdjLocked();
11302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 return;
11304 }
11305 r = mOrderedBroadcasts.get(0);
11306 boolean forceReceive = false;
11307
11308 // Ensure that even if something goes awry with the timeout
11309 // detection, we catch "hung" broadcasts here, discard them,
Jeff Hamiltonacf84742010-05-25 22:10:18 -050011310 // and continue to make progress.
11311 //
11312 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
Jeff Brown4d94a762010-09-23 11:33:28 -070011313 // receivers don't get executed with timeouts. They're intended for
Jeff Hamiltonacf84742010-05-25 22:10:18 -050011314 // one time heavy lifting after system upgrades and can take
11315 // significant amounts of time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011317 if (mProcessesReady && r.dispatchTime > 0) {
Jeff Hamiltonacf84742010-05-25 22:10:18 -050011318 long now = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011319 if ((numReceivers > 0) &&
11320 (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011321 Slog.w(TAG, "Hung broadcast discarded after timeout failure:"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011322 + " now=" + now
11323 + " dispatchTime=" + r.dispatchTime
Dianne Hackborn12527f92009-11-11 17:39:50 -080011324 + " startTime=" + r.receiverTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011325 + " intent=" + r.intent
11326 + " numReceivers=" + numReceivers
11327 + " nextReceiver=" + r.nextReceiver
11328 + " state=" + r.state);
Jeff Brown4d94a762010-09-23 11:33:28 -070011329 broadcastTimeoutLocked(false); // forcibly finish this broadcast
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011330 forceReceive = true;
11331 r.state = BroadcastRecord.IDLE;
11332 }
11333 }
11334
11335 if (r.state != BroadcastRecord.IDLE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011336 if (DEBUG_BROADCAST) Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011337 "processNextBroadcast() called when not idle (state="
11338 + r.state + ")");
11339 return;
11340 }
11341
11342 if (r.receivers == null || r.nextReceiver >= numReceivers
11343 || r.resultAbort || forceReceive) {
11344 // No more receivers for this broadcast! Send the final
11345 // result if requested...
11346 if (r.resultTo != null) {
11347 try {
11348 if (DEBUG_BROADCAST) {
11349 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011350 Slog.i(TAG, "Finishing broadcast " + r.intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011351 + " seq=" + seq + " app=" + r.callerApp);
11352 }
Jeff Brown4d94a762010-09-23 11:33:28 -070011353 performReceiveLocked(r.callerApp, r.resultTo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354 new Intent(r.intent), r.resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070011355 r.resultData, r.resultExtras, false, false);
Johannes Carlssonb5a86542010-10-27 10:08:10 +020011356 // Set this to null so that the reference
11357 // (local and remote) isnt kept in the mBroadcastHistory.
11358 r.resultTo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011359 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011360 Slog.w(TAG, "Failure sending broadcast result of " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 }
11362 }
11363
Joe Onorato8a9b2202010-02-26 18:56:32 -080011364 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
Jeff Brown4d94a762010-09-23 11:33:28 -070011365 cancelBroadcastTimeoutLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011366
Joe Onorato8a9b2202010-02-26 18:56:32 -080011367 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011368 + r);
11369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370 // ... and on to the next...
Dianne Hackborn12527f92009-11-11 17:39:50 -080011371 addBroadcastToHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011372 mOrderedBroadcasts.remove(0);
11373 r = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011374 looped = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011375 continue;
11376 }
11377 } while (r == null);
11378
11379 // Get the next receiver...
11380 int recIdx = r.nextReceiver++;
11381
11382 // Keep track of when this receiver started, and make sure there
11383 // is a timeout message pending to kill it if need be.
Dianne Hackborn12527f92009-11-11 17:39:50 -080011384 r.receiverTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 if (recIdx == 0) {
Dianne Hackborn12527f92009-11-11 17:39:50 -080011386 r.dispatchTime = r.receiverTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011387
Joe Onorato8a9b2202010-02-26 18:56:32 -080011388 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070011389 + r);
Jeff Brown4d94a762010-09-23 11:33:28 -070011390 }
11391 if (! mPendingBroadcastTimeoutMessage) {
11392 long timeoutTime = r.receiverTime + BROADCAST_TIMEOUT;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011393 if (DEBUG_BROADCAST) Slog.v(TAG,
Jeff Brown4d94a762010-09-23 11:33:28 -070011394 "Submitting BROADCAST_TIMEOUT_MSG for " + r + " at " + timeoutTime);
11395 setBroadcastTimeoutLocked(timeoutTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 }
11397
11398 Object nextReceiver = r.receivers.get(recIdx);
11399 if (nextReceiver instanceof BroadcastFilter) {
11400 // Simple case: this is a registered receiver who gets
11401 // a direct call.
11402 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011403 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn399cccb2010-04-13 22:57:49 -070011404 "Delivering ordered to registered "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 + filter + ": " + r);
Jeff Brown4d94a762010-09-23 11:33:28 -070011406 deliverToRegisteredReceiverLocked(r, filter, r.ordered);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011407 if (r.receiver == null || !r.ordered) {
11408 // The receiver has already finished, so schedule to
11409 // process the next one.
Dianne Hackborn399cccb2010-04-13 22:57:49 -070011410 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered="
11411 + r.ordered + " receiver=" + r.receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011412 r.state = BroadcastRecord.IDLE;
11413 scheduleBroadcastsLocked();
11414 }
11415 return;
11416 }
11417
11418 // Hard case: need to instantiate the receiver, possibly
11419 // starting its application process to host it.
11420
11421 ResolveInfo info =
11422 (ResolveInfo)nextReceiver;
11423
11424 boolean skip = false;
11425 int perm = checkComponentPermission(info.activityInfo.permission,
11426 r.callingPid, r.callingUid,
11427 info.activityInfo.exported
11428 ? -1 : info.activityInfo.applicationInfo.uid);
11429 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011430 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011431 + r.intent.toString()
11432 + " from " + r.callerPackage + " (pid=" + r.callingPid
11433 + ", uid=" + r.callingUid + ")"
11434 + " requires " + info.activityInfo.permission
11435 + " due to receiver " + info.activityInfo.packageName
11436 + "/" + info.activityInfo.name);
11437 skip = true;
11438 }
11439 if (r.callingUid != Process.SYSTEM_UID &&
11440 r.requiredPermission != null) {
11441 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011442 perm = AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011443 checkPermission(r.requiredPermission,
11444 info.activityInfo.applicationInfo.packageName);
11445 } catch (RemoteException e) {
11446 perm = PackageManager.PERMISSION_DENIED;
11447 }
11448 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011449 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011450 + r.intent + " to "
11451 + info.activityInfo.applicationInfo.packageName
11452 + " requires " + r.requiredPermission
11453 + " due to sender " + r.callerPackage
11454 + " (uid " + r.callingUid + ")");
11455 skip = true;
11456 }
11457 }
11458 if (r.curApp != null && r.curApp.crashing) {
11459 // If the target process is crashing, just skip it.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011460 if (DEBUG_BROADCAST) Slog.v(TAG,
11461 "Skipping deliver ordered " + r + " to " + r.curApp
11462 + ": process crashing");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 skip = true;
11464 }
11465
11466 if (skip) {
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011467 if (DEBUG_BROADCAST) Slog.v(TAG,
11468 "Skipping delivery of ordered " + r + " for whatever reason");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011469 r.receiver = null;
11470 r.curFilter = null;
11471 r.state = BroadcastRecord.IDLE;
11472 scheduleBroadcastsLocked();
11473 return;
11474 }
11475
11476 r.state = BroadcastRecord.APP_RECEIVE;
11477 String targetProcess = info.activityInfo.processName;
11478 r.curComponent = new ComponentName(
11479 info.activityInfo.applicationInfo.packageName,
11480 info.activityInfo.name);
11481 r.curReceiver = info.activityInfo;
11482
11483 // Is this receiver's application already running?
11484 ProcessRecord app = getProcessRecordLocked(targetProcess,
11485 info.activityInfo.applicationInfo.uid);
11486 if (app != null && app.thread != null) {
11487 try {
11488 processCurBroadcastLocked(r, app);
11489 return;
11490 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011491 Slog.w(TAG, "Exception when sending broadcast to "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011492 + r.curComponent, e);
11493 }
11494
11495 // If a dead object exception was thrown -- fall through to
11496 // restart the application.
11497 }
11498
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011499 // Not running -- get it started, to be executed when the app comes up.
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011500 if (DEBUG_BROADCAST) Slog.v(TAG,
11501 "Need to start app " + targetProcess + " for broadcast " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011502 if ((r.curApp=startProcessLocked(targetProcess,
11503 info.activityInfo.applicationInfo, true,
11504 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
Dianne Hackborn9acc0302009-08-25 00:27:12 -070011505 "broadcast", r.curComponent,
11506 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0))
11507 == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 // Ah, this recipient is unavailable. Finish it if necessary,
11509 // and mark the broadcast record as ready for the next.
Joe Onorato8a9b2202010-02-26 18:56:32 -080011510 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 + info.activityInfo.applicationInfo.packageName + "/"
11512 + info.activityInfo.applicationInfo.uid + " for broadcast "
11513 + r.intent + ": process is bad");
Jeff Brown4d94a762010-09-23 11:33:28 -070011514 logBroadcastReceiverDiscardLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011515 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
11516 r.resultExtras, r.resultAbort, true);
11517 scheduleBroadcastsLocked();
11518 r.state = BroadcastRecord.IDLE;
11519 return;
11520 }
11521
11522 mPendingBroadcast = r;
Dianne Hackborn8891fdc2010-09-20 20:44:46 -070011523 mPendingBroadcastRecvIndex = recIdx;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011524 }
11525 }
11526
11527 // =========================================================
11528 // INSTRUMENTATION
11529 // =========================================================
11530
11531 public boolean startInstrumentation(ComponentName className,
11532 String profileFile, int flags, Bundle arguments,
11533 IInstrumentationWatcher watcher) {
11534 // Refuse possible leaked file descriptors
11535 if (arguments != null && arguments.hasFileDescriptors()) {
11536 throw new IllegalArgumentException("File descriptors passed in Bundle");
11537 }
11538
11539 synchronized(this) {
11540 InstrumentationInfo ii = null;
11541 ApplicationInfo ai = null;
11542 try {
11543 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070011544 className, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011545 ai = mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070011546 ii.targetPackage, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011547 } catch (PackageManager.NameNotFoundException e) {
11548 }
11549 if (ii == null) {
11550 reportStartInstrumentationFailure(watcher, className,
11551 "Unable to find instrumentation info for: " + className);
11552 return false;
11553 }
11554 if (ai == null) {
11555 reportStartInstrumentationFailure(watcher, className,
11556 "Unable to find instrumentation target package: " + ii.targetPackage);
11557 return false;
11558 }
11559
11560 int match = mContext.getPackageManager().checkSignatures(
11561 ii.targetPackage, ii.packageName);
11562 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
11563 String msg = "Permission Denial: starting instrumentation "
11564 + className + " from pid="
11565 + Binder.getCallingPid()
11566 + ", uid=" + Binder.getCallingPid()
11567 + " not allowed because package " + ii.packageName
11568 + " does not have a signature matching the target "
11569 + ii.targetPackage;
11570 reportStartInstrumentationFailure(watcher, className, msg);
11571 throw new SecurityException(msg);
11572 }
11573
11574 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080011575 forceStopPackageLocked(ii.targetPackage, -1, true, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011576 ProcessRecord app = addAppLocked(ai);
11577 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011578 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011579 app.instrumentationProfileFile = profileFile;
11580 app.instrumentationArguments = arguments;
11581 app.instrumentationWatcher = watcher;
11582 app.instrumentationResultClass = className;
11583 Binder.restoreCallingIdentity(origId);
11584 }
11585
11586 return true;
11587 }
11588
11589 /**
11590 * Report errors that occur while attempting to start Instrumentation. Always writes the
11591 * error to the logs, but if somebody is watching, send the report there too. This enables
11592 * the "am" command to report errors with more information.
11593 *
11594 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
11595 * @param cn The component name of the instrumentation.
11596 * @param report The error report.
11597 */
11598 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
11599 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011600 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011601 try {
11602 if (watcher != null) {
11603 Bundle results = new Bundle();
11604 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
11605 results.putString("Error", report);
11606 watcher.instrumentationStatus(cn, -1, results);
11607 }
11608 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011609 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011610 }
11611 }
11612
11613 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
11614 if (app.instrumentationWatcher != null) {
11615 try {
11616 // NOTE: IInstrumentationWatcher *must* be oneway here
11617 app.instrumentationWatcher.instrumentationFinished(
11618 app.instrumentationClass,
11619 resultCode,
11620 results);
11621 } catch (RemoteException e) {
11622 }
11623 }
11624 app.instrumentationWatcher = null;
11625 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070011626 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011627 app.instrumentationProfileFile = null;
11628 app.instrumentationArguments = null;
11629
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080011630 forceStopPackageLocked(app.processName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011631 }
11632
11633 public void finishInstrumentation(IApplicationThread target,
11634 int resultCode, Bundle results) {
11635 // Refuse possible leaked file descriptors
11636 if (results != null && results.hasFileDescriptors()) {
11637 throw new IllegalArgumentException("File descriptors passed in Intent");
11638 }
11639
11640 synchronized(this) {
11641 ProcessRecord app = getRecordForAppLocked(target);
11642 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011643 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011644 return;
11645 }
11646 final long origId = Binder.clearCallingIdentity();
11647 finishInstrumentationLocked(app, resultCode, results);
11648 Binder.restoreCallingIdentity(origId);
11649 }
11650 }
11651
11652 // =========================================================
11653 // CONFIGURATION
11654 // =========================================================
11655
11656 public ConfigurationInfo getDeviceConfigurationInfo() {
11657 ConfigurationInfo config = new ConfigurationInfo();
11658 synchronized (this) {
11659 config.reqTouchScreen = mConfiguration.touchscreen;
11660 config.reqKeyboardType = mConfiguration.keyboard;
11661 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011662 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
11663 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011664 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
11665 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070011666 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
11667 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011668 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
11669 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070011670 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011671 }
11672 return config;
11673 }
11674
11675 public Configuration getConfiguration() {
11676 Configuration ci;
11677 synchronized(this) {
11678 ci = new Configuration(mConfiguration);
11679 }
11680 return ci;
11681 }
11682
11683 public void updateConfiguration(Configuration values) {
11684 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
11685 "updateConfiguration()");
11686
11687 synchronized(this) {
11688 if (values == null && mWindowManager != null) {
11689 // sentinel: fetch the current configuration from the window manager
11690 values = mWindowManager.computeNewConfiguration();
11691 }
11692
11693 final long origId = Binder.clearCallingIdentity();
11694 updateConfigurationLocked(values, null);
11695 Binder.restoreCallingIdentity(origId);
11696 }
11697 }
11698
11699 /**
11700 * Do either or both things: (1) change the current configuration, and (2)
11701 * make sure the given activity is running with the (now) current
11702 * configuration. Returns true if the activity has been left running, or
11703 * false if <var>starting</var> is being destroyed to match the new
11704 * configuration.
11705 */
11706 public boolean updateConfigurationLocked(Configuration values,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011707 ActivityRecord starting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011708 int changes = 0;
11709
11710 boolean kept = true;
11711
11712 if (values != null) {
11713 Configuration newConfig = new Configuration(mConfiguration);
11714 changes = newConfig.updateFrom(values);
11715 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011716 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011717 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011718 }
11719
Doug Zongker2bec3d42009-12-04 12:52:44 -080011720 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721
11722 if (values.locale != null) {
11723 saveLocaleLocked(values.locale,
11724 !values.locale.equals(mConfiguration.locale),
11725 values.userSetLocale);
11726 }
11727
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011728 mConfigurationSeq++;
11729 if (mConfigurationSeq <= 0) {
11730 mConfigurationSeq = 1;
11731 }
11732 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011733 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011734 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080011735
11736 AttributeCache ac = AttributeCache.instance();
11737 if (ac != null) {
11738 ac.updateConfiguration(mConfiguration);
11739 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011740
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080011741 if (Settings.System.hasInterestingConfigurationChanges(changes)) {
11742 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
11743 msg.obj = new Configuration(mConfiguration);
11744 mHandler.sendMessage(msg);
11745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011746
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011747 for (int i=mLruProcesses.size()-1; i>=0; i--) {
11748 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011749 try {
11750 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011751 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070011752 + app.processName + " new config " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 app.thread.scheduleConfigurationChanged(mConfiguration);
11754 }
11755 } catch (Exception e) {
11756 }
11757 }
11758 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080011759 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
11760 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011761 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
11762 null, false, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080011763 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
11764 broadcastIntentLocked(null, null,
11765 new Intent(Intent.ACTION_LOCALE_CHANGED),
11766 null, null, 0, null, null,
11767 null, false, false, MY_PID, Process.SYSTEM_UID);
11768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011769 }
11770 }
11771
11772 if (changes != 0 && starting == null) {
11773 // If the configuration changed, and the caller is not already
11774 // in the process of starting an activity, then find the top
11775 // activity to check if its configuration needs to change.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011776 starting = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011777 }
11778
11779 if (starting != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011780 kept = mMainStack.ensureActivityConfigurationLocked(starting, changes);
Dianne Hackborn5f4d6432010-12-21 20:40:11 -080011781 // And we need to make sure at this point that all other activities
11782 // are made visible with the correct configuration.
11783 mMainStack.ensureActivitiesVisibleLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011784 }
11785
Dianne Hackborne36d6e22010-02-17 19:46:25 -080011786 if (values != null && mWindowManager != null) {
11787 mWindowManager.setNewConfiguration(mConfiguration);
11788 }
11789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011790 return kept;
11791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011792
11793 /**
11794 * Save the locale. You must be inside a synchronized (this) block.
11795 */
11796 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
11797 if(isDiff) {
11798 SystemProperties.set("user.language", l.getLanguage());
11799 SystemProperties.set("user.region", l.getCountry());
11800 }
11801
11802 if(isPersist) {
11803 SystemProperties.set("persist.sys.language", l.getLanguage());
11804 SystemProperties.set("persist.sys.country", l.getCountry());
11805 SystemProperties.set("persist.sys.localevar", l.getVariant());
11806 }
11807 }
11808
11809 // =========================================================
11810 // LIFETIME MANAGEMENT
11811 // =========================================================
11812
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011813 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
11814 ProcessRecord TOP_APP, boolean recursed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011815 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011816 // This adjustment has already been computed. If we are calling
11817 // from the top, we may have already computed our adjustment with
11818 // an earlier hidden adjustment that isn't really for us... if
11819 // so, use the new hidden adjustment.
11820 if (!recursed && app.hidden) {
11821 app.curAdj = hiddenAdj;
11822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011823 return app.curAdj;
11824 }
11825
11826 if (app.thread == null) {
11827 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011828 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011829 return (app.curAdj=EMPTY_APP_ADJ);
11830 }
11831
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011832 if (app.maxAdj <= FOREGROUND_APP_ADJ) {
11833 // The max adjustment doesn't allow this app to be anything
11834 // below foreground, so it is not worth doing work for it.
11835 app.adjType = "fixed";
11836 app.adjSeq = mAdjSeq;
11837 app.curRawAdj = app.maxAdj;
Dianne Hackborn287952c2010-09-22 22:34:31 -070011838 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011839 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
11840 return (app.curAdj=app.maxAdj);
11841 }
11842
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011843 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011844 app.adjSource = null;
11845 app.adjTarget = null;
Dianne Hackborn287952c2010-09-22 22:34:31 -070011846 app.keeping = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011847 app.empty = false;
11848 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849
The Android Open Source Project4df24232009-03-05 14:34:35 -080011850 // Determine the importance of the process, starting with most
11851 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011852 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011853 int schedGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 int N;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011855 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856 // The last app on the list is the foreground app.
11857 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011858 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011859 app.adjType = "top-activity";
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011860 } else if (app.instrumentationClass != null) {
11861 // Don't want to kill running instrumentation.
11862 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011863 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011864 app.adjType = "instrumentation";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011865 } else if (app.curReceiver != null ||
11866 (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) {
11867 // An app that is currently receiving a broadcast also
11868 // counts as being in the foreground.
11869 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011870 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011871 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011872 } else if (app.executingServices.size() > 0) {
11873 // An app that is currently executing a service callback also
11874 // counts as being in the foreground.
11875 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011876 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011877 app.adjType = "exec-service";
11878 } else if (app.foregroundServices) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 // The user is aware of this app, so make it visible.
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011880 adj = PERCEPTIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011881 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011882 app.adjType = "foreground-service";
11883 } else if (app.forcingToForeground != null) {
11884 // The user is aware of this app, so make it visible.
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011885 adj = PERCEPTIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011886 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011887 app.adjType = "force-foreground";
11888 app.adjSource = app.forcingToForeground;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011889 } else if (app == mHeavyWeightProcess) {
11890 // We don't want to kill the current heavy-weight process.
11891 adj = HEAVY_WEIGHT_APP_ADJ;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080011892 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011893 app.adjType = "heavy";
The Android Open Source Project4df24232009-03-05 14:34:35 -080011894 } else if (app == mHomeProcess) {
11895 // This process is hosting what we currently consider to be the
11896 // home app, so we don't want to let it go into the background.
11897 adj = HOME_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011898 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011899 app.adjType = "home";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011900 } else if ((N=app.activities.size()) != 0) {
11901 // This app is in the background with paused activities.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011902 app.hidden = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011903 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011904 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011905 app.adjType = "bg-activities";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011906 N = app.activities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011907 for (int j=0; j<N; j++) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080011908 ActivityRecord r = app.activities.get(j);
11909 if (r.visible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011910 // This app has a visible activity!
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011911 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011912 adj = VISIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011913 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011914 app.adjType = "visible";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 break;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -080011916 } else if (r.state == ActivityState.PAUSING
11917 || r.state == ActivityState.PAUSED
11918 || r.state == ActivityState.STOPPING) {
11919 adj = PERCEPTIBLE_APP_ADJ;
11920 app.adjType = "stopping";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921 }
11922 }
11923 } else {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011924 // A very not-needed process. If this is lower in the lru list,
11925 // we will push it in to the empty bucket.
11926 app.hidden = true;
11927 app.empty = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011928 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011929 adj = hiddenAdj;
11930 app.adjType = "bg-empty";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011931 }
11932
Joe Onorato8a9b2202010-02-26 18:56:32 -080011933 //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj);
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011934
The Android Open Source Project4df24232009-03-05 14:34:35 -080011935 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011936 // there are applications dependent on our services or providers, but
11937 // this gives us a baseline and makes sure we don't get into an
11938 // infinite recursion.
11939 app.adjSeq = mAdjSeq;
11940 app.curRawAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011941
Christopher Tate6fa95972009-06-05 18:43:55 -070011942 if (mBackupTarget != null && app == mBackupTarget.app) {
11943 // If possible we want to avoid killing apps while they're being backed up
11944 if (adj > BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011945 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Christopher Tate6fa95972009-06-05 18:43:55 -070011946 adj = BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011947 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011948 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070011949 }
11950 }
11951
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011952 if (app.services.size() != 0 && (adj > FOREGROUND_APP_ADJ
11953 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011954 final long now = SystemClock.uptimeMillis();
11955 // This process is more important if the top activity is
11956 // bound to the service.
Dianne Hackborn860755f2010-06-03 18:47:52 -070011957 Iterator<ServiceRecord> jt = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011958 while (jt.hasNext() && adj > FOREGROUND_APP_ADJ) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070011959 ServiceRecord s = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011960 if (s.startRequested) {
11961 if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) {
11962 // This service has seen some activity within
11963 // recent memory, so we will keep its process ahead
11964 // of the background processes.
11965 if (adj > SECONDARY_SERVER_ADJ) {
11966 adj = SECONDARY_SERVER_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011967 app.adjType = "started-services";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011968 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011969 }
11970 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080011971 // If we have let the service slide into the background
11972 // state, still have some text describing what it is doing
11973 // even though the service no longer has an impact.
11974 if (adj > SECONDARY_SERVER_ADJ) {
11975 app.adjType = "started-bg-services";
11976 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070011977 // Don't kill this process because it is doing work; it
11978 // has said it is doing work.
11979 app.keeping = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011980 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011981 if (s.connections.size() > 0 && (adj > FOREGROUND_APP_ADJ
11982 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070011983 Iterator<ArrayList<ConnectionRecord>> kt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011984 = s.connections.values().iterator();
11985 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070011986 ArrayList<ConnectionRecord> clist = kt.next();
11987 for (int i=0; i<clist.size() && adj > FOREGROUND_APP_ADJ; i++) {
11988 // XXX should compute this based on the max of
11989 // all connected clients.
11990 ConnectionRecord cr = clist.get(i);
11991 if (cr.binding.client == app) {
11992 // Binding to ourself is not interesting.
11993 continue;
11994 }
11995 if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) {
11996 ProcessRecord client = cr.binding.client;
11997 int myHiddenAdj = hiddenAdj;
11998 if (myHiddenAdj > client.hiddenAdj) {
11999 if (client.hiddenAdj >= VISIBLE_APP_ADJ) {
12000 myHiddenAdj = client.hiddenAdj;
12001 } else {
12002 myHiddenAdj = VISIBLE_APP_ADJ;
12003 }
12004 }
12005 int clientAdj = computeOomAdjLocked(
12006 client, myHiddenAdj, TOP_APP, true);
12007 if (adj > clientAdj) {
12008 adj = clientAdj >= VISIBLE_APP_ADJ
12009 ? clientAdj : VISIBLE_APP_ADJ;
12010 if (!client.hidden) {
12011 app.hidden = false;
12012 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012013 if (client.keeping) {
12014 app.keeping = true;
12015 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012016 app.adjType = "service";
12017 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12018 .REASON_SERVICE_IN_USE;
12019 app.adjSource = cr.binding.client;
12020 app.adjTarget = s.name;
12021 }
12022 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
12023 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12024 schedGroup = Process.THREAD_GROUP_DEFAULT;
12025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012026 }
12027 }
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012028 ActivityRecord a = cr.activity;
12029 //if (a != null) {
12030 // Slog.i(TAG, "Connection to " + a ": state=" + a.state);
12031 //}
12032 if (a != null && adj > FOREGROUND_APP_ADJ &&
12033 (a.state == ActivityState.RESUMED
12034 || a.state == ActivityState.PAUSING)) {
12035 adj = FOREGROUND_APP_ADJ;
12036 schedGroup = Process.THREAD_GROUP_DEFAULT;
12037 app.hidden = false;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012038 app.adjType = "service";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070012039 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12040 .REASON_SERVICE_IN_USE;
Dianne Hackborn43d9ac82010-08-25 15:06:25 -070012041 app.adjSource = a;
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012042 app.adjTarget = s.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012043 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012044 }
12045 }
12046 }
12047 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012048
Dianne Hackborn287952c2010-09-22 22:34:31 -070012049 // Finally, if this process has active services running in it, we
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012050 // would like to avoid killing it unless it would prevent the current
12051 // application from running. By default we put the process in
12052 // with the rest of the background processes; as we scan through
12053 // its services we may bump it up from there.
12054 if (adj > hiddenAdj) {
12055 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012056 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070012057 app.adjType = "bg-services";
12058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012059 }
12060
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012061 if (app.pubProviders.size() != 0 && (adj > FOREGROUND_APP_ADJ
12062 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012063 Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator();
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012064 while (jt.hasNext() && (adj > FOREGROUND_APP_ADJ
12065 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070012066 ContentProviderRecord cpr = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012067 if (cpr.clients.size() != 0) {
12068 Iterator<ProcessRecord> kt = cpr.clients.iterator();
12069 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
12070 ProcessRecord client = kt.next();
The Android Open Source Project10592532009-03-18 17:39:46 -070012071 if (client == app) {
12072 // Being our own client is not interesting.
12073 continue;
12074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 int myHiddenAdj = hiddenAdj;
12076 if (myHiddenAdj > client.hiddenAdj) {
12077 if (client.hiddenAdj > FOREGROUND_APP_ADJ) {
12078 myHiddenAdj = client.hiddenAdj;
12079 } else {
12080 myHiddenAdj = FOREGROUND_APP_ADJ;
12081 }
12082 }
12083 int clientAdj = computeOomAdjLocked(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012084 client, myHiddenAdj, TOP_APP, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012085 if (adj > clientAdj) {
12086 adj = clientAdj > FOREGROUND_APP_ADJ
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012087 ? clientAdj : FOREGROUND_APP_ADJ;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012088 if (!client.hidden) {
12089 app.hidden = false;
12090 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012091 if (client.keeping) {
12092 app.keeping = true;
12093 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012094 app.adjType = "provider";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070012095 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
12096 .REASON_PROVIDER_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012097 app.adjSource = client;
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012098 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012100 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
12101 schedGroup = Process.THREAD_GROUP_DEFAULT;
12102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012103 }
12104 }
12105 // If the provider has external (non-framework) process
12106 // dependencies, ensure that its adjustment is at least
12107 // FOREGROUND_APP_ADJ.
12108 if (cpr.externals != 0) {
12109 if (adj > FOREGROUND_APP_ADJ) {
12110 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012111 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012112 app.hidden = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012113 app.keeping = true;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070012114 app.adjType = "provider";
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070012115 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012116 }
12117 }
12118 }
12119 }
12120
12121 app.curRawAdj = adj;
12122
Joe Onorato8a9b2202010-02-26 18:56:32 -080012123 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012124 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
12125 if (adj > app.maxAdj) {
12126 adj = app.maxAdj;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070012127 if (app.maxAdj <= PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012128 schedGroup = Process.THREAD_GROUP_DEFAULT;
12129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012130 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012131 if (adj < HIDDEN_APP_MIN_ADJ) {
12132 app.keeping = true;
12133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012134
12135 app.curAdj = adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012136 app.curSchedGroup = schedGroup;
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012138 return adj;
12139 }
12140
12141 /**
12142 * Ask a given process to GC right now.
12143 */
12144 final void performAppGcLocked(ProcessRecord app) {
12145 try {
12146 app.lastRequestedGc = SystemClock.uptimeMillis();
12147 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012148 if (app.reportLowMemory) {
12149 app.reportLowMemory = false;
12150 app.thread.scheduleLowMemory();
12151 } else {
12152 app.thread.processInBackground();
12153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012154 }
12155 } catch (Exception e) {
12156 // whatever.
12157 }
12158 }
12159
12160 /**
12161 * Returns true if things are idle enough to perform GCs.
12162 */
Josh Bartel7f208742010-02-25 11:01:44 -060012163 private final boolean canGcNowLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012164 return mParallelBroadcasts.size() == 0
12165 && mOrderedBroadcasts.size() == 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012166 && (mSleeping || (mMainStack.mResumedActivity != null &&
12167 mMainStack.mResumedActivity.idle));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 }
12169
12170 /**
12171 * Perform GCs on all processes that are waiting for it, but only
12172 * if things are idle.
12173 */
12174 final void performAppGcsLocked() {
12175 final int N = mProcessesToGc.size();
12176 if (N <= 0) {
12177 return;
12178 }
Josh Bartel7f208742010-02-25 11:01:44 -060012179 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012180 while (mProcessesToGc.size() > 0) {
12181 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070012182 if (proc.curRawAdj > PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012183 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
12184 <= SystemClock.uptimeMillis()) {
12185 // To avoid spamming the system, we will GC processes one
12186 // at a time, waiting a few seconds between each.
12187 performAppGcLocked(proc);
12188 scheduleAppGcsLocked();
12189 return;
12190 } else {
12191 // It hasn't been long enough since we last GCed this
12192 // process... put it in the list to wait for its time.
12193 addProcessToGcListLocked(proc);
12194 break;
12195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012196 }
12197 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012198
12199 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200 }
12201 }
12202
12203 /**
12204 * If all looks good, perform GCs on all processes waiting for them.
12205 */
12206 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060012207 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012208 performAppGcsLocked();
12209 return;
12210 }
12211 // Still not idle, wait some more.
12212 scheduleAppGcsLocked();
12213 }
12214
12215 /**
12216 * Schedule the execution of all pending app GCs.
12217 */
12218 final void scheduleAppGcsLocked() {
12219 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012220
12221 if (mProcessesToGc.size() > 0) {
12222 // Schedule a GC for the time to the next process.
12223 ProcessRecord proc = mProcessesToGc.get(0);
12224 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
12225
12226 long when = mProcessesToGc.get(0).lastRequestedGc + GC_MIN_INTERVAL;
12227 long now = SystemClock.uptimeMillis();
12228 if (when < (now+GC_TIMEOUT)) {
12229 when = now + GC_TIMEOUT;
12230 }
12231 mHandler.sendMessageAtTime(msg, when);
12232 }
12233 }
12234
12235 /**
12236 * Add a process to the array of processes waiting to be GCed. Keeps the
12237 * list in sorted order by the last GC time. The process can't already be
12238 * on the list.
12239 */
12240 final void addProcessToGcListLocked(ProcessRecord proc) {
12241 boolean added = false;
12242 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
12243 if (mProcessesToGc.get(i).lastRequestedGc <
12244 proc.lastRequestedGc) {
12245 added = true;
12246 mProcessesToGc.add(i+1, proc);
12247 break;
12248 }
12249 }
12250 if (!added) {
12251 mProcessesToGc.add(0, proc);
12252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012253 }
12254
12255 /**
12256 * Set up to ask a process to GC itself. This will either do it
12257 * immediately, or put it on the list of processes to gc the next
12258 * time things are idle.
12259 */
12260 final void scheduleAppGcLocked(ProcessRecord app) {
12261 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012262 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012263 return;
12264 }
12265 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070012266 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012267 scheduleAppGcsLocked();
12268 }
12269 }
12270
Dianne Hackborn287952c2010-09-22 22:34:31 -070012271 final void checkExcessivePowerUsageLocked(boolean doKills) {
12272 updateCpuStatsNow();
12273
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012274 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012275 boolean doWakeKills = doKills;
12276 boolean doCpuKills = doKills;
12277 if (mLastPowerCheckRealtime == 0) {
12278 doWakeKills = false;
12279 }
12280 if (mLastPowerCheckUptime == 0) {
12281 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012282 }
12283 if (stats.isScreenOn()) {
Dianne Hackborn287952c2010-09-22 22:34:31 -070012284 doWakeKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012285 }
12286 final long curRealtime = SystemClock.elapsedRealtime();
Dianne Hackborn287952c2010-09-22 22:34:31 -070012287 final long realtimeSince = curRealtime - mLastPowerCheckRealtime;
12288 final long curUptime = SystemClock.uptimeMillis();
12289 final long uptimeSince = curUptime - mLastPowerCheckUptime;
12290 mLastPowerCheckRealtime = curRealtime;
12291 mLastPowerCheckUptime = curUptime;
12292 if (realtimeSince < WAKE_LOCK_MIN_CHECK_DURATION) {
12293 doWakeKills = false;
12294 }
12295 if (uptimeSince < CPU_MIN_CHECK_DURATION) {
12296 doCpuKills = false;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012297 }
12298 int i = mLruProcesses.size();
12299 while (i > 0) {
12300 i--;
12301 ProcessRecord app = mLruProcesses.get(i);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012302 if (!app.keeping) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012303 long wtime;
12304 synchronized (stats) {
12305 wtime = stats.getProcessWakeTime(app.info.uid,
12306 app.pid, curRealtime);
12307 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012308 long wtimeUsed = wtime - app.lastWakeTime;
12309 long cputimeUsed = app.curCpuTime - app.lastCpuTime;
12310 if (DEBUG_POWER) {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012311 StringBuilder sb = new StringBuilder(128);
12312 sb.append("Wake for ");
12313 app.toShortString(sb);
12314 sb.append(": over ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012315 TimeUtils.formatDuration(realtimeSince, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012316 sb.append(" used ");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012317 TimeUtils.formatDuration(wtimeUsed, sb);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012318 sb.append(" (");
Dianne Hackborn287952c2010-09-22 22:34:31 -070012319 sb.append((wtimeUsed*100)/realtimeSince);
12320 sb.append("%)");
12321 Slog.i(TAG, sb.toString());
12322 sb.setLength(0);
12323 sb.append("CPU for ");
12324 app.toShortString(sb);
12325 sb.append(": over ");
12326 TimeUtils.formatDuration(uptimeSince, sb);
12327 sb.append(" used ");
12328 TimeUtils.formatDuration(cputimeUsed, sb);
12329 sb.append(" (");
12330 sb.append((cputimeUsed*100)/uptimeSince);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070012331 sb.append("%)");
12332 Slog.i(TAG, sb.toString());
12333 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012334 // If a process has held a wake lock for more
12335 // than 50% of the time during this period,
12336 // that sounds pad. Kill!
Dianne Hackborn287952c2010-09-22 22:34:31 -070012337 if (doWakeKills && realtimeSince > 0
12338 && ((wtimeUsed*100)/realtimeSince) >= 50) {
12339 synchronized (stats) {
12340 stats.reportExcessiveWakeLocked(app.info.uid, app.processName,
12341 realtimeSince, wtimeUsed);
12342 }
12343 Slog.w(TAG, "Excessive wake lock in " + app.processName
12344 + " (pid " + app.pid + "): held " + wtimeUsed
12345 + " during " + realtimeSince);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012346 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12347 app.processName, app.setAdj, "excessive wake lock");
12348 Process.killProcessQuiet(app.pid);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012349 } else if (doCpuKills && uptimeSince > 0
12350 && ((cputimeUsed*100)/uptimeSince) >= 50) {
12351 synchronized (stats) {
12352 stats.reportExcessiveCpuLocked(app.info.uid, app.processName,
12353 uptimeSince, cputimeUsed);
12354 }
12355 Slog.w(TAG, "Excessive CPU in " + app.processName
12356 + " (pid " + app.pid + "): used " + cputimeUsed
12357 + " during " + uptimeSince);
12358 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12359 app.processName, app.setAdj, "excessive cpu");
12360 Process.killProcessQuiet(app.pid);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012361 } else {
12362 app.lastWakeTime = wtime;
Dianne Hackborn287952c2010-09-22 22:34:31 -070012363 app.lastCpuTime = app.curCpuTime;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012364 }
12365 }
12366 }
12367 }
12368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012369 private final boolean updateOomAdjLocked(
12370 ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) {
12371 app.hiddenAdj = hiddenAdj;
12372
12373 if (app.thread == null) {
12374 return true;
12375 }
12376
Dianne Hackborn287952c2010-09-22 22:34:31 -070012377 final boolean wasKeeping = app.keeping;
12378
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012379 int adj = computeOomAdjLocked(app, hiddenAdj, TOP_APP, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012380
Dianne Hackborn09c916b2009-12-08 14:50:51 -080012381 if ((app.pid != 0 && app.pid != MY_PID) || Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012382 if (app.curRawAdj != app.setRawAdj) {
12383 if (app.curRawAdj > FOREGROUND_APP_ADJ
12384 && app.setRawAdj <= FOREGROUND_APP_ADJ) {
12385 // If this app is transitioning from foreground to
12386 // non-foreground, have it do a gc.
12387 scheduleAppGcLocked(app);
12388 } else if (app.curRawAdj >= HIDDEN_APP_MIN_ADJ
12389 && app.setRawAdj < HIDDEN_APP_MIN_ADJ) {
12390 // Likewise do a gc when an app is moving in to the
12391 // background (such as a service stopping).
12392 scheduleAppGcLocked(app);
Dianne Hackborn287952c2010-09-22 22:34:31 -070012393 }
12394
12395 if (wasKeeping && !app.keeping) {
12396 // This app is no longer something we want to keep. Note
12397 // its current wake lock time to later know to kill it if
12398 // it is not behaving well.
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070012399 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
12400 synchronized (stats) {
12401 app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
12402 app.pid, SystemClock.elapsedRealtime());
12403 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012404 app.lastCpuTime = app.curCpuTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 }
Dianne Hackborn287952c2010-09-22 22:34:31 -070012406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012407 app.setRawAdj = app.curRawAdj;
12408 }
12409 if (adj != app.setAdj) {
12410 if (Process.setOomAdj(app.pid, adj)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012411 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012412 TAG, "Set app " + app.processName +
12413 " oom adj to " + adj);
12414 app.setAdj = adj;
12415 } else {
12416 return false;
12417 }
12418 }
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012419 if (app.setSchedGroup != app.curSchedGroup) {
12420 app.setSchedGroup = app.curSchedGroup;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012421 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012422 "Setting process group of " + app.processName
12423 + " to " + app.curSchedGroup);
12424 if (true) {
San Mehat9438de22009-06-10 09:11:28 -070012425 long oldId = Binder.clearCallingIdentity();
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012426 try {
12427 Process.setProcessGroup(app.pid, app.curSchedGroup);
12428 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012429 Slog.w(TAG, "Failed setting process group of " + app.pid
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012430 + " to " + app.curSchedGroup);
San Mehat9438de22009-06-10 09:11:28 -070012431 e.printStackTrace();
12432 } finally {
12433 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070012434 }
12435 }
12436 if (false) {
12437 if (app.thread != null) {
12438 try {
12439 app.thread.setSchedulingGroup(app.curSchedGroup);
12440 } catch (RemoteException e) {
12441 }
12442 }
12443 }
12444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 }
12446
12447 return true;
12448 }
12449
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012450 private final ActivityRecord resumedAppLocked() {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012451 ActivityRecord resumedActivity = mMainStack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012452 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012453 resumedActivity = mMainStack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012454 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012455 resumedActivity = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012456 }
12457 }
12458 return resumedActivity;
12459 }
12460
12461 private final boolean updateOomAdjLocked(ProcessRecord app) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012462 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012463 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
12464 int curAdj = app.curAdj;
12465 final boolean wasHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
12466 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
12467
12468 mAdjSeq++;
12469
12470 final boolean res = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP);
12471 if (res) {
12472 final boolean nowHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
12473 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
12474 if (nowHidden != wasHidden) {
12475 // Changed to/from hidden state, so apps after it in the LRU
12476 // list may also be changed.
12477 updateOomAdjLocked();
12478 }
12479 }
12480 return res;
12481 }
12482
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012483 final boolean updateOomAdjLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012484 boolean didOomAdj = true;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012485 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
12487
12488 if (false) {
12489 RuntimeException e = new RuntimeException();
12490 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080012491 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012492 }
12493
12494 mAdjSeq++;
12495
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012496 // Let's determine how many processes we have running vs.
12497 // how many slots we have for background processes; we may want
12498 // to put multiple processes in a slot of there are enough of
12499 // them.
12500 int numSlots = HIDDEN_APP_MAX_ADJ - HIDDEN_APP_MIN_ADJ + 1;
12501 int factor = (mLruProcesses.size()-4)/numSlots;
12502 if (factor < 1) factor = 1;
12503 int step = 0;
Dianne Hackborn8633e682010-04-22 16:03:41 -070012504 int numHidden = 0;
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012506 // First try updating the OOM adjustment for each of the
12507 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012508 int i = mLruProcesses.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509 int curHiddenAdj = HIDDEN_APP_MIN_ADJ;
12510 while (i > 0) {
12511 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012512 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012513 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 if (updateOomAdjLocked(app, curHiddenAdj, TOP_APP)) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012515 if (curHiddenAdj < EMPTY_APP_ADJ
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 && app.curAdj == curHiddenAdj) {
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080012517 step++;
12518 if (step >= factor) {
12519 step = 0;
12520 curHiddenAdj++;
12521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070012523 if (app.curAdj >= HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn149427c2010-04-23 14:20:03 -070012524 if (!app.killedBackground) {
12525 numHidden++;
12526 if (numHidden > MAX_HIDDEN_APPS) {
Dianne Hackborn906497c2010-05-10 15:57:38 -070012527 Slog.i(TAG, "No longer want " + app.processName
12528 + " (pid " + app.pid + "): hidden #" + numHidden);
Dianne Hackborn149427c2010-04-23 14:20:03 -070012529 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
12530 app.processName, app.setAdj, "too many background");
12531 app.killedBackground = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -070012532 Process.killProcessQuiet(app.pid);
Dianne Hackborn149427c2010-04-23 14:20:03 -070012533 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070012534 }
12535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 } else {
12537 didOomAdj = false;
12538 }
12539 }
12540
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012541 // If we return false, we will fall back on killing processes to
12542 // have a fixed limit. Do this if a limit has been requested; else
12543 // only return false if one of the adjustments failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544 return ENFORCE_PROCESS_LIMIT || mProcessLimit > 0 ? false : didOomAdj;
12545 }
12546
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012547 final void trimApplications() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012548 synchronized (this) {
12549 int i;
12550
12551 // First remove any unused application processes whose package
12552 // has been removed.
12553 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
12554 final ProcessRecord app = mRemovedProcesses.get(i);
12555 if (app.activities.size() == 0
12556 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012557 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012558 TAG, "Exiting empty application process "
12559 + app.processName + " ("
12560 + (app.thread != null ? app.thread.asBinder() : null)
12561 + ")\n");
12562 if (app.pid > 0 && app.pid != MY_PID) {
12563 Process.killProcess(app.pid);
12564 } else {
12565 try {
12566 app.thread.scheduleExit();
12567 } catch (Exception e) {
12568 // Ignore exceptions.
12569 }
12570 }
12571 cleanUpApplicationRecordLocked(app, false, -1);
12572 mRemovedProcesses.remove(i);
12573
12574 if (app.persistent) {
12575 if (app.persistent) {
12576 addAppLocked(app.info);
12577 }
12578 }
12579 }
12580 }
12581
12582 // Now try updating the OOM adjustment for each of the
12583 // application processes based on their current state.
12584 // If the setOomAdj() API is not supported, then go with our
12585 // back-up plan...
12586 if (!updateOomAdjLocked()) {
12587
12588 // Count how many processes are running services.
12589 int numServiceProcs = 0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012590 for (i=mLruProcesses.size()-1; i>=0; i--) {
12591 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592
12593 if (app.persistent || app.services.size() != 0
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -070012594 || app.curReceiver != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012595 // Don't count processes holding services against our
12596 // maximum process count.
Joe Onorato8a9b2202010-02-26 18:56:32 -080012597 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012598 TAG, "Not trimming app " + app + " with services: "
12599 + app.services);
12600 numServiceProcs++;
12601 }
12602 }
12603
12604 int curMaxProcs = mProcessLimit;
12605 if (curMaxProcs <= 0) curMaxProcs = MAX_PROCESSES;
12606 if (mAlwaysFinishActivities) {
12607 curMaxProcs = 1;
12608 }
12609 curMaxProcs += numServiceProcs;
12610
12611 // Quit as many processes as we can to get down to the desired
12612 // process count. First remove any processes that no longer
12613 // have activites running in them.
12614 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012615 i<mLruProcesses.size()
12616 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012617 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012618 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012619 // Quit an application only if it is not currently
12620 // running any activities.
12621 if (!app.persistent && app.activities.size() == 0
12622 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080012623 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012624 TAG, "Exiting empty application process "
12625 + app.processName + " ("
12626 + (app.thread != null ? app.thread.asBinder() : null)
12627 + ")\n");
12628 if (app.pid > 0 && app.pid != MY_PID) {
12629 Process.killProcess(app.pid);
12630 } else {
12631 try {
12632 app.thread.scheduleExit();
12633 } catch (Exception e) {
12634 // Ignore exceptions.
12635 }
12636 }
12637 // todo: For now we assume the application is not buggy
12638 // or evil, and will quit as a result of our request.
12639 // Eventually we need to drive this off of the death
12640 // notification, and kill the process if it takes too long.
12641 cleanUpApplicationRecordLocked(app, false, i);
12642 i--;
12643 }
12644 }
12645
12646 // If we still have too many processes, now from the least
12647 // recently used process we start finishing activities.
Joe Onorato8a9b2202010-02-26 18:56:32 -080012648 if (Config.LOGV) Slog.v(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012649 TAG, "*** NOW HAVE " + mLruProcesses.size() +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012650 " of " + curMaxProcs + " processes");
12651 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012652 i<mLruProcesses.size()
12653 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012654 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012655 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012656 // Quit the application only if we have a state saved for
12657 // all of its activities.
12658 boolean canQuit = !app.persistent && app.curReceiver == null
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -070012659 && app.services.size() == 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012660 int NUMA = app.activities.size();
12661 int j;
Joe Onorato8a9b2202010-02-26 18:56:32 -080012662 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012663 TAG, "Looking to quit " + app.processName);
12664 for (j=0; j<NUMA && canQuit; j++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012665 ActivityRecord r = app.activities.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -080012666 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012667 TAG, " " + r.intent.getComponent().flattenToShortString()
12668 + ": frozen=" + r.haveState + ", visible=" + r.visible);
12669 canQuit = (r.haveState || !r.stateNotNeeded)
12670 && !r.visible && r.stopped;
12671 }
12672 if (canQuit) {
12673 // Finish all of the activities, and then the app itself.
12674 for (j=0; j<NUMA; j++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012675 ActivityRecord r = app.activities.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012676 if (!r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012677 r.stack.destroyActivityLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012678 }
12679 r.resultTo = null;
12680 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080012681 Slog.i(TAG, "Exiting application process "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012682 + app.processName + " ("
12683 + (app.thread != null ? app.thread.asBinder() : null)
12684 + ")\n");
12685 if (app.pid > 0 && app.pid != MY_PID) {
12686 Process.killProcess(app.pid);
12687 } else {
12688 try {
12689 app.thread.scheduleExit();
12690 } catch (Exception e) {
12691 // Ignore exceptions.
12692 }
12693 }
12694 // todo: For now we assume the application is not buggy
12695 // or evil, and will quit as a result of our request.
12696 // Eventually we need to drive this off of the death
12697 // notification, and kill the process if it takes too long.
12698 cleanUpApplicationRecordLocked(app, false, i);
12699 i--;
12700 //dump();
12701 }
12702 }
12703
12704 }
12705
12706 int curMaxActivities = MAX_ACTIVITIES;
12707 if (mAlwaysFinishActivities) {
12708 curMaxActivities = 1;
12709 }
12710
12711 // Finally, if there are too many activities now running, try to
12712 // finish as many as we can to get back down to the limit.
12713 for ( i=0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012714 i<mMainStack.mLRUActivities.size()
12715 && mMainStack.mLRUActivities.size() > curMaxActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012716 i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070012717 final ActivityRecord r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012718 = (ActivityRecord)mMainStack.mLRUActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012719
12720 // We can finish this one if we have its icicle saved and
12721 // it is not persistent.
12722 if ((r.haveState || !r.stateNotNeeded) && !r.visible
Dianne Hackborn3c4c2b72010-10-05 18:07:54 -070012723 && r.stopped && !r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012724 final int origSize = mMainStack.mLRUActivities.size();
12725 r.stack.destroyActivityLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012726
12727 // This will remove it from the LRU list, so keep
12728 // our index at the same value. Note that this check to
12729 // see if the size changes is just paranoia -- if
12730 // something unexpected happens, we don't want to end up
12731 // in an infinite loop.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070012732 if (origSize > mMainStack.mLRUActivities.size()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012733 i--;
12734 }
12735 }
12736 }
12737 }
12738 }
12739
12740 /** This method sends the specified signal to each of the persistent apps */
12741 public void signalPersistentProcesses(int sig) throws RemoteException {
12742 if (sig != Process.SIGNAL_USR1) {
12743 throw new SecurityException("Only SIGNAL_USR1 is allowed");
12744 }
12745
12746 synchronized (this) {
12747 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
12748 != PackageManager.PERMISSION_GRANTED) {
12749 throw new SecurityException("Requires permission "
12750 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
12751 }
12752
Dianne Hackborndd71fc82009-12-16 19:24:32 -080012753 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
12754 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012755 if (r.thread != null && r.persistent) {
12756 Process.sendSignal(r.pid, sig);
12757 }
12758 }
12759 }
12760 }
12761
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012762 public boolean profileControl(String process, boolean start,
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012763 String path, ParcelFileDescriptor fd) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012764
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012765 try {
12766 synchronized (this) {
12767 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
12768 // its own permission.
12769 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
12770 != PackageManager.PERMISSION_GRANTED) {
12771 throw new SecurityException("Requires permission "
12772 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012773 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012774
12775 if (start && fd == null) {
12776 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012777 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012778
12779 ProcessRecord proc = null;
12780 try {
12781 int pid = Integer.parseInt(process);
12782 synchronized (mPidsSelfLocked) {
12783 proc = mPidsSelfLocked.get(pid);
12784 }
12785 } catch (NumberFormatException e) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012786 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012787
12788 if (proc == null) {
12789 HashMap<String, SparseArray<ProcessRecord>> all
12790 = mProcessNames.getMap();
12791 SparseArray<ProcessRecord> procs = all.get(process);
12792 if (procs != null && procs.size() > 0) {
12793 proc = procs.valueAt(0);
12794 }
12795 }
12796
12797 if (proc == null || proc.thread == null) {
12798 throw new IllegalArgumentException("Unknown process: " + process);
12799 }
12800
12801 boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0"));
12802 if (isSecure) {
12803 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
12804 throw new SecurityException("Process not debuggable: " + proc);
12805 }
12806 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012807
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012808 proc.thread.profilerControl(start, path, fd);
12809 fd = null;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012810 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070012811 }
12812 } catch (RemoteException e) {
12813 throw new IllegalStateException("Process disappeared");
12814 } finally {
12815 if (fd != null) {
12816 try {
12817 fd.close();
12818 } catch (IOException e) {
12819 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080012820 }
12821 }
12822 }
Andy McFadden824c5102010-07-09 16:26:57 -070012823
12824 public boolean dumpHeap(String process, boolean managed,
12825 String path, ParcelFileDescriptor fd) throws RemoteException {
12826
12827 try {
12828 synchronized (this) {
12829 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
12830 // its own permission (same as profileControl).
12831 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
12832 != PackageManager.PERMISSION_GRANTED) {
12833 throw new SecurityException("Requires permission "
12834 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
12835 }
12836
12837 if (fd == null) {
12838 throw new IllegalArgumentException("null fd");
12839 }
12840
12841 ProcessRecord proc = null;
12842 try {
12843 int pid = Integer.parseInt(process);
12844 synchronized (mPidsSelfLocked) {
12845 proc = mPidsSelfLocked.get(pid);
12846 }
12847 } catch (NumberFormatException e) {
12848 }
12849
12850 if (proc == null) {
12851 HashMap<String, SparseArray<ProcessRecord>> all
12852 = mProcessNames.getMap();
12853 SparseArray<ProcessRecord> procs = all.get(process);
12854 if (procs != null && procs.size() > 0) {
12855 proc = procs.valueAt(0);
12856 }
12857 }
12858
12859 if (proc == null || proc.thread == null) {
12860 throw new IllegalArgumentException("Unknown process: " + process);
12861 }
12862
12863 boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0"));
12864 if (isSecure) {
12865 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
12866 throw new SecurityException("Process not debuggable: " + proc);
12867 }
12868 }
12869
12870 proc.thread.dumpHeap(managed, path, fd);
12871 fd = null;
12872 return true;
12873 }
12874 } catch (RemoteException e) {
12875 throw new IllegalStateException("Process disappeared");
12876 } finally {
12877 if (fd != null) {
12878 try {
12879 fd.close();
12880 } catch (IOException e) {
12881 }
12882 }
12883 }
12884 }
12885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012886 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
12887 public void monitor() {
12888 synchronized (this) { }
12889 }
12890}