blob: b37cd898974541610b3206b4e1e331d963c7307c [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;
58import android.content.Intent;
59import android.content.IntentFilter;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070060import android.content.IIntentReceiver;
61import android.content.IIntentSender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -070062import android.content.IntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.content.pm.ActivityInfo;
64import android.content.pm.ApplicationInfo;
65import android.content.pm.ConfigurationInfo;
66import android.content.pm.IPackageDataObserver;
67import android.content.pm.IPackageManager;
68import android.content.pm.InstrumentationInfo;
Dan Egnor66c40e72010-01-26 16:23:11 -080069import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.content.pm.PackageManager;
Dianne Hackborn2af632f2009-07-08 14:56:37 -070071import android.content.pm.PathPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.content.pm.ProviderInfo;
73import android.content.pm.ResolveInfo;
74import android.content.pm.ServiceInfo;
Dianne Hackborn860755f2010-06-03 18:47:52 -070075import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.content.res.Configuration;
77import android.graphics.Bitmap;
78import android.net.Uri;
79import android.os.Binder;
Dan Egnor60d87622009-12-16 16:32:58 -080080import android.os.Build;
Dan Egnor42471dd2010-01-07 17:25:22 -080081import android.os.Bundle;
Dianne Hackborn3025ef32009-08-31 21:31:47 -070082import android.os.Debug;
Dan Egnor60d87622009-12-16 16:32:58 -080083import android.os.DropBoxManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.os.Environment;
Dan Egnor42471dd2010-01-07 17:25:22 -080085import android.os.FileObserver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.os.FileUtils;
87import android.os.Handler;
88import android.os.IBinder;
89import android.os.IPermissionController;
90import android.os.Looper;
91import android.os.Message;
92import android.os.Parcel;
93import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.os.Process;
Dianne Hackbornb06ea702009-07-13 13:07:51 -070095import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.os.RemoteException;
97import android.os.ServiceManager;
Brad Fitzpatrick46d42382010-06-11 13:57:58 -070098import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.os.SystemClock;
100import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import android.util.Config;
103import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800104import android.util.Slog;
Joe Onorato5d3bea62010-03-01 13:44:29 -0800105import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106import android.util.PrintWriterPrinter;
107import android.util.SparseArray;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -0700108import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import android.view.Gravity;
110import android.view.LayoutInflater;
111import android.view.View;
112import android.view.WindowManager;
113import android.view.WindowManagerPolicy;
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import java.io.File;
116import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import java.io.FileNotFoundException;
Jacek Surazskif5b9c722009-05-18 12:09:59 +0200118import java.io.IOException;
Dan Egnora455d192010-03-12 08:52:28 -0800119import java.io.InputStreamReader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120import java.io.PrintWriter;
121import java.lang.IllegalStateException;
122import java.lang.ref.WeakReference;
123import java.util.ArrayList;
124import java.util.HashMap;
125import java.util.HashSet;
126import java.util.Iterator;
127import java.util.List;
128import java.util.Locale;
129import java.util.Map;
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -0700130import java.util.Set;
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700131import java.util.concurrent.atomic.AtomicBoolean;
132import java.util.concurrent.atomic.AtomicLong;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor {
135 static final String TAG = "ActivityManager";
136 static final boolean DEBUG = false;
137 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
138 static final boolean DEBUG_SWITCH = localLOGV || false;
139 static final boolean DEBUG_TASKS = localLOGV || false;
140 static final boolean DEBUG_PAUSE = localLOGV || false;
141 static final boolean DEBUG_OOM_ADJ = localLOGV || false;
142 static final boolean DEBUG_TRANSITION = localLOGV || false;
143 static final boolean DEBUG_BROADCAST = localLOGV || false;
Dianne Hackborn82f3f002009-06-16 18:49:05 -0700144 static final boolean DEBUG_BROADCAST_LIGHT = DEBUG_BROADCAST || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 static final boolean DEBUG_SERVICE = localLOGV || false;
146 static final boolean DEBUG_VISBILITY = localLOGV || false;
147 static final boolean DEBUG_PROCESSES = localLOGV || false;
Dianne Hackborna1e989b2009-09-01 19:54:29 -0700148 static final boolean DEBUG_PROVIDER = localLOGV || false;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800149 static final boolean DEBUG_URI_PERMISSION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 static final boolean DEBUG_USER_LEAVING = localLOGV || false;
The Android Open Source Project10592532009-03-18 17:39:46 -0700151 static final boolean DEBUG_RESULTS = localLOGV || false;
Christopher Tate436344a2009-09-30 16:17:37 -0700152 static final boolean DEBUG_BACKUP = localLOGV || false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700153 static final boolean DEBUG_CONFIGURATION = localLOGV || false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 static final boolean VALIDATE_TOKENS = false;
155 static final boolean SHOW_ACTIVITY_START_TIME = true;
156
157 // Control over CPU and battery monitoring.
158 static final long BATTERY_STATS_TIME = 30*60*1000; // write battery stats every 30 minutes.
159 static final boolean MONITOR_CPU_USAGE = true;
160 static final long MONITOR_CPU_MIN_TIME = 5*1000; // don't sample cpu less than every 5 seconds.
161 static final long MONITOR_CPU_MAX_TIME = 0x0fffffff; // wait possibly forever for next cpu sample.
162 static final boolean MONITOR_THREAD_CPU_USAGE = false;
163
Dianne Hackborn1655be42009-05-08 14:29:01 -0700164 // The flags that are set for all calls we make to the package manager.
Dianne Hackborn11b822d2009-07-21 20:03:02 -0700165 static final int STOCK_PM_FLAGS = PackageManager.GET_SHARED_LIBRARY_FILES;
Dianne Hackborn1655be42009-05-08 14:29:01 -0700166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 private static final String SYSTEM_SECURE = "ro.secure";
168
169 // This is the maximum number of application processes we would like
170 // to have running. Due to the asynchronous nature of things, we can
171 // temporarily go beyond this limit.
172 static final int MAX_PROCESSES = 2;
173
174 // Set to false to leave processes running indefinitely, relying on
175 // the kernel killing them as resources are required.
176 static final boolean ENFORCE_PROCESS_LIMIT = false;
177
178 // This is the maximum number of activities that we would like to have
179 // running at a given time.
180 static final int MAX_ACTIVITIES = 20;
181
182 // Maximum number of recent tasks that we can remember.
183 static final int MAX_RECENT_TASKS = 20;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700184
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700185 // Amount of time after a call to stopAppSwitches() during which we will
186 // prevent further untrusted switches from happening.
187 static final long APP_SWITCH_DELAY_TIME = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
189 // How long we wait for a launched process to attach to the activity manager
190 // before we decide it's never going to come up for real.
191 static final int PROC_START_TIMEOUT = 10*1000;
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 // How long to wait after going idle before forcing apps to GC.
194 static final int GC_TIMEOUT = 5*1000;
195
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700196 // The minimum amount of time between successive GC requests for a process.
197 static final int GC_MIN_INTERVAL = 60*1000;
198
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700199 // The rate at which we check for apps using excessive wake locks -- 15 mins.
200 static final int WAKE_LOCK_CHECK_DELAY = 15*60*1000;
201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 // How long we allow a receiver to run before giving up on it.
203 static final int BROADCAST_TIMEOUT = 10*1000;
204
205 // How long we wait for a service to finish executing.
206 static final int SERVICE_TIMEOUT = 20*1000;
207
208 // How long a service needs to be running until restarting its process
209 // is no longer considered to be a relaunch of the service.
210 static final int SERVICE_RESTART_DURATION = 5*1000;
211
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700212 // How long a service needs to be running until it will start back at
213 // SERVICE_RESTART_DURATION after being killed.
214 static final int SERVICE_RESET_RUN_DURATION = 60*1000;
215
216 // Multiplying factor to increase restart duration time by, for each time
217 // a service is killed before it has run for SERVICE_RESET_RUN_DURATION.
218 static final int SERVICE_RESTART_DURATION_FACTOR = 4;
219
220 // The minimum amount of time between restarting services that we allow.
221 // That is, when multiple services are restarting, we won't allow each
222 // to restart less than this amount of time from the last one.
223 static final int SERVICE_MIN_RESTART_TIME_BETWEEN = 10*1000;
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 // Maximum amount of time for there to be no activity on a service before
226 // we consider it non-essential and allow its process to go on the
227 // LRU background list.
Dianne Hackbornfd12af42009-08-27 00:44:33 -0700228 static final int MAX_SERVICE_INACTIVITY = 30*60*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
230 // How long we wait until we timeout on key dispatching.
231 static final int KEY_DISPATCHING_TIMEOUT = 5*1000;
232
233 // The minimum time we allow between crashes, for us to consider this
234 // application to be bad and stop and its services and reject broadcasts.
235 static final int MIN_CRASH_INTERVAL = 60*1000;
236
237 // How long we wait until we timeout on key dispatching during instrumentation.
238 static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT = 60*1000;
239
240 // OOM adjustments for processes in various states:
241
242 // This is a process without anything currently running in it. Definitely
243 // the first to go! Value set in system/rootdir/init.rc on startup.
244 // This value is initalized in the constructor, careful when refering to
245 // this static variable externally.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800246 static final int EMPTY_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
248 // This is a process only hosting activities that are not visible,
249 // so it can be killed without any disruption. Value set in
250 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800251 static final int HIDDEN_APP_MAX_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 static int HIDDEN_APP_MIN_ADJ;
253
The Android Open Source Project4df24232009-03-05 14:34:35 -0800254 // This is a process holding the home application -- we want to try
255 // avoiding killing it, even if it would normally be in the background,
256 // because the user interacts with it so much.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800257 static final int HOME_APP_ADJ;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800258
Christopher Tate6fa95972009-06-05 18:43:55 -0700259 // This is a process currently hosting a backup operation. Killing it
260 // is not entirely fatal but is generally a bad idea.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800261 static final int BACKUP_APP_ADJ;
Christopher Tate6fa95972009-06-05 18:43:55 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 // This is a process holding a secondary server -- killing it will not
264 // have much of an impact as far as the user is concerned. Value set in
265 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800266 static final int SECONDARY_SERVER_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700268 // This is a process with a heavy-weight application. It is in the
269 // background, but we want to try to avoid killing it. Value set in
270 // system/rootdir/init.rc on startup.
271 static final int HEAVY_WEIGHT_APP_ADJ;
272
273 // This is a process only hosting components that are perceptible to the
274 // user, and we really want to avoid killing them, but they are not
275 // immediately visible. An example is background music playback. Value set in
276 // system/rootdir/init.rc on startup.
277 static final int PERCEPTIBLE_APP_ADJ;
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 // This is a process only hosting activities that are visible to the
280 // user, so we'd prefer they don't disappear. Value set in
281 // system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800282 static final int VISIBLE_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 // This is the process running the current foreground app. We'd really
285 // rather not kill it! Value set in system/rootdir/init.rc on startup.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800286 static final int FOREGROUND_APP_ADJ;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
288 // This is a process running a core server, such as telephony. Definitely
289 // don't want to kill it, but doing so is not completely fatal.
290 static final int CORE_SERVER_ADJ = -12;
291
292 // The system process runs at the default adjustment.
293 static final int SYSTEM_ADJ = -16;
294
295 // Memory pages are 4K.
296 static final int PAGE_SIZE = 4*1024;
297
298 // Corresponding memory levels for above adjustments.
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800299 static final int EMPTY_APP_MEM;
300 static final int HIDDEN_APP_MEM;
301 static final int HOME_APP_MEM;
302 static final int BACKUP_APP_MEM;
303 static final int SECONDARY_SERVER_MEM;
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700304 static final int HEAVY_WEIGHT_APP_MEM;
305 static final int PERCEPTIBLE_APP_MEM;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800306 static final int VISIBLE_APP_MEM;
307 static final int FOREGROUND_APP_MEM;
308
309 // The minimum number of hidden apps we want to be able to keep around,
310 // without empty apps being able to push them out of memory.
311 static final int MIN_HIDDEN_APPS = 2;
312
Dianne Hackborn8633e682010-04-22 16:03:41 -0700313 // The maximum number of hidden processes we will keep around before
314 // killing them; this is just a control to not let us go too crazy with
315 // keeping around processes on devices with large amounts of RAM.
316 static final int MAX_HIDDEN_APPS = 15;
317
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800318 // We put empty content processes after any hidden processes that have
Dianne Hackborn906497c2010-05-10 15:57:38 -0700319 // been idle for less than 15 seconds.
320 static final long CONTENT_APP_IDLE_OFFSET = 15*1000;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800321
322 // We put empty content processes after any hidden processes that have
Dianne Hackborn906497c2010-05-10 15:57:38 -0700323 // been idle for less than 120 seconds.
324 static final long EMPTY_APP_IDLE_OFFSET = 120*1000;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800325
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700326 static int getIntProp(String name, boolean allowZero) {
327 String str = SystemProperties.get(name);
328 if (str == null) {
329 throw new IllegalArgumentException("Property not defined: " + name);
330 }
331 int val = Integer.valueOf(str);
332 if (val == 0 && !allowZero) {
333 throw new IllegalArgumentException("Property must not be zero: " + name);
334 }
335 return val;
336 }
337
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800338 static {
339 // These values are set in system/rootdir/init.rc on startup.
Dianne Hackborn32907cf2010-06-10 17:50:20 -0700340 FOREGROUND_APP_ADJ = getIntProp("ro.FOREGROUND_APP_ADJ", true);
341 VISIBLE_APP_ADJ = getIntProp("ro.VISIBLE_APP_ADJ", true);
342 PERCEPTIBLE_APP_ADJ = getIntProp("ro.PERCEPTIBLE_APP_ADJ", true);
343 HEAVY_WEIGHT_APP_ADJ = getIntProp("ro.HEAVY_WEIGHT_APP_ADJ", true);
344 SECONDARY_SERVER_ADJ = getIntProp("ro.SECONDARY_SERVER_ADJ", true);
345 BACKUP_APP_ADJ = getIntProp("ro.BACKUP_APP_ADJ", true);
346 HOME_APP_ADJ = getIntProp("ro.HOME_APP_ADJ", true);
347 HIDDEN_APP_MIN_ADJ = getIntProp("ro.HIDDEN_APP_MIN_ADJ", true);
348 EMPTY_APP_ADJ = getIntProp("ro.EMPTY_APP_ADJ", true);
349 // These days we use the last empty slot for hidden apps as well.
350 HIDDEN_APP_MAX_ADJ = EMPTY_APP_ADJ;
351 FOREGROUND_APP_MEM = getIntProp("ro.FOREGROUND_APP_MEM", false)*PAGE_SIZE;
352 VISIBLE_APP_MEM = getIntProp("ro.VISIBLE_APP_MEM", false)*PAGE_SIZE;
353 PERCEPTIBLE_APP_MEM = getIntProp("ro.PERCEPTIBLE_APP_MEM", false)*PAGE_SIZE;
354 HEAVY_WEIGHT_APP_MEM = getIntProp("ro.HEAVY_WEIGHT_APP_MEM", false)*PAGE_SIZE;
355 SECONDARY_SERVER_MEM = getIntProp("ro.SECONDARY_SERVER_MEM", false)*PAGE_SIZE;
356 BACKUP_APP_MEM = getIntProp("ro.BACKUP_APP_MEM", false)*PAGE_SIZE;
357 HOME_APP_MEM = getIntProp("ro.HOME_APP_MEM", false)*PAGE_SIZE;
358 HIDDEN_APP_MEM = getIntProp("ro.HIDDEN_APP_MEM", false)*PAGE_SIZE;
359 EMPTY_APP_MEM = getIntProp("ro.EMPTY_APP_MEM", false)*PAGE_SIZE;
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Dan Egnor42471dd2010-01-07 17:25:22 -0800362 static final int MY_PID = Process.myPid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
364 static final String[] EMPTY_STRING_ARRAY = new String[0];
365
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700366 public ActivityStack mMainStack;
367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700369 * Description of a request to start a new activity, which has been held
370 * due to app switches being disabled.
371 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700372 static class PendingActivityLaunch {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700373 ActivityRecord r;
374 ActivityRecord sourceRecord;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700375 Uri[] grantedUriPermissions;
376 int grantedMode;
377 boolean onlyIfNeeded;
378 }
379
380 final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
381 = new ArrayList<PendingActivityLaunch>();
382
383 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * List of all active broadcasts that are to be executed immediately
385 * (without waiting for another broadcast to finish). Currently this only
386 * contains broadcasts to registered receivers, to avoid spinning up
387 * a bunch of processes to execute IntentReceiver components.
388 */
389 final ArrayList<BroadcastRecord> mParallelBroadcasts
390 = new ArrayList<BroadcastRecord>();
391
392 /**
393 * List of all active broadcasts that are to be executed one at a time.
394 * The object at the top of the list is the currently activity broadcasts;
395 * those after it are waiting for the top to finish..
396 */
397 final ArrayList<BroadcastRecord> mOrderedBroadcasts
398 = new ArrayList<BroadcastRecord>();
399
400 /**
Dianne Hackborn12527f92009-11-11 17:39:50 -0800401 * Historical data of past broadcasts, for debugging.
402 */
403 static final int MAX_BROADCAST_HISTORY = 100;
404 final BroadcastRecord[] mBroadcastHistory
405 = new BroadcastRecord[MAX_BROADCAST_HISTORY];
406
407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 * Set when we current have a BROADCAST_INTENT_MSG in flight.
409 */
410 boolean mBroadcastsScheduled = false;
411
412 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 * Activity we have told the window manager to have key focus.
414 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700415 ActivityRecord mFocusedActivity = null;
Dianne Hackbornbfe319e2009-09-21 00:34:05 -0700416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 * List of intents that were used to start the most recent tasks.
418 */
419 final ArrayList<TaskRecord> mRecentTasks
420 = new ArrayList<TaskRecord>();
421
422 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 * All of the applications we currently have running organized by name.
424 * The keys are strings of the application package name (as
425 * returned by the package manager), and the keys are ApplicationRecord
426 * objects.
427 */
428 final ProcessMap<ProcessRecord> mProcessNames
429 = new ProcessMap<ProcessRecord>();
430
431 /**
Dianne Hackborn860755f2010-06-03 18:47:52 -0700432 * The currently running heavy-weight process, if any.
433 */
434 ProcessRecord mHeavyWeightProcess = null;
435
436 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 * The last time that various processes have crashed.
438 */
439 final ProcessMap<Long> mProcessCrashTimes = new ProcessMap<Long>();
440
441 /**
442 * Set of applications that we consider to be bad, and will reject
443 * incoming broadcasts from (which the user has no control over).
444 * Processes are added to this set when they have crashed twice within
445 * a minimum amount of time; they are removed from it when they are
446 * later restarted (hopefully due to some user action). The value is the
447 * time it was added to the list.
448 */
449 final ProcessMap<Long> mBadProcesses = new ProcessMap<Long>();
450
451 /**
452 * All of the processes we currently have running organized by pid.
453 * The keys are the pid running the application.
454 *
455 * <p>NOTE: This object is protected by its own lock, NOT the global
456 * activity manager lock!
457 */
458 final SparseArray<ProcessRecord> mPidsSelfLocked
459 = new SparseArray<ProcessRecord>();
460
461 /**
462 * All of the processes that have been forced to be foreground. The key
463 * is the pid of the caller who requested it (we hold a death
464 * link on it).
465 */
466 abstract class ForegroundToken implements IBinder.DeathRecipient {
467 int pid;
468 IBinder token;
469 }
470 final SparseArray<ForegroundToken> mForegroundProcesses
471 = new SparseArray<ForegroundToken>();
472
473 /**
474 * List of records for processes that someone had tried to start before the
475 * system was ready. We don't start them at that point, but ensure they
476 * are started by the time booting is complete.
477 */
478 final ArrayList<ProcessRecord> mProcessesOnHold
479 = new ArrayList<ProcessRecord>();
480
481 /**
482 * List of records for processes that we have started and are waiting
483 * for them to call back. This is really only needed when running in
484 * single processes mode, in which case we do not have a unique pid for
485 * each process.
486 */
487 final ArrayList<ProcessRecord> mStartingProcesses
488 = new ArrayList<ProcessRecord>();
489
490 /**
491 * List of persistent applications that are in the process
492 * of being started.
493 */
494 final ArrayList<ProcessRecord> mPersistentStartingProcesses
495 = new ArrayList<ProcessRecord>();
496
497 /**
498 * Processes that are being forcibly torn down.
499 */
500 final ArrayList<ProcessRecord> mRemovedProcesses
501 = new ArrayList<ProcessRecord>();
502
503 /**
504 * List of running applications, sorted by recent usage.
505 * The first entry in the list is the least recently used.
506 * It contains ApplicationRecord objects. This list does NOT include
507 * any persistent application records (since we never want to exit them).
508 */
Dianne Hackborndd71fc82009-12-16 19:24:32 -0800509 final ArrayList<ProcessRecord> mLruProcesses
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 = new ArrayList<ProcessRecord>();
511
512 /**
513 * List of processes that should gc as soon as things are idle.
514 */
515 final ArrayList<ProcessRecord> mProcessesToGc
516 = new ArrayList<ProcessRecord>();
517
518 /**
The Android Open Source Project4df24232009-03-05 14:34:35 -0800519 * This is the process holding what we currently consider to be
520 * the "home" activity.
521 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -0700522 ProcessRecord mHomeProcess;
The Android Open Source Project4df24232009-03-05 14:34:35 -0800523
524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * Set of PendingResultRecord objects that are currently active.
526 */
527 final HashSet mPendingResultRecords = new HashSet();
528
529 /**
530 * Set of IntentSenderRecord objects that are currently active.
531 */
532 final HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>> mIntentSenderRecords
533 = new HashMap<PendingIntentRecord.Key, WeakReference<PendingIntentRecord>>();
534
535 /**
Brad Fitzpatrick143666f2010-06-14 12:40:21 -0700536 * Fingerprints (String.hashCode()) of stack traces that we've
537 * already logged DropBox entries for. Guarded by itself. If
538 * something (rogue user app) forces this over
539 * MAX_DUP_SUPPRESSED_STACKS entries, the contents are cleared.
540 */
541 private final HashSet<Integer> mAlreadyLoggedViolatedStacks = new HashSet<Integer>();
542 private static final int MAX_DUP_SUPPRESSED_STACKS = 5000;
543
544 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -0700545 * Strict Mode background batched logging state.
546 *
547 * The string buffer is guarded by itself, and its lock is also
548 * used to determine if another batched write is already
549 * in-flight.
550 */
551 private final StringBuilder mStrictModeBuffer = new StringBuilder();
552
553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 * Intent broadcast that we have tried to start, but are
555 * waiting for its application's process to be created. We only
556 * need one (instead of a list) because we always process broadcasts
557 * one at a time, so no others can be started while waiting for this
558 * one.
559 */
560 BroadcastRecord mPendingBroadcast = null;
561
562 /**
563 * Keeps track of all IIntentReceivers that have been registered for
564 * broadcasts. Hash keys are the receiver IBinder, hash value is
565 * a ReceiverList.
566 */
567 final HashMap mRegisteredReceivers = new HashMap();
568
569 /**
570 * Resolver for broadcast intents to registered receivers.
571 * Holds BroadcastFilter (subclass of IntentFilter).
572 */
573 final IntentResolver<BroadcastFilter, BroadcastFilter> mReceiverResolver
574 = new IntentResolver<BroadcastFilter, BroadcastFilter>() {
575 @Override
576 protected boolean allowFilterResult(
577 BroadcastFilter filter, List<BroadcastFilter> dest) {
578 IBinder target = filter.receiverList.receiver.asBinder();
579 for (int i=dest.size()-1; i>=0; i--) {
580 if (dest.get(i).receiverList.receiver.asBinder() == target) {
581 return false;
582 }
583 }
584 return true;
585 }
586 };
587
588 /**
589 * State of all active sticky broadcasts. Keys are the action of the
590 * sticky Intent, values are an ArrayList of all broadcasted intents with
591 * that action (which should usually be one).
592 */
593 final HashMap<String, ArrayList<Intent>> mStickyBroadcasts =
594 new HashMap<String, ArrayList<Intent>>();
595
596 /**
597 * All currently running services.
598 */
599 final HashMap<ComponentName, ServiceRecord> mServices =
600 new HashMap<ComponentName, ServiceRecord>();
601
602 /**
603 * All currently running services indexed by the Intent used to start them.
604 */
605 final HashMap<Intent.FilterComparison, ServiceRecord> mServicesByIntent =
606 new HashMap<Intent.FilterComparison, ServiceRecord>();
607
608 /**
609 * All currently bound service connections. Keys are the IBinder of
610 * the client's IServiceConnection.
611 */
612 final HashMap<IBinder, ConnectionRecord> mServiceConnections
613 = new HashMap<IBinder, ConnectionRecord>();
614
615 /**
616 * List of services that we have been asked to start,
617 * but haven't yet been able to. It is used to hold start requests
618 * while waiting for their corresponding application thread to get
619 * going.
620 */
621 final ArrayList<ServiceRecord> mPendingServices
622 = new ArrayList<ServiceRecord>();
623
624 /**
625 * List of services that are scheduled to restart following a crash.
626 */
627 final ArrayList<ServiceRecord> mRestartingServices
628 = new ArrayList<ServiceRecord>();
629
630 /**
631 * List of services that are in the process of being stopped.
632 */
633 final ArrayList<ServiceRecord> mStoppingServices
634 = new ArrayList<ServiceRecord>();
635
636 /**
Christopher Tate181fafa2009-05-14 11:12:14 -0700637 * Backup/restore process management
638 */
639 String mBackupAppName = null;
640 BackupRecord mBackupTarget = null;
641
642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 * List of PendingThumbnailsRecord objects of clients who are still
644 * waiting to receive all of the thumbnails for a task.
645 */
646 final ArrayList mPendingThumbnails = new ArrayList();
647
648 /**
649 * List of HistoryRecord objects that have been finished and must
650 * still report back to a pending thumbnail receiver.
651 */
652 final ArrayList mCancelledThumbnails = new ArrayList();
653
654 /**
655 * All of the currently running global content providers. Keys are a
656 * string containing the provider name and values are a
657 * ContentProviderRecord object containing the data about it. Note
658 * that a single provider may be published under multiple names, so
659 * there may be multiple entries here for a single one in mProvidersByClass.
660 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700661 final HashMap<String, ContentProviderRecord> mProvidersByName
662 = new HashMap<String, ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663
664 /**
665 * All of the currently running global content providers. Keys are a
666 * string containing the provider's implementation class and values are a
667 * ContentProviderRecord object containing the data about it.
668 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700669 final HashMap<String, ContentProviderRecord> mProvidersByClass
670 = new HashMap<String, ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671
672 /**
673 * List of content providers who have clients waiting for them. The
674 * application is currently being launched and the provider will be
675 * removed from this list once it is published.
676 */
Dianne Hackborn860755f2010-06-03 18:47:52 -0700677 final ArrayList<ContentProviderRecord> mLaunchingProviders
678 = new ArrayList<ContentProviderRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679
680 /**
681 * Global set of specific Uri permissions that have been granted.
682 */
683 final private SparseArray<HashMap<Uri, UriPermission>> mGrantedUriPermissions
684 = new SparseArray<HashMap<Uri, UriPermission>>();
685
686 /**
687 * Thread-local storage used to carry caller permissions over through
688 * indirect content-provider access.
689 * @see #ActivityManagerService.openContentUri()
690 */
691 private class Identity {
692 public int pid;
693 public int uid;
694
695 Identity(int _pid, int _uid) {
696 pid = _pid;
697 uid = _uid;
698 }
699 }
700 private static ThreadLocal<Identity> sCallerIdentity = new ThreadLocal<Identity>();
701
702 /**
703 * All information we have collected about the runtime performance of
704 * any user id that can impact battery performance.
705 */
706 final BatteryStatsService mBatteryStatsService;
707
708 /**
709 * information about component usage
710 */
711 final UsageStatsService mUsageStatsService;
712
713 /**
714 * Current configuration information. HistoryRecord objects are given
715 * a reference to this object to indicate which configuration they are
716 * currently running in, so this object must be kept immutable.
717 */
718 Configuration mConfiguration = new Configuration();
719
720 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800721 * Current sequencing integer of the configuration, for skipping old
722 * configurations.
723 */
724 int mConfigurationSeq = 0;
725
726 /**
Jack Palevichb90d28c2009-07-22 15:35:24 -0700727 * Hardware-reported OpenGLES version.
728 */
729 final int GL_ES_VERSION;
730
731 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 * List of initialization arguments to pass to all processes when binding applications to them.
733 * For example, references to the commonly used services.
734 */
735 HashMap<String, IBinder> mAppBindArgs;
736
737 /**
Dianne Hackbornf210d6b2009-04-13 18:42:49 -0700738 * Temporary to avoid allocations. Protected by main lock.
739 */
740 final StringBuilder mStringBuilder = new StringBuilder(256);
741
742 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 * Used to control how we initialize the service.
744 */
745 boolean mStartRunning = false;
746 ComponentName mTopComponent;
747 String mTopAction;
748 String mTopData;
749 boolean mSystemReady = false;
750 boolean mBooting = false;
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700751 boolean mWaitingUpdate = false;
752 boolean mDidUpdate = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753
754 Context mContext;
755
756 int mFactoryTest;
757
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -0700758 boolean mCheckedForSetup;
759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 /**
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700761 * The time at which we will allow normal application switches again,
762 * after a call to {@link #stopAppSwitches()}.
763 */
764 long mAppSwitchesAllowedTime;
765
766 /**
767 * This is set to true after the first switch after mAppSwitchesAllowedTime
768 * is set; any switches after that will clear the time.
769 */
770 boolean mDidAppSwitch;
771
772 /**
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700773 * Last time (in realtime) at which we checked for wake lock usage.
774 */
775 long mLastWakeLockCheckTime;
776
777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 * Set while we are wanting to sleep, to prevent any
779 * activities from being started/resumed.
780 */
781 boolean mSleeping = false;
782
783 /**
Dianne Hackborn55280a92009-05-07 15:53:46 -0700784 * Set if we are shutting down the system, similar to sleeping.
785 */
786 boolean mShuttingDown = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 /**
789 * Task identifier that activities are currently being started
790 * in. Incremented each time a new task is created.
791 * todo: Replace this with a TokenSpace class that generates non-repeating
792 * integers that won't wrap.
793 */
794 int mCurTask = 1;
795
796 /**
797 * Current sequence id for oom_adj computation traversal.
798 */
799 int mAdjSeq = 0;
800
801 /**
Dianne Hackborn906497c2010-05-10 15:57:38 -0700802 * Current sequence id for process LRU updating.
803 */
804 int mLruSeq = 0;
805
806 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 * Set to true if the ANDROID_SIMPLE_PROCESS_MANAGEMENT envvar
808 * is set, indicating the user wants processes started in such a way
809 * that they can use ANDROID_PROCESS_WRAPPER and know what will be
810 * running in each process (thus no pre-initialized process, etc).
811 */
812 boolean mSimpleProcessManagement = false;
813
814 /**
815 * System monitoring: number of processes that died since the last
816 * N procs were started.
817 */
818 int[] mProcDeaths = new int[20];
819
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -0700820 /**
821 * This is set if we had to do a delayed dexopt of an app before launching
822 * it, to increasing the ANR timeouts in that case.
823 */
824 boolean mDidDexOpt;
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 String mDebugApp = null;
827 boolean mWaitForDebugger = false;
828 boolean mDebugTransient = false;
829 String mOrigDebugApp = null;
830 boolean mOrigWaitForDebugger = false;
831 boolean mAlwaysFinishActivities = false;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700832 IActivityController mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700834 final RemoteCallbackList<IActivityWatcher> mWatchers
835 = new RemoteCallbackList<IActivityWatcher>();
836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 /**
838 * Callback of last caller to {@link #requestPss}.
839 */
840 Runnable mRequestPssCallback;
841
842 /**
843 * Remaining processes for which we are waiting results from the last
844 * call to {@link #requestPss}.
845 */
846 final ArrayList<ProcessRecord> mRequestPssList
847 = new ArrayList<ProcessRecord>();
848
849 /**
850 * Runtime statistics collection thread. This object's lock is used to
851 * protect all related state.
852 */
853 final Thread mProcessStatsThread;
854
855 /**
856 * Used to collect process stats when showing not responding dialog.
857 * Protected by mProcessStatsThread.
858 */
859 final ProcessStats mProcessStats = new ProcessStats(
860 MONITOR_THREAD_CPU_USAGE);
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -0700861 final AtomicLong mLastCpuTime = new AtomicLong(0);
862 final AtomicBoolean mProcessStatsMutexFree = new AtomicBoolean(true);
863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 long mLastWriteTime = 0;
865
866 /**
867 * Set to true after the system has finished booting.
868 */
869 boolean mBooted = false;
870
871 int mProcessLimit = 0;
872
873 WindowManagerService mWindowManager;
874
875 static ActivityManagerService mSelf;
876 static ActivityThread mSystemThread;
877
878 private final class AppDeathRecipient implements IBinder.DeathRecipient {
879 final ProcessRecord mApp;
880 final int mPid;
881 final IApplicationThread mAppThread;
882
883 AppDeathRecipient(ProcessRecord app, int pid,
884 IApplicationThread thread) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800885 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 TAG, "New death recipient " + this
887 + " for thread " + thread.asBinder());
888 mApp = app;
889 mPid = pid;
890 mAppThread = thread;
891 }
892
893 public void binderDied() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800894 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 TAG, "Death received in " + this
896 + " for thread " + mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 synchronized(ActivityManagerService.this) {
898 appDiedLocked(mApp, mPid, mAppThread);
899 }
900 }
901 }
902
903 static final int SHOW_ERROR_MSG = 1;
904 static final int SHOW_NOT_RESPONDING_MSG = 2;
905 static final int SHOW_FACTORY_ERROR_MSG = 3;
906 static final int UPDATE_CONFIGURATION_MSG = 4;
907 static final int GC_BACKGROUND_PROCESSES_MSG = 5;
908 static final int WAIT_FOR_DEBUGGER_MSG = 6;
909 static final int BROADCAST_INTENT_MSG = 7;
910 static final int BROADCAST_TIMEOUT_MSG = 8;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 static final int SERVICE_TIMEOUT_MSG = 12;
912 static final int UPDATE_TIME_ZONE = 13;
913 static final int SHOW_UID_ERROR_MSG = 14;
914 static final int IM_FEELING_LUCKY_MSG = 15;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 static final int PROC_START_TIMEOUT_MSG = 20;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700916 static final int DO_PENDING_ACTIVITY_LAUNCHES_MSG = 21;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -0700917 static final int KILL_APPLICATION_MSG = 22;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800918 static final int FINALIZE_PENDING_INTENT_MSG = 23;
Dianne Hackborn860755f2010-06-03 18:47:52 -0700919 static final int POST_HEAVY_NOTIFICATION_MSG = 24;
920 static final int CANCEL_HEAVY_NOTIFICATION_MSG = 25;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700921 static final int SHOW_STRICT_MODE_VIOLATION_MSG = 26;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -0700922 static final int CHECK_EXCESSIVE_WAKE_LOCKS_MSG = 27;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923
924 AlertDialog mUidAlert;
925
926 final Handler mHandler = new Handler() {
927 //public Handler() {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800928 // if (localLOGV) Slog.v(TAG, "Handler started!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 //}
930
931 public void handleMessage(Message msg) {
932 switch (msg.what) {
933 case SHOW_ERROR_MSG: {
934 HashMap data = (HashMap) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 synchronized (ActivityManagerService.this) {
936 ProcessRecord proc = (ProcessRecord)data.get("app");
937 if (proc != null && proc.crashDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800938 Slog.e(TAG, "App already has crash dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 return;
940 }
941 AppErrorResult res = (AppErrorResult) data.get("result");
Dianne Hackborn55280a92009-05-07 15:53:46 -0700942 if (!mSleeping && !mShuttingDown) {
Dan Egnorb7f03672009-12-09 16:22:32 -0800943 Dialog d = new AppErrorDialog(mContext, res, proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 d.show();
945 proc.crashDialog = d;
946 } else {
947 // The device is asleep, so just pretend that the user
948 // saw a crash dialog and hit "force quit".
949 res.set(0);
950 }
951 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700952
953 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 } break;
955 case SHOW_NOT_RESPONDING_MSG: {
956 synchronized (ActivityManagerService.this) {
957 HashMap data = (HashMap) msg.obj;
958 ProcessRecord proc = (ProcessRecord)data.get("app");
959 if (proc != null && proc.anrDialog != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800960 Slog.e(TAG, "App already has anr dialog: " + proc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 return;
962 }
The Android Open Source Project4df24232009-03-05 14:34:35 -0800963
964 broadcastIntentLocked(null, null, new Intent("android.intent.action.ANR"),
965 null, null, 0, null, null, null,
966 false, false, MY_PID, Process.SYSTEM_UID);
967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 Dialog d = new AppNotRespondingDialog(ActivityManagerService.this,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700969 mContext, proc, (ActivityRecord)data.get("activity"));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 d.show();
971 proc.anrDialog = d;
972 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -0700973
Dianne Hackborn9acc0302009-08-25 00:27:12 -0700974 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 } break;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -0700976 case SHOW_STRICT_MODE_VIOLATION_MSG: {
977 HashMap<String, Object> data = (HashMap<String, Object>) msg.obj;
978 synchronized (ActivityManagerService.this) {
979 ProcessRecord proc = (ProcessRecord) data.get("app");
980 if (proc == null) {
981 Slog.e(TAG, "App not found when showing strict mode dialog.");
982 break;
983 }
984 if (proc.crashDialog != null) {
985 Slog.e(TAG, "App already has strict mode dialog: " + proc);
986 return;
987 }
988 AppErrorResult res = (AppErrorResult) data.get("result");
989 if (!mSleeping && !mShuttingDown) {
990 Dialog d = new StrictModeViolationDialog(mContext, res, proc);
991 d.show();
992 proc.crashDialog = d;
993 } else {
994 // The device is asleep, so just pretend that the user
995 // saw a crash dialog and hit "force quit".
996 res.set(0);
997 }
998 }
999 ensureBootCompleted();
1000 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 case SHOW_FACTORY_ERROR_MSG: {
1002 Dialog d = new FactoryErrorDialog(
1003 mContext, msg.getData().getCharSequence("msg"));
1004 d.show();
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001005 ensureBootCompleted();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 } break;
1007 case UPDATE_CONFIGURATION_MSG: {
1008 final ContentResolver resolver = mContext.getContentResolver();
1009 Settings.System.putConfiguration(resolver, (Configuration)msg.obj);
1010 } break;
1011 case GC_BACKGROUND_PROCESSES_MSG: {
1012 synchronized (ActivityManagerService.this) {
1013 performAppGcsIfAppropriateLocked();
1014 }
1015 } break;
1016 case WAIT_FOR_DEBUGGER_MSG: {
1017 synchronized (ActivityManagerService.this) {
1018 ProcessRecord app = (ProcessRecord)msg.obj;
1019 if (msg.arg1 != 0) {
1020 if (!app.waitedForDebugger) {
1021 Dialog d = new AppWaitingForDebuggerDialog(
1022 ActivityManagerService.this,
1023 mContext, app);
1024 app.waitDialog = d;
1025 app.waitedForDebugger = true;
1026 d.show();
1027 }
1028 } else {
1029 if (app.waitDialog != null) {
1030 app.waitDialog.dismiss();
1031 app.waitDialog = null;
1032 }
1033 }
1034 }
1035 } break;
1036 case BROADCAST_INTENT_MSG: {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001037 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 TAG, "Received BROADCAST_INTENT_MSG");
1039 processNextBroadcast(true);
1040 } break;
1041 case BROADCAST_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001042 if (mDidDexOpt) {
1043 mDidDexOpt = false;
1044 Message nmsg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
1045 mHandler.sendMessageDelayed(nmsg, BROADCAST_TIMEOUT);
1046 return;
1047 }
Jeff Hamiltonacf84742010-05-25 22:10:18 -05001048 // Only process broadcast timeouts if the system is ready. That way
1049 // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended
1050 // to do heavy lifting for system up
1051 if (mSystemReady) {
1052 broadcastTimeout();
1053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 case SERVICE_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001056 if (mDidDexOpt) {
1057 mDidDexOpt = false;
1058 Message nmsg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
1059 nmsg.obj = msg.obj;
1060 mHandler.sendMessageDelayed(nmsg, SERVICE_TIMEOUT);
1061 return;
1062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 serviceTimeout((ProcessRecord)msg.obj);
1064 } break;
1065 case UPDATE_TIME_ZONE: {
1066 synchronized (ActivityManagerService.this) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001067 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
1068 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 if (r.thread != null) {
1070 try {
1071 r.thread.updateTimeZone();
1072 } catch (RemoteException ex) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001073 Slog.w(TAG, "Failed to update time zone for: " + r.info.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 }
1075 }
1076 }
1077 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001078 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 case SHOW_UID_ERROR_MSG: {
1080 // XXX This is a temporary dialog, no need to localize.
1081 AlertDialog d = new BaseErrorDialog(mContext);
1082 d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
1083 d.setCancelable(false);
1084 d.setTitle("System UIDs Inconsistent");
1085 d.setMessage("UIDs on the system are inconsistent, you need to wipe your data partition or your device will be unstable.");
1086 d.setButton("I'm Feeling Lucky",
1087 mHandler.obtainMessage(IM_FEELING_LUCKY_MSG));
1088 mUidAlert = d;
1089 d.show();
1090 } break;
1091 case IM_FEELING_LUCKY_MSG: {
1092 if (mUidAlert != null) {
1093 mUidAlert.dismiss();
1094 mUidAlert = null;
1095 }
1096 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 case PROC_START_TIMEOUT_MSG: {
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001098 if (mDidDexOpt) {
1099 mDidDexOpt = false;
1100 Message nmsg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1101 nmsg.obj = msg.obj;
1102 mHandler.sendMessageDelayed(nmsg, PROC_START_TIMEOUT);
1103 return;
1104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 ProcessRecord app = (ProcessRecord)msg.obj;
1106 synchronized (ActivityManagerService.this) {
1107 processStartTimedOutLocked(app);
1108 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001109 } break;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001110 case DO_PENDING_ACTIVITY_LAUNCHES_MSG: {
1111 synchronized (ActivityManagerService.this) {
1112 doPendingActivityLaunchesLocked(true);
1113 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07001114 } break;
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001115 case KILL_APPLICATION_MSG: {
1116 synchronized (ActivityManagerService.this) {
1117 int uid = msg.arg1;
1118 boolean restart = (msg.arg2 == 1);
1119 String pkg = (String) msg.obj;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001120 forceStopPackageLocked(pkg, uid, restart, false, true);
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07001121 }
1122 } break;
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001123 case FINALIZE_PENDING_INTENT_MSG: {
1124 ((PendingIntentRecord)msg.obj).completeFinalize();
1125 } break;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001126 case POST_HEAVY_NOTIFICATION_MSG: {
1127 INotificationManager inm = NotificationManager.getService();
1128 if (inm == null) {
1129 return;
1130 }
1131
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001132 ActivityRecord root = (ActivityRecord)msg.obj;
Dianne Hackborn860755f2010-06-03 18:47:52 -07001133 ProcessRecord process = root.app;
1134 if (process == null) {
1135 return;
1136 }
1137
1138 try {
1139 Context context = mContext.createPackageContext(process.info.packageName, 0);
1140 String text = mContext.getString(R.string.heavy_weight_notification,
1141 context.getApplicationInfo().loadLabel(context.getPackageManager()));
1142 Notification notification = new Notification();
1143 notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
1144 notification.when = 0;
1145 notification.flags = Notification.FLAG_ONGOING_EVENT;
1146 notification.tickerText = text;
1147 notification.defaults = 0; // please be quiet
1148 notification.sound = null;
1149 notification.vibrate = null;
1150 notification.setLatestEventInfo(context, text,
1151 mContext.getText(R.string.heavy_weight_notification_detail),
1152 PendingIntent.getActivity(mContext, 0, root.intent,
1153 PendingIntent.FLAG_CANCEL_CURRENT));
1154
1155 try {
1156 int[] outId = new int[1];
1157 inm.enqueueNotification("android", R.string.heavy_weight_notification,
1158 notification, outId);
1159 } catch (RuntimeException e) {
1160 Slog.w(ActivityManagerService.TAG,
1161 "Error showing notification for heavy-weight app", e);
1162 } catch (RemoteException e) {
1163 }
1164 } catch (NameNotFoundException e) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07001165 Slog.w(TAG, "Unable to create context for heavy notification", e);
Dianne Hackborn860755f2010-06-03 18:47:52 -07001166 }
1167 } break;
1168 case CANCEL_HEAVY_NOTIFICATION_MSG: {
1169 INotificationManager inm = NotificationManager.getService();
1170 if (inm == null) {
1171 return;
1172 }
1173 try {
1174 inm.cancelNotification("android",
1175 R.string.heavy_weight_notification);
1176 } catch (RuntimeException e) {
1177 Slog.w(ActivityManagerService.TAG,
1178 "Error canceling notification for service", e);
1179 } catch (RemoteException e) {
1180 }
1181 } break;
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001182 case CHECK_EXCESSIVE_WAKE_LOCKS_MSG: {
1183 synchronized (ActivityManagerService.this) {
1184 checkExcessiveWakeLocksLocked(true);
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001185 removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001186 if (mSleeping) {
Dianne Hackborn1ebccf52010-08-15 13:04:34 -07001187 Message nmsg = obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
1188 sendMessageDelayed(nmsg, WAKE_LOCK_CHECK_DELAY);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07001189 }
1190 }
1191 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 }
1194 };
1195
1196 public static void setSystemProcess() {
1197 try {
1198 ActivityManagerService m = mSelf;
1199
1200 ServiceManager.addService("activity", m);
1201 ServiceManager.addService("meminfo", new MemBinder(m));
1202 if (MONITOR_CPU_USAGE) {
1203 ServiceManager.addService("cpuinfo", new CpuBinder(m));
1204 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 ServiceManager.addService("permission", new PermissionController(m));
1206
1207 ApplicationInfo info =
1208 mSelf.mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -07001209 "android", STOCK_PM_FLAGS);
Mike Cleron432b7132009-09-24 15:28:29 -07001210 mSystemThread.installSystemApplicationInfo(info);
1211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 synchronized (mSelf) {
1213 ProcessRecord app = mSelf.newProcessRecordLocked(
1214 mSystemThread.getApplicationThread(), info,
1215 info.processName);
1216 app.persistent = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08001217 app.pid = MY_PID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 app.maxAdj = SYSTEM_ADJ;
1219 mSelf.mProcessNames.put(app.processName, app.info.uid, app);
1220 synchronized (mSelf.mPidsSelfLocked) {
1221 mSelf.mPidsSelfLocked.put(app.pid, app);
1222 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001223 mSelf.updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 }
1225 } catch (PackageManager.NameNotFoundException e) {
1226 throw new RuntimeException(
1227 "Unable to find android system package", e);
1228 }
1229 }
1230
1231 public void setWindowManager(WindowManagerService wm) {
1232 mWindowManager = wm;
1233 }
1234
1235 public static final Context main(int factoryTest) {
1236 AThread thr = new AThread();
1237 thr.start();
1238
1239 synchronized (thr) {
1240 while (thr.mService == null) {
1241 try {
1242 thr.wait();
1243 } catch (InterruptedException e) {
1244 }
1245 }
1246 }
1247
1248 ActivityManagerService m = thr.mService;
1249 mSelf = m;
1250 ActivityThread at = ActivityThread.systemMain();
1251 mSystemThread = at;
1252 Context context = at.getSystemContext();
1253 m.mContext = context;
1254 m.mFactoryTest = factoryTest;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001255 m.mMainStack = new ActivityStack(m, context, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256
1257 m.mBatteryStatsService.publish(context);
1258 m.mUsageStatsService.publish(context);
1259
1260 synchronized (thr) {
1261 thr.mReady = true;
1262 thr.notifyAll();
1263 }
1264
1265 m.startRunning(null, null, null, null);
1266
1267 return context;
1268 }
1269
1270 public static ActivityManagerService self() {
1271 return mSelf;
1272 }
1273
1274 static class AThread extends Thread {
1275 ActivityManagerService mService;
1276 boolean mReady = false;
1277
1278 public AThread() {
1279 super("ActivityManager");
1280 }
1281
1282 public void run() {
1283 Looper.prepare();
1284
1285 android.os.Process.setThreadPriority(
1286 android.os.Process.THREAD_PRIORITY_FOREGROUND);
Christopher Tate160edb32010-06-30 17:46:30 -07001287 android.os.Process.setCanSelfBackground(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288
1289 ActivityManagerService m = new ActivityManagerService();
1290
1291 synchronized (this) {
1292 mService = m;
1293 notifyAll();
1294 }
1295
1296 synchronized (this) {
1297 while (!mReady) {
1298 try {
1299 wait();
1300 } catch (InterruptedException e) {
1301 }
1302 }
1303 }
1304
1305 Looper.loop();
1306 }
1307 }
1308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 static class MemBinder extends Binder {
1310 ActivityManagerService mActivityManagerService;
1311 MemBinder(ActivityManagerService activityManagerService) {
1312 mActivityManagerService = activityManagerService;
1313 }
1314
1315 @Override
1316 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1317 ActivityManagerService service = mActivityManagerService;
1318 ArrayList<ProcessRecord> procs;
1319 synchronized (mActivityManagerService) {
1320 if (args != null && args.length > 0
1321 && args[0].charAt(0) != '-') {
1322 procs = new ArrayList<ProcessRecord>();
1323 int pid = -1;
1324 try {
1325 pid = Integer.parseInt(args[0]);
1326 } catch (NumberFormatException e) {
1327
1328 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001329 for (int i=service.mLruProcesses.size()-1; i>=0; i--) {
1330 ProcessRecord proc = service.mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 if (proc.pid == pid) {
1332 procs.add(proc);
1333 } else if (proc.processName.equals(args[0])) {
1334 procs.add(proc);
1335 }
1336 }
1337 if (procs.size() <= 0) {
1338 pw.println("No process found for: " + args[0]);
1339 return;
1340 }
1341 } else {
Dianne Hackborn472ad872010-04-07 17:31:48 -07001342 procs = new ArrayList<ProcessRecord>(service.mLruProcesses);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 }
1344 }
1345 dumpApplicationMemoryUsage(fd, pw, procs, " ", args);
1346 }
1347 }
1348
1349 static class CpuBinder extends Binder {
1350 ActivityManagerService mActivityManagerService;
1351 CpuBinder(ActivityManagerService activityManagerService) {
1352 mActivityManagerService = activityManagerService;
1353 }
1354
1355 @Override
1356 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1357 synchronized (mActivityManagerService.mProcessStatsThread) {
1358 pw.print(mActivityManagerService.mProcessStats.printCurrentState());
1359 }
1360 }
1361 }
1362
1363 private ActivityManagerService() {
1364 String v = System.getenv("ANDROID_SIMPLE_PROCESS_MANAGEMENT");
1365 if (v != null && Integer.getInteger(v) != 0) {
1366 mSimpleProcessManagement = true;
1367 }
1368 v = System.getenv("ANDROID_DEBUG_APP");
1369 if (v != null) {
1370 mSimpleProcessManagement = true;
1371 }
1372
Joe Onorato8a9b2202010-02-26 18:56:32 -08001373 Slog.i(TAG, "Memory class: " + ActivityManager.staticGetMemoryClass());
Dianne Hackborn2c6c5e62009-10-08 17:55:49 -07001374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 File dataDir = Environment.getDataDirectory();
1376 File systemDir = new File(dataDir, "system");
1377 systemDir.mkdirs();
1378 mBatteryStatsService = new BatteryStatsService(new File(
1379 systemDir, "batterystats.bin").toString());
1380 mBatteryStatsService.getActiveStatistics().readLocked();
1381 mBatteryStatsService.getActiveStatistics().writeLocked();
1382
1383 mUsageStatsService = new UsageStatsService( new File(
Dianne Hackborn6447ca32009-04-07 19:50:08 -07001384 systemDir, "usagestats").toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385
Jack Palevichb90d28c2009-07-22 15:35:24 -07001386 GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
1387 ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
1388
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001389 mConfiguration.setToDefaults();
1390 mConfiguration.locale = Locale.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 mProcessStats.init();
1392
1393 // Add ourself to the Watchdog monitors.
1394 Watchdog.getInstance().addMonitor(this);
1395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 mProcessStatsThread = new Thread("ProcessStats") {
1397 public void run() {
1398 while (true) {
1399 try {
1400 try {
1401 synchronized(this) {
1402 final long now = SystemClock.uptimeMillis();
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001403 long nextCpuDelay = (mLastCpuTime.get()+MONITOR_CPU_MAX_TIME)-now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 long nextWriteDelay = (mLastWriteTime+BATTERY_STATS_TIME)-now;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001405 //Slog.i(TAG, "Cpu delay=" + nextCpuDelay
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 // + ", write delay=" + nextWriteDelay);
1407 if (nextWriteDelay < nextCpuDelay) {
1408 nextCpuDelay = nextWriteDelay;
1409 }
1410 if (nextCpuDelay > 0) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001411 mProcessStatsMutexFree.set(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 this.wait(nextCpuDelay);
1413 }
1414 }
1415 } catch (InterruptedException e) {
1416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 updateCpuStatsNow();
1418 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001419 Slog.e(TAG, "Unexpected exception collecting process stats", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421 }
1422 }
1423 };
1424 mProcessStatsThread.start();
1425 }
1426
1427 @Override
1428 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
1429 throws RemoteException {
1430 try {
1431 return super.onTransact(code, data, reply, flags);
1432 } catch (RuntimeException e) {
1433 // The activity manager only throws security exceptions, so let's
1434 // log all others.
1435 if (!(e instanceof SecurityException)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001436 Slog.e(TAG, "Activity Manager Crash", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 }
1438 throw e;
1439 }
1440 }
1441
1442 void updateCpuStats() {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001443 final long now = SystemClock.uptimeMillis();
1444 if (mLastCpuTime.get() >= now - MONITOR_CPU_MIN_TIME) {
1445 return;
1446 }
1447 if (mProcessStatsMutexFree.compareAndSet(true, false)) {
1448 synchronized (mProcessStatsThread) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 mProcessStatsThread.notify();
1450 }
1451 }
1452 }
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 void updateCpuStatsNow() {
1455 synchronized (mProcessStatsThread) {
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001456 mProcessStatsMutexFree.set(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 final long now = SystemClock.uptimeMillis();
1458 boolean haveNewCpuStats = false;
Amith Yamasanieaeb6632009-06-03 15:16:10 -07001459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (MONITOR_CPU_USAGE &&
Brad Fitzpatrick01fad4a2010-04-19 10:47:40 -07001461 mLastCpuTime.get() < (now-MONITOR_CPU_MIN_TIME)) {
1462 mLastCpuTime.set(now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 haveNewCpuStats = true;
1464 mProcessStats.update();
Joe Onorato8a9b2202010-02-26 18:56:32 -08001465 //Slog.i(TAG, mProcessStats.printCurrentState());
1466 //Slog.i(TAG, "Total CPU usage: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 // + mProcessStats.getTotalCpuPercent() + "%");
1468
Joe Onorato8a9b2202010-02-26 18:56:32 -08001469 // Slog the cpu usage if the property is set.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 if ("true".equals(SystemProperties.get("events.cpu"))) {
1471 int user = mProcessStats.getLastUserTime();
1472 int system = mProcessStats.getLastSystemTime();
1473 int iowait = mProcessStats.getLastIoWaitTime();
1474 int irq = mProcessStats.getLastIrqTime();
1475 int softIrq = mProcessStats.getLastSoftIrqTime();
1476 int idle = mProcessStats.getLastIdleTime();
1477
1478 int total = user + system + iowait + irq + softIrq + idle;
1479 if (total == 0) total = 1;
1480
Doug Zongker2bec3d42009-12-04 12:52:44 -08001481 EventLog.writeEvent(EventLogTags.CPU,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 ((user+system+iowait+irq+softIrq) * 100) / total,
1483 (user * 100) / total,
1484 (system * 100) / total,
1485 (iowait * 100) / total,
1486 (irq * 100) / total,
1487 (softIrq * 100) / total);
1488 }
1489 }
1490
Amith Yamasanie43530a2009-08-21 13:11:37 -07001491 long[] cpuSpeedTimes = mProcessStats.getLastCpuSpeedTimes();
Amith Yamasani819f9282009-06-24 23:18:15 -07001492 final BatteryStatsImpl bstats = mBatteryStatsService.getActiveStatistics();
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001493 synchronized(bstats) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 synchronized(mPidsSelfLocked) {
1495 if (haveNewCpuStats) {
1496 if (mBatteryStatsService.isOnBattery()) {
1497 final int N = mProcessStats.countWorkingStats();
1498 for (int i=0; i<N; i++) {
1499 ProcessStats.Stats st
1500 = mProcessStats.getWorkingStats(i);
1501 ProcessRecord pr = mPidsSelfLocked.get(st.pid);
1502 if (pr != null) {
1503 BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
1504 ps.addCpuTimeLocked(st.rel_utime, st.rel_stime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001505 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001506 } else {
1507 BatteryStatsImpl.Uid.Proc ps =
Amith Yamasani819f9282009-06-24 23:18:15 -07001508 bstats.getProcessStatsLocked(st.name, st.pid);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001509 if (ps != null) {
1510 ps.addCpuTimeLocked(st.rel_utime, st.rel_stime);
Amith Yamasanie43530a2009-08-21 13:11:37 -07001511 ps.addSpeedStepTimes(cpuSpeedTimes);
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 }
1514 }
1515 }
1516 }
1517 }
Amith Yamasani32dbefd2009-06-19 09:21:17 -07001518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 if (mLastWriteTime < (now-BATTERY_STATS_TIME)) {
1520 mLastWriteTime = now;
1521 mBatteryStatsService.getActiveStatistics().writeLocked();
1522 }
1523 }
1524 }
1525 }
1526
1527 /**
1528 * Initialize the application bind args. These are passed to each
1529 * process when the bindApplication() IPC is sent to the process. They're
1530 * lazily setup to make sure the services are running when they're asked for.
1531 */
1532 private HashMap<String, IBinder> getCommonServicesLocked() {
1533 if (mAppBindArgs == null) {
1534 mAppBindArgs = new HashMap<String, IBinder>();
1535
1536 // Setup the application init args
1537 mAppBindArgs.put("package", ServiceManager.getService("package"));
1538 mAppBindArgs.put("window", ServiceManager.getService("window"));
1539 mAppBindArgs.put(Context.ALARM_SERVICE,
1540 ServiceManager.getService(Context.ALARM_SERVICE));
1541 }
1542 return mAppBindArgs;
1543 }
1544
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001545 final void setFocusedActivityLocked(ActivityRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if (mFocusedActivity != r) {
1547 mFocusedActivity = r;
1548 mWindowManager.setFocusedApp(r, true);
1549 }
1550 }
1551
Dianne Hackborn906497c2010-05-10 15:57:38 -07001552 private final void updateLruProcessInternalLocked(ProcessRecord app,
1553 boolean oomAdj, boolean updateActivityTime, int bestPos) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 // put it on the LRU to keep track of when it should be exited.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001555 int lrui = mLruProcesses.indexOf(app);
1556 if (lrui >= 0) mLruProcesses.remove(lrui);
1557
1558 int i = mLruProcesses.size()-1;
1559 int skipTop = 0;
1560
Dianne Hackborn906497c2010-05-10 15:57:38 -07001561 app.lruSeq = mLruSeq;
1562
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001563 // compute the new weight for this process.
1564 if (updateActivityTime) {
1565 app.lastActivityTime = SystemClock.uptimeMillis();
1566 }
1567 if (app.activities.size() > 0) {
1568 // If this process has activities, we more strongly want to keep
1569 // it around.
1570 app.lruWeight = app.lastActivityTime;
1571 } else if (app.pubProviders.size() > 0) {
1572 // If this process contains content providers, we want to keep
1573 // it a little more strongly.
1574 app.lruWeight = app.lastActivityTime - CONTENT_APP_IDLE_OFFSET;
1575 // Also don't let it kick out the first few "real" hidden processes.
1576 skipTop = MIN_HIDDEN_APPS;
1577 } else {
1578 // If this process doesn't have activities, we less strongly
1579 // want to keep it around, and generally want to avoid getting
1580 // in front of any very recently used activities.
1581 app.lruWeight = app.lastActivityTime - EMPTY_APP_IDLE_OFFSET;
1582 // Also don't let it kick out the first few "real" hidden processes.
1583 skipTop = MIN_HIDDEN_APPS;
1584 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001585
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001586 while (i >= 0) {
1587 ProcessRecord p = mLruProcesses.get(i);
1588 // If this app shouldn't be in front of the first N background
1589 // apps, then skip over that many that are currently hidden.
1590 if (skipTop > 0 && p.setAdj >= HIDDEN_APP_MIN_ADJ) {
1591 skipTop--;
1592 }
Dianne Hackborn906497c2010-05-10 15:57:38 -07001593 if (p.lruWeight <= app.lruWeight || i < bestPos) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08001594 mLruProcesses.add(i+1, app);
1595 break;
1596 }
1597 i--;
1598 }
1599 if (i < 0) {
1600 mLruProcesses.add(0, app);
1601 }
1602
Dianne Hackborn906497c2010-05-10 15:57:38 -07001603 // If the app is currently using a content provider or service,
1604 // bump those processes as well.
1605 if (app.connections.size() > 0) {
1606 for (ConnectionRecord cr : app.connections) {
1607 if (cr.binding != null && cr.binding.service != null
1608 && cr.binding.service.app != null
1609 && cr.binding.service.app.lruSeq != mLruSeq) {
1610 updateLruProcessInternalLocked(cr.binding.service.app, oomAdj,
1611 updateActivityTime, i+1);
1612 }
1613 }
1614 }
1615 if (app.conProviders.size() > 0) {
1616 for (ContentProviderRecord cpr : app.conProviders.keySet()) {
1617 if (cpr.app != null && cpr.app.lruSeq != mLruSeq) {
1618 updateLruProcessInternalLocked(cpr.app, oomAdj,
1619 updateActivityTime, i+1);
1620 }
1621 }
1622 }
1623
Joe Onorato8a9b2202010-02-26 18:56:32 -08001624 //Slog.i(TAG, "Putting proc to front: " + app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 if (oomAdj) {
1626 updateOomAdjLocked();
1627 }
1628 }
1629
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001630 final void updateLruProcessLocked(ProcessRecord app,
Dianne Hackborn906497c2010-05-10 15:57:38 -07001631 boolean oomAdj, boolean updateActivityTime) {
1632 mLruSeq++;
1633 updateLruProcessInternalLocked(app, oomAdj, updateActivityTime, 0);
1634 }
1635
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001636 final ProcessRecord getProcessRecordLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 String processName, int uid) {
1638 if (uid == Process.SYSTEM_UID) {
1639 // The system gets to run in any process. If there are multiple
1640 // processes with the same uid, just pick the first (this
1641 // should never happen).
1642 SparseArray<ProcessRecord> procs = mProcessNames.getMap().get(
1643 processName);
1644 return procs != null ? procs.valueAt(0) : null;
1645 }
1646 ProcessRecord proc = mProcessNames.get(processName, uid);
1647 return proc;
1648 }
1649
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001650 void ensurePackageDexOpt(String packageName) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001651 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07001652 try {
1653 if (pm.performDexOpt(packageName)) {
1654 mDidDexOpt = true;
1655 }
1656 } catch (RemoteException e) {
1657 }
1658 }
1659
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001660 boolean isNextTransitionForward() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 int transit = mWindowManager.getPendingAppTransition();
1662 return transit == WindowManagerPolicy.TRANSIT_ACTIVITY_OPEN
1663 || transit == WindowManagerPolicy.TRANSIT_TASK_OPEN
1664 || transit == WindowManagerPolicy.TRANSIT_TASK_TO_FRONT;
1665 }
1666
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001667 final ProcessRecord startProcessLocked(String processName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 ApplicationInfo info, boolean knownToBeDead, int intentFlags,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001669 String hostingType, ComponentName hostingName, boolean allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 ProcessRecord app = getProcessRecordLocked(processName, info.uid);
1671 // We don't have to do anything more if:
1672 // (1) There is an existing application record; and
1673 // (2) The caller doesn't think it is dead, OR there is no thread
1674 // object attached to it so we know it couldn't have crashed; and
1675 // (3) There is a pid assigned to it, so it is either starting or
1676 // already running.
Joe Onorato8a9b2202010-02-26 18:56:32 -08001677 if (DEBUG_PROCESSES) Slog.v(TAG, "startProcess: name=" + processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 + " app=" + app + " knownToBeDead=" + knownToBeDead
1679 + " thread=" + (app != null ? app.thread : null)
1680 + " pid=" + (app != null ? app.pid : -1));
Magnus Edlund7bb25812010-02-24 15:45:06 +01001681 if (app != null && app.pid > 0) {
1682 if (!knownToBeDead || app.thread == null) {
Dianne Hackborn20cb56e2010-03-04 00:58:29 -08001683 // We already have the app running, or are waiting for it to
1684 // come up (we have a pid but not yet its thread), so keep it.
Magnus Edlund7bb25812010-02-24 15:45:06 +01001685 return app;
1686 } else {
1687 // An application record is attached to a previous process,
1688 // clean it up now.
1689 handleAppDiedLocked(app, true);
1690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01001692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 String hostingNameStr = hostingName != null
1694 ? hostingName.flattenToShortString() : null;
1695
1696 if ((intentFlags&Intent.FLAG_FROM_BACKGROUND) != 0) {
1697 // If we are in the background, then check to see if this process
1698 // is bad. If so, we will just silently fail.
1699 if (mBadProcesses.get(info.processName, info.uid) != null) {
1700 return null;
1701 }
1702 } else {
1703 // When the user is explicitly starting a process, then clear its
1704 // crash count so that we won't make it bad until they see at
1705 // least one crash dialog again, and make the process good again
1706 // if it had been bad.
1707 mProcessCrashTimes.remove(info.processName, info.uid);
1708 if (mBadProcesses.get(info.processName, info.uid) != null) {
Doug Zongker2bec3d42009-12-04 12:52:44 -08001709 EventLog.writeEvent(EventLogTags.AM_PROC_GOOD, info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 info.processName);
1711 mBadProcesses.remove(info.processName, info.uid);
1712 if (app != null) {
1713 app.bad = false;
1714 }
1715 }
1716 }
1717
1718 if (app == null) {
1719 app = newProcessRecordLocked(null, info, processName);
1720 mProcessNames.put(processName, info.uid, app);
1721 } else {
1722 // If this is a new package in the process, add the package to the list
1723 app.addPackage(info.packageName);
1724 }
1725
1726 // If the system is not ready yet, then hold off on starting this
1727 // process until it is.
1728 if (!mSystemReady
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001729 && !isAllowedWhileBooting(info)
1730 && !allowWhileBooting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 if (!mProcessesOnHold.contains(app)) {
1732 mProcessesOnHold.add(app);
1733 }
1734 return app;
1735 }
1736
1737 startProcessLocked(app, hostingType, hostingNameStr);
1738 return (app.pid != 0) ? app : null;
1739 }
1740
Dianne Hackborn9acc0302009-08-25 00:27:12 -07001741 boolean isAllowedWhileBooting(ApplicationInfo ai) {
1742 return (ai.flags&ApplicationInfo.FLAG_PERSISTENT) != 0;
1743 }
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 private final void startProcessLocked(ProcessRecord app,
1746 String hostingType, String hostingNameStr) {
1747 if (app.pid > 0 && app.pid != MY_PID) {
1748 synchronized (mPidsSelfLocked) {
1749 mPidsSelfLocked.remove(app.pid);
1750 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
1751 }
1752 app.pid = 0;
1753 }
1754
1755 mProcessesOnHold.remove(app);
1756
1757 updateCpuStats();
1758
1759 System.arraycopy(mProcDeaths, 0, mProcDeaths, 1, mProcDeaths.length-1);
1760 mProcDeaths[0] = 0;
1761
1762 try {
1763 int uid = app.info.uid;
1764 int[] gids = null;
1765 try {
1766 gids = mContext.getPackageManager().getPackageGids(
1767 app.info.packageName);
1768 } catch (PackageManager.NameNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001769 Slog.w(TAG, "Unable to retrieve gids", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 }
1771 if (mFactoryTest != SystemServer.FACTORY_TEST_OFF) {
1772 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
1773 && mTopComponent != null
1774 && app.processName.equals(mTopComponent.getPackageName())) {
1775 uid = 0;
1776 }
1777 if (mFactoryTest == SystemServer.FACTORY_TEST_HIGH_LEVEL
1778 && (app.info.flags&ApplicationInfo.FLAG_FACTORY_TEST) != 0) {
1779 uid = 0;
1780 }
1781 }
1782 int debugFlags = 0;
1783 if ((app.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
1784 debugFlags |= Zygote.DEBUG_ENABLE_DEBUGGER;
1785 }
Ben Cheng6c0afff2010-02-14 16:18:56 -08001786 // Run the app in safe mode if its manifest requests so or the
1787 // system is booted in safe mode.
1788 if ((app.info.flags & ApplicationInfo.FLAG_VM_SAFE_MODE) != 0 ||
1789 Zygote.systemInSafeMode == true) {
Ben Cheng23085b72010-02-08 16:06:32 -08001790 debugFlags |= Zygote.DEBUG_ENABLE_SAFEMODE;
1791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 if ("1".equals(SystemProperties.get("debug.checkjni"))) {
1793 debugFlags |= Zygote.DEBUG_ENABLE_CHECKJNI;
1794 }
1795 if ("1".equals(SystemProperties.get("debug.assert"))) {
1796 debugFlags |= Zygote.DEBUG_ENABLE_ASSERT;
1797 }
1798 int pid = Process.start("android.app.ActivityThread",
1799 mSimpleProcessManagement ? app.processName : null, uid, uid,
1800 gids, debugFlags, null);
1801 BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
1802 synchronized (bs) {
1803 if (bs.isOnBattery()) {
1804 app.batteryStats.incStartsLocked();
1805 }
1806 }
1807
Doug Zongker2bec3d42009-12-04 12:52:44 -08001808 EventLog.writeEvent(EventLogTags.AM_PROC_START, pid, uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 app.processName, hostingType,
1810 hostingNameStr != null ? hostingNameStr : "");
1811
1812 if (app.persistent) {
Christopher Tatec27181c2010-06-30 14:41:09 -07001813 Watchdog.getInstance().processStarted(app.processName, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07001816 StringBuilder buf = mStringBuilder;
1817 buf.setLength(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 buf.append("Start proc ");
1819 buf.append(app.processName);
1820 buf.append(" for ");
1821 buf.append(hostingType);
1822 if (hostingNameStr != null) {
1823 buf.append(" ");
1824 buf.append(hostingNameStr);
1825 }
1826 buf.append(": pid=");
1827 buf.append(pid);
1828 buf.append(" uid=");
1829 buf.append(uid);
1830 buf.append(" gids={");
1831 if (gids != null) {
1832 for (int gi=0; gi<gids.length; gi++) {
1833 if (gi != 0) buf.append(", ");
1834 buf.append(gids[gi]);
1835
1836 }
1837 }
1838 buf.append("}");
Joe Onorato8a9b2202010-02-26 18:56:32 -08001839 Slog.i(TAG, buf.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 if (pid == 0 || pid == MY_PID) {
1841 // Processes are being emulated with threads.
1842 app.pid = MY_PID;
1843 app.removed = false;
1844 mStartingProcesses.add(app);
1845 } else if (pid > 0) {
1846 app.pid = pid;
1847 app.removed = false;
1848 synchronized (mPidsSelfLocked) {
1849 this.mPidsSelfLocked.put(pid, app);
1850 Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
1851 msg.obj = app;
1852 mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT);
1853 }
1854 } else {
1855 app.pid = 0;
1856 RuntimeException e = new RuntimeException(
1857 "Failure starting process " + app.processName
1858 + ": returned pid=" + pid);
Joe Onorato8a9b2202010-02-26 18:56:32 -08001859 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 } catch (RuntimeException e) {
1862 // XXX do better error recovery.
1863 app.pid = 0;
Joe Onorato8a9b2202010-02-26 18:56:32 -08001864 Slog.e(TAG, "Failure starting process " + app.processName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 }
1866 }
1867
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001868 void updateUsageStats(ActivityRecord resumedComponent, boolean resumed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 if (resumed) {
1870 mUsageStatsService.noteResumeComponent(resumedComponent.realActivity);
1871 } else {
1872 mUsageStatsService.notePauseComponent(resumedComponent.realActivity);
1873 }
1874 }
1875
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001876 boolean startHomeActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07001877 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
1878 && mTopAction == null) {
1879 // We are running in factory test mode, but unable to find
1880 // the factory test app, so just sit around displaying the
1881 // error message and don't try to start anything.
1882 return false;
1883 }
1884 Intent intent = new Intent(
1885 mTopAction,
1886 mTopData != null ? Uri.parse(mTopData) : null);
1887 intent.setComponent(mTopComponent);
1888 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
1889 intent.addCategory(Intent.CATEGORY_HOME);
1890 }
1891 ActivityInfo aInfo =
1892 intent.resolveActivityInfo(mContext.getPackageManager(),
1893 STOCK_PM_FLAGS);
1894 if (aInfo != null) {
1895 intent.setComponent(new ComponentName(
1896 aInfo.applicationInfo.packageName, aInfo.name));
1897 // Don't do this if the home app is currently being
1898 // instrumented.
1899 ProcessRecord app = getProcessRecordLocked(aInfo.processName,
1900 aInfo.applicationInfo.uid);
1901 if (app == null || app.instrumentationClass == null) {
1902 intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001903 mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07001904 null, null, 0, 0, 0, false, false);
1905 }
1906 }
1907
1908
1909 return true;
1910 }
1911
1912 /**
1913 * Starts the "new version setup screen" if appropriate.
1914 */
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001915 void startSetupActivityLocked() {
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07001916 // Only do this once per boot.
1917 if (mCheckedForSetup) {
1918 return;
1919 }
1920
1921 // We will show this screen if the current one is a different
1922 // version than the last one shown, and we are not running in
1923 // low-level factory test mode.
1924 final ContentResolver resolver = mContext.getContentResolver();
1925 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL &&
1926 Settings.Secure.getInt(resolver,
1927 Settings.Secure.DEVICE_PROVISIONED, 0) != 0) {
1928 mCheckedForSetup = true;
1929
1930 // See if we should be showing the platform update setup UI.
1931 Intent intent = new Intent(Intent.ACTION_UPGRADE_SETUP);
1932 List<ResolveInfo> ris = mSelf.mContext.getPackageManager()
1933 .queryIntentActivities(intent, PackageManager.GET_META_DATA);
1934
1935 // We don't allow third party apps to replace this.
1936 ResolveInfo ri = null;
1937 for (int i=0; ris != null && i<ris.size(); i++) {
1938 if ((ris.get(i).activityInfo.applicationInfo.flags
1939 & ApplicationInfo.FLAG_SYSTEM) != 0) {
1940 ri = ris.get(i);
1941 break;
1942 }
1943 }
1944
1945 if (ri != null) {
1946 String vers = ri.activityInfo.metaData != null
1947 ? ri.activityInfo.metaData.getString(Intent.METADATA_SETUP_VERSION)
1948 : null;
1949 if (vers == null && ri.activityInfo.applicationInfo.metaData != null) {
1950 vers = ri.activityInfo.applicationInfo.metaData.getString(
1951 Intent.METADATA_SETUP_VERSION);
1952 }
1953 String lastVers = Settings.Secure.getString(
1954 resolver, Settings.Secure.LAST_SETUP_SHOWN);
1955 if (vers != null && !vers.equals(lastVers)) {
1956 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1957 intent.setComponent(new ComponentName(
1958 ri.activityInfo.packageName, ri.activityInfo.name));
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001959 mMainStack.startActivityLocked(null, intent, null, null, 0, ri.activityInfo,
Dianne Hackbornd7cd29d2009-07-01 11:22:45 -07001960 null, null, 0, 0, 0, false, false);
1961 }
1962 }
1963 }
1964 }
1965
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001966 void reportResumedActivityLocked(ActivityRecord r) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001967 //Slog.i(TAG, "**** REPORT RESUME: " + r);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001968
1969 final int identHash = System.identityHashCode(r);
1970 updateUsageStats(r, true);
1971
1972 int i = mWatchers.beginBroadcast();
1973 while (i > 0) {
1974 i--;
1975 IActivityWatcher w = mWatchers.getBroadcastItem(i);
1976 if (w != null) {
1977 try {
1978 w.activityResuming(identHash);
1979 } catch (RemoteException e) {
1980 }
1981 }
1982 }
1983 mWatchers.finishBroadcast();
1984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001986 final void doPendingActivityLaunchesLocked(boolean doResume) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001987 final int N = mPendingActivityLaunches.size();
1988 if (N <= 0) {
1989 return;
1990 }
1991 for (int i=0; i<N; i++) {
1992 PendingActivityLaunch pal = mPendingActivityLaunches.get(i);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07001993 mMainStack.startActivityUncheckedLocked(pal.r, pal.sourceRecord,
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07001994 pal.grantedUriPermissions, pal.grantedMode, pal.onlyIfNeeded,
1995 doResume && i == (N-1));
1996 }
1997 mPendingActivityLaunches.clear();
1998 }
1999
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002000 public final int startActivity(IApplicationThread caller,
2001 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2002 int grantedMode, IBinder resultTo,
2003 String resultWho, int requestCode, boolean onlyIfNeeded,
2004 boolean debug) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002005 return mMainStack.startActivityMayWait(caller, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002006 grantedUriPermissions, grantedMode, resultTo, resultWho,
2007 requestCode, onlyIfNeeded, debug, null, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002008 }
2009
2010 public final WaitResult startActivityAndWait(IApplicationThread caller,
2011 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2012 int grantedMode, IBinder resultTo,
2013 String resultWho, int requestCode, boolean onlyIfNeeded,
2014 boolean debug) {
2015 WaitResult res = new WaitResult();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002016 mMainStack.startActivityMayWait(caller, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002017 grantedUriPermissions, grantedMode, resultTo, resultWho,
2018 requestCode, onlyIfNeeded, debug, res, null);
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002019 return res;
2020 }
2021
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002022 public final int startActivityWithConfig(IApplicationThread caller,
2023 Intent intent, String resolvedType, Uri[] grantedUriPermissions,
2024 int grantedMode, IBinder resultTo,
2025 String resultWho, int requestCode, boolean onlyIfNeeded,
2026 boolean debug, Configuration config) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002027 return mMainStack.startActivityMayWait(caller, intent, resolvedType,
Dianne Hackborn2ccda4d2010-03-22 21:49:15 -07002028 grantedUriPermissions, grantedMode, resultTo, resultWho,
2029 requestCode, onlyIfNeeded, debug, null, config);
2030 }
2031
Dianne Hackborn8f7f35e2010-02-25 18:48:12 -08002032 public int startActivityIntentSender(IApplicationThread caller,
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002033 IntentSender intent, Intent fillInIntent, String resolvedType,
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002034 IBinder resultTo, String resultWho, int requestCode,
2035 int flagsMask, int flagsValues) {
2036 // Refuse possible leaked file descriptors
2037 if (fillInIntent != null && fillInIntent.hasFileDescriptors()) {
2038 throw new IllegalArgumentException("File descriptors passed in Intent");
2039 }
2040
2041 IIntentSender sender = intent.getTarget();
2042 if (!(sender instanceof PendingIntentRecord)) {
2043 throw new IllegalArgumentException("Bad PendingIntent object");
2044 }
2045
2046 PendingIntentRecord pir = (PendingIntentRecord)sender;
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002047
2048 synchronized (this) {
2049 // If this is coming from the currently resumed activity, it is
2050 // effectively saying that app switches are allowed at this point.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002051 if (mMainStack.mResumedActivity != null
2052 && mMainStack.mResumedActivity.info.applicationInfo.uid ==
Dianne Hackbornfa82f222009-09-17 15:14:12 -07002053 Binder.getCallingUid()) {
2054 mAppSwitchesAllowedTime = 0;
2055 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -07002056 }
2057
2058 return pir.sendInner(0, fillInIntent, resolvedType,
2059 null, resultTo, resultWho, requestCode, flagsMask, flagsValues);
2060 }
2061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 public boolean startNextMatchingActivity(IBinder callingActivity,
2063 Intent intent) {
2064 // Refuse possible leaked file descriptors
2065 if (intent != null && intent.hasFileDescriptors() == true) {
2066 throw new IllegalArgumentException("File descriptors passed in Intent");
2067 }
2068
2069 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002070 int index = mMainStack.indexOfTokenLocked(callingActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 if (index < 0) {
2072 return false;
2073 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002074 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 if (r.app == null || r.app.thread == null) {
2076 // The caller is not running... d'oh!
2077 return false;
2078 }
2079 intent = new Intent(intent);
2080 // The caller is not allowed to change the data.
2081 intent.setDataAndType(r.intent.getData(), r.intent.getType());
2082 // And we are resetting to find the next component...
2083 intent.setComponent(null);
2084
2085 ActivityInfo aInfo = null;
2086 try {
2087 List<ResolveInfo> resolves =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002088 AppGlobals.getPackageManager().queryIntentActivities(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 intent, r.resolvedType,
Dianne Hackborn1655be42009-05-08 14:29:01 -07002090 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091
2092 // Look for the original activity in the list...
2093 final int N = resolves != null ? resolves.size() : 0;
2094 for (int i=0; i<N; i++) {
2095 ResolveInfo rInfo = resolves.get(i);
2096 if (rInfo.activityInfo.packageName.equals(r.packageName)
2097 && rInfo.activityInfo.name.equals(r.info.name)) {
2098 // We found the current one... the next matching is
2099 // after it.
2100 i++;
2101 if (i<N) {
2102 aInfo = resolves.get(i).activityInfo;
2103 }
2104 break;
2105 }
2106 }
2107 } catch (RemoteException e) {
2108 }
2109
2110 if (aInfo == null) {
2111 // Nobody who is next!
2112 return false;
2113 }
2114
2115 intent.setComponent(new ComponentName(
2116 aInfo.applicationInfo.packageName, aInfo.name));
2117 intent.setFlags(intent.getFlags()&~(
2118 Intent.FLAG_ACTIVITY_FORWARD_RESULT|
2119 Intent.FLAG_ACTIVITY_CLEAR_TOP|
2120 Intent.FLAG_ACTIVITY_MULTIPLE_TASK|
2121 Intent.FLAG_ACTIVITY_NEW_TASK));
2122
2123 // Okay now we need to start the new activity, replacing the
2124 // currently running activity. This is a little tricky because
2125 // we want to start the new one as if the current one is finished,
2126 // but not finish the current one first so that there is no flicker.
2127 // And thus...
2128 final boolean wasFinishing = r.finishing;
2129 r.finishing = true;
2130
2131 // Propagate reply information over to the new activity.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002132 final ActivityRecord resultTo = r.resultTo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 final String resultWho = r.resultWho;
2134 final int requestCode = r.requestCode;
2135 r.resultTo = null;
2136 if (resultTo != null) {
2137 resultTo.removeResultsLocked(r, resultWho, requestCode);
2138 }
2139
2140 final long origId = Binder.clearCallingIdentity();
2141 // XXX we are not dealing with propagating grantedUriPermissions...
2142 // those are not yet exposed to user code, so there is no need.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002143 int res = mMainStack.startActivityLocked(r.app.thread, intent,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 r.resolvedType, null, 0, aInfo, resultTo, resultWho,
The Android Open Source Project4df24232009-03-05 14:34:35 -08002145 requestCode, -1, r.launchedFromUid, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 Binder.restoreCallingIdentity(origId);
2147
2148 r.finishing = wasFinishing;
2149 if (res != START_SUCCESS) {
2150 return false;
2151 }
2152 return true;
2153 }
2154 }
2155
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002156 public final int startActivityInPackage(int uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 Intent intent, String resolvedType, IBinder resultTo,
2158 String resultWho, int requestCode, boolean onlyIfNeeded) {
Dianne Hackborn2d91af02009-07-16 13:34:33 -07002159
2160 // This is so super not safe, that only the system (or okay root)
2161 // can do it.
2162 final int callingUid = Binder.getCallingUid();
2163 if (callingUid != 0 && callingUid != Process.myUid()) {
2164 throw new SecurityException(
2165 "startActivityInPackage only available to the system");
2166 }
2167
The Android Open Source Project4df24232009-03-05 14:34:35 -08002168 final boolean componentSpecified = intent.getComponent() != null;
2169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 // Don't modify the client's object!
2171 intent = new Intent(intent);
2172
2173 // Collect information about the target of the Intent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002174 ActivityInfo aInfo;
2175 try {
2176 ResolveInfo rInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002177 AppGlobals.getPackageManager().resolveIntent(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 intent, resolvedType,
Dianne Hackborn1655be42009-05-08 14:29:01 -07002179 PackageManager.MATCH_DEFAULT_ONLY | STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 aInfo = rInfo != null ? rInfo.activityInfo : null;
2181 } catch (RemoteException e) {
2182 aInfo = null;
2183 }
2184
2185 if (aInfo != null) {
2186 // Store the found target back into the intent, because now that
2187 // we have it we never want to do this again. For example, if the
2188 // user navigates back to this point in the history, we should
2189 // always restart the exact same activity.
2190 intent.setComponent(new ComponentName(
2191 aInfo.applicationInfo.packageName, aInfo.name));
2192 }
2193
2194 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002195 return mMainStack.startActivityLocked(null, intent, resolvedType,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 null, 0, aInfo, resultTo, resultWho, requestCode, -1, uid,
The Android Open Source Project4df24232009-03-05 14:34:35 -08002197 onlyIfNeeded, componentSpecified);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 }
2199 }
2200
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002201 final void addRecentTaskLocked(TaskRecord task) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 // Remove any existing entries that are the same kind of task.
2203 int N = mRecentTasks.size();
2204 for (int i=0; i<N; i++) {
2205 TaskRecord tr = mRecentTasks.get(i);
2206 if ((task.affinity != null && task.affinity.equals(tr.affinity))
2207 || (task.intent != null && task.intent.filterEquals(tr.intent))) {
2208 mRecentTasks.remove(i);
2209 i--;
2210 N--;
2211 if (task.intent == null) {
2212 // If the new recent task we are adding is not fully
2213 // specified, then replace it with the existing recent task.
2214 task = tr;
2215 }
2216 }
2217 }
2218 if (N >= MAX_RECENT_TASKS) {
2219 mRecentTasks.remove(N-1);
2220 }
2221 mRecentTasks.add(0, task);
2222 }
2223
2224 public void setRequestedOrientation(IBinder token,
2225 int requestedOrientation) {
2226 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002227 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 if (index < 0) {
2229 return;
2230 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002231 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 final long origId = Binder.clearCallingIdentity();
2233 mWindowManager.setAppOrientation(r, requestedOrientation);
2234 Configuration config = mWindowManager.updateOrientationFromAppTokens(
The Android Open Source Project10592532009-03-18 17:39:46 -07002235 mConfiguration,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 r.mayFreezeScreenLocked(r.app) ? r : null);
2237 if (config != null) {
2238 r.frozenBeforeDestroy = true;
2239 if (!updateConfigurationLocked(config, r)) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002240 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 }
2242 }
2243 Binder.restoreCallingIdentity(origId);
2244 }
2245 }
2246
2247 public int getRequestedOrientation(IBinder token) {
2248 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002249 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 if (index < 0) {
2251 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
2252 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002253 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 return mWindowManager.getAppOrientation(r);
2255 }
2256 }
2257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002258 /**
2259 * This is the internal entry point for handling Activity.finish().
2260 *
2261 * @param token The Binder token referencing the Activity we want to finish.
2262 * @param resultCode Result code, if any, from this Activity.
2263 * @param resultData Result data (Intent), if any, from this Activity.
2264 *
Alexey Tarasov83bad3d2009-08-12 15:05:43 +11002265 * @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 -08002266 */
2267 public final boolean finishActivity(IBinder token, int resultCode, Intent resultData) {
2268 // Refuse possible leaked file descriptors
2269 if (resultData != null && resultData.hasFileDescriptors() == true) {
2270 throw new IllegalArgumentException("File descriptors passed in Intent");
2271 }
2272
2273 synchronized(this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002274 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 // Find the first activity that is not finishing.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002276 ActivityRecord next = mMainStack.topRunningActivityLocked(token, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277 if (next != null) {
2278 // ask watcher if this is allowed
2279 boolean resumeOK = true;
2280 try {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002281 resumeOK = mController.activityResuming(next.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002283 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 }
2285
2286 if (!resumeOK) {
2287 return false;
2288 }
2289 }
2290 }
2291 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002292 boolean res = mMainStack.requestFinishActivityLocked(token, resultCode,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 resultData, "app-request");
2294 Binder.restoreCallingIdentity(origId);
2295 return res;
2296 }
2297 }
2298
Dianne Hackborn860755f2010-06-03 18:47:52 -07002299 public final void finishHeavyWeightApp() {
2300 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2301 != PackageManager.PERMISSION_GRANTED) {
2302 String msg = "Permission Denial: finishHeavyWeightApp() from pid="
2303 + Binder.getCallingPid()
2304 + ", uid=" + Binder.getCallingUid()
2305 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2306 Slog.w(TAG, msg);
2307 throw new SecurityException(msg);
2308 }
2309
2310 synchronized(this) {
2311 if (mHeavyWeightProcess == null) {
2312 return;
2313 }
2314
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002315 ArrayList<ActivityRecord> activities = new ArrayList<ActivityRecord>(
Dianne Hackborn860755f2010-06-03 18:47:52 -07002316 mHeavyWeightProcess.activities);
2317 for (int i=0; i<activities.size(); i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002318 ActivityRecord r = activities.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002319 if (!r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002320 int index = mMainStack.indexOfTokenLocked(r);
Dianne Hackborn860755f2010-06-03 18:47:52 -07002321 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002322 mMainStack.finishActivityLocked(r, index, Activity.RESULT_CANCELED,
Dianne Hackborn860755f2010-06-03 18:47:52 -07002323 null, "finish-heavy");
2324 }
2325 }
2326 }
2327
2328 mHeavyWeightProcess = null;
2329 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
2330 }
2331 }
2332
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002333 public void crashApplication(int uid, int initialPid, String packageName,
2334 String message) {
2335 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2336 != PackageManager.PERMISSION_GRANTED) {
2337 String msg = "Permission Denial: crashApplication() from pid="
2338 + Binder.getCallingPid()
2339 + ", uid=" + Binder.getCallingUid()
2340 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
2341 Slog.w(TAG, msg);
2342 throw new SecurityException(msg);
2343 }
2344
2345 synchronized(this) {
2346 ProcessRecord proc = null;
2347
2348 // Figure out which process to kill. We don't trust that initialPid
2349 // still has any relation to current pids, so must scan through the
2350 // list.
2351 synchronized (mPidsSelfLocked) {
2352 for (int i=0; i<mPidsSelfLocked.size(); i++) {
2353 ProcessRecord p = mPidsSelfLocked.valueAt(i);
2354 if (p.info.uid != uid) {
2355 continue;
2356 }
2357 if (p.pid == initialPid) {
2358 proc = p;
2359 break;
2360 }
2361 for (String str : p.pkgList) {
2362 if (str.equals(packageName)) {
2363 proc = p;
2364 }
2365 }
2366 }
2367 }
2368
2369 if (proc == null) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07002370 Slog.w(TAG, "crashApplication: nothing for uid=" + uid
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07002371 + " initialPid=" + initialPid
2372 + " packageName=" + packageName);
2373 return;
2374 }
2375
2376 if (proc.thread != null) {
2377 long ident = Binder.clearCallingIdentity();
2378 try {
2379 proc.thread.scheduleCrash(message);
2380 } catch (RemoteException e) {
2381 }
2382 Binder.restoreCallingIdentity(ident);
2383 }
2384 }
2385 }
2386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 public final void finishSubActivity(IBinder token, String resultWho,
2388 int requestCode) {
2389 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002390 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 if (index < 0) {
2392 return;
2393 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002394 ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395
2396 final long origId = Binder.clearCallingIdentity();
2397
2398 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002399 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2400 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 if (r.resultTo == self && r.requestCode == requestCode) {
2402 if ((r.resultWho == null && resultWho == null) ||
2403 (r.resultWho != null && r.resultWho.equals(resultWho))) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002404 mMainStack.finishActivityLocked(r, i,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 Activity.RESULT_CANCELED, null, "request-sub");
2406 }
2407 }
2408 }
2409
2410 Binder.restoreCallingIdentity(origId);
2411 }
2412 }
2413
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002414 public boolean willActivityBeVisible(IBinder token) {
2415 synchronized(this) {
2416 int i;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002417 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
2418 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002419 if (r == token) {
2420 return true;
2421 }
2422 if (r.fullscreen && !r.finishing) {
2423 return false;
2424 }
2425 }
2426 return true;
2427 }
2428 }
2429
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002430 public void overridePendingTransition(IBinder token, String packageName,
2431 int enterAnim, int exitAnim) {
2432 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002433 int index = mMainStack.indexOfTokenLocked(token);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002434 if (index < 0) {
2435 return;
2436 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002437 ActivityRecord self = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackborn3b3e1452009-09-24 19:22:12 -07002438
2439 final long origId = Binder.clearCallingIdentity();
2440
2441 if (self.state == ActivityState.RESUMED
2442 || self.state == ActivityState.PAUSING) {
2443 mWindowManager.overridePendingAppTransition(packageName,
2444 enterAnim, exitAnim);
2445 }
2446
2447 Binder.restoreCallingIdentity(origId);
2448 }
2449 }
2450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 * Main function for removing an existing process from the activity manager
2453 * as a result of that process going away. Clears out all connections
2454 * to the process.
2455 */
2456 private final void handleAppDiedLocked(ProcessRecord app,
2457 boolean restarting) {
2458 cleanUpApplicationRecordLocked(app, restarting, -1);
2459 if (!restarting) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002460 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 }
2462
2463 // Just in case...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002464 if (mMainStack.mPausingActivity != null && mMainStack.mPausingActivity.app == app) {
2465 if (DEBUG_PAUSE) Slog.v(TAG, "App died while pausing: " +mMainStack.mPausingActivity);
2466 mMainStack.mPausingActivity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002468 if (mMainStack.mLastPausedActivity != null && mMainStack.mLastPausedActivity.app == app) {
2469 mMainStack.mLastPausedActivity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
2471
2472 // Remove this application's activities from active lists.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002473 mMainStack.removeHistoryRecordsForAppLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474
2475 boolean atTop = true;
2476 boolean hasVisibleActivities = false;
2477
2478 // Clean out the history list.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002479 int i = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08002480 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 TAG, "Removing app " + app + " from history with " + i + " entries");
2482 while (i > 0) {
2483 i--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002484 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002485 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 TAG, "Record #" + i + " " + r + ": app=" + r.app);
2487 if (r.app == app) {
2488 if ((!r.haveState && !r.stateNotNeeded) || r.finishing) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002489 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 TAG, "Removing this entry! frozen=" + r.haveState
2491 + " finishing=" + r.finishing);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002492 mMainStack.mHistory.remove(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493
2494 r.inHistory = false;
2495 mWindowManager.removeAppToken(r);
2496 if (VALIDATE_TOKENS) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002497 mWindowManager.validateAppTokens(mMainStack.mHistory);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002499 r.removeUriPermissionsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002500
2501 } else {
2502 // We have the current state for this activity, so
2503 // it can be restarted later when needed.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002504 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 TAG, "Keeping entry, setting app to null");
2506 if (r.visible) {
2507 hasVisibleActivities = true;
2508 }
2509 r.app = null;
2510 r.nowVisible = false;
2511 if (!r.haveState) {
2512 r.icicle = null;
2513 }
2514 }
2515
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002516 r.stack.cleanUpActivityLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 r.state = ActivityState.STOPPED;
2518 }
2519 atTop = false;
2520 }
2521
2522 app.activities.clear();
2523
2524 if (app.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002525 Slog.w(TAG, "Crash of app " + app.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 + " running instrumentation " + app.instrumentationClass);
2527 Bundle info = new Bundle();
2528 info.putString("shortMsg", "Process crashed.");
2529 finishInstrumentationLocked(app, Activity.RESULT_CANCELED, info);
2530 }
2531
2532 if (!restarting) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002533 if (!mMainStack.resumeTopActivityLocked(null)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002534 // If there was nothing to resume, and we are not already
2535 // restarting this process, but there is a visible activity that
2536 // is hosted by the process... then make sure all visible
2537 // activities are running, taking care of restarting this
2538 // process.
2539 if (hasVisibleActivities) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002540 mMainStack.ensureActivitiesVisibleLocked(null, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
2542 }
2543 }
2544 }
2545
2546 private final int getLRURecordIndexForAppLocked(IApplicationThread thread) {
2547 IBinder threadBinder = thread.asBinder();
2548
2549 // Find the application record.
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002550 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2551 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 if (rec.thread != null && rec.thread.asBinder() == threadBinder) {
2553 return i;
2554 }
2555 }
2556 return -1;
2557 }
2558
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002559 final ProcessRecord getRecordForAppLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 IApplicationThread thread) {
2561 if (thread == null) {
2562 return null;
2563 }
2564
2565 int appIndex = getLRURecordIndexForAppLocked(thread);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002566 return appIndex >= 0 ? mLruProcesses.get(appIndex) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 }
2568
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002569 final void appDiedLocked(ProcessRecord app, int pid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 IApplicationThread thread) {
2571
2572 mProcDeaths[0]++;
2573
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07002574 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
2575 synchronized (stats) {
2576 stats.noteProcessDiedLocked(app.info.uid, pid);
2577 }
2578
Magnus Edlund7bb25812010-02-24 15:45:06 +01002579 // Clean up already done if the process has been re-started.
2580 if (app.pid == pid && app.thread != null &&
2581 app.thread.asBinder() == thread.asBinder()) {
Dianne Hackborn906497c2010-05-10 15:57:38 -07002582 if (!app.killedBackground) {
2583 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
2584 + ") has died.");
2585 }
Doug Zongker2bec3d42009-12-04 12:52:44 -08002586 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Joe Onorato8a9b2202010-02-26 18:56:32 -08002587 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 TAG, "Dying app: " + app + ", pid: " + pid
2589 + ", thread: " + thread.asBinder());
2590 boolean doLowMem = app.instrumentationClass == null;
2591 handleAppDiedLocked(app, false);
2592
2593 if (doLowMem) {
2594 // If there are no longer any background processes running,
2595 // and the app that died was not running instrumentation,
2596 // then tell everyone we are now low on memory.
2597 boolean haveBg = false;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002598 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2599 ProcessRecord rec = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 if (rec.thread != null && rec.setAdj >= HIDDEN_APP_MIN_ADJ) {
2601 haveBg = true;
2602 break;
2603 }
2604 }
2605
2606 if (!haveBg) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002607 Slog.i(TAG, "Low Memory: No more background processes.");
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002608 EventLog.writeEvent(EventLogTags.AM_LOW_MEMORY, mLruProcesses.size());
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002609 long now = SystemClock.uptimeMillis();
Dianne Hackborndd71fc82009-12-16 19:24:32 -08002610 for (int i=mLruProcesses.size()-1; i>=0; i--) {
2611 ProcessRecord rec = mLruProcesses.get(i);
Dianne Hackborn36124872009-10-08 16:22:03 -07002612 if (rec != app && rec.thread != null &&
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002613 (rec.lastLowMemory+GC_MIN_INTERVAL) <= now) {
2614 // The low memory report is overriding any current
2615 // state for a GC request. Make sure to do
Dianne Hackborn32907cf2010-06-10 17:50:20 -07002616 // heavy/important/visible/foreground processes first.
2617 if (rec.setAdj <= HEAVY_WEIGHT_APP_ADJ) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002618 rec.lastRequestedGc = 0;
2619 } else {
2620 rec.lastRequestedGc = rec.lastLowMemory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002622 rec.reportLowMemory = true;
2623 rec.lastLowMemory = now;
2624 mProcessesToGc.remove(rec);
2625 addProcessToGcListLocked(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 }
2627 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07002628 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
2630 }
Magnus Edlund7bb25812010-02-24 15:45:06 +01002631 } else if (app.pid != pid) {
2632 // A new process has already been started.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002633 Slog.i(TAG, "Process " + app.processName + " (pid " + pid
Magnus Edlund7bb25812010-02-24 15:45:06 +01002634 + ") has died and restarted (pid " + app.pid + ").");
Dianne Hackborn28a8c2b2010-03-01 11:30:02 -08002635 EventLog.writeEvent(EventLogTags.AM_PROC_DIED, app.pid, app.processName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08002636 } else if (DEBUG_PROCESSES) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002637 Slog.d(TAG, "Received spurious death notification for thread "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 + thread.asBinder());
2639 }
2640 }
2641
Dan Egnor42471dd2010-01-07 17:25:22 -08002642 /**
2643 * If a stack trace dump file is configured, dump process stack traces.
Christopher Tate6ee412d2010-05-28 12:01:56 -07002644 * @param clearTraces causes the dump file to be erased prior to the new
2645 * traces being written, if true; when false, the new traces will be
2646 * appended to any existing file content.
Dan Egnor42471dd2010-01-07 17:25:22 -08002647 * @param pids of dalvik VM processes to dump stack traces for
2648 * @return file containing stack traces, or null if no dump file is configured
2649 */
Christopher Tate6ee412d2010-05-28 12:01:56 -07002650 public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> pids) {
Dan Egnor42471dd2010-01-07 17:25:22 -08002651 String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
2652 if (tracesPath == null || tracesPath.length() == 0) {
2653 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002654 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002655
2656 File tracesFile = new File(tracesPath);
2657 try {
2658 File tracesDir = tracesFile.getParentFile();
2659 if (!tracesDir.exists()) tracesFile.mkdirs();
2660 FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1); // drwxrwxr-x
2661
Christopher Tate6ee412d2010-05-28 12:01:56 -07002662 if (clearTraces && tracesFile.exists()) tracesFile.delete();
Dan Egnor42471dd2010-01-07 17:25:22 -08002663 tracesFile.createNewFile();
2664 FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1); // -rw-rw-rw-
2665 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002666 Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08002667 return null;
2668 }
2669
2670 // Use a FileObserver to detect when traces finish writing.
2671 // The order of traces is considered important to maintain for legibility.
2672 FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {
2673 public synchronized void onEvent(int event, String path) { notify(); }
2674 };
2675
2676 try {
2677 observer.startWatching();
2678 int num = pids.size();
2679 for (int i = 0; i < num; i++) {
2680 synchronized (observer) {
2681 Process.sendSignal(pids.get(i), Process.SIGNAL_QUIT);
2682 observer.wait(200); // Wait for write-close, give up after 200msec
2683 }
2684 }
2685 } catch (InterruptedException e) {
Joe Onorato5d3bea62010-03-01 13:44:29 -08002686 Log.wtf(TAG, e);
Dan Egnor42471dd2010-01-07 17:25:22 -08002687 } finally {
2688 observer.stopWatching();
2689 }
2690
2691 return tracesFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 }
2693
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002694 final void appNotResponding(ProcessRecord app, ActivityRecord activity,
2695 ActivityRecord parent, final String annotation) {
Dan Egnor42471dd2010-01-07 17:25:22 -08002696 ArrayList<Integer> pids = new ArrayList<Integer>(20);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002697
2698 synchronized (this) {
2699 // PowerManager.reboot() can block for a long time, so ignore ANRs while shutting down.
2700 if (mShuttingDown) {
2701 Slog.i(TAG, "During shutdown skipping ANR: " + app + " " + annotation);
2702 return;
2703 } else if (app.notResponding) {
2704 Slog.i(TAG, "Skipping duplicate ANR: " + app + " " + annotation);
2705 return;
2706 } else if (app.crashing) {
2707 Slog.i(TAG, "Crashing app skipping ANR: " + app + " " + annotation);
2708 return;
2709 }
2710
2711 // In case we come through here for the same app before completing
2712 // this one, mark as anring now so we will bail out.
2713 app.notResponding = true;
Dan Egnor42471dd2010-01-07 17:25:22 -08002714
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002715 // Log the ANR to the event log.
2716 EventLog.writeEvent(EventLogTags.AM_ANR, app.pid, app.processName, app.info.flags,
2717 annotation);
Dan Egnor42471dd2010-01-07 17:25:22 -08002718
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002719 // Dump thread traces as quickly as we can, starting with "interesting" processes.
2720 pids.add(app.pid);
2721
2722 int parentPid = app.pid;
2723 if (parent != null && parent.app != null && parent.app.pid > 0) parentPid = parent.app.pid;
2724 if (parentPid != app.pid) pids.add(parentPid);
2725
2726 if (MY_PID != app.pid && MY_PID != parentPid) pids.add(MY_PID);
Dan Egnor42471dd2010-01-07 17:25:22 -08002727
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002728 for (int i = mLruProcesses.size() - 1; i >= 0; i--) {
2729 ProcessRecord r = mLruProcesses.get(i);
2730 if (r != null && r.thread != null) {
2731 int pid = r.pid;
2732 if (pid > 0 && pid != app.pid && pid != parentPid && pid != MY_PID) pids.add(pid);
2733 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 }
2735 }
2736
Christopher Tate6ee412d2010-05-28 12:01:56 -07002737 File tracesFile = dumpStackTraces(true, pids);
Dan Egnor42471dd2010-01-07 17:25:22 -08002738
2739 // Log the ANR to the main log.
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07002740 StringBuilder info = mStringBuilder;
2741 info.setLength(0);
Dan Egnor42471dd2010-01-07 17:25:22 -08002742 info.append("ANR in ").append(app.processName);
2743 if (activity != null && activity.shortComponentName != null) {
2744 info.append(" (").append(activity.shortComponentName).append(")");
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07002745 }
Eric Rowe6f4f6192010-02-17 18:29:04 -08002746 info.append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 if (annotation != null) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08002748 info.append("Reason: ").append(annotation).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002750 if (parent != null && parent != activity) {
Eric Rowe6f4f6192010-02-17 18:29:04 -08002751 info.append("Parent: ").append(parent.shortComponentName).append("\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753
Dan Egnor42471dd2010-01-07 17:25:22 -08002754 String cpuInfo = null;
2755 if (MONITOR_CPU_USAGE) {
2756 updateCpuStatsNow();
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002757 synchronized (mProcessStatsThread) {
2758 cpuInfo = mProcessStats.printCurrentState();
2759 }
Dan Egnor42471dd2010-01-07 17:25:22 -08002760 info.append(cpuInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 }
2762
Joe Onorato8a9b2202010-02-26 18:56:32 -08002763 Slog.e(TAG, info.toString());
Dan Egnor42471dd2010-01-07 17:25:22 -08002764 if (tracesFile == null) {
2765 // There is no trace file, so dump (only) the alleged culprit's threads to the log
2766 Process.sendSignal(app.pid, Process.SIGNAL_QUIT);
2767 }
2768
2769 addErrorToDropBox("anr", app, activity, parent, annotation, cpuInfo, tracesFile, null);
2770
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002771 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 try {
Dan Egnor42471dd2010-01-07 17:25:22 -08002773 // 0 == show dialog, 1 = keep waiting, -1 = kill process immediately
2774 int res = mController.appNotResponding(app.processName, app.pid, info.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 if (res != 0) {
Dan Egnor42471dd2010-01-07 17:25:22 -08002776 if (res < 0 && app.pid != MY_PID) Process.killProcess(app.pid);
2777 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 }
2779 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002780 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 }
2782 }
2783
Dan Egnor42471dd2010-01-07 17:25:22 -08002784 // Unless configured otherwise, swallow ANRs in background processes & kill the process.
2785 boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
2786 Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
Dianne Hackbornad5499d2010-03-29 18:08:45 -07002787
2788 synchronized (this) {
2789 if (!showBackground && !app.isInterestingToUserLocked() && app.pid != MY_PID) {
2790 Process.killProcess(app.pid);
2791 return;
2792 }
2793
2794 // Set the app's notResponding state, and look up the errorReportReceiver
2795 makeAppNotRespondingLocked(app,
2796 activity != null ? activity.shortComponentName : null,
2797 annotation != null ? "ANR " + annotation : "ANR",
2798 info.toString());
2799
2800 // Bring up the infamous App Not Responding dialog
2801 Message msg = Message.obtain();
2802 HashMap map = new HashMap();
2803 msg.what = SHOW_NOT_RESPONDING_MSG;
2804 msg.obj = map;
2805 map.put("app", app);
2806 if (activity != null) {
2807 map.put("activity", activity);
2808 }
2809
2810 mHandler.sendMessage(msg);
Dan Egnor42471dd2010-01-07 17:25:22 -08002811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 }
2813
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002814 final void decPersistentCountLocked(ProcessRecord app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 app.persistentActivities--;
2816 if (app.persistentActivities > 0) {
2817 // Still more of 'em...
2818 return;
2819 }
2820 if (app.persistent) {
2821 // Ah, but the application itself is persistent. Whatever!
2822 return;
2823 }
2824
2825 // App is no longer persistent... make sure it and the ones
2826 // following it in the LRU list have the correc oom_adj.
2827 updateOomAdjLocked();
2828 }
2829
2830 public void setPersistent(IBinder token, boolean isPersistent) {
2831 if (checkCallingPermission(android.Manifest.permission.PERSISTENT_ACTIVITY)
2832 != PackageManager.PERMISSION_GRANTED) {
2833 String msg = "Permission Denial: setPersistent() from pid="
2834 + Binder.getCallingPid()
2835 + ", uid=" + Binder.getCallingUid()
2836 + " requires " + android.Manifest.permission.PERSISTENT_ACTIVITY;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002837 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 throw new SecurityException(msg);
2839 }
2840
2841 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002842 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 if (index < 0) {
2844 return;
2845 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07002846 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 ProcessRecord app = r.app;
2848
Joe Onorato8a9b2202010-02-26 18:56:32 -08002849 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 TAG, "Setting persistence " + isPersistent + ": " + r);
2851
2852 if (isPersistent) {
2853 if (r.persistent) {
2854 // Okay okay, I heard you already!
Joe Onorato8a9b2202010-02-26 18:56:32 -08002855 if (localLOGV) Slog.v(TAG, "Already persistent!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002856 return;
2857 }
2858 r.persistent = true;
2859 app.persistentActivities++;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002860 if (localLOGV) Slog.v(TAG, "Num persistent now: " + app.persistentActivities);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (app.persistentActivities > 1) {
2862 // We aren't the first...
Joe Onorato8a9b2202010-02-26 18:56:32 -08002863 if (localLOGV) Slog.v(TAG, "Not the first!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 return;
2865 }
2866 if (app.persistent) {
2867 // This would be redundant.
Joe Onorato8a9b2202010-02-26 18:56:32 -08002868 if (localLOGV) Slog.v(TAG, "App is persistent!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 return;
2870 }
2871
2872 // App is now persistent... make sure it and the ones
2873 // following it now have the correct oom_adj.
2874 final long origId = Binder.clearCallingIdentity();
2875 updateOomAdjLocked();
2876 Binder.restoreCallingIdentity(origId);
2877
2878 } else {
2879 if (!r.persistent) {
2880 // Okay okay, I heard you already!
2881 return;
2882 }
2883 r.persistent = false;
2884 final long origId = Binder.clearCallingIdentity();
2885 decPersistentCountLocked(app);
2886 Binder.restoreCallingIdentity(origId);
2887
2888 }
2889 }
2890 }
2891
2892 public boolean clearApplicationUserData(final String packageName,
2893 final IPackageDataObserver observer) {
2894 int uid = Binder.getCallingUid();
2895 int pid = Binder.getCallingPid();
2896 long callingId = Binder.clearCallingIdentity();
2897 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002898 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 int pkgUid = -1;
2900 synchronized(this) {
2901 try {
2902 pkgUid = pm.getPackageUid(packageName);
2903 } catch (RemoteException e) {
2904 }
2905 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002906 Slog.w(TAG, "Invalid packageName:" + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 return false;
2908 }
2909 if (uid == pkgUid || checkComponentPermission(
2910 android.Manifest.permission.CLEAR_APP_USER_DATA,
2911 pid, uid, -1)
2912 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08002913 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 } else {
2915 throw new SecurityException(pid+" does not have permission:"+
2916 android.Manifest.permission.CLEAR_APP_USER_DATA+" to clear data" +
2917 "for process:"+packageName);
2918 }
2919 }
2920
2921 try {
2922 //clear application user data
2923 pm.clearApplicationUserData(packageName, observer);
2924 Intent intent = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED,
2925 Uri.fromParts("package", packageName, null));
2926 intent.putExtra(Intent.EXTRA_UID, pkgUid);
Josh Bartel2ecce342010-02-25 10:55:48 -06002927 synchronized (this) {
2928 broadcastIntentLocked(null, null, intent,
2929 null, null, 0, null, null, null,
2930 false, false, MY_PID, Process.SYSTEM_UID);
2931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 } catch (RemoteException e) {
2933 }
2934 } finally {
2935 Binder.restoreCallingIdentity(callingId);
2936 }
2937 return true;
2938 }
2939
Dianne Hackborn03abb812010-01-04 18:43:19 -08002940 public void killBackgroundProcesses(final String packageName) {
2941 if (checkCallingPermission(android.Manifest.permission.KILL_BACKGROUND_PROCESSES)
2942 != PackageManager.PERMISSION_GRANTED &&
2943 checkCallingPermission(android.Manifest.permission.RESTART_PACKAGES)
2944 != PackageManager.PERMISSION_GRANTED) {
2945 String msg = "Permission Denial: killBackgroundProcesses() from pid="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 + Binder.getCallingPid()
2947 + ", uid=" + Binder.getCallingUid()
Dianne Hackborn03abb812010-01-04 18:43:19 -08002948 + " requires " + android.Manifest.permission.KILL_BACKGROUND_PROCESSES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002949 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002950 throw new SecurityException(msg);
2951 }
2952
2953 long callingId = Binder.clearCallingIdentity();
2954 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002955 IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 int pkgUid = -1;
2957 synchronized(this) {
2958 try {
2959 pkgUid = pm.getPackageUid(packageName);
2960 } catch (RemoteException e) {
2961 }
2962 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002963 Slog.w(TAG, "Invalid packageName: " + packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 return;
2965 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08002966 killPackageProcessesLocked(packageName, pkgUid,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08002967 SECONDARY_SERVER_ADJ, false, true);
Dianne Hackborn03abb812010-01-04 18:43:19 -08002968 }
2969 } finally {
2970 Binder.restoreCallingIdentity(callingId);
2971 }
2972 }
2973
2974 public void forceStopPackage(final String packageName) {
2975 if (checkCallingPermission(android.Manifest.permission.FORCE_STOP_PACKAGES)
2976 != PackageManager.PERMISSION_GRANTED) {
2977 String msg = "Permission Denial: forceStopPackage() from pid="
2978 + Binder.getCallingPid()
2979 + ", uid=" + Binder.getCallingUid()
2980 + " requires " + android.Manifest.permission.FORCE_STOP_PACKAGES;
Joe Onorato8a9b2202010-02-26 18:56:32 -08002981 Slog.w(TAG, msg);
Dianne Hackborn03abb812010-01-04 18:43:19 -08002982 throw new SecurityException(msg);
2983 }
2984
2985 long callingId = Binder.clearCallingIdentity();
2986 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002987 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn03abb812010-01-04 18:43:19 -08002988 int pkgUid = -1;
2989 synchronized(this) {
2990 try {
2991 pkgUid = pm.getPackageUid(packageName);
2992 } catch (RemoteException e) {
2993 }
2994 if (pkgUid == -1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08002995 Slog.w(TAG, "Invalid packageName: " + packageName);
Dianne Hackborn03abb812010-01-04 18:43:19 -08002996 return;
2997 }
2998 forceStopPackageLocked(packageName, pkgUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 }
3000 } finally {
3001 Binder.restoreCallingIdentity(callingId);
3002 }
3003 }
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003004
3005 /*
3006 * The pkg name and uid have to be specified.
3007 * @see android.app.IActivityManager#killApplicationWithUid(java.lang.String, int)
3008 */
3009 public void killApplicationWithUid(String pkg, int uid) {
3010 if (pkg == null) {
3011 return;
3012 }
3013 // Make sure the uid is valid.
3014 if (uid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003015 Slog.w(TAG, "Invalid uid specified for pkg : " + pkg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003016 return;
3017 }
3018 int callerUid = Binder.getCallingUid();
3019 // Only the system server can kill an application
3020 if (callerUid == Process.SYSTEM_UID) {
Suchi Amalapurapud9d25762009-08-17 16:57:03 -07003021 // Post an aysnc message to kill the application
3022 Message msg = mHandler.obtainMessage(KILL_APPLICATION_MSG);
3023 msg.arg1 = uid;
3024 msg.arg2 = 0;
3025 msg.obj = pkg;
Suchi Amalapurapud50066f2009-08-18 16:57:41 -07003026 mHandler.sendMessage(msg);
Suchi Amalapurapu261e66a2009-07-27 15:21:34 -07003027 } else {
3028 throw new SecurityException(callerUid + " cannot kill pkg: " +
3029 pkg);
3030 }
3031 }
3032
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003033 public void closeSystemDialogs(String reason) {
3034 Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
3035 if (reason != null) {
3036 intent.putExtra("reason", reason);
3037 }
3038
3039 final int uid = Binder.getCallingUid();
3040 final long origId = Binder.clearCallingIdentity();
3041 synchronized (this) {
3042 int i = mWatchers.beginBroadcast();
3043 while (i > 0) {
3044 i--;
3045 IActivityWatcher w = mWatchers.getBroadcastItem(i);
3046 if (w != null) {
3047 try {
3048 w.closingSystemDialogs(reason);
3049 } catch (RemoteException e) {
3050 }
3051 }
3052 }
3053 mWatchers.finishBroadcast();
3054
Dianne Hackbornffa42482009-09-23 22:20:11 -07003055 mWindowManager.closeSystemDialogs(reason);
3056
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003057 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
3058 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
Dianne Hackbornffa42482009-09-23 22:20:11 -07003059 if ((r.info.flags&ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS) != 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003060 r.stack.finishActivityLocked(r, i,
Dianne Hackbornffa42482009-09-23 22:20:11 -07003061 Activity.RESULT_CANCELED, null, "close-sys");
3062 }
3063 }
3064
Dianne Hackborna6ddc8a2009-07-28 17:49:55 -07003065 broadcastIntentLocked(null, null, intent, null,
3066 null, 0, null, null, null, false, false, -1, uid);
3067 }
3068 Binder.restoreCallingIdentity(origId);
3069 }
3070
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003071 public Debug.MemoryInfo[] getProcessMemoryInfo(int[] pids)
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003072 throws RemoteException {
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003073 Debug.MemoryInfo[] infos = new Debug.MemoryInfo[pids.length];
3074 for (int i=pids.length-1; i>=0; i--) {
3075 infos[i] = new Debug.MemoryInfo();
3076 Debug.getMemoryInfo(pids[i], infos[i]);
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003077 }
Dianne Hackborn4f21c4c2009-09-17 10:24:05 -07003078 return infos;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07003079 }
Christopher Tate5e1ab332009-09-01 20:32:49 -07003080
3081 public void killApplicationProcess(String processName, int uid) {
3082 if (processName == null) {
3083 return;
3084 }
3085
3086 int callerUid = Binder.getCallingUid();
3087 // Only the system server can kill an application
3088 if (callerUid == Process.SYSTEM_UID) {
3089 synchronized (this) {
3090 ProcessRecord app = getProcessRecordLocked(processName, uid);
3091 if (app != null) {
3092 try {
3093 app.thread.scheduleSuicide();
3094 } catch (RemoteException e) {
3095 // If the other end already died, then our work here is done.
3096 }
3097 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003098 Slog.w(TAG, "Process/uid not found attempting kill of "
Christopher Tate5e1ab332009-09-01 20:32:49 -07003099 + processName + " / " + uid);
3100 }
3101 }
3102 } else {
3103 throw new SecurityException(callerUid + " cannot kill app process: " +
3104 processName);
3105 }
3106 }
3107
Dianne Hackborn03abb812010-01-04 18:43:19 -08003108 private void forceStopPackageLocked(final String packageName, int uid) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003109 forceStopPackageLocked(packageName, uid, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 Intent intent = new Intent(Intent.ACTION_PACKAGE_RESTARTED,
3111 Uri.fromParts("package", packageName, null));
3112 intent.putExtra(Intent.EXTRA_UID, uid);
3113 broadcastIntentLocked(null, null, intent,
3114 null, null, 0, null, null, null,
3115 false, false, MY_PID, Process.SYSTEM_UID);
3116 }
3117
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003118 private final boolean killPackageProcessesLocked(String packageName, int uid,
3119 int minOomAdj, boolean callerWillRestart, boolean doit) {
Dianne Hackborn03abb812010-01-04 18:43:19 -08003120 ArrayList<ProcessRecord> procs = new ArrayList<ProcessRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121
Dianne Hackborn03abb812010-01-04 18:43:19 -08003122 // Remove all processes this package may have touched: all with the
3123 // same UID (except for the system or root user), and all whose name
3124 // matches the package name.
3125 final String procNamePrefix = packageName + ":";
3126 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
3127 final int NA = apps.size();
3128 for (int ia=0; ia<NA; ia++) {
3129 ProcessRecord app = apps.valueAt(ia);
3130 if (app.removed) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003131 if (doit) {
3132 procs.add(app);
3133 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003134 } else if ((uid > 0 && uid != Process.SYSTEM_UID && app.info.uid == uid)
3135 || app.processName.equals(packageName)
3136 || app.processName.startsWith(procNamePrefix)) {
3137 if (app.setAdj >= minOomAdj) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003138 if (!doit) {
3139 return true;
3140 }
Dianne Hackborn03abb812010-01-04 18:43:19 -08003141 app.removed = true;
3142 procs.add(app);
3143 }
3144 }
3145 }
3146 }
3147
3148 int N = procs.size();
3149 for (int i=0; i<N; i++) {
3150 removeProcessLocked(procs.get(i), callerWillRestart);
3151 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003152 return N > 0;
Dianne Hackborn03abb812010-01-04 18:43:19 -08003153 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003154
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003155 private final boolean forceStopPackageLocked(String name, int uid,
3156 boolean callerWillRestart, boolean purgeCache, boolean doit) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 int i, N;
3158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 if (uid < 0) {
3160 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003161 uid = AppGlobals.getPackageManager().getPackageUid(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 } catch (RemoteException e) {
3163 }
3164 }
3165
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003166 if (doit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003167 Slog.i(TAG, "Force stopping package " + name + " uid=" + uid);
Dianne Hackborn03abb812010-01-04 18:43:19 -08003168
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003169 Iterator<SparseArray<Long>> badApps = mProcessCrashTimes.getMap().values().iterator();
3170 while (badApps.hasNext()) {
3171 SparseArray<Long> ba = badApps.next();
3172 if (ba.get(uid) != null) {
3173 badApps.remove();
3174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 }
3176 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003177
3178 boolean didSomething = killPackageProcessesLocked(name, uid, -100,
3179 callerWillRestart, doit);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003181 for (i=mMainStack.mHistory.size()-1; i>=0; i--) {
3182 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 if (r.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003184 if (!doit) {
3185 return true;
3186 }
3187 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003188 Slog.i(TAG, " Force finishing activity " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 if (r.app != null) {
3190 r.app.removed = true;
3191 }
3192 r.app = null;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003193 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "uninstall");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 }
3195 }
3196
3197 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
3198 for (ServiceRecord service : mServices.values()) {
3199 if (service.packageName.equals(name)) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003200 if (!doit) {
3201 return true;
3202 }
3203 didSomething = true;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003204 Slog.i(TAG, " Force stopping service " + service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 if (service.app != null) {
3206 service.app.removed = true;
3207 }
3208 service.app = null;
3209 services.add(service);
3210 }
3211 }
3212
3213 N = services.size();
3214 for (i=0; i<N; i++) {
3215 bringDownServiceLocked(services.get(i), true);
3216 }
3217
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003218 if (doit) {
3219 if (purgeCache) {
3220 AttributeCache ac = AttributeCache.instance();
3221 if (ac != null) {
3222 ac.removePackage(name);
3223 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003224 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003225 mMainStack.resumeTopActivityLocked(null);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08003226 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003227
3228 return didSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
3230
3231 private final boolean removeProcessLocked(ProcessRecord app, boolean callerWillRestart) {
3232 final String name = app.processName;
3233 final int uid = app.info.uid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003234 if (DEBUG_PROCESSES) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 TAG, "Force removing process " + app + " (" + name
3236 + "/" + uid + ")");
3237
3238 mProcessNames.remove(name, uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003239 if (mHeavyWeightProcess == app) {
3240 mHeavyWeightProcess = null;
3241 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 boolean needRestart = false;
3244 if (app.pid > 0 && app.pid != MY_PID) {
3245 int pid = app.pid;
3246 synchronized (mPidsSelfLocked) {
3247 mPidsSelfLocked.remove(pid);
3248 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3249 }
3250 handleAppDiedLocked(app, true);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003251 mLruProcesses.remove(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 Process.killProcess(pid);
3253
3254 if (app.persistent) {
3255 if (!callerWillRestart) {
3256 addAppLocked(app.info);
3257 } else {
3258 needRestart = true;
3259 }
3260 }
3261 } else {
3262 mRemovedProcesses.add(app);
3263 }
3264
3265 return needRestart;
3266 }
3267
3268 private final void processStartTimedOutLocked(ProcessRecord app) {
3269 final int pid = app.pid;
3270 boolean gone = false;
3271 synchronized (mPidsSelfLocked) {
3272 ProcessRecord knownApp = mPidsSelfLocked.get(pid);
3273 if (knownApp != null && knownApp.thread == null) {
3274 mPidsSelfLocked.remove(pid);
3275 gone = true;
3276 }
3277 }
3278
3279 if (gone) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003280 Slog.w(TAG, "Process " + app + " failed to attach");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003281 EventLog.writeEvent(EventLogTags.AM_PROCESS_START_TIMEOUT, pid, app.info.uid,
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003282 app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003283 mProcessNames.remove(app.processName, app.info.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003284 if (mHeavyWeightProcess == app) {
3285 mHeavyWeightProcess = null;
3286 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
3287 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003288 // Take care of any launching providers waiting for this process.
3289 checkAppInLaunchingProvidersLocked(app, true);
3290 // Take care of any services that are waiting for the process.
3291 for (int i=0; i<mPendingServices.size(); i++) {
3292 ServiceRecord sr = mPendingServices.get(i);
3293 if (app.info.uid == sr.appInfo.uid
3294 && app.processName.equals(sr.processName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003295 Slog.w(TAG, "Forcing bringing down service: " + sr);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003296 mPendingServices.remove(i);
3297 i--;
3298 bringDownServiceLocked(sr, true);
3299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003301 Process.killProcess(pid);
Christopher Tate181fafa2009-05-14 11:12:14 -07003302 if (mBackupTarget != null && mBackupTarget.app.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003303 Slog.w(TAG, "Unattached app died before backup, skipping");
Christopher Tate181fafa2009-05-14 11:12:14 -07003304 try {
3305 IBackupManager bm = IBackupManager.Stub.asInterface(
3306 ServiceManager.getService(Context.BACKUP_SERVICE));
3307 bm.agentDisconnected(app.info.packageName);
3308 } catch (RemoteException e) {
3309 // Can't happen; the backup manager is local
3310 }
3311 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003312 if (mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003313 Slog.w(TAG, "Unattached app died before broadcast acknowledged, skipping");
Dianne Hackbornf670ef72009-11-16 13:59:16 -08003314 mPendingBroadcast = null;
3315 scheduleBroadcastsLocked();
3316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003318 Slog.w(TAG, "Spurious process start timeout - pid not known for " + app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 }
3320 }
3321
3322 private final boolean attachApplicationLocked(IApplicationThread thread,
3323 int pid) {
3324
3325 // Find the application record that is being attached... either via
3326 // the pid if we are running in multiple processes, or just pull the
3327 // next app record if we are emulating process with anonymous threads.
3328 ProcessRecord app;
3329 if (pid != MY_PID && pid >= 0) {
3330 synchronized (mPidsSelfLocked) {
3331 app = mPidsSelfLocked.get(pid);
3332 }
3333 } else if (mStartingProcesses.size() > 0) {
3334 app = mStartingProcesses.remove(0);
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07003335 app.setPid(pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 } else {
3337 app = null;
3338 }
3339
3340 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003341 Slog.w(TAG, "No pending application record for pid " + pid
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 + " (IApplicationThread " + thread + "); dropping process");
Doug Zongker2bec3d42009-12-04 12:52:44 -08003343 EventLog.writeEvent(EventLogTags.AM_DROP_PROCESS, pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 if (pid > 0 && pid != MY_PID) {
3345 Process.killProcess(pid);
3346 } else {
3347 try {
3348 thread.scheduleExit();
3349 } catch (Exception e) {
3350 // Ignore exceptions.
3351 }
3352 }
3353 return false;
3354 }
3355
3356 // If this application record is still attached to a previous
3357 // process, clean it up now.
3358 if (app.thread != null) {
3359 handleAppDiedLocked(app, true);
3360 }
3361
3362 // Tell the process all about itself.
3363
Joe Onorato8a9b2202010-02-26 18:56:32 -08003364 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 TAG, "Binding process pid " + pid + " to record " + app);
3366
3367 String processName = app.processName;
3368 try {
3369 thread.asBinder().linkToDeath(new AppDeathRecipient(
3370 app, pid, thread), 0);
3371 } catch (RemoteException e) {
3372 app.resetPackageList();
3373 startProcessLocked(app, "link fail", processName);
3374 return false;
3375 }
3376
Doug Zongker2bec3d42009-12-04 12:52:44 -08003377 EventLog.writeEvent(EventLogTags.AM_PROC_BOUND, app.pid, app.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378
3379 app.thread = thread;
3380 app.curAdj = app.setAdj = -100;
Dianne Hackborn09c916b2009-12-08 14:50:51 -08003381 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
3382 app.setSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 app.forcingToForeground = null;
3384 app.foregroundServices = false;
3385 app.debugging = false;
3386
3387 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
3388
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003389 boolean normalMode = mSystemReady || isAllowedWhileBooting(app.info);
3390 List providers = normalMode ? generateApplicationProvidersLocked(app) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003392 if (!normalMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003393 Slog.i(TAG, "Launching preboot mode app: " + app);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003394 }
3395
Joe Onorato8a9b2202010-02-26 18:56:32 -08003396 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 TAG, "New app record " + app
3398 + " thread=" + thread.asBinder() + " pid=" + pid);
3399 try {
3400 int testMode = IApplicationThread.DEBUG_OFF;
3401 if (mDebugApp != null && mDebugApp.equals(processName)) {
3402 testMode = mWaitForDebugger
3403 ? IApplicationThread.DEBUG_WAIT
3404 : IApplicationThread.DEBUG_ON;
3405 app.debugging = true;
3406 if (mDebugTransient) {
3407 mDebugApp = mOrigDebugApp;
3408 mWaitForDebugger = mOrigWaitForDebugger;
3409 }
3410 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003411
Christopher Tate181fafa2009-05-14 11:12:14 -07003412 // If the app is being launched for restore or full backup, set it up specially
3413 boolean isRestrictedBackupMode = false;
3414 if (mBackupTarget != null && mBackupAppName.equals(processName)) {
3415 isRestrictedBackupMode = (mBackupTarget.backupMode == BackupRecord.RESTORE)
3416 || (mBackupTarget.backupMode == BackupRecord.BACKUP_FULL);
3417 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003418
Dianne Hackbornd7f6daa2009-06-22 17:06:35 -07003419 ensurePackageDexOpt(app.instrumentationInfo != null
3420 ? app.instrumentationInfo.packageName
3421 : app.info.packageName);
3422 if (app.instrumentationClass != null) {
3423 ensurePackageDexOpt(app.instrumentationClass.getPackageName());
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003424 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08003425 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Binding proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003426 + processName + " with config " + mConfiguration);
Dianne Hackborn1655be42009-05-08 14:29:01 -07003427 thread.bindApplication(processName, app.instrumentationInfo != null
3428 ? app.instrumentationInfo : app.info, providers,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 app.instrumentationClass, app.instrumentationProfileFile,
3430 app.instrumentationArguments, app.instrumentationWatcher, testMode,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003431 isRestrictedBackupMode || !normalMode,
3432 mConfiguration, getCommonServicesLocked());
Dianne Hackborndd71fc82009-12-16 19:24:32 -08003433 updateLruProcessLocked(app, false, true);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07003434 app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 } catch (Exception e) {
3436 // todo: Yikes! What should we do? For now we will try to
3437 // start another process, but that could easily get us in
3438 // an infinite loop of restarting processes...
Joe Onorato8a9b2202010-02-26 18:56:32 -08003439 Slog.w(TAG, "Exception thrown during bind!", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440
3441 app.resetPackageList();
3442 startProcessLocked(app, "bind fail", processName);
3443 return false;
3444 }
3445
3446 // Remove this record from the list of starting applications.
3447 mPersistentStartingProcesses.remove(app);
3448 mProcessesOnHold.remove(app);
3449
3450 boolean badApp = false;
3451 boolean didSomething = false;
3452
3453 // See if the top visible activity is waiting to run in this process...
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003454 ActivityRecord hr = mMainStack.topRunningActivityLocked(null);
Christopher Tate04c0af82010-06-07 18:35:20 -07003455 if (hr != null && normalMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 if (hr.app == null && app.info.uid == hr.info.applicationInfo.uid
3457 && processName.equals(hr.processName)) {
3458 try {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003459 if (mMainStack.realStartActivityLocked(hr, app, true, true)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 didSomething = true;
3461 }
3462 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003463 Slog.w(TAG, "Exception in new application when starting activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 + hr.intent.getComponent().flattenToShortString(), e);
3465 badApp = true;
3466 }
3467 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003468 mMainStack.ensureActivitiesVisibleLocked(hr, null, processName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003469 }
3470 }
3471
3472 // Find any services that should be running in this process...
3473 if (!badApp && mPendingServices.size() > 0) {
3474 ServiceRecord sr = null;
3475 try {
3476 for (int i=0; i<mPendingServices.size(); i++) {
3477 sr = mPendingServices.get(i);
3478 if (app.info.uid != sr.appInfo.uid
3479 || !processName.equals(sr.processName)) {
3480 continue;
3481 }
3482
3483 mPendingServices.remove(i);
3484 i--;
3485 realStartServiceLocked(sr, app);
3486 didSomething = true;
3487 }
3488 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003489 Slog.w(TAG, "Exception in new application when starting service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 + sr.shortName, e);
3491 badApp = true;
3492 }
3493 }
3494
3495 // Check if the next broadcast receiver is in this process...
3496 BroadcastRecord br = mPendingBroadcast;
3497 if (!badApp && br != null && br.curApp == app) {
3498 try {
3499 mPendingBroadcast = null;
3500 processCurBroadcastLocked(br, app);
3501 didSomething = true;
3502 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003503 Slog.w(TAG, "Exception in new application when starting receiver "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 + br.curComponent.flattenToShortString(), e);
3505 badApp = true;
3506 logBroadcastReceiverDiscard(br);
3507 finishReceiverLocked(br.receiver, br.resultCode, br.resultData,
3508 br.resultExtras, br.resultAbort, true);
3509 scheduleBroadcastsLocked();
Magnus Edlund7bb25812010-02-24 15:45:06 +01003510 // We need to reset the state if we fails to start the receiver.
3511 br.state = BroadcastRecord.IDLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 }
3513 }
3514
Christopher Tate181fafa2009-05-14 11:12:14 -07003515 // Check whether the next backup agent is in this process...
3516 if (!badApp && mBackupTarget != null && mBackupTarget.appInfo.uid == app.info.uid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003517 if (DEBUG_BACKUP) Slog.v(TAG, "New app is backup target, launching agent for " + app);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07003518 ensurePackageDexOpt(mBackupTarget.appInfo.packageName);
Christopher Tate181fafa2009-05-14 11:12:14 -07003519 try {
3520 thread.scheduleCreateBackupAgent(mBackupTarget.appInfo, mBackupTarget.backupMode);
3521 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003522 Slog.w(TAG, "Exception scheduling backup agent creation: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07003523 e.printStackTrace();
3524 }
3525 }
3526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 if (badApp) {
3528 // todo: Also need to kill application to deal with all
3529 // kinds of exceptions.
3530 handleAppDiedLocked(app, false);
3531 return false;
3532 }
3533
3534 if (!didSomething) {
3535 updateOomAdjLocked();
3536 }
3537
3538 return true;
3539 }
3540
3541 public final void attachApplication(IApplicationThread thread) {
3542 synchronized (this) {
3543 int callingPid = Binder.getCallingPid();
3544 final long origId = Binder.clearCallingIdentity();
3545 attachApplicationLocked(thread, callingPid);
3546 Binder.restoreCallingIdentity(origId);
3547 }
3548 }
3549
Dianne Hackborne88846e2009-09-30 21:34:25 -07003550 public final void activityIdle(IBinder token, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003552 mMainStack.activityIdleInternal(token, false, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 Binder.restoreCallingIdentity(origId);
3554 }
3555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 void enableScreenAfterBoot() {
Doug Zongker2bec3d42009-12-04 12:52:44 -08003557 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_ENABLE_SCREEN,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003558 SystemClock.uptimeMillis());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 mWindowManager.enableScreenAfterBoot();
3560 }
3561
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003562 final void finishBooting() {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003563 IntentFilter pkgFilter = new IntentFilter();
3564 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
3565 pkgFilter.addDataScheme("package");
3566 mContext.registerReceiver(new BroadcastReceiver() {
3567 @Override
3568 public void onReceive(Context context, Intent intent) {
3569 String[] pkgs = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
3570 if (pkgs != null) {
3571 for (String pkg : pkgs) {
3572 if (forceStopPackageLocked(pkg, -1, false, false, false)) {
3573 setResultCode(Activity.RESULT_OK);
3574 return;
3575 }
3576 }
3577 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003578 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003579 }, pkgFilter);
3580
3581 synchronized (this) {
3582 // Ensure that any processes we had put on hold are now started
3583 // up.
3584 final int NP = mProcessesOnHold.size();
3585 if (NP > 0) {
3586 ArrayList<ProcessRecord> procs =
3587 new ArrayList<ProcessRecord>(mProcessesOnHold);
3588 for (int ip=0; ip<NP; ip++) {
3589 this.startProcessLocked(procs.get(ip), "on-hold", null);
3590 }
3591 }
3592
3593 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07003594 // Start looking for apps that are abusing wake locks.
3595 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
3596 mHandler.sendMessageDelayed(nmsg, WAKE_LOCK_CHECK_DELAY);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08003597 // Tell anyone interested that we are done booting!
Dianne Hackbornf4c454b2010-08-11 12:47:41 -07003598 SystemProperties.set("sys.boot_completed", "1");
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003599 broadcastIntentLocked(null, null,
3600 new Intent(Intent.ACTION_BOOT_COMPLETED, null),
3601 null, null, 0, null, null,
3602 android.Manifest.permission.RECEIVE_BOOT_COMPLETED,
3603 false, false, MY_PID, Process.SYSTEM_UID);
3604 }
3605 }
3606 }
3607
3608 final void ensureBootCompleted() {
3609 boolean booting;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003610 boolean enableScreen;
3611 synchronized (this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003612 booting = mBooting;
3613 mBooting = false;
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003614 enableScreen = !mBooted;
3615 mBooted = true;
3616 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003617
3618 if (booting) {
3619 finishBooting();
3620 }
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003621
3622 if (enableScreen) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07003623 enableScreenAfterBoot();
3624 }
3625 }
3626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 public final void activityPaused(IBinder token, Bundle icicle) {
3628 // Refuse possible leaked file descriptors
3629 if (icicle != null && icicle.hasFileDescriptors()) {
3630 throw new IllegalArgumentException("File descriptors passed in Bundle");
3631 }
3632
3633 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003634 mMainStack.activityPaused(token, icicle, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 Binder.restoreCallingIdentity(origId);
3636 }
3637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 public final void activityStopped(IBinder token, Bitmap thumbnail,
3639 CharSequence description) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003640 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 TAG, "Activity stopped: token=" + token);
3642
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003643 ActivityRecord r = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644
3645 final long origId = Binder.clearCallingIdentity();
3646
3647 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003648 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003649 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003650 r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 r.thumbnail = thumbnail;
3652 r.description = description;
3653 r.stopped = true;
3654 r.state = ActivityState.STOPPED;
3655 if (!r.finishing) {
3656 if (r.configDestroy) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003657 r.stack.destroyActivityLocked(r, true);
3658 r.stack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
3660 }
3661 }
3662 }
3663
3664 if (r != null) {
3665 sendPendingThumbnail(r, null, null, null, false);
3666 }
3667
3668 trimApplications();
3669
3670 Binder.restoreCallingIdentity(origId);
3671 }
3672
3673 public final void activityDestroyed(IBinder token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003674 if (DEBUG_SWITCH) Slog.v(TAG, "ACTIVITY DESTROYED: " + token);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003675 mMainStack.activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 }
3677
3678 public String getCallingPackage(IBinder token) {
3679 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003680 ActivityRecord r = getCallingRecordLocked(token);
Dianne Hackborn9bbcb912009-10-20 15:42:38 -07003681 return r != null && r.app != null ? r.info.packageName : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 }
3683 }
3684
3685 public ComponentName getCallingActivity(IBinder token) {
3686 synchronized (this) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003687 ActivityRecord r = getCallingRecordLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 return r != null ? r.intent.getComponent() : null;
3689 }
3690 }
3691
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003692 private ActivityRecord getCallingRecordLocked(IBinder token) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003693 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003695 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 if (r != null) {
3697 return r.resultTo;
3698 }
3699 }
3700 return null;
3701 }
3702
3703 public ComponentName getActivityClassForToken(IBinder token) {
3704 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003705 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003707 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 return r.intent.getComponent();
3709 }
3710 return null;
3711 }
3712 }
3713
3714 public String getPackageForToken(IBinder token) {
3715 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003716 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003718 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 return r.packageName;
3720 }
3721 return null;
3722 }
3723 }
3724
3725 public IIntentSender getIntentSender(int type,
3726 String packageName, IBinder token, String resultWho,
3727 int requestCode, Intent intent, String resolvedType, int flags) {
3728 // Refuse possible leaked file descriptors
3729 if (intent != null && intent.hasFileDescriptors() == true) {
3730 throw new IllegalArgumentException("File descriptors passed in Intent");
3731 }
3732
Dianne Hackborn9acc0302009-08-25 00:27:12 -07003733 if (type == INTENT_SENDER_BROADCAST) {
3734 if ((intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
3735 throw new IllegalArgumentException(
3736 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
3737 }
3738 }
3739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 synchronized(this) {
3741 int callingUid = Binder.getCallingUid();
3742 try {
3743 if (callingUid != 0 && callingUid != Process.SYSTEM_UID &&
3744 Process.supportsProcesses()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003745 int uid = AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 .getPackageUid(packageName);
3747 if (uid != Binder.getCallingUid()) {
3748 String msg = "Permission Denial: getIntentSender() from pid="
3749 + Binder.getCallingPid()
3750 + ", uid=" + Binder.getCallingUid()
3751 + ", (need uid=" + uid + ")"
3752 + " is not allowed to send as package " + packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003753 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 throw new SecurityException(msg);
3755 }
3756 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003757
3758 return getIntentSenderLocked(type, packageName, callingUid,
3759 token, resultWho, requestCode, intent, resolvedType, flags);
3760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003761 } catch (RemoteException e) {
3762 throw new SecurityException(e);
3763 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003764 }
3765 }
3766
3767 IIntentSender getIntentSenderLocked(int type,
3768 String packageName, int callingUid, IBinder token, String resultWho,
3769 int requestCode, Intent intent, String resolvedType, int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003770 ActivityRecord activity = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07003771 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003772 int index = mMainStack.indexOfTokenLocked(token);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003773 if (index < 0) {
3774 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003775 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07003776 activity = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackborn860755f2010-06-03 18:47:52 -07003777 if (activity.finishing) {
3778 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003780 }
3781
3782 final boolean noCreate = (flags&PendingIntent.FLAG_NO_CREATE) != 0;
3783 final boolean cancelCurrent = (flags&PendingIntent.FLAG_CANCEL_CURRENT) != 0;
3784 final boolean updateCurrent = (flags&PendingIntent.FLAG_UPDATE_CURRENT) != 0;
3785 flags &= ~(PendingIntent.FLAG_NO_CREATE|PendingIntent.FLAG_CANCEL_CURRENT
3786 |PendingIntent.FLAG_UPDATE_CURRENT);
3787
3788 PendingIntentRecord.Key key = new PendingIntentRecord.Key(
3789 type, packageName, activity, resultWho,
3790 requestCode, intent, resolvedType, flags);
3791 WeakReference<PendingIntentRecord> ref;
3792 ref = mIntentSenderRecords.get(key);
3793 PendingIntentRecord rec = ref != null ? ref.get() : null;
3794 if (rec != null) {
3795 if (!cancelCurrent) {
3796 if (updateCurrent) {
3797 rec.key.requestIntent.replaceExtras(intent);
3798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 return rec;
3800 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003801 rec.canceled = true;
3802 mIntentSenderRecords.remove(key);
3803 }
3804 if (noCreate) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 return rec;
3806 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07003807 rec = new PendingIntentRecord(this, key, callingUid);
3808 mIntentSenderRecords.put(key, rec.ref);
3809 if (type == INTENT_SENDER_ACTIVITY_RESULT) {
3810 if (activity.pendingResults == null) {
3811 activity.pendingResults
3812 = new HashSet<WeakReference<PendingIntentRecord>>();
3813 }
3814 activity.pendingResults.add(rec.ref);
3815 }
3816 return rec;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003817 }
3818
3819 public void cancelIntentSender(IIntentSender sender) {
3820 if (!(sender instanceof PendingIntentRecord)) {
3821 return;
3822 }
3823 synchronized(this) {
3824 PendingIntentRecord rec = (PendingIntentRecord)sender;
3825 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003826 int uid = AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 .getPackageUid(rec.key.packageName);
3828 if (uid != Binder.getCallingUid()) {
3829 String msg = "Permission Denial: cancelIntentSender() from pid="
3830 + Binder.getCallingPid()
3831 + ", uid=" + Binder.getCallingUid()
3832 + " is not allowed to cancel packges "
3833 + rec.key.packageName;
Joe Onorato8a9b2202010-02-26 18:56:32 -08003834 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 throw new SecurityException(msg);
3836 }
3837 } catch (RemoteException e) {
3838 throw new SecurityException(e);
3839 }
3840 cancelIntentSenderLocked(rec, true);
3841 }
3842 }
3843
3844 void cancelIntentSenderLocked(PendingIntentRecord rec, boolean cleanActivity) {
3845 rec.canceled = true;
3846 mIntentSenderRecords.remove(rec.key);
3847 if (cleanActivity && rec.key.activity != null) {
3848 rec.key.activity.pendingResults.remove(rec.ref);
3849 }
3850 }
3851
3852 public String getPackageForIntentSender(IIntentSender pendingResult) {
3853 if (!(pendingResult instanceof PendingIntentRecord)) {
3854 return null;
3855 }
Brad Fitzpatrickb213d102010-04-19 11:58:52 -07003856 try {
3857 PendingIntentRecord res = (PendingIntentRecord)pendingResult;
3858 return res.key.packageName;
3859 } catch (ClassCastException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 }
3861 return null;
3862 }
3863
3864 public void setProcessLimit(int max) {
3865 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
3866 "setProcessLimit()");
3867 mProcessLimit = max;
3868 }
3869
3870 public int getProcessLimit() {
3871 return mProcessLimit;
3872 }
3873
3874 void foregroundTokenDied(ForegroundToken token) {
3875 synchronized (ActivityManagerService.this) {
3876 synchronized (mPidsSelfLocked) {
3877 ForegroundToken cur
3878 = mForegroundProcesses.get(token.pid);
3879 if (cur != token) {
3880 return;
3881 }
3882 mForegroundProcesses.remove(token.pid);
3883 ProcessRecord pr = mPidsSelfLocked.get(token.pid);
3884 if (pr == null) {
3885 return;
3886 }
3887 pr.forcingToForeground = null;
3888 pr.foregroundServices = false;
3889 }
3890 updateOomAdjLocked();
3891 }
3892 }
3893
3894 public void setProcessForeground(IBinder token, int pid, boolean isForeground) {
3895 enforceCallingPermission(android.Manifest.permission.SET_PROCESS_LIMIT,
3896 "setProcessForeground()");
3897 synchronized(this) {
3898 boolean changed = false;
3899
3900 synchronized (mPidsSelfLocked) {
3901 ProcessRecord pr = mPidsSelfLocked.get(pid);
3902 if (pr == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003903 Slog.w(TAG, "setProcessForeground called on unknown pid: " + pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003904 return;
3905 }
3906 ForegroundToken oldToken = mForegroundProcesses.get(pid);
3907 if (oldToken != null) {
3908 oldToken.token.unlinkToDeath(oldToken, 0);
3909 mForegroundProcesses.remove(pid);
3910 pr.forcingToForeground = null;
3911 changed = true;
3912 }
3913 if (isForeground && token != null) {
3914 ForegroundToken newToken = new ForegroundToken() {
3915 public void binderDied() {
3916 foregroundTokenDied(this);
3917 }
3918 };
3919 newToken.pid = pid;
3920 newToken.token = token;
3921 try {
3922 token.linkToDeath(newToken, 0);
3923 mForegroundProcesses.put(pid, newToken);
3924 pr.forcingToForeground = token;
3925 changed = true;
3926 } catch (RemoteException e) {
3927 // If the process died while doing this, we will later
3928 // do the cleanup with the process death link.
3929 }
3930 }
3931 }
3932
3933 if (changed) {
3934 updateOomAdjLocked();
3935 }
3936 }
3937 }
3938
3939 // =========================================================
3940 // PERMISSIONS
3941 // =========================================================
3942
3943 static class PermissionController extends IPermissionController.Stub {
3944 ActivityManagerService mActivityManagerService;
3945 PermissionController(ActivityManagerService activityManagerService) {
3946 mActivityManagerService = activityManagerService;
3947 }
3948
3949 public boolean checkPermission(String permission, int pid, int uid) {
3950 return mActivityManagerService.checkPermission(permission, pid,
3951 uid) == PackageManager.PERMISSION_GRANTED;
3952 }
3953 }
3954
3955 /**
3956 * This can be called with or without the global lock held.
3957 */
3958 int checkComponentPermission(String permission, int pid, int uid,
3959 int reqUid) {
3960 // We might be performing an operation on behalf of an indirect binder
3961 // invocation, e.g. via {@link #openContentUri}. Check and adjust the
3962 // client identity accordingly before proceeding.
3963 Identity tlsIdentity = sCallerIdentity.get();
3964 if (tlsIdentity != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003965 Slog.d(TAG, "checkComponentPermission() adjusting {pid,uid} to {"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 + tlsIdentity.pid + "," + tlsIdentity.uid + "}");
3967 uid = tlsIdentity.uid;
3968 pid = tlsIdentity.pid;
3969 }
3970
3971 // Root, system server and our own process get to do everything.
3972 if (uid == 0 || uid == Process.SYSTEM_UID || pid == MY_PID ||
3973 !Process.supportsProcesses()) {
3974 return PackageManager.PERMISSION_GRANTED;
3975 }
3976 // If the target requires a specific UID, always fail for others.
3977 if (reqUid >= 0 && uid != reqUid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08003978 Slog.w(TAG, "Permission denied: checkComponentPermission() reqUid=" + reqUid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 return PackageManager.PERMISSION_DENIED;
3980 }
3981 if (permission == null) {
3982 return PackageManager.PERMISSION_GRANTED;
3983 }
3984 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003985 return AppGlobals.getPackageManager()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003986 .checkUidPermission(permission, uid);
3987 } catch (RemoteException e) {
3988 // Should never happen, but if it does... deny!
Joe Onorato8a9b2202010-02-26 18:56:32 -08003989 Slog.e(TAG, "PackageManager is dead?!?", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991 return PackageManager.PERMISSION_DENIED;
3992 }
3993
3994 /**
3995 * As the only public entry point for permissions checking, this method
3996 * can enforce the semantic that requesting a check on a null global
3997 * permission is automatically denied. (Internally a null permission
3998 * string is used when calling {@link #checkComponentPermission} in cases
3999 * when only uid-based security is needed.)
4000 *
4001 * This can be called with or without the global lock held.
4002 */
4003 public int checkPermission(String permission, int pid, int uid) {
4004 if (permission == null) {
4005 return PackageManager.PERMISSION_DENIED;
4006 }
4007 return checkComponentPermission(permission, pid, uid, -1);
4008 }
4009
4010 /**
4011 * Binder IPC calls go through the public entry point.
4012 * This can be called with or without the global lock held.
4013 */
4014 int checkCallingPermission(String permission) {
4015 return checkPermission(permission,
4016 Binder.getCallingPid(),
4017 Binder.getCallingUid());
4018 }
4019
4020 /**
4021 * This can be called with or without the global lock held.
4022 */
4023 void enforceCallingPermission(String permission, String func) {
4024 if (checkCallingPermission(permission)
4025 == PackageManager.PERMISSION_GRANTED) {
4026 return;
4027 }
4028
4029 String msg = "Permission Denial: " + func + " from pid="
4030 + Binder.getCallingPid()
4031 + ", uid=" + Binder.getCallingUid()
4032 + " requires " + permission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004033 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 throw new SecurityException(msg);
4035 }
4036
4037 private final boolean checkHoldingPermissionsLocked(IPackageManager pm,
4038 ProviderInfo pi, int uid, int modeFlags) {
4039 try {
4040 if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
4041 if ((pi.readPermission != null) &&
4042 (pm.checkUidPermission(pi.readPermission, uid)
4043 != PackageManager.PERMISSION_GRANTED)) {
4044 return false;
4045 }
4046 }
4047 if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
4048 if ((pi.writePermission != null) &&
4049 (pm.checkUidPermission(pi.writePermission, uid)
4050 != PackageManager.PERMISSION_GRANTED)) {
4051 return false;
4052 }
4053 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004054 if (!pi.exported && pi.applicationInfo.uid != uid) {
4055 return false;
4056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 return true;
4058 } catch (RemoteException e) {
4059 return false;
4060 }
4061 }
4062
4063 private final boolean checkUriPermissionLocked(Uri uri, int uid,
4064 int modeFlags) {
4065 // Root gets to do everything.
4066 if (uid == 0 || !Process.supportsProcesses()) {
4067 return true;
4068 }
4069 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(uid);
4070 if (perms == null) return false;
4071 UriPermission perm = perms.get(uri);
4072 if (perm == null) return false;
4073 return (modeFlags&perm.modeFlags) == modeFlags;
4074 }
4075
4076 public int checkUriPermission(Uri uri, int pid, int uid, int modeFlags) {
4077 // Another redirected-binder-call permissions check as in
4078 // {@link checkComponentPermission}.
4079 Identity tlsIdentity = sCallerIdentity.get();
4080 if (tlsIdentity != null) {
4081 uid = tlsIdentity.uid;
4082 pid = tlsIdentity.pid;
4083 }
4084
4085 // Our own process gets to do everything.
4086 if (pid == MY_PID) {
4087 return PackageManager.PERMISSION_GRANTED;
4088 }
4089 synchronized(this) {
4090 return checkUriPermissionLocked(uri, uid, modeFlags)
4091 ? PackageManager.PERMISSION_GRANTED
4092 : PackageManager.PERMISSION_DENIED;
4093 }
4094 }
4095
Dianne Hackborn39792d22010-08-19 18:01:52 -07004096 /**
4097 * Check if the targetPkg can be granted permission to access uri by
4098 * the callingUid using the given modeFlags. Throws a security exception
4099 * if callingUid is not allowed to do this. Returns the uid of the target
4100 * if the URI permission grant should be performed; returns -1 if it is not
4101 * needed (for example targetPkg already has permission to access the URI).
4102 */
4103 int checkGrantUriPermissionLocked(int callingUid, String targetPkg,
4104 Uri uri, int modeFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4106 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4107 if (modeFlags == 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004108 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 }
4110
Joe Onorato8a9b2202010-02-26 18:56:32 -08004111 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07004112 "Checking grant " + targetPkg + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004113
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004114 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115
4116 // If this is not a content: uri, we can't do anything with it.
4117 if (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004118 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004119 "Can't grant URI permission for non-content URI: " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004120 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 }
4122
4123 String name = uri.getAuthority();
4124 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004125 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004126 if (cpr != null) {
4127 pi = cpr.info;
4128 } else {
4129 try {
4130 pi = pm.resolveContentProvider(name,
4131 PackageManager.GET_URI_PERMISSION_PATTERNS);
4132 } catch (RemoteException ex) {
4133 }
4134 }
4135 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004136 Slog.w(TAG, "No content provider found for: " + name);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004137 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004138 }
4139
4140 int targetUid;
4141 try {
4142 targetUid = pm.getPackageUid(targetPkg);
4143 if (targetUid < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004144 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004145 "Can't grant URI permission no uid for: " + targetPkg);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004146 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 }
4148 } catch (RemoteException ex) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004149 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 }
4151
4152 // First... does the target actually need this permission?
4153 if (checkHoldingPermissionsLocked(pm, pi, targetUid, modeFlags)) {
4154 // No need to grant the target this permission.
Joe Onorato8a9b2202010-02-26 18:56:32 -08004155 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004156 "Target " + targetPkg + " already has full permission to " + uri);
Dianne Hackborn39792d22010-08-19 18:01:52 -07004157 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 }
4159
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004160 // Second... is the provider allowing granting of URI permissions?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 if (!pi.grantUriPermissions) {
4162 throw new SecurityException("Provider " + pi.packageName
4163 + "/" + pi.name
4164 + " does not allow granting of Uri permissions (uri "
4165 + uri + ")");
4166 }
4167 if (pi.uriPermissionPatterns != null) {
4168 final int N = pi.uriPermissionPatterns.length;
4169 boolean allowed = false;
4170 for (int i=0; i<N; i++) {
4171 if (pi.uriPermissionPatterns[i] != null
4172 && pi.uriPermissionPatterns[i].match(uri.getPath())) {
4173 allowed = true;
4174 break;
4175 }
4176 }
4177 if (!allowed) {
4178 throw new SecurityException("Provider " + pi.packageName
4179 + "/" + pi.name
4180 + " does not allow granting of permission to path of Uri "
4181 + uri);
4182 }
4183 }
4184
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004185 // Third... does the caller itself have permission to access
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 // this uri?
4187 if (!checkHoldingPermissionsLocked(pm, pi, callingUid, modeFlags)) {
4188 if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
4189 throw new SecurityException("Uid " + callingUid
4190 + " does not have permission to uri " + uri);
4191 }
4192 }
4193
Dianne Hackborn39792d22010-08-19 18:01:52 -07004194 return targetUid;
4195 }
4196
4197 void grantUriPermissionUncheckedLocked(int targetUid, String targetPkg,
4198 Uri uri, int modeFlags, UriPermissionOwner owner) {
4199 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4200 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4201 if (modeFlags == 0) {
4202 return;
4203 }
4204
4205 // So here we are: the caller has the assumed permission
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 // to the uri, and the target doesn't. Let's now give this to
4207 // the target.
4208
Joe Onorato8a9b2202010-02-26 18:56:32 -08004209 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07004210 "Granting " + targetPkg + "/" + targetUid + " permission to " + uri);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 HashMap<Uri, UriPermission> targetUris
4213 = mGrantedUriPermissions.get(targetUid);
4214 if (targetUris == null) {
4215 targetUris = new HashMap<Uri, UriPermission>();
4216 mGrantedUriPermissions.put(targetUid, targetUris);
4217 }
4218
4219 UriPermission perm = targetUris.get(uri);
4220 if (perm == null) {
4221 perm = new UriPermission(targetUid, uri);
4222 targetUris.put(uri, perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 }
Dianne Hackbornb424b632010-08-18 15:59:05 -07004224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 perm.modeFlags |= modeFlags;
Dianne Hackborn39792d22010-08-19 18:01:52 -07004226 if (owner == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 perm.globalModeFlags |= modeFlags;
4228 } else if ((modeFlags&Intent.FLAG_GRANT_READ_URI_PERMISSION) != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004229 perm.readOwners.add(owner);
4230 owner.addReadPermission(perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 } else if ((modeFlags&Intent.FLAG_GRANT_WRITE_URI_PERMISSION) != 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004232 perm.writeOwners.add(owner);
4233 owner.addWritePermission(perm);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 }
4235 }
4236
Dianne Hackborn39792d22010-08-19 18:01:52 -07004237 void grantUriPermissionLocked(int callingUid,
4238 String targetPkg, Uri uri, int modeFlags, UriPermissionOwner owner) {
4239 int targetUid = checkGrantUriPermissionLocked(callingUid, targetPkg, uri, modeFlags);
4240 if (targetUid < 0) {
4241 return;
4242 }
4243
4244 grantUriPermissionUncheckedLocked(targetUid, targetPkg, uri, modeFlags, owner);
4245 }
4246
4247 /**
4248 * Like checkGrantUriPermissionLocked, but takes an Intent.
4249 */
4250 int checkGrantUriPermissionFromIntentLocked(int callingUid,
4251 String targetPkg, Intent intent) {
Dianne Hackbornb424b632010-08-18 15:59:05 -07004252 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn39792d22010-08-19 18:01:52 -07004253 "Checking URI perm to " + (intent != null ? intent.getData() : null)
Dianne Hackbornb424b632010-08-18 15:59:05 -07004254 + " from " + intent + "; flags=0x"
4255 + Integer.toHexString(intent != null ? intent.getFlags() : 0));
4256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 if (intent == null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004258 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 }
4260 Uri data = intent.getData();
4261 if (data == null) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07004262 return -1;
4263 }
4264 return checkGrantUriPermissionLocked(callingUid, targetPkg, data,
4265 intent.getFlags());
4266 }
4267
4268 /**
4269 * Like grantUriPermissionUncheckedLocked, but takes an Intent.
4270 */
4271 void grantUriPermissionUncheckedFromIntentLocked(int targetUid,
4272 String targetPkg, Intent intent, UriPermissionOwner owner) {
4273 grantUriPermissionUncheckedLocked(targetUid, targetPkg, intent.getData(),
4274 intent.getFlags(), owner);
4275 }
4276
4277 void grantUriPermissionFromIntentLocked(int callingUid,
4278 String targetPkg, Intent intent, UriPermissionOwner owner) {
4279 int targetUid = checkGrantUriPermissionFromIntentLocked(callingUid, targetPkg, intent);
4280 if (targetUid < 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281 return;
4282 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07004283
4284 grantUriPermissionUncheckedFromIntentLocked(targetUid, targetPkg, intent, owner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
4286
4287 public void grantUriPermission(IApplicationThread caller, String targetPkg,
4288 Uri uri, int modeFlags) {
4289 synchronized(this) {
4290 final ProcessRecord r = getRecordForAppLocked(caller);
4291 if (r == null) {
4292 throw new SecurityException("Unable to find app for caller "
4293 + caller
4294 + " when granting permission to uri " + uri);
4295 }
4296 if (targetPkg == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004297 Slog.w(TAG, "grantUriPermission: null target");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 return;
4299 }
4300 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004301 Slog.w(TAG, "grantUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 return;
4303 }
4304
4305 grantUriPermissionLocked(r.info.uid, targetPkg, uri, modeFlags,
4306 null);
4307 }
4308 }
4309
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004310 void removeUriPermissionIfNeededLocked(UriPermission perm) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 if ((perm.modeFlags&(Intent.FLAG_GRANT_READ_URI_PERMISSION
4312 |Intent.FLAG_GRANT_WRITE_URI_PERMISSION)) == 0) {
4313 HashMap<Uri, UriPermission> perms
4314 = mGrantedUriPermissions.get(perm.uid);
4315 if (perms != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004316 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004317 "Removing " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 perms.remove(perm.uri);
4319 if (perms.size() == 0) {
4320 mGrantedUriPermissions.remove(perm.uid);
4321 }
4322 }
4323 }
4324 }
4325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 private void revokeUriPermissionLocked(int callingUid, Uri uri,
4327 int modeFlags) {
4328 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4329 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4330 if (modeFlags == 0) {
4331 return;
4332 }
4333
Joe Onorato8a9b2202010-02-26 18:56:32 -08004334 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004335 "Revoking all granted permissions to " + uri);
4336
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004337 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338
4339 final String authority = uri.getAuthority();
4340 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004341 ContentProviderRecord cpr = mProvidersByName.get(authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 if (cpr != null) {
4343 pi = cpr.info;
4344 } else {
4345 try {
4346 pi = pm.resolveContentProvider(authority,
4347 PackageManager.GET_URI_PERMISSION_PATTERNS);
4348 } catch (RemoteException ex) {
4349 }
4350 }
4351 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004352 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004353 return;
4354 }
4355
4356 // Does the caller have this permission on the URI?
4357 if (!checkHoldingPermissionsLocked(pm, pi, callingUid, modeFlags)) {
4358 // Right now, if you are not the original owner of the permission,
4359 // you are not allowed to revoke it.
4360 //if (!checkUriPermissionLocked(uri, callingUid, modeFlags)) {
4361 throw new SecurityException("Uid " + callingUid
4362 + " does not have permission to uri " + uri);
4363 //}
4364 }
4365
4366 // Go through all of the permissions and remove any that match.
4367 final List<String> SEGMENTS = uri.getPathSegments();
4368 if (SEGMENTS != null) {
4369 final int NS = SEGMENTS.size();
4370 int N = mGrantedUriPermissions.size();
4371 for (int i=0; i<N; i++) {
4372 HashMap<Uri, UriPermission> perms
4373 = mGrantedUriPermissions.valueAt(i);
4374 Iterator<UriPermission> it = perms.values().iterator();
4375 toploop:
4376 while (it.hasNext()) {
4377 UriPermission perm = it.next();
4378 Uri targetUri = perm.uri;
4379 if (!authority.equals(targetUri.getAuthority())) {
4380 continue;
4381 }
4382 List<String> targetSegments = targetUri.getPathSegments();
4383 if (targetSegments == null) {
4384 continue;
4385 }
4386 if (targetSegments.size() < NS) {
4387 continue;
4388 }
4389 for (int j=0; j<NS; j++) {
4390 if (!SEGMENTS.get(j).equals(targetSegments.get(j))) {
4391 continue toploop;
4392 }
4393 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08004394 if (DEBUG_URI_PERMISSION) Slog.v(TAG,
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08004395 "Revoking " + perm.uid + " permission to " + perm.uri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 perm.clearModes(modeFlags);
4397 if (perm.modeFlags == 0) {
4398 it.remove();
4399 }
4400 }
4401 if (perms.size() == 0) {
4402 mGrantedUriPermissions.remove(
4403 mGrantedUriPermissions.keyAt(i));
4404 N--;
4405 i--;
4406 }
4407 }
4408 }
4409 }
4410
4411 public void revokeUriPermission(IApplicationThread caller, Uri uri,
4412 int modeFlags) {
4413 synchronized(this) {
4414 final ProcessRecord r = getRecordForAppLocked(caller);
4415 if (r == null) {
4416 throw new SecurityException("Unable to find app for caller "
4417 + caller
4418 + " when revoking permission to uri " + uri);
4419 }
4420 if (uri == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004421 Slog.w(TAG, "revokeUriPermission: null uri");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 return;
4423 }
4424
4425 modeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION
4426 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
4427 if (modeFlags == 0) {
4428 return;
4429 }
4430
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004431 final IPackageManager pm = AppGlobals.getPackageManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004432
4433 final String authority = uri.getAuthority();
4434 ProviderInfo pi = null;
Dianne Hackborn860755f2010-06-03 18:47:52 -07004435 ContentProviderRecord cpr = mProvidersByName.get(authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 if (cpr != null) {
4437 pi = cpr.info;
4438 } else {
4439 try {
4440 pi = pm.resolveContentProvider(authority,
4441 PackageManager.GET_URI_PERMISSION_PATTERNS);
4442 } catch (RemoteException ex) {
4443 }
4444 }
4445 if (pi == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004446 Slog.w(TAG, "No content provider found for: " + authority);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447 return;
4448 }
4449
4450 revokeUriPermissionLocked(r.info.uid, uri, modeFlags);
4451 }
4452 }
4453
4454 public void showWaitingForDebugger(IApplicationThread who, boolean waiting) {
4455 synchronized (this) {
4456 ProcessRecord app =
4457 who != null ? getRecordForAppLocked(who) : null;
4458 if (app == null) return;
4459
4460 Message msg = Message.obtain();
4461 msg.what = WAIT_FOR_DEBUGGER_MSG;
4462 msg.obj = app;
4463 msg.arg1 = waiting ? 1 : 0;
4464 mHandler.sendMessage(msg);
4465 }
4466 }
4467
4468 public void getMemoryInfo(ActivityManager.MemoryInfo outInfo) {
4469 outInfo.availMem = Process.getFreeMemory();
The Android Open Source Project4df24232009-03-05 14:34:35 -08004470 outInfo.threshold = HOME_APP_MEM;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 outInfo.lowMemory = outInfo.availMem <
The Android Open Source Project4df24232009-03-05 14:34:35 -08004472 (HOME_APP_MEM + ((HIDDEN_APP_MEM-HOME_APP_MEM)/2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 }
4474
4475 // =========================================================
4476 // TASK MANAGEMENT
4477 // =========================================================
4478
4479 public List getTasks(int maxNum, int flags,
4480 IThumbnailReceiver receiver) {
4481 ArrayList list = new ArrayList();
4482
4483 PendingThumbnailsRecord pending = null;
4484 IApplicationThread topThumbnail = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004485 ActivityRecord topRecord = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486
4487 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004488 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 TAG, "getTasks: max=" + maxNum + ", flags=" + flags
4490 + ", receiver=" + receiver);
4491
4492 if (checkCallingPermission(android.Manifest.permission.GET_TASKS)
4493 != PackageManager.PERMISSION_GRANTED) {
4494 if (receiver != null) {
4495 // If the caller wants to wait for pending thumbnails,
4496 // it ain't gonna get them.
4497 try {
4498 receiver.finished();
4499 } catch (RemoteException ex) {
4500 }
4501 }
4502 String msg = "Permission Denial: getTasks() from pid="
4503 + Binder.getCallingPid()
4504 + ", uid=" + Binder.getCallingUid()
4505 + " requires " + android.Manifest.permission.GET_TASKS;
Joe Onorato8a9b2202010-02-26 18:56:32 -08004506 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 throw new SecurityException(msg);
4508 }
4509
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004510 int pos = mMainStack.mHistory.size()-1;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004511 ActivityRecord next =
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004512 pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004513 ActivityRecord top = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 CharSequence topDescription = null;
4515 TaskRecord curTask = null;
4516 int numActivities = 0;
4517 int numRunning = 0;
4518 while (pos >= 0 && maxNum > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004519 final ActivityRecord r = next;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 pos--;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004521 next = pos >= 0 ? (ActivityRecord)mMainStack.mHistory.get(pos) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522
4523 // Initialize state for next task if needed.
4524 if (top == null ||
4525 (top.state == ActivityState.INITIALIZING
4526 && top.task == r.task)) {
4527 top = r;
4528 topDescription = r.description;
4529 curTask = r.task;
4530 numActivities = numRunning = 0;
4531 }
4532
4533 // Add 'r' into the current task.
4534 numActivities++;
4535 if (r.app != null && r.app.thread != null) {
4536 numRunning++;
4537 }
4538 if (topDescription == null) {
4539 topDescription = r.description;
4540 }
4541
Joe Onorato8a9b2202010-02-26 18:56:32 -08004542 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 TAG, r.intent.getComponent().flattenToShortString()
4544 + ": task=" + r.task);
4545
4546 // If the next one is a different task, generate a new
4547 // TaskInfo entry for what we have.
4548 if (next == null || next.task != curTask) {
4549 ActivityManager.RunningTaskInfo ci
4550 = new ActivityManager.RunningTaskInfo();
4551 ci.id = curTask.taskId;
4552 ci.baseActivity = r.intent.getComponent();
4553 ci.topActivity = top.intent.getComponent();
4554 ci.thumbnail = top.thumbnail;
4555 ci.description = topDescription;
4556 ci.numActivities = numActivities;
4557 ci.numRunning = numRunning;
4558 //System.out.println(
4559 // "#" + maxNum + ": " + " descr=" + ci.description);
4560 if (ci.thumbnail == null && receiver != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004561 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 TAG, "State=" + top.state + "Idle=" + top.idle
4563 + " app=" + top.app
4564 + " thr=" + (top.app != null ? top.app.thread : null));
4565 if (top.state == ActivityState.RESUMED
4566 || top.state == ActivityState.PAUSING) {
4567 if (top.idle && top.app != null
4568 && top.app.thread != null) {
4569 topRecord = top;
4570 topThumbnail = top.app.thread;
4571 } else {
4572 top.thumbnailNeeded = true;
4573 }
4574 }
4575 if (pending == null) {
4576 pending = new PendingThumbnailsRecord(receiver);
4577 }
4578 pending.pendingRecords.add(top);
4579 }
4580 list.add(ci);
4581 maxNum--;
4582 top = null;
4583 }
4584 }
4585
4586 if (pending != null) {
4587 mPendingThumbnails.add(pending);
4588 }
4589 }
4590
Joe Onorato8a9b2202010-02-26 18:56:32 -08004591 if (localLOGV) Slog.v(TAG, "We have pending thumbnails: " + pending);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592
4593 if (topThumbnail != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004594 if (localLOGV) Slog.v(TAG, "Requesting top thumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 try {
4596 topThumbnail.requestThumbnail(topRecord);
4597 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004598 Slog.w(TAG, "Exception thrown when requesting thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 sendPendingThumbnail(null, topRecord, null, null, true);
4600 }
4601 }
4602
4603 if (pending == null && receiver != null) {
4604 // In this case all thumbnails were available and the client
4605 // is being asked to be told when the remaining ones come in...
4606 // which is unusually, since the top-most currently running
4607 // activity should never have a canned thumbnail! Oh well.
4608 try {
4609 receiver.finished();
4610 } catch (RemoteException ex) {
4611 }
4612 }
4613
4614 return list;
4615 }
4616
4617 public List<ActivityManager.RecentTaskInfo> getRecentTasks(int maxNum,
4618 int flags) {
4619 synchronized (this) {
4620 enforceCallingPermission(android.Manifest.permission.GET_TASKS,
4621 "getRecentTasks()");
4622
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004623 IPackageManager pm = AppGlobals.getPackageManager();
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07004624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 final int N = mRecentTasks.size();
4626 ArrayList<ActivityManager.RecentTaskInfo> res
4627 = new ArrayList<ActivityManager.RecentTaskInfo>(
4628 maxNum < N ? maxNum : N);
4629 for (int i=0; i<N && maxNum > 0; i++) {
4630 TaskRecord tr = mRecentTasks.get(i);
4631 if (((flags&ActivityManager.RECENT_WITH_EXCLUDED) != 0)
4632 || (tr.intent == null)
4633 || ((tr.intent.getFlags()
4634 &Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) == 0)) {
4635 ActivityManager.RecentTaskInfo rti
4636 = new ActivityManager.RecentTaskInfo();
4637 rti.id = tr.numActivities > 0 ? tr.taskId : -1;
4638 rti.baseIntent = new Intent(
4639 tr.intent != null ? tr.intent : tr.affinityIntent);
4640 rti.origActivity = tr.origActivity;
Dianne Hackborn53d9264d2010-04-13 12:49:14 -07004641
4642 if ((flags&ActivityManager.RECENT_IGNORE_UNAVAILABLE) != 0) {
4643 // Check whether this activity is currently available.
4644 try {
4645 if (rti.origActivity != null) {
4646 if (pm.getActivityInfo(rti.origActivity, 0) == null) {
4647 continue;
4648 }
4649 } else if (rti.baseIntent != null) {
4650 if (pm.queryIntentActivities(rti.baseIntent,
4651 null, 0) == null) {
4652 continue;
4653 }
4654 }
4655 } catch (RemoteException e) {
4656 // Will never happen.
4657 }
4658 }
4659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 res.add(rti);
4661 maxNum--;
4662 }
4663 }
4664 return res;
4665 }
4666 }
4667
4668 private final int findAffinityTaskTopLocked(int startIndex, String affinity) {
4669 int j;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004670 TaskRecord startTask = ((ActivityRecord)mMainStack.mHistory.get(startIndex)).task;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004671 TaskRecord jt = startTask;
4672
4673 // First look backwards
4674 for (j=startIndex-1; j>=0; j--) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004675 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 if (r.task != jt) {
4677 jt = r.task;
4678 if (affinity.equals(jt.affinity)) {
4679 return j;
4680 }
4681 }
4682 }
4683
4684 // Now look forwards
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004685 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 jt = startTask;
4687 for (j=startIndex+1; j<N; j++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004688 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 if (r.task != jt) {
4690 if (affinity.equals(jt.affinity)) {
4691 return j;
4692 }
4693 jt = r.task;
4694 }
4695 }
4696
4697 // Might it be at the top?
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004698 if (affinity.equals(((ActivityRecord)mMainStack.mHistory.get(N-1)).task.affinity)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 return N-1;
4700 }
4701
4702 return -1;
4703 }
4704
4705 /**
Dianne Hackbornb06ea702009-07-13 13:07:51 -07004706 * TODO: Add mController hook
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 */
4708 public void moveTaskToFront(int task) {
4709 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
4710 "moveTaskToFront()");
4711
4712 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004713 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
4714 Binder.getCallingUid(), "Task to front")) {
4715 return;
4716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 final long origId = Binder.clearCallingIdentity();
4718 try {
4719 int N = mRecentTasks.size();
4720 for (int i=0; i<N; i++) {
4721 TaskRecord tr = mRecentTasks.get(i);
4722 if (tr.taskId == task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004723 mMainStack.moveTaskToFrontLocked(tr, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 return;
4725 }
4726 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004727 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
4728 ActivityRecord hr = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 if (hr.task.taskId == task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004730 mMainStack.moveTaskToFrontLocked(hr.task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 return;
4732 }
4733 }
4734 } finally {
4735 Binder.restoreCallingIdentity(origId);
4736 }
4737 }
4738 }
4739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 public void moveTaskToBack(int task) {
4741 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
4742 "moveTaskToBack()");
4743
4744 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004745 if (mMainStack.mResumedActivity != null
4746 && mMainStack.mResumedActivity.task.taskId == task) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004747 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
4748 Binder.getCallingUid(), "Task to back")) {
4749 return;
4750 }
4751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004752 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004753 mMainStack.moveTaskToBackLocked(task, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004754 Binder.restoreCallingIdentity(origId);
4755 }
4756 }
4757
4758 /**
4759 * Moves an activity, and all of the other activities within the same task, to the bottom
4760 * of the history stack. The activity's order within the task is unchanged.
4761 *
4762 * @param token A reference to the activity we wish to move
4763 * @param nonRoot If false then this only works if the activity is the root
4764 * of a task; if true it will work for any activity in a task.
4765 * @return Returns true if the move completed, false if not.
4766 */
4767 public boolean moveActivityTaskToBack(IBinder token, boolean nonRoot) {
4768 synchronized(this) {
4769 final long origId = Binder.clearCallingIdentity();
4770 int taskId = getTaskForActivityLocked(token, !nonRoot);
4771 if (taskId >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004772 return mMainStack.moveTaskToBackLocked(taskId, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 }
4774 Binder.restoreCallingIdentity(origId);
4775 }
4776 return false;
4777 }
4778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 public void moveTaskBackwards(int task) {
4780 enforceCallingPermission(android.Manifest.permission.REORDER_TASKS,
4781 "moveTaskBackwards()");
4782
4783 synchronized(this) {
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07004784 if (!checkAppSwitchAllowedLocked(Binder.getCallingPid(),
4785 Binder.getCallingUid(), "Task backwards")) {
4786 return;
4787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788 final long origId = Binder.clearCallingIdentity();
4789 moveTaskBackwardsLocked(task);
4790 Binder.restoreCallingIdentity(origId);
4791 }
4792 }
4793
4794 private final void moveTaskBackwardsLocked(int task) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004795 Slog.e(TAG, "moveTaskBackwards not yet implemented!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 }
4797
4798 public int getTaskForActivity(IBinder token, boolean onlyRoot) {
4799 synchronized(this) {
4800 return getTaskForActivityLocked(token, onlyRoot);
4801 }
4802 }
4803
4804 int getTaskForActivityLocked(IBinder token, boolean onlyRoot) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004805 final int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 TaskRecord lastTask = null;
4807 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004808 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004809 if (r == token) {
4810 if (!onlyRoot || lastTask != r.task) {
4811 return r.task.taskId;
4812 }
4813 return -1;
4814 }
4815 lastTask = r.task;
4816 }
4817
4818 return -1;
4819 }
4820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 public void finishOtherInstances(IBinder token, ComponentName className) {
4822 synchronized(this) {
4823 final long origId = Binder.clearCallingIdentity();
4824
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004825 int N = mMainStack.mHistory.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 TaskRecord lastTask = null;
4827 for (int i=0; i<N; i++) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004828 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004829 if (r.realActivity.equals(className)
4830 && r != token && lastTask != r.task) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004831 if (r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004832 null, "others")) {
4833 i--;
4834 N--;
4835 }
4836 }
4837 lastTask = r.task;
4838 }
4839
4840 Binder.restoreCallingIdentity(origId);
4841 }
4842 }
4843
4844 // =========================================================
4845 // THUMBNAILS
4846 // =========================================================
4847
4848 public void reportThumbnail(IBinder token,
4849 Bitmap thumbnail, CharSequence description) {
4850 //System.out.println("Report thumbnail for " + token + ": " + thumbnail);
4851 final long origId = Binder.clearCallingIdentity();
4852 sendPendingThumbnail(null, token, thumbnail, description, true);
4853 Binder.restoreCallingIdentity(origId);
4854 }
4855
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004856 final void sendPendingThumbnail(ActivityRecord r, IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 Bitmap thumbnail, CharSequence description, boolean always) {
4858 TaskRecord task = null;
4859 ArrayList receivers = null;
4860
4861 //System.out.println("Send pending thumbnail: " + r);
4862
4863 synchronized(this) {
4864 if (r == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004865 int index = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 if (index < 0) {
4867 return;
4868 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07004869 r = (ActivityRecord)mMainStack.mHistory.get(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870 }
4871 if (thumbnail == null) {
4872 thumbnail = r.thumbnail;
4873 description = r.description;
4874 }
4875 if (thumbnail == null && !always) {
4876 // If there is no thumbnail, and this entry is not actually
4877 // going away, then abort for now and pick up the next
4878 // thumbnail we get.
4879 return;
4880 }
4881 task = r.task;
4882
4883 int N = mPendingThumbnails.size();
4884 int i=0;
4885 while (i<N) {
4886 PendingThumbnailsRecord pr =
4887 (PendingThumbnailsRecord)mPendingThumbnails.get(i);
4888 //System.out.println("Looking in " + pr.pendingRecords);
4889 if (pr.pendingRecords.remove(r)) {
4890 if (receivers == null) {
4891 receivers = new ArrayList();
4892 }
4893 receivers.add(pr);
4894 if (pr.pendingRecords.size() == 0) {
4895 pr.finished = true;
4896 mPendingThumbnails.remove(i);
4897 N--;
4898 continue;
4899 }
4900 }
4901 i++;
4902 }
4903 }
4904
4905 if (receivers != null) {
4906 final int N = receivers.size();
4907 for (int i=0; i<N; i++) {
4908 try {
4909 PendingThumbnailsRecord pr =
4910 (PendingThumbnailsRecord)receivers.get(i);
4911 pr.receiver.newThumbnail(
4912 task != null ? task.taskId : -1, thumbnail, description);
4913 if (pr.finished) {
4914 pr.receiver.finished();
4915 }
4916 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08004917 Slog.w(TAG, "Exception thrown when sending thumbnail", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 }
4919 }
4920 }
4921 }
4922
4923 // =========================================================
4924 // CONTENT PROVIDERS
4925 // =========================================================
4926
4927 private final List generateApplicationProvidersLocked(ProcessRecord app) {
4928 List providers = null;
4929 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004930 providers = AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 queryContentProviders(app.processName, app.info.uid,
Dianne Hackborn1655be42009-05-08 14:29:01 -07004932 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004933 } catch (RemoteException ex) {
4934 }
4935 if (providers != null) {
4936 final int N = providers.size();
4937 for (int i=0; i<N; i++) {
4938 ProviderInfo cpi =
4939 (ProviderInfo)providers.get(i);
Dianne Hackborn860755f2010-06-03 18:47:52 -07004940 ContentProviderRecord cpr = mProvidersByClass.get(cpi.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 if (cpr == null) {
4942 cpr = new ContentProviderRecord(cpi, app.info);
4943 mProvidersByClass.put(cpi.name, cpr);
4944 }
4945 app.pubProviders.put(cpi.name, cpr);
4946 app.addPackage(cpi.applicationInfo.packageName);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07004947 ensurePackageDexOpt(cpi.applicationInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 }
4949 }
4950 return providers;
4951 }
4952
4953 private final String checkContentProviderPermissionLocked(
Dianne Hackbornb424b632010-08-18 15:59:05 -07004954 ProviderInfo cpi, ProcessRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 final int callingPid = (r != null) ? r.pid : Binder.getCallingPid();
4956 final int callingUid = (r != null) ? r.info.uid : Binder.getCallingUid();
4957 if (checkComponentPermission(cpi.readPermission, callingPid, callingUid,
4958 cpi.exported ? -1 : cpi.applicationInfo.uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -07004959 == PackageManager.PERMISSION_GRANTED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 return null;
4961 }
4962 if (checkComponentPermission(cpi.writePermission, callingPid, callingUid,
4963 cpi.exported ? -1 : cpi.applicationInfo.uid)
4964 == PackageManager.PERMISSION_GRANTED) {
4965 return null;
4966 }
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004967
4968 PathPermission[] pps = cpi.pathPermissions;
4969 if (pps != null) {
4970 int i = pps.length;
4971 while (i > 0) {
4972 i--;
4973 PathPermission pp = pps[i];
4974 if (checkComponentPermission(pp.getReadPermission(), callingPid, callingUid,
4975 cpi.exported ? -1 : cpi.applicationInfo.uid)
Dianne Hackbornb424b632010-08-18 15:59:05 -07004976 == PackageManager.PERMISSION_GRANTED) {
Dianne Hackborn2af632f2009-07-08 14:56:37 -07004977 return null;
4978 }
4979 if (checkComponentPermission(pp.getWritePermission(), callingPid, callingUid,
4980 cpi.exported ? -1 : cpi.applicationInfo.uid)
4981 == PackageManager.PERMISSION_GRANTED) {
4982 return null;
4983 }
4984 }
4985 }
4986
Dianne Hackbornb424b632010-08-18 15:59:05 -07004987 HashMap<Uri, UriPermission> perms = mGrantedUriPermissions.get(callingUid);
4988 if (perms != null) {
4989 for (Map.Entry<Uri, UriPermission> uri : perms.entrySet()) {
4990 if (uri.getKey().getAuthority().equals(cpi.authority)) {
4991 return null;
4992 }
4993 }
4994 }
4995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 String msg = "Permission Denial: opening provider " + cpi.name
4997 + " from " + (r != null ? r : "(null)") + " (pid=" + callingPid
4998 + ", uid=" + callingUid + ") requires "
4999 + cpi.readPermission + " or " + cpi.writePermission;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005000 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 return msg;
5002 }
5003
5004 private final ContentProviderHolder getContentProviderImpl(
5005 IApplicationThread caller, String name) {
5006 ContentProviderRecord cpr;
5007 ProviderInfo cpi = null;
5008
5009 synchronized(this) {
5010 ProcessRecord r = null;
5011 if (caller != null) {
5012 r = getRecordForAppLocked(caller);
5013 if (r == null) {
5014 throw new SecurityException(
5015 "Unable to find app for caller " + caller
5016 + " (pid=" + Binder.getCallingPid()
5017 + ") when getting content provider " + name);
5018 }
5019 }
5020
5021 // First check if this content provider has been published...
Dianne Hackborn860755f2010-06-03 18:47:52 -07005022 cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 if (cpr != null) {
5024 cpi = cpr.info;
Dianne Hackbornb424b632010-08-18 15:59:05 -07005025 String msg;
5026 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
5027 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005028 }
5029
5030 if (r != null && cpr.canRunHere(r)) {
5031 // This provider has been published or is in the process
5032 // of being published... but it is also allowed to run
5033 // in the caller's process, so don't make a connection
5034 // and just let the caller instantiate its own instance.
5035 if (cpr.provider != null) {
5036 // don't give caller the provider object, it needs
5037 // to make its own.
5038 cpr = new ContentProviderRecord(cpr);
5039 }
5040 return cpr;
5041 }
5042
5043 final long origId = Binder.clearCallingIdentity();
5044
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005045 // In this case the provider instance already exists, so we can
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005046 // return it right away.
5047 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005048 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005049 "Adding provider requested by "
5050 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005051 + cpr.info.processName);
5052 Integer cnt = r.conProviders.get(cpr);
5053 if (cnt == null) {
5054 r.conProviders.put(cpr, new Integer(1));
5055 } else {
5056 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
5057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 cpr.clients.add(r);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07005059 if (cpr.app != null && r.setAdj <= PERCEPTIBLE_APP_ADJ) {
5060 // If this is a perceptible app accessing the provider,
Dianne Hackborn906497c2010-05-10 15:57:38 -07005061 // make sure to count it as being accessed and thus
5062 // back up on the LRU list. This is good because
5063 // content providers are often expensive to start.
5064 updateLruProcessLocked(cpr.app, false, true);
5065 }
Dianne Hackborn6f86c0e2010-05-11 14:20:52 -07005066 } else {
5067 cpr.externals++;
5068 }
5069
5070 if (cpr.app != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 updateOomAdjLocked(cpr.app);
5072 }
5073
5074 Binder.restoreCallingIdentity(origId);
5075
5076 } else {
5077 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005078 cpi = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07005079 resolveContentProvider(name,
5080 STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 } catch (RemoteException ex) {
5082 }
5083 if (cpi == null) {
5084 return null;
5085 }
5086
Dianne Hackbornb424b632010-08-18 15:59:05 -07005087 String msg;
5088 if ((msg=checkContentProviderPermissionLocked(cpi, r)) != null) {
5089 throw new SecurityException(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 }
5091
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08005092 if (!mSystemReady && !mDidUpdate && !mWaitingUpdate
5093 && !cpi.processName.equals("system")) {
5094 // If this content provider does not run in the system
5095 // process, and the system is not yet ready to run other
5096 // processes, then fail fast instead of hanging.
5097 throw new IllegalArgumentException(
5098 "Attempt to launch content provider before system ready");
5099 }
5100
Dianne Hackborn860755f2010-06-03 18:47:52 -07005101 cpr = mProvidersByClass.get(cpi.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005102 final boolean firstClass = cpr == null;
5103 if (firstClass) {
5104 try {
5105 ApplicationInfo ai =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005106 AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107 getApplicationInfo(
5108 cpi.applicationInfo.packageName,
Dianne Hackborn1655be42009-05-08 14:29:01 -07005109 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 if (ai == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005111 Slog.w(TAG, "No package info for content provider "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 + cpi.name);
5113 return null;
5114 }
5115 cpr = new ContentProviderRecord(cpi, ai);
5116 } catch (RemoteException ex) {
5117 // pm is in same process, this will never happen.
5118 }
5119 }
5120
5121 if (r != null && cpr.canRunHere(r)) {
5122 // If this is a multiprocess provider, then just return its
5123 // info and allow the caller to instantiate it. Only do
5124 // this if the provider is the same user as the caller's
5125 // process, or can run as root (so can be in any process).
5126 return cpr;
5127 }
5128
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005129 if (DEBUG_PROVIDER) {
5130 RuntimeException e = new RuntimeException("here");
Joe Onorato8a9b2202010-02-26 18:56:32 -08005131 Slog.w(TAG, "LAUNCHING REMOTE PROVIDER (myuid " + r.info.uid
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005132 + " pruid " + cpr.appInfo.uid + "): " + cpr.info.name, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 }
5134
5135 // This is single process, and our app is now connecting to it.
5136 // See if we are already in the process of launching this
5137 // provider.
5138 final int N = mLaunchingProviders.size();
5139 int i;
5140 for (i=0; i<N; i++) {
5141 if (mLaunchingProviders.get(i) == cpr) {
5142 break;
5143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 }
5145
5146 // If the provider is not already being launched, then get it
5147 // started.
5148 if (i >= N) {
5149 final long origId = Binder.clearCallingIdentity();
5150 ProcessRecord proc = startProcessLocked(cpi.processName,
5151 cpr.appInfo, false, 0, "content provider",
5152 new ComponentName(cpi.applicationInfo.packageName,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005153 cpi.name), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005155 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 + cpi.applicationInfo.packageName + "/"
5157 + cpi.applicationInfo.uid + " for provider "
5158 + name + ": process is bad");
5159 return null;
5160 }
5161 cpr.launchingApp = proc;
5162 mLaunchingProviders.add(cpr);
5163 Binder.restoreCallingIdentity(origId);
5164 }
5165
5166 // Make sure the provider is published (the same provider class
5167 // may be published under multiple names).
5168 if (firstClass) {
5169 mProvidersByClass.put(cpi.name, cpr);
5170 }
5171 mProvidersByName.put(name, cpr);
5172
5173 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005174 if (DEBUG_PROVIDER) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005175 "Adding provider requested by "
5176 + r.processName + " from process "
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005177 + cpr.info.processName);
5178 Integer cnt = r.conProviders.get(cpr);
5179 if (cnt == null) {
5180 r.conProviders.put(cpr, new Integer(1));
5181 } else {
5182 r.conProviders.put(cpr, new Integer(cnt.intValue()+1));
5183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 cpr.clients.add(r);
5185 } else {
5186 cpr.externals++;
5187 }
5188 }
5189 }
5190
5191 // Wait for the provider to be published...
5192 synchronized (cpr) {
5193 while (cpr.provider == null) {
5194 if (cpr.launchingApp == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005195 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 + cpi.applicationInfo.packageName + "/"
5197 + cpi.applicationInfo.uid + " for provider "
5198 + name + ": launching app became null");
Doug Zongker2bec3d42009-12-04 12:52:44 -08005199 EventLog.writeEvent(EventLogTags.AM_PROVIDER_LOST_PROCESS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 cpi.applicationInfo.packageName,
5201 cpi.applicationInfo.uid, name);
5202 return null;
5203 }
5204 try {
5205 cpr.wait();
5206 } catch (InterruptedException ex) {
5207 }
5208 }
5209 }
5210 return cpr;
5211 }
5212
5213 public final ContentProviderHolder getContentProvider(
5214 IApplicationThread caller, String name) {
5215 if (caller == null) {
5216 String msg = "null IApplicationThread when getting content provider "
5217 + name;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005218 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 throw new SecurityException(msg);
5220 }
5221
5222 return getContentProviderImpl(caller, name);
5223 }
5224
5225 private ContentProviderHolder getContentProviderExternal(String name) {
5226 return getContentProviderImpl(null, name);
5227 }
5228
5229 /**
5230 * Drop a content provider from a ProcessRecord's bookkeeping
5231 * @param cpr
5232 */
5233 public void removeContentProvider(IApplicationThread caller, String name) {
5234 synchronized (this) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07005235 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 if(cpr == null) {
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005237 // remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08005238 if (DEBUG_PROVIDER) Slog.v(TAG, name +
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005239 " provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 return;
5241 }
5242 final ProcessRecord r = getRecordForAppLocked(caller);
5243 if (r == null) {
5244 throw new SecurityException(
5245 "Unable to find app for caller " + caller +
5246 " when removing content provider " + name);
5247 }
5248 //update content provider record entry info
Dianne Hackborn860755f2010-06-03 18:47:52 -07005249 ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005250 if (DEBUG_PROVIDER) Slog.v(TAG, "Removing provider requested by "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005251 + r.info.processName + " from process "
5252 + localCpr.appInfo.processName);
5253 if (localCpr.app == r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 //should not happen. taken care of as a local provider
Joe Onorato8a9b2202010-02-26 18:56:32 -08005255 Slog.w(TAG, "removeContentProvider called on local provider: "
Dianne Hackborna1e989b2009-09-01 19:54:29 -07005256 + cpr.info.name + " in process " + r.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 return;
5258 } else {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07005259 Integer cnt = r.conProviders.get(localCpr);
5260 if (cnt == null || cnt.intValue() <= 1) {
5261 localCpr.clients.remove(r);
5262 r.conProviders.remove(localCpr);
5263 } else {
5264 r.conProviders.put(localCpr, new Integer(cnt.intValue()-1));
5265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 }
5267 updateOomAdjLocked();
5268 }
5269 }
5270
5271 private void removeContentProviderExternal(String name) {
5272 synchronized (this) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07005273 ContentProviderRecord cpr = mProvidersByName.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005274 if(cpr == null) {
5275 //remove from mProvidersByClass
Joe Onorato8a9b2202010-02-26 18:56:32 -08005276 if(localLOGV) Slog.v(TAG, name+" content provider not found in providers list");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 return;
5278 }
5279
5280 //update content provider record entry info
Dianne Hackborn860755f2010-06-03 18:47:52 -07005281 ContentProviderRecord localCpr = mProvidersByClass.get(cpr.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005282 localCpr.externals--;
5283 if (localCpr.externals < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005284 Slog.e(TAG, "Externals < 0 for content provider " + localCpr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 }
5286 updateOomAdjLocked();
5287 }
5288 }
5289
5290 public final void publishContentProviders(IApplicationThread caller,
5291 List<ContentProviderHolder> providers) {
5292 if (providers == null) {
5293 return;
5294 }
5295
5296 synchronized(this) {
5297 final ProcessRecord r = getRecordForAppLocked(caller);
5298 if (r == null) {
5299 throw new SecurityException(
5300 "Unable to find app for caller " + caller
5301 + " (pid=" + Binder.getCallingPid()
5302 + ") when publishing content providers");
5303 }
5304
5305 final long origId = Binder.clearCallingIdentity();
5306
5307 final int N = providers.size();
5308 for (int i=0; i<N; i++) {
5309 ContentProviderHolder src = providers.get(i);
5310 if (src == null || src.info == null || src.provider == null) {
5311 continue;
5312 }
Dianne Hackborn860755f2010-06-03 18:47:52 -07005313 ContentProviderRecord dst = r.pubProviders.get(src.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 if (dst != null) {
5315 mProvidersByClass.put(dst.info.name, dst);
5316 String names[] = dst.info.authority.split(";");
5317 for (int j = 0; j < names.length; j++) {
5318 mProvidersByName.put(names[j], dst);
5319 }
5320
5321 int NL = mLaunchingProviders.size();
5322 int j;
5323 for (j=0; j<NL; j++) {
5324 if (mLaunchingProviders.get(j) == dst) {
5325 mLaunchingProviders.remove(j);
5326 j--;
5327 NL--;
5328 }
5329 }
5330 synchronized (dst) {
5331 dst.provider = src.provider;
5332 dst.app = r;
5333 dst.notifyAll();
5334 }
5335 updateOomAdjLocked(r);
5336 }
5337 }
5338
5339 Binder.restoreCallingIdentity(origId);
5340 }
5341 }
5342
5343 public static final void installSystemProviders() {
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08005344 List providers;
Josh Bartel2ecce342010-02-25 10:55:48 -06005345 synchronized (mSelf) {
5346 ProcessRecord app = mSelf.mProcessNames.get("system", Process.SYSTEM_UID);
5347 providers = mSelf.generateApplicationProvidersLocked(app);
Dianne Hackborn5c83a5f2010-03-12 16:46:46 -08005348 if (providers != null) {
5349 for (int i=providers.size()-1; i>=0; i--) {
5350 ProviderInfo pi = (ProviderInfo)providers.get(i);
5351 if ((pi.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM) == 0) {
5352 Slog.w(TAG, "Not installing system proc provider " + pi.name
5353 + ": not system .apk");
5354 providers.remove(i);
5355 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -08005356 }
5357 }
5358 }
Josh Bartel2ecce342010-02-25 10:55:48 -06005359 if (providers != null) {
5360 mSystemThread.installSystemProviders(providers);
5361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005362 }
5363
5364 // =========================================================
5365 // GLOBAL MANAGEMENT
5366 // =========================================================
5367
5368 final ProcessRecord newProcessRecordLocked(IApplicationThread thread,
5369 ApplicationInfo info, String customProcess) {
5370 String proc = customProcess != null ? customProcess : info.processName;
5371 BatteryStatsImpl.Uid.Proc ps = null;
5372 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
5373 synchronized (stats) {
5374 ps = stats.getProcessStatsLocked(info.uid, proc);
5375 }
5376 return new ProcessRecord(ps, thread, info, proc);
5377 }
5378
5379 final ProcessRecord addAppLocked(ApplicationInfo info) {
5380 ProcessRecord app = getProcessRecordLocked(info.processName, info.uid);
5381
5382 if (app == null) {
5383 app = newProcessRecordLocked(null, info, null);
5384 mProcessNames.put(info.processName, info.uid, app);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08005385 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 }
5387
5388 if ((info.flags&(ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT))
5389 == (ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_PERSISTENT)) {
5390 app.persistent = true;
5391 app.maxAdj = CORE_SERVER_ADJ;
5392 }
5393 if (app.thread == null && mPersistentStartingProcesses.indexOf(app) < 0) {
5394 mPersistentStartingProcesses.add(app);
5395 startProcessLocked(app, "added application", app.processName);
5396 }
5397
5398 return app;
5399 }
5400
5401 public void unhandledBack() {
5402 enforceCallingPermission(android.Manifest.permission.FORCE_BACK,
5403 "unhandledBack()");
5404
5405 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005406 int count = mMainStack.mHistory.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -08005407 if (DEBUG_SWITCH) Slog.d(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 TAG, "Performing unhandledBack(): stack size = " + count);
5409 if (count > 1) {
5410 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005411 mMainStack.finishActivityLocked((ActivityRecord)mMainStack.mHistory.get(count-1),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412 count-1, Activity.RESULT_CANCELED, null, "unhandled-back");
5413 Binder.restoreCallingIdentity(origId);
5414 }
5415 }
5416 }
5417
5418 public ParcelFileDescriptor openContentUri(Uri uri) throws RemoteException {
5419 String name = uri.getAuthority();
5420 ContentProviderHolder cph = getContentProviderExternal(name);
5421 ParcelFileDescriptor pfd = null;
5422 if (cph != null) {
5423 // We record the binder invoker's uid in thread-local storage before
5424 // going to the content provider to open the file. Later, in the code
5425 // that handles all permissions checks, we look for this uid and use
5426 // that rather than the Activity Manager's own uid. The effect is that
5427 // we do the check against the caller's permissions even though it looks
5428 // to the content provider like the Activity Manager itself is making
5429 // the request.
5430 sCallerIdentity.set(new Identity(
5431 Binder.getCallingPid(), Binder.getCallingUid()));
5432 try {
5433 pfd = cph.provider.openFile(uri, "r");
5434 } catch (FileNotFoundException e) {
5435 // do nothing; pfd will be returned null
5436 } finally {
5437 // Ensure that whatever happens, we clean up the identity state
5438 sCallerIdentity.remove();
5439 }
5440
5441 // We've got the fd now, so we're done with the provider.
5442 removeContentProviderExternal(name);
5443 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005444 Slog.d(TAG, "Failed to get provider for authority '" + name + "'");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005445 }
5446 return pfd;
5447 }
5448
5449 public void goingToSleep() {
5450 synchronized(this) {
5451 mSleeping = true;
5452 mWindowManager.setEventDispatching(false);
5453
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005454 if (mMainStack.mResumedActivity != null) {
5455 mMainStack.pauseIfSleepingLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005457 Slog.w(TAG, "goingToSleep with no resumed activity!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005458 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005459
5460 // Initialize the wake times of all processes.
5461 checkExcessiveWakeLocksLocked(false);
5462 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
5463 Message nmsg = mHandler.obtainMessage(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
5464 mHandler.sendMessageDelayed(nmsg, WAKE_LOCK_CHECK_DELAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005465 }
5466 }
5467
Dianne Hackborn55280a92009-05-07 15:53:46 -07005468 public boolean shutdown(int timeout) {
5469 if (checkCallingPermission(android.Manifest.permission.SHUTDOWN)
5470 != PackageManager.PERMISSION_GRANTED) {
5471 throw new SecurityException("Requires permission "
5472 + android.Manifest.permission.SHUTDOWN);
5473 }
5474
5475 boolean timedout = false;
5476
5477 synchronized(this) {
5478 mShuttingDown = true;
5479 mWindowManager.setEventDispatching(false);
5480
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005481 if (mMainStack.mResumedActivity != null) {
5482 mMainStack.pauseIfSleepingLocked();
Dianne Hackborn55280a92009-05-07 15:53:46 -07005483 final long endTime = System.currentTimeMillis() + timeout;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005484 while (mMainStack.mResumedActivity != null
5485 || mMainStack.mPausingActivity != null) {
Dianne Hackborn55280a92009-05-07 15:53:46 -07005486 long delay = endTime - System.currentTimeMillis();
5487 if (delay <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08005488 Slog.w(TAG, "Activity manager shutdown timed out");
Dianne Hackborn55280a92009-05-07 15:53:46 -07005489 timedout = true;
5490 break;
5491 }
5492 try {
5493 this.wait();
5494 } catch (InterruptedException e) {
5495 }
5496 }
5497 }
5498 }
5499
5500 mUsageStatsService.shutdown();
5501 mBatteryStatsService.shutdown();
5502
5503 return timedout;
5504 }
5505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 public void wakingUp() {
5507 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005508 if (mMainStack.mGoingToSleep.isHeld()) {
5509 mMainStack.mGoingToSleep.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 }
5511 mWindowManager.setEventDispatching(true);
5512 mSleeping = false;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005513 mMainStack.resumeTopActivityLocked(null);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -07005514 mHandler.removeMessages(CHECK_EXCESSIVE_WAKE_LOCKS_MSG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005515 }
5516 }
5517
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005518 public void stopAppSwitches() {
5519 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
5520 != PackageManager.PERMISSION_GRANTED) {
5521 throw new SecurityException("Requires permission "
5522 + android.Manifest.permission.STOP_APP_SWITCHES);
5523 }
5524
5525 synchronized(this) {
5526 mAppSwitchesAllowedTime = SystemClock.uptimeMillis()
5527 + APP_SWITCH_DELAY_TIME;
5528 mDidAppSwitch = false;
5529 mHandler.removeMessages(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
5530 Message msg = mHandler.obtainMessage(DO_PENDING_ACTIVITY_LAUNCHES_MSG);
5531 mHandler.sendMessageDelayed(msg, APP_SWITCH_DELAY_TIME);
5532 }
5533 }
5534
5535 public void resumeAppSwitches() {
5536 if (checkCallingPermission(android.Manifest.permission.STOP_APP_SWITCHES)
5537 != PackageManager.PERMISSION_GRANTED) {
5538 throw new SecurityException("Requires permission "
5539 + android.Manifest.permission.STOP_APP_SWITCHES);
5540 }
5541
5542 synchronized(this) {
5543 // Note that we don't execute any pending app switches... we will
5544 // let those wait until either the timeout, or the next start
5545 // activity request.
5546 mAppSwitchesAllowedTime = 0;
5547 }
5548 }
5549
5550 boolean checkAppSwitchAllowedLocked(int callingPid, int callingUid,
5551 String name) {
5552 if (mAppSwitchesAllowedTime < SystemClock.uptimeMillis()) {
5553 return true;
5554 }
5555
5556 final int perm = checkComponentPermission(
5557 android.Manifest.permission.STOP_APP_SWITCHES, callingPid,
5558 callingUid, -1);
5559 if (perm == PackageManager.PERMISSION_GRANTED) {
5560 return true;
5561 }
5562
Joe Onorato8a9b2202010-02-26 18:56:32 -08005563 Slog.w(TAG, name + " request from " + callingUid + " stopped");
Dianne Hackborn95fc68f2009-05-19 18:37:45 -07005564 return false;
5565 }
5566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 public void setDebugApp(String packageName, boolean waitForDebugger,
5568 boolean persistent) {
5569 enforceCallingPermission(android.Manifest.permission.SET_DEBUG_APP,
5570 "setDebugApp()");
5571
5572 // Note that this is not really thread safe if there are multiple
5573 // callers into it at the same time, but that's not a situation we
5574 // care about.
5575 if (persistent) {
5576 final ContentResolver resolver = mContext.getContentResolver();
5577 Settings.System.putString(
5578 resolver, Settings.System.DEBUG_APP,
5579 packageName);
5580 Settings.System.putInt(
5581 resolver, Settings.System.WAIT_FOR_DEBUGGER,
5582 waitForDebugger ? 1 : 0);
5583 }
5584
5585 synchronized (this) {
5586 if (!persistent) {
5587 mOrigDebugApp = mDebugApp;
5588 mOrigWaitForDebugger = mWaitForDebugger;
5589 }
5590 mDebugApp = packageName;
5591 mWaitForDebugger = waitForDebugger;
5592 mDebugTransient = !persistent;
5593 if (packageName != null) {
5594 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08005595 forceStopPackageLocked(packageName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 Binder.restoreCallingIdentity(origId);
5597 }
5598 }
5599 }
5600
5601 public void setAlwaysFinish(boolean enabled) {
5602 enforceCallingPermission(android.Manifest.permission.SET_ALWAYS_FINISH,
5603 "setAlwaysFinish()");
5604
5605 Settings.System.putInt(
5606 mContext.getContentResolver(),
5607 Settings.System.ALWAYS_FINISH_ACTIVITIES, enabled ? 1 : 0);
5608
5609 synchronized (this) {
5610 mAlwaysFinishActivities = enabled;
5611 }
5612 }
5613
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005614 public void setActivityController(IActivityController controller) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 enforceCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER,
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005616 "setActivityController()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005618 mController = controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 }
5620 }
5621
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08005622 public boolean isUserAMonkey() {
5623 // For now the fact that there is a controller implies
5624 // we have a monkey.
5625 synchronized (this) {
5626 return mController != null;
5627 }
5628 }
5629
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005630 public void registerActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06005631 synchronized (this) {
5632 mWatchers.register(watcher);
5633 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005634 }
5635
5636 public void unregisterActivityWatcher(IActivityWatcher watcher) {
Josh Bartel2ecce342010-02-25 10:55:48 -06005637 synchronized (this) {
5638 mWatchers.unregister(watcher);
5639 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07005640 }
5641
Daniel Sandler69a48172010-06-23 16:29:36 -04005642 public void setImmersive(IBinder token, boolean immersive) {
5643 synchronized(this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005644 int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1;
Daniel Sandler69a48172010-06-23 16:29:36 -04005645 if (index < 0) {
5646 throw new IllegalArgumentException();
5647 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005648 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
Daniel Sandler69a48172010-06-23 16:29:36 -04005649 r.immersive = immersive;
5650 }
5651 }
5652
5653 public boolean isImmersive(IBinder token) {
5654 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005655 int index = (token != null) ? mMainStack.indexOfTokenLocked(token) : -1;
Daniel Sandler69a48172010-06-23 16:29:36 -04005656 if (index < 0) {
5657 throw new IllegalArgumentException();
5658 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005659 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(index);
Daniel Sandler69a48172010-06-23 16:29:36 -04005660 return r.immersive;
5661 }
5662 }
5663
5664 public boolean isTopActivityImmersive() {
5665 synchronized (this) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005666 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Daniel Sandler69a48172010-06-23 16:29:36 -04005667 return (r != null) ? r.immersive : false;
5668 }
5669 }
5670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 public final void enterSafeMode() {
5672 synchronized(this) {
5673 // It only makes sense to do this before the system is ready
5674 // and started launching other packages.
5675 if (!mSystemReady) {
5676 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005677 AppGlobals.getPackageManager().enterSafeMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 } catch (RemoteException e) {
5679 }
5680
5681 View v = LayoutInflater.from(mContext).inflate(
5682 com.android.internal.R.layout.safe_mode, null);
5683 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
5684 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
5685 lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
5686 lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
5687 lp.gravity = Gravity.BOTTOM | Gravity.LEFT;
5688 lp.format = v.getBackground().getOpacity();
5689 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
5690 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
5691 ((WindowManager)mContext.getSystemService(
5692 Context.WINDOW_SERVICE)).addView(v, lp);
5693 }
5694 }
5695 }
5696
5697 public void noteWakeupAlarm(IIntentSender sender) {
5698 if (!(sender instanceof PendingIntentRecord)) {
5699 return;
5700 }
5701 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
5702 synchronized (stats) {
5703 if (mBatteryStatsService.isOnBattery()) {
5704 mBatteryStatsService.enforceCallingPermission();
5705 PendingIntentRecord rec = (PendingIntentRecord)sender;
5706 int MY_UID = Binder.getCallingUid();
5707 int uid = rec.uid == MY_UID ? Process.SYSTEM_UID : rec.uid;
5708 BatteryStatsImpl.Uid.Pkg pkg =
5709 stats.getPackageStatsLocked(uid, rec.key.packageName);
5710 pkg.incWakeupsLocked();
5711 }
5712 }
5713 }
5714
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07005715 public boolean killPids(int[] pids, String pReason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005716 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07005717 throw new SecurityException("killPids only available to the system");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07005719 String reason = (pReason == null) ? "Unknown" : pReason;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 // XXX Note: don't acquire main activity lock here, because the window
5721 // manager calls in with its locks held.
5722
5723 boolean killed = false;
5724 synchronized (mPidsSelfLocked) {
5725 int[] types = new int[pids.length];
5726 int worstType = 0;
5727 for (int i=0; i<pids.length; i++) {
5728 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
5729 if (proc != null) {
5730 int type = proc.setAdj;
5731 types[i] = type;
5732 if (type > worstType) {
5733 worstType = type;
5734 }
5735 }
5736 }
5737
5738 // If the worse oom_adj is somewhere in the hidden proc LRU range,
5739 // then constrain it so we will kill all hidden procs.
5740 if (worstType < EMPTY_APP_ADJ && worstType > HIDDEN_APP_MIN_ADJ) {
5741 worstType = HIDDEN_APP_MIN_ADJ;
5742 }
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07005743 Slog.w(TAG, "Killing processes " + reason + " at adjustment " + worstType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005744 for (int i=0; i<pids.length; i++) {
5745 ProcessRecord proc = mPidsSelfLocked.get(pids[i]);
5746 if (proc == null) {
5747 continue;
5748 }
5749 int adj = proc.setAdj;
Dianne Hackborn906497c2010-05-10 15:57:38 -07005750 if (adj >= worstType && !proc.killedBackground) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07005751 Slog.w(TAG, "Killing " + proc + " (adj " + adj + "): " + reason);
Suchi Amalapurapue99bb5f2010-03-19 14:36:49 -07005752 EventLog.writeEvent(EventLogTags.AM_KILL, proc.pid,
5753 proc.processName, adj, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 killed = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -07005755 proc.killedBackground = true;
5756 Process.killProcessQuiet(pids[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757 }
5758 }
5759 }
5760 return killed;
5761 }
5762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 public final void startRunning(String pkg, String cls, String action,
5764 String data) {
5765 synchronized(this) {
5766 if (mStartRunning) {
5767 return;
5768 }
5769 mStartRunning = true;
5770 mTopComponent = pkg != null && cls != null
5771 ? new ComponentName(pkg, cls) : null;
5772 mTopAction = action != null ? action : Intent.ACTION_MAIN;
5773 mTopData = data;
5774 if (!mSystemReady) {
5775 return;
5776 }
5777 }
5778
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07005779 systemReady(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 }
5781
5782 private void retrieveSettings() {
5783 final ContentResolver resolver = mContext.getContentResolver();
5784 String debugApp = Settings.System.getString(
5785 resolver, Settings.System.DEBUG_APP);
5786 boolean waitForDebugger = Settings.System.getInt(
5787 resolver, Settings.System.WAIT_FOR_DEBUGGER, 0) != 0;
5788 boolean alwaysFinishActivities = Settings.System.getInt(
5789 resolver, Settings.System.ALWAYS_FINISH_ACTIVITIES, 0) != 0;
5790
5791 Configuration configuration = new Configuration();
5792 Settings.System.getConfiguration(resolver, configuration);
5793
5794 synchronized (this) {
5795 mDebugApp = mOrigDebugApp = debugApp;
5796 mWaitForDebugger = mOrigWaitForDebugger = waitForDebugger;
5797 mAlwaysFinishActivities = alwaysFinishActivities;
5798 // This happens before any activities are started, so we can
5799 // change mConfiguration in-place.
5800 mConfiguration.updateFrom(configuration);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005801 mConfigurationSeq = mConfiguration.seq = 1;
Joe Onorato8a9b2202010-02-26 18:56:32 -08005802 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Initial config: " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803 }
5804 }
5805
5806 public boolean testIsSystemReady() {
5807 // no need to synchronize(this) just to read & return the value
5808 return mSystemReady;
5809 }
5810
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07005811 public void systemReady(final Runnable goingCallback) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 // In the simulator, startRunning will never have been called, which
5813 // normally sets a few crucial variables. Do it here instead.
5814 if (!Process.supportsProcesses()) {
5815 mStartRunning = true;
5816 mTopAction = Intent.ACTION_MAIN;
5817 }
5818
5819 synchronized(this) {
5820 if (mSystemReady) {
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07005821 if (goingCallback != null) goingCallback.run();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 return;
5823 }
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005824
5825 // Check to see if there are any update receivers to run.
5826 if (!mDidUpdate) {
5827 if (mWaitingUpdate) {
5828 return;
5829 }
5830 Intent intent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
5831 List<ResolveInfo> ris = null;
5832 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005833 ris = AppGlobals.getPackageManager().queryIntentReceivers(
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005834 intent, null, 0);
5835 } catch (RemoteException e) {
5836 }
5837 if (ris != null) {
5838 for (int i=ris.size()-1; i>=0; i--) {
5839 if ((ris.get(i).activityInfo.applicationInfo.flags
5840 &ApplicationInfo.FLAG_SYSTEM) == 0) {
5841 ris.remove(i);
5842 }
5843 }
5844 intent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE);
5845 for (int i=0; i<ris.size(); i++) {
5846 ActivityInfo ai = ris.get(i).activityInfo;
5847 intent.setComponent(new ComponentName(ai.packageName, ai.name));
5848 IIntentReceiver finisher = null;
Dianne Hackbornd6847842010-01-12 18:14:19 -08005849 if (i == ris.size()-1) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005850 finisher = new IIntentReceiver.Stub() {
5851 public void performReceive(Intent intent, int resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -07005852 String data, Bundle extras, boolean ordered,
5853 boolean sticky)
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005854 throws RemoteException {
5855 synchronized (ActivityManagerService.this) {
5856 mDidUpdate = true;
5857 }
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07005858 systemReady(goingCallback);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005859 }
5860 };
5861 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08005862 Slog.i(TAG, "Sending system update to: " + intent.getComponent());
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005863 broadcastIntentLocked(null, null, intent, null, finisher,
5864 0, null, null, null, true, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackbornd6847842010-01-12 18:14:19 -08005865 if (finisher != null) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005866 mWaitingUpdate = true;
5867 }
5868 }
5869 }
5870 if (mWaitingUpdate) {
5871 return;
5872 }
5873 mDidUpdate = true;
5874 }
5875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 mSystemReady = true;
5877 if (!mStartRunning) {
5878 return;
5879 }
5880 }
5881
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005882 ArrayList<ProcessRecord> procsToKill = null;
5883 synchronized(mPidsSelfLocked) {
5884 for (int i=mPidsSelfLocked.size()-1; i>=0; i--) {
5885 ProcessRecord proc = mPidsSelfLocked.valueAt(i);
5886 if (!isAllowedWhileBooting(proc.info)){
5887 if (procsToKill == null) {
5888 procsToKill = new ArrayList<ProcessRecord>();
5889 }
5890 procsToKill.add(proc);
5891 }
5892 }
5893 }
5894
5895 if (procsToKill != null) {
5896 synchronized(this) {
5897 for (int i=procsToKill.size()-1; i>=0; i--) {
5898 ProcessRecord proc = procsToKill.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08005899 Slog.i(TAG, "Removing system update proc: " + proc);
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005900 removeProcessLocked(proc, true);
5901 }
5902 }
5903 }
5904
Joe Onorato8a9b2202010-02-26 18:56:32 -08005905 Slog.i(TAG, "System now ready");
Doug Zongker2bec3d42009-12-04 12:52:44 -08005906 EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_AMS_READY,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 SystemClock.uptimeMillis());
5908
5909 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005910 // Make sure we have no pre-ready processes sitting around.
5911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005912 if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
5913 ResolveInfo ri = mContext.getPackageManager()
5914 .resolveActivity(new Intent(Intent.ACTION_FACTORY_TEST),
Dianne Hackborn1655be42009-05-08 14:29:01 -07005915 STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005916 CharSequence errorMsg = null;
5917 if (ri != null) {
5918 ActivityInfo ai = ri.activityInfo;
5919 ApplicationInfo app = ai.applicationInfo;
5920 if ((app.flags&ApplicationInfo.FLAG_SYSTEM) != 0) {
5921 mTopAction = Intent.ACTION_FACTORY_TEST;
5922 mTopData = null;
5923 mTopComponent = new ComponentName(app.packageName,
5924 ai.name);
5925 } else {
5926 errorMsg = mContext.getResources().getText(
5927 com.android.internal.R.string.factorytest_not_system);
5928 }
5929 } else {
5930 errorMsg = mContext.getResources().getText(
5931 com.android.internal.R.string.factorytest_no_action);
5932 }
5933 if (errorMsg != null) {
5934 mTopAction = null;
5935 mTopData = null;
5936 mTopComponent = null;
5937 Message msg = Message.obtain();
5938 msg.what = SHOW_FACTORY_ERROR_MSG;
5939 msg.getData().putCharSequence("msg", errorMsg);
5940 mHandler.sendMessage(msg);
5941 }
5942 }
5943 }
5944
5945 retrieveSettings();
5946
Dianne Hackborna34f1ad2009-09-02 13:26:28 -07005947 if (goingCallback != null) goingCallback.run();
5948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005949 synchronized (this) {
5950 if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
5951 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005952 List apps = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07005953 getPersistentApplications(STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005954 if (apps != null) {
5955 int N = apps.size();
5956 int i;
5957 for (i=0; i<N; i++) {
5958 ApplicationInfo info
5959 = (ApplicationInfo)apps.get(i);
5960 if (info != null &&
5961 !info.packageName.equals("android")) {
5962 addAppLocked(info);
5963 }
5964 }
5965 }
5966 } catch (RemoteException ex) {
5967 // pm is in same process, this will never happen.
5968 }
5969 }
5970
Dianne Hackborn9acc0302009-08-25 00:27:12 -07005971 // Start up initial activity.
5972 mBooting = true;
5973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005974 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07005975 if (AppGlobals.getPackageManager().hasSystemUidErrors()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005976 Message msg = Message.obtain();
5977 msg.what = SHOW_UID_ERROR_MSG;
5978 mHandler.sendMessage(msg);
5979 }
5980 } catch (RemoteException e) {
5981 }
5982
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07005983 mMainStack.resumeTopActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 }
5985 }
5986
Dan Egnorb7f03672009-12-09 16:22:32 -08005987 private boolean makeAppCrashingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08005988 String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 app.crashing = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08005990 app.crashingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08005991 ActivityManager.ProcessErrorStateInfo.CRASHED, null, shortMsg, longMsg, stackTrace);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005992 startAppProblemLocked(app);
5993 app.stopFreezingAllLocked();
5994 return handleAppCrashLocked(app);
5995 }
5996
Dan Egnorb7f03672009-12-09 16:22:32 -08005997 private void makeAppNotRespondingLocked(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08005998 String activity, String shortMsg, String longMsg) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005999 app.notResponding = true;
Dan Egnorb7f03672009-12-09 16:22:32 -08006000 app.notRespondingReport = generateProcessError(app,
Dan Egnor60d87622009-12-16 16:32:58 -08006001 ActivityManager.ProcessErrorStateInfo.NOT_RESPONDING,
6002 activity, shortMsg, longMsg, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006003 startAppProblemLocked(app);
6004 app.stopFreezingAllLocked();
6005 }
6006
6007 /**
6008 * Generate a process error record, suitable for attachment to a ProcessRecord.
6009 *
6010 * @param app The ProcessRecord in which the error occurred.
6011 * @param condition Crashing, Application Not Responding, etc. Values are defined in
6012 * ActivityManager.AppErrorStateInfo
Dan Egnor60d87622009-12-16 16:32:58 -08006013 * @param activity The activity associated with the crash, if known.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006014 * @param shortMsg Short message describing the crash.
6015 * @param longMsg Long message describing the crash.
Dan Egnorb7f03672009-12-09 16:22:32 -08006016 * @param stackTrace Full crash stack trace, may be null.
6017 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006018 * @return Returns a fully-formed AppErrorStateInfo record.
6019 */
6020 private ActivityManager.ProcessErrorStateInfo generateProcessError(ProcessRecord app,
Dan Egnor60d87622009-12-16 16:32:58 -08006021 int condition, String activity, String shortMsg, String longMsg, String stackTrace) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 ActivityManager.ProcessErrorStateInfo report = new ActivityManager.ProcessErrorStateInfo();
Dan Egnorb7f03672009-12-09 16:22:32 -08006023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006024 report.condition = condition;
6025 report.processName = app.processName;
6026 report.pid = app.pid;
6027 report.uid = app.info.uid;
Dan Egnor60d87622009-12-16 16:32:58 -08006028 report.tag = activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006029 report.shortMsg = shortMsg;
6030 report.longMsg = longMsg;
Dan Egnorb7f03672009-12-09 16:22:32 -08006031 report.stackTrace = stackTrace;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006032
6033 return report;
6034 }
6035
Dan Egnor42471dd2010-01-07 17:25:22 -08006036 void killAppAtUsersRequest(ProcessRecord app, Dialog fromDialog) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 synchronized (this) {
6038 app.crashing = false;
6039 app.crashingReport = null;
6040 app.notResponding = false;
6041 app.notRespondingReport = null;
6042 if (app.anrDialog == fromDialog) {
6043 app.anrDialog = null;
6044 }
6045 if (app.waitDialog == fromDialog) {
6046 app.waitDialog = null;
6047 }
6048 if (app.pid > 0 && app.pid != MY_PID) {
Dan Egnor42471dd2010-01-07 17:25:22 -08006049 handleAppCrashLocked(app);
Dianne Hackborn8633e682010-04-22 16:03:41 -07006050 Slog.i(ActivityManagerService.TAG, "Killing "
6051 + app.processName + " (pid=" + app.pid + "): user's request");
6052 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
6053 app.processName, app.setAdj, "user's request after error");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 Process.killProcess(app.pid);
6055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 }
6057 }
Dan Egnor42471dd2010-01-07 17:25:22 -08006058
Dan Egnorb7f03672009-12-09 16:22:32 -08006059 private boolean handleAppCrashLocked(ProcessRecord app) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006060 long now = SystemClock.uptimeMillis();
6061
6062 Long crashTime = mProcessCrashTimes.get(app.info.processName,
6063 app.info.uid);
6064 if (crashTime != null && now < crashTime+MIN_CRASH_INTERVAL) {
6065 // This process loses!
Joe Onorato8a9b2202010-02-26 18:56:32 -08006066 Slog.w(TAG, "Process " + app.info.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006067 + " has crashed too many times: killing!");
Doug Zongker2bec3d42009-12-04 12:52:44 -08006068 EventLog.writeEvent(EventLogTags.AM_PROCESS_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006069 app.info.processName, app.info.uid);
6070 killServicesLocked(app, false);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006071 for (int i=mMainStack.mHistory.size()-1; i>=0; i--) {
6072 ActivityRecord r = (ActivityRecord)mMainStack.mHistory.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006073 if (r.app == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006074 Slog.w(TAG, " Force finishing activity "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006075 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006076 r.stack.finishActivityLocked(r, i, Activity.RESULT_CANCELED, null, "crashed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 }
6078 }
6079 if (!app.persistent) {
6080 // We don't want to start this process again until the user
6081 // explicitly does so... but for persistent process, we really
6082 // need to keep it running. If a persistent process is actually
6083 // repeatedly crashing, then badness for everyone.
Doug Zongker2bec3d42009-12-04 12:52:44 -08006084 EventLog.writeEvent(EventLogTags.AM_PROC_BAD, app.info.uid,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006085 app.info.processName);
6086 mBadProcesses.put(app.info.processName, app.info.uid, now);
6087 app.bad = true;
6088 mProcessCrashTimes.remove(app.info.processName, app.info.uid);
6089 app.removed = true;
6090 removeProcessLocked(app, false);
6091 return false;
6092 }
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006093 } else {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006094 ActivityRecord r = mMainStack.topRunningActivityLocked(null);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006095 if (r.app == app) {
6096 // If the top running activity is from this crashing
6097 // process, then terminate it to avoid getting in a loop.
6098 Slog.w(TAG, " Force finishing activity "
6099 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006100 int index = mMainStack.indexOfTokenLocked(r);
6101 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006102 Activity.RESULT_CANCELED, null, "crashed");
6103 // Also terminate an activities below it that aren't yet
6104 // stopped, to avoid a situation where one will get
6105 // re-start our crashing activity once it gets resumed again.
6106 index--;
6107 if (index >= 0) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006108 r = (ActivityRecord)mMainStack.mHistory.get(index);
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006109 if (r.state == ActivityState.RESUMED
6110 || r.state == ActivityState.PAUSING
6111 || r.state == ActivityState.PAUSED) {
6112 if (!r.isHomeActivity) {
6113 Slog.w(TAG, " Force finishing activity "
6114 + r.intent.getComponent().flattenToShortString());
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006115 r.stack.finishActivityLocked(r, index,
Dianne Hackbornf83c5552010-03-31 22:19:32 -07006116 Activity.RESULT_CANCELED, null, "crashed");
6117 }
6118 }
6119 }
6120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 }
6122
6123 // Bump up the crash count of any services currently running in the proc.
6124 if (app.services.size() != 0) {
6125 // Any services running in the application need to be placed
6126 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07006127 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07006129 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006130 sr.crashCount++;
6131 }
6132 }
6133
6134 mProcessCrashTimes.put(app.info.processName, app.info.uid, now);
6135 return true;
6136 }
6137
6138 void startAppProblemLocked(ProcessRecord app) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08006139 app.errorReportReceiver = ApplicationErrorReport.getErrorReportReceiver(
6140 mContext, app.info.packageName, app.info.flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 skipCurrentReceiverLocked(app);
6142 }
6143
6144 void skipCurrentReceiverLocked(ProcessRecord app) {
6145 boolean reschedule = false;
6146 BroadcastRecord r = app.curReceiver;
6147 if (r != null) {
6148 // The current broadcast is waiting for this app's receiver
6149 // to be finished. Looks like that's not going to happen, so
6150 // let the broadcast continue.
6151 logBroadcastReceiverDiscard(r);
6152 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
6153 r.resultExtras, r.resultAbort, true);
6154 reschedule = true;
6155 }
6156 r = mPendingBroadcast;
6157 if (r != null && r.curApp == app) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006158 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006159 "skip & discard pending app " + r);
6160 logBroadcastReceiverDiscard(r);
6161 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
6162 r.resultExtras, r.resultAbort, true);
6163 reschedule = true;
6164 }
6165 if (reschedule) {
6166 scheduleBroadcastsLocked();
6167 }
6168 }
6169
Dan Egnor60d87622009-12-16 16:32:58 -08006170 /**
6171 * Used by {@link com.android.internal.os.RuntimeInit} to report when an application crashes.
6172 * The application process will exit immediately after this call returns.
6173 * @param app object of the crashing app, null for the system server
6174 * @param crashInfo describing the exception
6175 */
6176 public void handleApplicationCrash(IBinder app, ApplicationErrorReport.CrashInfo crashInfo) {
6177 ProcessRecord r = findAppProcess(app);
6178
6179 EventLog.writeEvent(EventLogTags.AM_CRASH, Binder.getCallingPid(),
6180 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08006181 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08006182 crashInfo.exceptionClassName,
6183 crashInfo.exceptionMessage,
6184 crashInfo.throwFileName,
6185 crashInfo.throwLineNumber);
6186
Dan Egnor42471dd2010-01-07 17:25:22 -08006187 addErrorToDropBox("crash", r, null, null, null, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08006188
6189 crashApplication(r, crashInfo);
6190 }
6191
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006192 public void handleApplicationStrictModeViolation(
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006193 IBinder app,
6194 int violationMask,
6195 StrictMode.ViolationInfo info) {
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006196 ProcessRecord r = findAppProcess(app);
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006197
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006198 if ((violationMask & StrictMode.PENALTY_DROPBOX) != 0) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006199 Integer stackFingerprint = info.crashInfo.stackTrace.hashCode();
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006200 boolean logIt = true;
6201 synchronized (mAlreadyLoggedViolatedStacks) {
6202 if (mAlreadyLoggedViolatedStacks.contains(stackFingerprint)) {
6203 logIt = false;
6204 // TODO: sub-sample into EventLog for these, with
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006205 // the info.durationMillis? Then we'd get
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006206 // the relative pain numbers, without logging all
6207 // the stack traces repeatedly. We'd want to do
6208 // likewise in the client code, which also does
6209 // dup suppression, before the Binder call.
6210 } else {
6211 if (mAlreadyLoggedViolatedStacks.size() >= MAX_DUP_SUPPRESSED_STACKS) {
6212 mAlreadyLoggedViolatedStacks.clear();
6213 }
6214 mAlreadyLoggedViolatedStacks.add(stackFingerprint);
6215 }
6216 }
6217 if (logIt) {
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006218 logStrictModeViolationToDropBox(r, info);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006219 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006220 }
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006221
6222 if ((violationMask & StrictMode.PENALTY_DIALOG) != 0) {
6223 AppErrorResult result = new AppErrorResult();
6224 synchronized (this) {
6225 final long origId = Binder.clearCallingIdentity();
6226
6227 Message msg = Message.obtain();
6228 msg.what = SHOW_STRICT_MODE_VIOLATION_MSG;
6229 HashMap<String, Object> data = new HashMap<String, Object>();
6230 data.put("result", result);
6231 data.put("app", r);
Brad Fitzpatrick143666f2010-06-14 12:40:21 -07006232 data.put("violationMask", violationMask);
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006233 data.put("info", info);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006234 msg.obj = data;
6235 mHandler.sendMessage(msg);
6236
6237 Binder.restoreCallingIdentity(origId);
6238 }
6239 int res = result.get();
Dianne Hackbornb424b632010-08-18 15:59:05 -07006240 Slog.w(TAG, "handleApplicationStrictModeViolation; res=" + res);
Brad Fitzpatrick46d42382010-06-11 13:57:58 -07006241 }
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07006242 }
6243
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006244 // Depending on the policy in effect, there could be a bunch of
6245 // these in quick succession so we try to batch these together to
6246 // minimize disk writes, number of dropbox entries, and maximize
6247 // compression, by having more fewer, larger records.
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006248 private void logStrictModeViolationToDropBox(
6249 ProcessRecord process,
6250 StrictMode.ViolationInfo info) {
6251 if (info == null) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006252 return;
6253 }
6254 final boolean isSystemApp = process == null ||
6255 (process.info.flags & (ApplicationInfo.FLAG_SYSTEM |
6256 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
6257 final String dropboxTag = isSystemApp ? "system_app_strictmode" : "data_app_strictmode";
6258 final DropBoxManager dbox = (DropBoxManager)
6259 mContext.getSystemService(Context.DROPBOX_SERVICE);
6260
6261 // Exit early if the dropbox isn't configured to accept this report type.
6262 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
6263
6264 boolean bufferWasEmpty;
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006265 boolean needsFlush;
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006266 final StringBuilder sb = isSystemApp ? mStrictModeBuffer : new StringBuilder(1024);
6267 synchronized (sb) {
6268 bufferWasEmpty = sb.length() == 0;
6269 appendDropBoxProcessHeaders(process, sb);
6270 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
6271 sb.append("System-App: ").append(isSystemApp).append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006272 sb.append("Uptime-Millis: ").append(info.violationUptimeMillis).append("\n");
6273 if (info.violationNumThisLoop != 0) {
6274 sb.append("Loop-Violation-Number: ").append(info.violationNumThisLoop).append("\n");
6275 }
6276 if (info != null && info.durationMillis != -1) {
6277 sb.append("Duration-Millis: ").append(info.durationMillis).append("\n");
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006278 }
6279 sb.append("\n");
Brad Fitzpatrickcb9ceb12010-07-29 14:29:02 -07006280 if (info.crashInfo != null && info.crashInfo.stackTrace != null) {
6281 sb.append(info.crashInfo.stackTrace);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006282 }
6283 sb.append("\n");
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006284
6285 // Only buffer up to ~64k. Various logging bits truncate
6286 // things at 128k.
6287 needsFlush = (sb.length() > 64 * 1024);
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006288 }
6289
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006290 // Flush immediately if the buffer's grown too large, or this
6291 // is a non-system app. Non-system apps are isolated with a
6292 // different tag & policy and not batched.
6293 //
6294 // Batching is useful during internal testing with
6295 // StrictMode settings turned up high. Without batching,
6296 // thousands of separate files could be created on boot.
6297 if (!isSystemApp || needsFlush) {
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006298 new Thread("Error dump: " + dropboxTag) {
6299 @Override
6300 public void run() {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006301 String report;
6302 synchronized (sb) {
6303 report = sb.toString();
6304 sb.delete(0, sb.length());
6305 sb.trimToSize();
6306 }
6307 if (report.length() != 0) {
6308 dbox.addText(dropboxTag, report);
6309 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006310 }
6311 }.start();
6312 return;
6313 }
6314
6315 // System app batching:
6316 if (!bufferWasEmpty) {
Brad Fitzpatricke73eb532010-07-27 16:54:39 -07006317 // An existing dropbox-writing thread is outstanding, so
6318 // we don't need to start it up. The existing thread will
6319 // catch the buffer appends we just did.
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006320 return;
6321 }
6322
6323 // Worker thread to both batch writes and to avoid blocking the caller on I/O.
6324 // (After this point, we shouldn't access AMS internal data structures.)
6325 new Thread("Error dump: " + dropboxTag) {
6326 @Override
6327 public void run() {
6328 // 5 second sleep to let stacks arrive and be batched together
6329 try {
6330 Thread.sleep(5000); // 5 seconds
6331 } catch (InterruptedException e) {}
6332
6333 String errorReport;
6334 synchronized (mStrictModeBuffer) {
6335 errorReport = mStrictModeBuffer.toString();
6336 if (errorReport.length() == 0) {
6337 return;
6338 }
6339 mStrictModeBuffer.delete(0, mStrictModeBuffer.length());
6340 mStrictModeBuffer.trimToSize();
6341 }
6342 dbox.addText(dropboxTag, errorReport);
6343 }
6344 }.start();
6345 }
6346
Dan Egnor60d87622009-12-16 16:32:58 -08006347 /**
6348 * Used by {@link Log} via {@link com.android.internal.os.RuntimeInit} to report serious errors.
6349 * @param app object of the crashing app, null for the system server
6350 * @param tag reported by the caller
6351 * @param crashInfo describing the context of the error
6352 * @return true if the process should exit immediately (WTF is fatal)
6353 */
6354 public boolean handleApplicationWtf(IBinder app, String tag,
Dan Egnorb7f03672009-12-09 16:22:32 -08006355 ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnor60d87622009-12-16 16:32:58 -08006356 ProcessRecord r = findAppProcess(app);
6357
6358 EventLog.writeEvent(EventLogTags.AM_WTF, Binder.getCallingPid(),
6359 app == null ? "system" : (r == null ? "unknown" : r.processName),
Dan Egnor2780e732010-01-22 14:47:35 -08006360 r == null ? -1 : r.info.flags,
Dan Egnor60d87622009-12-16 16:32:58 -08006361 tag, crashInfo.exceptionMessage);
6362
Dan Egnor42471dd2010-01-07 17:25:22 -08006363 addErrorToDropBox("wtf", r, null, null, tag, null, null, crashInfo);
Dan Egnor60d87622009-12-16 16:32:58 -08006364
Doug Zongker43866e02010-01-07 12:09:54 -08006365 if (Settings.Secure.getInt(mContext.getContentResolver(),
6366 Settings.Secure.WTF_IS_FATAL, 0) != 0) {
Dan Egnor60d87622009-12-16 16:32:58 -08006367 crashApplication(r, crashInfo);
6368 return true;
6369 } else {
6370 return false;
6371 }
6372 }
6373
6374 /**
6375 * @param app object of some object (as stored in {@link com.android.internal.os.RuntimeInit})
6376 * @return the corresponding {@link ProcessRecord} object, or null if none could be found
6377 */
6378 private ProcessRecord findAppProcess(IBinder app) {
6379 if (app == null) {
6380 return null;
6381 }
6382
6383 synchronized (this) {
6384 for (SparseArray<ProcessRecord> apps : mProcessNames.getMap().values()) {
6385 final int NA = apps.size();
6386 for (int ia=0; ia<NA; ia++) {
6387 ProcessRecord p = apps.valueAt(ia);
6388 if (p.thread != null && p.thread.asBinder() == app) {
6389 return p;
6390 }
6391 }
6392 }
6393
Joe Onorato8a9b2202010-02-26 18:56:32 -08006394 Slog.w(TAG, "Can't find mystery application: " + app);
Dan Egnor60d87622009-12-16 16:32:58 -08006395 return null;
6396 }
6397 }
6398
6399 /**
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006400 * Utility function for addErrorToDropBox and handleStrictModeViolation's logging
6401 * to append various headers to the dropbox log text.
Dan Egnor60d87622009-12-16 16:32:58 -08006402 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006403 private static void appendDropBoxProcessHeaders(ProcessRecord process, StringBuilder sb) {
Dan Egnora455d192010-03-12 08:52:28 -08006404 if (process == null || process.pid == MY_PID) {
6405 sb.append("Process: system_server\n");
6406 } else {
6407 sb.append("Process: ").append(process.processName).append("\n");
6408 }
6409 if (process != null) {
6410 int flags = process.info.flags;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006411 IPackageManager pm = AppGlobals.getPackageManager();
Dan Egnora455d192010-03-12 08:52:28 -08006412 sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
6413 for (String pkg : process.pkgList) {
6414 sb.append("Package: ").append(pkg);
Dan Egnor42471dd2010-01-07 17:25:22 -08006415 try {
Dan Egnora455d192010-03-12 08:52:28 -08006416 PackageInfo pi = pm.getPackageInfo(pkg, 0);
6417 if (pi != null) {
6418 sb.append(" v").append(pi.versionCode);
6419 if (pi.versionName != null) {
6420 sb.append(" (").append(pi.versionName).append(")");
6421 }
6422 }
6423 } catch (RemoteException e) {
6424 Slog.e(TAG, "Error getting package info: " + pkg, e);
Dan Egnor60d87622009-12-16 16:32:58 -08006425 }
Dan Egnora455d192010-03-12 08:52:28 -08006426 sb.append("\n");
Dan Egnor60d87622009-12-16 16:32:58 -08006427 }
Dan Egnora455d192010-03-12 08:52:28 -08006428 }
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07006429 }
6430
6431 private static String processClass(ProcessRecord process) {
6432 if (process == null || process.pid == MY_PID) {
6433 return "system_server";
6434 } else if ((process.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
6435 return "system_app";
6436 } else {
6437 return "data_app";
6438 }
6439 }
6440
6441 /**
6442 * Write a description of an error (crash, WTF, ANR) to the drop box.
6443 * @param eventType to include in the drop box tag ("crash", "wtf", etc.)
6444 * @param process which caused the error, null means the system server
6445 * @param activity which triggered the error, null if unknown
6446 * @param parent activity related to the error, null if unknown
6447 * @param subject line related to the error, null if absent
6448 * @param report in long form describing the error, null if absent
6449 * @param logFile to include in the report, null if none
6450 * @param crashInfo giving an application stack trace, null if absent
6451 */
6452 public void addErrorToDropBox(String eventType,
6453 ProcessRecord process, ActivityRecord activity, ActivityRecord parent, String subject,
6454 final String report, final File logFile,
6455 final ApplicationErrorReport.CrashInfo crashInfo) {
6456 // NOTE -- this must never acquire the ActivityManagerService lock,
6457 // otherwise the watchdog may be prevented from resetting the system.
6458
6459 final String dropboxTag = processClass(process) + "_" + eventType;
6460 final DropBoxManager dbox = (DropBoxManager)
6461 mContext.getSystemService(Context.DROPBOX_SERVICE);
6462
6463 // Exit early if the dropbox isn't configured to accept this report type.
6464 if (dbox == null || !dbox.isTagEnabled(dropboxTag)) return;
6465
6466 final StringBuilder sb = new StringBuilder(1024);
6467 appendDropBoxProcessHeaders(process, sb);
Dan Egnora455d192010-03-12 08:52:28 -08006468 if (activity != null) {
6469 sb.append("Activity: ").append(activity.shortComponentName).append("\n");
6470 }
6471 if (parent != null && parent.app != null && parent.app.pid != process.pid) {
6472 sb.append("Parent-Process: ").append(parent.app.processName).append("\n");
6473 }
6474 if (parent != null && parent != activity) {
6475 sb.append("Parent-Activity: ").append(parent.shortComponentName).append("\n");
6476 }
6477 if (subject != null) {
6478 sb.append("Subject: ").append(subject).append("\n");
6479 }
6480 sb.append("Build: ").append(Build.FINGERPRINT).append("\n");
6481 sb.append("\n");
6482
6483 // Do the rest in a worker thread to avoid blocking the caller on I/O
6484 // (After this point, we shouldn't access AMS internal data structures.)
6485 Thread worker = new Thread("Error dump: " + dropboxTag) {
6486 @Override
6487 public void run() {
6488 if (report != null) {
6489 sb.append(report);
6490 }
6491 if (logFile != null) {
6492 try {
6493 sb.append(FileUtils.readTextFile(logFile, 128 * 1024, "\n\n[[TRUNCATED]]"));
6494 } catch (IOException e) {
6495 Slog.e(TAG, "Error reading " + logFile, e);
6496 }
6497 }
6498 if (crashInfo != null && crashInfo.stackTrace != null) {
6499 sb.append(crashInfo.stackTrace);
6500 }
6501
6502 String setting = Settings.Secure.ERROR_LOGCAT_PREFIX + dropboxTag;
6503 int lines = Settings.Secure.getInt(mContext.getContentResolver(), setting, 0);
6504 if (lines > 0) {
6505 sb.append("\n");
6506
6507 // Merge several logcat streams, and take the last N lines
6508 InputStreamReader input = null;
6509 try {
6510 java.lang.Process logcat = new ProcessBuilder("/system/bin/logcat",
6511 "-v", "time", "-b", "events", "-b", "system", "-b", "main",
6512 "-t", String.valueOf(lines)).redirectErrorStream(true).start();
6513
6514 try { logcat.getOutputStream().close(); } catch (IOException e) {}
6515 try { logcat.getErrorStream().close(); } catch (IOException e) {}
6516 input = new InputStreamReader(logcat.getInputStream());
6517
6518 int num;
6519 char[] buf = new char[8192];
6520 while ((num = input.read(buf)) > 0) sb.append(buf, 0, num);
6521 } catch (IOException e) {
6522 Slog.e(TAG, "Error running logcat", e);
6523 } finally {
6524 if (input != null) try { input.close(); } catch (IOException e) {}
6525 }
6526 }
6527
6528 dbox.addText(dropboxTag, sb.toString());
Dan Egnor60d87622009-12-16 16:32:58 -08006529 }
Dan Egnora455d192010-03-12 08:52:28 -08006530 };
6531
6532 if (process == null || process.pid == MY_PID) {
6533 worker.run(); // We may be about to die -- need to run this synchronously
6534 } else {
6535 worker.start();
Dan Egnor60d87622009-12-16 16:32:58 -08006536 }
6537 }
6538
6539 /**
6540 * Bring up the "unexpected error" dialog box for a crashing app.
6541 * Deal with edge cases (intercepts from instrumented applications,
6542 * ActivityController, error intent receivers, that sort of thing).
6543 * @param r the application crashing
6544 * @param crashInfo describing the failure
6545 */
6546 private void crashApplication(ProcessRecord r, ApplicationErrorReport.CrashInfo crashInfo) {
Dan Egnorb7f03672009-12-09 16:22:32 -08006547 long timeMillis = System.currentTimeMillis();
6548 String shortMsg = crashInfo.exceptionClassName;
6549 String longMsg = crashInfo.exceptionMessage;
6550 String stackTrace = crashInfo.stackTrace;
6551 if (shortMsg != null && longMsg != null) {
6552 longMsg = shortMsg + ": " + longMsg;
6553 } else if (shortMsg != null) {
6554 longMsg = shortMsg;
6555 }
6556
Dan Egnor60d87622009-12-16 16:32:58 -08006557 AppErrorResult result = new AppErrorResult();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 synchronized (this) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006559 if (mController != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006560 try {
6561 String name = r != null ? r.processName : null;
6562 int pid = r != null ? r.pid : Binder.getCallingPid();
Dan Egnor60d87622009-12-16 16:32:58 -08006563 if (!mController.appCrashed(name, pid,
Dan Egnorb7f03672009-12-09 16:22:32 -08006564 shortMsg, longMsg, timeMillis, crashInfo.stackTrace)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006565 Slog.w(TAG, "Force-killing crashed app " + name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006566 + " at watcher's request");
6567 Process.killProcess(pid);
Dan Egnorb7f03672009-12-09 16:22:32 -08006568 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006569 }
6570 } catch (RemoteException e) {
Dianne Hackbornb06ea702009-07-13 13:07:51 -07006571 mController = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006572 }
6573 }
6574
6575 final long origId = Binder.clearCallingIdentity();
6576
6577 // If this process is running instrumentation, finish it.
6578 if (r != null && r.instrumentationClass != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006579 Slog.w(TAG, "Error in app " + r.processName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006580 + " running instrumentation " + r.instrumentationClass + ":");
Joe Onorato8a9b2202010-02-26 18:56:32 -08006581 if (shortMsg != null) Slog.w(TAG, " " + shortMsg);
6582 if (longMsg != null) Slog.w(TAG, " " + longMsg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006583 Bundle info = new Bundle();
6584 info.putString("shortMsg", shortMsg);
6585 info.putString("longMsg", longMsg);
6586 finishInstrumentationLocked(r, Activity.RESULT_CANCELED, info);
6587 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08006588 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 }
6590
Dan Egnor60d87622009-12-16 16:32:58 -08006591 // If we can't identify the process or it's already exceeded its crash quota,
6592 // quit right away without showing a crash dialog.
6593 if (r == null || !makeAppCrashingLocked(r, shortMsg, longMsg, stackTrace)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 Binder.restoreCallingIdentity(origId);
Dan Egnorb7f03672009-12-09 16:22:32 -08006595 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 }
6597
6598 Message msg = Message.obtain();
6599 msg.what = SHOW_ERROR_MSG;
6600 HashMap data = new HashMap();
6601 data.put("result", result);
6602 data.put("app", r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 msg.obj = data;
6604 mHandler.sendMessage(msg);
6605
6606 Binder.restoreCallingIdentity(origId);
6607 }
6608
6609 int res = result.get();
6610
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006611 Intent appErrorIntent = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006612 synchronized (this) {
6613 if (r != null) {
6614 mProcessCrashTimes.put(r.info.processName, r.info.uid,
6615 SystemClock.uptimeMillis());
6616 }
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006617 if (res == AppErrorDialog.FORCE_QUIT_AND_REPORT) {
Dan Egnorb7f03672009-12-09 16:22:32 -08006618 appErrorIntent = createAppErrorIntentLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006619 }
6620 }
6621
6622 if (appErrorIntent != null) {
6623 try {
6624 mContext.startActivity(appErrorIntent);
6625 } catch (ActivityNotFoundException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006626 Slog.w(TAG, "bug report receiver dissappeared", e);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006629 }
Dan Egnorb7f03672009-12-09 16:22:32 -08006630
6631 Intent createAppErrorIntentLocked(ProcessRecord r,
6632 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
6633 ApplicationErrorReport report = createAppErrorReportLocked(r, timeMillis, crashInfo);
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006634 if (report == null) {
6635 return null;
6636 }
6637 Intent result = new Intent(Intent.ACTION_APP_ERROR);
6638 result.setComponent(r.errorReportReceiver);
6639 result.putExtra(Intent.EXTRA_BUG_REPORT, report);
6640 result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
6641 return result;
6642 }
6643
Dan Egnorb7f03672009-12-09 16:22:32 -08006644 private ApplicationErrorReport createAppErrorReportLocked(ProcessRecord r,
6645 long timeMillis, ApplicationErrorReport.CrashInfo crashInfo) {
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006646 if (r.errorReportReceiver == null) {
6647 return null;
6648 }
6649
6650 if (!r.crashing && !r.notResponding) {
6651 return null;
6652 }
6653
Dan Egnorb7f03672009-12-09 16:22:32 -08006654 ApplicationErrorReport report = new ApplicationErrorReport();
6655 report.packageName = r.info.packageName;
6656 report.installerPackageName = r.errorReportReceiver.getPackageName();
6657 report.processName = r.processName;
6658 report.time = timeMillis;
Jacek Surazskie0ee6ef2010-01-07 16:23:03 +01006659 report.systemApp = (r.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006660
Dan Egnorb7f03672009-12-09 16:22:32 -08006661 if (r.crashing) {
6662 report.type = ApplicationErrorReport.TYPE_CRASH;
6663 report.crashInfo = crashInfo;
6664 } else if (r.notResponding) {
6665 report.type = ApplicationErrorReport.TYPE_ANR;
6666 report.anrInfo = new ApplicationErrorReport.AnrInfo();
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006667
Dan Egnorb7f03672009-12-09 16:22:32 -08006668 report.anrInfo.activity = r.notRespondingReport.tag;
6669 report.anrInfo.cause = r.notRespondingReport.shortMsg;
6670 report.anrInfo.info = r.notRespondingReport.longMsg;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006671 }
6672
Dan Egnorb7f03672009-12-09 16:22:32 -08006673 return report;
Jacek Surazskif5b9c722009-05-18 12:09:59 +02006674 }
6675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 public List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState() {
6677 // assume our apps are happy - lazy create the list
6678 List<ActivityManager.ProcessErrorStateInfo> errList = null;
6679
6680 synchronized (this) {
6681
6682 // iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08006683 for (int i=mLruProcesses.size()-1; i>=0; i--) {
6684 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006685 if ((app.thread != null) && (app.crashing || app.notResponding)) {
6686 // This one's in trouble, so we'll generate a report for it
6687 // crashes are higher priority (in case there's a crash *and* an anr)
6688 ActivityManager.ProcessErrorStateInfo report = null;
6689 if (app.crashing) {
6690 report = app.crashingReport;
6691 } else if (app.notResponding) {
6692 report = app.notRespondingReport;
6693 }
6694
6695 if (report != null) {
6696 if (errList == null) {
6697 errList = new ArrayList<ActivityManager.ProcessErrorStateInfo>(1);
6698 }
6699 errList.add(report);
6700 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08006701 Slog.w(TAG, "Missing app error report, app = " + app.processName +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006702 " crashing = " + app.crashing +
6703 " notResponding = " + app.notResponding);
6704 }
6705 }
6706 }
6707 }
6708
6709 return errList;
6710 }
6711
6712 public List<ActivityManager.RunningAppProcessInfo> getRunningAppProcesses() {
6713 // Lazy instantiation of list
6714 List<ActivityManager.RunningAppProcessInfo> runList = null;
6715 synchronized (this) {
6716 // Iterate across all processes
Dianne Hackborndd71fc82009-12-16 19:24:32 -08006717 for (int i=mLruProcesses.size()-1; i>=0; i--) {
6718 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006719 if ((app.thread != null) && (!app.crashing && !app.notResponding)) {
6720 // Generate process state info for running application
6721 ActivityManager.RunningAppProcessInfo currApp =
6722 new ActivityManager.RunningAppProcessInfo(app.processName,
6723 app.pid, app.getPackageList());
Dianne Hackborneb034652009-09-07 00:49:58 -07006724 currApp.uid = app.info.uid;
Dianne Hackbornbaf42c62010-06-24 11:23:39 -07006725 if (mHeavyWeightProcess == app) {
6726 currApp.flags |= ActivityManager.RunningAppProcessInfo.FLAG_HEAVY_WEIGHT;
6727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006728 int adj = app.curAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -08006729 if (adj >= EMPTY_APP_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006730 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY;
6731 } else if (adj >= HIDDEN_APP_MIN_ADJ) {
6732 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
The Android Open Source Project4df24232009-03-05 14:34:35 -08006733 currApp.lru = adj - HIDDEN_APP_MIN_ADJ + 1;
6734 } else if (adj >= HOME_APP_ADJ) {
6735 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND;
6736 currApp.lru = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006737 } else if (adj >= SECONDARY_SERVER_ADJ) {
6738 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07006739 } else if (adj >= HEAVY_WEIGHT_APP_ADJ) {
6740 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_HEAVY_WEIGHT;
6741 } else if (adj >= PERCEPTIBLE_APP_ADJ) {
6742 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006743 } else if (adj >= VISIBLE_APP_ADJ) {
6744 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
6745 } else {
6746 currApp.importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
6747 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07006748 currApp.importanceReasonCode = app.adjTypeCode;
6749 if (app.adjSource instanceof ProcessRecord) {
6750 currApp.importanceReasonPid = ((ProcessRecord)app.adjSource).pid;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006751 } else if (app.adjSource instanceof ActivityRecord) {
6752 ActivityRecord r = (ActivityRecord)app.adjSource;
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07006753 if (r.app != null) currApp.importanceReasonPid = r.app.pid;
6754 }
6755 if (app.adjTarget instanceof ComponentName) {
6756 currApp.importanceReasonComponent = (ComponentName)app.adjTarget;
6757 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08006758 //Slog.v(TAG, "Proc " + app.processName + ": imp=" + currApp.importance
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006759 // + " lru=" + currApp.lru);
6760 if (runList == null) {
6761 runList = new ArrayList<ActivityManager.RunningAppProcessInfo>();
6762 }
6763 runList.add(currApp);
6764 }
6765 }
6766 }
6767 return runList;
6768 }
6769
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07006770 public List<ApplicationInfo> getRunningExternalApplications() {
6771 List<ActivityManager.RunningAppProcessInfo> runningApps = getRunningAppProcesses();
6772 List<ApplicationInfo> retList = new ArrayList<ApplicationInfo>();
6773 if (runningApps != null && runningApps.size() > 0) {
6774 Set<String> extList = new HashSet<String>();
6775 for (ActivityManager.RunningAppProcessInfo app : runningApps) {
6776 if (app.pkgList != null) {
6777 for (String pkg : app.pkgList) {
6778 extList.add(pkg);
6779 }
6780 }
6781 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07006782 IPackageManager pm = AppGlobals.getPackageManager();
Suchi Amalapurapuf7f5dda2010-03-23 10:34:28 -07006783 for (String pkg : extList) {
6784 try {
6785 ApplicationInfo info = pm.getApplicationInfo(pkg, 0);
6786 if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
6787 retList.add(info);
6788 }
6789 } catch (RemoteException e) {
6790 }
6791 }
6792 }
6793 return retList;
6794 }
6795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 @Override
6797 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006798 if (checkCallingPermission(android.Manifest.permission.DUMP)
6799 != PackageManager.PERMISSION_GRANTED) {
6800 pw.println("Permission Denial: can't dump ActivityManager from from pid="
6801 + Binder.getCallingPid()
6802 + ", uid=" + Binder.getCallingUid()
6803 + " without permission "
6804 + android.Manifest.permission.DUMP);
6805 return;
6806 }
6807
6808 boolean dumpAll = false;
6809
6810 int opti = 0;
6811 while (opti < args.length) {
6812 String opt = args[opti];
6813 if (opt == null || opt.length() <= 0 || opt.charAt(0) != '-') {
6814 break;
6815 }
6816 opti++;
6817 if ("-a".equals(opt)) {
6818 dumpAll = true;
6819 } else if ("-h".equals(opt)) {
6820 pw.println("Activity manager dump options:");
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07006821 pw.println(" [-a] [-h] [cmd] ...");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006822 pw.println(" cmd may be one of:");
6823 pw.println(" activities: activity stack state");
6824 pw.println(" broadcasts: broadcast state");
6825 pw.println(" intents: pending intent state");
6826 pw.println(" processes: process state");
6827 pw.println(" providers: content provider state");
6828 pw.println(" services: service state");
6829 pw.println(" service [name]: service client-side state");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006830 return;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006831 } else {
6832 pw.println("Unknown argument: " + opt + "; use -h for help");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006834 }
6835
6836 // Is the caller requesting to dump a particular piece of data?
6837 if (opti < args.length) {
6838 String cmd = args[opti];
6839 opti++;
6840 if ("activities".equals(cmd) || "a".equals(cmd)) {
6841 synchronized (this) {
6842 dumpActivitiesLocked(fd, pw, args, opti, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006844 return;
6845 } else if ("broadcasts".equals(cmd) || "b".equals(cmd)) {
6846 synchronized (this) {
6847 dumpBroadcastsLocked(fd, pw, args, opti, true);
6848 }
6849 return;
6850 } else if ("intents".equals(cmd) || "i".equals(cmd)) {
6851 synchronized (this) {
6852 dumpPendingIntentsLocked(fd, pw, args, opti, true);
6853 }
6854 return;
6855 } else if ("processes".equals(cmd) || "p".equals(cmd)) {
6856 synchronized (this) {
6857 dumpProcessesLocked(fd, pw, args, opti, true);
6858 }
6859 return;
6860 } else if ("providers".equals(cmd) || "prov".equals(cmd)) {
6861 synchronized (this) {
6862 dumpProvidersLocked(fd, pw, args, opti, true);
6863 }
6864 return;
6865 } else if ("service".equals(cmd)) {
Dianne Hackborn14bfa392010-07-24 19:58:06 -07006866 dumpService(fd, pw, args, opti, dumpAll);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006867 return;
6868 } else if ("services".equals(cmd) || "s".equals(cmd)) {
6869 synchronized (this) {
6870 dumpServicesLocked(fd, pw, args, opti, true);
6871 }
6872 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006873 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006874 }
6875
6876 // No piece of data specified, dump everything.
6877 synchronized (this) {
6878 boolean needSep;
6879 if (dumpAll) {
6880 pw.println("Providers in Current Activity Manager State:");
6881 }
6882 needSep = dumpProvidersLocked(fd, pw, args, opti, dumpAll);
6883 if (needSep) {
6884 pw.println(" ");
6885 }
6886 if (dumpAll) {
6887 pw.println("-------------------------------------------------------------------------------");
6888 pw.println("Broadcasts in Current Activity Manager State:");
6889 }
6890 needSep = dumpBroadcastsLocked(fd, pw, args, opti, dumpAll);
6891 if (needSep) {
6892 pw.println(" ");
6893 }
6894 if (dumpAll) {
6895 pw.println("-------------------------------------------------------------------------------");
6896 pw.println("Services in Current Activity Manager State:");
6897 }
6898 needSep = dumpServicesLocked(fd, pw, args, opti, dumpAll);
6899 if (needSep) {
6900 pw.println(" ");
6901 }
6902 if (dumpAll) {
6903 pw.println("-------------------------------------------------------------------------------");
6904 pw.println("PendingIntents in Current Activity Manager State:");
6905 }
6906 needSep = dumpPendingIntentsLocked(fd, pw, args, opti, dumpAll);
6907 if (needSep) {
6908 pw.println(" ");
6909 }
6910 if (dumpAll) {
6911 pw.println("-------------------------------------------------------------------------------");
6912 pw.println("Activities in Current Activity Manager State:");
6913 }
6914 needSep = dumpActivitiesLocked(fd, pw, args, opti, dumpAll, !dumpAll);
6915 if (needSep) {
6916 pw.println(" ");
6917 }
6918 if (dumpAll) {
6919 pw.println("-------------------------------------------------------------------------------");
6920 pw.println("Processes in Current Activity Manager State:");
6921 }
6922 dumpProcessesLocked(fd, pw, args, opti, dumpAll);
6923 }
6924 }
6925
6926 boolean dumpActivitiesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
6927 int opti, boolean dumpAll, boolean needHeader) {
6928 if (needHeader) {
6929 pw.println(" Activity stack:");
6930 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006931 dumpHistoryList(pw, mMainStack.mHistory, " ", "Hist", true);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006932 pw.println(" ");
6933 pw.println(" Running activities (most recent first):");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006934 dumpHistoryList(pw, mMainStack.mLRUActivities, " ", "Run", false);
6935 if (mMainStack.mWaitingVisibleActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006937 pw.println(" Activities waiting for another to become visible:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006938 dumpHistoryList(pw, mMainStack.mWaitingVisibleActivities, " ", "Wait", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006939 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006940 if (mMainStack.mStoppingActivities.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006942 pw.println(" Activities waiting to stop:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006943 dumpHistoryList(pw, mMainStack.mStoppingActivities, " ", "Stop", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006944 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006945 if (mMainStack.mFinishingActivities.size() > 0) {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006946 pw.println(" ");
6947 pw.println(" Activities waiting to finish:");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006948 dumpHistoryList(pw, mMainStack.mFinishingActivities, " ", "Fin", false);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006951 pw.println(" ");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006952 pw.println(" mPausingActivity: " + mMainStack.mPausingActivity);
6953 pw.println(" mResumedActivity: " + mMainStack.mResumedActivity);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006954 pw.println(" mFocusedActivity: " + mFocusedActivity);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07006955 pw.println(" mLastPausedActivity: " + mMainStack.mLastPausedActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006957 if (dumpAll && mRecentTasks.size() > 0) {
6958 pw.println(" ");
6959 pw.println("Recent tasks in Current Activity Manager State:");
6960
6961 final int N = mRecentTasks.size();
6962 for (int i=0; i<N; i++) {
6963 TaskRecord tr = mRecentTasks.get(i);
6964 pw.print(" * Recent #"); pw.print(i); pw.print(": ");
6965 pw.println(tr);
6966 mRecentTasks.get(i).dump(pw, " ");
6967 }
6968 }
6969
6970 pw.println(" ");
6971 pw.println(" mCurTask: " + mCurTask);
6972
6973 return true;
6974 }
6975
6976 boolean dumpProcessesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
6977 int opti, boolean dumpAll) {
6978 boolean needSep = false;
6979 int numPers = 0;
6980
6981 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006982 for (SparseArray<ProcessRecord> procs : mProcessNames.getMap().values()) {
6983 final int NA = procs.size();
6984 for (int ia=0; ia<NA; ia++) {
6985 if (!needSep) {
6986 pw.println(" All known processes:");
6987 needSep = true;
6988 }
6989 ProcessRecord r = procs.valueAt(ia);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07006990 pw.print(r.persistent ? " *PERS*" : " *APP*");
6991 pw.print(" UID "); pw.print(procs.keyAt(ia));
6992 pw.print(" "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 r.dump(pw, " ");
6994 if (r.persistent) {
6995 numPers++;
6996 }
6997 }
6998 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08006999 }
7000
7001 if (mLruProcesses.size() > 0) {
7002 if (needSep) pw.println(" ");
7003 needSep = true;
7004 pw.println(" Running processes (most recent first):");
7005 dumpProcessList(pw, this, mLruProcesses, " ",
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007006 "Proc", "PERS", true);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007007 needSep = true;
7008 }
7009
7010 synchronized (mPidsSelfLocked) {
7011 if (mPidsSelfLocked.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 if (needSep) pw.println(" ");
7013 needSep = true;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007014 pw.println(" PID mappings:");
7015 for (int i=0; i<mPidsSelfLocked.size(); i++) {
7016 pw.print(" PID #"); pw.print(mPidsSelfLocked.keyAt(i));
7017 pw.print(": "); pw.println(mPidsSelfLocked.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 }
7019 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007020 }
7021
7022 if (mForegroundProcesses.size() > 0) {
7023 if (needSep) pw.println(" ");
7024 needSep = true;
7025 pw.println(" Foreground Processes:");
7026 for (int i=0; i<mForegroundProcesses.size(); i++) {
7027 pw.print(" PID #"); pw.print(mForegroundProcesses.keyAt(i));
7028 pw.print(": "); pw.println(mForegroundProcesses.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007029 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007030 }
7031
7032 if (mPersistentStartingProcesses.size() > 0) {
7033 if (needSep) pw.println(" ");
7034 needSep = true;
7035 pw.println(" Persisent processes that are starting:");
7036 dumpProcessList(pw, this, mPersistentStartingProcesses, " ",
7037 "Starting Norm", "Restarting PERS", false);
7038 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007040 if (mStartingProcesses.size() > 0) {
7041 if (needSep) pw.println(" ");
7042 needSep = true;
7043 pw.println(" Processes that are starting:");
7044 dumpProcessList(pw, this, mStartingProcesses, " ",
7045 "Starting Norm", "Starting PERS", false);
7046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007047
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007048 if (mRemovedProcesses.size() > 0) {
7049 if (needSep) pw.println(" ");
7050 needSep = true;
7051 pw.println(" Processes that are being removed:");
7052 dumpProcessList(pw, this, mRemovedProcesses, " ",
7053 "Removed Norm", "Removed PERS", false);
7054 }
7055
7056 if (mProcessesOnHold.size() > 0) {
7057 if (needSep) pw.println(" ");
7058 needSep = true;
7059 pw.println(" Processes that are on old until the system is ready:");
7060 dumpProcessList(pw, this, mProcessesOnHold, " ",
7061 "OnHold Norm", "OnHold PERS", false);
7062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007064 if (mProcessesToGc.size() > 0) {
7065 if (needSep) pw.println(" ");
7066 needSep = true;
7067 pw.println(" Processes that are waiting to GC:");
7068 long now = SystemClock.uptimeMillis();
7069 for (int i=0; i<mProcessesToGc.size(); i++) {
7070 ProcessRecord proc = mProcessesToGc.get(i);
7071 pw.print(" Process "); pw.println(proc);
7072 pw.print(" lowMem="); pw.print(proc.reportLowMemory);
7073 pw.print(", last gced=");
7074 pw.print(now-proc.lastRequestedGc);
7075 pw.print(" ms ago, last lowMem=");
7076 pw.print(now-proc.lastLowMemory);
7077 pw.println(" ms ago");
7078
7079 }
7080 }
7081
7082 if (mProcessCrashTimes.getMap().size() > 0) {
7083 if (needSep) pw.println(" ");
7084 needSep = true;
7085 pw.println(" Time since processes crashed:");
7086 long now = SystemClock.uptimeMillis();
7087 for (Map.Entry<String, SparseArray<Long>> procs
7088 : mProcessCrashTimes.getMap().entrySet()) {
7089 SparseArray<Long> uids = procs.getValue();
7090 final int N = uids.size();
7091 for (int i=0; i<N; i++) {
7092 pw.print(" Process "); pw.print(procs.getKey());
7093 pw.print(" uid "); pw.print(uids.keyAt(i));
7094 pw.print(": last crashed ");
7095 pw.print((now-uids.valueAt(i)));
Dianne Hackbornfd12af42009-08-27 00:44:33 -07007096 pw.println(" ms ago");
Dianne Hackbornfd12af42009-08-27 00:44:33 -07007097 }
7098 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007100
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007101 if (mBadProcesses.getMap().size() > 0) {
7102 if (needSep) pw.println(" ");
7103 needSep = true;
7104 pw.println(" Bad processes:");
7105 for (Map.Entry<String, SparseArray<Long>> procs
7106 : mBadProcesses.getMap().entrySet()) {
7107 SparseArray<Long> uids = procs.getValue();
7108 final int N = uids.size();
7109 for (int i=0; i<N; i++) {
7110 pw.print(" Bad process "); pw.print(procs.getKey());
7111 pw.print(" uid "); pw.print(uids.keyAt(i));
7112 pw.print(": crashed at time ");
7113 pw.println(uids.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 }
7115 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007117
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007118 pw.println(" ");
7119 pw.println(" mHomeProcess: " + mHomeProcess);
Dianne Hackborn860755f2010-06-03 18:47:52 -07007120 if (mHeavyWeightProcess != null) {
7121 pw.println(" mHeavyWeightProcess: " + mHeavyWeightProcess);
7122 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007123 pw.println(" mConfiguration: " + mConfiguration);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007124 pw.println(" mConfigWillChange: " + mMainStack.mConfigWillChange);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007125 pw.println(" mSleeping=" + mSleeping + " mShuttingDown=" + mShuttingDown);
7126 if (mDebugApp != null || mOrigDebugApp != null || mDebugTransient
7127 || mOrigWaitForDebugger) {
7128 pw.println(" mDebugApp=" + mDebugApp + "/orig=" + mOrigDebugApp
7129 + " mDebugTransient=" + mDebugTransient
7130 + " mOrigWaitForDebugger=" + mOrigWaitForDebugger);
7131 }
7132 if (mAlwaysFinishActivities || mController != null) {
7133 pw.println(" mAlwaysFinishActivities=" + mAlwaysFinishActivities
7134 + " mController=" + mController);
7135 }
7136 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007137 pw.println(" Total persistent processes: " + numPers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007138 pw.println(" mStartRunning=" + mStartRunning
7139 + " mSystemReady=" + mSystemReady
7140 + " mBooting=" + mBooting
7141 + " mBooted=" + mBooted
7142 + " mFactoryTest=" + mFactoryTest);
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07007143 pw.println(" mGoingToSleep=" + mMainStack.mGoingToSleep);
7144 pw.println(" mLaunchingActivity=" + mMainStack.mLaunchingActivity);
Dianne Hackborn906497c2010-05-10 15:57:38 -07007145 pw.println(" mAdjSeq=" + mAdjSeq + " mLruSeq=" + mLruSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007146 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007147
7148 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007149 }
7150
7151 /**
7152 * There are three ways to call this:
7153 * - no service specified: dump all the services
7154 * - a flattened component name that matched an existing service was specified as the
7155 * first arg: dump that one service
7156 * - the first arg isn't the flattened component name of an existing service:
7157 * dump all services whose component contains the first arg as a substring
7158 */
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007159 protected void dumpService(FileDescriptor fd, PrintWriter pw, String[] args,
7160 int opti, boolean dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007161 String[] newArgs;
7162 String componentNameString;
7163 ServiceRecord r;
Kenny Root3619b9ab2010-02-13 10:05:42 -08007164 if (opti >= args.length) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007165 componentNameString = null;
7166 newArgs = EMPTY_STRING_ARRAY;
7167 r = null;
7168 } else {
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007169 componentNameString = args[opti];
7170 opti++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 ComponentName componentName = ComponentName.unflattenFromString(componentNameString);
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007172 synchronized (this) {
7173 r = componentName != null ? mServices.get(componentName) : null;
7174 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007175 newArgs = new String[args.length - opti];
7176 if (args.length > 2) System.arraycopy(args, opti, newArgs, 0, args.length - opti);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 }
7178
7179 if (r != null) {
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007180 dumpService(fd, pw, r, newArgs, dumpAll);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007181 } else {
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007182 ArrayList<ServiceRecord> services = new ArrayList<ServiceRecord>();
7183 synchronized (this) {
7184 for (ServiceRecord r1 : mServices.values()) {
7185 if (componentNameString == null
7186 || r1.name.flattenToString().contains(componentNameString)) {
7187 services.add(r1);
7188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007189 }
7190 }
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007191 for (int i=0; i<services.size(); i++) {
7192 dumpService(fd, pw, services.get(i), newArgs, dumpAll);
7193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007194 }
7195 }
7196
7197 /**
7198 * Invokes IApplicationThread.dumpService() on the thread of the specified service if
7199 * there is a thread associated with the service.
7200 */
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007201 private void dumpService(FileDescriptor fd, PrintWriter pw, ServiceRecord r, String[] args,
7202 boolean dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007203 pw.println(" Service " + r.name.flattenToString());
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007204 if (dumpAll) {
7205 synchronized (this) {
7206 pw.print(" * "); pw.println(r);
7207 r.dump(pw, " ");
7208 }
7209 pw.println("");
7210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007211 if (r.app != null && r.app.thread != null) {
7212 try {
7213 // flush anything that is already in the PrintWriter since the thread is going
7214 // to write to the file descriptor directly
7215 pw.flush();
7216 r.app.thread.dumpService(fd, r, args);
7217 pw.print("\n");
Dianne Hackborn14bfa392010-07-24 19:58:06 -07007218 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007219 } catch (RemoteException e) {
7220 pw.println("got a RemoteException while dumping the service");
7221 }
7222 }
7223 }
7224
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007225 boolean dumpBroadcastsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7226 int opti, boolean dumpAll) {
7227 boolean needSep = false;
7228
7229 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007230 if (mRegisteredReceivers.size() > 0) {
7231 pw.println(" ");
7232 pw.println(" Registered Receivers:");
7233 Iterator it = mRegisteredReceivers.values().iterator();
7234 while (it.hasNext()) {
7235 ReceiverList r = (ReceiverList)it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007236 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007237 r.dump(pw, " ");
7238 }
7239 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007241 pw.println(" ");
7242 pw.println("Receiver Resolver Table:");
Dianne Hackbornd4310ac2010-03-16 22:55:08 -07007243 mReceiverResolver.dump(pw, null, " ", null);
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007244 needSep = true;
7245 }
7246
7247 if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0
7248 || mPendingBroadcast != null) {
7249 if (mParallelBroadcasts.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007251 pw.println(" Active broadcasts:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007252 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007253 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
7254 pw.println(" Broadcast #" + i + ":");
7255 mParallelBroadcasts.get(i).dump(pw, " ");
7256 }
7257 if (mOrderedBroadcasts.size() > 0) {
7258 pw.println(" ");
Dianne Hackborn399cccb2010-04-13 22:57:49 -07007259 pw.println(" Active ordered broadcasts:");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007260 }
7261 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
7262 pw.println(" Serialized Broadcast #" + i + ":");
7263 mOrderedBroadcasts.get(i).dump(pw, " ");
7264 }
7265 pw.println(" ");
7266 pw.println(" Pending broadcast:");
7267 if (mPendingBroadcast != null) {
7268 mPendingBroadcast.dump(pw, " ");
7269 } else {
7270 pw.println(" (null)");
7271 }
7272 needSep = true;
7273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007274
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007275 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007276 pw.println(" ");
Dianne Hackborn12527f92009-11-11 17:39:50 -08007277 pw.println(" Historical broadcasts:");
7278 for (int i=0; i<MAX_BROADCAST_HISTORY; i++) {
7279 BroadcastRecord r = mBroadcastHistory[i];
7280 if (r == null) {
7281 break;
7282 }
7283 pw.println(" Historical Broadcast #" + i + ":");
7284 r.dump(pw, " ");
7285 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007286 needSep = true;
7287 }
7288
7289 if (mStickyBroadcasts != null) {
Dianne Hackborn12527f92009-11-11 17:39:50 -08007290 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007291 pw.println(" Sticky broadcasts:");
7292 StringBuilder sb = new StringBuilder(128);
7293 for (Map.Entry<String, ArrayList<Intent>> ent
7294 : mStickyBroadcasts.entrySet()) {
7295 pw.print(" * Sticky action "); pw.print(ent.getKey());
7296 pw.println(":");
7297 ArrayList<Intent> intents = ent.getValue();
7298 final int N = intents.size();
7299 for (int i=0; i<N; i++) {
7300 sb.setLength(0);
7301 sb.append(" Intent: ");
7302 intents.get(i).toShortString(sb, true, false);
7303 pw.println(sb.toString());
7304 Bundle bundle = intents.get(i).getExtras();
7305 if (bundle != null) {
7306 pw.print(" ");
7307 pw.println(bundle.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 }
7309 }
7310 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007311 needSep = true;
7312 }
7313
7314 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 pw.println(" ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007316 pw.println(" mBroadcastsScheduled=" + mBroadcastsScheduled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007317 pw.println(" mHandler:");
7318 mHandler.dump(new PrintWriterPrinter(pw), " ");
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007319 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007321
7322 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007323 }
7324
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007325 boolean dumpServicesLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7326 int opti, boolean dumpAll) {
7327 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007328
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007329 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007330 if (mServices.size() > 0) {
7331 pw.println(" Active services:");
7332 Iterator<ServiceRecord> it = mServices.values().iterator();
7333 while (it.hasNext()) {
7334 ServiceRecord r = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007335 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007336 r.dump(pw, " ");
7337 }
7338 needSep = true;
7339 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007342 if (mPendingServices.size() > 0) {
7343 if (needSep) pw.println(" ");
7344 pw.println(" Pending services:");
7345 for (int i=0; i<mPendingServices.size(); i++) {
7346 ServiceRecord r = mPendingServices.get(i);
7347 pw.print(" * Pending "); pw.println(r);
7348 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007350 needSep = true;
7351 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007352
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007353 if (mRestartingServices.size() > 0) {
7354 if (needSep) pw.println(" ");
7355 pw.println(" Restarting services:");
7356 for (int i=0; i<mRestartingServices.size(); i++) {
7357 ServiceRecord r = mRestartingServices.get(i);
7358 pw.print(" * Restarting "); pw.println(r);
7359 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007361 needSep = true;
7362 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007363
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007364 if (mStoppingServices.size() > 0) {
7365 if (needSep) pw.println(" ");
7366 pw.println(" Stopping services:");
7367 for (int i=0; i<mStoppingServices.size(); i++) {
7368 ServiceRecord r = mStoppingServices.get(i);
7369 pw.print(" * Stopping "); pw.println(r);
7370 r.dump(pw, " ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007371 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007372 needSep = true;
7373 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007374
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007375 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007376 if (mServiceConnections.size() > 0) {
7377 if (needSep) pw.println(" ");
7378 pw.println(" Connection bindings to services:");
7379 Iterator<ConnectionRecord> it
7380 = mServiceConnections.values().iterator();
7381 while (it.hasNext()) {
7382 ConnectionRecord r = it.next();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007383 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 r.dump(pw, " ");
7385 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007386 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007387 }
7388 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007389
7390 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007391 }
7392
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007393 boolean dumpProvidersLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7394 int opti, boolean dumpAll) {
7395 boolean needSep = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007396
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007397 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007398 if (mProvidersByClass.size() > 0) {
7399 if (needSep) pw.println(" ");
7400 pw.println(" Published content providers (by class):");
Dianne Hackborn860755f2010-06-03 18:47:52 -07007401 Iterator<Map.Entry<String, ContentProviderRecord>> it
7402 = mProvidersByClass.entrySet().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007404 Map.Entry<String, ContentProviderRecord> e = it.next();
7405 ContentProviderRecord r = e.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007406 pw.print(" * "); pw.println(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407 r.dump(pw, " ");
7408 }
7409 needSep = true;
7410 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007411
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007412 if (mProvidersByName.size() > 0) {
7413 pw.println(" ");
7414 pw.println(" Authority to provider mappings:");
Dianne Hackborn860755f2010-06-03 18:47:52 -07007415 Iterator<Map.Entry<String, ContentProviderRecord>> it
7416 = mProvidersByName.entrySet().iterator();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007417 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007418 Map.Entry<String, ContentProviderRecord> e = it.next();
7419 ContentProviderRecord r = e.getValue();
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007420 pw.print(" "); pw.print(e.getKey()); pw.print(": ");
7421 pw.println(r);
7422 }
7423 needSep = true;
7424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007426
7427 if (mLaunchingProviders.size() > 0) {
7428 if (needSep) pw.println(" ");
7429 pw.println(" Launching content providers:");
7430 for (int i=mLaunchingProviders.size()-1; i>=0; i--) {
7431 pw.print(" Launching #"); pw.print(i); pw.print(": ");
7432 pw.println(mLaunchingProviders.get(i));
7433 }
7434 needSep = true;
7435 }
7436
7437 if (mGrantedUriPermissions.size() > 0) {
7438 pw.println();
7439 pw.println("Granted Uri Permissions:");
7440 for (int i=0; i<mGrantedUriPermissions.size(); i++) {
7441 int uid = mGrantedUriPermissions.keyAt(i);
7442 HashMap<Uri, UriPermission> perms
7443 = mGrantedUriPermissions.valueAt(i);
7444 pw.print(" * UID "); pw.print(uid);
7445 pw.println(" holds:");
7446 for (UriPermission perm : perms.values()) {
7447 pw.print(" "); pw.println(perm);
7448 perm.dump(pw, " ");
7449 }
7450 }
7451 needSep = true;
7452 }
7453
7454 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 }
7456
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007457 boolean dumpPendingIntentsLocked(FileDescriptor fd, PrintWriter pw, String[] args,
7458 int opti, boolean dumpAll) {
7459 boolean needSep = false;
7460
7461 if (dumpAll) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007462 if (this.mIntentSenderRecords.size() > 0) {
7463 Iterator<WeakReference<PendingIntentRecord>> it
7464 = mIntentSenderRecords.values().iterator();
7465 while (it.hasNext()) {
7466 WeakReference<PendingIntentRecord> ref = it.next();
7467 PendingIntentRecord rec = ref != null ? ref.get(): null;
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007468 needSep = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007469 if (rec != null) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007470 pw.print(" * "); pw.println(rec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007471 rec.dump(pw, " ");
7472 } else {
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007473 pw.print(" * "); pw.print(ref);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 }
7475 }
7476 }
7477 }
Dianne Hackbornc59411b2009-12-21 20:10:14 -08007478
7479 return needSep;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007480 }
7481
7482 private static final void dumpHistoryList(PrintWriter pw, List list,
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007483 String prefix, String label, boolean complete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007484 TaskRecord lastTask = null;
7485 for (int i=list.size()-1; i>=0; i--) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07007486 ActivityRecord r = (ActivityRecord)list.get(i);
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007487 final boolean full = complete || !r.inHistory;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007488 if (lastTask != r.task) {
7489 lastTask = r.task;
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007490 pw.print(prefix);
7491 pw.print(full ? "* " : " ");
7492 pw.println(lastTask);
7493 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007494 lastTask.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -07007497 pw.print(prefix); pw.print(full ? " * " : " "); pw.print(label);
7498 pw.print(" #"); pw.print(i); pw.print(": ");
7499 pw.println(r);
7500 if (full) {
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007501 r.dump(pw, prefix + " ");
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007502 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 }
7504 }
7505
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007506 private static String buildOomTag(String prefix, String space, int val, int base) {
7507 if (val == base) {
7508 if (space == null) return prefix;
7509 return prefix + " ";
7510 }
7511 return prefix + "+" + Integer.toString(val-base);
7512 }
7513
7514 private static final int dumpProcessList(PrintWriter pw,
7515 ActivityManagerService service, List list,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007516 String prefix, String normalLabel, String persistentLabel,
7517 boolean inclOomAdj) {
7518 int numPers = 0;
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007519 final int N = list.size()-1;
7520 for (int i=N; i>=0; i--) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007521 ProcessRecord r = (ProcessRecord)list.get(i);
7522 if (false) {
7523 pw.println(prefix + (r.persistent ? persistentLabel : normalLabel)
7524 + " #" + i + ":");
7525 r.dump(pw, prefix + " ");
7526 } else if (inclOomAdj) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007527 String oomAdj;
7528 if (r.setAdj >= EMPTY_APP_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007529 oomAdj = buildOomTag("empty", null, r.setAdj, EMPTY_APP_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007530 } else if (r.setAdj >= HIDDEN_APP_MIN_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007531 oomAdj = buildOomTag("bak", " ", r.setAdj, HIDDEN_APP_MIN_ADJ);
7532 } else if (r.setAdj >= HOME_APP_ADJ) {
7533 oomAdj = buildOomTag("home ", null, r.setAdj, HOME_APP_ADJ);
7534 } else if (r.setAdj >= SECONDARY_SERVER_ADJ) {
7535 oomAdj = buildOomTag("svc", " ", r.setAdj, SECONDARY_SERVER_ADJ);
7536 } else if (r.setAdj >= BACKUP_APP_ADJ) {
7537 oomAdj = buildOomTag("bckup", null, r.setAdj, BACKUP_APP_ADJ);
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007538 } else if (r.setAdj >= HEAVY_WEIGHT_APP_ADJ) {
7539 oomAdj = buildOomTag("hvy ", null, r.setAdj, HEAVY_WEIGHT_APP_ADJ);
7540 } else if (r.setAdj >= PERCEPTIBLE_APP_ADJ) {
7541 oomAdj = buildOomTag("prcp ", null, r.setAdj, PERCEPTIBLE_APP_ADJ);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007542 } else if (r.setAdj >= VISIBLE_APP_ADJ) {
7543 oomAdj = buildOomTag("vis ", null, r.setAdj, VISIBLE_APP_ADJ);
7544 } else if (r.setAdj >= FOREGROUND_APP_ADJ) {
7545 oomAdj = buildOomTag("fore ", null, r.setAdj, FOREGROUND_APP_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007546 } else if (r.setAdj >= CORE_SERVER_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007547 oomAdj = buildOomTag("core ", null, r.setAdj, CORE_SERVER_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007548 } else if (r.setAdj >= SYSTEM_ADJ) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007549 oomAdj = buildOomTag("sys ", null, r.setAdj, SYSTEM_ADJ);
Dianne Hackborn09c916b2009-12-08 14:50:51 -08007550 } else {
7551 oomAdj = Integer.toString(r.setAdj);
7552 }
7553 String schedGroup;
7554 switch (r.setSchedGroup) {
7555 case Process.THREAD_GROUP_BG_NONINTERACTIVE:
7556 schedGroup = "B";
7557 break;
7558 case Process.THREAD_GROUP_DEFAULT:
7559 schedGroup = "F";
7560 break;
7561 default:
7562 schedGroup = Integer.toString(r.setSchedGroup);
7563 break;
7564 }
7565 pw.println(String.format("%s%s #%2d: adj=%s/%s %s (%s)",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007566 prefix, (r.persistent ? persistentLabel : normalLabel),
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007567 N-i, oomAdj, schedGroup, r.toShortString(), r.adjType));
Dianne Hackbornde42bb62009-08-05 12:26:15 -07007568 if (r.adjSource != null || r.adjTarget != null) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -07007569 pw.print(prefix);
7570 pw.print(" ");
7571 if (r.adjTarget instanceof ComponentName) {
7572 pw.print(((ComponentName)r.adjTarget).flattenToShortString());
7573 } else if (r.adjTarget != null) {
7574 pw.print(r.adjTarget.toString());
7575 } else {
7576 pw.print("{null}");
7577 }
7578 pw.print("<=");
7579 if (r.adjSource instanceof ProcessRecord) {
7580 pw.print("Proc{");
7581 pw.print(((ProcessRecord)r.adjSource).toShortString());
7582 pw.println("}");
7583 } else if (r.adjSource != null) {
7584 pw.println(r.adjSource.toString());
7585 } else {
7586 pw.println("{null}");
7587 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -07007588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 } else {
7590 pw.println(String.format("%s%s #%2d: %s",
7591 prefix, (r.persistent ? persistentLabel : normalLabel),
7592 i, r.toString()));
7593 }
7594 if (r.persistent) {
7595 numPers++;
7596 }
7597 }
7598 return numPers;
7599 }
7600
Dianne Hackborn472ad872010-04-07 17:31:48 -07007601 static final void dumpApplicationMemoryUsage(FileDescriptor fd,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007602 PrintWriter pw, List list, String prefix, String[] args) {
Dianne Hackborn6447ca32009-04-07 19:50:08 -07007603 final boolean isCheckinRequest = scanArgs(args, "--checkin");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007604 long uptime = SystemClock.uptimeMillis();
7605 long realtime = SystemClock.elapsedRealtime();
7606
7607 if (isCheckinRequest) {
7608 // short checkin version
7609 pw.println(uptime + "," + realtime);
7610 pw.flush();
7611 } else {
7612 pw.println("Applications Memory Usage (kB):");
7613 pw.println("Uptime: " + uptime + " Realtime: " + realtime);
7614 }
7615 for (int i = list.size() - 1 ; i >= 0 ; i--) {
7616 ProcessRecord r = (ProcessRecord)list.get(i);
7617 if (r.thread != null) {
7618 if (!isCheckinRequest) {
7619 pw.println("\n** MEMINFO in pid " + r.pid + " [" + r.processName + "] **");
7620 pw.flush();
7621 }
7622 try {
7623 r.thread.asBinder().dump(fd, args);
7624 } catch (RemoteException e) {
7625 if (!isCheckinRequest) {
7626 pw.println("Got RemoteException!");
7627 pw.flush();
7628 }
7629 }
7630 }
7631 }
7632 }
7633
7634 /**
7635 * Searches array of arguments for the specified string
7636 * @param args array of argument strings
7637 * @param value value to search for
7638 * @return true if the value is contained in the array
7639 */
7640 private static boolean scanArgs(String[] args, String value) {
7641 if (args != null) {
7642 for (String arg : args) {
7643 if (value.equals(arg)) {
7644 return true;
7645 }
7646 }
7647 }
7648 return false;
7649 }
7650
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007651 private final void killServicesLocked(ProcessRecord app,
7652 boolean allowRestart) {
7653 // Report disconnected services.
7654 if (false) {
7655 // XXX we are letting the client link to the service for
7656 // death notifications.
7657 if (app.services.size() > 0) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007658 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007659 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007660 ServiceRecord r = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 if (r.connections.size() > 0) {
7662 Iterator<ConnectionRecord> jt
7663 = r.connections.values().iterator();
7664 while (jt.hasNext()) {
7665 ConnectionRecord c = jt.next();
7666 if (c.binding.client != app) {
7667 try {
7668 //c.conn.connected(r.className, null);
7669 } catch (Exception e) {
7670 // todo: this should be asynchronous!
Joe Onorato8a9b2202010-02-26 18:56:32 -08007671 Slog.w(TAG, "Exception thrown disconnected servce "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 + r.shortName
7673 + " from app " + app.processName, e);
7674 }
7675 }
7676 }
7677 }
7678 }
7679 }
7680 }
7681
7682 // Clean up any connections this application has to other services.
7683 if (app.connections.size() > 0) {
7684 Iterator<ConnectionRecord> it = app.connections.iterator();
7685 while (it.hasNext()) {
7686 ConnectionRecord r = it.next();
7687 removeConnectionLocked(r, app, null);
7688 }
7689 }
7690 app.connections.clear();
7691
7692 if (app.services.size() != 0) {
7693 // Any services running in the application need to be placed
7694 // back in the pending list.
Dianne Hackborn860755f2010-06-03 18:47:52 -07007695 Iterator<ServiceRecord> it = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007696 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007697 ServiceRecord sr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007698 synchronized (sr.stats.getBatteryStats()) {
7699 sr.stats.stopLaunchedLocked();
7700 }
7701 sr.app = null;
7702 sr.executeNesting = 0;
7703 mStoppingServices.remove(sr);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07007704
7705 boolean hasClients = sr.bindings.size() > 0;
7706 if (hasClients) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007707 Iterator<IntentBindRecord> bindings
7708 = sr.bindings.values().iterator();
7709 while (bindings.hasNext()) {
7710 IntentBindRecord b = bindings.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -08007711 if (DEBUG_SERVICE) Slog.v(TAG, "Killing binding " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 + ": shouldUnbind=" + b.hasBound);
7713 b.binder = null;
7714 b.requested = b.received = b.hasBound = false;
7715 }
7716 }
7717
7718 if (sr.crashCount >= 2) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007719 Slog.w(TAG, "Service crashed " + sr.crashCount
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007720 + " times, stopping: " + sr);
Doug Zongker2bec3d42009-12-04 12:52:44 -08007721 EventLog.writeEvent(EventLogTags.AM_SERVICE_CRASHED_TOO_MUCH,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 sr.crashCount, sr.shortName, app.pid);
7723 bringDownServiceLocked(sr, true);
7724 } else if (!allowRestart) {
7725 bringDownServiceLocked(sr, true);
7726 } else {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07007727 boolean canceled = scheduleServiceRestartLocked(sr, true);
7728
7729 // Should the service remain running? Note that in the
7730 // extreme case of so many attempts to deliver a command
7731 // that it failed, that we also will stop it here.
7732 if (sr.startRequested && (sr.stopIfKilled || canceled)) {
7733 if (sr.pendingStarts.size() == 0) {
7734 sr.startRequested = false;
7735 if (!hasClients) {
7736 // Whoops, no reason to restart!
7737 bringDownServiceLocked(sr, true);
7738 }
7739 }
7740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 }
7742 }
7743
7744 if (!allowRestart) {
7745 app.services.clear();
7746 }
7747 }
7748
Dianne Hackbornde42bb62009-08-05 12:26:15 -07007749 // Make sure we have no more records on the stopping list.
7750 int i = mStoppingServices.size();
7751 while (i > 0) {
7752 i--;
7753 ServiceRecord sr = mStoppingServices.get(i);
7754 if (sr.app == app) {
7755 mStoppingServices.remove(i);
7756 }
7757 }
7758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 app.executingServices.clear();
7760 }
7761
7762 private final void removeDyingProviderLocked(ProcessRecord proc,
7763 ContentProviderRecord cpr) {
7764 synchronized (cpr) {
7765 cpr.launchingApp = null;
7766 cpr.notifyAll();
7767 }
7768
7769 mProvidersByClass.remove(cpr.info.name);
7770 String names[] = cpr.info.authority.split(";");
7771 for (int j = 0; j < names.length; j++) {
7772 mProvidersByName.remove(names[j]);
7773 }
7774
7775 Iterator<ProcessRecord> cit = cpr.clients.iterator();
7776 while (cit.hasNext()) {
7777 ProcessRecord capp = cit.next();
7778 if (!capp.persistent && capp.thread != null
7779 && capp.pid != 0
7780 && capp.pid != MY_PID) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07007781 Slog.i(TAG, "Kill " + capp.processName
7782 + " (pid " + capp.pid + "): provider " + cpr.info.name
7783 + " in dying process " + proc.processName);
7784 EventLog.writeEvent(EventLogTags.AM_KILL, capp.pid,
7785 capp.processName, capp.setAdj, "dying provider " + proc.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007786 Process.killProcess(capp.pid);
7787 }
7788 }
7789
7790 mLaunchingProviders.remove(cpr);
7791 }
7792
7793 /**
7794 * Main code for cleaning up a process when it has gone away. This is
7795 * called both as a result of the process dying, or directly when stopping
7796 * a process when running in single process mode.
7797 */
7798 private final void cleanUpApplicationRecordLocked(ProcessRecord app,
7799 boolean restarting, int index) {
7800 if (index >= 0) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -08007801 mLruProcesses.remove(index);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 }
7803
Dianne Hackborn36124872009-10-08 16:22:03 -07007804 mProcessesToGc.remove(app);
7805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 // Dismiss any open dialogs.
7807 if (app.crashDialog != null) {
7808 app.crashDialog.dismiss();
7809 app.crashDialog = null;
7810 }
7811 if (app.anrDialog != null) {
7812 app.anrDialog.dismiss();
7813 app.anrDialog = null;
7814 }
7815 if (app.waitDialog != null) {
7816 app.waitDialog.dismiss();
7817 app.waitDialog = null;
7818 }
7819
7820 app.crashing = false;
7821 app.notResponding = false;
7822
7823 app.resetPackageList();
7824 app.thread = null;
7825 app.forcingToForeground = null;
7826 app.foregroundServices = false;
7827
7828 killServicesLocked(app, true);
7829
7830 boolean restart = false;
7831
7832 int NL = mLaunchingProviders.size();
7833
7834 // Remove published content providers.
7835 if (!app.pubProviders.isEmpty()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007836 Iterator<ContentProviderRecord> it = app.pubProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 while (it.hasNext()) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007838 ContentProviderRecord cpr = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 cpr.provider = null;
7840 cpr.app = null;
7841
7842 // See if someone is waiting for this provider... in which
7843 // case we don't remove it, but just let it restart.
7844 int i = 0;
7845 if (!app.bad) {
7846 for (; i<NL; i++) {
7847 if (mLaunchingProviders.get(i) == cpr) {
7848 restart = true;
7849 break;
7850 }
7851 }
7852 } else {
7853 i = NL;
7854 }
7855
7856 if (i >= NL) {
7857 removeDyingProviderLocked(app, cpr);
7858 NL = mLaunchingProviders.size();
7859 }
7860 }
7861 app.pubProviders.clear();
7862 }
7863
Dianne Hackbornf670ef72009-11-16 13:59:16 -08007864 // Take care of any launching providers waiting for this process.
7865 if (checkAppInLaunchingProvidersLocked(app, false)) {
7866 restart = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867 }
Dianne Hackbornf670ef72009-11-16 13:59:16 -08007868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007869 // Unregister from connected content providers.
7870 if (!app.conProviders.isEmpty()) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07007871 Iterator it = app.conProviders.keySet().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 while (it.hasNext()) {
7873 ContentProviderRecord cpr = (ContentProviderRecord)it.next();
7874 cpr.clients.remove(app);
7875 }
7876 app.conProviders.clear();
7877 }
7878
Dianne Hackbornde42bb62009-08-05 12:26:15 -07007879 // At this point there may be remaining entries in mLaunchingProviders
7880 // where we were the only one waiting, so they are no longer of use.
7881 // Look for these and clean up if found.
7882 // XXX Commented out for now. Trying to figure out a way to reproduce
7883 // the actual situation to identify what is actually going on.
7884 if (false) {
7885 for (int i=0; i<NL; i++) {
7886 ContentProviderRecord cpr = (ContentProviderRecord)
7887 mLaunchingProviders.get(i);
7888 if (cpr.clients.size() <= 0 && cpr.externals <= 0) {
7889 synchronized (cpr) {
7890 cpr.launchingApp = null;
7891 cpr.notifyAll();
7892 }
7893 }
7894 }
7895 }
7896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 skipCurrentReceiverLocked(app);
7898
7899 // Unregister any receivers.
7900 if (app.receivers.size() > 0) {
7901 Iterator<ReceiverList> it = app.receivers.iterator();
7902 while (it.hasNext()) {
7903 removeReceiverLocked(it.next());
7904 }
7905 app.receivers.clear();
7906 }
7907
Christopher Tate181fafa2009-05-14 11:12:14 -07007908 // If the app is undergoing backup, tell the backup manager about it
7909 if (mBackupTarget != null && app.pid == mBackupTarget.app.pid) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007910 if (DEBUG_BACKUP) Slog.d(TAG, "App " + mBackupTarget.appInfo + " died during backup");
Christopher Tate181fafa2009-05-14 11:12:14 -07007911 try {
7912 IBackupManager bm = IBackupManager.Stub.asInterface(
7913 ServiceManager.getService(Context.BACKUP_SERVICE));
7914 bm.agentDisconnected(app.info.packageName);
7915 } catch (RemoteException e) {
7916 // can't happen; backup manager is local
7917 }
7918 }
7919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007920 // If the caller is restarting this app, then leave it in its
7921 // current lists and let the caller take care of it.
7922 if (restarting) {
7923 return;
7924 }
7925
7926 if (!app.persistent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08007927 if (DEBUG_PROCESSES) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007928 "Removing non-persistent process during cleanup: " + app);
7929 mProcessNames.remove(app.processName, app.info.uid);
Dianne Hackborn860755f2010-06-03 18:47:52 -07007930 if (mHeavyWeightProcess == app) {
7931 mHeavyWeightProcess = null;
7932 mHandler.sendEmptyMessage(CANCEL_HEAVY_NOTIFICATION_MSG);
7933 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007934 } else if (!app.removed) {
7935 // This app is persistent, so we need to keep its record around.
7936 // If it is not already on the pending app list, add it there
7937 // and start a new process for it.
7938 app.thread = null;
7939 app.forcingToForeground = null;
7940 app.foregroundServices = false;
7941 if (mPersistentStartingProcesses.indexOf(app) < 0) {
7942 mPersistentStartingProcesses.add(app);
7943 restart = true;
7944 }
7945 }
7946 mProcessesOnHold.remove(app);
7947
The Android Open Source Project4df24232009-03-05 14:34:35 -08007948 if (app == mHomeProcess) {
7949 mHomeProcess = null;
7950 }
7951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 if (restart) {
7953 // We have components that still need to be running in the
7954 // process, so re-launch it.
7955 mProcessNames.put(app.processName, app.info.uid, app);
7956 startProcessLocked(app, "restart", app.processName);
7957 } else if (app.pid > 0 && app.pid != MY_PID) {
7958 // Goodbye!
7959 synchronized (mPidsSelfLocked) {
7960 mPidsSelfLocked.remove(app.pid);
7961 mHandler.removeMessages(PROC_START_TIMEOUT_MSG, app);
7962 }
Dianne Hackbornf210d6b2009-04-13 18:42:49 -07007963 app.setPid(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007964 }
7965 }
7966
Dianne Hackbornf670ef72009-11-16 13:59:16 -08007967 boolean checkAppInLaunchingProvidersLocked(ProcessRecord app, boolean alwaysBad) {
7968 // Look through the content providers we are waiting to have launched,
7969 // and if any run in this process then either schedule a restart of
7970 // the process or kill the client waiting for it if this process has
7971 // gone bad.
7972 int NL = mLaunchingProviders.size();
7973 boolean restart = false;
7974 for (int i=0; i<NL; i++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -07007975 ContentProviderRecord cpr = mLaunchingProviders.get(i);
Dianne Hackbornf670ef72009-11-16 13:59:16 -08007976 if (cpr.launchingApp == app) {
7977 if (!alwaysBad && !app.bad) {
7978 restart = true;
7979 } else {
7980 removeDyingProviderLocked(app, cpr);
7981 NL = mLaunchingProviders.size();
7982 }
7983 }
7984 }
7985 return restart;
7986 }
7987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 // =========================================================
7989 // SERVICES
7990 // =========================================================
7991
7992 ActivityManager.RunningServiceInfo makeRunningServiceInfoLocked(ServiceRecord r) {
7993 ActivityManager.RunningServiceInfo info =
7994 new ActivityManager.RunningServiceInfo();
7995 info.service = r.name;
7996 if (r.app != null) {
7997 info.pid = r.app.pid;
7998 }
Dianne Hackborn3025ef32009-08-31 21:31:47 -07007999 info.uid = r.appInfo.uid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 info.process = r.processName;
8001 info.foreground = r.isForeground;
8002 info.activeSince = r.createTime;
8003 info.started = r.startRequested;
8004 info.clientCount = r.connections.size();
8005 info.crashCount = r.crashCount;
8006 info.lastActivityTime = r.lastActivity;
Dianne Hackborn3025ef32009-08-31 21:31:47 -07008007 if (r.isForeground) {
8008 info.flags |= ActivityManager.RunningServiceInfo.FLAG_FOREGROUND;
8009 }
8010 if (r.startRequested) {
8011 info.flags |= ActivityManager.RunningServiceInfo.FLAG_STARTED;
8012 }
Dan Egnor42471dd2010-01-07 17:25:22 -08008013 if (r.app != null && r.app.pid == MY_PID) {
Dianne Hackborn3025ef32009-08-31 21:31:47 -07008014 info.flags |= ActivityManager.RunningServiceInfo.FLAG_SYSTEM_PROCESS;
8015 }
8016 if (r.app != null && r.app.persistent) {
8017 info.flags |= ActivityManager.RunningServiceInfo.FLAG_PERSISTENT_PROCESS;
8018 }
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008019 for (ConnectionRecord conn : r.connections.values()) {
8020 if (conn.clientLabel != 0) {
8021 info.clientPackage = conn.binding.client.info.packageName;
8022 info.clientLabel = conn.clientLabel;
8023 break;
8024 }
8025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008026 return info;
8027 }
8028
8029 public List<ActivityManager.RunningServiceInfo> getServices(int maxNum,
8030 int flags) {
8031 synchronized (this) {
8032 ArrayList<ActivityManager.RunningServiceInfo> res
8033 = new ArrayList<ActivityManager.RunningServiceInfo>();
8034
8035 if (mServices.size() > 0) {
8036 Iterator<ServiceRecord> it = mServices.values().iterator();
8037 while (it.hasNext() && res.size() < maxNum) {
8038 res.add(makeRunningServiceInfoLocked(it.next()));
8039 }
8040 }
8041
8042 for (int i=0; i<mRestartingServices.size() && res.size() < maxNum; i++) {
8043 ServiceRecord r = mRestartingServices.get(i);
8044 ActivityManager.RunningServiceInfo info =
8045 makeRunningServiceInfoLocked(r);
8046 info.restarting = r.nextRestartTime;
8047 res.add(info);
8048 }
8049
8050 return res;
8051 }
8052 }
8053
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008054 public PendingIntent getRunningServiceControlPanel(ComponentName name) {
8055 synchronized (this) {
8056 ServiceRecord r = mServices.get(name);
8057 if (r != null) {
8058 for (ConnectionRecord conn : r.connections.values()) {
8059 if (conn.clientIntent != null) {
8060 return conn.clientIntent;
8061 }
8062 }
8063 }
8064 }
8065 return null;
8066 }
8067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008068 private final ServiceRecord findServiceLocked(ComponentName name,
8069 IBinder token) {
8070 ServiceRecord r = mServices.get(name);
8071 return r == token ? r : null;
8072 }
8073
8074 private final class ServiceLookupResult {
8075 final ServiceRecord record;
8076 final String permission;
8077
8078 ServiceLookupResult(ServiceRecord _record, String _permission) {
8079 record = _record;
8080 permission = _permission;
8081 }
8082 };
8083
8084 private ServiceLookupResult findServiceLocked(Intent service,
8085 String resolvedType) {
8086 ServiceRecord r = null;
8087 if (service.getComponent() != null) {
8088 r = mServices.get(service.getComponent());
8089 }
8090 if (r == null) {
8091 Intent.FilterComparison filter = new Intent.FilterComparison(service);
8092 r = mServicesByIntent.get(filter);
8093 }
8094
8095 if (r == null) {
8096 try {
8097 ResolveInfo rInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008098 AppGlobals.getPackageManager().resolveService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 service, resolvedType, 0);
8100 ServiceInfo sInfo =
8101 rInfo != null ? rInfo.serviceInfo : null;
8102 if (sInfo == null) {
8103 return null;
8104 }
8105
8106 ComponentName name = new ComponentName(
8107 sInfo.applicationInfo.packageName, sInfo.name);
8108 r = mServices.get(name);
8109 } catch (RemoteException ex) {
8110 // pm is in same process, this will never happen.
8111 }
8112 }
8113 if (r != null) {
8114 int callingPid = Binder.getCallingPid();
8115 int callingUid = Binder.getCallingUid();
8116 if (checkComponentPermission(r.permission,
8117 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
8118 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008119 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008120 + " from pid=" + callingPid
8121 + ", uid=" + callingUid
8122 + " requires " + r.permission);
8123 return new ServiceLookupResult(null, r.permission);
8124 }
8125 return new ServiceLookupResult(r, null);
8126 }
8127 return null;
8128 }
8129
8130 private class ServiceRestarter implements Runnable {
8131 private ServiceRecord mService;
8132
8133 void setService(ServiceRecord service) {
8134 mService = service;
8135 }
8136
8137 public void run() {
8138 synchronized(ActivityManagerService.this) {
8139 performServiceRestartLocked(mService);
8140 }
8141 }
8142 }
8143
8144 private ServiceLookupResult retrieveServiceLocked(Intent service,
8145 String resolvedType, int callingPid, int callingUid) {
8146 ServiceRecord r = null;
8147 if (service.getComponent() != null) {
8148 r = mServices.get(service.getComponent());
8149 }
8150 Intent.FilterComparison filter = new Intent.FilterComparison(service);
8151 r = mServicesByIntent.get(filter);
8152 if (r == null) {
8153 try {
8154 ResolveInfo rInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008155 AppGlobals.getPackageManager().resolveService(
Dianne Hackborn1655be42009-05-08 14:29:01 -07008156 service, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 ServiceInfo sInfo =
8158 rInfo != null ? rInfo.serviceInfo : null;
8159 if (sInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008160 Slog.w(TAG, "Unable to start service " + service +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 ": not found");
8162 return null;
8163 }
8164
8165 ComponentName name = new ComponentName(
8166 sInfo.applicationInfo.packageName, sInfo.name);
8167 r = mServices.get(name);
8168 if (r == null) {
8169 filter = new Intent.FilterComparison(service.cloneFilter());
8170 ServiceRestarter res = new ServiceRestarter();
8171 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
8172 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
8173 synchronized (stats) {
8174 ss = stats.getServiceStatsLocked(
8175 sInfo.applicationInfo.uid, sInfo.packageName,
8176 sInfo.name);
8177 }
Dianne Hackbornb1c4a2a2010-01-19 15:36:42 -08008178 r = new ServiceRecord(this, ss, name, filter, sInfo, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 res.setService(r);
8180 mServices.put(name, r);
8181 mServicesByIntent.put(filter, r);
8182
8183 // Make sure this component isn't in the pending list.
8184 int N = mPendingServices.size();
8185 for (int i=0; i<N; i++) {
8186 ServiceRecord pr = mPendingServices.get(i);
8187 if (pr.name.equals(name)) {
8188 mPendingServices.remove(i);
8189 i--;
8190 N--;
8191 }
8192 }
8193 }
8194 } catch (RemoteException ex) {
8195 // pm is in same process, this will never happen.
8196 }
8197 }
8198 if (r != null) {
8199 if (checkComponentPermission(r.permission,
8200 callingPid, callingUid, r.exported ? -1 : r.appInfo.uid)
8201 != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008202 Slog.w(TAG, "Permission Denial: Accessing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008203 + " from pid=" + Binder.getCallingPid()
8204 + ", uid=" + Binder.getCallingUid()
8205 + " requires " + r.permission);
8206 return new ServiceLookupResult(null, r.permission);
8207 }
8208 return new ServiceLookupResult(r, null);
8209 }
8210 return null;
8211 }
8212
8213 private final void bumpServiceExecutingLocked(ServiceRecord r) {
8214 long now = SystemClock.uptimeMillis();
8215 if (r.executeNesting == 0 && r.app != null) {
8216 if (r.app.executingServices.size() == 0) {
8217 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
8218 msg.obj = r.app;
8219 mHandler.sendMessageAtTime(msg, now+SERVICE_TIMEOUT);
8220 }
8221 r.app.executingServices.add(r);
8222 }
8223 r.executeNesting++;
8224 r.executingStart = now;
8225 }
8226
8227 private final void sendServiceArgsLocked(ServiceRecord r,
8228 boolean oomAdjusted) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008229 final int N = r.pendingStarts.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008230 if (N == 0) {
8231 return;
8232 }
8233
Dianne Hackborn39792d22010-08-19 18:01:52 -07008234 while (r.pendingStarts.size() > 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008235 try {
Dianne Hackborn39792d22010-08-19 18:01:52 -07008236 ServiceRecord.StartItem si = r.pendingStarts.remove(0);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008237 if (DEBUG_SERVICE) Slog.v(TAG, "Sending arguments to service: "
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008238 + r.name + " " + r.intent + " args=" + si.intent);
Dianne Hackborn39792d22010-08-19 18:01:52 -07008239 if (si.intent == null) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008240 // If somehow we got a dummy start at the front, then
8241 // just drop it here.
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008242 continue;
8243 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07008244 si.deliveredTime = SystemClock.uptimeMillis();
8245 r.deliveredStarts.add(si);
8246 si.deliveryCount++;
8247 if (si.targetPermissionUid >= 0) {
8248 grantUriPermissionUncheckedFromIntentLocked(si.targetPermissionUid,
8249 r.packageName, si.intent, si);
8250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008251 bumpServiceExecutingLocked(r);
8252 if (!oomAdjusted) {
8253 oomAdjusted = true;
8254 updateOomAdjLocked(r.app);
8255 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008256 int flags = 0;
8257 if (si.deliveryCount > 0) {
8258 flags |= Service.START_FLAG_RETRY;
8259 }
8260 if (si.doneExecutingCount > 0) {
8261 flags |= Service.START_FLAG_REDELIVERY;
8262 }
8263 r.app.thread.scheduleServiceArgs(r, si.id, flags, si.intent);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008264 } catch (RemoteException e) {
8265 // Remote process gone... we'll let the normal cleanup take
8266 // care of this.
8267 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008268 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008269 Slog.w(TAG, "Unexpected exception", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008270 break;
8271 }
8272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008273 }
8274
8275 private final boolean requestServiceBindingLocked(ServiceRecord r,
8276 IntentBindRecord i, boolean rebind) {
8277 if (r.app == null || r.app.thread == null) {
8278 // If service is not currently running, can't yet bind.
8279 return false;
8280 }
8281 if ((!i.requested || rebind) && i.apps.size() > 0) {
8282 try {
8283 bumpServiceExecutingLocked(r);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008284 if (DEBUG_SERVICE) Slog.v(TAG, "Connecting binding " + i
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 + ": shouldUnbind=" + i.hasBound);
8286 r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind);
8287 if (!rebind) {
8288 i.requested = true;
8289 }
8290 i.hasBound = true;
8291 i.doRebind = false;
8292 } catch (RemoteException e) {
8293 return false;
8294 }
8295 }
8296 return true;
8297 }
8298
8299 private final void requestServiceBindingsLocked(ServiceRecord r) {
8300 Iterator<IntentBindRecord> bindings = r.bindings.values().iterator();
8301 while (bindings.hasNext()) {
8302 IntentBindRecord i = bindings.next();
8303 if (!requestServiceBindingLocked(r, i, false)) {
8304 break;
8305 }
8306 }
8307 }
8308
8309 private final void realStartServiceLocked(ServiceRecord r,
8310 ProcessRecord app) throws RemoteException {
8311 if (app.thread == null) {
8312 throw new RemoteException();
8313 }
8314
8315 r.app = app;
The Android Open Source Project10592532009-03-18 17:39:46 -07008316 r.restartTime = r.lastActivity = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008317
8318 app.services.add(r);
8319 bumpServiceExecutingLocked(r);
Dianne Hackborndd71fc82009-12-16 19:24:32 -08008320 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008321
8322 boolean created = false;
8323 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008324 if (DEBUG_SERVICE) Slog.v(TAG, "Scheduling start service: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008325 + r.name + " " + r.intent);
Dianne Hackborna33e3f72009-09-29 17:28:24 -07008326 mStringBuilder.setLength(0);
8327 r.intent.getIntent().toShortString(mStringBuilder, false, true);
Doug Zongker2bec3d42009-12-04 12:52:44 -08008328 EventLog.writeEvent(EventLogTags.AM_CREATE_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008329 System.identityHashCode(r), r.shortName,
Dianne Hackborna33e3f72009-09-29 17:28:24 -07008330 mStringBuilder.toString(), r.app.pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008331 synchronized (r.stats.getBatteryStats()) {
8332 r.stats.startLaunchedLocked();
8333 }
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -07008334 ensurePackageDexOpt(r.serviceInfo.packageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 app.thread.scheduleCreateService(r, r.serviceInfo);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008336 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008337 created = true;
8338 } finally {
8339 if (!created) {
8340 app.services.remove(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008341 scheduleServiceRestartLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008342 }
8343 }
8344
8345 requestServiceBindingsLocked(r);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008346
8347 // If the service is in the started state, and there are no
8348 // pending arguments, then fake up one so its onStartCommand() will
8349 // be called.
8350 if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {
8351 r.lastStartId++;
8352 if (r.lastStartId < 1) {
8353 r.lastStartId = 1;
8354 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07008355 r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId, null, -1));
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008356 }
8357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 sendServiceArgsLocked(r, true);
8359 }
8360
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008361 private final boolean scheduleServiceRestartLocked(ServiceRecord r,
8362 boolean allowCancel) {
8363 boolean canceled = false;
8364
Dianne Hackbornfd12af42009-08-27 00:44:33 -07008365 final long now = SystemClock.uptimeMillis();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008366 long minDuration = SERVICE_RESTART_DURATION;
Dianne Hackborn6ccd2af2009-08-27 12:26:44 -07008367 long resetTime = SERVICE_RESET_RUN_DURATION;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008368
8369 // Any delivered but not yet finished starts should be put back
8370 // on the pending list.
8371 final int N = r.deliveredStarts.size();
8372 if (N > 0) {
8373 for (int i=N-1; i>=0; i--) {
8374 ServiceRecord.StartItem si = r.deliveredStarts.get(i);
Dianne Hackborn39792d22010-08-19 18:01:52 -07008375 si.removeUriPermissionsLocked();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008376 if (si.intent == null) {
8377 // We'll generate this again if needed.
8378 } else if (!allowCancel || (si.deliveryCount < ServiceRecord.MAX_DELIVERY_COUNT
8379 && si.doneExecutingCount < ServiceRecord.MAX_DONE_EXECUTING_COUNT)) {
8380 r.pendingStarts.add(0, si);
8381 long dur = SystemClock.uptimeMillis() - si.deliveredTime;
8382 dur *= 2;
8383 if (minDuration < dur) minDuration = dur;
8384 if (resetTime < dur) resetTime = dur;
8385 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008386 Slog.w(TAG, "Canceling start item " + si.intent + " in service "
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008387 + r.name);
8388 canceled = true;
8389 }
8390 }
8391 r.deliveredStarts.clear();
8392 }
8393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 r.totalRestartCount++;
8395 if (r.restartDelay == 0) {
8396 r.restartCount++;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008397 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008398 } else {
8399 // If it has been a "reasonably long time" since the service
8400 // was started, then reset our restart duration back to
8401 // the beginning, so we don't infinitely increase the duration
8402 // on a service that just occasionally gets killed (which is
8403 // a normal case, due to process being killed to reclaim memory).
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008404 if (now > (r.restartTime+resetTime)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 r.restartCount = 1;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008406 r.restartDelay = minDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008407 } else {
Dianne Hackbornfd12af42009-08-27 00:44:33 -07008408 r.restartDelay *= SERVICE_RESTART_DURATION_FACTOR;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008409 if (r.restartDelay < minDuration) {
8410 r.restartDelay = minDuration;
8411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008412 }
8413 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07008414
8415 r.nextRestartTime = now + r.restartDelay;
8416
8417 // Make sure that we don't end up restarting a bunch of services
8418 // all at the same time.
8419 boolean repeat;
8420 do {
8421 repeat = false;
8422 for (int i=mRestartingServices.size()-1; i>=0; i--) {
8423 ServiceRecord r2 = mRestartingServices.get(i);
8424 if (r2 != r && r.nextRestartTime
8425 >= (r2.nextRestartTime-SERVICE_MIN_RESTART_TIME_BETWEEN)
8426 && r.nextRestartTime
8427 < (r2.nextRestartTime+SERVICE_MIN_RESTART_TIME_BETWEEN)) {
8428 r.nextRestartTime = r2.nextRestartTime + SERVICE_MIN_RESTART_TIME_BETWEEN;
8429 r.restartDelay = r.nextRestartTime - now;
8430 repeat = true;
8431 break;
8432 }
8433 }
8434 } while (repeat);
8435
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008436 if (!mRestartingServices.contains(r)) {
8437 mRestartingServices.add(r);
8438 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -07008439
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008440 r.cancelNotification();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008442 mHandler.removeCallbacks(r.restarter);
Dianne Hackbornfd12af42009-08-27 00:44:33 -07008443 mHandler.postAtTime(r.restarter, r.nextRestartTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 r.nextRestartTime = SystemClock.uptimeMillis() + r.restartDelay;
Joe Onorato8a9b2202010-02-26 18:56:32 -08008445 Slog.w(TAG, "Scheduling restart of crashed service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008446 + r.shortName + " in " + r.restartDelay + "ms");
Doug Zongker2bec3d42009-12-04 12:52:44 -08008447 EventLog.writeEvent(EventLogTags.AM_SCHEDULE_SERVICE_RESTART,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 r.shortName, r.restartDelay);
8449
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008450 return canceled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 }
8452
8453 final void performServiceRestartLocked(ServiceRecord r) {
8454 if (!mRestartingServices.contains(r)) {
8455 return;
8456 }
8457 bringUpServiceLocked(r, r.intent.getIntent().getFlags(), true);
8458 }
8459
8460 private final boolean unscheduleServiceRestartLocked(ServiceRecord r) {
8461 if (r.restartDelay == 0) {
8462 return false;
8463 }
8464 r.resetRestartCounter();
8465 mRestartingServices.remove(r);
8466 mHandler.removeCallbacks(r.restarter);
8467 return true;
8468 }
8469
8470 private final boolean bringUpServiceLocked(ServiceRecord r,
8471 int intentFlags, boolean whileRestarting) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008472 //Slog.i(TAG, "Bring up service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008473 //r.dump(" ");
8474
Dianne Hackborn36124872009-10-08 16:22:03 -07008475 if (r.app != null && r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 sendServiceArgsLocked(r, false);
8477 return true;
8478 }
8479
8480 if (!whileRestarting && r.restartDelay > 0) {
8481 // If waiting for a restart, then do nothing.
8482 return true;
8483 }
8484
Joe Onorato8a9b2202010-02-26 18:56:32 -08008485 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing up service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008486 + " " + r.intent);
8487
Dianne Hackbornde42bb62009-08-05 12:26:15 -07008488 // We are now bringing the service up, so no longer in the
8489 // restarting state.
8490 mRestartingServices.remove(r);
8491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 final String appName = r.processName;
8493 ProcessRecord app = getProcessRecordLocked(appName, r.appInfo.uid);
8494 if (app != null && app.thread != null) {
8495 try {
8496 realStartServiceLocked(r, app);
8497 return true;
8498 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008499 Slog.w(TAG, "Exception when starting service " + r.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008500 }
8501
8502 // If a dead object exception was thrown -- fall through to
8503 // restart the application.
8504 }
8505
Dianne Hackborn36124872009-10-08 16:22:03 -07008506 // Not running -- get it started, and enqueue this service record
8507 // to be executed when the app comes up.
8508 if (startProcessLocked(appName, r.appInfo, true, intentFlags,
8509 "service", r.name, false) == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008510 Slog.w(TAG, "Unable to launch app "
Dianne Hackborn36124872009-10-08 16:22:03 -07008511 + r.appInfo.packageName + "/"
8512 + r.appInfo.uid + " for service "
8513 + r.intent.getIntent() + ": process is bad");
8514 bringDownServiceLocked(r, true);
8515 return false;
8516 }
8517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008518 if (!mPendingServices.contains(r)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 mPendingServices.add(r);
8520 }
Dianne Hackborn36124872009-10-08 16:22:03 -07008521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008522 return true;
8523 }
8524
8525 private final void bringDownServiceLocked(ServiceRecord r, boolean force) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008526 //Slog.i(TAG, "Bring down service:");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008527 //r.dump(" ");
8528
8529 // Does it still need to run?
8530 if (!force && r.startRequested) {
8531 return;
8532 }
8533 if (r.connections.size() > 0) {
8534 if (!force) {
8535 // XXX should probably keep a count of the number of auto-create
8536 // connections directly in the service.
8537 Iterator<ConnectionRecord> it = r.connections.values().iterator();
8538 while (it.hasNext()) {
8539 ConnectionRecord cr = it.next();
8540 if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) {
8541 return;
8542 }
8543 }
8544 }
8545
8546 // Report to all of the connections that the service is no longer
8547 // available.
8548 Iterator<ConnectionRecord> it = r.connections.values().iterator();
8549 while (it.hasNext()) {
8550 ConnectionRecord c = it.next();
8551 try {
8552 // todo: shouldn't be a synchronous call!
8553 c.conn.connected(r.name, null);
8554 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008555 Slog.w(TAG, "Failure disconnecting service " + r.name +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008556 " to connection " + c.conn.asBinder() +
8557 " (in " + c.binding.client.processName + ")", e);
8558 }
8559 }
8560 }
8561
8562 // Tell the service that it has been unbound.
8563 if (r.bindings.size() > 0 && r.app != null && r.app.thread != null) {
8564 Iterator<IntentBindRecord> it = r.bindings.values().iterator();
8565 while (it.hasNext()) {
8566 IntentBindRecord ibr = it.next();
Joe Onorato8a9b2202010-02-26 18:56:32 -08008567 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down binding " + ibr
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008568 + ": hasBound=" + ibr.hasBound);
8569 if (r.app != null && r.app.thread != null && ibr.hasBound) {
8570 try {
8571 bumpServiceExecutingLocked(r);
8572 updateOomAdjLocked(r.app);
8573 ibr.hasBound = false;
8574 r.app.thread.scheduleUnbindService(r,
8575 ibr.intent.getIntent());
8576 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008577 Slog.w(TAG, "Exception when unbinding service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 + r.shortName, e);
8579 serviceDoneExecutingLocked(r, true);
8580 }
8581 }
8582 }
8583 }
8584
Joe Onorato8a9b2202010-02-26 18:56:32 -08008585 if (DEBUG_SERVICE) Slog.v(TAG, "Bringing down service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008586 + " " + r.intent);
Doug Zongker2bec3d42009-12-04 12:52:44 -08008587 EventLog.writeEvent(EventLogTags.AM_DESTROY_SERVICE,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008588 System.identityHashCode(r), r.shortName,
8589 (r.app != null) ? r.app.pid : -1);
8590
8591 mServices.remove(r.name);
8592 mServicesByIntent.remove(r.intent);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008593 if (localLOGV) Slog.v(TAG, "BRING DOWN SERVICE: " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 r.totalRestartCount = 0;
8595 unscheduleServiceRestartLocked(r);
8596
8597 // Also make sure it is not on the pending list.
8598 int N = mPendingServices.size();
8599 for (int i=0; i<N; i++) {
8600 if (mPendingServices.get(i) == r) {
8601 mPendingServices.remove(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -08008602 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 TAG, "Removed pending service: " + r.shortName);
8604 i--;
8605 N--;
8606 }
8607 }
8608
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008609 r.cancelNotification();
8610 r.isForeground = false;
8611 r.foregroundId = 0;
8612 r.foregroundNoti = null;
8613
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008614 // Clear start entries.
Dianne Hackborn39792d22010-08-19 18:01:52 -07008615 r.clearDeliveredStartsLocked();
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008616 r.pendingStarts.clear();
8617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008618 if (r.app != null) {
8619 synchronized (r.stats.getBatteryStats()) {
8620 r.stats.stopLaunchedLocked();
8621 }
8622 r.app.services.remove(r);
8623 if (r.app.thread != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008624 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008625 if (DEBUG_SERVICE) Slog.v(TAG,
Dianne Hackborna1e989b2009-09-01 19:54:29 -07008626 "Stopping service: " + r.shortName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008627 bumpServiceExecutingLocked(r);
8628 mStoppingServices.add(r);
8629 updateOomAdjLocked(r.app);
8630 r.app.thread.scheduleStopService(r);
8631 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008632 Slog.w(TAG, "Exception when stopping service "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 + r.shortName, e);
8634 serviceDoneExecutingLocked(r, true);
8635 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008636 updateServiceForegroundLocked(r.app, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008638 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008639 TAG, "Removed service that has no process: " + r.shortName);
8640 }
8641 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008642 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008643 TAG, "Removed service that is not running: " + r.shortName);
8644 }
8645 }
8646
8647 ComponentName startServiceLocked(IApplicationThread caller,
8648 Intent service, String resolvedType,
8649 int callingPid, int callingUid) {
8650 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008651 if (DEBUG_SERVICE) Slog.v(TAG, "startService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008652 + " type=" + resolvedType + " args=" + service.getExtras());
8653
8654 if (caller != null) {
8655 final ProcessRecord callerApp = getRecordForAppLocked(caller);
8656 if (callerApp == null) {
8657 throw new SecurityException(
8658 "Unable to find app for caller " + caller
8659 + " (pid=" + Binder.getCallingPid()
8660 + ") when starting service " + service);
8661 }
8662 }
8663
8664 ServiceLookupResult res =
8665 retrieveServiceLocked(service, resolvedType,
8666 callingPid, callingUid);
8667 if (res == null) {
8668 return null;
8669 }
8670 if (res.record == null) {
8671 return new ComponentName("!", res.permission != null
8672 ? res.permission : "private to package");
8673 }
8674 ServiceRecord r = res.record;
Dianne Hackborn39792d22010-08-19 18:01:52 -07008675 int targetPermissionUid = checkGrantUriPermissionFromIntentLocked(
8676 callingUid, r.packageName, service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008677 if (unscheduleServiceRestartLocked(r)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008678 if (DEBUG_SERVICE) Slog.v(TAG, "START SERVICE WHILE RESTART PENDING: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 + r.shortName);
8680 }
8681 r.startRequested = true;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008682 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008683 r.lastStartId++;
8684 if (r.lastStartId < 1) {
8685 r.lastStartId = 1;
8686 }
Dianne Hackborn39792d22010-08-19 18:01:52 -07008687 r.pendingStarts.add(new ServiceRecord.StartItem(r, r.lastStartId,
8688 service, targetPermissionUid));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008689 r.lastActivity = SystemClock.uptimeMillis();
8690 synchronized (r.stats.getBatteryStats()) {
8691 r.stats.startRunningLocked();
8692 }
8693 if (!bringUpServiceLocked(r, service.getFlags(), false)) {
8694 return new ComponentName("!", "Service process is bad");
8695 }
8696 return r.name;
8697 }
8698 }
8699
8700 public ComponentName startService(IApplicationThread caller, Intent service,
8701 String resolvedType) {
8702 // Refuse possible leaked file descriptors
8703 if (service != null && service.hasFileDescriptors() == true) {
8704 throw new IllegalArgumentException("File descriptors passed in Intent");
8705 }
8706
8707 synchronized(this) {
8708 final int callingPid = Binder.getCallingPid();
8709 final int callingUid = Binder.getCallingUid();
8710 final long origId = Binder.clearCallingIdentity();
8711 ComponentName res = startServiceLocked(caller, service,
8712 resolvedType, callingPid, callingUid);
8713 Binder.restoreCallingIdentity(origId);
8714 return res;
8715 }
8716 }
8717
8718 ComponentName startServiceInPackage(int uid,
8719 Intent service, String resolvedType) {
8720 synchronized(this) {
8721 final long origId = Binder.clearCallingIdentity();
8722 ComponentName res = startServiceLocked(null, service,
8723 resolvedType, -1, uid);
8724 Binder.restoreCallingIdentity(origId);
8725 return res;
8726 }
8727 }
8728
8729 public int stopService(IApplicationThread caller, Intent service,
8730 String resolvedType) {
8731 // Refuse possible leaked file descriptors
8732 if (service != null && service.hasFileDescriptors() == true) {
8733 throw new IllegalArgumentException("File descriptors passed in Intent");
8734 }
8735
8736 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008737 if (DEBUG_SERVICE) Slog.v(TAG, "stopService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008738 + " type=" + resolvedType);
8739
8740 final ProcessRecord callerApp = getRecordForAppLocked(caller);
8741 if (caller != null && callerApp == null) {
8742 throw new SecurityException(
8743 "Unable to find app for caller " + caller
8744 + " (pid=" + Binder.getCallingPid()
8745 + ") when stopping service " + service);
8746 }
8747
8748 // If this service is active, make sure it is stopped.
8749 ServiceLookupResult r = findServiceLocked(service, resolvedType);
8750 if (r != null) {
8751 if (r.record != null) {
8752 synchronized (r.record.stats.getBatteryStats()) {
8753 r.record.stats.stopRunningLocked();
8754 }
8755 r.record.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008756 r.record.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757 final long origId = Binder.clearCallingIdentity();
8758 bringDownServiceLocked(r.record, false);
8759 Binder.restoreCallingIdentity(origId);
8760 return 1;
8761 }
8762 return -1;
8763 }
8764 }
8765
8766 return 0;
8767 }
8768
8769 public IBinder peekService(Intent service, String resolvedType) {
8770 // Refuse possible leaked file descriptors
8771 if (service != null && service.hasFileDescriptors() == true) {
8772 throw new IllegalArgumentException("File descriptors passed in Intent");
8773 }
8774
8775 IBinder ret = null;
8776
8777 synchronized(this) {
8778 ServiceLookupResult r = findServiceLocked(service, resolvedType);
8779
8780 if (r != null) {
8781 // r.record is null if findServiceLocked() failed the caller permission check
8782 if (r.record == null) {
8783 throw new SecurityException(
8784 "Permission Denial: Accessing service " + r.record.name
8785 + " from pid=" + Binder.getCallingPid()
8786 + ", uid=" + Binder.getCallingUid()
8787 + " requires " + r.permission);
8788 }
8789 IntentBindRecord ib = r.record.bindings.get(r.record.intent);
8790 if (ib != null) {
8791 ret = ib.binder;
8792 }
8793 }
8794 }
8795
8796 return ret;
8797 }
8798
8799 public boolean stopServiceToken(ComponentName className, IBinder token,
8800 int startId) {
8801 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008802 if (DEBUG_SERVICE) Slog.v(TAG, "stopServiceToken: " + className
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 + " " + token + " startId=" + startId);
8804 ServiceRecord r = findServiceLocked(className, token);
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008805 if (r != null) {
8806 if (startId >= 0) {
8807 // Asked to only stop if done with all work. Note that
8808 // to avoid leaks, we will take this as dropping all
8809 // start items up to and including this one.
8810 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
8811 if (si != null) {
8812 while (r.deliveredStarts.size() > 0) {
Dianne Hackborn39792d22010-08-19 18:01:52 -07008813 ServiceRecord.StartItem cur = r.deliveredStarts.remove(0);
8814 cur.removeUriPermissionsLocked();
8815 if (cur == si) {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008816 break;
8817 }
8818 }
8819 }
8820
8821 if (r.lastStartId != startId) {
8822 return false;
8823 }
8824
8825 if (r.deliveredStarts.size() > 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008826 Slog.w(TAG, "stopServiceToken startId " + startId
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008827 + " is last, but have " + r.deliveredStarts.size()
8828 + " remaining args");
8829 }
8830 }
8831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 synchronized (r.stats.getBatteryStats()) {
8833 r.stats.stopRunningLocked();
8834 r.startRequested = false;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07008835 r.callStart = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 }
8837 final long origId = Binder.clearCallingIdentity();
8838 bringDownServiceLocked(r, false);
8839 Binder.restoreCallingIdentity(origId);
8840 return true;
8841 }
8842 }
8843 return false;
8844 }
8845
8846 public void setServiceForeground(ComponentName className, IBinder token,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008847 int id, Notification notification, boolean removeNotification) {
8848 final long origId = Binder.clearCallingIdentity();
8849 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 synchronized(this) {
8851 ServiceRecord r = findServiceLocked(className, token);
8852 if (r != null) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008853 if (id != 0) {
8854 if (notification == null) {
8855 throw new IllegalArgumentException("null notification");
8856 }
8857 if (r.foregroundId != id) {
8858 r.cancelNotification();
8859 r.foregroundId = id;
8860 }
8861 notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
8862 r.foregroundNoti = notification;
8863 r.isForeground = true;
8864 r.postNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 if (r.app != null) {
8866 updateServiceForegroundLocked(r.app, true);
8867 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008868 } else {
8869 if (r.isForeground) {
8870 r.isForeground = false;
8871 if (r.app != null) {
Dianne Hackborn8633e682010-04-22 16:03:41 -07008872 updateLruProcessLocked(r.app, false, true);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008873 updateServiceForegroundLocked(r.app, true);
8874 }
8875 }
8876 if (removeNotification) {
8877 r.cancelNotification();
8878 r.foregroundId = 0;
8879 r.foregroundNoti = null;
8880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008881 }
8882 }
8883 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07008884 } finally {
8885 Binder.restoreCallingIdentity(origId);
8886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008887 }
8888
8889 public void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) {
8890 boolean anyForeground = false;
Dianne Hackborn860755f2010-06-03 18:47:52 -07008891 for (ServiceRecord sr : proc.services) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008892 if (sr.isForeground) {
8893 anyForeground = true;
8894 break;
8895 }
8896 }
8897 if (anyForeground != proc.foregroundServices) {
8898 proc.foregroundServices = anyForeground;
8899 if (oomAdj) {
8900 updateOomAdjLocked();
8901 }
8902 }
8903 }
8904
8905 public int bindService(IApplicationThread caller, IBinder token,
8906 Intent service, String resolvedType,
8907 IServiceConnection connection, int flags) {
8908 // Refuse possible leaked file descriptors
8909 if (service != null && service.hasFileDescriptors() == true) {
8910 throw new IllegalArgumentException("File descriptors passed in Intent");
8911 }
8912
8913 synchronized(this) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008914 if (DEBUG_SERVICE) Slog.v(TAG, "bindService: " + service
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008915 + " type=" + resolvedType + " conn=" + connection.asBinder()
8916 + " flags=0x" + Integer.toHexString(flags));
8917 final ProcessRecord callerApp = getRecordForAppLocked(caller);
8918 if (callerApp == null) {
8919 throw new SecurityException(
8920 "Unable to find app for caller " + caller
8921 + " (pid=" + Binder.getCallingPid()
8922 + ") when binding service " + service);
8923 }
8924
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07008925 ActivityRecord activity = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 if (token != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008927 int aindex = mMainStack.indexOfTokenLocked(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 if (aindex < 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008929 Slog.w(TAG, "Binding with unknown activity: " + token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008930 return 0;
8931 }
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07008932 activity = (ActivityRecord)mMainStack.mHistory.get(aindex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008933 }
8934
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008935 int clientLabel = 0;
8936 PendingIntent clientIntent = null;
8937
8938 if (callerApp.info.uid == Process.SYSTEM_UID) {
8939 // Hacky kind of thing -- allow system stuff to tell us
8940 // what they are, so we can report this elsewhere for
8941 // others to know why certain services are running.
8942 try {
8943 clientIntent = (PendingIntent)service.getParcelableExtra(
8944 Intent.EXTRA_CLIENT_INTENT);
8945 } catch (RuntimeException e) {
8946 }
8947 if (clientIntent != null) {
8948 clientLabel = service.getIntExtra(Intent.EXTRA_CLIENT_LABEL, 0);
8949 if (clientLabel != 0) {
8950 // There are no useful extras in the intent, trash them.
8951 // System code calling with this stuff just needs to know
8952 // this will happen.
8953 service = service.cloneFilter();
8954 }
8955 }
8956 }
8957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008958 ServiceLookupResult res =
8959 retrieveServiceLocked(service, resolvedType,
8960 Binder.getCallingPid(), Binder.getCallingUid());
8961 if (res == null) {
8962 return 0;
8963 }
8964 if (res.record == null) {
8965 return -1;
8966 }
8967 ServiceRecord s = res.record;
8968
8969 final long origId = Binder.clearCallingIdentity();
8970
8971 if (unscheduleServiceRestartLocked(s)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08008972 if (DEBUG_SERVICE) Slog.v(TAG, "BIND SERVICE WHILE RESTART PENDING: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 + s.shortName);
8974 }
8975
8976 AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
8977 ConnectionRecord c = new ConnectionRecord(b, activity,
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07008978 connection, flags, clientLabel, clientIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008979
8980 IBinder binder = connection.asBinder();
8981 s.connections.put(binder, c);
8982 b.connections.add(c);
8983 if (activity != null) {
8984 if (activity.connections == null) {
8985 activity.connections = new HashSet<ConnectionRecord>();
8986 }
8987 activity.connections.add(c);
8988 }
8989 b.client.connections.add(c);
8990 mServiceConnections.put(binder, c);
8991
8992 if ((flags&Context.BIND_AUTO_CREATE) != 0) {
8993 s.lastActivity = SystemClock.uptimeMillis();
8994 if (!bringUpServiceLocked(s, service.getFlags(), false)) {
8995 return 0;
8996 }
8997 }
8998
8999 if (s.app != null) {
9000 // This could have made the service more important.
9001 updateOomAdjLocked(s.app);
9002 }
9003
Joe Onorato8a9b2202010-02-26 18:56:32 -08009004 if (DEBUG_SERVICE) Slog.v(TAG, "Bind " + s + " with " + b
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 + ": received=" + b.intent.received
9006 + " apps=" + b.intent.apps.size()
9007 + " doRebind=" + b.intent.doRebind);
9008
9009 if (s.app != null && b.intent.received) {
9010 // Service is already running, so we can immediately
9011 // publish the connection.
9012 try {
9013 c.conn.connected(s.name, b.intent.binder);
9014 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009015 Slog.w(TAG, "Failure sending service " + s.shortName
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016 + " to connection " + c.conn.asBinder()
9017 + " (in " + c.binding.client.processName + ")", e);
9018 }
9019
9020 // If this is the first app connected back to this binding,
9021 // and the service had previously asked to be told when
9022 // rebound, then do so.
9023 if (b.intent.apps.size() == 1 && b.intent.doRebind) {
9024 requestServiceBindingLocked(s, b.intent, true);
9025 }
9026 } else if (!b.intent.requested) {
9027 requestServiceBindingLocked(s, b.intent, false);
9028 }
9029
9030 Binder.restoreCallingIdentity(origId);
9031 }
9032
9033 return 1;
9034 }
9035
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -07009036 void removeConnectionLocked(
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009037 ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038 IBinder binder = c.conn.asBinder();
9039 AppBindRecord b = c.binding;
9040 ServiceRecord s = b.service;
9041 s.connections.remove(binder);
9042 b.connections.remove(c);
9043 if (c.activity != null && c.activity != skipAct) {
9044 if (c.activity.connections != null) {
9045 c.activity.connections.remove(c);
9046 }
9047 }
9048 if (b.client != skipApp) {
9049 b.client.connections.remove(c);
9050 }
9051 mServiceConnections.remove(binder);
9052
9053 if (b.connections.size() == 0) {
9054 b.intent.apps.remove(b.client);
9055 }
9056
Joe Onorato8a9b2202010-02-26 18:56:32 -08009057 if (DEBUG_SERVICE) Slog.v(TAG, "Disconnecting binding " + b.intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009058 + ": shouldUnbind=" + b.intent.hasBound);
9059 if (s.app != null && s.app.thread != null && b.intent.apps.size() == 0
9060 && b.intent.hasBound) {
9061 try {
9062 bumpServiceExecutingLocked(s);
9063 updateOomAdjLocked(s.app);
9064 b.intent.hasBound = false;
9065 // Assume the client doesn't want to know about a rebind;
9066 // we will deal with that later if it asks for one.
9067 b.intent.doRebind = false;
9068 s.app.thread.scheduleUnbindService(s, b.intent.intent.getIntent());
9069 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009070 Slog.w(TAG, "Exception when unbinding service " + s.shortName, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009071 serviceDoneExecutingLocked(s, true);
9072 }
9073 }
9074
9075 if ((c.flags&Context.BIND_AUTO_CREATE) != 0) {
9076 bringDownServiceLocked(s, false);
9077 }
9078 }
9079
9080 public boolean unbindService(IServiceConnection connection) {
9081 synchronized (this) {
9082 IBinder binder = connection.asBinder();
Joe Onorato8a9b2202010-02-26 18:56:32 -08009083 if (DEBUG_SERVICE) Slog.v(TAG, "unbindService: conn=" + binder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009084 ConnectionRecord r = mServiceConnections.get(binder);
9085 if (r == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009086 Slog.w(TAG, "Unbind failed: could not find connection for "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009087 + connection.asBinder());
9088 return false;
9089 }
9090
9091 final long origId = Binder.clearCallingIdentity();
9092
9093 removeConnectionLocked(r, null, null);
9094
9095 if (r.binding.service.app != null) {
9096 // This could have made the service less important.
9097 updateOomAdjLocked(r.binding.service.app);
9098 }
9099
9100 Binder.restoreCallingIdentity(origId);
9101 }
9102
9103 return true;
9104 }
9105
9106 public void publishService(IBinder token, Intent intent, IBinder service) {
9107 // Refuse possible leaked file descriptors
9108 if (intent != null && intent.hasFileDescriptors() == true) {
9109 throw new IllegalArgumentException("File descriptors passed in Intent");
9110 }
9111
9112 synchronized(this) {
9113 if (!(token instanceof ServiceRecord)) {
9114 throw new IllegalArgumentException("Invalid service token");
9115 }
9116 ServiceRecord r = (ServiceRecord)token;
9117
9118 final long origId = Binder.clearCallingIdentity();
9119
Joe Onorato8a9b2202010-02-26 18:56:32 -08009120 if (DEBUG_SERVICE) Slog.v(TAG, "PUBLISHING SERVICE " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009121 + " " + intent + ": " + service);
9122 if (r != null) {
9123 Intent.FilterComparison filter
9124 = new Intent.FilterComparison(intent);
9125 IntentBindRecord b = r.bindings.get(filter);
9126 if (b != null && !b.received) {
9127 b.binder = service;
9128 b.requested = true;
9129 b.received = true;
9130 if (r.connections.size() > 0) {
9131 Iterator<ConnectionRecord> it
9132 = r.connections.values().iterator();
9133 while (it.hasNext()) {
9134 ConnectionRecord c = it.next();
9135 if (!filter.equals(c.binding.intent.intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009136 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 TAG, "Not publishing to: " + c);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009138 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 TAG, "Bound intent: " + c.binding.intent.intent);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009140 if (DEBUG_SERVICE) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009141 TAG, "Published intent: " + intent);
9142 continue;
9143 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08009144 if (DEBUG_SERVICE) Slog.v(TAG, "Publishing to: " + c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 try {
9146 c.conn.connected(r.name, service);
9147 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009148 Slog.w(TAG, "Failure sending service " + r.name +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 " to connection " + c.conn.asBinder() +
9150 " (in " + c.binding.client.processName + ")", e);
9151 }
9152 }
9153 }
9154 }
9155
9156 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
9157
9158 Binder.restoreCallingIdentity(origId);
9159 }
9160 }
9161 }
9162
9163 public void unbindFinished(IBinder token, Intent intent, boolean doRebind) {
9164 // Refuse possible leaked file descriptors
9165 if (intent != null && intent.hasFileDescriptors() == true) {
9166 throw new IllegalArgumentException("File descriptors passed in Intent");
9167 }
9168
9169 synchronized(this) {
9170 if (!(token instanceof ServiceRecord)) {
9171 throw new IllegalArgumentException("Invalid service token");
9172 }
9173 ServiceRecord r = (ServiceRecord)token;
9174
9175 final long origId = Binder.clearCallingIdentity();
9176
9177 if (r != null) {
9178 Intent.FilterComparison filter
9179 = new Intent.FilterComparison(intent);
9180 IntentBindRecord b = r.bindings.get(filter);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009181 if (DEBUG_SERVICE) Slog.v(TAG, "unbindFinished in " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009182 + " at " + b + ": apps="
9183 + (b != null ? b.apps.size() : 0));
9184 if (b != null) {
9185 if (b.apps.size() > 0) {
9186 // Applications have already bound since the last
9187 // unbind, so just rebind right here.
9188 requestServiceBindingLocked(r, b, true);
9189 } else {
9190 // Note to tell the service the next time there is
9191 // a new client.
9192 b.doRebind = true;
9193 }
9194 }
9195
9196 serviceDoneExecutingLocked(r, mStoppingServices.contains(r));
9197
9198 Binder.restoreCallingIdentity(origId);
9199 }
9200 }
9201 }
9202
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009203 public void serviceDoneExecuting(IBinder token, int type, int startId, int res) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009204 synchronized(this) {
9205 if (!(token instanceof ServiceRecord)) {
9206 throw new IllegalArgumentException("Invalid service token");
9207 }
9208 ServiceRecord r = (ServiceRecord)token;
9209 boolean inStopping = mStoppingServices.contains(token);
9210 if (r != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009211 if (DEBUG_SERVICE) Slog.v(TAG, "DONE EXECUTING SERVICE " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009212 + ": nesting=" + r.executeNesting
9213 + ", inStopping=" + inStopping);
9214 if (r != token) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009215 Slog.w(TAG, "Done executing service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 + " with incorrect token: given " + token
9217 + ", expected " + r);
9218 return;
9219 }
9220
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07009221 if (type == 1) {
9222 // This is a call from a service start... take care of
9223 // book-keeping.
9224 r.callStart = true;
9225 switch (res) {
9226 case Service.START_STICKY_COMPATIBILITY:
9227 case Service.START_STICKY: {
9228 // We are done with the associated start arguments.
9229 r.findDeliveredStart(startId, true);
9230 // Don't stop if killed.
9231 r.stopIfKilled = false;
9232 break;
9233 }
9234 case Service.START_NOT_STICKY: {
9235 // We are done with the associated start arguments.
9236 r.findDeliveredStart(startId, true);
9237 if (r.lastStartId == startId) {
9238 // There is no more work, and this service
9239 // doesn't want to hang around if killed.
9240 r.stopIfKilled = true;
9241 }
9242 break;
9243 }
9244 case Service.START_REDELIVER_INTENT: {
9245 // We'll keep this item until they explicitly
9246 // call stop for it, but keep track of the fact
9247 // that it was delivered.
9248 ServiceRecord.StartItem si = r.findDeliveredStart(startId, false);
9249 if (si != null) {
9250 si.deliveryCount = 0;
9251 si.doneExecutingCount++;
9252 // Don't stop if killed.
9253 r.stopIfKilled = true;
9254 }
9255 break;
9256 }
9257 default:
9258 throw new IllegalArgumentException(
9259 "Unknown service start result: " + res);
9260 }
9261 if (res == Service.START_STICKY_COMPATIBILITY) {
9262 r.callStart = false;
9263 }
9264 }
9265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266 final long origId = Binder.clearCallingIdentity();
9267 serviceDoneExecutingLocked(r, inStopping);
9268 Binder.restoreCallingIdentity(origId);
9269 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009270 Slog.w(TAG, "Done executing unknown service " + r.name
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009271 + " with token " + token);
9272 }
9273 }
9274 }
9275
9276 public void serviceDoneExecutingLocked(ServiceRecord r, boolean inStopping) {
9277 r.executeNesting--;
9278 if (r.executeNesting <= 0 && r.app != null) {
9279 r.app.executingServices.remove(r);
9280 if (r.app.executingServices.size() == 0) {
9281 mHandler.removeMessages(SERVICE_TIMEOUT_MSG, r.app);
9282 }
9283 if (inStopping) {
9284 mStoppingServices.remove(r);
9285 }
9286 updateOomAdjLocked(r.app);
9287 }
9288 }
9289
9290 void serviceTimeout(ProcessRecord proc) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -07009291 String anrMessage = null;
9292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 synchronized(this) {
9294 if (proc.executingServices.size() == 0 || proc.thread == null) {
9295 return;
9296 }
9297 long maxTime = SystemClock.uptimeMillis() - SERVICE_TIMEOUT;
9298 Iterator<ServiceRecord> it = proc.executingServices.iterator();
9299 ServiceRecord timeout = null;
9300 long nextTime = 0;
9301 while (it.hasNext()) {
9302 ServiceRecord sr = it.next();
9303 if (sr.executingStart < maxTime) {
9304 timeout = sr;
9305 break;
9306 }
9307 if (sr.executingStart > nextTime) {
9308 nextTime = sr.executingStart;
9309 }
9310 }
Dianne Hackborndd71fc82009-12-16 19:24:32 -08009311 if (timeout != null && mLruProcesses.contains(proc)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009312 Slog.w(TAG, "Timeout executing service: " + timeout);
Dianne Hackbornad5499d2010-03-29 18:08:45 -07009313 anrMessage = "Executing service " + timeout.shortName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 } else {
9315 Message msg = mHandler.obtainMessage(SERVICE_TIMEOUT_MSG);
9316 msg.obj = proc;
9317 mHandler.sendMessageAtTime(msg, nextTime+SERVICE_TIMEOUT);
9318 }
9319 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -07009320
9321 if (anrMessage != null) {
9322 appNotResponding(proc, null, null, anrMessage);
9323 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009324 }
9325
9326 // =========================================================
Christopher Tate181fafa2009-05-14 11:12:14 -07009327 // BACKUP AND RESTORE
9328 // =========================================================
9329
9330 // Cause the target app to be launched if necessary and its backup agent
9331 // instantiated. The backup agent will invoke backupAgentCreated() on the
9332 // activity manager to announce its creation.
9333 public boolean bindBackupAgent(ApplicationInfo app, int backupMode) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009334 if (DEBUG_BACKUP) Slog.v(TAG, "startBackupAgent: app=" + app + " mode=" + backupMode);
Christopher Tate181fafa2009-05-14 11:12:14 -07009335 enforceCallingPermission("android.permission.BACKUP", "startBackupAgent");
9336
9337 synchronized(this) {
9338 // !!! TODO: currently no check here that we're already bound
9339 BatteryStatsImpl.Uid.Pkg.Serv ss = null;
9340 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
9341 synchronized (stats) {
9342 ss = stats.getServiceStatsLocked(app.uid, app.packageName, app.name);
9343 }
9344
9345 BackupRecord r = new BackupRecord(ss, app, backupMode);
9346 ComponentName hostingName = new ComponentName(app.packageName, app.backupAgentName);
9347 // startProcessLocked() returns existing proc's record if it's already running
9348 ProcessRecord proc = startProcessLocked(app.processName, app,
Dianne Hackborn9acc0302009-08-25 00:27:12 -07009349 false, 0, "backup", hostingName, false);
Christopher Tate181fafa2009-05-14 11:12:14 -07009350 if (proc == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009351 Slog.e(TAG, "Unable to start backup agent process " + r);
Christopher Tate181fafa2009-05-14 11:12:14 -07009352 return false;
9353 }
9354
9355 r.app = proc;
9356 mBackupTarget = r;
9357 mBackupAppName = app.packageName;
9358
Christopher Tate6fa95972009-06-05 18:43:55 -07009359 // Try not to kill the process during backup
9360 updateOomAdjLocked(proc);
9361
Christopher Tate181fafa2009-05-14 11:12:14 -07009362 // If the process is already attached, schedule the creation of the backup agent now.
9363 // If it is not yet live, this will be done when it attaches to the framework.
9364 if (proc.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009365 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc already running: " + proc);
Christopher Tate181fafa2009-05-14 11:12:14 -07009366 try {
9367 proc.thread.scheduleCreateBackupAgent(app, backupMode);
9368 } catch (RemoteException e) {
Christopher Tate436344a2009-09-30 16:17:37 -07009369 // Will time out on the backup manager side
Christopher Tate181fafa2009-05-14 11:12:14 -07009370 }
9371 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009372 if (DEBUG_BACKUP) Slog.v(TAG, "Agent proc not running, waiting for attach");
Christopher Tate181fafa2009-05-14 11:12:14 -07009373 }
9374 // Invariants: at this point, the target app process exists and the application
9375 // is either already running or in the process of coming up. mBackupTarget and
9376 // mBackupAppName describe the app, so that when it binds back to the AM we
9377 // know that it's scheduled for a backup-agent operation.
9378 }
9379
9380 return true;
9381 }
9382
9383 // A backup agent has just come up
9384 public void backupAgentCreated(String agentPackageName, IBinder agent) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009385 if (DEBUG_BACKUP) Slog.v(TAG, "backupAgentCreated: " + agentPackageName
Christopher Tate181fafa2009-05-14 11:12:14 -07009386 + " = " + agent);
9387
9388 synchronized(this) {
9389 if (!agentPackageName.equals(mBackupAppName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009390 Slog.e(TAG, "Backup agent created for " + agentPackageName + " but not requested!");
Christopher Tate181fafa2009-05-14 11:12:14 -07009391 return;
9392 }
9393
Christopher Tate043dadc2009-06-02 16:11:00 -07009394 long oldIdent = Binder.clearCallingIdentity();
Christopher Tate181fafa2009-05-14 11:12:14 -07009395 try {
9396 IBackupManager bm = IBackupManager.Stub.asInterface(
9397 ServiceManager.getService(Context.BACKUP_SERVICE));
9398 bm.agentConnected(agentPackageName, agent);
9399 } catch (RemoteException e) {
9400 // can't happen; the backup manager service is local
9401 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009402 Slog.w(TAG, "Exception trying to deliver BackupAgent binding: ");
Christopher Tate181fafa2009-05-14 11:12:14 -07009403 e.printStackTrace();
Christopher Tate043dadc2009-06-02 16:11:00 -07009404 } finally {
9405 Binder.restoreCallingIdentity(oldIdent);
Christopher Tate181fafa2009-05-14 11:12:14 -07009406 }
9407 }
9408 }
9409
9410 // done with this agent
9411 public void unbindBackupAgent(ApplicationInfo appInfo) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009412 if (DEBUG_BACKUP) Slog.v(TAG, "unbindBackupAgent: " + appInfo);
Christopher Tate8a27f922009-06-26 11:49:18 -07009413 if (appInfo == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009414 Slog.w(TAG, "unbind backup agent for null app");
Christopher Tate8a27f922009-06-26 11:49:18 -07009415 return;
9416 }
Christopher Tate181fafa2009-05-14 11:12:14 -07009417
9418 synchronized(this) {
Christopher Tate8a27f922009-06-26 11:49:18 -07009419 if (mBackupAppName == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009420 Slog.w(TAG, "Unbinding backup agent with no active backup");
Christopher Tate8a27f922009-06-26 11:49:18 -07009421 return;
9422 }
9423
Christopher Tate181fafa2009-05-14 11:12:14 -07009424 if (!mBackupAppName.equals(appInfo.packageName)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009425 Slog.e(TAG, "Unbind of " + appInfo + " but is not the current backup target");
Christopher Tate181fafa2009-05-14 11:12:14 -07009426 return;
9427 }
9428
Christopher Tate6fa95972009-06-05 18:43:55 -07009429 ProcessRecord proc = mBackupTarget.app;
9430 mBackupTarget = null;
9431 mBackupAppName = null;
9432
9433 // Not backing this app up any more; reset its OOM adjustment
9434 updateOomAdjLocked(proc);
9435
Christopher Tatec7b31e32009-06-10 15:49:30 -07009436 // If the app crashed during backup, 'thread' will be null here
9437 if (proc.thread != null) {
9438 try {
9439 proc.thread.scheduleDestroyBackupAgent(appInfo);
9440 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009441 Slog.e(TAG, "Exception when unbinding backup agent:");
Christopher Tatec7b31e32009-06-10 15:49:30 -07009442 e.printStackTrace();
9443 }
Christopher Tate181fafa2009-05-14 11:12:14 -07009444 }
Christopher Tate181fafa2009-05-14 11:12:14 -07009445 }
9446 }
9447 // =========================================================
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009448 // BROADCASTS
9449 // =========================================================
9450
Josh Bartel7f208742010-02-25 11:01:44 -06009451 private final List getStickiesLocked(String action, IntentFilter filter,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009452 List cur) {
9453 final ContentResolver resolver = mContext.getContentResolver();
9454 final ArrayList<Intent> list = mStickyBroadcasts.get(action);
9455 if (list == null) {
9456 return cur;
9457 }
9458 int N = list.size();
9459 for (int i=0; i<N; i++) {
9460 Intent intent = list.get(i);
9461 if (filter.match(resolver, intent, true, TAG) >= 0) {
9462 if (cur == null) {
9463 cur = new ArrayList<Intent>();
9464 }
9465 cur.add(intent);
9466 }
9467 }
9468 return cur;
9469 }
9470
9471 private final void scheduleBroadcastsLocked() {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009472 if (DEBUG_BROADCAST) Slog.v(TAG, "Schedule broadcasts: current="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 + mBroadcastsScheduled);
9474
9475 if (mBroadcastsScheduled) {
9476 return;
9477 }
9478 mHandler.sendEmptyMessage(BROADCAST_INTENT_MSG);
9479 mBroadcastsScheduled = true;
9480 }
9481
9482 public Intent registerReceiver(IApplicationThread caller,
9483 IIntentReceiver receiver, IntentFilter filter, String permission) {
9484 synchronized(this) {
9485 ProcessRecord callerApp = null;
9486 if (caller != null) {
9487 callerApp = getRecordForAppLocked(caller);
9488 if (callerApp == null) {
9489 throw new SecurityException(
9490 "Unable to find app for caller " + caller
9491 + " (pid=" + Binder.getCallingPid()
9492 + ") when registering receiver " + receiver);
9493 }
9494 }
9495
9496 List allSticky = null;
9497
9498 // Look for any matching sticky broadcasts...
9499 Iterator actions = filter.actionsIterator();
9500 if (actions != null) {
9501 while (actions.hasNext()) {
9502 String action = (String)actions.next();
Josh Bartel7f208742010-02-25 11:01:44 -06009503 allSticky = getStickiesLocked(action, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009504 }
9505 } else {
Josh Bartel7f208742010-02-25 11:01:44 -06009506 allSticky = getStickiesLocked(null, filter, allSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009507 }
9508
9509 // The first sticky in the list is returned directly back to
9510 // the client.
9511 Intent sticky = allSticky != null ? (Intent)allSticky.get(0) : null;
9512
Joe Onorato8a9b2202010-02-26 18:56:32 -08009513 if (DEBUG_BROADCAST) Slog.v(TAG, "Register receiver " + filter
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009514 + ": " + sticky);
9515
9516 if (receiver == null) {
9517 return sticky;
9518 }
9519
9520 ReceiverList rl
9521 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
9522 if (rl == null) {
9523 rl = new ReceiverList(this, callerApp,
9524 Binder.getCallingPid(),
9525 Binder.getCallingUid(), receiver);
9526 if (rl.app != null) {
9527 rl.app.receivers.add(rl);
9528 } else {
9529 try {
9530 receiver.asBinder().linkToDeath(rl, 0);
9531 } catch (RemoteException e) {
9532 return sticky;
9533 }
9534 rl.linkedToDeath = true;
9535 }
9536 mRegisteredReceivers.put(receiver.asBinder(), rl);
9537 }
9538 BroadcastFilter bf = new BroadcastFilter(filter, rl, permission);
9539 rl.add(bf);
9540 if (!bf.debugCheck()) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009541 Slog.w(TAG, "==> For Dynamic broadast");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009542 }
9543 mReceiverResolver.addFilter(bf);
9544
9545 // Enqueue broadcasts for all existing stickies that match
9546 // this filter.
9547 if (allSticky != null) {
9548 ArrayList receivers = new ArrayList();
9549 receivers.add(bf);
9550
9551 int N = allSticky.size();
9552 for (int i=0; i<N; i++) {
9553 Intent intent = (Intent)allSticky.get(i);
9554 BroadcastRecord r = new BroadcastRecord(intent, null,
9555 null, -1, -1, null, receivers, null, 0, null, null,
Dianne Hackborn12527f92009-11-11 17:39:50 -08009556 false, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 if (mParallelBroadcasts.size() == 0) {
9558 scheduleBroadcastsLocked();
9559 }
9560 mParallelBroadcasts.add(r);
9561 }
9562 }
9563
9564 return sticky;
9565 }
9566 }
9567
9568 public void unregisterReceiver(IIntentReceiver receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009569 if (DEBUG_BROADCAST) Slog.v(TAG, "Unregister receiver: " + receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570
9571 boolean doNext = false;
9572
9573 synchronized(this) {
9574 ReceiverList rl
9575 = (ReceiverList)mRegisteredReceivers.get(receiver.asBinder());
9576 if (rl != null) {
9577 if (rl.curBroadcast != null) {
9578 BroadcastRecord r = rl.curBroadcast;
9579 doNext = finishReceiverLocked(
9580 receiver.asBinder(), r.resultCode, r.resultData,
9581 r.resultExtras, r.resultAbort, true);
9582 }
9583
9584 if (rl.app != null) {
9585 rl.app.receivers.remove(rl);
9586 }
9587 removeReceiverLocked(rl);
9588 if (rl.linkedToDeath) {
9589 rl.linkedToDeath = false;
9590 rl.receiver.asBinder().unlinkToDeath(rl, 0);
9591 }
9592 }
9593 }
9594
9595 if (!doNext) {
9596 return;
9597 }
9598
9599 final long origId = Binder.clearCallingIdentity();
9600 processNextBroadcast(false);
9601 trimApplications();
9602 Binder.restoreCallingIdentity(origId);
9603 }
9604
9605 void removeReceiverLocked(ReceiverList rl) {
9606 mRegisteredReceivers.remove(rl.receiver.asBinder());
9607 int N = rl.size();
9608 for (int i=0; i<N; i++) {
9609 mReceiverResolver.removeFilter(rl.get(i));
9610 }
9611 }
9612
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07009613 private final void sendPackageBroadcastLocked(int cmd, String[] packages) {
9614 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
9615 ProcessRecord r = mLruProcesses.get(i);
9616 if (r.thread != null) {
9617 try {
9618 r.thread.dispatchPackageBroadcast(cmd, packages);
9619 } catch (RemoteException ex) {
9620 }
9621 }
9622 }
9623 }
9624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009625 private final int broadcastIntentLocked(ProcessRecord callerApp,
9626 String callerPackage, Intent intent, String resolvedType,
9627 IIntentReceiver resultTo, int resultCode, String resultData,
9628 Bundle map, String requiredPermission,
9629 boolean ordered, boolean sticky, int callingPid, int callingUid) {
9630 intent = new Intent(intent);
9631
Joe Onorato8a9b2202010-02-26 18:56:32 -08009632 if (DEBUG_BROADCAST_LIGHT) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009633 TAG, (sticky ? "Broadcast sticky: ": "Broadcast: ") + intent
9634 + " ordered=" + ordered);
9635 if ((resultTo != null) && !ordered) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009636 Slog.w(TAG, "Broadcast " + intent + " not ordered but result callback requested!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009637 }
9638
9639 // Handle special intents: if this broadcast is from the package
9640 // manager about a package being removed, we need to remove all of
9641 // its activities from the history stack.
9642 final boolean uidRemoved = intent.ACTION_UID_REMOVED.equals(
9643 intent.getAction());
9644 if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())
9645 || intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08009646 || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 || uidRemoved) {
9648 if (checkComponentPermission(
9649 android.Manifest.permission.BROADCAST_PACKAGE_REMOVED,
9650 callingPid, callingUid, -1)
9651 == PackageManager.PERMISSION_GRANTED) {
9652 if (uidRemoved) {
9653 final Bundle intentExtras = intent.getExtras();
9654 final int uid = intentExtras != null
9655 ? intentExtras.getInt(Intent.EXTRA_UID) : -1;
9656 if (uid >= 0) {
9657 BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
9658 synchronized (bs) {
9659 bs.removeUidStatsLocked(uid);
9660 }
9661 }
9662 } else {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009663 // If resources are unvailble just force stop all
9664 // those packages and flush the attribute cache as well.
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08009665 if (Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009666 String list[] = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
9667 if (list != null && (list.length > 0)) {
9668 for (String pkg : list) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08009669 forceStopPackageLocked(pkg, -1, false, true, true);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009670 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07009671 sendPackageBroadcastLocked(
9672 IApplicationThread.EXTERNAL_STORAGE_UNAVAILABLE, list);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009673 }
9674 } else {
9675 Uri data = intent.getData();
9676 String ssp;
9677 if (data != null && (ssp=data.getSchemeSpecificPart()) != null) {
9678 if (!intent.getBooleanExtra(Intent.EXTRA_DONT_KILL_APP, false)) {
9679 forceStopPackageLocked(ssp,
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08009680 intent.getIntExtra(Intent.EXTRA_UID, -1), false, true, true);
Dianne Hackbornde7faf62009-06-30 13:27:30 -07009681 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07009682 if (intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
9683 sendPackageBroadcastLocked(IApplicationThread.PACKAGE_REMOVED,
9684 new String[] {ssp});
9685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009686 }
9687 }
9688 }
9689 } else {
9690 String msg = "Permission Denial: " + intent.getAction()
9691 + " broadcast from " + callerPackage + " (pid=" + callingPid
9692 + ", uid=" + callingUid + ")"
9693 + " requires "
9694 + android.Manifest.permission.BROADCAST_PACKAGE_REMOVED;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009695 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 throw new SecurityException(msg);
9697 }
9698 }
9699
9700 /*
9701 * If this is the time zone changed action, queue up a message that will reset the timezone
9702 * of all currently running processes. This message will get queued up before the broadcast
9703 * happens.
9704 */
9705 if (intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
9706 mHandler.sendEmptyMessage(UPDATE_TIME_ZONE);
9707 }
9708
Dianne Hackborn854060af2009-07-09 18:14:31 -07009709 /*
9710 * Prevent non-system code (defined here to be non-persistent
9711 * processes) from sending protected broadcasts.
9712 */
9713 if (callingUid == Process.SYSTEM_UID || callingUid == Process.PHONE_UID
9714 || callingUid == Process.SHELL_UID || callingUid == 0) {
9715 // Always okay.
9716 } else if (callerApp == null || !callerApp.persistent) {
9717 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009718 if (AppGlobals.getPackageManager().isProtectedBroadcast(
Dianne Hackborn854060af2009-07-09 18:14:31 -07009719 intent.getAction())) {
9720 String msg = "Permission Denial: not allowed to send broadcast "
9721 + intent.getAction() + " from pid="
9722 + callingPid + ", uid=" + callingUid;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009723 Slog.w(TAG, msg);
Dianne Hackborn854060af2009-07-09 18:14:31 -07009724 throw new SecurityException(msg);
9725 }
9726 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009727 Slog.w(TAG, "Remote exception", e);
Dianne Hackborn854060af2009-07-09 18:14:31 -07009728 return BROADCAST_SUCCESS;
9729 }
9730 }
9731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 // Add to the sticky list if requested.
9733 if (sticky) {
9734 if (checkPermission(android.Manifest.permission.BROADCAST_STICKY,
9735 callingPid, callingUid)
9736 != PackageManager.PERMISSION_GRANTED) {
9737 String msg = "Permission Denial: broadcastIntent() requesting a sticky broadcast from pid="
9738 + callingPid + ", uid=" + callingUid
9739 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -08009740 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009741 throw new SecurityException(msg);
9742 }
9743 if (requiredPermission != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009744 Slog.w(TAG, "Can't broadcast sticky intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 + " and enforce permission " + requiredPermission);
9746 return BROADCAST_STICKY_CANT_HAVE_PERMISSION;
9747 }
9748 if (intent.getComponent() != null) {
9749 throw new SecurityException(
9750 "Sticky broadcasts can't target a specific component");
9751 }
9752 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
9753 if (list == null) {
9754 list = new ArrayList<Intent>();
9755 mStickyBroadcasts.put(intent.getAction(), list);
9756 }
9757 int N = list.size();
9758 int i;
9759 for (i=0; i<N; i++) {
9760 if (intent.filterEquals(list.get(i))) {
9761 // This sticky already exists, replace it.
9762 list.set(i, new Intent(intent));
9763 break;
9764 }
9765 }
9766 if (i >= N) {
9767 list.add(new Intent(intent));
9768 }
9769 }
9770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009771 // Figure out who all will receive this broadcast.
9772 List receivers = null;
9773 List<BroadcastFilter> registeredReceivers = null;
9774 try {
9775 if (intent.getComponent() != null) {
9776 // Broadcast is going to one specific receiver class...
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009777 ActivityInfo ai = AppGlobals.getPackageManager().
Dianne Hackborn1655be42009-05-08 14:29:01 -07009778 getReceiverInfo(intent.getComponent(), STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 if (ai != null) {
9780 receivers = new ArrayList();
9781 ResolveInfo ri = new ResolveInfo();
9782 ri.activityInfo = ai;
9783 receivers.add(ri);
9784 }
9785 } else {
9786 // Need to resolve the intent to interested receivers...
9787 if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
9788 == 0) {
9789 receivers =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07009790 AppGlobals.getPackageManager().queryIntentReceivers(
Dianne Hackborn1655be42009-05-08 14:29:01 -07009791 intent, resolvedType, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009792 }
Mihai Preda074edef2009-05-18 17:13:31 +02009793 registeredReceivers = mReceiverResolver.queryIntent(intent, resolvedType, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009794 }
9795 } catch (RemoteException ex) {
9796 // pm is in same process, this will never happen.
9797 }
9798
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009799 final boolean replacePending =
9800 (intent.getFlags()&Intent.FLAG_RECEIVER_REPLACE_PENDING) != 0;
9801
Joe Onorato8a9b2202010-02-26 18:56:32 -08009802 if (DEBUG_BROADCAST) Slog.v(TAG, "Enqueing broadcast: " + intent.getAction()
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009803 + " replacePending=" + replacePending);
9804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009805 int NR = registeredReceivers != null ? registeredReceivers.size() : 0;
9806 if (!ordered && NR > 0) {
9807 // If we are not serializing this broadcast, then send the
9808 // registered receivers separately so they don't wait for the
9809 // components to be launched.
9810 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
9811 callerPackage, callingPid, callingUid, requiredPermission,
9812 registeredReceivers, resultTo, resultCode, resultData, map,
Dianne Hackborn12527f92009-11-11 17:39:50 -08009813 ordered, sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009814 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009815 TAG, "Enqueueing parallel broadcast " + r
9816 + ": prev had " + mParallelBroadcasts.size());
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009817 boolean replaced = false;
9818 if (replacePending) {
9819 for (int i=mParallelBroadcasts.size()-1; i>=0; i--) {
9820 if (intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009821 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009822 "***** DROPPING PARALLEL: " + intent);
9823 mParallelBroadcasts.set(i, r);
9824 replaced = true;
9825 break;
9826 }
9827 }
9828 }
9829 if (!replaced) {
9830 mParallelBroadcasts.add(r);
9831 scheduleBroadcastsLocked();
9832 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 registeredReceivers = null;
9834 NR = 0;
9835 }
9836
9837 // Merge into one list.
9838 int ir = 0;
9839 if (receivers != null) {
9840 // A special case for PACKAGE_ADDED: do not allow the package
9841 // being added to see this broadcast. This prevents them from
9842 // using this as a back door to get run as soon as they are
9843 // installed. Maybe in the future we want to have a special install
9844 // broadcast or such for apps, but we'd like to deliberately make
9845 // this decision.
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009846 String skipPackages[] = null;
9847 if (intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())
9848 || intent.ACTION_PACKAGE_RESTARTED.equals(intent.getAction())
9849 || intent.ACTION_PACKAGE_DATA_CLEARED.equals(intent.getAction())) {
9850 Uri data = intent.getData();
9851 if (data != null) {
9852 String pkgName = data.getSchemeSpecificPart();
9853 if (pkgName != null) {
9854 skipPackages = new String[] { pkgName };
9855 }
9856 }
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08009857 } else if (intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(intent.getAction())) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009858 skipPackages = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
The Android Open Source Project10592532009-03-18 17:39:46 -07009859 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08009860 if (skipPackages != null && (skipPackages.length > 0)) {
9861 for (String skipPackage : skipPackages) {
9862 if (skipPackage != null) {
9863 int NT = receivers.size();
9864 for (int it=0; it<NT; it++) {
9865 ResolveInfo curt = (ResolveInfo)receivers.get(it);
9866 if (curt.activityInfo.packageName.equals(skipPackage)) {
9867 receivers.remove(it);
9868 it--;
9869 NT--;
9870 }
9871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 }
9873 }
9874 }
9875
9876 int NT = receivers != null ? receivers.size() : 0;
9877 int it = 0;
9878 ResolveInfo curt = null;
9879 BroadcastFilter curr = null;
9880 while (it < NT && ir < NR) {
9881 if (curt == null) {
9882 curt = (ResolveInfo)receivers.get(it);
9883 }
9884 if (curr == null) {
9885 curr = registeredReceivers.get(ir);
9886 }
9887 if (curr.getPriority() >= curt.priority) {
9888 // Insert this broadcast record into the final list.
9889 receivers.add(it, curr);
9890 ir++;
9891 curr = null;
9892 it++;
9893 NT++;
9894 } else {
9895 // Skip to the next ResolveInfo in the final list.
9896 it++;
9897 curt = null;
9898 }
9899 }
9900 }
9901 while (ir < NR) {
9902 if (receivers == null) {
9903 receivers = new ArrayList();
9904 }
9905 receivers.add(registeredReceivers.get(ir));
9906 ir++;
9907 }
9908
9909 if ((receivers != null && receivers.size() > 0)
9910 || resultTo != null) {
9911 BroadcastRecord r = new BroadcastRecord(intent, callerApp,
9912 callerPackage, callingPid, callingUid, requiredPermission,
Dianne Hackborn12527f92009-11-11 17:39:50 -08009913 receivers, resultTo, resultCode, resultData, map, ordered,
9914 sticky, false);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009915 if (DEBUG_BROADCAST) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 TAG, "Enqueueing ordered broadcast " + r
9917 + ": prev had " + mOrderedBroadcasts.size());
9918 if (DEBUG_BROADCAST) {
9919 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -08009920 Slog.i(TAG, "Enqueueing broadcast " + r.intent.getAction() + " seq=" + seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921 }
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009922 boolean replaced = false;
9923 if (replacePending) {
9924 for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) {
9925 if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08009926 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn1c633fc2009-12-08 19:45:14 -08009927 "***** DROPPING ORDERED: " + intent);
9928 mOrderedBroadcasts.set(i, r);
9929 replaced = true;
9930 break;
9931 }
9932 }
9933 }
9934 if (!replaced) {
9935 mOrderedBroadcasts.add(r);
9936 scheduleBroadcastsLocked();
9937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 }
9939
9940 return BROADCAST_SUCCESS;
9941 }
9942
9943 public final int broadcastIntent(IApplicationThread caller,
9944 Intent intent, String resolvedType, IIntentReceiver resultTo,
9945 int resultCode, String resultData, Bundle map,
9946 String requiredPermission, boolean serialized, boolean sticky) {
9947 // Refuse possible leaked file descriptors
9948 if (intent != null && intent.hasFileDescriptors() == true) {
9949 throw new IllegalArgumentException("File descriptors passed in Intent");
9950 }
9951
9952 synchronized(this) {
Dianne Hackborn9acc0302009-08-25 00:27:12 -07009953 int flags = intent.getFlags();
9954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009955 if (!mSystemReady) {
9956 // if the caller really truly claims to know what they're doing, go
9957 // ahead and allow the broadcast without launching any receivers
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009958 if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT) != 0) {
9959 intent = new Intent(intent);
9960 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
9961 } else if ((flags&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0){
Joe Onorato8a9b2202010-02-26 18:56:32 -08009962 Slog.e(TAG, "Attempt to launch receivers of broadcast intent " + intent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 + " before boot completion");
9964 throw new IllegalStateException("Cannot broadcast before boot completed");
9965 }
9966 }
9967
Dianne Hackborn9acc0302009-08-25 00:27:12 -07009968 if ((flags&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
9969 throw new IllegalArgumentException(
9970 "Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
9971 }
9972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009973 final ProcessRecord callerApp = getRecordForAppLocked(caller);
9974 final int callingPid = Binder.getCallingPid();
9975 final int callingUid = Binder.getCallingUid();
9976 final long origId = Binder.clearCallingIdentity();
9977 int res = broadcastIntentLocked(callerApp,
9978 callerApp != null ? callerApp.info.packageName : null,
9979 intent, resolvedType, resultTo,
9980 resultCode, resultData, map, requiredPermission, serialized,
9981 sticky, callingPid, callingUid);
9982 Binder.restoreCallingIdentity(origId);
9983 return res;
9984 }
9985 }
9986
9987 int broadcastIntentInPackage(String packageName, int uid,
9988 Intent intent, String resolvedType, IIntentReceiver resultTo,
9989 int resultCode, String resultData, Bundle map,
9990 String requiredPermission, boolean serialized, boolean sticky) {
9991 synchronized(this) {
9992 final long origId = Binder.clearCallingIdentity();
9993 int res = broadcastIntentLocked(null, packageName, intent, resolvedType,
9994 resultTo, resultCode, resultData, map, requiredPermission,
9995 serialized, sticky, -1, uid);
9996 Binder.restoreCallingIdentity(origId);
9997 return res;
9998 }
9999 }
10000
10001 public final void unbroadcastIntent(IApplicationThread caller,
10002 Intent intent) {
10003 // Refuse possible leaked file descriptors
10004 if (intent != null && intent.hasFileDescriptors() == true) {
10005 throw new IllegalArgumentException("File descriptors passed in Intent");
10006 }
10007
10008 synchronized(this) {
10009 if (checkCallingPermission(android.Manifest.permission.BROADCAST_STICKY)
10010 != PackageManager.PERMISSION_GRANTED) {
10011 String msg = "Permission Denial: unbroadcastIntent() from pid="
10012 + Binder.getCallingPid()
10013 + ", uid=" + Binder.getCallingUid()
10014 + " requires " + android.Manifest.permission.BROADCAST_STICKY;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010015 Slog.w(TAG, msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 throw new SecurityException(msg);
10017 }
10018 ArrayList<Intent> list = mStickyBroadcasts.get(intent.getAction());
10019 if (list != null) {
10020 int N = list.size();
10021 int i;
10022 for (i=0; i<N; i++) {
10023 if (intent.filterEquals(list.get(i))) {
10024 list.remove(i);
10025 break;
10026 }
10027 }
10028 }
10029 }
10030 }
10031
10032 private final boolean finishReceiverLocked(IBinder receiver, int resultCode,
10033 String resultData, Bundle resultExtras, boolean resultAbort,
10034 boolean explicit) {
10035 if (mOrderedBroadcasts.size() == 0) {
10036 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010037 Slog.w(TAG, "finishReceiver called but no pending broadcasts");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010038 }
10039 return false;
10040 }
10041 BroadcastRecord r = mOrderedBroadcasts.get(0);
10042 if (r.receiver == null) {
10043 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010044 Slog.w(TAG, "finishReceiver called but none active");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 }
10046 return false;
10047 }
10048 if (r.receiver != receiver) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010049 Slog.w(TAG, "finishReceiver called but active receiver is different");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010050 return false;
10051 }
10052 int state = r.state;
10053 r.state = r.IDLE;
10054 if (state == r.IDLE) {
10055 if (explicit) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010056 Slog.w(TAG, "finishReceiver called but state is IDLE");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010057 }
10058 }
10059 r.receiver = null;
10060 r.intent.setComponent(null);
10061 if (r.curApp != null) {
10062 r.curApp.curReceiver = null;
10063 }
10064 if (r.curFilter != null) {
10065 r.curFilter.receiverList.curBroadcast = null;
10066 }
10067 r.curFilter = null;
10068 r.curApp = null;
10069 r.curComponent = null;
10070 r.curReceiver = null;
10071 mPendingBroadcast = null;
10072
10073 r.resultCode = resultCode;
10074 r.resultData = resultData;
10075 r.resultExtras = resultExtras;
10076 r.resultAbort = resultAbort;
10077
10078 // We will process the next receiver right now if this is finishing
10079 // an app receiver (which is always asynchronous) or after we have
10080 // come back from calling a receiver.
10081 return state == BroadcastRecord.APP_RECEIVE
10082 || state == BroadcastRecord.CALL_DONE_RECEIVE;
10083 }
10084
10085 public void finishReceiver(IBinder who, int resultCode, String resultData,
10086 Bundle resultExtras, boolean resultAbort) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010087 if (DEBUG_BROADCAST) Slog.v(TAG, "Finish receiver: " + who);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010088
10089 // Refuse possible leaked file descriptors
10090 if (resultExtras != null && resultExtras.hasFileDescriptors()) {
10091 throw new IllegalArgumentException("File descriptors passed in Bundle");
10092 }
10093
10094 boolean doNext;
10095
10096 final long origId = Binder.clearCallingIdentity();
10097
10098 synchronized(this) {
10099 doNext = finishReceiverLocked(
10100 who, resultCode, resultData, resultExtras, resultAbort, true);
10101 }
10102
10103 if (doNext) {
10104 processNextBroadcast(false);
10105 }
10106 trimApplications();
10107
10108 Binder.restoreCallingIdentity(origId);
10109 }
10110
10111 private final void logBroadcastReceiverDiscard(BroadcastRecord r) {
10112 if (r.nextReceiver > 0) {
10113 Object curReceiver = r.receivers.get(r.nextReceiver-1);
10114 if (curReceiver instanceof BroadcastFilter) {
10115 BroadcastFilter bf = (BroadcastFilter) curReceiver;
Doug Zongker2bec3d42009-12-04 12:52:44 -080010116 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010117 System.identityHashCode(r),
10118 r.intent.getAction(),
10119 r.nextReceiver - 1,
10120 System.identityHashCode(bf));
10121 } else {
Doug Zongker2bec3d42009-12-04 12:52:44 -080010122 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010123 System.identityHashCode(r),
10124 r.intent.getAction(),
10125 r.nextReceiver - 1,
10126 ((ResolveInfo)curReceiver).toString());
10127 }
10128 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010129 Slog.w(TAG, "Discarding broadcast before first receiver is invoked: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010130 + r);
Doug Zongker2bec3d42009-12-04 12:52:44 -080010131 EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 System.identityHashCode(r),
10133 r.intent.getAction(),
10134 r.nextReceiver,
10135 "NONE");
10136 }
10137 }
10138
10139 private final void broadcastTimeout() {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010140 ProcessRecord app = null;
10141 String anrMessage = null;
10142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143 synchronized (this) {
10144 if (mOrderedBroadcasts.size() == 0) {
10145 return;
10146 }
10147 long now = SystemClock.uptimeMillis();
10148 BroadcastRecord r = mOrderedBroadcasts.get(0);
Dianne Hackborn12527f92009-11-11 17:39:50 -080010149 if ((r.receiverTime+BROADCAST_TIMEOUT) > now) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010150 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010151 "Premature timeout @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for "
Dianne Hackborn12527f92009-11-11 17:39:50 -080010152 + (r.receiverTime + BROADCAST_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010153 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
Dianne Hackborn12527f92009-11-11 17:39:50 -080010154 mHandler.sendMessageAtTime(msg, r.receiverTime+BROADCAST_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010155 return;
10156 }
10157
Joe Onorato8a9b2202010-02-26 18:56:32 -080010158 Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r.receiver);
Dianne Hackborn12527f92009-11-11 17:39:50 -080010159 r.receiverTime = now;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010160 r.anrCount++;
10161
10162 // Current receiver has passed its expiration date.
10163 if (r.nextReceiver <= 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010164 Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010165 return;
10166 }
10167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010168 Object curReceiver = r.receivers.get(r.nextReceiver-1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010169 Slog.w(TAG, "Receiver during timeout: " + curReceiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010170 logBroadcastReceiverDiscard(r);
10171 if (curReceiver instanceof BroadcastFilter) {
10172 BroadcastFilter bf = (BroadcastFilter)curReceiver;
10173 if (bf.receiverList.pid != 0
10174 && bf.receiverList.pid != MY_PID) {
10175 synchronized (this.mPidsSelfLocked) {
10176 app = this.mPidsSelfLocked.get(
10177 bf.receiverList.pid);
10178 }
10179 }
10180 } else {
10181 app = r.curApp;
10182 }
10183
10184 if (app != null) {
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010185 anrMessage = "Broadcast of " + r.intent.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010186 }
10187
10188 if (mPendingBroadcast == r) {
10189 mPendingBroadcast = null;
10190 }
10191
10192 // Move on to the next receiver.
10193 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
10194 r.resultExtras, r.resultAbort, true);
10195 scheduleBroadcastsLocked();
10196 }
Dianne Hackbornad5499d2010-03-29 18:08:45 -070010197
10198 if (anrMessage != null) {
10199 appNotResponding(app, null, null, anrMessage);
10200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010201 }
10202
10203 private final void processCurBroadcastLocked(BroadcastRecord r,
10204 ProcessRecord app) throws RemoteException {
10205 if (app.thread == null) {
10206 throw new RemoteException();
10207 }
10208 r.receiver = app.thread.asBinder();
10209 r.curApp = app;
10210 app.curReceiver = r;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010211 updateLruProcessLocked(app, true, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010212
10213 // Tell the application to launch this receiver.
10214 r.intent.setComponent(r.curComponent);
10215
10216 boolean started = false;
10217 try {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010218 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219 "Delivering to component " + r.curComponent
10220 + ": " + r);
Dianne Hackborn5c1e00b2009-06-18 17:10:57 -070010221 ensurePackageDexOpt(r.intent.getComponent().getPackageName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010222 app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver,
10223 r.resultCode, r.resultData, r.resultExtras, r.ordered);
10224 started = true;
10225 } finally {
10226 if (!started) {
10227 r.receiver = null;
10228 r.curApp = null;
10229 app.curReceiver = null;
10230 }
10231 }
10232
10233 }
10234
10235 static void performReceive(ProcessRecord app, IIntentReceiver receiver,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070010236 Intent intent, int resultCode, String data, Bundle extras,
10237 boolean ordered, boolean sticky) throws RemoteException {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010238 if (app != null && app.thread != null) {
10239 // If we have an app thread, do the call through that so it is
10240 // correctly ordered with other one-way calls.
10241 app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070010242 data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 } else {
Dianne Hackborn68d881c2009-10-05 13:58:17 -070010244 receiver.performReceive(intent, resultCode, data, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010245 }
10246 }
10247
10248 private final void deliverToRegisteredReceiver(BroadcastRecord r,
10249 BroadcastFilter filter, boolean ordered) {
10250 boolean skip = false;
10251 if (filter.requiredPermission != null) {
10252 int perm = checkComponentPermission(filter.requiredPermission,
10253 r.callingPid, r.callingUid, -1);
10254 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010255 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 + r.intent.toString()
10257 + " from " + r.callerPackage + " (pid="
10258 + r.callingPid + ", uid=" + r.callingUid + ")"
10259 + " requires " + filter.requiredPermission
10260 + " due to registered receiver " + filter);
10261 skip = true;
10262 }
10263 }
10264 if (r.requiredPermission != null) {
10265 int perm = checkComponentPermission(r.requiredPermission,
10266 filter.receiverList.pid, filter.receiverList.uid, -1);
10267 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010268 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010269 + r.intent.toString()
10270 + " to " + filter.receiverList.app
10271 + " (pid=" + filter.receiverList.pid
10272 + ", uid=" + filter.receiverList.uid + ")"
10273 + " requires " + r.requiredPermission
10274 + " due to sender " + r.callerPackage
10275 + " (uid " + r.callingUid + ")");
10276 skip = true;
10277 }
10278 }
10279
10280 if (!skip) {
10281 // If this is not being sent as an ordered broadcast, then we
10282 // don't want to touch the fields that keep track of the current
10283 // state of ordered broadcasts.
10284 if (ordered) {
10285 r.receiver = filter.receiverList.receiver.asBinder();
10286 r.curFilter = filter;
10287 filter.receiverList.curBroadcast = r;
10288 r.state = BroadcastRecord.CALL_IN_RECEIVE;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010289 if (filter.receiverList.app != null) {
10290 // Bump hosting application to no longer be in background
10291 // scheduling class. Note that we can't do that if there
10292 // isn't an app... but we can only be in that case for
10293 // things that directly call the IActivityManager API, which
10294 // are already core system stuff so don't matter for this.
10295 r.curApp = filter.receiverList.app;
10296 filter.receiverList.app.curReceiver = r;
10297 updateOomAdjLocked();
10298 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010299 }
10300 try {
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010301 if (DEBUG_BROADCAST_LIGHT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 int seq = r.intent.getIntExtra("seq", -1);
Dianne Hackborn399cccb2010-04-13 22:57:49 -070010303 Slog.i(TAG, "Delivering to " + filter
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010304 + " (seq=" + seq + "): " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010305 }
10306 performReceive(filter.receiverList.app, filter.receiverList.receiver,
10307 new Intent(r.intent), r.resultCode,
Dianne Hackborn12527f92009-11-11 17:39:50 -080010308 r.resultData, r.resultExtras, r.ordered, r.initialSticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010309 if (ordered) {
10310 r.state = BroadcastRecord.CALL_DONE_RECEIVE;
10311 }
10312 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010313 Slog.w(TAG, "Failure sending broadcast " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010314 if (ordered) {
10315 r.receiver = null;
10316 r.curFilter = null;
10317 filter.receiverList.curBroadcast = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010318 if (filter.receiverList.app != null) {
10319 filter.receiverList.app.curReceiver = null;
10320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010321 }
10322 }
10323 }
10324 }
10325
Dianne Hackborn12527f92009-11-11 17:39:50 -080010326 private final void addBroadcastToHistoryLocked(BroadcastRecord r) {
10327 if (r.callingUid < 0) {
10328 // This was from a registerReceiver() call; ignore it.
10329 return;
10330 }
10331 System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1,
10332 MAX_BROADCAST_HISTORY-1);
10333 r.finishTime = SystemClock.uptimeMillis();
10334 mBroadcastHistory[0] = r;
10335 }
10336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010337 private final void processNextBroadcast(boolean fromMsg) {
10338 synchronized(this) {
10339 BroadcastRecord r;
10340
Joe Onorato8a9b2202010-02-26 18:56:32 -080010341 if (DEBUG_BROADCAST) Slog.v(TAG, "processNextBroadcast: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 + mParallelBroadcasts.size() + " broadcasts, "
Dianne Hackborn399cccb2010-04-13 22:57:49 -070010343 + mOrderedBroadcasts.size() + " ordered broadcasts");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010344
10345 updateCpuStats();
10346
10347 if (fromMsg) {
10348 mBroadcastsScheduled = false;
10349 }
10350
10351 // First, deliver any non-serialized broadcasts right away.
10352 while (mParallelBroadcasts.size() > 0) {
10353 r = mParallelBroadcasts.remove(0);
Dianne Hackborn12527f92009-11-11 17:39:50 -080010354 r.dispatchTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010355 final int N = r.receivers.size();
Joe Onorato8a9b2202010-02-26 18:56:32 -080010356 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010357 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010358 for (int i=0; i<N; i++) {
10359 Object target = r.receivers.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010360 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn399cccb2010-04-13 22:57:49 -070010361 "Delivering non-ordered to registered "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362 + target + ": " + r);
10363 deliverToRegisteredReceiver(r, (BroadcastFilter)target, false);
10364 }
Dianne Hackborn12527f92009-11-11 17:39:50 -080010365 addBroadcastToHistoryLocked(r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010366 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Done with parallel broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010367 + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010368 }
10369
10370 // Now take care of the next serialized one...
10371
10372 // If we are waiting for a process to come up to handle the next
10373 // broadcast, then do nothing at this point. Just in case, we
10374 // check that the process we're waiting for still exists.
10375 if (mPendingBroadcast != null) {
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070010376 if (DEBUG_BROADCAST_LIGHT) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010377 Slog.v(TAG, "processNextBroadcast: waiting for "
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070010378 + mPendingBroadcast.curApp);
10379 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010380
10381 boolean isDead;
10382 synchronized (mPidsSelfLocked) {
10383 isDead = (mPidsSelfLocked.get(mPendingBroadcast.curApp.pid) == null);
10384 }
10385 if (!isDead) {
10386 // It's still alive, so keep waiting
10387 return;
10388 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010389 Slog.w(TAG, "pending app " + mPendingBroadcast.curApp
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010390 + " died before responding to broadcast");
10391 mPendingBroadcast = null;
10392 }
10393 }
10394
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010395 boolean looped = false;
10396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010397 do {
10398 if (mOrderedBroadcasts.size() == 0) {
10399 // No more broadcasts pending, so all done!
10400 scheduleAppGcsLocked();
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010401 if (looped) {
10402 // If we had finished the last ordered broadcast, then
10403 // make sure all processes have correct oom and sched
10404 // adjustments.
10405 updateOomAdjLocked();
10406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 return;
10408 }
10409 r = mOrderedBroadcasts.get(0);
10410 boolean forceReceive = false;
10411
10412 // Ensure that even if something goes awry with the timeout
10413 // detection, we catch "hung" broadcasts here, discard them,
Jeff Hamiltonacf84742010-05-25 22:10:18 -050010414 // and continue to make progress.
10415 //
10416 // This is only done if the system is ready so that PRE_BOOT_COMPLETED
10417 // receivers don't get executed with with timeouts. They're intended for
10418 // one time heavy lifting after system upgrades and can take
10419 // significant amounts of time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010420 int numReceivers = (r.receivers != null) ? r.receivers.size() : 0;
Jeff Hamiltonacf84742010-05-25 22:10:18 -050010421 if (mSystemReady && r.dispatchTime > 0) {
10422 long now = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010423 if ((numReceivers > 0) &&
10424 (now > r.dispatchTime + (2*BROADCAST_TIMEOUT*numReceivers))) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010425 Slog.w(TAG, "Hung broadcast discarded after timeout failure:"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010426 + " now=" + now
10427 + " dispatchTime=" + r.dispatchTime
Dianne Hackborn12527f92009-11-11 17:39:50 -080010428 + " startTime=" + r.receiverTime
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010429 + " intent=" + r.intent
10430 + " numReceivers=" + numReceivers
10431 + " nextReceiver=" + r.nextReceiver
10432 + " state=" + r.state);
10433 broadcastTimeout(); // forcibly finish this broadcast
10434 forceReceive = true;
10435 r.state = BroadcastRecord.IDLE;
10436 }
10437 }
10438
10439 if (r.state != BroadcastRecord.IDLE) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010440 if (DEBUG_BROADCAST) Slog.d(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010441 "processNextBroadcast() called when not idle (state="
10442 + r.state + ")");
10443 return;
10444 }
10445
10446 if (r.receivers == null || r.nextReceiver >= numReceivers
10447 || r.resultAbort || forceReceive) {
10448 // No more receivers for this broadcast! Send the final
10449 // result if requested...
10450 if (r.resultTo != null) {
10451 try {
10452 if (DEBUG_BROADCAST) {
10453 int seq = r.intent.getIntExtra("seq", -1);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010454 Slog.i(TAG, "Finishing broadcast " + r.intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010455 + " seq=" + seq + " app=" + r.callerApp);
10456 }
10457 performReceive(r.callerApp, r.resultTo,
10458 new Intent(r.intent), r.resultCode,
Dianne Hackborn68d881c2009-10-05 13:58:17 -070010459 r.resultData, r.resultExtras, false, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010461 Slog.w(TAG, "Failure sending broadcast result of " + r.intent, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 }
10463 }
10464
Joe Onorato8a9b2202010-02-26 18:56:32 -080010465 if (DEBUG_BROADCAST) Slog.v(TAG, "Cancelling BROADCAST_TIMEOUT_MSG");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 mHandler.removeMessages(BROADCAST_TIMEOUT_MSG);
10467
Joe Onorato8a9b2202010-02-26 18:56:32 -080010468 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Finished with ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010469 + r);
10470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010471 // ... and on to the next...
Dianne Hackborn12527f92009-11-11 17:39:50 -080010472 addBroadcastToHistoryLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010473 mOrderedBroadcasts.remove(0);
10474 r = null;
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010475 looped = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010476 continue;
10477 }
10478 } while (r == null);
10479
10480 // Get the next receiver...
10481 int recIdx = r.nextReceiver++;
10482
10483 // Keep track of when this receiver started, and make sure there
10484 // is a timeout message pending to kill it if need be.
Dianne Hackborn12527f92009-11-11 17:39:50 -080010485 r.receiverTime = SystemClock.uptimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010486 if (recIdx == 0) {
Dianne Hackborn12527f92009-11-11 17:39:50 -080010487 r.dispatchTime = r.receiverTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010488
Joe Onorato8a9b2202010-02-26 18:56:32 -080010489 if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG, "Processing ordered broadcast "
Dianne Hackborn82f3f002009-06-16 18:49:05 -070010490 + r);
Joe Onorato8a9b2202010-02-26 18:56:32 -080010491 if (DEBUG_BROADCAST) Slog.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010492 "Submitting BROADCAST_TIMEOUT_MSG for "
Dianne Hackborn12527f92009-11-11 17:39:50 -080010493 + (r.receiverTime + BROADCAST_TIMEOUT));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010494 Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG);
Dianne Hackborn12527f92009-11-11 17:39:50 -080010495 mHandler.sendMessageAtTime(msg, r.receiverTime+BROADCAST_TIMEOUT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 }
10497
10498 Object nextReceiver = r.receivers.get(recIdx);
10499 if (nextReceiver instanceof BroadcastFilter) {
10500 // Simple case: this is a registered receiver who gets
10501 // a direct call.
10502 BroadcastFilter filter = (BroadcastFilter)nextReceiver;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010503 if (DEBUG_BROADCAST) Slog.v(TAG,
Dianne Hackborn399cccb2010-04-13 22:57:49 -070010504 "Delivering ordered to registered "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010505 + filter + ": " + r);
10506 deliverToRegisteredReceiver(r, filter, r.ordered);
10507 if (r.receiver == null || !r.ordered) {
10508 // The receiver has already finished, so schedule to
10509 // process the next one.
Dianne Hackborn399cccb2010-04-13 22:57:49 -070010510 if (DEBUG_BROADCAST) Slog.v(TAG, "Quick finishing: ordered="
10511 + r.ordered + " receiver=" + r.receiver);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 r.state = BroadcastRecord.IDLE;
10513 scheduleBroadcastsLocked();
10514 }
10515 return;
10516 }
10517
10518 // Hard case: need to instantiate the receiver, possibly
10519 // starting its application process to host it.
10520
10521 ResolveInfo info =
10522 (ResolveInfo)nextReceiver;
10523
10524 boolean skip = false;
10525 int perm = checkComponentPermission(info.activityInfo.permission,
10526 r.callingPid, r.callingUid,
10527 info.activityInfo.exported
10528 ? -1 : info.activityInfo.applicationInfo.uid);
10529 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010530 Slog.w(TAG, "Permission Denial: broadcasting "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010531 + r.intent.toString()
10532 + " from " + r.callerPackage + " (pid=" + r.callingPid
10533 + ", uid=" + r.callingUid + ")"
10534 + " requires " + info.activityInfo.permission
10535 + " due to receiver " + info.activityInfo.packageName
10536 + "/" + info.activityInfo.name);
10537 skip = true;
10538 }
10539 if (r.callingUid != Process.SYSTEM_UID &&
10540 r.requiredPermission != null) {
10541 try {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070010542 perm = AppGlobals.getPackageManager().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 checkPermission(r.requiredPermission,
10544 info.activityInfo.applicationInfo.packageName);
10545 } catch (RemoteException e) {
10546 perm = PackageManager.PERMISSION_DENIED;
10547 }
10548 if (perm != PackageManager.PERMISSION_GRANTED) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010549 Slog.w(TAG, "Permission Denial: receiving "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550 + r.intent + " to "
10551 + info.activityInfo.applicationInfo.packageName
10552 + " requires " + r.requiredPermission
10553 + " due to sender " + r.callerPackage
10554 + " (uid " + r.callingUid + ")");
10555 skip = true;
10556 }
10557 }
10558 if (r.curApp != null && r.curApp.crashing) {
10559 // If the target process is crashing, just skip it.
10560 skip = true;
10561 }
10562
10563 if (skip) {
10564 r.receiver = null;
10565 r.curFilter = null;
10566 r.state = BroadcastRecord.IDLE;
10567 scheduleBroadcastsLocked();
10568 return;
10569 }
10570
10571 r.state = BroadcastRecord.APP_RECEIVE;
10572 String targetProcess = info.activityInfo.processName;
10573 r.curComponent = new ComponentName(
10574 info.activityInfo.applicationInfo.packageName,
10575 info.activityInfo.name);
10576 r.curReceiver = info.activityInfo;
10577
10578 // Is this receiver's application already running?
10579 ProcessRecord app = getProcessRecordLocked(targetProcess,
10580 info.activityInfo.applicationInfo.uid);
10581 if (app != null && app.thread != null) {
10582 try {
10583 processCurBroadcastLocked(r, app);
10584 return;
10585 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010586 Slog.w(TAG, "Exception when sending broadcast to "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010587 + r.curComponent, e);
10588 }
10589
10590 // If a dead object exception was thrown -- fall through to
10591 // restart the application.
10592 }
10593
Dianne Hackborn9acc0302009-08-25 00:27:12 -070010594 // Not running -- get it started, to be executed when the app comes up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 if ((r.curApp=startProcessLocked(targetProcess,
10596 info.activityInfo.applicationInfo, true,
10597 r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND,
Dianne Hackborn9acc0302009-08-25 00:27:12 -070010598 "broadcast", r.curComponent,
10599 (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0))
10600 == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010601 // Ah, this recipient is unavailable. Finish it if necessary,
10602 // and mark the broadcast record as ready for the next.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010603 Slog.w(TAG, "Unable to launch app "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010604 + info.activityInfo.applicationInfo.packageName + "/"
10605 + info.activityInfo.applicationInfo.uid + " for broadcast "
10606 + r.intent + ": process is bad");
10607 logBroadcastReceiverDiscard(r);
10608 finishReceiverLocked(r.receiver, r.resultCode, r.resultData,
10609 r.resultExtras, r.resultAbort, true);
10610 scheduleBroadcastsLocked();
10611 r.state = BroadcastRecord.IDLE;
10612 return;
10613 }
10614
10615 mPendingBroadcast = r;
10616 }
10617 }
10618
10619 // =========================================================
10620 // INSTRUMENTATION
10621 // =========================================================
10622
10623 public boolean startInstrumentation(ComponentName className,
10624 String profileFile, int flags, Bundle arguments,
10625 IInstrumentationWatcher watcher) {
10626 // Refuse possible leaked file descriptors
10627 if (arguments != null && arguments.hasFileDescriptors()) {
10628 throw new IllegalArgumentException("File descriptors passed in Bundle");
10629 }
10630
10631 synchronized(this) {
10632 InstrumentationInfo ii = null;
10633 ApplicationInfo ai = null;
10634 try {
10635 ii = mContext.getPackageManager().getInstrumentationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070010636 className, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 ai = mContext.getPackageManager().getApplicationInfo(
Dianne Hackborn1655be42009-05-08 14:29:01 -070010638 ii.targetPackage, STOCK_PM_FLAGS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 } catch (PackageManager.NameNotFoundException e) {
10640 }
10641 if (ii == null) {
10642 reportStartInstrumentationFailure(watcher, className,
10643 "Unable to find instrumentation info for: " + className);
10644 return false;
10645 }
10646 if (ai == null) {
10647 reportStartInstrumentationFailure(watcher, className,
10648 "Unable to find instrumentation target package: " + ii.targetPackage);
10649 return false;
10650 }
10651
10652 int match = mContext.getPackageManager().checkSignatures(
10653 ii.targetPackage, ii.packageName);
10654 if (match < 0 && match != PackageManager.SIGNATURE_FIRST_NOT_SIGNED) {
10655 String msg = "Permission Denial: starting instrumentation "
10656 + className + " from pid="
10657 + Binder.getCallingPid()
10658 + ", uid=" + Binder.getCallingPid()
10659 + " not allowed because package " + ii.packageName
10660 + " does not have a signature matching the target "
10661 + ii.targetPackage;
10662 reportStartInstrumentationFailure(watcher, className, msg);
10663 throw new SecurityException(msg);
10664 }
10665
10666 final long origId = Binder.clearCallingIdentity();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080010667 forceStopPackageLocked(ii.targetPackage, -1, true, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 ProcessRecord app = addAppLocked(ai);
10669 app.instrumentationClass = className;
Dianne Hackborn1655be42009-05-08 14:29:01 -070010670 app.instrumentationInfo = ai;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010671 app.instrumentationProfileFile = profileFile;
10672 app.instrumentationArguments = arguments;
10673 app.instrumentationWatcher = watcher;
10674 app.instrumentationResultClass = className;
10675 Binder.restoreCallingIdentity(origId);
10676 }
10677
10678 return true;
10679 }
10680
10681 /**
10682 * Report errors that occur while attempting to start Instrumentation. Always writes the
10683 * error to the logs, but if somebody is watching, send the report there too. This enables
10684 * the "am" command to report errors with more information.
10685 *
10686 * @param watcher The IInstrumentationWatcher. Null if there isn't one.
10687 * @param cn The component name of the instrumentation.
10688 * @param report The error report.
10689 */
10690 private void reportStartInstrumentationFailure(IInstrumentationWatcher watcher,
10691 ComponentName cn, String report) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010692 Slog.w(TAG, report);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 try {
10694 if (watcher != null) {
10695 Bundle results = new Bundle();
10696 results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, "ActivityManagerService");
10697 results.putString("Error", report);
10698 watcher.instrumentationStatus(cn, -1, results);
10699 }
10700 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010701 Slog.w(TAG, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 }
10703 }
10704
10705 void finishInstrumentationLocked(ProcessRecord app, int resultCode, Bundle results) {
10706 if (app.instrumentationWatcher != null) {
10707 try {
10708 // NOTE: IInstrumentationWatcher *must* be oneway here
10709 app.instrumentationWatcher.instrumentationFinished(
10710 app.instrumentationClass,
10711 resultCode,
10712 results);
10713 } catch (RemoteException e) {
10714 }
10715 }
10716 app.instrumentationWatcher = null;
10717 app.instrumentationClass = null;
Dianne Hackborn1655be42009-05-08 14:29:01 -070010718 app.instrumentationInfo = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010719 app.instrumentationProfileFile = null;
10720 app.instrumentationArguments = null;
10721
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080010722 forceStopPackageLocked(app.processName, -1, false, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010723 }
10724
10725 public void finishInstrumentation(IApplicationThread target,
10726 int resultCode, Bundle results) {
10727 // Refuse possible leaked file descriptors
10728 if (results != null && results.hasFileDescriptors()) {
10729 throw new IllegalArgumentException("File descriptors passed in Intent");
10730 }
10731
10732 synchronized(this) {
10733 ProcessRecord app = getRecordForAppLocked(target);
10734 if (app == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010735 Slog.w(TAG, "finishInstrumentation: no app for " + target);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 return;
10737 }
10738 final long origId = Binder.clearCallingIdentity();
10739 finishInstrumentationLocked(app, resultCode, results);
10740 Binder.restoreCallingIdentity(origId);
10741 }
10742 }
10743
10744 // =========================================================
10745 // CONFIGURATION
10746 // =========================================================
10747
10748 public ConfigurationInfo getDeviceConfigurationInfo() {
10749 ConfigurationInfo config = new ConfigurationInfo();
10750 synchronized (this) {
10751 config.reqTouchScreen = mConfiguration.touchscreen;
10752 config.reqKeyboardType = mConfiguration.keyboard;
10753 config.reqNavigation = mConfiguration.navigation;
Dianne Hackbornfae76f52009-07-16 13:41:23 -070010754 if (mConfiguration.navigation == Configuration.NAVIGATION_DPAD
10755 || mConfiguration.navigation == Configuration.NAVIGATION_TRACKBALL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_FIVE_WAY_NAV;
10757 }
Dianne Hackbornfae76f52009-07-16 13:41:23 -070010758 if (mConfiguration.keyboard != Configuration.KEYBOARD_UNDEFINED
10759 && mConfiguration.keyboard != Configuration.KEYBOARD_NOKEYS) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 config.reqInputFeatures |= ConfigurationInfo.INPUT_FEATURE_HARD_KEYBOARD;
10761 }
Jack Palevichb90d28c2009-07-22 15:35:24 -070010762 config.reqGlEsVersion = GL_ES_VERSION;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 }
10764 return config;
10765 }
10766
10767 public Configuration getConfiguration() {
10768 Configuration ci;
10769 synchronized(this) {
10770 ci = new Configuration(mConfiguration);
10771 }
10772 return ci;
10773 }
10774
10775 public void updateConfiguration(Configuration values) {
10776 enforceCallingPermission(android.Manifest.permission.CHANGE_CONFIGURATION,
10777 "updateConfiguration()");
10778
10779 synchronized(this) {
10780 if (values == null && mWindowManager != null) {
10781 // sentinel: fetch the current configuration from the window manager
10782 values = mWindowManager.computeNewConfiguration();
10783 }
10784
10785 final long origId = Binder.clearCallingIdentity();
10786 updateConfigurationLocked(values, null);
10787 Binder.restoreCallingIdentity(origId);
10788 }
10789 }
10790
10791 /**
10792 * Do either or both things: (1) change the current configuration, and (2)
10793 * make sure the given activity is running with the (now) current
10794 * configuration. Returns true if the activity has been left running, or
10795 * false if <var>starting</var> is being destroyed to match the new
10796 * configuration.
10797 */
10798 public boolean updateConfigurationLocked(Configuration values,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070010799 ActivityRecord starting) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010800 int changes = 0;
10801
10802 boolean kept = true;
10803
10804 if (values != null) {
10805 Configuration newConfig = new Configuration(mConfiguration);
10806 changes = newConfig.updateFrom(values);
10807 if (changes != 0) {
Dianne Hackborndc6b6352009-09-30 14:20:09 -070010808 if (DEBUG_SWITCH || DEBUG_CONFIGURATION) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010809 Slog.i(TAG, "Updating configuration to: " + values);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010810 }
10811
Doug Zongker2bec3d42009-12-04 12:52:44 -080010812 EventLog.writeEvent(EventLogTags.CONFIGURATION_CHANGED, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010813
10814 if (values.locale != null) {
10815 saveLocaleLocked(values.locale,
10816 !values.locale.equals(mConfiguration.locale),
10817 values.userSetLocale);
10818 }
10819
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010820 mConfigurationSeq++;
10821 if (mConfigurationSeq <= 0) {
10822 mConfigurationSeq = 1;
10823 }
10824 newConfig.seq = mConfigurationSeq;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010825 mConfiguration = newConfig;
Joe Onorato8a9b2202010-02-26 18:56:32 -080010826 Slog.i(TAG, "Config changed: " + newConfig);
Dianne Hackborn826d17c2009-11-12 12:55:51 -080010827
10828 AttributeCache ac = AttributeCache.instance();
10829 if (ac != null) {
10830 ac.updateConfiguration(mConfiguration);
10831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010832
Dianne Hackbornb8b11a02010-03-10 15:53:11 -080010833 if (Settings.System.hasInterestingConfigurationChanges(changes)) {
10834 Message msg = mHandler.obtainMessage(UPDATE_CONFIGURATION_MSG);
10835 msg.obj = new Configuration(mConfiguration);
10836 mHandler.sendMessage(msg);
10837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010838
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010839 for (int i=mLruProcesses.size()-1; i>=0; i--) {
10840 ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 try {
10842 if (app.thread != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080010843 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Sending to proc "
Dianne Hackborndc6b6352009-09-30 14:20:09 -070010844 + app.processName + " new config " + mConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010845 app.thread.scheduleConfigurationChanged(mConfiguration);
10846 }
10847 } catch (Exception e) {
10848 }
10849 }
10850 Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
Dianne Hackborn1c633fc2009-12-08 19:45:14 -080010851 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
10852 | Intent.FLAG_RECEIVER_REPLACE_PENDING);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010853 broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
10854 null, false, false, MY_PID, Process.SYSTEM_UID);
Dianne Hackborn362d5b92009-11-11 18:04:39 -080010855 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
10856 broadcastIntentLocked(null, null,
10857 new Intent(Intent.ACTION_LOCALE_CHANGED),
10858 null, null, 0, null, null,
10859 null, false, false, MY_PID, Process.SYSTEM_UID);
10860 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010861 }
10862 }
10863
10864 if (changes != 0 && starting == null) {
10865 // If the configuration changed, and the caller is not already
10866 // in the process of starting an activity, then find the top
10867 // activity to check if its configuration needs to change.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070010868 starting = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 }
10870
10871 if (starting != null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070010872 kept = mMainStack.ensureActivityConfigurationLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010873 if (kept) {
10874 // If this didn't result in the starting activity being
10875 // destroyed, then we need to make sure at this point that all
10876 // other activities are made visible.
Joe Onorato8a9b2202010-02-26 18:56:32 -080010877 if (DEBUG_SWITCH) Slog.i(TAG, "Config didn't destroy " + starting
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010878 + ", ensuring others are correct.");
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070010879 mMainStack.ensureActivitiesVisibleLocked(starting, changes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010880 }
10881 }
10882
Dianne Hackborne36d6e22010-02-17 19:46:25 -080010883 if (values != null && mWindowManager != null) {
10884 mWindowManager.setNewConfiguration(mConfiguration);
10885 }
10886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010887 return kept;
10888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889
10890 /**
10891 * Save the locale. You must be inside a synchronized (this) block.
10892 */
10893 private void saveLocaleLocked(Locale l, boolean isDiff, boolean isPersist) {
10894 if(isDiff) {
10895 SystemProperties.set("user.language", l.getLanguage());
10896 SystemProperties.set("user.region", l.getCountry());
10897 }
10898
10899 if(isPersist) {
10900 SystemProperties.set("persist.sys.language", l.getLanguage());
10901 SystemProperties.set("persist.sys.country", l.getCountry());
10902 SystemProperties.set("persist.sys.localevar", l.getVariant());
10903 }
10904 }
10905
10906 // =========================================================
10907 // LIFETIME MANAGEMENT
10908 // =========================================================
10909
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010910 private final int computeOomAdjLocked(ProcessRecord app, int hiddenAdj,
10911 ProcessRecord TOP_APP, boolean recursed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010912 if (mAdjSeq == app.adjSeq) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010913 // This adjustment has already been computed. If we are calling
10914 // from the top, we may have already computed our adjustment with
10915 // an earlier hidden adjustment that isn't really for us... if
10916 // so, use the new hidden adjustment.
10917 if (!recursed && app.hidden) {
10918 app.curAdj = hiddenAdj;
10919 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010920 return app.curAdj;
10921 }
10922
10923 if (app.thread == null) {
10924 app.adjSeq = mAdjSeq;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010925 app.curSchedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010926 return (app.curAdj=EMPTY_APP_ADJ);
10927 }
10928
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010929 if (app.maxAdj <= FOREGROUND_APP_ADJ) {
10930 // The max adjustment doesn't allow this app to be anything
10931 // below foreground, so it is not worth doing work for it.
10932 app.adjType = "fixed";
10933 app.adjSeq = mAdjSeq;
10934 app.curRawAdj = app.maxAdj;
10935 app.curSchedGroup = Process.THREAD_GROUP_DEFAULT;
10936 return (app.curAdj=app.maxAdj);
10937 }
10938
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070010939 app.adjTypeCode = ActivityManager.RunningAppProcessInfo.REASON_UNKNOWN;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010940 app.adjSource = null;
10941 app.adjTarget = null;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080010942 app.empty = false;
10943 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010944
The Android Open Source Project4df24232009-03-05 14:34:35 -080010945 // Determine the importance of the process, starting with most
10946 // important to least, and assign an appropriate OOM adjustment.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 int adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010948 int schedGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949 int N;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010950 if (app == TOP_APP) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010951 // The last app on the list is the foreground app.
10952 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010953 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070010954 app.adjType = "top-activity";
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010955 } else if (app.instrumentationClass != null) {
10956 // Don't want to kill running instrumentation.
10957 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010958 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070010959 app.adjType = "instrumentation";
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010960 } else if (app.persistentActivities > 0) {
10961 // Special persistent activities... shouldn't be used these days.
10962 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010963 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070010964 app.adjType = "persistent";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010965 } else if (app.curReceiver != null ||
10966 (mPendingBroadcast != null && mPendingBroadcast.curApp == app)) {
10967 // An app that is currently receiving a broadcast also
10968 // counts as being in the foreground.
10969 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010970 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010971 app.adjType = "broadcast";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010972 } else if (app.executingServices.size() > 0) {
10973 // An app that is currently executing a service callback also
10974 // counts as being in the foreground.
10975 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010976 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010977 app.adjType = "exec-service";
10978 } else if (app.foregroundServices) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979 // The user is aware of this app, so make it visible.
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010980 adj = PERCEPTIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010981 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010982 app.adjType = "foreground-service";
10983 } else if (app.forcingToForeground != null) {
10984 // The user is aware of this app, so make it visible.
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010985 adj = PERCEPTIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010986 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010987 app.adjType = "force-foreground";
10988 app.adjSource = app.forcingToForeground;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070010989 } else if (app == mHeavyWeightProcess) {
10990 // We don't want to kill the current heavy-weight process.
10991 adj = HEAVY_WEIGHT_APP_ADJ;
10992 schedGroup = Process.THREAD_GROUP_DEFAULT;
10993 app.adjType = "heavy";
The Android Open Source Project4df24232009-03-05 14:34:35 -080010994 } else if (app == mHomeProcess) {
10995 // This process is hosting what we currently consider to be the
10996 // home app, so we don't want to let it go into the background.
10997 adj = HOME_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080010998 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070010999 app.adjType = "home";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011000 } else if ((N=app.activities.size()) != 0) {
11001 // This app is in the background with paused activities.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011002 app.hidden = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 adj = hiddenAdj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011004 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011005 app.adjType = "bg-activities";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011006 N = app.activities.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 for (int j=0; j<N; j++) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070011008 if (app.activities.get(j).visible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011009 // This app has a visible activity!
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011010 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011011 adj = VISIBLE_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011012 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011013 app.adjType = "visible";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011014 break;
11015 }
11016 }
11017 } else {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011018 // A very not-needed process. If this is lower in the lru list,
11019 // we will push it in to the empty bucket.
11020 app.hidden = true;
11021 app.empty = true;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011022 schedGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011023 adj = hiddenAdj;
11024 app.adjType = "bg-empty";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011025 }
11026
Joe Onorato8a9b2202010-02-26 18:56:32 -080011027 //Slog.i(TAG, "OOM " + app + ": initial adj=" + adj);
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011028
The Android Open Source Project4df24232009-03-05 14:34:35 -080011029 // By default, we use the computed adjustment. It may be changed if
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011030 // there are applications dependent on our services or providers, but
11031 // this gives us a baseline and makes sure we don't get into an
11032 // infinite recursion.
11033 app.adjSeq = mAdjSeq;
11034 app.curRawAdj = adj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035
Christopher Tate6fa95972009-06-05 18:43:55 -070011036 if (mBackupTarget != null && app == mBackupTarget.app) {
11037 // If possible we want to avoid killing apps while they're being backed up
11038 if (adj > BACKUP_APP_ADJ) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011039 if (DEBUG_BACKUP) Slog.v(TAG, "oom BACKUP_APP_ADJ for " + app);
Christopher Tate6fa95972009-06-05 18:43:55 -070011040 adj = BACKUP_APP_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011041 app.adjType = "backup";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011042 app.hidden = false;
Christopher Tate6fa95972009-06-05 18:43:55 -070011043 }
11044 }
11045
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011046 if (app.services.size() != 0 && (adj > FOREGROUND_APP_ADJ
11047 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011048 final long now = SystemClock.uptimeMillis();
11049 // This process is more important if the top activity is
11050 // bound to the service.
Dianne Hackborn860755f2010-06-03 18:47:52 -070011051 Iterator<ServiceRecord> jt = app.services.iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011052 while (jt.hasNext() && adj > FOREGROUND_APP_ADJ) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070011053 ServiceRecord s = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011054 if (s.startRequested) {
11055 if (now < (s.lastActivity+MAX_SERVICE_INACTIVITY)) {
11056 // This service has seen some activity within
11057 // recent memory, so we will keep its process ahead
11058 // of the background processes.
11059 if (adj > SECONDARY_SERVER_ADJ) {
11060 adj = SECONDARY_SERVER_ADJ;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011061 app.adjType = "started-services";
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011062 app.hidden = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 }
11064 }
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080011065 // If we have let the service slide into the background
11066 // state, still have some text describing what it is doing
11067 // even though the service no longer has an impact.
11068 if (adj > SECONDARY_SERVER_ADJ) {
11069 app.adjType = "started-bg-services";
11070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011071 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011072 if (s.connections.size() > 0 && (adj > FOREGROUND_APP_ADJ
11073 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011074 Iterator<ConnectionRecord> kt
11075 = s.connections.values().iterator();
11076 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
11077 // XXX should compute this based on the max of
11078 // all connected clients.
11079 ConnectionRecord cr = kt.next();
The Android Open Source Project10592532009-03-18 17:39:46 -070011080 if (cr.binding.client == app) {
11081 // Binding to ourself is not interesting.
11082 continue;
11083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011084 if ((cr.flags&Context.BIND_AUTO_CREATE) != 0) {
11085 ProcessRecord client = cr.binding.client;
11086 int myHiddenAdj = hiddenAdj;
11087 if (myHiddenAdj > client.hiddenAdj) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011088 if (client.hiddenAdj >= VISIBLE_APP_ADJ) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011089 myHiddenAdj = client.hiddenAdj;
11090 } else {
11091 myHiddenAdj = VISIBLE_APP_ADJ;
11092 }
11093 }
11094 int clientAdj = computeOomAdjLocked(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011095 client, myHiddenAdj, TOP_APP, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011096 if (adj > clientAdj) {
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011097 adj = clientAdj >= VISIBLE_APP_ADJ
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011098 ? clientAdj : VISIBLE_APP_ADJ;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011099 if (!client.hidden) {
11100 app.hidden = false;
11101 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011102 app.adjType = "service";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011103 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
11104 .REASON_SERVICE_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011105 app.adjSource = cr.binding.client;
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070011106 app.adjTarget = s.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011107 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011108 if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
11109 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
11110 schedGroup = Process.THREAD_GROUP_DEFAULT;
11111 }
11112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011113 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011114 ActivityRecord a = cr.activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 //if (a != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011116 // Slog.i(TAG, "Connection to " + a ": state=" + a.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011117 //}
11118 if (a != null && adj > FOREGROUND_APP_ADJ &&
11119 (a.state == ActivityState.RESUMED
11120 || a.state == ActivityState.PAUSING)) {
11121 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011122 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011123 app.hidden = false;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011124 app.adjType = "service";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011125 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
11126 .REASON_SERVICE_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011127 app.adjSource = a;
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070011128 app.adjTarget = s.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 }
11130 }
11131 }
11132 }
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070011133
11134 // Finally, f this process has active services running in it, we
11135 // would like to avoid killing it unless it would prevent the current
11136 // application from running. By default we put the process in
11137 // with the rest of the background processes; as we scan through
11138 // its services we may bump it up from there.
11139 if (adj > hiddenAdj) {
11140 adj = hiddenAdj;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011141 app.hidden = false;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070011142 app.adjType = "bg-services";
11143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011144 }
11145
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011146 if (app.pubProviders.size() != 0 && (adj > FOREGROUND_APP_ADJ
11147 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070011148 Iterator<ContentProviderRecord> jt = app.pubProviders.values().iterator();
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011149 while (jt.hasNext() && (adj > FOREGROUND_APP_ADJ
11150 || schedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE)) {
Dianne Hackborn860755f2010-06-03 18:47:52 -070011151 ContentProviderRecord cpr = jt.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011152 if (cpr.clients.size() != 0) {
11153 Iterator<ProcessRecord> kt = cpr.clients.iterator();
11154 while (kt.hasNext() && adj > FOREGROUND_APP_ADJ) {
11155 ProcessRecord client = kt.next();
The Android Open Source Project10592532009-03-18 17:39:46 -070011156 if (client == app) {
11157 // Being our own client is not interesting.
11158 continue;
11159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011160 int myHiddenAdj = hiddenAdj;
11161 if (myHiddenAdj > client.hiddenAdj) {
11162 if (client.hiddenAdj > FOREGROUND_APP_ADJ) {
11163 myHiddenAdj = client.hiddenAdj;
11164 } else {
11165 myHiddenAdj = FOREGROUND_APP_ADJ;
11166 }
11167 }
11168 int clientAdj = computeOomAdjLocked(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011169 client, myHiddenAdj, TOP_APP, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 if (adj > clientAdj) {
11171 adj = clientAdj > FOREGROUND_APP_ADJ
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011172 ? clientAdj : FOREGROUND_APP_ADJ;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011173 if (!client.hidden) {
11174 app.hidden = false;
11175 }
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011176 app.adjType = "provider";
Dianne Hackborndd9b82c2009-09-03 00:18:47 -070011177 app.adjTypeCode = ActivityManager.RunningAppProcessInfo
11178 .REASON_PROVIDER_IN_USE;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011179 app.adjSource = client;
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070011180 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 }
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011182 if (client.curSchedGroup == Process.THREAD_GROUP_DEFAULT) {
11183 schedGroup = Process.THREAD_GROUP_DEFAULT;
11184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 }
11186 }
11187 // If the provider has external (non-framework) process
11188 // dependencies, ensure that its adjustment is at least
11189 // FOREGROUND_APP_ADJ.
11190 if (cpr.externals != 0) {
11191 if (adj > FOREGROUND_APP_ADJ) {
11192 adj = FOREGROUND_APP_ADJ;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011193 schedGroup = Process.THREAD_GROUP_DEFAULT;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011194 app.hidden = false;
Dianne Hackbornde42bb62009-08-05 12:26:15 -070011195 app.adjType = "provider";
Dianne Hackbornb7bb3b32010-06-06 22:47:50 -070011196 app.adjTarget = cpr.name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011197 }
11198 }
11199 }
11200 }
11201
11202 app.curRawAdj = adj;
11203
Joe Onorato8a9b2202010-02-26 18:56:32 -080011204 //Slog.i(TAG, "OOM ADJ " + app + ": pid=" + app.pid +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011205 // " adj=" + adj + " curAdj=" + app.curAdj + " maxAdj=" + app.maxAdj);
11206 if (adj > app.maxAdj) {
11207 adj = app.maxAdj;
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011208 if (app.maxAdj <= PERCEPTIBLE_APP_ADJ) {
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011209 schedGroup = Process.THREAD_GROUP_DEFAULT;
11210 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011211 }
11212
11213 app.curAdj = adj;
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011214 app.curSchedGroup = schedGroup;
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011216 return adj;
11217 }
11218
11219 /**
11220 * Ask a given process to GC right now.
11221 */
11222 final void performAppGcLocked(ProcessRecord app) {
11223 try {
11224 app.lastRequestedGc = SystemClock.uptimeMillis();
11225 if (app.thread != null) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011226 if (app.reportLowMemory) {
11227 app.reportLowMemory = false;
11228 app.thread.scheduleLowMemory();
11229 } else {
11230 app.thread.processInBackground();
11231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011232 }
11233 } catch (Exception e) {
11234 // whatever.
11235 }
11236 }
11237
11238 /**
11239 * Returns true if things are idle enough to perform GCs.
11240 */
Josh Bartel7f208742010-02-25 11:01:44 -060011241 private final boolean canGcNowLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011242 return mParallelBroadcasts.size() == 0
11243 && mOrderedBroadcasts.size() == 0
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011244 && (mSleeping || (mMainStack.mResumedActivity != null &&
11245 mMainStack.mResumedActivity.idle));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 }
11247
11248 /**
11249 * Perform GCs on all processes that are waiting for it, but only
11250 * if things are idle.
11251 */
11252 final void performAppGcsLocked() {
11253 final int N = mProcessesToGc.size();
11254 if (N <= 0) {
11255 return;
11256 }
Josh Bartel7f208742010-02-25 11:01:44 -060011257 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011258 while (mProcessesToGc.size() > 0) {
11259 ProcessRecord proc = mProcessesToGc.remove(0);
Dianne Hackborn32907cf2010-06-10 17:50:20 -070011260 if (proc.curRawAdj > PERCEPTIBLE_APP_ADJ || proc.reportLowMemory) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011261 if ((proc.lastRequestedGc+GC_MIN_INTERVAL)
11262 <= SystemClock.uptimeMillis()) {
11263 // To avoid spamming the system, we will GC processes one
11264 // at a time, waiting a few seconds between each.
11265 performAppGcLocked(proc);
11266 scheduleAppGcsLocked();
11267 return;
11268 } else {
11269 // It hasn't been long enough since we last GCed this
11270 // process... put it in the list to wait for its time.
11271 addProcessToGcListLocked(proc);
11272 break;
11273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 }
11275 }
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011276
11277 scheduleAppGcsLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011278 }
11279 }
11280
11281 /**
11282 * If all looks good, perform GCs on all processes waiting for them.
11283 */
11284 final void performAppGcsIfAppropriateLocked() {
Josh Bartel7f208742010-02-25 11:01:44 -060011285 if (canGcNowLocked()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011286 performAppGcsLocked();
11287 return;
11288 }
11289 // Still not idle, wait some more.
11290 scheduleAppGcsLocked();
11291 }
11292
11293 /**
11294 * Schedule the execution of all pending app GCs.
11295 */
11296 final void scheduleAppGcsLocked() {
11297 mHandler.removeMessages(GC_BACKGROUND_PROCESSES_MSG);
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011298
11299 if (mProcessesToGc.size() > 0) {
11300 // Schedule a GC for the time to the next process.
11301 ProcessRecord proc = mProcessesToGc.get(0);
11302 Message msg = mHandler.obtainMessage(GC_BACKGROUND_PROCESSES_MSG);
11303
11304 long when = mProcessesToGc.get(0).lastRequestedGc + GC_MIN_INTERVAL;
11305 long now = SystemClock.uptimeMillis();
11306 if (when < (now+GC_TIMEOUT)) {
11307 when = now + GC_TIMEOUT;
11308 }
11309 mHandler.sendMessageAtTime(msg, when);
11310 }
11311 }
11312
11313 /**
11314 * Add a process to the array of processes waiting to be GCed. Keeps the
11315 * list in sorted order by the last GC time. The process can't already be
11316 * on the list.
11317 */
11318 final void addProcessToGcListLocked(ProcessRecord proc) {
11319 boolean added = false;
11320 for (int i=mProcessesToGc.size()-1; i>=0; i--) {
11321 if (mProcessesToGc.get(i).lastRequestedGc <
11322 proc.lastRequestedGc) {
11323 added = true;
11324 mProcessesToGc.add(i+1, proc);
11325 break;
11326 }
11327 }
11328 if (!added) {
11329 mProcessesToGc.add(0, proc);
11330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 }
11332
11333 /**
11334 * Set up to ask a process to GC itself. This will either do it
11335 * immediately, or put it on the list of processes to gc the next
11336 * time things are idle.
11337 */
11338 final void scheduleAppGcLocked(ProcessRecord app) {
11339 long now = SystemClock.uptimeMillis();
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011340 if ((app.lastRequestedGc+GC_MIN_INTERVAL) > now) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011341 return;
11342 }
11343 if (!mProcessesToGc.contains(app)) {
Dianne Hackbornfd12af42009-08-27 00:44:33 -070011344 addProcessToGcListLocked(app);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 scheduleAppGcsLocked();
11346 }
11347 }
11348
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070011349 final void checkExcessiveWakeLocksLocked(boolean doKills) {
11350 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
11351 if (mLastWakeLockCheckTime == 0) {
11352 doKills = false;
11353 }
11354 if (stats.isScreenOn()) {
11355 doKills = false;
11356 }
11357 final long curRealtime = SystemClock.elapsedRealtime();
11358 final long timeSince = curRealtime - mLastWakeLockCheckTime;
11359 mLastWakeLockCheckTime = curRealtime;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070011360 if (timeSince < (WAKE_LOCK_CHECK_DELAY/3)) {
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070011361 doKills = false;
11362 }
11363 int i = mLruProcesses.size();
11364 while (i > 0) {
11365 i--;
11366 ProcessRecord app = mLruProcesses.get(i);
11367 if (app.curAdj >= HIDDEN_APP_MIN_ADJ) {
11368 long wtime;
11369 synchronized (stats) {
11370 wtime = stats.getProcessWakeTime(app.info.uid,
11371 app.pid, curRealtime);
11372 }
11373 long timeUsed = wtime - app.lastWakeTime;
Dianne Hackborn1ebccf52010-08-15 13:04:34 -070011374 if (false) {
11375 StringBuilder sb = new StringBuilder(128);
11376 sb.append("Wake for ");
11377 app.toShortString(sb);
11378 sb.append(": over ");
11379 TimeUtils.formatDuration(timeSince, sb);
11380 sb.append(" used ");
11381 TimeUtils.formatDuration(timeUsed, sb);
11382 sb.append(" (");
11383 sb.append((timeUsed*100)/timeSince);
11384 sb.append("%)");
11385 Slog.i(TAG, sb.toString());
11386 }
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070011387 // If a process has held a wake lock for more
11388 // than 50% of the time during this period,
11389 // that sounds pad. Kill!
11390 if (doKills && timeSince > 0
11391 && ((timeUsed*100)/timeSince) >= 50) {
11392 Slog.i(TAG, "Excessive wake lock in " + app.processName
11393 + " (pid " + app.pid + "): held " + timeUsed
11394 + " during " + timeSince);
11395 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
11396 app.processName, app.setAdj, "excessive wake lock");
11397 Process.killProcessQuiet(app.pid);
11398 } else {
11399 app.lastWakeTime = wtime;
11400 }
11401 }
11402 }
11403 }
11404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011405 private final boolean updateOomAdjLocked(
11406 ProcessRecord app, int hiddenAdj, ProcessRecord TOP_APP) {
11407 app.hiddenAdj = hiddenAdj;
11408
11409 if (app.thread == null) {
11410 return true;
11411 }
11412
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011413 int adj = computeOomAdjLocked(app, hiddenAdj, TOP_APP, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011414
Dianne Hackborn09c916b2009-12-08 14:50:51 -080011415 if ((app.pid != 0 && app.pid != MY_PID) || Process.supportsProcesses()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416 if (app.curRawAdj != app.setRawAdj) {
11417 if (app.curRawAdj > FOREGROUND_APP_ADJ
11418 && app.setRawAdj <= FOREGROUND_APP_ADJ) {
11419 // If this app is transitioning from foreground to
11420 // non-foreground, have it do a gc.
11421 scheduleAppGcLocked(app);
11422 } else if (app.curRawAdj >= HIDDEN_APP_MIN_ADJ
11423 && app.setRawAdj < HIDDEN_APP_MIN_ADJ) {
11424 // Likewise do a gc when an app is moving in to the
11425 // background (such as a service stopping).
11426 scheduleAppGcLocked(app);
Dianne Hackborn9adb9c32010-08-13 14:09:56 -070011427 // And note its current wake lock time.
11428 BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics();
11429 synchronized (stats) {
11430 app.lastWakeTime = stats.getProcessWakeTime(app.info.uid,
11431 app.pid, SystemClock.elapsedRealtime());
11432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 }
11434 app.setRawAdj = app.curRawAdj;
11435 }
11436 if (adj != app.setAdj) {
11437 if (Process.setOomAdj(app.pid, adj)) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011438 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011439 TAG, "Set app " + app.processName +
11440 " oom adj to " + adj);
11441 app.setAdj = adj;
11442 } else {
11443 return false;
11444 }
11445 }
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011446 if (app.setSchedGroup != app.curSchedGroup) {
11447 app.setSchedGroup = app.curSchedGroup;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011448 if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG,
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011449 "Setting process group of " + app.processName
11450 + " to " + app.curSchedGroup);
11451 if (true) {
San Mehat9438de22009-06-10 09:11:28 -070011452 long oldId = Binder.clearCallingIdentity();
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011453 try {
11454 Process.setProcessGroup(app.pid, app.curSchedGroup);
11455 } catch (Exception e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011456 Slog.w(TAG, "Failed setting process group of " + app.pid
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011457 + " to " + app.curSchedGroup);
San Mehat9438de22009-06-10 09:11:28 -070011458 e.printStackTrace();
11459 } finally {
11460 Binder.restoreCallingIdentity(oldId);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -070011461 }
11462 }
11463 if (false) {
11464 if (app.thread != null) {
11465 try {
11466 app.thread.setSchedulingGroup(app.curSchedGroup);
11467 } catch (RemoteException e) {
11468 }
11469 }
11470 }
11471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011472 }
11473
11474 return true;
11475 }
11476
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011477 private final ActivityRecord resumedAppLocked() {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011478 ActivityRecord resumedActivity = mMainStack.mResumedActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011479 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011480 resumedActivity = mMainStack.mPausingActivity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011481 if (resumedActivity == null || resumedActivity.app == null) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011482 resumedActivity = mMainStack.topRunningActivityLocked(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011483 }
11484 }
11485 return resumedActivity;
11486 }
11487
11488 private final boolean updateOomAdjLocked(ProcessRecord app) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011489 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011490 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
11491 int curAdj = app.curAdj;
11492 final boolean wasHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
11493 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
11494
11495 mAdjSeq++;
11496
11497 final boolean res = updateOomAdjLocked(app, app.hiddenAdj, TOP_APP);
11498 if (res) {
11499 final boolean nowHidden = app.curAdj >= HIDDEN_APP_MIN_ADJ
11500 && app.curAdj <= HIDDEN_APP_MAX_ADJ;
11501 if (nowHidden != wasHidden) {
11502 // Changed to/from hidden state, so apps after it in the LRU
11503 // list may also be changed.
11504 updateOomAdjLocked();
11505 }
11506 }
11507 return res;
11508 }
11509
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011510 final boolean updateOomAdjLocked() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 boolean didOomAdj = true;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011512 final ActivityRecord TOP_ACT = resumedAppLocked();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011513 final ProcessRecord TOP_APP = TOP_ACT != null ? TOP_ACT.app : null;
11514
11515 if (false) {
11516 RuntimeException e = new RuntimeException();
11517 e.fillInStackTrace();
Joe Onorato8a9b2202010-02-26 18:56:32 -080011518 Slog.i(TAG, "updateOomAdj: top=" + TOP_ACT, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011519 }
11520
11521 mAdjSeq++;
11522
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080011523 // Let's determine how many processes we have running vs.
11524 // how many slots we have for background processes; we may want
11525 // to put multiple processes in a slot of there are enough of
11526 // them.
11527 int numSlots = HIDDEN_APP_MAX_ADJ - HIDDEN_APP_MIN_ADJ + 1;
11528 int factor = (mLruProcesses.size()-4)/numSlots;
11529 if (factor < 1) factor = 1;
11530 int step = 0;
Dianne Hackborn8633e682010-04-22 16:03:41 -070011531 int numHidden = 0;
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080011532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011533 // First try updating the OOM adjustment for each of the
11534 // application processes based on their current state.
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011535 int i = mLruProcesses.size();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011536 int curHiddenAdj = HIDDEN_APP_MIN_ADJ;
11537 while (i > 0) {
11538 i--;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011539 ProcessRecord app = mLruProcesses.get(i);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011540 //Slog.i(TAG, "OOM " + app + ": cur hidden=" + curHiddenAdj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011541 if (updateOomAdjLocked(app, curHiddenAdj, TOP_APP)) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011542 if (curHiddenAdj < EMPTY_APP_ADJ
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011543 && app.curAdj == curHiddenAdj) {
Dianne Hackborn5ce7d282010-02-12 19:30:02 -080011544 step++;
11545 if (step >= factor) {
11546 step = 0;
11547 curHiddenAdj++;
11548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070011550 if (app.curAdj >= HIDDEN_APP_MIN_ADJ) {
Dianne Hackborn149427c2010-04-23 14:20:03 -070011551 if (!app.killedBackground) {
11552 numHidden++;
11553 if (numHidden > MAX_HIDDEN_APPS) {
Dianne Hackborn906497c2010-05-10 15:57:38 -070011554 Slog.i(TAG, "No longer want " + app.processName
11555 + " (pid " + app.pid + "): hidden #" + numHidden);
Dianne Hackborn149427c2010-04-23 14:20:03 -070011556 EventLog.writeEvent(EventLogTags.AM_KILL, app.pid,
11557 app.processName, app.setAdj, "too many background");
11558 app.killedBackground = true;
Dianne Hackborn906497c2010-05-10 15:57:38 -070011559 Process.killProcessQuiet(app.pid);
Dianne Hackborn149427c2010-04-23 14:20:03 -070011560 }
Dianne Hackborn8633e682010-04-22 16:03:41 -070011561 }
11562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011563 } else {
11564 didOomAdj = false;
11565 }
11566 }
11567
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011568 // If we return false, we will fall back on killing processes to
11569 // have a fixed limit. Do this if a limit has been requested; else
11570 // only return false if one of the adjustments failed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011571 return ENFORCE_PROCESS_LIMIT || mProcessLimit > 0 ? false : didOomAdj;
11572 }
11573
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011574 final void trimApplications() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011575 synchronized (this) {
11576 int i;
11577
11578 // First remove any unused application processes whose package
11579 // has been removed.
11580 for (i=mRemovedProcesses.size()-1; i>=0; i--) {
11581 final ProcessRecord app = mRemovedProcesses.get(i);
11582 if (app.activities.size() == 0
11583 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011584 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011585 TAG, "Exiting empty application process "
11586 + app.processName + " ("
11587 + (app.thread != null ? app.thread.asBinder() : null)
11588 + ")\n");
11589 if (app.pid > 0 && app.pid != MY_PID) {
11590 Process.killProcess(app.pid);
11591 } else {
11592 try {
11593 app.thread.scheduleExit();
11594 } catch (Exception e) {
11595 // Ignore exceptions.
11596 }
11597 }
11598 cleanUpApplicationRecordLocked(app, false, -1);
11599 mRemovedProcesses.remove(i);
11600
11601 if (app.persistent) {
11602 if (app.persistent) {
11603 addAppLocked(app.info);
11604 }
11605 }
11606 }
11607 }
11608
11609 // Now try updating the OOM adjustment for each of the
11610 // application processes based on their current state.
11611 // If the setOomAdj() API is not supported, then go with our
11612 // back-up plan...
11613 if (!updateOomAdjLocked()) {
11614
11615 // Count how many processes are running services.
11616 int numServiceProcs = 0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011617 for (i=mLruProcesses.size()-1; i>=0; i--) {
11618 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011619
11620 if (app.persistent || app.services.size() != 0
11621 || app.curReceiver != null
11622 || app.persistentActivities > 0) {
11623 // Don't count processes holding services against our
11624 // maximum process count.
Joe Onorato8a9b2202010-02-26 18:56:32 -080011625 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011626 TAG, "Not trimming app " + app + " with services: "
11627 + app.services);
11628 numServiceProcs++;
11629 }
11630 }
11631
11632 int curMaxProcs = mProcessLimit;
11633 if (curMaxProcs <= 0) curMaxProcs = MAX_PROCESSES;
11634 if (mAlwaysFinishActivities) {
11635 curMaxProcs = 1;
11636 }
11637 curMaxProcs += numServiceProcs;
11638
11639 // Quit as many processes as we can to get down to the desired
11640 // process count. First remove any processes that no longer
11641 // have activites running in them.
11642 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011643 i<mLruProcesses.size()
11644 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011645 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011646 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011647 // Quit an application only if it is not currently
11648 // running any activities.
11649 if (!app.persistent && app.activities.size() == 0
11650 && app.curReceiver == null && app.services.size() == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080011651 Slog.i(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011652 TAG, "Exiting empty application process "
11653 + app.processName + " ("
11654 + (app.thread != null ? app.thread.asBinder() : null)
11655 + ")\n");
11656 if (app.pid > 0 && app.pid != MY_PID) {
11657 Process.killProcess(app.pid);
11658 } else {
11659 try {
11660 app.thread.scheduleExit();
11661 } catch (Exception e) {
11662 // Ignore exceptions.
11663 }
11664 }
11665 // todo: For now we assume the application is not buggy
11666 // or evil, and will quit as a result of our request.
11667 // Eventually we need to drive this off of the death
11668 // notification, and kill the process if it takes too long.
11669 cleanUpApplicationRecordLocked(app, false, i);
11670 i--;
11671 }
11672 }
11673
11674 // If we still have too many processes, now from the least
11675 // recently used process we start finishing activities.
Joe Onorato8a9b2202010-02-26 18:56:32 -080011676 if (Config.LOGV) Slog.v(
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011677 TAG, "*** NOW HAVE " + mLruProcesses.size() +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011678 " of " + curMaxProcs + " processes");
11679 for ( i=0;
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011680 i<mLruProcesses.size()
11681 && mLruProcesses.size() > curMaxProcs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011682 i++) {
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011683 final ProcessRecord app = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011684 // Quit the application only if we have a state saved for
11685 // all of its activities.
11686 boolean canQuit = !app.persistent && app.curReceiver == null
11687 && app.services.size() == 0
11688 && app.persistentActivities == 0;
11689 int NUMA = app.activities.size();
11690 int j;
Joe Onorato8a9b2202010-02-26 18:56:32 -080011691 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011692 TAG, "Looking to quit " + app.processName);
11693 for (j=0; j<NUMA && canQuit; j++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011694 ActivityRecord r = app.activities.get(j);
Joe Onorato8a9b2202010-02-26 18:56:32 -080011695 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011696 TAG, " " + r.intent.getComponent().flattenToShortString()
11697 + ": frozen=" + r.haveState + ", visible=" + r.visible);
11698 canQuit = (r.haveState || !r.stateNotNeeded)
11699 && !r.visible && r.stopped;
11700 }
11701 if (canQuit) {
11702 // Finish all of the activities, and then the app itself.
11703 for (j=0; j<NUMA; j++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011704 ActivityRecord r = app.activities.get(j);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 if (!r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011706 r.stack.destroyActivityLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011707 }
11708 r.resultTo = null;
11709 }
Joe Onorato8a9b2202010-02-26 18:56:32 -080011710 Slog.i(TAG, "Exiting application process "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011711 + app.processName + " ("
11712 + (app.thread != null ? app.thread.asBinder() : null)
11713 + ")\n");
11714 if (app.pid > 0 && app.pid != MY_PID) {
11715 Process.killProcess(app.pid);
11716 } else {
11717 try {
11718 app.thread.scheduleExit();
11719 } catch (Exception e) {
11720 // Ignore exceptions.
11721 }
11722 }
11723 // todo: For now we assume the application is not buggy
11724 // or evil, and will quit as a result of our request.
11725 // Eventually we need to drive this off of the death
11726 // notification, and kill the process if it takes too long.
11727 cleanUpApplicationRecordLocked(app, false, i);
11728 i--;
11729 //dump();
11730 }
11731 }
11732
11733 }
11734
11735 int curMaxActivities = MAX_ACTIVITIES;
11736 if (mAlwaysFinishActivities) {
11737 curMaxActivities = 1;
11738 }
11739
11740 // Finally, if there are too many activities now running, try to
11741 // finish as many as we can to get back down to the limit.
11742 for ( i=0;
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011743 i<mMainStack.mLRUActivities.size()
11744 && mMainStack.mLRUActivities.size() > curMaxActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745 i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -070011746 final ActivityRecord r
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011747 = (ActivityRecord)mMainStack.mLRUActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011748
11749 // We can finish this one if we have its icicle saved and
11750 // it is not persistent.
11751 if ((r.haveState || !r.stateNotNeeded) && !r.visible
11752 && r.stopped && !r.persistent && !r.finishing) {
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011753 final int origSize = mMainStack.mLRUActivities.size();
11754 r.stack.destroyActivityLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755
11756 // This will remove it from the LRU list, so keep
11757 // our index at the same value. Note that this check to
11758 // see if the size changes is just paranoia -- if
11759 // something unexpected happens, we don't want to end up
11760 // in an infinite loop.
Dianne Hackborn50dc3bc2010-06-25 10:05:59 -070011761 if (origSize > mMainStack.mLRUActivities.size()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011762 i--;
11763 }
11764 }
11765 }
11766 }
11767 }
11768
11769 /** This method sends the specified signal to each of the persistent apps */
11770 public void signalPersistentProcesses(int sig) throws RemoteException {
11771 if (sig != Process.SIGNAL_USR1) {
11772 throw new SecurityException("Only SIGNAL_USR1 is allowed");
11773 }
11774
11775 synchronized (this) {
11776 if (checkCallingPermission(android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES)
11777 != PackageManager.PERMISSION_GRANTED) {
11778 throw new SecurityException("Requires permission "
11779 + android.Manifest.permission.SIGNAL_PERSISTENT_PROCESSES);
11780 }
11781
Dianne Hackborndd71fc82009-12-16 19:24:32 -080011782 for (int i = mLruProcesses.size() - 1 ; i >= 0 ; i--) {
11783 ProcessRecord r = mLruProcesses.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011784 if (r.thread != null && r.persistent) {
11785 Process.sendSignal(r.pid, sig);
11786 }
11787 }
11788 }
11789 }
11790
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011791 public boolean profileControl(String process, boolean start,
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011792 String path, ParcelFileDescriptor fd) throws RemoteException {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011793
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011794 try {
11795 synchronized (this) {
11796 // note: hijacking SET_ACTIVITY_WATCHER, but should be changed to
11797 // its own permission.
11798 if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER)
11799 != PackageManager.PERMISSION_GRANTED) {
11800 throw new SecurityException("Requires permission "
11801 + android.Manifest.permission.SET_ACTIVITY_WATCHER);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011802 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011803
11804 if (start && fd == null) {
11805 throw new IllegalArgumentException("null fd");
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011806 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011807
11808 ProcessRecord proc = null;
11809 try {
11810 int pid = Integer.parseInt(process);
11811 synchronized (mPidsSelfLocked) {
11812 proc = mPidsSelfLocked.get(pid);
11813 }
11814 } catch (NumberFormatException e) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011815 }
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011816
11817 if (proc == null) {
11818 HashMap<String, SparseArray<ProcessRecord>> all
11819 = mProcessNames.getMap();
11820 SparseArray<ProcessRecord> procs = all.get(process);
11821 if (procs != null && procs.size() > 0) {
11822 proc = procs.valueAt(0);
11823 }
11824 }
11825
11826 if (proc == null || proc.thread == null) {
11827 throw new IllegalArgumentException("Unknown process: " + process);
11828 }
11829
11830 boolean isSecure = "1".equals(SystemProperties.get(SYSTEM_SECURE, "0"));
11831 if (isSecure) {
11832 if ((proc.info.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0) {
11833 throw new SecurityException("Process not debuggable: " + proc);
11834 }
11835 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011836
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011837 proc.thread.profilerControl(start, path, fd);
11838 fd = null;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011839 return true;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070011840 }
11841 } catch (RemoteException e) {
11842 throw new IllegalStateException("Process disappeared");
11843 } finally {
11844 if (fd != null) {
11845 try {
11846 fd.close();
11847 } catch (IOException e) {
11848 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -080011849 }
11850 }
11851 }
11852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011853 /** In this method we try to acquire our lock to make sure that we have not deadlocked */
11854 public void monitor() {
11855 synchronized (this) { }
11856 }
11857}