blob: 3f7490454345cffa1b89a4b5e87d44ec4d7f838d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 android.app;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
21import android.content.ComponentCallbacks;
22import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
32import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070037import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080042import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Bitmap;
44import android.graphics.Canvas;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070045import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Bundle;
47import android.os.Debug;
48import android.os.Handler;
49import android.os.IBinder;
50import android.os.Looper;
51import android.os.Message;
52import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070053import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Process;
55import android.os.RemoteException;
56import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070057import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.SystemClock;
59import android.util.AndroidRuntimeException;
60import android.util.Config;
61import android.util.DisplayMetrics;
62import android.util.EventLog;
63import android.util.Log;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080064import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070066import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.View;
68import android.view.ViewDebug;
69import android.view.ViewManager;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -080070import android.view.ViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.Window;
72import android.view.WindowManager;
73import android.view.WindowManagerImpl;
74
75import com.android.internal.os.BinderInternal;
76import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070077import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
Romain Guya14c8e02010-09-03 16:51:54 -070079import dalvik.system.VMDebug;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
81
82import java.io.File;
83import java.io.FileDescriptor;
84import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070085import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import java.io.PrintWriter;
87import java.lang.ref.WeakReference;
88import java.util.ArrayList;
89import java.util.HashMap;
90import java.util.Iterator;
91import java.util.List;
92import java.util.Locale;
93import java.util.Map;
94import java.util.TimeZone;
95import java.util.regex.Pattern;
96
Bob Leee5408332009-09-04 18:31:17 -070097import dalvik.system.SamplingProfiler;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099final class SuperNotCalledException extends AndroidRuntimeException {
100 public SuperNotCalledException(String msg) {
101 super(msg);
102 }
103}
104
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700105final class RemoteServiceException extends AndroidRuntimeException {
106 public RemoteServiceException(String msg) {
107 super(msg);
108 }
109}
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111/**
112 * This manages the execution of the main thread in an
113 * application process, scheduling and executing activities,
114 * broadcasts, and other operations on it as the activity
115 * manager requests.
116 *
117 * {@hide}
118 */
119public final class ActivityThread {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700120 static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700121 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 private static final boolean DEBUG = false;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700123 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
124 static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700125 private static final boolean DEBUG_RESULTS = false;
Christopher Tate436344a2009-09-30 16:17:37 -0700126 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700127 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
129 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
130 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
131 private static final int LOG_ON_PAUSE_CALLED = 30021;
132 private static final int LOG_ON_RESUME_CALLED = 30022;
133
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700134 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700135
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700136 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700138 final ApplicationThread mAppThread = new ApplicationThread();
139 final Looper mLooper = Looper.myLooper();
140 final H mH = new H();
141 final HashMap<IBinder, ActivityClientRecord> mActivities
142 = new HashMap<IBinder, ActivityClientRecord>();
143 // List of new activities (via ActivityRecord.nextIdle) that should
144 // be reported when next we idle.
145 ActivityClientRecord mNewActivities = null;
146 // Number of activities that are currently visible on-screen.
147 int mNumVisibleActivities = 0;
148 final HashMap<IBinder, Service> mServices
149 = new HashMap<IBinder, Service>();
150 AppBindData mBoundApplication;
151 Configuration mConfiguration;
152 Configuration mResConfiguration;
153 Application mInitialApplication;
154 final ArrayList<Application> mAllApplications
155 = new ArrayList<Application>();
156 // set of instantiated backup agents, keyed by package name
157 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700158 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 Instrumentation mInstrumentation;
160 String mInstrumentationAppDir = null;
161 String mInstrumentationAppPackage = null;
162 String mInstrumentedAppDir = null;
163 boolean mSystemThread = false;
164 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700166 // These can be accessed by multiple threads; mPackages is the lock.
167 // XXX For now we keep around information about all packages we have
168 // seen, not removing entries from this map.
169 final HashMap<String, WeakReference<LoadedApk>> mPackages
170 = new HashMap<String, WeakReference<LoadedApk>>();
171 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
172 = new HashMap<String, WeakReference<LoadedApk>>();
173 Display mDisplay = null;
174 DisplayMetrics mDisplayMetrics = null;
175 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
176 = new HashMap<ResourcesKey, WeakReference<Resources> >();
177 final ArrayList<ActivityClientRecord> mRelaunchingActivities
178 = new ArrayList<ActivityClientRecord>();
179 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 // The lock of mProviderMap protects the following variables.
182 final HashMap<String, ProviderClientRecord> mProviderMap
183 = new HashMap<String, ProviderClientRecord>();
184 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
185 = new HashMap<IBinder, ProviderRefCount>();
186 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
187 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700189 final GcIdler mGcIdler = new GcIdler();
190 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700192 static Handler sMainThreadHandler; // set once in main()
193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 private static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700196 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 Intent intent;
198 Bundle state;
199 Activity activity;
200 Window window;
201 Activity parent;
202 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700203 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 boolean paused;
205 boolean stopped;
206 boolean hideForNow;
207 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700208 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
211 ActivityInfo activityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700212 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
214 List<ResultInfo> pendingResults;
215 List<Intent> pendingIntents;
216
217 boolean startsNotResumed;
218 boolean isForward;
219
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700220 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 parent = null;
222 embeddedID = null;
223 paused = false;
224 stopped = false;
225 hideForNow = false;
226 nextIdle = null;
227 }
228
229 public String toString() {
230 ComponentName componentName = intent.getComponent();
231 return "ActivityRecord{"
232 + Integer.toHexString(System.identityHashCode(this))
233 + " token=" + token + " " + (componentName == null
234 ? "no component name" : componentName.toShortString())
235 + "}";
236 }
237 }
238
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 private final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 final String mName;
241 final IContentProvider mProvider;
242 final ContentProvider mLocalProvider;
243
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700244 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 ContentProvider localProvider) {
246 mName = name;
247 mProvider = provider;
248 mLocalProvider = localProvider;
249 }
250
251 public void binderDied() {
252 removeDeadProvider(mName, mProvider);
253 }
254 }
255
256 private static final class NewIntentData {
257 List<Intent> intents;
258 IBinder token;
259 public String toString() {
260 return "NewIntentData{intents=" + intents + " token=" + token + "}";
261 }
262 }
263
264 private static final class ReceiverData {
265 Intent intent;
266 ActivityInfo info;
267 int resultCode;
268 String resultData;
269 Bundle resultExtras;
270 boolean sync;
271 boolean resultAbort;
272 public String toString() {
273 return "ReceiverData{intent=" + intent + " packageName=" +
274 info.packageName + " resultCode=" + resultCode
275 + " resultData=" + resultData + " resultExtras=" + resultExtras + "}";
276 }
277 }
278
Christopher Tate181fafa2009-05-14 11:12:14 -0700279 private static final class CreateBackupAgentData {
280 ApplicationInfo appInfo;
281 int backupMode;
282 public String toString() {
283 return "CreateBackupAgentData{appInfo=" + appInfo
284 + " backupAgent=" + appInfo.backupAgentName
285 + " mode=" + backupMode + "}";
286 }
287 }
Bob Leee5408332009-09-04 18:31:17 -0700288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 private static final class CreateServiceData {
290 IBinder token;
291 ServiceInfo info;
292 Intent intent;
293 public String toString() {
294 return "CreateServiceData{token=" + token + " className="
295 + info.name + " packageName=" + info.packageName
296 + " intent=" + intent + "}";
297 }
298 }
299
300 private static final class BindServiceData {
301 IBinder token;
302 Intent intent;
303 boolean rebind;
304 public String toString() {
305 return "BindServiceData{token=" + token + " intent=" + intent + "}";
306 }
307 }
308
309 private static final class ServiceArgsData {
310 IBinder token;
311 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700312 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 Intent args;
314 public String toString() {
315 return "ServiceArgsData{token=" + token + " startId=" + startId
316 + " args=" + args + "}";
317 }
318 }
319
320 private static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700321 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 String processName;
323 ApplicationInfo appInfo;
324 List<ProviderInfo> providers;
325 ComponentName instrumentationName;
326 String profileFile;
327 Bundle instrumentationArgs;
328 IInstrumentationWatcher instrumentationWatcher;
329 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700330 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 Configuration config;
332 boolean handlingProfiling;
333 public String toString() {
334 return "AppBindData{appInfo=" + appInfo + "}";
335 }
336 }
337
Dianne Hackborn625ac272010-09-17 18:29:22 -0700338 private static final class DumpComponentInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 FileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700340 IBinder token;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 String[] args;
342 boolean dumped;
343 }
344
345 private static final class ResultData {
346 IBinder token;
347 List<ResultInfo> results;
348 public String toString() {
349 return "ResultData{token=" + token + " results" + results + "}";
350 }
351 }
352
353 private static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800354 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 String what;
356 String who;
357 }
358
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700359 private static final class ProfilerControlData {
360 String path;
361 ParcelFileDescriptor fd;
362 }
363
Andy McFadden824c5102010-07-09 16:26:57 -0700364 private static final class DumpHeapData {
365 String path;
366 ParcelFileDescriptor fd;
367 }
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 private final class ApplicationThread extends ApplicationThreadNative {
370 private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
371 private static final String ONE_COUNT_COLUMN = "%17s %8d";
372 private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700373 private static final String TWO_COUNT_COLUMNS_DB = "%20s %8d %20s %8d";
374 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 // Formatting for checkin service - update version if row format changes
377 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 public final void schedulePauseActivity(IBinder token, boolean finished,
380 boolean userLeaving, int configChanges) {
381 queueOrSendMessage(
382 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
383 token,
384 (userLeaving ? 1 : 0),
385 configChanges);
386 }
387
388 public final void scheduleStopActivity(IBinder token, boolean showWindow,
389 int configChanges) {
390 queueOrSendMessage(
391 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
392 token, 0, configChanges);
393 }
394
395 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
396 queueOrSendMessage(
397 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
398 token);
399 }
400
401 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
402 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
403 }
404
405 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
406 ResultData res = new ResultData();
407 res.token = token;
408 res.results = results;
409 queueOrSendMessage(H.SEND_RESULT, res);
410 }
411
412 // we use token to identify this activity without having to send the
413 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700414 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
416 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700417 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
419 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700420 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 r.intent = intent;
422 r.activityInfo = info;
423 r.state = state;
424
425 r.pendingResults = pendingResults;
426 r.pendingIntents = pendingNewIntents;
427
428 r.startsNotResumed = notResumed;
429 r.isForward = isForward;
430
431 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
432 }
433
434 public final void scheduleRelaunchActivity(IBinder token,
435 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800436 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700437 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438
439 r.token = token;
440 r.pendingResults = pendingResults;
441 r.pendingIntents = pendingNewIntents;
442 r.startsNotResumed = notResumed;
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800443 r.createdConfig = config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800445 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 mRelaunchingActivities.add(r);
447 }
Bob Leee5408332009-09-04 18:31:17 -0700448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 queueOrSendMessage(H.RELAUNCH_ACTIVITY, r, configChanges);
450 }
451
452 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
453 NewIntentData data = new NewIntentData();
454 data.intents = intents;
455 data.token = token;
456
457 queueOrSendMessage(H.NEW_INTENT, data);
458 }
459
460 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
461 int configChanges) {
462 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
463 configChanges);
464 }
465
466 public final void scheduleReceiver(Intent intent, ActivityInfo info,
467 int resultCode, String data, Bundle extras, boolean sync) {
468 ReceiverData r = new ReceiverData();
469
470 r.intent = intent;
471 r.info = info;
472 r.resultCode = resultCode;
473 r.resultData = data;
474 r.resultExtras = extras;
475 r.sync = sync;
476
477 queueOrSendMessage(H.RECEIVER, r);
478 }
479
Christopher Tate181fafa2009-05-14 11:12:14 -0700480 public final void scheduleCreateBackupAgent(ApplicationInfo app, int backupMode) {
481 CreateBackupAgentData d = new CreateBackupAgentData();
482 d.appInfo = app;
483 d.backupMode = backupMode;
484
485 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
486 }
487
488 public final void scheduleDestroyBackupAgent(ApplicationInfo app) {
489 CreateBackupAgentData d = new CreateBackupAgentData();
490 d.appInfo = app;
491
492 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
493 }
494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 public final void scheduleCreateService(IBinder token,
496 ServiceInfo info) {
497 CreateServiceData s = new CreateServiceData();
498 s.token = token;
499 s.info = info;
500
501 queueOrSendMessage(H.CREATE_SERVICE, s);
502 }
503
504 public final void scheduleBindService(IBinder token, Intent intent,
505 boolean rebind) {
506 BindServiceData s = new BindServiceData();
507 s.token = token;
508 s.intent = intent;
509 s.rebind = rebind;
510
511 queueOrSendMessage(H.BIND_SERVICE, s);
512 }
513
514 public final void scheduleUnbindService(IBinder token, Intent intent) {
515 BindServiceData s = new BindServiceData();
516 s.token = token;
517 s.intent = intent;
518
519 queueOrSendMessage(H.UNBIND_SERVICE, s);
520 }
521
522 public final void scheduleServiceArgs(IBinder token, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700523 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 ServiceArgsData s = new ServiceArgsData();
525 s.token = token;
526 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700527 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 s.args = args;
529
530 queueOrSendMessage(H.SERVICE_ARGS, s);
531 }
532
533 public final void scheduleStopService(IBinder token) {
534 queueOrSendMessage(H.STOP_SERVICE, token);
535 }
536
537 public final void bindApplication(String processName,
538 ApplicationInfo appInfo, List<ProviderInfo> providers,
539 ComponentName instrumentationName, String profileFile,
540 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700541 int debugMode, boolean isRestrictedBackupMode, Configuration config,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 Map<String, IBinder> services) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
544 if (services != null) {
545 // Setup the service cache in the ServiceManager
546 ServiceManager.initServiceCache(services);
547 }
548
549 AppBindData data = new AppBindData();
550 data.processName = processName;
551 data.appInfo = appInfo;
552 data.providers = providers;
553 data.instrumentationName = instrumentationName;
554 data.profileFile = profileFile;
555 data.instrumentationArgs = instrumentationArgs;
556 data.instrumentationWatcher = instrumentationWatcher;
557 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700558 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 data.config = config;
560 queueOrSendMessage(H.BIND_APPLICATION, data);
561 }
562
563 public final void scheduleExit() {
564 queueOrSendMessage(H.EXIT_APPLICATION, null);
565 }
566
Christopher Tate5e1ab332009-09-01 20:32:49 -0700567 public final void scheduleSuicide() {
568 queueOrSendMessage(H.SUICIDE, null);
569 }
570
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 public void requestThumbnail(IBinder token) {
572 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
573 }
574
575 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800576 synchronized (mPackages) {
577 if (mPendingConfiguration == null ||
578 mPendingConfiguration.isOtherSeqNewer(config)) {
579 mPendingConfiguration = config;
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 }
582 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
583 }
584
585 public void updateTimeZone() {
586 TimeZone.setDefault(null);
587 }
588
589 public void processInBackground() {
590 mH.removeMessages(H.GC_WHEN_IDLE);
591 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
592 }
593
594 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700595 DumpComponentInfo data = new DumpComponentInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 data.fd = fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700597 data.token = servicetoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 data.args = args;
599 data.dumped = false;
600 queueOrSendMessage(H.DUMP_SERVICE, data);
601 synchronized (data) {
602 while (!data.dumped) {
603 try {
604 data.wait();
605 } catch (InterruptedException e) {
606 // no need to do anything here, we will keep waiting until
607 // dumped is set
608 }
609 }
610 }
611 }
612
613 // This function exists to make sure all receiver dispatching is
614 // correctly ordered, since these are one-way calls and the binder driver
615 // applies transaction ordering per object for such calls.
616 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700617 int resultCode, String dataStr, Bundle extras, boolean ordered,
618 boolean sticky) throws RemoteException {
619 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 }
Bob Leee5408332009-09-04 18:31:17 -0700621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public void scheduleLowMemory() {
623 queueOrSendMessage(H.LOW_MEMORY, null);
624 }
625
626 public void scheduleActivityConfigurationChanged(IBinder token) {
627 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
628 }
629
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700630 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
631 ProfilerControlData pcd = new ProfilerControlData();
632 pcd.path = path;
633 pcd.fd = fd;
634 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800635 }
636
Andy McFadden824c5102010-07-09 16:26:57 -0700637 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
638 DumpHeapData dhd = new DumpHeapData();
639 dhd.path = path;
640 dhd.fd = fd;
641 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
642 }
643
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700644 public void setSchedulingGroup(int group) {
645 // Note: do this immediately, since going into the foreground
646 // should happen regardless of what pending work we have to do
647 // and the activity manager will wait for us to report back that
648 // we are done before sending us to the background.
649 try {
650 Process.setProcessGroup(Process.myPid(), group);
651 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800652 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700653 }
654 }
Bob Leee5408332009-09-04 18:31:17 -0700655
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700656 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
657 Debug.getMemoryInfo(outInfo);
658 }
Bob Leee5408332009-09-04 18:31:17 -0700659
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700660 public void dispatchPackageBroadcast(int cmd, String[] packages) {
661 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
662 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700663
664 public void scheduleCrash(String msg) {
665 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
666 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700667
668 public void dumpActivity(FileDescriptor fd, IBinder activitytoken, String[] args) {
669 DumpComponentInfo data = new DumpComponentInfo();
670 data.fd = fd;
671 data.token = activitytoken;
672 data.args = args;
673 data.dumped = false;
674 queueOrSendMessage(H.DUMP_ACTIVITY, data);
675 synchronized (data) {
676 while (!data.dumped) {
677 try {
678 data.wait();
679 } catch (InterruptedException e) {
680 // no need to do anything here, we will keep waiting until
681 // dumped is set
682 }
683 }
684 }
685 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 @Override
688 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
689 long nativeMax = Debug.getNativeHeapSize() / 1024;
690 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
691 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
692
693 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
694 Debug.getMemoryInfo(memInfo);
695
696 final int nativeShared = memInfo.nativeSharedDirty;
697 final int dalvikShared = memInfo.dalvikSharedDirty;
698 final int otherShared = memInfo.otherSharedDirty;
699
700 final int nativePrivate = memInfo.nativePrivateDirty;
701 final int dalvikPrivate = memInfo.dalvikPrivateDirty;
702 final int otherPrivate = memInfo.otherPrivateDirty;
703
704 Runtime runtime = Runtime.getRuntime();
705
706 long dalvikMax = runtime.totalMemory() / 1024;
707 long dalvikFree = runtime.freeMemory() / 1024;
708 long dalvikAllocated = dalvikMax - dalvikFree;
709 long viewInstanceCount = ViewDebug.getViewInstanceCount();
710 long viewRootInstanceCount = ViewDebug.getViewRootInstanceCount();
Romain Guya14c8e02010-09-03 16:51:54 -0700711 long appContextInstanceCount = VMDebug.countInstancesOfClass(ContextImpl.class);
712 long activityInstanceCount = VMDebug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 int globalAssetCount = AssetManager.getGlobalAssetCount();
714 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
715 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
716 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
717 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
718 int openSslSocketCount = OpenSSLSocketImpl.getInstanceCount();
719 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800720 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 // Check to see if we were called by checkin server. If so, print terse format.
723 boolean doCheckinFormat = false;
724 if (args != null) {
725 for (String arg : args) {
726 if ("-c".equals(arg)) doCheckinFormat = true;
727 }
728 }
Bob Leee5408332009-09-04 18:31:17 -0700729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 // For checkin, we print one long comma-separated list of values
731 if (doCheckinFormat) {
732 // NOTE: if you change anything significant below, also consider changing
733 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700734 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 // Header
738 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
739 pw.print(Process.myPid()); pw.print(',');
740 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 // Heap info - max
743 pw.print(nativeMax); pw.print(',');
744 pw.print(dalvikMax); pw.print(',');
745 pw.print("N/A,");
746 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 // Heap info - allocated
749 pw.print(nativeAllocated); pw.print(',');
750 pw.print(dalvikAllocated); pw.print(',');
751 pw.print("N/A,");
752 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 // Heap info - free
755 pw.print(nativeFree); pw.print(',');
756 pw.print(dalvikFree); pw.print(',');
757 pw.print("N/A,");
758 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 // Heap info - proportional set size
761 pw.print(memInfo.nativePss); pw.print(',');
762 pw.print(memInfo.dalvikPss); pw.print(',');
763 pw.print(memInfo.otherPss); pw.print(',');
764 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 // Heap info - shared
Bob Leee5408332009-09-04 18:31:17 -0700767 pw.print(nativeShared); pw.print(',');
768 pw.print(dalvikShared); pw.print(',');
769 pw.print(otherShared); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 pw.print(nativeShared + dalvikShared + otherShared); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 // Heap info - private
Bob Leee5408332009-09-04 18:31:17 -0700773 pw.print(nativePrivate); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 pw.print(dalvikPrivate); pw.print(',');
775 pw.print(otherPrivate); pw.print(',');
776 pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 // Object counts
779 pw.print(viewInstanceCount); pw.print(',');
780 pw.print(viewRootInstanceCount); pw.print(',');
781 pw.print(appContextInstanceCount); pw.print(',');
782 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 pw.print(globalAssetCount); pw.print(',');
785 pw.print(globalAssetManagerCount); pw.print(',');
786 pw.print(binderLocalObjectCount); pw.print(',');
787 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 pw.print(binderDeathObjectCount); pw.print(',');
790 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 // SQL
793 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800794 pw.print(stats.memoryUsed / 1024); pw.print(',');
795 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
796 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
797 for (int i = 0; i < stats.dbStats.size(); i++) {
798 DbStats dbStats = stats.dbStats.get(i);
799 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700800 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800801 pw.print(',');
802 }
Bob Leee5408332009-09-04 18:31:17 -0700803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 return;
805 }
Bob Leee5408332009-09-04 18:31:17 -0700806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 // otherwise, show human-readable format
808 printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total");
809 printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
810 printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
811 nativeAllocated + dalvikAllocated);
812 printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A",
813 nativeFree + dalvikFree);
814
815 printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
816 memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
817
818 printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared,
819 nativeShared + dalvikShared + otherShared);
820 printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
821 nativePrivate + dalvikPrivate + otherPrivate);
822
823 pw.println(" ");
824 pw.println(" Objects");
825 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRoots:",
826 viewRootInstanceCount);
827
828 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
829 "Activities:", activityInstanceCount);
830
831 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
832 "AssetManagers:", globalAssetManagerCount);
833
834 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
835 "Proxy Binders:", binderProxyObjectCount);
836 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
837
838 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 // SQLite mem info
841 pw.println(" ");
842 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700843 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800844 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700845 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
846 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800847 pw.println(" ");
848 int N = stats.dbStats.size();
849 if (N > 0) {
850 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700851 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
852 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800853 for (int i = 0; i < N; i++) {
854 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700855 printRow(pw, DB_INFO_FORMAT,
856 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
857 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
858 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
859 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800860 }
861 }
Bob Leee5408332009-09-04 18:31:17 -0700862
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700863 // Asset details.
864 String assetAlloc = AssetManager.getAssetAllocations();
865 if (assetAlloc != null) {
866 pw.println(" ");
867 pw.println(" Asset Allocations");
868 pw.print(assetAlloc);
869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 }
871
872 private void printRow(PrintWriter pw, String format, Object...objs) {
873 pw.println(String.format(format, objs));
874 }
875 }
876
877 private final class H extends Handler {
878 public static final int LAUNCH_ACTIVITY = 100;
879 public static final int PAUSE_ACTIVITY = 101;
880 public static final int PAUSE_ACTIVITY_FINISHING= 102;
881 public static final int STOP_ACTIVITY_SHOW = 103;
882 public static final int STOP_ACTIVITY_HIDE = 104;
883 public static final int SHOW_WINDOW = 105;
884 public static final int HIDE_WINDOW = 106;
885 public static final int RESUME_ACTIVITY = 107;
886 public static final int SEND_RESULT = 108;
887 public static final int DESTROY_ACTIVITY = 109;
888 public static final int BIND_APPLICATION = 110;
889 public static final int EXIT_APPLICATION = 111;
890 public static final int NEW_INTENT = 112;
891 public static final int RECEIVER = 113;
892 public static final int CREATE_SERVICE = 114;
893 public static final int SERVICE_ARGS = 115;
894 public static final int STOP_SERVICE = 116;
895 public static final int REQUEST_THUMBNAIL = 117;
896 public static final int CONFIGURATION_CHANGED = 118;
897 public static final int CLEAN_UP_CONTEXT = 119;
898 public static final int GC_WHEN_IDLE = 120;
899 public static final int BIND_SERVICE = 121;
900 public static final int UNBIND_SERVICE = 122;
901 public static final int DUMP_SERVICE = 123;
902 public static final int LOW_MEMORY = 124;
903 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
904 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800905 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700906 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700907 public static final int DESTROY_BACKUP_AGENT = 129;
908 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700909 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800910 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700911 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700912 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700913 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700914 public static final int DUMP_ACTIVITY = 136;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 String codeToString(int code) {
916 if (localLOGV) {
917 switch (code) {
918 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
919 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
920 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
921 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
922 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
923 case SHOW_WINDOW: return "SHOW_WINDOW";
924 case HIDE_WINDOW: return "HIDE_WINDOW";
925 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
926 case SEND_RESULT: return "SEND_RESULT";
927 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
928 case BIND_APPLICATION: return "BIND_APPLICATION";
929 case EXIT_APPLICATION: return "EXIT_APPLICATION";
930 case NEW_INTENT: return "NEW_INTENT";
931 case RECEIVER: return "RECEIVER";
932 case CREATE_SERVICE: return "CREATE_SERVICE";
933 case SERVICE_ARGS: return "SERVICE_ARGS";
934 case STOP_SERVICE: return "STOP_SERVICE";
935 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
936 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
937 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
938 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
939 case BIND_SERVICE: return "BIND_SERVICE";
940 case UNBIND_SERVICE: return "UNBIND_SERVICE";
941 case DUMP_SERVICE: return "DUMP_SERVICE";
942 case LOW_MEMORY: return "LOW_MEMORY";
943 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
944 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800945 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -0700946 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
947 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -0700948 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700949 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800950 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700951 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700952 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -0700953 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -0700954 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 }
956 }
957 return "(unknown)";
958 }
959 public void handleMessage(Message msg) {
960 switch (msg.what) {
961 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700962 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963
964 r.packageInfo = getPackageInfoNoCheck(
965 r.activityInfo.applicationInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -0700966 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 } break;
968 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700969 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 handleRelaunchActivity(r, msg.arg1);
971 } break;
972 case PAUSE_ACTIVITY:
973 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -0700974 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 break;
976 case PAUSE_ACTIVITY_FINISHING:
977 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
978 break;
979 case STOP_ACTIVITY_SHOW:
980 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
981 break;
982 case STOP_ACTIVITY_HIDE:
983 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
984 break;
985 case SHOW_WINDOW:
986 handleWindowVisibility((IBinder)msg.obj, true);
987 break;
988 case HIDE_WINDOW:
989 handleWindowVisibility((IBinder)msg.obj, false);
990 break;
991 case RESUME_ACTIVITY:
992 handleResumeActivity((IBinder)msg.obj, true,
993 msg.arg1 != 0);
994 break;
995 case SEND_RESULT:
996 handleSendResult((ResultData)msg.obj);
997 break;
998 case DESTROY_ACTIVITY:
999 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1000 msg.arg2, false);
1001 break;
1002 case BIND_APPLICATION:
1003 AppBindData data = (AppBindData)msg.obj;
1004 handleBindApplication(data);
1005 break;
1006 case EXIT_APPLICATION:
1007 if (mInitialApplication != null) {
1008 mInitialApplication.onTerminate();
1009 }
1010 Looper.myLooper().quit();
1011 break;
1012 case NEW_INTENT:
1013 handleNewIntent((NewIntentData)msg.obj);
1014 break;
1015 case RECEIVER:
1016 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001017 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 break;
1019 case CREATE_SERVICE:
1020 handleCreateService((CreateServiceData)msg.obj);
1021 break;
1022 case BIND_SERVICE:
1023 handleBindService((BindServiceData)msg.obj);
1024 break;
1025 case UNBIND_SERVICE:
1026 handleUnbindService((BindServiceData)msg.obj);
1027 break;
1028 case SERVICE_ARGS:
1029 handleServiceArgs((ServiceArgsData)msg.obj);
1030 break;
1031 case STOP_SERVICE:
1032 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001033 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 break;
1035 case REQUEST_THUMBNAIL:
1036 handleRequestThumbnail((IBinder)msg.obj);
1037 break;
1038 case CONFIGURATION_CHANGED:
1039 handleConfigurationChanged((Configuration)msg.obj);
1040 break;
1041 case CLEAN_UP_CONTEXT:
1042 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1043 cci.context.performFinalCleanup(cci.who, cci.what);
1044 break;
1045 case GC_WHEN_IDLE:
1046 scheduleGcIdler();
1047 break;
1048 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001049 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 break;
1051 case LOW_MEMORY:
1052 handleLowMemory();
1053 break;
1054 case ACTIVITY_CONFIGURATION_CHANGED:
1055 handleActivityConfigurationChanged((IBinder)msg.obj);
1056 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001057 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001058 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001059 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001060 case CREATE_BACKUP_AGENT:
1061 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1062 break;
1063 case DESTROY_BACKUP_AGENT:
1064 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1065 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001066 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001067 Process.killProcess(Process.myPid());
1068 break;
1069 case REMOVE_PROVIDER:
1070 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001071 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001072 case ENABLE_JIT:
1073 ensureJitEnabled();
1074 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001075 case DISPATCH_PACKAGE_BROADCAST:
1076 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1077 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001078 case SCHEDULE_CRASH:
1079 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001080 case DUMP_HEAP:
1081 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1082 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001083 case DUMP_ACTIVITY:
1084 handleDumpActivity((DumpComponentInfo)msg.obj);
1085 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 }
1087 }
Bob Leee5408332009-09-04 18:31:17 -07001088
1089 void maybeSnapshot() {
1090 if (mBoundApplication != null) {
Sen Hubde75702010-05-28 01:54:03 -07001091 // convert the *private* ActivityThread.PackageInfo to *public* known
1092 // android.content.pm.PackageInfo
1093 String packageName = mBoundApplication.info.mPackageName;
1094 android.content.pm.PackageInfo packageInfo = null;
1095 try {
1096 Context context = getSystemContext();
1097 if(context == null) {
1098 Log.e(TAG, "cannot get a valid context");
1099 return;
1100 }
1101 PackageManager pm = context.getPackageManager();
1102 if(pm == null) {
1103 Log.e(TAG, "cannot get a valid PackageManager");
1104 return;
1105 }
1106 packageInfo = pm.getPackageInfo(
1107 packageName, PackageManager.GET_ACTIVITIES);
1108 } catch (NameNotFoundException e) {
1109 Log.e(TAG, "cannot get package info for " + packageName, e);
1110 }
1111 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001112 }
1113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115
1116 private final class Idler implements MessageQueue.IdleHandler {
1117 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001118 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 if (a != null) {
1120 mNewActivities = null;
1121 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001122 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001124 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 TAG, "Reporting idle of " + a +
1126 " finished=" +
1127 (a.activity != null ? a.activity.mFinished : false));
1128 if (a.activity != null && !a.activity.mFinished) {
1129 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001130 am.activityIdle(a.token, a.createdConfig);
1131 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 } catch (RemoteException ex) {
1133 }
1134 }
1135 prev = a;
1136 a = a.nextIdle;
1137 prev.nextIdle = null;
1138 } while (a != null);
1139 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001140 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 return false;
1142 }
1143 }
1144
1145 final class GcIdler implements MessageQueue.IdleHandler {
1146 public final boolean queueIdle() {
1147 doGcIfNeeded();
1148 return false;
1149 }
1150 }
1151
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001152 private final static class ResourcesKey {
1153 final private String mResDir;
1154 final private float mScale;
1155 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001156
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001157 ResourcesKey(String resDir, float scale) {
1158 mResDir = resDir;
1159 mScale = scale;
1160 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1161 }
Bob Leee5408332009-09-04 18:31:17 -07001162
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001163 @Override
1164 public int hashCode() {
1165 return mHash;
1166 }
1167
1168 @Override
1169 public boolean equals(Object obj) {
1170 if (!(obj instanceof ResourcesKey)) {
1171 return false;
1172 }
1173 ResourcesKey peer = (ResourcesKey) obj;
1174 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1175 }
1176 }
1177
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001178 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001179 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001180 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001182 public static final String currentPackageName() {
1183 ActivityThread am = currentActivityThread();
1184 return (am != null && am.mBoundApplication != null)
1185 ? am.mBoundApplication.processName : null;
1186 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001188 public static final Application currentApplication() {
1189 ActivityThread am = currentActivityThread();
1190 return am != null ? am.mInitialApplication : null;
1191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001193 public static IPackageManager getPackageManager() {
1194 if (sPackageManager != null) {
1195 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1196 return sPackageManager;
1197 }
1198 IBinder b = ServiceManager.getService("package");
1199 //Slog.v("PackageManager", "default service binder = " + b);
1200 sPackageManager = IPackageManager.Stub.asInterface(b);
1201 //Slog.v("PackageManager", "default service = " + sPackageManager);
1202 return sPackageManager;
1203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001205 DisplayMetrics getDisplayMetricsLocked(boolean forceUpdate) {
1206 if (mDisplayMetrics != null && !forceUpdate) {
1207 return mDisplayMetrics;
1208 }
1209 if (mDisplay == null) {
1210 WindowManager wm = WindowManagerImpl.getDefault();
1211 mDisplay = wm.getDefaultDisplay();
1212 }
1213 DisplayMetrics metrics = mDisplayMetrics = new DisplayMetrics();
1214 mDisplay.getMetrics(metrics);
1215 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1216 // + metrics.heightPixels + " den=" + metrics.density
1217 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
1218 return metrics;
1219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001221 /**
1222 * Creates the top level Resources for applications with the given compatibility info.
1223 *
1224 * @param resDir the resource directory.
1225 * @param compInfo the compability info. It will use the default compatibility info when it's
1226 * null.
1227 */
1228 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1229 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1230 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001232 // Resources is app scale dependent.
1233 if (false) {
1234 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1235 + compInfo.applicationScale);
1236 }
1237 WeakReference<Resources> wr = mActiveResources.get(key);
1238 r = wr != null ? wr.get() : null;
1239 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1240 if (r != null && r.getAssets().isUpToDate()) {
1241 if (false) {
1242 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1243 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1244 }
1245 return r;
1246 }
1247 }
1248
1249 //if (r != null) {
1250 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1251 // + r + " " + resDir);
1252 //}
1253
1254 AssetManager assets = new AssetManager();
1255 if (assets.addAssetPath(resDir) == 0) {
1256 return null;
1257 }
1258
1259 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
1260 DisplayMetrics metrics = getDisplayMetricsLocked(false);
1261 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1262 if (false) {
1263 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1264 + r.getConfiguration() + " appScale="
1265 + r.getCompatibilityInfo().applicationScale);
1266 }
1267
1268 synchronized (mPackages) {
1269 WeakReference<Resources> wr = mActiveResources.get(key);
1270 Resources existing = wr != null ? wr.get() : null;
1271 if (existing != null && existing.getAssets().isUpToDate()) {
1272 // Someone else already created the resources while we were
1273 // unlocked; go ahead and use theirs.
1274 r.getAssets().close();
1275 return existing;
1276 }
1277
1278 // XXX need to remove entries when weak references go away
1279 mActiveResources.put(key, new WeakReference<Resources>(r));
1280 return r;
1281 }
1282 }
1283
1284 /**
1285 * Creates the top level resources for the given package.
1286 */
1287 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
1288 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo);
1289 }
1290
1291 final Handler getHandler() {
1292 return mH;
1293 }
1294
1295 public final LoadedApk getPackageInfo(String packageName, int flags) {
1296 synchronized (mPackages) {
1297 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1299 ref = mPackages.get(packageName);
1300 } else {
1301 ref = mResourcePackages.get(packageName);
1302 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001303 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001304 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001305 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1306 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (packageInfo != null && (packageInfo.mResources == null
1308 || packageInfo.mResources.getAssets().isUpToDate())) {
1309 if (packageInfo.isSecurityViolation()
1310 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1311 throw new SecurityException(
1312 "Requesting code from " + packageName
1313 + " to be run in process "
1314 + mBoundApplication.processName
1315 + "/" + mBoundApplication.appInfo.uid);
1316 }
1317 return packageInfo;
1318 }
1319 }
1320
1321 ApplicationInfo ai = null;
1322 try {
1323 ai = getPackageManager().getApplicationInfo(packageName,
1324 PackageManager.GET_SHARED_LIBRARY_FILES);
1325 } catch (RemoteException e) {
1326 }
1327
1328 if (ai != null) {
1329 return getPackageInfo(ai, flags);
1330 }
1331
1332 return null;
1333 }
1334
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001335 public final LoadedApk getPackageInfo(ApplicationInfo ai, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1337 boolean securityViolation = includeCode && ai.uid != 0
1338 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1339 ? ai.uid != mBoundApplication.appInfo.uid : true);
1340 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1341 |Context.CONTEXT_IGNORE_SECURITY))
1342 == Context.CONTEXT_INCLUDE_CODE) {
1343 if (securityViolation) {
1344 String msg = "Requesting code from " + ai.packageName
1345 + " (with uid " + ai.uid + ")";
1346 if (mBoundApplication != null) {
1347 msg = msg + " to be run in process "
1348 + mBoundApplication.processName + " (with uid "
1349 + mBoundApplication.appInfo.uid + ")";
1350 }
1351 throw new SecurityException(msg);
1352 }
1353 }
1354 return getPackageInfo(ai, null, securityViolation, includeCode);
1355 }
1356
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001357 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 return getPackageInfo(ai, null, false, true);
1359 }
1360
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001361 private final LoadedApk getPackageInfo(ApplicationInfo aInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1363 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001364 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 if (includeCode) {
1366 ref = mPackages.get(aInfo.packageName);
1367 } else {
1368 ref = mResourcePackages.get(aInfo.packageName);
1369 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001370 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 if (packageInfo == null || (packageInfo.mResources != null
1372 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001373 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 : "Loading resource-only package ") + aInfo.packageName
1375 + " (in " + (mBoundApplication != null
1376 ? mBoundApplication.processName : null)
1377 + ")");
1378 packageInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001379 new LoadedApk(this, aInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 securityViolation, includeCode &&
1381 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1382 if (includeCode) {
1383 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001384 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 } else {
1386 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001387 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 }
1390 return packageInfo;
1391 }
1392 }
1393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 ActivityThread() {
1395 }
1396
1397 public ApplicationThread getApplicationThread()
1398 {
1399 return mAppThread;
1400 }
1401
1402 public Instrumentation getInstrumentation()
1403 {
1404 return mInstrumentation;
1405 }
1406
1407 public Configuration getConfiguration() {
1408 return mConfiguration;
1409 }
1410
1411 public boolean isProfiling() {
1412 return mBoundApplication != null && mBoundApplication.profileFile != null;
1413 }
1414
1415 public String getProfileFilePath() {
1416 return mBoundApplication.profileFile;
1417 }
1418
1419 public Looper getLooper() {
1420 return mLooper;
1421 }
1422
1423 public Application getApplication() {
1424 return mInitialApplication;
1425 }
Bob Leee5408332009-09-04 18:31:17 -07001426
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001427 public String getProcessName() {
1428 return mBoundApplication.processName;
1429 }
Bob Leee5408332009-09-04 18:31:17 -07001430
Dianne Hackborn21556372010-02-04 16:34:40 -08001431 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 synchronized (this) {
1433 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001434 ContextImpl context =
1435 ContextImpl.createSystemContext(this);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001436 LoadedApk info = new LoadedApk(this, "android", context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 context.init(info, null, this);
1438 context.getResources().updateConfiguration(
1439 getConfiguration(), getDisplayMetricsLocked(false));
1440 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001441 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 // + ": " + context.getResources().getConfiguration());
1443 }
1444 }
1445 return mSystemContext;
1446 }
1447
Mike Cleron432b7132009-09-24 15:28:29 -07001448 public void installSystemApplicationInfo(ApplicationInfo info) {
1449 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001450 ContextImpl context = getSystemContext();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001451 context.init(new LoadedApk(this, "android", context, info), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001452 }
1453 }
1454
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001455 void ensureJitEnabled() {
1456 if (!mJitEnabled) {
1457 mJitEnabled = true;
1458 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1459 }
1460 }
1461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 void scheduleGcIdler() {
1463 if (!mGcIdlerScheduled) {
1464 mGcIdlerScheduled = true;
1465 Looper.myQueue().addIdleHandler(mGcIdler);
1466 }
1467 mH.removeMessages(H.GC_WHEN_IDLE);
1468 }
1469
1470 void unscheduleGcIdler() {
1471 if (mGcIdlerScheduled) {
1472 mGcIdlerScheduled = false;
1473 Looper.myQueue().removeIdleHandler(mGcIdler);
1474 }
1475 mH.removeMessages(H.GC_WHEN_IDLE);
1476 }
1477
1478 void doGcIfNeeded() {
1479 mGcIdlerScheduled = false;
1480 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001481 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 // + "m now=" + now);
1483 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001484 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 BinderInternal.forceGc("bg");
1486 }
1487 }
1488
1489 public final ActivityInfo resolveActivityInfo(Intent intent) {
1490 ActivityInfo aInfo = intent.resolveActivityInfo(
1491 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1492 if (aInfo == null) {
1493 // Throw an exception.
1494 Instrumentation.checkStartActivityResult(
1495 IActivityManager.START_CLASS_NOT_FOUND, intent);
1496 }
1497 return aInfo;
1498 }
Bob Leee5408332009-09-04 18:31:17 -07001499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001502 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001503 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001505 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 r.intent = intent;
1507 r.state = state;
1508 r.parent = parent;
1509 r.embeddedID = id;
1510 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001511 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 if (localLOGV) {
1513 ComponentName compname = intent.getComponent();
1514 String name;
1515 if (compname != null) {
1516 name = compname.toShortString();
1517 } else {
1518 name = "(Intent " + intent + ").getComponent() returned null";
1519 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001520 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 + ", comp=" + name
1522 + ", token=" + token);
1523 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001524 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
1526
1527 public final Activity getActivity(IBinder token) {
1528 return mActivities.get(token).activity;
1529 }
1530
1531 public final void sendActivityResult(
1532 IBinder token, String id, int requestCode,
1533 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001534 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001535 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1537 list.add(new ResultInfo(id, requestCode, resultCode, data));
1538 mAppThread.scheduleSendResult(token, list);
1539 }
1540
1541 // if the thread hasn't started yet, we don't have the handler, so just
1542 // save the messages until we're ready.
1543 private final void queueOrSendMessage(int what, Object obj) {
1544 queueOrSendMessage(what, obj, 0, 0);
1545 }
1546
1547 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1548 queueOrSendMessage(what, obj, arg1, 0);
1549 }
1550
1551 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1552 synchronized (this) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001553 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1555 + ": " + arg1 + " / " + obj);
1556 Message msg = Message.obtain();
1557 msg.what = what;
1558 msg.obj = obj;
1559 msg.arg1 = arg1;
1560 msg.arg2 = arg2;
1561 mH.sendMessage(msg);
1562 }
1563 }
1564
Dianne Hackborn21556372010-02-04 16:34:40 -08001565 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 String what) {
1567 ContextCleanupInfo cci = new ContextCleanupInfo();
1568 cci.context = context;
1569 cci.who = who;
1570 cci.what = what;
1571 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1572 }
1573
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001574 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1576
1577 ActivityInfo aInfo = r.activityInfo;
1578 if (r.packageInfo == null) {
1579 r.packageInfo = getPackageInfo(aInfo.applicationInfo,
1580 Context.CONTEXT_INCLUDE_CODE);
1581 }
Bob Leee5408332009-09-04 18:31:17 -07001582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 ComponentName component = r.intent.getComponent();
1584 if (component == null) {
1585 component = r.intent.resolveActivity(
1586 mInitialApplication.getPackageManager());
1587 r.intent.setComponent(component);
1588 }
1589
1590 if (r.activityInfo.targetActivity != null) {
1591 component = new ComponentName(r.activityInfo.packageName,
1592 r.activityInfo.targetActivity);
1593 }
1594
1595 Activity activity = null;
1596 try {
1597 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1598 activity = mInstrumentation.newActivity(
1599 cl, component.getClassName(), r.intent);
1600 r.intent.setExtrasClassLoader(cl);
1601 if (r.state != null) {
1602 r.state.setClassLoader(cl);
1603 }
1604 } catch (Exception e) {
1605 if (!mInstrumentation.onException(activity, e)) {
1606 throw new RuntimeException(
1607 "Unable to instantiate activity " + component
1608 + ": " + e.toString(), e);
1609 }
1610 }
1611
1612 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001613 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001614
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001615 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1616 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 TAG, r + ": app=" + app
1618 + ", appName=" + app.getPackageName()
1619 + ", pkg=" + r.packageInfo.getPackageName()
1620 + ", comp=" + r.intent.getComponent().toShortString()
1621 + ", dir=" + r.packageInfo.getAppDir());
1622
1623 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001624 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 appContext.init(r.packageInfo, r.token, this);
1626 appContext.setOuterContext(activity);
1627 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
1628 Configuration config = new Configuration(mConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001629 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001630 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001631 activity.attach(appContext, this, getInstrumentation(), r.token,
1632 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001633 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001634
Christopher Tateb70f3df2009-04-07 16:07:59 -07001635 if (customIntent != null) {
1636 activity.mIntent = customIntent;
1637 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001638 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 activity.mStartedActivity = false;
1640 int theme = r.activityInfo.getThemeResource();
1641 if (theme != 0) {
1642 activity.setTheme(theme);
1643 }
1644
1645 activity.mCalled = false;
1646 mInstrumentation.callActivityOnCreate(activity, r.state);
1647 if (!activity.mCalled) {
1648 throw new SuperNotCalledException(
1649 "Activity " + r.intent.getComponent().toShortString() +
1650 " did not call through to super.onCreate()");
1651 }
1652 r.activity = activity;
1653 r.stopped = true;
1654 if (!r.activity.mFinished) {
1655 activity.performStart();
1656 r.stopped = false;
1657 }
1658 if (!r.activity.mFinished) {
1659 if (r.state != null) {
1660 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1661 }
1662 }
1663 if (!r.activity.mFinished) {
1664 activity.mCalled = false;
1665 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1666 if (!activity.mCalled) {
1667 throw new SuperNotCalledException(
1668 "Activity " + r.intent.getComponent().toShortString() +
1669 " did not call through to super.onPostCreate()");
1670 }
1671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 }
1673 r.paused = true;
1674
1675 mActivities.put(r.token, r);
1676
1677 } catch (SuperNotCalledException e) {
1678 throw e;
1679
1680 } catch (Exception e) {
1681 if (!mInstrumentation.onException(activity, e)) {
1682 throw new RuntimeException(
1683 "Unable to start activity " + component
1684 + ": " + e.toString(), e);
1685 }
1686 }
1687
1688 return activity;
1689 }
1690
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001691 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 // If we are getting ready to gc after going to the background, well
1693 // we are back active so skip it.
1694 unscheduleGcIdler();
1695
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001696 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001698 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699
1700 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001701 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001702 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 handleResumeActivity(r.token, false, r.isForward);
1704
1705 if (!r.activity.mFinished && r.startsNotResumed) {
1706 // The activity manager actually wants this one to start out
1707 // paused, because it needs to be visible but isn't in the
1708 // foreground. We accomplish this by going through the
1709 // normal startup (because activities expect to go through
1710 // onResume() the first time they run, before their window
1711 // is displayed), and then pausing it. However, in this case
1712 // we do -not- need to do the full pause cycle (of freezing
1713 // and such) because the activity manager assumes it can just
1714 // retain the current state it has.
1715 try {
1716 r.activity.mCalled = false;
1717 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001718 // We need to keep around the original state, in case
1719 // we need to be created again.
1720 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 if (!r.activity.mCalled) {
1722 throw new SuperNotCalledException(
1723 "Activity " + r.intent.getComponent().toShortString() +
1724 " did not call through to super.onPause()");
1725 }
1726
1727 } catch (SuperNotCalledException e) {
1728 throw e;
1729
1730 } catch (Exception e) {
1731 if (!mInstrumentation.onException(r.activity, e)) {
1732 throw new RuntimeException(
1733 "Unable to pause activity "
1734 + r.intent.getComponent().toShortString()
1735 + ": " + e.toString(), e);
1736 }
1737 }
1738 r.paused = true;
1739 }
1740 } else {
1741 // If there was an error, for any reason, tell the activity
1742 // manager to stop us.
1743 try {
1744 ActivityManagerNative.getDefault()
1745 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1746 } catch (RemoteException ex) {
1747 }
1748 }
1749 }
1750
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001751 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 List<Intent> intents) {
1753 final int N = intents.size();
1754 for (int i=0; i<N; i++) {
1755 Intent intent = intents.get(i);
1756 intent.setExtrasClassLoader(r.activity.getClassLoader());
1757 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1758 }
1759 }
1760
1761 public final void performNewIntents(IBinder token,
1762 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001763 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (r != null) {
1765 final boolean resumed = !r.paused;
1766 if (resumed) {
1767 mInstrumentation.callActivityOnPause(r.activity);
1768 }
1769 deliverNewIntents(r, intents);
1770 if (resumed) {
1771 mInstrumentation.callActivityOnResume(r.activity);
1772 }
1773 }
1774 }
Bob Leee5408332009-09-04 18:31:17 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 private final void handleNewIntent(NewIntentData data) {
1777 performNewIntents(data.token, data.intents);
1778 }
1779
1780 private final void handleReceiver(ReceiverData data) {
1781 // If we are getting ready to gc after going to the background, well
1782 // we are back active so skip it.
1783 unscheduleGcIdler();
1784
1785 String component = data.intent.getComponent().getClassName();
1786
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001787 LoadedApk packageInfo = getPackageInfoNoCheck(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 data.info.applicationInfo);
1789
1790 IActivityManager mgr = ActivityManagerNative.getDefault();
1791
1792 BroadcastReceiver receiver = null;
1793 try {
1794 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1795 data.intent.setExtrasClassLoader(cl);
1796 if (data.resultExtras != null) {
1797 data.resultExtras.setClassLoader(cl);
1798 }
1799 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1800 } catch (Exception e) {
1801 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001802 if (DEBUG_BROADCAST) Slog.i(TAG,
1803 "Finishing failed broadcast to " + data.intent.getComponent());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 mgr.finishReceiver(mAppThread.asBinder(), data.resultCode,
1805 data.resultData, data.resultExtras, data.resultAbort);
1806 } catch (RemoteException ex) {
1807 }
1808 throw new RuntimeException(
1809 "Unable to instantiate receiver " + component
1810 + ": " + e.toString(), e);
1811 }
1812
1813 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001814 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001815
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001816 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 TAG, "Performing receive of " + data.intent
1818 + ": app=" + app
1819 + ", appName=" + app.getPackageName()
1820 + ", pkg=" + packageInfo.getPackageName()
1821 + ", comp=" + data.intent.getComponent().toShortString()
1822 + ", dir=" + packageInfo.getAppDir());
1823
Dianne Hackborn21556372010-02-04 16:34:40 -08001824 ContextImpl context = (ContextImpl)app.getBaseContext();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 receiver.setOrderedHint(true);
1826 receiver.setResult(data.resultCode, data.resultData,
1827 data.resultExtras);
1828 receiver.setOrderedHint(data.sync);
1829 receiver.onReceive(context.getReceiverRestrictedContext(),
1830 data.intent);
1831 } catch (Exception e) {
1832 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001833 if (DEBUG_BROADCAST) Slog.i(TAG,
1834 "Finishing failed broadcast to " + data.intent.getComponent());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 mgr.finishReceiver(mAppThread.asBinder(), data.resultCode,
1836 data.resultData, data.resultExtras, data.resultAbort);
1837 } catch (RemoteException ex) {
1838 }
1839 if (!mInstrumentation.onException(receiver, e)) {
1840 throw new RuntimeException(
1841 "Unable to start receiver " + component
1842 + ": " + e.toString(), e);
1843 }
1844 }
1845
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001846 QueuedWork.waitToFinish();
1847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 try {
1849 if (data.sync) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001850 if (DEBUG_BROADCAST) Slog.i(TAG,
1851 "Finishing ordered broadcast to " + data.intent.getComponent());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 mgr.finishReceiver(
1853 mAppThread.asBinder(), receiver.getResultCode(),
1854 receiver.getResultData(), receiver.getResultExtras(false),
1855 receiver.getAbortBroadcast());
1856 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001857 if (DEBUG_BROADCAST) Slog.i(TAG,
1858 "Finishing broadcast to " + data.intent.getComponent());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 mgr.finishReceiver(mAppThread.asBinder(), 0, null, null, false);
1860 }
1861 } catch (RemoteException ex) {
1862 }
1863 }
1864
Christopher Tate181fafa2009-05-14 11:12:14 -07001865 // Instantiate a BackupAgent and tell it that it's alive
1866 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001867 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001868
1869 // no longer idle; we have backup work to do
1870 unscheduleGcIdler();
1871
1872 // instantiate the BackupAgent class named in the manifest
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001873 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001874 String packageName = packageInfo.mPackageName;
1875 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001876 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07001877 + " already exists");
1878 return;
1879 }
Bob Leee5408332009-09-04 18:31:17 -07001880
Christopher Tate181fafa2009-05-14 11:12:14 -07001881 BackupAgent agent = null;
1882 String classname = data.appInfo.backupAgentName;
1883 if (classname == null) {
1884 if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001885 Slog.e(TAG, "Attempted incremental backup but no defined agent for "
Christopher Tate181fafa2009-05-14 11:12:14 -07001886 + packageName);
1887 return;
1888 }
1889 classname = "android.app.FullBackupAgent";
1890 }
1891 try {
Christopher Tated1475e02009-07-09 15:36:17 -07001892 IBinder binder = null;
1893 try {
1894 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1895 agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance();
1896
1897 // set up the agent's context
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001898 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing BackupAgent "
Christopher Tated1475e02009-07-09 15:36:17 -07001899 + data.appInfo.backupAgentName);
1900
Dianne Hackborn21556372010-02-04 16:34:40 -08001901 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07001902 context.init(packageInfo, null, this);
1903 context.setOuterContext(agent);
1904 agent.attach(context);
1905
1906 agent.onCreate();
1907 binder = agent.onBind();
1908 mBackupAgents.put(packageName, agent);
1909 } catch (Exception e) {
1910 // If this is during restore, fail silently; otherwise go
1911 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001912 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07001913 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
1914 throw e;
1915 }
1916 // falling through with 'binder' still null
1917 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001918
1919 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07001920 try {
1921 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
1922 } catch (RemoteException e) {
1923 // nothing to do.
1924 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001925 } catch (Exception e) {
1926 throw new RuntimeException("Unable to create BackupAgent "
1927 + data.appInfo.backupAgentName + ": " + e.toString(), e);
1928 }
1929 }
1930
1931 // Tear down a BackupAgent
1932 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001933 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07001934
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001935 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001936 String packageName = packageInfo.mPackageName;
1937 BackupAgent agent = mBackupAgents.get(packageName);
1938 if (agent != null) {
1939 try {
1940 agent.onDestroy();
1941 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001942 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001943 e.printStackTrace();
1944 }
1945 mBackupAgents.remove(packageName);
1946 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001947 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001948 }
1949 }
1950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 private final void handleCreateService(CreateServiceData data) {
1952 // If we are getting ready to gc after going to the background, well
1953 // we are back active so skip it.
1954 unscheduleGcIdler();
1955
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001956 LoadedApk packageInfo = getPackageInfoNoCheck(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 data.info.applicationInfo);
1958 Service service = null;
1959 try {
1960 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1961 service = (Service) cl.loadClass(data.info.name).newInstance();
1962 } catch (Exception e) {
1963 if (!mInstrumentation.onException(service, e)) {
1964 throw new RuntimeException(
1965 "Unable to instantiate service " + data.info.name
1966 + ": " + e.toString(), e);
1967 }
1968 }
1969
1970 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001971 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972
Dianne Hackborn21556372010-02-04 16:34:40 -08001973 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 context.init(packageInfo, null, this);
1975
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001976 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 context.setOuterContext(service);
1978 service.attach(context, this, data.info.name, data.token, app,
1979 ActivityManagerNative.getDefault());
1980 service.onCreate();
1981 mServices.put(data.token, service);
1982 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07001983 ActivityManagerNative.getDefault().serviceDoneExecuting(
1984 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 } catch (RemoteException e) {
1986 // nothing to do.
1987 }
1988 } catch (Exception e) {
1989 if (!mInstrumentation.onException(service, e)) {
1990 throw new RuntimeException(
1991 "Unable to create service " + data.info.name
1992 + ": " + e.toString(), e);
1993 }
1994 }
1995 }
1996
1997 private final void handleBindService(BindServiceData data) {
1998 Service s = mServices.get(data.token);
1999 if (s != null) {
2000 try {
2001 data.intent.setExtrasClassLoader(s.getClassLoader());
2002 try {
2003 if (!data.rebind) {
2004 IBinder binder = s.onBind(data.intent);
2005 ActivityManagerNative.getDefault().publishService(
2006 data.token, data.intent, binder);
2007 } else {
2008 s.onRebind(data.intent);
2009 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002010 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002012 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 } catch (RemoteException ex) {
2014 }
2015 } catch (Exception e) {
2016 if (!mInstrumentation.onException(s, e)) {
2017 throw new RuntimeException(
2018 "Unable to bind to service " + s
2019 + " with " + data.intent + ": " + e.toString(), e);
2020 }
2021 }
2022 }
2023 }
2024
2025 private final void handleUnbindService(BindServiceData data) {
2026 Service s = mServices.get(data.token);
2027 if (s != null) {
2028 try {
2029 data.intent.setExtrasClassLoader(s.getClassLoader());
2030 boolean doRebind = s.onUnbind(data.intent);
2031 try {
2032 if (doRebind) {
2033 ActivityManagerNative.getDefault().unbindFinished(
2034 data.token, data.intent, doRebind);
2035 } else {
2036 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002037 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
2039 } catch (RemoteException ex) {
2040 }
2041 } catch (Exception e) {
2042 if (!mInstrumentation.onException(s, e)) {
2043 throw new RuntimeException(
2044 "Unable to unbind to service " + s
2045 + " with " + data.intent + ": " + e.toString(), e);
2046 }
2047 }
2048 }
2049 }
2050
Dianne Hackborn625ac272010-09-17 18:29:22 -07002051 private void handleDumpService(DumpComponentInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 try {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002053 Service s = mServices.get(info.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 if (s != null) {
2055 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
2056 s.dump(info.fd, pw, info.args);
2057 pw.close();
2058 }
2059 } finally {
2060 synchronized (info) {
2061 info.dumped = true;
2062 info.notifyAll();
2063 }
2064 }
2065 }
2066
Dianne Hackborn625ac272010-09-17 18:29:22 -07002067 private void handleDumpActivity(DumpComponentInfo info) {
2068 try {
2069 ActivityClientRecord r = mActivities.get(info.token);
2070 if (r != null && r.activity != null) {
2071 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
2072 r.activity.dump(info.fd, pw, info.args);
2073 pw.close();
2074 }
2075 } finally {
2076 synchronized (info) {
2077 info.dumped = true;
2078 info.notifyAll();
2079 }
2080 }
2081 }
2082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 private final void handleServiceArgs(ServiceArgsData data) {
2084 Service s = mServices.get(data.token);
2085 if (s != null) {
2086 try {
2087 if (data.args != null) {
2088 data.args.setExtrasClassLoader(s.getClassLoader());
2089 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002090 int res = s.onStartCommand(data.args, data.flags, data.startId);
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002091
2092 QueuedWork.waitToFinish();
2093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002095 ActivityManagerNative.getDefault().serviceDoneExecuting(
2096 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 } catch (RemoteException e) {
2098 // nothing to do.
2099 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002100 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 } catch (Exception e) {
2102 if (!mInstrumentation.onException(s, e)) {
2103 throw new RuntimeException(
2104 "Unable to start service " + s
2105 + " with " + data.args + ": " + e.toString(), e);
2106 }
2107 }
2108 }
2109 }
2110
2111 private final void handleStopService(IBinder token) {
2112 Service s = mServices.remove(token);
2113 if (s != null) {
2114 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002115 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 s.onDestroy();
2117 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002118 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002120 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002122
2123 QueuedWork.waitToFinish();
2124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002126 ActivityManagerNative.getDefault().serviceDoneExecuting(
2127 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 } catch (RemoteException e) {
2129 // nothing to do.
2130 }
2131 } catch (Exception e) {
2132 if (!mInstrumentation.onException(s, e)) {
2133 throw new RuntimeException(
2134 "Unable to stop service " + s
2135 + ": " + e.toString(), e);
2136 }
2137 }
2138 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002139 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 }
2141
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002142 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002144 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002145 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 + " finished=" + r.activity.mFinished);
2147 if (r != null && !r.activity.mFinished) {
2148 if (clearHide) {
2149 r.hideForNow = false;
2150 r.activity.mStartedActivity = false;
2151 }
2152 try {
2153 if (r.pendingIntents != null) {
2154 deliverNewIntents(r, r.pendingIntents);
2155 r.pendingIntents = null;
2156 }
2157 if (r.pendingResults != null) {
2158 deliverResults(r, r.pendingResults);
2159 r.pendingResults = null;
2160 }
2161 r.activity.performResume();
2162
Bob Leee5408332009-09-04 18:31:17 -07002163 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 r.paused = false;
2167 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 r.state = null;
2169 } catch (Exception e) {
2170 if (!mInstrumentation.onException(r.activity, e)) {
2171 throw new RuntimeException(
2172 "Unable to resume activity "
2173 + r.intent.getComponent().toShortString()
2174 + ": " + e.toString(), e);
2175 }
2176 }
2177 }
2178 return r;
2179 }
2180
2181 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2182 // If we are getting ready to gc after going to the background, well
2183 // we are back active so skip it.
2184 unscheduleGcIdler();
2185
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002186 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187
2188 if (r != null) {
2189 final Activity a = r.activity;
2190
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002191 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 TAG, "Resume " + r + " started activity: " +
2193 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2194 + ", finished: " + a.mFinished);
2195
2196 final int forwardBit = isForward ?
2197 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 // If the window hasn't yet been added to the window manager,
2200 // and this guy didn't finish itself or start another activity,
2201 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002202 boolean willBeVisible = !a.mStartedActivity;
2203 if (!willBeVisible) {
2204 try {
2205 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2206 a.getActivityToken());
2207 } catch (RemoteException e) {
2208 }
2209 }
2210 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 r.window = r.activity.getWindow();
2212 View decor = r.window.getDecorView();
2213 decor.setVisibility(View.INVISIBLE);
2214 ViewManager wm = a.getWindowManager();
2215 WindowManager.LayoutParams l = r.window.getAttributes();
2216 a.mDecor = decor;
2217 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2218 l.softInputMode |= forwardBit;
2219 if (a.mVisibleFromClient) {
2220 a.mWindowAdded = true;
2221 wm.addView(decor, l);
2222 }
2223
2224 // If the window has already been added, but during resume
2225 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002226 // window visible.
2227 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002228 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 TAG, "Launch " + r + " mStartedActivity set");
2230 r.hideForNow = true;
2231 }
2232
2233 // The window is now visible if it has been added, we are not
2234 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002235 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002236 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002238 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002239 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 performConfigurationChanged(r.activity, r.newConfig);
2241 r.newConfig = null;
2242 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002243 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 + isForward);
2245 WindowManager.LayoutParams l = r.window.getAttributes();
2246 if ((l.softInputMode
2247 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2248 != forwardBit) {
2249 l.softInputMode = (l.softInputMode
2250 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2251 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002252 if (r.activity.mVisibleFromClient) {
2253 ViewManager wm = a.getWindowManager();
2254 View decor = r.window.getDecorView();
2255 wm.updateViewLayout(decor, l);
2256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 }
2258 r.activity.mVisibleFromServer = true;
2259 mNumVisibleActivities++;
2260 if (r.activity.mVisibleFromClient) {
2261 r.activity.makeVisible();
2262 }
2263 }
2264
2265 r.nextIdle = mNewActivities;
2266 mNewActivities = r;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002267 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 TAG, "Scheduling idle handler for " + r);
2269 Looper.myQueue().addIdleHandler(new Idler());
2270
2271 } else {
2272 // If an exception was thrown when trying to resume, then
2273 // just end this activity.
2274 try {
2275 ActivityManagerNative.getDefault()
2276 .finishActivity(token, Activity.RESULT_CANCELED, null);
2277 } catch (RemoteException ex) {
2278 }
2279 }
2280 }
2281
2282 private int mThumbnailWidth = -1;
2283 private int mThumbnailHeight = -1;
2284
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002285 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002286 Bitmap thumbnail = null;
2287 try {
2288 int w = mThumbnailWidth;
2289 int h;
2290 if (w < 0) {
2291 Resources res = r.activity.getResources();
2292 mThumbnailHeight = h =
2293 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
2294
2295 mThumbnailWidth = w =
2296 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2297 } else {
2298 h = mThumbnailHeight;
2299 }
2300
Jim Miller0b2a6d02010-07-13 18:01:29 -07002301 // On platforms where we don't want thumbnails, set dims to (0,0)
2302 if ((w > 0) && (h > 0)) {
2303 View topView = r.activity.getWindow().getDecorView();
2304
2305 // Maximize bitmap by capturing in native aspect.
2306 if (topView.getWidth() >= topView.getHeight()) {
2307 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2308 } else {
2309 thumbnail = Bitmap.createBitmap(h, w, THUMBNAIL_FORMAT);
2310 }
2311
2312 thumbnail.eraseColor(0);
2313 Canvas cv = new Canvas(thumbnail);
2314 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2315 thumbnail = null;
2316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002317 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 } catch (Exception e) {
2320 if (!mInstrumentation.onException(r.activity, e)) {
2321 throw new RuntimeException(
2322 "Unable to create thumbnail of "
2323 + r.intent.getComponent().toShortString()
2324 + ": " + e.toString(), e);
2325 }
2326 thumbnail = null;
2327 }
2328
2329 return thumbnail;
2330 }
2331
2332 private final void handlePauseActivity(IBinder token, boolean finished,
2333 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002334 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002336 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 if (userLeaving) {
2338 performUserLeavingActivity(r);
2339 }
Bob Leee5408332009-09-04 18:31:17 -07002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 r.activity.mConfigChangeFlags |= configChanges;
2342 Bundle state = performPauseActivity(token, finished, true);
2343
2344 // Tell the activity manager we have paused.
2345 try {
2346 ActivityManagerNative.getDefault().activityPaused(token, state);
2347 } catch (RemoteException ex) {
2348 }
2349 }
2350 }
2351
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002352 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 mInstrumentation.callActivityOnUserLeaving(r.activity);
2354 }
2355
2356 final Bundle performPauseActivity(IBinder token, boolean finished,
2357 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002358 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 return r != null ? performPauseActivity(r, finished, saveState) : null;
2360 }
2361
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002362 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 boolean saveState) {
2364 if (r.paused) {
2365 if (r.activity.mFinished) {
2366 // If we are finishing, we won't call onResume() in certain cases.
2367 // So here we likewise don't want to call onPause() if the activity
2368 // isn't resumed.
2369 return null;
2370 }
2371 RuntimeException e = new RuntimeException(
2372 "Performing pause of activity that is not resumed: "
2373 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002374 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 }
2376 Bundle state = null;
2377 if (finished) {
2378 r.activity.mFinished = true;
2379 }
2380 try {
2381 // Next have the activity save its current state and managed dialogs...
2382 if (!r.activity.mFinished && saveState) {
2383 state = new Bundle();
2384 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2385 r.state = state;
2386 }
2387 // Now we are idle.
2388 r.activity.mCalled = false;
2389 mInstrumentation.callActivityOnPause(r.activity);
2390 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2391 if (!r.activity.mCalled) {
2392 throw new SuperNotCalledException(
2393 "Activity " + r.intent.getComponent().toShortString() +
2394 " did not call through to super.onPause()");
2395 }
2396
2397 } catch (SuperNotCalledException e) {
2398 throw e;
2399
2400 } catch (Exception e) {
2401 if (!mInstrumentation.onException(r.activity, e)) {
2402 throw new RuntimeException(
2403 "Unable to pause activity "
2404 + r.intent.getComponent().toShortString()
2405 + ": " + e.toString(), e);
2406 }
2407 }
2408 r.paused = true;
2409 return state;
2410 }
2411
2412 final void performStopActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002413 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 performStopActivityInner(r, null, false);
2415 }
2416
2417 private static class StopInfo {
2418 Bitmap thumbnail;
2419 CharSequence description;
2420 }
2421
2422 private final class ProviderRefCount {
2423 public int count;
2424 ProviderRefCount(int pCount) {
2425 count = pCount;
2426 }
2427 }
2428
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002429 private final void performStopActivityInner(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 StopInfo info, boolean keepShown) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002431 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 if (r != null) {
2433 if (!keepShown && r.stopped) {
2434 if (r.activity.mFinished) {
2435 // If we are finishing, we won't call onResume() in certain
2436 // cases. So here we likewise don't want to call onStop()
2437 // if the activity isn't resumed.
2438 return;
2439 }
2440 RuntimeException e = new RuntimeException(
2441 "Performing stop of activity that is not resumed: "
2442 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002443 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 }
2445
2446 if (info != null) {
2447 try {
2448 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002449 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 info.description = r.activity.onCreateDescription();
2451 } catch (Exception e) {
2452 if (!mInstrumentation.onException(r.activity, e)) {
2453 throw new RuntimeException(
2454 "Unable to save state of activity "
2455 + r.intent.getComponent().toShortString()
2456 + ": " + e.toString(), e);
2457 }
2458 }
2459 }
2460
2461 if (!keepShown) {
2462 try {
2463 // Now we are idle.
2464 r.activity.performStop();
2465 } catch (Exception e) {
2466 if (!mInstrumentation.onException(r.activity, e)) {
2467 throw new RuntimeException(
2468 "Unable to stop activity "
2469 + r.intent.getComponent().toShortString()
2470 + ": " + e.toString(), e);
2471 }
2472 }
2473 r.stopped = true;
2474 }
2475
2476 r.paused = true;
2477 }
2478 }
2479
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002480 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 View v = r.activity.mDecor;
2482 if (v != null) {
2483 if (show) {
2484 if (!r.activity.mVisibleFromServer) {
2485 r.activity.mVisibleFromServer = true;
2486 mNumVisibleActivities++;
2487 if (r.activity.mVisibleFromClient) {
2488 r.activity.makeVisible();
2489 }
2490 }
2491 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002492 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002493 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 performConfigurationChanged(r.activity, r.newConfig);
2495 r.newConfig = null;
2496 }
2497 } else {
2498 if (r.activity.mVisibleFromServer) {
2499 r.activity.mVisibleFromServer = false;
2500 mNumVisibleActivities--;
2501 v.setVisibility(View.INVISIBLE);
2502 }
2503 }
2504 }
2505 }
2506
2507 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002508 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002509 r.activity.mConfigChangeFlags |= configChanges;
2510
2511 StopInfo info = new StopInfo();
2512 performStopActivityInner(r, info, show);
2513
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002514 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 TAG, "Finishing stop of " + r + ": show=" + show
2516 + " win=" + r.window);
2517
2518 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 // Tell activity manager we have been stopped.
2521 try {
2522 ActivityManagerNative.getDefault().activityStopped(
2523 r.token, info.thumbnail, info.description);
2524 } catch (RemoteException ex) {
2525 }
2526 }
2527
2528 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002529 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 if (r.stopped) {
2531 r.activity.performRestart();
2532 r.stopped = false;
2533 }
2534 }
2535
2536 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002537 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 if (!show && !r.stopped) {
2539 performStopActivityInner(r, null, show);
2540 } else if (show && r.stopped) {
2541 // If we are getting ready to gc after going to the background, well
2542 // we are back active so skip it.
2543 unscheduleGcIdler();
2544
2545 r.activity.performRestart();
2546 r.stopped = false;
2547 }
2548 if (r.activity.mDecor != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002549 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 TAG, "Handle window " + r + " visibility: " + show);
2551 updateVisibility(r, show);
2552 }
2553 }
2554
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002555 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 final int N = results.size();
2557 for (int i=0; i<N; i++) {
2558 ResultInfo ri = results.get(i);
2559 try {
2560 if (ri.mData != null) {
2561 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2562 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002563 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002564 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 r.activity.dispatchActivityResult(ri.mResultWho,
2566 ri.mRequestCode, ri.mResultCode, ri.mData);
2567 } catch (Exception e) {
2568 if (!mInstrumentation.onException(r.activity, e)) {
2569 throw new RuntimeException(
2570 "Failure delivering result " + ri + " to activity "
2571 + r.intent.getComponent().toShortString()
2572 + ": " + e.toString(), e);
2573 }
2574 }
2575 }
2576 }
2577
2578 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002579 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002580 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 if (r != null) {
2582 final boolean resumed = !r.paused;
2583 if (!r.activity.mFinished && r.activity.mDecor != null
2584 && r.hideForNow && resumed) {
2585 // We had hidden the activity because it started another
2586 // one... we have gotten a result back and we are not
2587 // paused, so make sure our window is visible.
2588 updateVisibility(r, true);
2589 }
2590 if (resumed) {
2591 try {
2592 // Now we are idle.
2593 r.activity.mCalled = false;
2594 mInstrumentation.callActivityOnPause(r.activity);
2595 if (!r.activity.mCalled) {
2596 throw new SuperNotCalledException(
2597 "Activity " + r.intent.getComponent().toShortString()
2598 + " did not call through to super.onPause()");
2599 }
2600 } catch (SuperNotCalledException e) {
2601 throw e;
2602 } catch (Exception e) {
2603 if (!mInstrumentation.onException(r.activity, e)) {
2604 throw new RuntimeException(
2605 "Unable to pause activity "
2606 + r.intent.getComponent().toShortString()
2607 + ": " + e.toString(), e);
2608 }
2609 }
2610 }
2611 deliverResults(r, res.results);
2612 if (resumed) {
2613 mInstrumentation.callActivityOnResume(r.activity);
2614 }
2615 }
2616 }
2617
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002618 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 return performDestroyActivity(token, finishing, 0, false);
2620 }
2621
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002622 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002624 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002625 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 if (r != null) {
2627 r.activity.mConfigChangeFlags |= configChanges;
2628 if (finishing) {
2629 r.activity.mFinished = true;
2630 }
Jeff Hamilton3d32f6e2010-04-01 00:04:16 -05002631 if (getNonConfigInstance) {
2632 r.activity.mChangingConfigurations = true;
2633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 if (!r.paused) {
2635 try {
2636 r.activity.mCalled = false;
2637 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002638 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 r.activity.getComponentName().getClassName());
2640 if (!r.activity.mCalled) {
2641 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002642 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 + " did not call through to super.onPause()");
2644 }
2645 } catch (SuperNotCalledException e) {
2646 throw e;
2647 } catch (Exception e) {
2648 if (!mInstrumentation.onException(r.activity, e)) {
2649 throw new RuntimeException(
2650 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002651 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 + ": " + e.toString(), e);
2653 }
2654 }
2655 r.paused = true;
2656 }
2657 if (!r.stopped) {
2658 try {
2659 r.activity.performStop();
2660 } catch (SuperNotCalledException e) {
2661 throw e;
2662 } catch (Exception e) {
2663 if (!mInstrumentation.onException(r.activity, e)) {
2664 throw new RuntimeException(
2665 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002666 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 + ": " + e.toString(), e);
2668 }
2669 }
2670 r.stopped = true;
2671 }
2672 if (getNonConfigInstance) {
2673 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002674 r.lastNonConfigurationInstances
2675 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 } catch (Exception e) {
2677 if (!mInstrumentation.onException(r.activity, e)) {
2678 throw new RuntimeException(
2679 "Unable to retain activity "
2680 + r.intent.getComponent().toShortString()
2681 + ": " + e.toString(), e);
2682 }
2683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 }
2685 try {
2686 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002687 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 if (!r.activity.mCalled) {
2689 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002690 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 " did not call through to super.onDestroy()");
2692 }
2693 if (r.window != null) {
2694 r.window.closeAllPanels();
2695 }
2696 } catch (SuperNotCalledException e) {
2697 throw e;
2698 } catch (Exception e) {
2699 if (!mInstrumentation.onException(r.activity, e)) {
2700 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002701 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2702 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 }
2704 }
2705 }
2706 mActivities.remove(token);
2707
2708 return r;
2709 }
2710
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002711 private static String safeToComponentShortString(Intent intent) {
2712 ComponentName component = intent.getComponent();
2713 return component == null ? "[Unknown]" : component.toShortString();
2714 }
2715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 private final void handleDestroyActivity(IBinder token, boolean finishing,
2717 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002718 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002719 configChanges, getNonConfigInstance);
2720 if (r != null) {
2721 WindowManager wm = r.activity.getWindowManager();
2722 View v = r.activity.mDecor;
2723 if (v != null) {
2724 if (r.activity.mVisibleFromServer) {
2725 mNumVisibleActivities--;
2726 }
2727 IBinder wtoken = v.getWindowToken();
2728 if (r.activity.mWindowAdded) {
2729 wm.removeViewImmediate(v);
2730 }
2731 if (wtoken != null) {
2732 WindowManagerImpl.getDefault().closeAll(wtoken,
2733 r.activity.getClass().getName(), "Activity");
2734 }
2735 r.activity.mDecor = null;
2736 }
2737 WindowManagerImpl.getDefault().closeAll(token,
2738 r.activity.getClass().getName(), "Activity");
2739
2740 // Mocked out contexts won't be participating in the normal
2741 // process lifecycle, but if we're running with a proper
2742 // ApplicationContext we need to have it tear down things
2743 // cleanly.
2744 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002745 if (c instanceof ContextImpl) {
2746 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 r.activity.getClass().getName(), "Activity");
2748 }
2749 }
2750 if (finishing) {
2751 try {
2752 ActivityManagerNative.getDefault().activityDestroyed(token);
2753 } catch (RemoteException ex) {
2754 // If the system process has died, it's game over for everyone.
2755 }
2756 }
2757 }
2758
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002759 private final void handleRelaunchActivity(ActivityClientRecord tmp, int configChanges) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 // If we are getting ready to gc after going to the background, well
2761 // we are back active so skip it.
2762 unscheduleGcIdler();
2763
2764 Configuration changedConfig = null;
Bob Leee5408332009-09-04 18:31:17 -07002765
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002766 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002767 + tmp.token + " with configChanges=0x"
2768 + Integer.toHexString(configChanges));
2769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 // First: make sure we have the most recent configuration and most
2771 // recent version of the activity, or skip it if some previous call
2772 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002773 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 int N = mRelaunchingActivities.size();
2775 IBinder token = tmp.token;
2776 tmp = null;
2777 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002778 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 if (r.token == token) {
2780 tmp = r;
2781 mRelaunchingActivities.remove(i);
2782 i--;
2783 N--;
2784 }
2785 }
Bob Leee5408332009-09-04 18:31:17 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002788 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 return;
2790 }
Bob Leee5408332009-09-04 18:31:17 -07002791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002792 if (mPendingConfiguration != null) {
2793 changedConfig = mPendingConfiguration;
2794 mPendingConfiguration = null;
2795 }
2796 }
Bob Leee5408332009-09-04 18:31:17 -07002797
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002798 if (tmp.createdConfig != null) {
2799 // If the activity manager is passing us its current config,
2800 // assume that is really what we want regardless of what we
2801 // may have pending.
2802 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002803 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
2804 && mConfiguration.diff(tmp.createdConfig) != 0)) {
2805 if (changedConfig == null
2806 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
2807 changedConfig = tmp.createdConfig;
2808 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002809 }
2810 }
2811
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002812 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002813 + tmp.token + ": changedConfig=" + changedConfig);
2814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 // If there was a pending configuration change, execute it first.
2816 if (changedConfig != null) {
2817 handleConfigurationChanged(changedConfig);
2818 }
Bob Leee5408332009-09-04 18:31:17 -07002819
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002820 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002821 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 if (r == null) {
2823 return;
2824 }
Bob Leee5408332009-09-04 18:31:17 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 r.activity.mConfigChangeFlags |= configChanges;
Christopher Tateb70f3df2009-04-07 16:07:59 -07002827 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07002828
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 Bundle savedState = null;
2830 if (!r.paused) {
2831 savedState = performPauseActivity(r.token, false, true);
2832 }
Bob Leee5408332009-09-04 18:31:17 -07002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07002835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 r.activity = null;
2837 r.window = null;
2838 r.hideForNow = false;
2839 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07002840 // Merge any pending results and pending intents; don't just replace them
2841 if (tmp.pendingResults != null) {
2842 if (r.pendingResults == null) {
2843 r.pendingResults = tmp.pendingResults;
2844 } else {
2845 r.pendingResults.addAll(tmp.pendingResults);
2846 }
2847 }
2848 if (tmp.pendingIntents != null) {
2849 if (r.pendingIntents == null) {
2850 r.pendingIntents = tmp.pendingIntents;
2851 } else {
2852 r.pendingIntents.addAll(tmp.pendingIntents);
2853 }
2854 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 r.startsNotResumed = tmp.startsNotResumed;
2856 if (savedState != null) {
2857 r.state = savedState;
2858 }
Bob Leee5408332009-09-04 18:31:17 -07002859
Christopher Tateb70f3df2009-04-07 16:07:59 -07002860 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 }
2862
2863 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002864 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002865 Bitmap thumbnail = createThumbnailBitmap(r);
2866 CharSequence description = null;
2867 try {
2868 description = r.activity.onCreateDescription();
2869 } catch (Exception e) {
2870 if (!mInstrumentation.onException(r.activity, e)) {
2871 throw new RuntimeException(
2872 "Unable to create description of activity "
2873 + r.intent.getComponent().toShortString()
2874 + ": " + e.toString(), e);
2875 }
2876 }
2877 //System.out.println("Reporting top thumbnail " + thumbnail);
2878 try {
2879 ActivityManagerNative.getDefault().reportThumbnail(
2880 token, thumbnail, description);
2881 } catch (RemoteException ex) {
2882 }
2883 }
2884
2885 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
2886 boolean allActivities, Configuration newConfig) {
2887 ArrayList<ComponentCallbacks> callbacks
2888 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07002889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002891 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002893 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 Activity a = ar.activity;
2895 if (a != null) {
2896 if (!ar.activity.mFinished && (allActivities ||
2897 (a != null && !ar.paused))) {
2898 // If the activity is currently resumed, its configuration
2899 // needs to change right now.
2900 callbacks.add(a);
2901 } else if (newConfig != null) {
2902 // Otherwise, we will tell it about the change
2903 // the next time it is resumed or shown. Note that
2904 // the activity manager may, before then, decide the
2905 // activity needs to be destroyed to handle its new
2906 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002907 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002908 + ar.activityInfo.name + " newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 ar.newConfig = newConfig;
2910 }
2911 }
2912 }
2913 }
2914 if (mServices.size() > 0) {
2915 Iterator<Service> it = mServices.values().iterator();
2916 while (it.hasNext()) {
2917 callbacks.add(it.next());
2918 }
2919 }
2920 synchronized (mProviderMap) {
2921 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002922 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 while (it.hasNext()) {
2924 callbacks.add(it.next().mLocalProvider);
2925 }
2926 }
2927 }
2928 final int N = mAllApplications.size();
2929 for (int i=0; i<N; i++) {
2930 callbacks.add(mAllApplications.get(i));
2931 }
Bob Leee5408332009-09-04 18:31:17 -07002932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 return callbacks;
2934 }
Bob Leee5408332009-09-04 18:31:17 -07002935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 private final void performConfigurationChanged(
2937 ComponentCallbacks cb, Configuration config) {
2938 // Only for Activity objects, check that they actually call up to their
2939 // superclass implementation. ComponentCallbacks is an interface, so
2940 // we check the runtime type and act accordingly.
2941 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
2942 if (activity != null) {
2943 activity.mCalled = false;
2944 }
Bob Leee5408332009-09-04 18:31:17 -07002945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 boolean shouldChangeConfig = false;
2947 if ((activity == null) || (activity.mCurrentConfig == null)) {
2948 shouldChangeConfig = true;
2949 } else {
Bob Leee5408332009-09-04 18:31:17 -07002950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 // If the new config is the same as the config this Activity
2952 // is already running with then don't bother calling
2953 // onConfigurationChanged
2954 int diff = activity.mCurrentConfig.diff(config);
2955 if (diff != 0) {
Bob Leee5408332009-09-04 18:31:17 -07002956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 // If this activity doesn't handle any of the config changes
2958 // then don't bother calling onConfigurationChanged as we're
2959 // going to destroy it.
2960 if ((~activity.mActivityInfo.configChanges & diff) == 0) {
2961 shouldChangeConfig = true;
2962 }
2963 }
2964 }
Bob Leee5408332009-09-04 18:31:17 -07002965
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002966 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002967 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 if (shouldChangeConfig) {
2969 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07002970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 if (activity != null) {
2972 if (!activity.mCalled) {
2973 throw new SuperNotCalledException(
2974 "Activity " + activity.getLocalClassName() +
2975 " did not call through to super.onConfigurationChanged()");
2976 }
2977 activity.mConfigChangeFlags = 0;
2978 activity.mCurrentConfig = new Configuration(config);
2979 }
2980 }
2981 }
2982
Dianne Hackbornae078162010-03-18 11:29:37 -07002983 final boolean applyConfigurationToResourcesLocked(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002984 if (mResConfiguration == null) {
2985 mResConfiguration = new Configuration();
2986 }
2987 if (!mResConfiguration.isOtherSeqNewer(config)) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002988 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002989 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07002990 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002991 }
Dianne Hackbornae078162010-03-18 11:29:37 -07002992 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002993 DisplayMetrics dm = getDisplayMetricsLocked(true);
Bob Leee5408332009-09-04 18:31:17 -07002994
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002995 // set it for java, this also affects newly created Resources
2996 if (config.locale != null) {
2997 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 }
Bob Leee5408332009-09-04 18:31:17 -07002999
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003000 Resources.updateSystemConfiguration(config, dm);
Bob Leee5408332009-09-04 18:31:17 -07003001
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003002 ContextImpl.ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003003 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003004
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003005 Iterator<WeakReference<Resources>> it =
3006 mActiveResources.values().iterator();
3007 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3008 // mActiveResources.entrySet().iterator();
3009 while (it.hasNext()) {
3010 WeakReference<Resources> v = it.next();
3011 Resources r = v.get();
3012 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003013 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003014 + r + " config to: " + config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003015 r.updateConfiguration(config, dm);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003016 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003017 // + " " + r + ": " + r.getConfiguration());
3018 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003019 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003020 it.remove();
3021 }
3022 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003023
3024 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003025 }
3026
3027 final void handleConfigurationChanged(Configuration config) {
3028
3029 ArrayList<ComponentCallbacks> callbacks = null;
3030
3031 synchronized (mPackages) {
3032 if (mPendingConfiguration != null) {
3033 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3034 config = mPendingConfiguration;
3035 }
3036 mPendingConfiguration = null;
3037 }
3038
3039 if (config == null) {
3040 return;
3041 }
3042
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003043 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003044 + config);
3045
3046 applyConfigurationToResourcesLocked(config);
3047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 if (mConfiguration == null) {
3049 mConfiguration = new Configuration();
3050 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003051 if (!mConfiguration.isOtherSeqNewer(config)) {
3052 return;
3053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 mConfiguration.updateFrom(config);
Bob Leee5408332009-09-04 18:31:17 -07003055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 callbacks = collectComponentCallbacksLocked(false, config);
3057 }
Bob Leee5408332009-09-04 18:31:17 -07003058
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003059 if (callbacks != null) {
3060 final int N = callbacks.size();
3061 for (int i=0; i<N; i++) {
3062 performConfigurationChanged(callbacks.get(i), config);
3063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 }
3065 }
3066
3067 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003068 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 if (r == null || r.activity == null) {
3070 return;
3071 }
Bob Leee5408332009-09-04 18:31:17 -07003072
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003073 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003074 + r.activityInfo.name);
3075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 performConfigurationChanged(r.activity, mConfiguration);
3077 }
3078
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003079 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003080 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003081 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003082 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3083 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003084 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003085 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003086 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003087 } finally {
3088 try {
3089 pcd.fd.close();
3090 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003091 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003092 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003093 }
3094 } else {
3095 Debug.stopMethodTracing();
3096 }
3097 }
Bob Leee5408332009-09-04 18:31:17 -07003098
Andy McFadden824c5102010-07-09 16:26:57 -07003099 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3100 if (managed) {
3101 try {
3102 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3103 } catch (IOException e) {
3104 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3105 + " -- can the process access this path?");
3106 } finally {
3107 try {
3108 dhd.fd.close();
3109 } catch (IOException e) {
3110 Slog.w(TAG, "Failure closing profile fd", e);
3111 }
3112 }
3113 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003114 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003115 }
3116 }
3117
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003118 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3119 boolean hasPkgInfo = false;
3120 if (packages != null) {
3121 for (int i=packages.length-1; i>=0; i--) {
3122 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3123 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003124 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003125 ref = mPackages.get(packages[i]);
3126 if (ref != null && ref.get() != null) {
3127 hasPkgInfo = true;
3128 } else {
3129 ref = mResourcePackages.get(packages[i]);
3130 if (ref != null && ref.get() != null) {
3131 hasPkgInfo = true;
3132 }
3133 }
3134 }
3135 mPackages.remove(packages[i]);
3136 mResourcePackages.remove(packages[i]);
3137 }
3138 }
3139 ContextImpl.ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
3140 hasPkgInfo);
3141 }
3142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 final void handleLowMemory() {
3144 ArrayList<ComponentCallbacks> callbacks
3145 = new ArrayList<ComponentCallbacks>();
3146
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003147 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 callbacks = collectComponentCallbacksLocked(true, null);
3149 }
Bob Leee5408332009-09-04 18:31:17 -07003150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 final int N = callbacks.size();
3152 for (int i=0; i<N; i++) {
3153 callbacks.get(i).onLowMemory();
3154 }
3155
Chris Tatece229052009-03-25 16:44:52 -07003156 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3157 if (Process.myUid() != Process.SYSTEM_UID) {
3158 int sqliteReleased = SQLiteDatabase.releaseMemory();
3159 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3160 }
Bob Leee5408332009-09-04 18:31:17 -07003161
Mike Reedcaf0df12009-04-27 14:32:05 -04003162 // Ask graphics to free up as much as possible (font/image caches)
3163 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164
3165 BinderInternal.forceGc("mem");
3166 }
3167
3168 private final void handleBindApplication(AppBindData data) {
3169 mBoundApplication = data;
3170 mConfiguration = new Configuration(data.config);
3171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003173 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 android.ddm.DdmHandleAppName.setAppName(data.processName);
3175
3176 /*
3177 * Before spawning a new process, reset the time zone to be the system time zone.
3178 * This needs to be done because the system time zone could have changed after the
3179 * the spawning of this process. Without doing this this process would have the incorrect
3180 * system time zone.
3181 */
3182 TimeZone.setDefault(null);
3183
3184 /*
3185 * Initialize the default locale in this process for the reasons we set the time zone.
3186 */
3187 Locale.setDefault(data.config.locale);
3188
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003189 /*
3190 * Update the system configuration since its preloaded and might not
3191 * reflect configuration changes. The configuration object passed
3192 * in AppBindData can be safely assumed to be up to date
3193 */
3194 Resources.getSystem().updateConfiguration(mConfiguration, null);
3195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 data.info = getPackageInfoNoCheck(data.appInfo);
3197
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003198 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003199 * For system applications on userdebug/eng builds, log stack
3200 * traces of disk and network access to dropbox for analysis.
3201 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003202 if ((data.appInfo.flags &
3203 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003204 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3205 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003206 }
3207
3208 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003209 * Switch this process to density compatibility mode if needed.
3210 */
3211 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3212 == 0) {
3213 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3214 }
Bob Leee5408332009-09-04 18:31:17 -07003215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3217 // XXX should have option to change the port.
3218 Debug.changeDebugPort(8100);
3219 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003220 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 + " is waiting for the debugger on port 8100...");
3222
3223 IActivityManager mgr = ActivityManagerNative.getDefault();
3224 try {
3225 mgr.showWaitingForDebugger(mAppThread, true);
3226 } catch (RemoteException ex) {
3227 }
3228
3229 Debug.waitForDebugger();
3230
3231 try {
3232 mgr.showWaitingForDebugger(mAppThread, false);
3233 } catch (RemoteException ex) {
3234 }
3235
3236 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003237 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 + " can be debugged on port 8100...");
3239 }
3240 }
3241
3242 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003243 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 appContext.init(data.info, null, this);
3245 InstrumentationInfo ii = null;
3246 try {
3247 ii = appContext.getPackageManager().
3248 getInstrumentationInfo(data.instrumentationName, 0);
3249 } catch (PackageManager.NameNotFoundException e) {
3250 }
3251 if (ii == null) {
3252 throw new RuntimeException(
3253 "Unable to find instrumentation info for: "
3254 + data.instrumentationName);
3255 }
3256
3257 mInstrumentationAppDir = ii.sourceDir;
3258 mInstrumentationAppPackage = ii.packageName;
3259 mInstrumentedAppDir = data.info.getAppDir();
3260
3261 ApplicationInfo instrApp = new ApplicationInfo();
3262 instrApp.packageName = ii.packageName;
3263 instrApp.sourceDir = ii.sourceDir;
3264 instrApp.publicSourceDir = ii.publicSourceDir;
3265 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003266 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003267 LoadedApk pi = getPackageInfo(instrApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003269 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 instrContext.init(pi, null, this);
3271
3272 try {
3273 java.lang.ClassLoader cl = instrContext.getClassLoader();
3274 mInstrumentation = (Instrumentation)
3275 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3276 } catch (Exception e) {
3277 throw new RuntimeException(
3278 "Unable to instantiate instrumentation "
3279 + data.instrumentationName + ": " + e.toString(), e);
3280 }
3281
3282 mInstrumentation.init(this, instrContext, appContext,
3283 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3284
3285 if (data.profileFile != null && !ii.handleProfiling) {
3286 data.handlingProfiling = true;
3287 File file = new File(data.profileFile);
3288 file.getParentFile().mkdirs();
3289 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3290 }
3291
3292 try {
3293 mInstrumentation.onCreate(data.instrumentationArgs);
3294 }
3295 catch (Exception e) {
3296 throw new RuntimeException(
3297 "Exception thrown in onCreate() of "
3298 + data.instrumentationName + ": " + e.toString(), e);
3299 }
3300
3301 } else {
3302 mInstrumentation = new Instrumentation();
3303 }
3304
Christopher Tate181fafa2009-05-14 11:12:14 -07003305 // If the app is being launched for full backup or restore, bring it up in
3306 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003307 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 mInitialApplication = app;
3309
3310 List<ProviderInfo> providers = data.providers;
3311 if (providers != null) {
3312 installContentProviders(app, providers);
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003313 // For process that contain content providers, we want to
3314 // ensure that the JIT is enabled "at some point".
3315 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
3317
3318 try {
3319 mInstrumentation.callApplicationOnCreate(app);
3320 } catch (Exception e) {
3321 if (!mInstrumentation.onException(app, e)) {
3322 throw new RuntimeException(
3323 "Unable to create application " + app.getClass().getName()
3324 + ": " + e.toString(), e);
3325 }
3326 }
3327 }
3328
3329 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3330 IActivityManager am = ActivityManagerNative.getDefault();
3331 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3332 Debug.stopMethodTracing();
3333 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003334 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 // + ", app thr: " + mAppThread);
3336 try {
3337 am.finishInstrumentation(mAppThread, resultCode, results);
3338 } catch (RemoteException ex) {
3339 }
3340 }
3341
3342 private final void installContentProviders(
3343 Context context, List<ProviderInfo> providers) {
3344 final ArrayList<IActivityManager.ContentProviderHolder> results =
3345 new ArrayList<IActivityManager.ContentProviderHolder>();
3346
3347 Iterator<ProviderInfo> i = providers.iterator();
3348 while (i.hasNext()) {
3349 ProviderInfo cpi = i.next();
3350 StringBuilder buf = new StringBuilder(128);
3351 buf.append("Publishing provider ");
3352 buf.append(cpi.authority);
3353 buf.append(": ");
3354 buf.append(cpi.name);
3355 Log.i(TAG, buf.toString());
3356 IContentProvider cp = installProvider(context, null, cpi, false);
3357 if (cp != null) {
3358 IActivityManager.ContentProviderHolder cph =
3359 new IActivityManager.ContentProviderHolder(cpi);
3360 cph.provider = cp;
3361 results.add(cph);
3362 // Don't ever unload this provider from the process.
3363 synchronized(mProviderMap) {
3364 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3365 }
3366 }
3367 }
3368
3369 try {
3370 ActivityManagerNative.getDefault().publishContentProviders(
3371 getApplicationThread(), results);
3372 } catch (RemoteException ex) {
3373 }
3374 }
3375
3376 private final IContentProvider getProvider(Context context, String name) {
3377 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003378 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 if (pr != null) {
3380 return pr.mProvider;
3381 }
3382 }
3383
3384 IActivityManager.ContentProviderHolder holder = null;
3385 try {
3386 holder = ActivityManagerNative.getDefault().getContentProvider(
3387 getApplicationThread(), name);
3388 } catch (RemoteException ex) {
3389 }
3390 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003391 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 return null;
3393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394
3395 IContentProvider prov = installProvider(context, holder.provider,
3396 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003397 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 if (holder.noReleaseNeeded || holder.provider == null) {
3399 // We are not going to release the provider if it is an external
3400 // provider that doesn't care about being released, or if it is
3401 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003402 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 synchronized(mProviderMap) {
3404 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3405 }
3406 }
3407 return prov;
3408 }
3409
3410 public final IContentProvider acquireProvider(Context c, String name) {
3411 IContentProvider provider = getProvider(c, name);
3412 if(provider == null)
3413 return null;
3414 IBinder jBinder = provider.asBinder();
3415 synchronized(mProviderMap) {
3416 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3417 if(prc == null) {
3418 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3419 } else {
3420 prc.count++;
3421 } //end else
3422 } //end synchronized
3423 return provider;
3424 }
3425
3426 public final boolean releaseProvider(IContentProvider provider) {
3427 if(provider == null) {
3428 return false;
3429 }
3430 IBinder jBinder = provider.asBinder();
3431 synchronized(mProviderMap) {
3432 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3433 if(prc == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003434 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldnt be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 return false;
3436 } else {
3437 prc.count--;
3438 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003439 // Schedule the actual remove asynchronously, since we
3440 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003441 // TODO: it would be nice to post a delayed message, so
3442 // if we come back and need the same provider quickly
3443 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003444 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3445 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 } //end if
3447 } //end else
3448 } //end synchronized
3449 return true;
3450 }
3451
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003452 final void completeRemoveProvider(IContentProvider provider) {
3453 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003454 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003455 synchronized(mProviderMap) {
3456 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3457 if(prc != null && prc.count == 0) {
3458 mProviderRefCountMap.remove(jBinder);
3459 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003460 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003461 }
3462 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003463
3464 if (name != null) {
3465 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003466 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003467 "ActivityManagerNative.removeContentProvider(" + name);
3468 ActivityManagerNative.getDefault().removeContentProvider(
3469 getApplicationThread(), name);
3470 } catch (RemoteException e) {
3471 //do nothing content provider object is dead any way
3472 } //end catch
3473 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003474 }
3475
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003476 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003478 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003482 String name = null;
3483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003485 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003487 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 IBinder myBinder = pr.mProvider.asBinder();
3489 if (myBinder == providerBinder) {
3490 //find if its published by this process itself
3491 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003492 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003493 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003495 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 "death recipient");
3497 //content provider is in another process
3498 myBinder.unlinkToDeath(pr, 0);
3499 iter.remove();
3500 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003501 if(name == null) {
3502 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 }
3504 } //end if myBinder
3505 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003506
3507 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 }
3509
3510 final void removeDeadProvider(String name, IContentProvider provider) {
3511 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003512 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003514 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003515 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003516 if (removed != null) {
3517 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 }
3520 }
3521 }
3522
3523 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003524 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003526 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003527 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003528 if (removed != null) {
3529 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3530 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 }
3532 }
3533
3534 private final IContentProvider installProvider(Context context,
3535 IContentProvider provider, ProviderInfo info, boolean noisy) {
3536 ContentProvider localProvider = null;
3537 if (provider == null) {
3538 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003539 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 + info.name);
3541 }
3542 Context c = null;
3543 ApplicationInfo ai = info.applicationInfo;
3544 if (context.getPackageName().equals(ai.packageName)) {
3545 c = context;
3546 } else if (mInitialApplication != null &&
3547 mInitialApplication.getPackageName().equals(ai.packageName)) {
3548 c = mInitialApplication;
3549 } else {
3550 try {
3551 c = context.createPackageContext(ai.packageName,
3552 Context.CONTEXT_INCLUDE_CODE);
3553 } catch (PackageManager.NameNotFoundException e) {
3554 }
3555 }
3556 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003557 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 ai.packageName +
3559 " while loading content provider " +
3560 info.name);
3561 return null;
3562 }
3563 try {
3564 final java.lang.ClassLoader cl = c.getClassLoader();
3565 localProvider = (ContentProvider)cl.
3566 loadClass(info.name).newInstance();
3567 provider = localProvider.getIContentProvider();
3568 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003569 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 info.name + " from sourceDir " +
3571 info.applicationInfo.sourceDir);
3572 return null;
3573 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003574 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 TAG, "Instantiating local provider " + info.name);
3576 // XXX Need to create the correct context for this provider.
3577 localProvider.attachInfo(c, info);
3578 } catch (java.lang.Exception e) {
3579 if (!mInstrumentation.onException(null, e)) {
3580 throw new RuntimeException(
3581 "Unable to get provider " + info.name
3582 + ": " + e.toString(), e);
3583 }
3584 return null;
3585 }
3586 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003587 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 + info.name);
3589 }
3590
3591 synchronized (mProviderMap) {
3592 // Cache the pointer for the remote provider.
3593 String names[] = PATTERN_SEMICOLON.split(info.authority);
3594 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003595 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 localProvider);
3597 try {
3598 provider.asBinder().linkToDeath(pr, 0);
3599 mProviderMap.put(names[i], pr);
3600 } catch (RemoteException e) {
3601 return null;
3602 }
3603 }
3604 if (localProvider != null) {
3605 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003606 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 }
3608 }
3609
3610 return provider;
3611 }
3612
3613 private final void attach(boolean system) {
3614 sThreadLocal.set(this);
3615 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 if (!system) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003617 ViewRoot.addFirstDrawHandler(new Runnable() {
3618 public void run() {
3619 ensureJitEnabled();
3620 }
3621 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
3623 RuntimeInit.setApplicationObject(mAppThread.asBinder());
3624 IActivityManager mgr = ActivityManagerNative.getDefault();
3625 try {
3626 mgr.attachApplication(mAppThread);
3627 } catch (RemoteException ex) {
3628 }
3629 } else {
3630 // Don't set application object here -- if the system crashes,
3631 // we can't display an alert, we just want to die die die.
3632 android.ddm.DdmHandleAppName.setAppName("system_process");
3633 try {
3634 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08003635 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 context.init(getSystemContext().mPackageInfo, null, this);
3637 Application app = Instrumentation.newApplication(Application.class, context);
3638 mAllApplications.add(app);
3639 mInitialApplication = app;
3640 app.onCreate();
3641 } catch (Exception e) {
3642 throw new RuntimeException(
3643 "Unable to instantiate Application():" + e.toString(), e);
3644 }
3645 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003646
3647 ViewRoot.addConfigCallback(new ComponentCallbacks() {
3648 public void onConfigurationChanged(Configuration newConfig) {
3649 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07003650 // We need to apply this change to the resources
3651 // immediately, because upon returning the view
3652 // hierarchy will be informed about it.
3653 if (applyConfigurationToResourcesLocked(newConfig)) {
3654 // This actually changed the resources! Tell
3655 // everyone about it.
3656 if (mPendingConfiguration == null ||
3657 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
3658 mPendingConfiguration = newConfig;
3659
3660 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
3661 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003662 }
3663 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003664 }
3665 public void onLowMemory() {
3666 }
3667 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 }
3669
3670 private final void detach()
3671 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 sThreadLocal.set(null);
3673 }
3674
3675 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07003676 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 ActivityThread thread = new ActivityThread();
3678 thread.attach(true);
3679 return thread;
3680 }
3681
3682 public final void installSystemProviders(List providers) {
3683 if (providers != null) {
3684 installContentProviders(mInitialApplication,
3685 (List<ProviderInfo>)providers);
3686 }
3687 }
3688
3689 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07003690 SamplingProfilerIntegration.start();
3691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 Process.setArgV0("<pre-initialized>");
3693
3694 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003695 if (sMainThreadHandler == null) {
3696 sMainThreadHandler = new Handler();
3697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698
3699 ActivityThread thread = new ActivityThread();
3700 thread.attach(false);
3701
3702 Looper.loop();
3703
3704 if (Process.supportsProcesses()) {
3705 throw new RuntimeException("Main thread loop unexpectedly exited");
3706 }
3707
3708 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02003709 String name = (thread.mInitialApplication != null)
3710 ? thread.mInitialApplication.getPackageName()
3711 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003712 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 }
3714}