blob: bd83762e33d8e68af0fabda8dce3d272a907b3e9 [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;
Robert Greenwalt434203a2010-10-11 16:00:27 -070045import android.net.IConnectivityManager;
46import android.net.Proxy;
47import android.net.ProxyProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.Bundle;
49import android.os.Debug;
50import android.os.Handler;
51import android.os.IBinder;
52import android.os.Looper;
53import android.os.Message;
54import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070055import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.os.Process;
57import android.os.RemoteException;
58import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070059import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.SystemClock;
61import android.util.AndroidRuntimeException;
62import android.util.Config;
63import android.util.DisplayMetrics;
64import android.util.EventLog;
65import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070066import android.util.LogPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080067import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070069import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.view.View;
71import android.view.ViewDebug;
72import android.view.ViewManager;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -080073import android.view.ViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.Window;
75import android.view.WindowManager;
76import android.view.WindowManagerImpl;
77
78import com.android.internal.os.BinderInternal;
79import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070080import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
83
84import java.io.File;
85import java.io.FileDescriptor;
86import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070087import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.io.PrintWriter;
89import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070090import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091import java.util.ArrayList;
92import java.util.HashMap;
93import java.util.Iterator;
94import java.util.List;
95import java.util.Locale;
96import java.util.Map;
97import java.util.TimeZone;
98import java.util.regex.Pattern;
99
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800100import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102final class SuperNotCalledException extends AndroidRuntimeException {
103 public SuperNotCalledException(String msg) {
104 super(msg);
105 }
106}
107
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700108final class RemoteServiceException extends AndroidRuntimeException {
109 public RemoteServiceException(String msg) {
110 super(msg);
111 }
112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114/**
115 * This manages the execution of the main thread in an
116 * application process, scheduling and executing activities,
117 * broadcasts, and other operations on it as the activity
118 * manager requests.
119 *
120 * {@hide}
121 */
122public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700123 /** @hide */
124 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700125 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 private static final boolean DEBUG = false;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700127 static final boolean localLOGV = DEBUG ? Config.LOGD : Config.LOGV;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700128 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700129 /** @hide */
130 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700131 private static final boolean DEBUG_RESULTS = false;
Christopher Tate436344a2009-09-30 16:17:37 -0700132 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700133 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
135 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
136 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
137 private static final int LOG_ON_PAUSE_CALLED = 30021;
138 private static final int LOG_ON_RESUME_CALLED = 30022;
139
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700140 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700141
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700142 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700144 final ApplicationThread mAppThread = new ApplicationThread();
145 final Looper mLooper = Looper.myLooper();
146 final H mH = new H();
147 final HashMap<IBinder, ActivityClientRecord> mActivities
148 = new HashMap<IBinder, ActivityClientRecord>();
149 // List of new activities (via ActivityRecord.nextIdle) that should
150 // be reported when next we idle.
151 ActivityClientRecord mNewActivities = null;
152 // Number of activities that are currently visible on-screen.
153 int mNumVisibleActivities = 0;
154 final HashMap<IBinder, Service> mServices
155 = new HashMap<IBinder, Service>();
156 AppBindData mBoundApplication;
157 Configuration mConfiguration;
158 Configuration mResConfiguration;
159 Application mInitialApplication;
160 final ArrayList<Application> mAllApplications
161 = new ArrayList<Application>();
162 // set of instantiated backup agents, keyed by package name
163 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700164 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700165 Instrumentation mInstrumentation;
166 String mInstrumentationAppDir = null;
167 String mInstrumentationAppPackage = null;
168 String mInstrumentedAppDir = null;
169 boolean mSystemThread = false;
170 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700172 // These can be accessed by multiple threads; mPackages is the lock.
173 // XXX For now we keep around information about all packages we have
174 // seen, not removing entries from this map.
175 final HashMap<String, WeakReference<LoadedApk>> mPackages
176 = new HashMap<String, WeakReference<LoadedApk>>();
177 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
178 = new HashMap<String, WeakReference<LoadedApk>>();
179 Display mDisplay = null;
180 DisplayMetrics mDisplayMetrics = null;
181 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
182 = new HashMap<ResourcesKey, WeakReference<Resources> >();
183 final ArrayList<ActivityClientRecord> mRelaunchingActivities
184 = new ArrayList<ActivityClientRecord>();
185 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 // The lock of mProviderMap protects the following variables.
188 final HashMap<String, ProviderClientRecord> mProviderMap
189 = new HashMap<String, ProviderClientRecord>();
190 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
191 = new HashMap<IBinder, ProviderRefCount>();
192 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
193 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
Jeff Hamilton52d32032011-01-08 15:31:26 -0600195 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
196 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
197
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700198 final GcIdler mGcIdler = new GcIdler();
199 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700201 static Handler sMainThreadHandler; // set once in main()
202
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800203 Bundle mCoreSettings = null;
204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 private static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700207 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 Intent intent;
209 Bundle state;
210 Activity activity;
211 Window window;
212 Activity parent;
213 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700214 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean paused;
216 boolean stopped;
217 boolean hideForNow;
218 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700219 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700220 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 ActivityInfo activityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224
225 List<ResultInfo> pendingResults;
226 List<Intent> pendingIntents;
227
228 boolean startsNotResumed;
229 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800230 int pendingConfigChanges;
231 boolean onlyLocalRequest;
232
233 View mPendingRemoveWindow;
234 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700236 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 parent = null;
238 embeddedID = null;
239 paused = false;
240 stopped = false;
241 hideForNow = false;
242 nextIdle = null;
243 }
244
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800245 public boolean isPreHoneycomb() {
246 if (activity != null) {
247 return activity.getApplicationInfo().targetSdkVersion
248 < android.os.Build.VERSION_CODES.HONEYCOMB;
249 }
250 return false;
251 }
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 public String toString() {
254 ComponentName componentName = intent.getComponent();
255 return "ActivityRecord{"
256 + Integer.toHexString(System.identityHashCode(this))
257 + " token=" + token + " " + (componentName == null
258 ? "no component name" : componentName.toShortString())
259 + "}";
260 }
261 }
262
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700263 private final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 final String mName;
265 final IContentProvider mProvider;
266 final ContentProvider mLocalProvider;
267
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700268 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 ContentProvider localProvider) {
270 mName = name;
271 mProvider = provider;
272 mLocalProvider = localProvider;
273 }
274
275 public void binderDied() {
276 removeDeadProvider(mName, mProvider);
277 }
278 }
279
280 private static final class NewIntentData {
281 List<Intent> intents;
282 IBinder token;
283 public String toString() {
284 return "NewIntentData{intents=" + intents + " token=" + token + "}";
285 }
286 }
287
Dianne Hackborne829fef2010-10-26 17:44:01 -0700288 private static final class ReceiverData extends BroadcastReceiver.PendingResult {
289 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
290 boolean ordered, boolean sticky, IBinder token) {
291 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
292 this.intent = intent;
293 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 Intent intent;
296 ActivityInfo info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 public String toString() {
298 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700299 info.packageName + " resultCode=" + getResultCode()
300 + " resultData=" + getResultData() + " resultExtras="
301 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 }
303 }
304
Christopher Tate181fafa2009-05-14 11:12:14 -0700305 private static final class CreateBackupAgentData {
306 ApplicationInfo appInfo;
307 int backupMode;
308 public String toString() {
309 return "CreateBackupAgentData{appInfo=" + appInfo
310 + " backupAgent=" + appInfo.backupAgentName
311 + " mode=" + backupMode + "}";
312 }
313 }
Bob Leee5408332009-09-04 18:31:17 -0700314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 private static final class CreateServiceData {
316 IBinder token;
317 ServiceInfo info;
318 Intent intent;
319 public String toString() {
320 return "CreateServiceData{token=" + token + " className="
321 + info.name + " packageName=" + info.packageName
322 + " intent=" + intent + "}";
323 }
324 }
325
326 private static final class BindServiceData {
327 IBinder token;
328 Intent intent;
329 boolean rebind;
330 public String toString() {
331 return "BindServiceData{token=" + token + " intent=" + intent + "}";
332 }
333 }
334
335 private static final class ServiceArgsData {
336 IBinder token;
337 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700338 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 Intent args;
340 public String toString() {
341 return "ServiceArgsData{token=" + token + " startId=" + startId
342 + " args=" + args + "}";
343 }
344 }
345
346 private static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700347 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 String processName;
349 ApplicationInfo appInfo;
350 List<ProviderInfo> providers;
351 ComponentName instrumentationName;
352 String profileFile;
353 Bundle instrumentationArgs;
354 IInstrumentationWatcher instrumentationWatcher;
355 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700356 boolean restrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 Configuration config;
358 boolean handlingProfiling;
359 public String toString() {
360 return "AppBindData{appInfo=" + appInfo + "}";
361 }
362 }
363
Dianne Hackborn625ac272010-09-17 18:29:22 -0700364 private static final class DumpComponentInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 FileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700366 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800367 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 String[] args;
369 boolean dumped;
370 }
371
372 private static final class ResultData {
373 IBinder token;
374 List<ResultInfo> results;
375 public String toString() {
376 return "ResultData{token=" + token + " results" + results + "}";
377 }
378 }
379
380 private static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800381 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 String what;
383 String who;
384 }
385
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700386 private static final class ProfilerControlData {
387 String path;
388 ParcelFileDescriptor fd;
389 }
390
Andy McFadden824c5102010-07-09 16:26:57 -0700391 private static final class DumpHeapData {
392 String path;
393 ParcelFileDescriptor fd;
394 }
395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 private final class ApplicationThread extends ApplicationThreadNative {
397 private static final String HEAP_COLUMN = "%17s %8s %8s %8s %8s";
398 private static final String ONE_COUNT_COLUMN = "%17s %8d";
399 private static final String TWO_COUNT_COLUMNS = "%17s %8d %17s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700400 private static final String TWO_COUNT_COLUMNS_DB = "%20s %8d %20s %8d";
401 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 // Formatting for checkin service - update version if row format changes
404 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 public final void schedulePauseActivity(IBinder token, boolean finished,
407 boolean userLeaving, int configChanges) {
408 queueOrSendMessage(
409 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
410 token,
411 (userLeaving ? 1 : 0),
412 configChanges);
413 }
414
415 public final void scheduleStopActivity(IBinder token, boolean showWindow,
416 int configChanges) {
417 queueOrSendMessage(
418 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
419 token, 0, configChanges);
420 }
421
422 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
423 queueOrSendMessage(
424 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
425 token);
426 }
427
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800428 public final void scheduleSleeping(IBinder token, boolean sleeping) {
429 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
430 }
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
433 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
434 }
435
436 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
437 ResultData res = new ResultData();
438 res.token = token;
439 res.results = results;
440 queueOrSendMessage(H.SEND_RESULT, res);
441 }
442
443 // we use token to identify this activity without having to send the
444 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700445 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 ActivityInfo info, Bundle state, List<ResultInfo> pendingResults,
447 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700448 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449
450 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700451 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 r.intent = intent;
453 r.activityInfo = info;
454 r.state = state;
455
456 r.pendingResults = pendingResults;
457 r.pendingIntents = pendingNewIntents;
458
459 r.startsNotResumed = notResumed;
460 r.isForward = isForward;
461
462 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
463 }
464
465 public final void scheduleRelaunchActivity(IBinder token,
466 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800467 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800468 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
469 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471
472 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
473 NewIntentData data = new NewIntentData();
474 data.intents = intents;
475 data.token = token;
476
477 queueOrSendMessage(H.NEW_INTENT, data);
478 }
479
480 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
481 int configChanges) {
482 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
483 configChanges);
484 }
485
486 public final void scheduleReceiver(Intent intent, ActivityInfo info,
487 int resultCode, String data, Bundle extras, boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700488 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
489 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 r.info = info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 queueOrSendMessage(H.RECEIVER, r);
492 }
493
Christopher Tate181fafa2009-05-14 11:12:14 -0700494 public final void scheduleCreateBackupAgent(ApplicationInfo app, int backupMode) {
495 CreateBackupAgentData d = new CreateBackupAgentData();
496 d.appInfo = app;
497 d.backupMode = backupMode;
498
499 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
500 }
501
502 public final void scheduleDestroyBackupAgent(ApplicationInfo app) {
503 CreateBackupAgentData d = new CreateBackupAgentData();
504 d.appInfo = app;
505
506 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
507 }
508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 public final void scheduleCreateService(IBinder token,
510 ServiceInfo info) {
511 CreateServiceData s = new CreateServiceData();
512 s.token = token;
513 s.info = info;
514
515 queueOrSendMessage(H.CREATE_SERVICE, s);
516 }
517
518 public final void scheduleBindService(IBinder token, Intent intent,
519 boolean rebind) {
520 BindServiceData s = new BindServiceData();
521 s.token = token;
522 s.intent = intent;
523 s.rebind = rebind;
524
525 queueOrSendMessage(H.BIND_SERVICE, s);
526 }
527
528 public final void scheduleUnbindService(IBinder token, Intent intent) {
529 BindServiceData s = new BindServiceData();
530 s.token = token;
531 s.intent = intent;
532
533 queueOrSendMessage(H.UNBIND_SERVICE, s);
534 }
535
536 public final void scheduleServiceArgs(IBinder token, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700537 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 ServiceArgsData s = new ServiceArgsData();
539 s.token = token;
540 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700541 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 s.args = args;
543
544 queueOrSendMessage(H.SERVICE_ARGS, s);
545 }
546
547 public final void scheduleStopService(IBinder token) {
548 queueOrSendMessage(H.STOP_SERVICE, token);
549 }
550
551 public final void bindApplication(String processName,
552 ApplicationInfo appInfo, List<ProviderInfo> providers,
553 ComponentName instrumentationName, String profileFile,
554 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Christopher Tate181fafa2009-05-14 11:12:14 -0700555 int debugMode, boolean isRestrictedBackupMode, Configuration config,
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800556 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
558 if (services != null) {
559 // Setup the service cache in the ServiceManager
560 ServiceManager.initServiceCache(services);
561 }
562
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800563 setCoreSettings(coreSettings);
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 AppBindData data = new AppBindData();
566 data.processName = processName;
567 data.appInfo = appInfo;
568 data.providers = providers;
569 data.instrumentationName = instrumentationName;
570 data.profileFile = profileFile;
571 data.instrumentationArgs = instrumentationArgs;
572 data.instrumentationWatcher = instrumentationWatcher;
573 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700574 data.restrictedBackupMode = isRestrictedBackupMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 data.config = config;
576 queueOrSendMessage(H.BIND_APPLICATION, data);
577 }
578
579 public final void scheduleExit() {
580 queueOrSendMessage(H.EXIT_APPLICATION, null);
581 }
582
Christopher Tate5e1ab332009-09-01 20:32:49 -0700583 public final void scheduleSuicide() {
584 queueOrSendMessage(H.SUICIDE, null);
585 }
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 public void requestThumbnail(IBinder token) {
588 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
589 }
590
591 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800592 synchronized (mPackages) {
593 if (mPendingConfiguration == null ||
594 mPendingConfiguration.isOtherSeqNewer(config)) {
595 mPendingConfiguration = config;
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
598 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
599 }
600
601 public void updateTimeZone() {
602 TimeZone.setDefault(null);
603 }
604
Robert Greenwalt03595d02010-11-02 14:08:23 -0700605 public void clearDnsCache() {
606 // a non-standard API to get this to libcore
607 InetAddress.clearDnsCache();
608 }
609
Robert Greenwalt434203a2010-10-11 16:00:27 -0700610 public void setHttpProxy(String host, String port, String exclList) {
611 Proxy.setHttpProxySystemProperty(host, port, exclList);
612 }
613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public void processInBackground() {
615 mH.removeMessages(H.GC_WHEN_IDLE);
616 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
617 }
618
619 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700620 DumpComponentInfo data = new DumpComponentInfo();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 data.fd = fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700622 data.token = servicetoken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 data.args = args;
624 data.dumped = false;
625 queueOrSendMessage(H.DUMP_SERVICE, data);
626 synchronized (data) {
627 while (!data.dumped) {
628 try {
629 data.wait();
630 } catch (InterruptedException e) {
631 // no need to do anything here, we will keep waiting until
632 // dumped is set
633 }
634 }
635 }
636 }
637
638 // This function exists to make sure all receiver dispatching is
639 // correctly ordered, since these are one-way calls and the binder driver
640 // applies transaction ordering per object for such calls.
641 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700642 int resultCode, String dataStr, Bundle extras, boolean ordered,
643 boolean sticky) throws RemoteException {
644 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 }
Bob Leee5408332009-09-04 18:31:17 -0700646
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 public void scheduleLowMemory() {
648 queueOrSendMessage(H.LOW_MEMORY, null);
649 }
650
651 public void scheduleActivityConfigurationChanged(IBinder token) {
652 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
653 }
654
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700655 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd) {
656 ProfilerControlData pcd = new ProfilerControlData();
657 pcd.path = path;
658 pcd.fd = fd;
659 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800660 }
661
Andy McFadden824c5102010-07-09 16:26:57 -0700662 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
663 DumpHeapData dhd = new DumpHeapData();
664 dhd.path = path;
665 dhd.fd = fd;
666 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
667 }
668
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700669 public void setSchedulingGroup(int group) {
670 // Note: do this immediately, since going into the foreground
671 // should happen regardless of what pending work we have to do
672 // and the activity manager will wait for us to report back that
673 // we are done before sending us to the background.
674 try {
675 Process.setProcessGroup(Process.myPid(), group);
676 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800677 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700678 }
679 }
Bob Leee5408332009-09-04 18:31:17 -0700680
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700681 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
682 Debug.getMemoryInfo(outInfo);
683 }
Bob Leee5408332009-09-04 18:31:17 -0700684
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700685 public void dispatchPackageBroadcast(int cmd, String[] packages) {
686 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
687 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700688
689 public void scheduleCrash(String msg) {
690 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
691 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700692
Dianne Hackborn30d71892010-12-11 10:37:55 -0800693 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
694 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700695 DumpComponentInfo data = new DumpComponentInfo();
696 data.fd = fd;
697 data.token = activitytoken;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800698 data.prefix = prefix;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700699 data.args = args;
700 data.dumped = false;
701 queueOrSendMessage(H.DUMP_ACTIVITY, data);
702 synchronized (data) {
703 while (!data.dumped) {
704 try {
705 data.wait();
706 } catch (InterruptedException e) {
707 // no need to do anything here, we will keep waiting until
708 // dumped is set
709 }
710 }
711 }
712 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 @Override
715 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
716 long nativeMax = Debug.getNativeHeapSize() / 1024;
717 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
718 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
719
720 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
721 Debug.getMemoryInfo(memInfo);
722
723 final int nativeShared = memInfo.nativeSharedDirty;
724 final int dalvikShared = memInfo.dalvikSharedDirty;
725 final int otherShared = memInfo.otherSharedDirty;
726
727 final int nativePrivate = memInfo.nativePrivateDirty;
728 final int dalvikPrivate = memInfo.dalvikPrivateDirty;
729 final int otherPrivate = memInfo.otherPrivateDirty;
730
731 Runtime runtime = Runtime.getRuntime();
732
733 long dalvikMax = runtime.totalMemory() / 1024;
734 long dalvikFree = runtime.freeMemory() / 1024;
735 long dalvikAllocated = dalvikMax - dalvikFree;
736 long viewInstanceCount = ViewDebug.getViewInstanceCount();
737 long viewRootInstanceCount = ViewDebug.getViewRootInstanceCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700738 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
739 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 int globalAssetCount = AssetManager.getGlobalAssetCount();
741 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
742 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
743 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
744 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700745 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800747 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 // Check to see if we were called by checkin server. If so, print terse format.
750 boolean doCheckinFormat = false;
751 if (args != null) {
752 for (String arg : args) {
753 if ("-c".equals(arg)) doCheckinFormat = true;
754 }
755 }
Bob Leee5408332009-09-04 18:31:17 -0700756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 // For checkin, we print one long comma-separated list of values
758 if (doCheckinFormat) {
759 // NOTE: if you change anything significant below, also consider changing
760 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700761 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 // Header
765 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
766 pw.print(Process.myPid()); pw.print(',');
767 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 // Heap info - max
770 pw.print(nativeMax); pw.print(',');
771 pw.print(dalvikMax); pw.print(',');
772 pw.print("N/A,");
773 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 // Heap info - allocated
776 pw.print(nativeAllocated); pw.print(',');
777 pw.print(dalvikAllocated); pw.print(',');
778 pw.print("N/A,");
779 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 // Heap info - free
782 pw.print(nativeFree); pw.print(',');
783 pw.print(dalvikFree); pw.print(',');
784 pw.print("N/A,");
785 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 // Heap info - proportional set size
788 pw.print(memInfo.nativePss); pw.print(',');
789 pw.print(memInfo.dalvikPss); pw.print(',');
790 pw.print(memInfo.otherPss); pw.print(',');
791 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 // Heap info - shared
Bob Leee5408332009-09-04 18:31:17 -0700794 pw.print(nativeShared); pw.print(',');
795 pw.print(dalvikShared); pw.print(',');
796 pw.print(otherShared); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 pw.print(nativeShared + dalvikShared + otherShared); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 // Heap info - private
Bob Leee5408332009-09-04 18:31:17 -0700800 pw.print(nativePrivate); pw.print(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 pw.print(dalvikPrivate); pw.print(',');
802 pw.print(otherPrivate); pw.print(',');
803 pw.print(nativePrivate + dalvikPrivate + otherPrivate); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 // Object counts
806 pw.print(viewInstanceCount); pw.print(',');
807 pw.print(viewRootInstanceCount); pw.print(',');
808 pw.print(appContextInstanceCount); pw.print(',');
809 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 pw.print(globalAssetCount); pw.print(',');
812 pw.print(globalAssetManagerCount); pw.print(',');
813 pw.print(binderLocalObjectCount); pw.print(',');
814 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 pw.print(binderDeathObjectCount); pw.print(',');
817 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 // SQL
820 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800821 pw.print(stats.memoryUsed / 1024); pw.print(',');
822 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
823 pw.print(stats.largestMemAlloc / 1024); pw.print(',');
824 for (int i = 0; i < stats.dbStats.size(); i++) {
825 DbStats dbStats = stats.dbStats.get(i);
826 printRow(pw, DB_INFO_FORMAT, dbStats.pageSize, dbStats.dbSize,
Vasu Nori90a367262010-04-12 12:49:09 -0700827 dbStats.lookaside, dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800828 pw.print(',');
829 }
Bob Leee5408332009-09-04 18:31:17 -0700830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 return;
832 }
Bob Leee5408332009-09-04 18:31:17 -0700833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 // otherwise, show human-readable format
835 printRow(pw, HEAP_COLUMN, "", "native", "dalvik", "other", "total");
836 printRow(pw, HEAP_COLUMN, "size:", nativeMax, dalvikMax, "N/A", nativeMax + dalvikMax);
837 printRow(pw, HEAP_COLUMN, "allocated:", nativeAllocated, dalvikAllocated, "N/A",
838 nativeAllocated + dalvikAllocated);
839 printRow(pw, HEAP_COLUMN, "free:", nativeFree, dalvikFree, "N/A",
840 nativeFree + dalvikFree);
841
842 printRow(pw, HEAP_COLUMN, "(Pss):", memInfo.nativePss, memInfo.dalvikPss,
843 memInfo.otherPss, memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss);
844
845 printRow(pw, HEAP_COLUMN, "(shared dirty):", nativeShared, dalvikShared, otherShared,
846 nativeShared + dalvikShared + otherShared);
847 printRow(pw, HEAP_COLUMN, "(priv dirty):", nativePrivate, dalvikPrivate, otherPrivate,
848 nativePrivate + dalvikPrivate + otherPrivate);
849
850 pw.println(" ");
851 pw.println(" Objects");
852 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRoots:",
853 viewRootInstanceCount);
854
855 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
856 "Activities:", activityInstanceCount);
857
858 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
859 "AssetManagers:", globalAssetManagerCount);
860
861 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
862 "Proxy Binders:", binderProxyObjectCount);
863 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
864
865 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 // SQLite mem info
868 pw.println(" ");
869 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700870 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800871 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700872 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
873 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800874 pw.println(" ");
875 int N = stats.dbStats.size();
876 if (N > 0) {
877 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700878 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
879 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800880 for (int i = 0; i < N; i++) {
881 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700882 printRow(pw, DB_INFO_FORMAT,
883 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
884 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
885 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
886 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -0800887 }
888 }
Bob Leee5408332009-09-04 18:31:17 -0700889
Dianne Hackborn82e1ee92009-08-11 18:56:41 -0700890 // Asset details.
891 String assetAlloc = AssetManager.getAssetAllocations();
892 if (assetAlloc != null) {
893 pw.println(" ");
894 pw.println(" Asset Allocations");
895 pw.print(assetAlloc);
896 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898
899 private void printRow(PrintWriter pw, String format, Object...objs) {
900 pw.println(String.format(format, objs));
901 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800902
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800903 public void setCoreSettings(Bundle coreSettings) {
904 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
907
908 private final class H extends Handler {
909 public static final int LAUNCH_ACTIVITY = 100;
910 public static final int PAUSE_ACTIVITY = 101;
911 public static final int PAUSE_ACTIVITY_FINISHING= 102;
912 public static final int STOP_ACTIVITY_SHOW = 103;
913 public static final int STOP_ACTIVITY_HIDE = 104;
914 public static final int SHOW_WINDOW = 105;
915 public static final int HIDE_WINDOW = 106;
916 public static final int RESUME_ACTIVITY = 107;
917 public static final int SEND_RESULT = 108;
918 public static final int DESTROY_ACTIVITY = 109;
919 public static final int BIND_APPLICATION = 110;
920 public static final int EXIT_APPLICATION = 111;
921 public static final int NEW_INTENT = 112;
922 public static final int RECEIVER = 113;
923 public static final int CREATE_SERVICE = 114;
924 public static final int SERVICE_ARGS = 115;
925 public static final int STOP_SERVICE = 116;
926 public static final int REQUEST_THUMBNAIL = 117;
927 public static final int CONFIGURATION_CHANGED = 118;
928 public static final int CLEAN_UP_CONTEXT = 119;
929 public static final int GC_WHEN_IDLE = 120;
930 public static final int BIND_SERVICE = 121;
931 public static final int UNBIND_SERVICE = 122;
932 public static final int DUMP_SERVICE = 123;
933 public static final int LOW_MEMORY = 124;
934 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
935 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800936 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -0700937 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -0700938 public static final int DESTROY_BACKUP_AGENT = 129;
939 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700940 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800941 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700942 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700943 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -0700944 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700945 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800946 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800947 public static final int SET_CORE_SETTINGS = 138;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700949 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 switch (code) {
951 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
952 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
953 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
954 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
955 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
956 case SHOW_WINDOW: return "SHOW_WINDOW";
957 case HIDE_WINDOW: return "HIDE_WINDOW";
958 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
959 case SEND_RESULT: return "SEND_RESULT";
960 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
961 case BIND_APPLICATION: return "BIND_APPLICATION";
962 case EXIT_APPLICATION: return "EXIT_APPLICATION";
963 case NEW_INTENT: return "NEW_INTENT";
964 case RECEIVER: return "RECEIVER";
965 case CREATE_SERVICE: return "CREATE_SERVICE";
966 case SERVICE_ARGS: return "SERVICE_ARGS";
967 case STOP_SERVICE: return "STOP_SERVICE";
968 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
969 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
970 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
971 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
972 case BIND_SERVICE: return "BIND_SERVICE";
973 case UNBIND_SERVICE: return "UNBIND_SERVICE";
974 case DUMP_SERVICE: return "DUMP_SERVICE";
975 case LOW_MEMORY: return "LOW_MEMORY";
976 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
977 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800978 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -0700979 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
980 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -0700981 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -0700982 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800983 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700984 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700985 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -0700986 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -0700987 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800988 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -0800989 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991 }
992 return "(unknown)";
993 }
994 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -0700995 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 switch (msg.what) {
997 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700998 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999
1000 r.packageInfo = getPackageInfoNoCheck(
1001 r.activityInfo.applicationInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001002 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 } break;
1004 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001005 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001006 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 } break;
1008 case PAUSE_ACTIVITY:
1009 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001010 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 break;
1012 case PAUSE_ACTIVITY_FINISHING:
1013 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1014 break;
1015 case STOP_ACTIVITY_SHOW:
1016 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1017 break;
1018 case STOP_ACTIVITY_HIDE:
1019 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1020 break;
1021 case SHOW_WINDOW:
1022 handleWindowVisibility((IBinder)msg.obj, true);
1023 break;
1024 case HIDE_WINDOW:
1025 handleWindowVisibility((IBinder)msg.obj, false);
1026 break;
1027 case RESUME_ACTIVITY:
1028 handleResumeActivity((IBinder)msg.obj, true,
1029 msg.arg1 != 0);
1030 break;
1031 case SEND_RESULT:
1032 handleSendResult((ResultData)msg.obj);
1033 break;
1034 case DESTROY_ACTIVITY:
1035 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1036 msg.arg2, false);
1037 break;
1038 case BIND_APPLICATION:
1039 AppBindData data = (AppBindData)msg.obj;
1040 handleBindApplication(data);
1041 break;
1042 case EXIT_APPLICATION:
1043 if (mInitialApplication != null) {
1044 mInitialApplication.onTerminate();
1045 }
1046 Looper.myLooper().quit();
1047 break;
1048 case NEW_INTENT:
1049 handleNewIntent((NewIntentData)msg.obj);
1050 break;
1051 case RECEIVER:
1052 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001053 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 break;
1055 case CREATE_SERVICE:
1056 handleCreateService((CreateServiceData)msg.obj);
1057 break;
1058 case BIND_SERVICE:
1059 handleBindService((BindServiceData)msg.obj);
1060 break;
1061 case UNBIND_SERVICE:
1062 handleUnbindService((BindServiceData)msg.obj);
1063 break;
1064 case SERVICE_ARGS:
1065 handleServiceArgs((ServiceArgsData)msg.obj);
1066 break;
1067 case STOP_SERVICE:
1068 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001069 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 break;
1071 case REQUEST_THUMBNAIL:
1072 handleRequestThumbnail((IBinder)msg.obj);
1073 break;
1074 case CONFIGURATION_CHANGED:
1075 handleConfigurationChanged((Configuration)msg.obj);
1076 break;
1077 case CLEAN_UP_CONTEXT:
1078 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1079 cci.context.performFinalCleanup(cci.who, cci.what);
1080 break;
1081 case GC_WHEN_IDLE:
1082 scheduleGcIdler();
1083 break;
1084 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001085 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 break;
1087 case LOW_MEMORY:
1088 handleLowMemory();
1089 break;
1090 case ACTIVITY_CONFIGURATION_CHANGED:
1091 handleActivityConfigurationChanged((IBinder)msg.obj);
1092 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001093 case PROFILER_CONTROL:
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07001094 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001095 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001096 case CREATE_BACKUP_AGENT:
1097 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1098 break;
1099 case DESTROY_BACKUP_AGENT:
1100 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1101 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001102 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001103 Process.killProcess(Process.myPid());
1104 break;
1105 case REMOVE_PROVIDER:
1106 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001107 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001108 case ENABLE_JIT:
1109 ensureJitEnabled();
1110 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001111 case DISPATCH_PACKAGE_BROADCAST:
1112 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1113 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001114 case SCHEDULE_CRASH:
1115 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001116 case DUMP_HEAP:
1117 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1118 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001119 case DUMP_ACTIVITY:
1120 handleDumpActivity((DumpComponentInfo)msg.obj);
1121 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001122 case SLEEPING:
1123 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1124 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001125 case SET_CORE_SETTINGS:
1126 handleSetCoreSettings((Bundle) msg.obj);
1127 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001129 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
Bob Leee5408332009-09-04 18:31:17 -07001131
1132 void maybeSnapshot() {
1133 if (mBoundApplication != null) {
Sen Hubde75702010-05-28 01:54:03 -07001134 // convert the *private* ActivityThread.PackageInfo to *public* known
1135 // android.content.pm.PackageInfo
1136 String packageName = mBoundApplication.info.mPackageName;
1137 android.content.pm.PackageInfo packageInfo = null;
1138 try {
1139 Context context = getSystemContext();
1140 if(context == null) {
1141 Log.e(TAG, "cannot get a valid context");
1142 return;
1143 }
1144 PackageManager pm = context.getPackageManager();
1145 if(pm == null) {
1146 Log.e(TAG, "cannot get a valid PackageManager");
1147 return;
1148 }
1149 packageInfo = pm.getPackageInfo(
1150 packageName, PackageManager.GET_ACTIVITIES);
1151 } catch (NameNotFoundException e) {
1152 Log.e(TAG, "cannot get package info for " + packageName, e);
1153 }
1154 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001155 }
1156 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 }
1158
1159 private final class Idler implements MessageQueue.IdleHandler {
1160 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001161 ActivityClientRecord a = mNewActivities;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 if (a != null) {
1163 mNewActivities = null;
1164 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001165 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001167 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 TAG, "Reporting idle of " + a +
1169 " finished=" +
1170 (a.activity != null ? a.activity.mFinished : false));
1171 if (a.activity != null && !a.activity.mFinished) {
1172 try {
Dianne Hackborne88846e2009-09-30 21:34:25 -07001173 am.activityIdle(a.token, a.createdConfig);
1174 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 } catch (RemoteException ex) {
1176 }
1177 }
1178 prev = a;
1179 a = a.nextIdle;
1180 prev.nextIdle = null;
1181 } while (a != null);
1182 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001183 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 return false;
1185 }
1186 }
1187
1188 final class GcIdler implements MessageQueue.IdleHandler {
1189 public final boolean queueIdle() {
1190 doGcIfNeeded();
1191 return false;
1192 }
1193 }
1194
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001195 private final static class ResourcesKey {
1196 final private String mResDir;
1197 final private float mScale;
1198 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001199
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001200 ResourcesKey(String resDir, float scale) {
1201 mResDir = resDir;
1202 mScale = scale;
1203 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1204 }
Bob Leee5408332009-09-04 18:31:17 -07001205
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001206 @Override
1207 public int hashCode() {
1208 return mHash;
1209 }
1210
1211 @Override
1212 public boolean equals(Object obj) {
1213 if (!(obj instanceof ResourcesKey)) {
1214 return false;
1215 }
1216 ResourcesKey peer = (ResourcesKey) obj;
1217 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1218 }
1219 }
1220
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001221 public static final ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001222 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001225 public static final String currentPackageName() {
1226 ActivityThread am = currentActivityThread();
1227 return (am != null && am.mBoundApplication != null)
1228 ? am.mBoundApplication.processName : null;
1229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001231 public static final Application currentApplication() {
1232 ActivityThread am = currentActivityThread();
1233 return am != null ? am.mInitialApplication : null;
1234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001236 public static IPackageManager getPackageManager() {
1237 if (sPackageManager != null) {
1238 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1239 return sPackageManager;
1240 }
1241 IBinder b = ServiceManager.getService("package");
1242 //Slog.v("PackageManager", "default service binder = " + b);
1243 sPackageManager = IPackageManager.Stub.asInterface(b);
1244 //Slog.v("PackageManager", "default service = " + sPackageManager);
1245 return sPackageManager;
1246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001248 DisplayMetrics getDisplayMetricsLocked(boolean forceUpdate) {
1249 if (mDisplayMetrics != null && !forceUpdate) {
1250 return mDisplayMetrics;
1251 }
1252 if (mDisplay == null) {
1253 WindowManager wm = WindowManagerImpl.getDefault();
1254 mDisplay = wm.getDefaultDisplay();
1255 }
1256 DisplayMetrics metrics = mDisplayMetrics = new DisplayMetrics();
1257 mDisplay.getMetrics(metrics);
1258 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1259 // + metrics.heightPixels + " den=" + metrics.density
1260 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
1261 return metrics;
1262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001264 /**
1265 * Creates the top level Resources for applications with the given compatibility info.
1266 *
1267 * @param resDir the resource directory.
1268 * @param compInfo the compability info. It will use the default compatibility info when it's
1269 * null.
1270 */
1271 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1272 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1273 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001275 // Resources is app scale dependent.
1276 if (false) {
1277 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1278 + compInfo.applicationScale);
1279 }
1280 WeakReference<Resources> wr = mActiveResources.get(key);
1281 r = wr != null ? wr.get() : null;
1282 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1283 if (r != null && r.getAssets().isUpToDate()) {
1284 if (false) {
1285 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1286 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1287 }
1288 return r;
1289 }
1290 }
1291
1292 //if (r != null) {
1293 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1294 // + r + " " + resDir);
1295 //}
1296
1297 AssetManager assets = new AssetManager();
1298 if (assets.addAssetPath(resDir) == 0) {
1299 return null;
1300 }
1301
1302 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
1303 DisplayMetrics metrics = getDisplayMetricsLocked(false);
1304 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1305 if (false) {
1306 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1307 + r.getConfiguration() + " appScale="
1308 + r.getCompatibilityInfo().applicationScale);
1309 }
1310
1311 synchronized (mPackages) {
1312 WeakReference<Resources> wr = mActiveResources.get(key);
1313 Resources existing = wr != null ? wr.get() : null;
1314 if (existing != null && existing.getAssets().isUpToDate()) {
1315 // Someone else already created the resources while we were
1316 // unlocked; go ahead and use theirs.
1317 r.getAssets().close();
1318 return existing;
1319 }
1320
1321 // XXX need to remove entries when weak references go away
1322 mActiveResources.put(key, new WeakReference<Resources>(r));
1323 return r;
1324 }
1325 }
1326
1327 /**
1328 * Creates the top level resources for the given package.
1329 */
1330 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
1331 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo);
1332 }
1333
1334 final Handler getHandler() {
1335 return mH;
1336 }
1337
1338 public final LoadedApk getPackageInfo(String packageName, int flags) {
1339 synchronized (mPackages) {
1340 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1342 ref = mPackages.get(packageName);
1343 } else {
1344 ref = mResourcePackages.get(packageName);
1345 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001346 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001347 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001348 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1349 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 if (packageInfo != null && (packageInfo.mResources == null
1351 || packageInfo.mResources.getAssets().isUpToDate())) {
1352 if (packageInfo.isSecurityViolation()
1353 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1354 throw new SecurityException(
1355 "Requesting code from " + packageName
1356 + " to be run in process "
1357 + mBoundApplication.processName
1358 + "/" + mBoundApplication.appInfo.uid);
1359 }
1360 return packageInfo;
1361 }
1362 }
1363
1364 ApplicationInfo ai = null;
1365 try {
1366 ai = getPackageManager().getApplicationInfo(packageName,
1367 PackageManager.GET_SHARED_LIBRARY_FILES);
1368 } catch (RemoteException e) {
1369 }
1370
1371 if (ai != null) {
1372 return getPackageInfo(ai, flags);
1373 }
1374
1375 return null;
1376 }
1377
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001378 public final LoadedApk getPackageInfo(ApplicationInfo ai, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1380 boolean securityViolation = includeCode && ai.uid != 0
1381 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1382 ? ai.uid != mBoundApplication.appInfo.uid : true);
1383 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1384 |Context.CONTEXT_IGNORE_SECURITY))
1385 == Context.CONTEXT_INCLUDE_CODE) {
1386 if (securityViolation) {
1387 String msg = "Requesting code from " + ai.packageName
1388 + " (with uid " + ai.uid + ")";
1389 if (mBoundApplication != null) {
1390 msg = msg + " to be run in process "
1391 + mBoundApplication.processName + " (with uid "
1392 + mBoundApplication.appInfo.uid + ")";
1393 }
1394 throw new SecurityException(msg);
1395 }
1396 }
1397 return getPackageInfo(ai, null, securityViolation, includeCode);
1398 }
1399
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001400 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 return getPackageInfo(ai, null, false, true);
1402 }
1403
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001404 private final LoadedApk getPackageInfo(ApplicationInfo aInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1406 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001407 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 if (includeCode) {
1409 ref = mPackages.get(aInfo.packageName);
1410 } else {
1411 ref = mResourcePackages.get(aInfo.packageName);
1412 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001413 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 if (packageInfo == null || (packageInfo.mResources != null
1415 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001416 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 : "Loading resource-only package ") + aInfo.packageName
1418 + " (in " + (mBoundApplication != null
1419 ? mBoundApplication.processName : null)
1420 + ")");
1421 packageInfo =
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001422 new LoadedApk(this, aInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 securityViolation, includeCode &&
1424 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1425 if (includeCode) {
1426 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001427 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 } else {
1429 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001430 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 }
1433 return packageInfo;
1434 }
1435 }
1436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 ActivityThread() {
1438 }
1439
1440 public ApplicationThread getApplicationThread()
1441 {
1442 return mAppThread;
1443 }
1444
1445 public Instrumentation getInstrumentation()
1446 {
1447 return mInstrumentation;
1448 }
1449
1450 public Configuration getConfiguration() {
1451 return mConfiguration;
1452 }
1453
1454 public boolean isProfiling() {
1455 return mBoundApplication != null && mBoundApplication.profileFile != null;
1456 }
1457
1458 public String getProfileFilePath() {
1459 return mBoundApplication.profileFile;
1460 }
1461
1462 public Looper getLooper() {
1463 return mLooper;
1464 }
1465
1466 public Application getApplication() {
1467 return mInitialApplication;
1468 }
Bob Leee5408332009-09-04 18:31:17 -07001469
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001470 public String getProcessName() {
1471 return mBoundApplication.processName;
1472 }
Bob Leee5408332009-09-04 18:31:17 -07001473
Dianne Hackborn21556372010-02-04 16:34:40 -08001474 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 synchronized (this) {
1476 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001477 ContextImpl context =
1478 ContextImpl.createSystemContext(this);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001479 LoadedApk info = new LoadedApk(this, "android", context, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 context.init(info, null, this);
1481 context.getResources().updateConfiguration(
1482 getConfiguration(), getDisplayMetricsLocked(false));
1483 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001484 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 // + ": " + context.getResources().getConfiguration());
1486 }
1487 }
1488 return mSystemContext;
1489 }
1490
Mike Cleron432b7132009-09-24 15:28:29 -07001491 public void installSystemApplicationInfo(ApplicationInfo info) {
1492 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001493 ContextImpl context = getSystemContext();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001494 context.init(new LoadedApk(this, "android", context, info), null, this);
Mike Cleron432b7132009-09-24 15:28:29 -07001495 }
1496 }
1497
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001498 void ensureJitEnabled() {
1499 if (!mJitEnabled) {
1500 mJitEnabled = true;
1501 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1502 }
1503 }
1504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 void scheduleGcIdler() {
1506 if (!mGcIdlerScheduled) {
1507 mGcIdlerScheduled = true;
1508 Looper.myQueue().addIdleHandler(mGcIdler);
1509 }
1510 mH.removeMessages(H.GC_WHEN_IDLE);
1511 }
1512
1513 void unscheduleGcIdler() {
1514 if (mGcIdlerScheduled) {
1515 mGcIdlerScheduled = false;
1516 Looper.myQueue().removeIdleHandler(mGcIdler);
1517 }
1518 mH.removeMessages(H.GC_WHEN_IDLE);
1519 }
1520
1521 void doGcIfNeeded() {
1522 mGcIdlerScheduled = false;
1523 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001524 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 // + "m now=" + now);
1526 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001527 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 BinderInternal.forceGc("bg");
1529 }
1530 }
1531
Jeff Hamilton52d32032011-01-08 15:31:26 -06001532 public void registerOnActivityPausedListener(Activity activity,
1533 OnActivityPausedListener listener) {
1534 synchronized (mOnPauseListeners) {
1535 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1536 if (list == null) {
1537 list = new ArrayList<OnActivityPausedListener>();
1538 mOnPauseListeners.put(activity, list);
1539 }
1540 list.add(listener);
1541 }
1542 }
1543
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001544 public void unregisterOnActivityPausedListener(Activity activity,
1545 OnActivityPausedListener listener) {
1546 synchronized (mOnPauseListeners) {
1547 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1548 if (list != null) {
1549 list.remove(listener);
1550 }
1551 }
1552 }
1553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 public final ActivityInfo resolveActivityInfo(Intent intent) {
1555 ActivityInfo aInfo = intent.resolveActivityInfo(
1556 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1557 if (aInfo == null) {
1558 // Throw an exception.
1559 Instrumentation.checkStartActivityResult(
1560 IActivityManager.START_CLASS_NOT_FOUND, intent);
1561 }
1562 return aInfo;
1563 }
Bob Leee5408332009-09-04 18:31:17 -07001564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001567 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001568 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001570 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 r.intent = intent;
1572 r.state = state;
1573 r.parent = parent;
1574 r.embeddedID = id;
1575 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001576 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 if (localLOGV) {
1578 ComponentName compname = intent.getComponent();
1579 String name;
1580 if (compname != null) {
1581 name = compname.toShortString();
1582 } else {
1583 name = "(Intent " + intent + ").getComponent() returned null";
1584 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001585 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 + ", comp=" + name
1587 + ", token=" + token);
1588 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001589 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
1592 public final Activity getActivity(IBinder token) {
1593 return mActivities.get(token).activity;
1594 }
1595
1596 public final void sendActivityResult(
1597 IBinder token, String id, int requestCode,
1598 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001599 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001600 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1602 list.add(new ResultInfo(id, requestCode, resultCode, data));
1603 mAppThread.scheduleSendResult(token, list);
1604 }
1605
1606 // if the thread hasn't started yet, we don't have the handler, so just
1607 // save the messages until we're ready.
1608 private final void queueOrSendMessage(int what, Object obj) {
1609 queueOrSendMessage(what, obj, 0, 0);
1610 }
1611
1612 private final void queueOrSendMessage(int what, Object obj, int arg1) {
1613 queueOrSendMessage(what, obj, arg1, 0);
1614 }
1615
1616 private final void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
1617 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001618 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1620 + ": " + arg1 + " / " + obj);
1621 Message msg = Message.obtain();
1622 msg.what = what;
1623 msg.obj = obj;
1624 msg.arg1 = arg1;
1625 msg.arg2 = arg2;
1626 mH.sendMessage(msg);
1627 }
1628 }
1629
Dianne Hackborn21556372010-02-04 16:34:40 -08001630 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 String what) {
1632 ContextCleanupInfo cci = new ContextCleanupInfo();
1633 cci.context = context;
1634 cci.who = who;
1635 cci.what = what;
1636 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1637 }
1638
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001639 private final Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1641
1642 ActivityInfo aInfo = r.activityInfo;
1643 if (r.packageInfo == null) {
1644 r.packageInfo = getPackageInfo(aInfo.applicationInfo,
1645 Context.CONTEXT_INCLUDE_CODE);
1646 }
Bob Leee5408332009-09-04 18:31:17 -07001647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 ComponentName component = r.intent.getComponent();
1649 if (component == null) {
1650 component = r.intent.resolveActivity(
1651 mInitialApplication.getPackageManager());
1652 r.intent.setComponent(component);
1653 }
1654
1655 if (r.activityInfo.targetActivity != null) {
1656 component = new ComponentName(r.activityInfo.packageName,
1657 r.activityInfo.targetActivity);
1658 }
1659
1660 Activity activity = null;
1661 try {
1662 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1663 activity = mInstrumentation.newActivity(
1664 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001665 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 r.intent.setExtrasClassLoader(cl);
1667 if (r.state != null) {
1668 r.state.setClassLoader(cl);
1669 }
1670 } catch (Exception e) {
1671 if (!mInstrumentation.onException(activity, e)) {
1672 throw new RuntimeException(
1673 "Unable to instantiate activity " + component
1674 + ": " + e.toString(), e);
1675 }
1676 }
1677
1678 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001679 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001680
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001681 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1682 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 TAG, r + ": app=" + app
1684 + ", appName=" + app.getPackageName()
1685 + ", pkg=" + r.packageInfo.getPackageName()
1686 + ", comp=" + r.intent.getComponent().toShortString()
1687 + ", dir=" + r.packageInfo.getAppDir());
1688
1689 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001690 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 appContext.init(r.packageInfo, r.token, this);
1692 appContext.setOuterContext(activity);
1693 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
1694 Configuration config = new Configuration(mConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001695 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001696 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001697 activity.attach(appContext, this, getInstrumentation(), r.token,
1698 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001699 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001700
Christopher Tateb70f3df2009-04-07 16:07:59 -07001701 if (customIntent != null) {
1702 activity.mIntent = customIntent;
1703 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001704 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 activity.mStartedActivity = false;
1706 int theme = r.activityInfo.getThemeResource();
1707 if (theme != 0) {
1708 activity.setTheme(theme);
1709 }
1710
1711 activity.mCalled = false;
1712 mInstrumentation.callActivityOnCreate(activity, r.state);
1713 if (!activity.mCalled) {
1714 throw new SuperNotCalledException(
1715 "Activity " + r.intent.getComponent().toShortString() +
1716 " did not call through to super.onCreate()");
1717 }
1718 r.activity = activity;
1719 r.stopped = true;
1720 if (!r.activity.mFinished) {
1721 activity.performStart();
1722 r.stopped = false;
1723 }
1724 if (!r.activity.mFinished) {
1725 if (r.state != null) {
1726 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1727 }
1728 }
1729 if (!r.activity.mFinished) {
1730 activity.mCalled = false;
1731 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1732 if (!activity.mCalled) {
1733 throw new SuperNotCalledException(
1734 "Activity " + r.intent.getComponent().toShortString() +
1735 " did not call through to super.onPostCreate()");
1736 }
1737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739 r.paused = true;
1740
1741 mActivities.put(r.token, r);
1742
1743 } catch (SuperNotCalledException e) {
1744 throw e;
1745
1746 } catch (Exception e) {
1747 if (!mInstrumentation.onException(activity, e)) {
1748 throw new RuntimeException(
1749 "Unable to start activity " + component
1750 + ": " + e.toString(), e);
1751 }
1752 }
1753
1754 return activity;
1755 }
1756
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001757 private final void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 // If we are getting ready to gc after going to the background, well
1759 // we are back active so skip it.
1760 unscheduleGcIdler();
1761
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001762 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001764 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765
1766 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001767 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001768 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 handleResumeActivity(r.token, false, r.isForward);
1770
1771 if (!r.activity.mFinished && r.startsNotResumed) {
1772 // The activity manager actually wants this one to start out
1773 // paused, because it needs to be visible but isn't in the
1774 // foreground. We accomplish this by going through the
1775 // normal startup (because activities expect to go through
1776 // onResume() the first time they run, before their window
1777 // is displayed), and then pausing it. However, in this case
1778 // we do -not- need to do the full pause cycle (of freezing
1779 // and such) because the activity manager assumes it can just
1780 // retain the current state it has.
1781 try {
1782 r.activity.mCalled = false;
1783 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001784 // We need to keep around the original state, in case
1785 // we need to be created again.
1786 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 if (!r.activity.mCalled) {
1788 throw new SuperNotCalledException(
1789 "Activity " + r.intent.getComponent().toShortString() +
1790 " did not call through to super.onPause()");
1791 }
1792
1793 } catch (SuperNotCalledException e) {
1794 throw e;
1795
1796 } catch (Exception e) {
1797 if (!mInstrumentation.onException(r.activity, e)) {
1798 throw new RuntimeException(
1799 "Unable to pause activity "
1800 + r.intent.getComponent().toShortString()
1801 + ": " + e.toString(), e);
1802 }
1803 }
1804 r.paused = true;
1805 }
1806 } else {
1807 // If there was an error, for any reason, tell the activity
1808 // manager to stop us.
1809 try {
1810 ActivityManagerNative.getDefault()
1811 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
1812 } catch (RemoteException ex) {
1813 }
1814 }
1815 }
1816
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001817 private final void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 List<Intent> intents) {
1819 final int N = intents.size();
1820 for (int i=0; i<N; i++) {
1821 Intent intent = intents.get(i);
1822 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001823 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
1825 }
1826 }
1827
1828 public final void performNewIntents(IBinder token,
1829 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001830 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 if (r != null) {
1832 final boolean resumed = !r.paused;
1833 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001834 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 mInstrumentation.callActivityOnPause(r.activity);
1836 }
1837 deliverNewIntents(r, intents);
1838 if (resumed) {
1839 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07001840 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842 }
1843 }
Bob Leee5408332009-09-04 18:31:17 -07001844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 private final void handleNewIntent(NewIntentData data) {
1846 performNewIntents(data.token, data.intents);
1847 }
1848
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001849 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
1850
1851 /**
1852 * Return the Intent that's currently being handled by a
1853 * BroadcastReceiver on this thread, or null if none.
1854 * @hide
1855 */
1856 public static Intent getIntentBeingBroadcast() {
1857 return sCurrentBroadcastIntent.get();
1858 }
1859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 private final void handleReceiver(ReceiverData data) {
1861 // If we are getting ready to gc after going to the background, well
1862 // we are back active so skip it.
1863 unscheduleGcIdler();
1864
1865 String component = data.intent.getComponent().getClassName();
1866
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001867 LoadedApk packageInfo = getPackageInfoNoCheck(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 data.info.applicationInfo);
1869
1870 IActivityManager mgr = ActivityManagerNative.getDefault();
1871
1872 BroadcastReceiver receiver = null;
1873 try {
1874 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1875 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001876 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
1878 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001879 if (DEBUG_BROADCAST) Slog.i(TAG,
1880 "Finishing failed broadcast to " + data.intent.getComponent());
1881 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 throw new RuntimeException(
1883 "Unable to instantiate receiver " + component
1884 + ": " + e.toString(), e);
1885 }
1886
1887 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001888 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001889
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001890 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 TAG, "Performing receive of " + data.intent
1892 + ": app=" + app
1893 + ", appName=" + app.getPackageName()
1894 + ", pkg=" + packageInfo.getPackageName()
1895 + ", comp=" + data.intent.getComponent().toShortString()
1896 + ", dir=" + packageInfo.getAppDir());
1897
Dianne Hackborn21556372010-02-04 16:34:40 -08001898 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001899 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07001900 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 receiver.onReceive(context.getReceiverRestrictedContext(),
1902 data.intent);
1903 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07001904 if (DEBUG_BROADCAST) Slog.i(TAG,
1905 "Finishing failed broadcast to " + data.intent.getComponent());
1906 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 if (!mInstrumentation.onException(receiver, e)) {
1908 throw new RuntimeException(
1909 "Unable to start receiver " + component
1910 + ": " + e.toString(), e);
1911 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07001912 } finally {
1913 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 }
1915
Dianne Hackborne829fef2010-10-26 17:44:01 -07001916 if (receiver.getPendingResult() != null) {
1917 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 }
1920
Christopher Tate181fafa2009-05-14 11:12:14 -07001921 // Instantiate a BackupAgent and tell it that it's alive
1922 private final void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001923 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07001924
1925 // no longer idle; we have backup work to do
1926 unscheduleGcIdler();
1927
1928 // instantiate the BackupAgent class named in the manifest
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001929 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001930 String packageName = packageInfo.mPackageName;
1931 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001932 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07001933 + " already exists");
1934 return;
1935 }
Bob Leee5408332009-09-04 18:31:17 -07001936
Christopher Tate181fafa2009-05-14 11:12:14 -07001937 BackupAgent agent = null;
1938 String classname = data.appInfo.backupAgentName;
1939 if (classname == null) {
1940 if (data.backupMode == IApplicationThread.BACKUP_MODE_INCREMENTAL) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001941 Slog.e(TAG, "Attempted incremental backup but no defined agent for "
Christopher Tate181fafa2009-05-14 11:12:14 -07001942 + packageName);
1943 return;
1944 }
1945 classname = "android.app.FullBackupAgent";
1946 }
1947 try {
Christopher Tated1475e02009-07-09 15:36:17 -07001948 IBinder binder = null;
1949 try {
1950 java.lang.ClassLoader cl = packageInfo.getClassLoader();
1951 agent = (BackupAgent) cl.loadClass(data.appInfo.backupAgentName).newInstance();
1952
1953 // set up the agent's context
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001954 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing BackupAgent "
Christopher Tated1475e02009-07-09 15:36:17 -07001955 + data.appInfo.backupAgentName);
1956
Dianne Hackborn21556372010-02-04 16:34:40 -08001957 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07001958 context.init(packageInfo, null, this);
1959 context.setOuterContext(agent);
1960 agent.attach(context);
1961
1962 agent.onCreate();
1963 binder = agent.onBind();
1964 mBackupAgents.put(packageName, agent);
1965 } catch (Exception e) {
1966 // If this is during restore, fail silently; otherwise go
1967 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001968 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tated1475e02009-07-09 15:36:17 -07001969 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE) {
1970 throw e;
1971 }
1972 // falling through with 'binder' still null
1973 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001974
1975 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07001976 try {
1977 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
1978 } catch (RemoteException e) {
1979 // nothing to do.
1980 }
Christopher Tate181fafa2009-05-14 11:12:14 -07001981 } catch (Exception e) {
1982 throw new RuntimeException("Unable to create BackupAgent "
1983 + data.appInfo.backupAgentName + ": " + e.toString(), e);
1984 }
1985 }
1986
1987 // Tear down a BackupAgent
1988 private final void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001989 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07001990
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001991 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001992 String packageName = packageInfo.mPackageName;
1993 BackupAgent agent = mBackupAgents.get(packageName);
1994 if (agent != null) {
1995 try {
1996 agent.onDestroy();
1997 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001998 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07001999 e.printStackTrace();
2000 }
2001 mBackupAgents.remove(packageName);
2002 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002003 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002004 }
2005 }
2006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 private final void handleCreateService(CreateServiceData data) {
2008 // If we are getting ready to gc after going to the background, well
2009 // we are back active so skip it.
2010 unscheduleGcIdler();
2011
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002012 LoadedApk packageInfo = getPackageInfoNoCheck(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 data.info.applicationInfo);
2014 Service service = null;
2015 try {
2016 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2017 service = (Service) cl.loadClass(data.info.name).newInstance();
2018 } catch (Exception e) {
2019 if (!mInstrumentation.onException(service, e)) {
2020 throw new RuntimeException(
2021 "Unable to instantiate service " + data.info.name
2022 + ": " + e.toString(), e);
2023 }
2024 }
2025
2026 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002027 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028
Dianne Hackborn21556372010-02-04 16:34:40 -08002029 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 context.init(packageInfo, null, this);
2031
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002032 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 context.setOuterContext(service);
2034 service.attach(context, this, data.info.name, data.token, app,
2035 ActivityManagerNative.getDefault());
2036 service.onCreate();
2037 mServices.put(data.token, service);
2038 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002039 ActivityManagerNative.getDefault().serviceDoneExecuting(
2040 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 } catch (RemoteException e) {
2042 // nothing to do.
2043 }
2044 } catch (Exception e) {
2045 if (!mInstrumentation.onException(service, e)) {
2046 throw new RuntimeException(
2047 "Unable to create service " + data.info.name
2048 + ": " + e.toString(), e);
2049 }
2050 }
2051 }
2052
2053 private final void handleBindService(BindServiceData data) {
2054 Service s = mServices.get(data.token);
2055 if (s != null) {
2056 try {
2057 data.intent.setExtrasClassLoader(s.getClassLoader());
2058 try {
2059 if (!data.rebind) {
2060 IBinder binder = s.onBind(data.intent);
2061 ActivityManagerNative.getDefault().publishService(
2062 data.token, data.intent, binder);
2063 } else {
2064 s.onRebind(data.intent);
2065 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002066 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002068 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 } catch (RemoteException ex) {
2070 }
2071 } catch (Exception e) {
2072 if (!mInstrumentation.onException(s, e)) {
2073 throw new RuntimeException(
2074 "Unable to bind to service " + s
2075 + " with " + data.intent + ": " + e.toString(), e);
2076 }
2077 }
2078 }
2079 }
2080
2081 private final void handleUnbindService(BindServiceData data) {
2082 Service s = mServices.get(data.token);
2083 if (s != null) {
2084 try {
2085 data.intent.setExtrasClassLoader(s.getClassLoader());
2086 boolean doRebind = s.onUnbind(data.intent);
2087 try {
2088 if (doRebind) {
2089 ActivityManagerNative.getDefault().unbindFinished(
2090 data.token, data.intent, doRebind);
2091 } else {
2092 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002093 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095 } catch (RemoteException ex) {
2096 }
2097 } catch (Exception e) {
2098 if (!mInstrumentation.onException(s, e)) {
2099 throw new RuntimeException(
2100 "Unable to unbind to service " + s
2101 + " with " + data.intent + ": " + e.toString(), e);
2102 }
2103 }
2104 }
2105 }
2106
Dianne Hackborn625ac272010-09-17 18:29:22 -07002107 private void handleDumpService(DumpComponentInfo info) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 try {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002109 Service s = mServices.get(info.token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 if (s != null) {
2111 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
2112 s.dump(info.fd, pw, info.args);
2113 pw.close();
2114 }
2115 } finally {
2116 synchronized (info) {
2117 info.dumped = true;
2118 info.notifyAll();
2119 }
2120 }
2121 }
2122
Dianne Hackborn625ac272010-09-17 18:29:22 -07002123 private void handleDumpActivity(DumpComponentInfo info) {
2124 try {
2125 ActivityClientRecord r = mActivities.get(info.token);
2126 if (r != null && r.activity != null) {
2127 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd));
Dianne Hackborn30d71892010-12-11 10:37:55 -08002128 r.activity.dump(info.prefix, info.fd, pw, info.args);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002129 pw.close();
2130 }
2131 } finally {
2132 synchronized (info) {
2133 info.dumped = true;
2134 info.notifyAll();
2135 }
2136 }
2137 }
2138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 private final void handleServiceArgs(ServiceArgsData data) {
2140 Service s = mServices.get(data.token);
2141 if (s != null) {
2142 try {
2143 if (data.args != null) {
2144 data.args.setExtrasClassLoader(s.getClassLoader());
2145 }
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002146 int res = s.onStartCommand(data.args, data.flags, data.startId);
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002147
2148 QueuedWork.waitToFinish();
2149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002151 ActivityManagerNative.getDefault().serviceDoneExecuting(
2152 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 } catch (RemoteException e) {
2154 // nothing to do.
2155 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002156 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 } catch (Exception e) {
2158 if (!mInstrumentation.onException(s, e)) {
2159 throw new RuntimeException(
2160 "Unable to start service " + s
2161 + " with " + data.args + ": " + e.toString(), e);
2162 }
2163 }
2164 }
2165 }
2166
2167 private final void handleStopService(IBinder token) {
2168 Service s = mServices.remove(token);
2169 if (s != null) {
2170 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002171 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 s.onDestroy();
2173 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002174 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002176 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002178
2179 QueuedWork.waitToFinish();
2180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002182 ActivityManagerNative.getDefault().serviceDoneExecuting(
2183 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 } catch (RemoteException e) {
2185 // nothing to do.
2186 }
2187 } catch (Exception e) {
2188 if (!mInstrumentation.onException(s, e)) {
2189 throw new RuntimeException(
2190 "Unable to stop service " + s
2191 + ": " + e.toString(), e);
2192 }
2193 }
2194 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002195 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
2197
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002198 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002200 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002201 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 + " finished=" + r.activity.mFinished);
2203 if (r != null && !r.activity.mFinished) {
2204 if (clearHide) {
2205 r.hideForNow = false;
2206 r.activity.mStartedActivity = false;
2207 }
2208 try {
2209 if (r.pendingIntents != null) {
2210 deliverNewIntents(r, r.pendingIntents);
2211 r.pendingIntents = null;
2212 }
2213 if (r.pendingResults != null) {
2214 deliverResults(r, r.pendingResults);
2215 r.pendingResults = null;
2216 }
2217 r.activity.performResume();
2218
Bob Leee5408332009-09-04 18:31:17 -07002219 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 r.paused = false;
2223 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 r.state = null;
2225 } catch (Exception e) {
2226 if (!mInstrumentation.onException(r.activity, e)) {
2227 throw new RuntimeException(
2228 "Unable to resume activity "
2229 + r.intent.getComponent().toShortString()
2230 + ": " + e.toString(), e);
2231 }
2232 }
2233 }
2234 return r;
2235 }
2236
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002237 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2238 if (r.mPendingRemoveWindow != null) {
2239 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2240 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2241 if (wtoken != null) {
2242 WindowManagerImpl.getDefault().closeAll(wtoken,
2243 r.activity.getClass().getName(), "Activity");
2244 }
2245 }
2246 r.mPendingRemoveWindow = null;
2247 r.mPendingRemoveWindowManager = null;
2248 }
2249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2251 // If we are getting ready to gc after going to the background, well
2252 // we are back active so skip it.
2253 unscheduleGcIdler();
2254
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002255 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256
2257 if (r != null) {
2258 final Activity a = r.activity;
2259
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002260 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 TAG, "Resume " + r + " started activity: " +
2262 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2263 + ", finished: " + a.mFinished);
2264
2265 final int forwardBit = isForward ?
2266 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 // If the window hasn't yet been added to the window manager,
2269 // and this guy didn't finish itself or start another activity,
2270 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002271 boolean willBeVisible = !a.mStartedActivity;
2272 if (!willBeVisible) {
2273 try {
2274 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2275 a.getActivityToken());
2276 } catch (RemoteException e) {
2277 }
2278 }
2279 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 r.window = r.activity.getWindow();
2281 View decor = r.window.getDecorView();
2282 decor.setVisibility(View.INVISIBLE);
2283 ViewManager wm = a.getWindowManager();
2284 WindowManager.LayoutParams l = r.window.getAttributes();
2285 a.mDecor = decor;
2286 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2287 l.softInputMode |= forwardBit;
2288 if (a.mVisibleFromClient) {
2289 a.mWindowAdded = true;
2290 wm.addView(decor, l);
2291 }
2292
2293 // If the window has already been added, but during resume
2294 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002295 // window visible.
2296 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002297 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002298 TAG, "Launch " + r + " mStartedActivity set");
2299 r.hideForNow = true;
2300 }
2301
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002302 // Get rid of anything left hanging around.
2303 cleanUpPendingRemoveWindows(r);
2304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 // The window is now visible if it has been added, we are not
2306 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002307 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002308 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002310 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002311 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 performConfigurationChanged(r.activity, r.newConfig);
2313 r.newConfig = null;
2314 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002315 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 + isForward);
2317 WindowManager.LayoutParams l = r.window.getAttributes();
2318 if ((l.softInputMode
2319 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2320 != forwardBit) {
2321 l.softInputMode = (l.softInputMode
2322 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2323 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002324 if (r.activity.mVisibleFromClient) {
2325 ViewManager wm = a.getWindowManager();
2326 View decor = r.window.getDecorView();
2327 wm.updateViewLayout(decor, l);
2328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002329 }
2330 r.activity.mVisibleFromServer = true;
2331 mNumVisibleActivities++;
2332 if (r.activity.mVisibleFromClient) {
2333 r.activity.makeVisible();
2334 }
2335 }
2336
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002337 if (!r.onlyLocalRequest) {
2338 r.nextIdle = mNewActivities;
2339 mNewActivities = r;
2340 if (localLOGV) Slog.v(
2341 TAG, "Scheduling idle handler for " + r);
2342 Looper.myQueue().addIdleHandler(new Idler());
2343 }
2344 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345
2346 } else {
2347 // If an exception was thrown when trying to resume, then
2348 // just end this activity.
2349 try {
2350 ActivityManagerNative.getDefault()
2351 .finishActivity(token, Activity.RESULT_CANCELED, null);
2352 } catch (RemoteException ex) {
2353 }
2354 }
2355 }
2356
2357 private int mThumbnailWidth = -1;
2358 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002359 private Bitmap mAvailThumbnailBitmap = null;
2360 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002362 private final Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002363 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002365 if (thumbnail == null) {
2366 int w = mThumbnailWidth;
2367 int h;
2368 if (w < 0) {
2369 Resources res = r.activity.getResources();
2370 mThumbnailHeight = h =
2371 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002373 mThumbnailWidth = w =
2374 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2375 } else {
2376 h = mThumbnailHeight;
2377 }
2378
2379 // On platforms where we don't want thumbnails, set dims to (0,0)
2380 if ((w > 0) && (h > 0)) {
2381 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2382 thumbnail.eraseColor(0);
2383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
2385
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002386 if (thumbnail != null) {
2387 Canvas cv = mThumbnailCanvas;
2388 if (cv == null) {
2389 mThumbnailCanvas = cv = new Canvas();
2390 }
2391
2392 cv.setBitmap(thumbnail);
2393 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2394 mAvailThumbnailBitmap = thumbnail;
2395 thumbnail = null;
2396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 } catch (Exception e) {
2400 if (!mInstrumentation.onException(r.activity, e)) {
2401 throw new RuntimeException(
2402 "Unable to create thumbnail of "
2403 + r.intent.getComponent().toShortString()
2404 + ": " + e.toString(), e);
2405 }
2406 thumbnail = null;
2407 }
2408
2409 return thumbnail;
2410 }
2411
2412 private final void handlePauseActivity(IBinder token, boolean finished,
2413 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002414 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002416 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (userLeaving) {
2418 performUserLeavingActivity(r);
2419 }
Bob Leee5408332009-09-04 18:31:17 -07002420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002422 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002424 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002425 if (r.isPreHoneycomb()) {
2426 QueuedWork.waitToFinish();
2427 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 // Tell the activity manager we have paused.
2430 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002431 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 } catch (RemoteException ex) {
2433 }
2434 }
2435 }
2436
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002437 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 mInstrumentation.callActivityOnUserLeaving(r.activity);
2439 }
2440
2441 final Bundle performPauseActivity(IBinder token, boolean finished,
2442 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002443 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 return r != null ? performPauseActivity(r, finished, saveState) : null;
2445 }
2446
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002447 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 boolean saveState) {
2449 if (r.paused) {
2450 if (r.activity.mFinished) {
2451 // If we are finishing, we won't call onResume() in certain cases.
2452 // So here we likewise don't want to call onPause() if the activity
2453 // isn't resumed.
2454 return null;
2455 }
2456 RuntimeException e = new RuntimeException(
2457 "Performing pause of activity that is not resumed: "
2458 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002459 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 }
2461 Bundle state = null;
2462 if (finished) {
2463 r.activity.mFinished = true;
2464 }
2465 try {
2466 // Next have the activity save its current state and managed dialogs...
2467 if (!r.activity.mFinished && saveState) {
2468 state = new Bundle();
2469 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2470 r.state = state;
2471 }
2472 // Now we are idle.
2473 r.activity.mCalled = false;
2474 mInstrumentation.callActivityOnPause(r.activity);
2475 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2476 if (!r.activity.mCalled) {
2477 throw new SuperNotCalledException(
2478 "Activity " + r.intent.getComponent().toShortString() +
2479 " did not call through to super.onPause()");
2480 }
2481
2482 } catch (SuperNotCalledException e) {
2483 throw e;
2484
2485 } catch (Exception e) {
2486 if (!mInstrumentation.onException(r.activity, e)) {
2487 throw new RuntimeException(
2488 "Unable to pause activity "
2489 + r.intent.getComponent().toShortString()
2490 + ": " + e.toString(), e);
2491 }
2492 }
2493 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002494
2495 // Notify any outstanding on paused listeners
2496 ArrayList<OnActivityPausedListener> listeners;
2497 synchronized (mOnPauseListeners) {
2498 listeners = mOnPauseListeners.remove(r.activity);
2499 }
2500 int size = (listeners != null ? listeners.size() : 0);
2501 for (int i = 0; i < size; i++) {
2502 listeners.get(i).onPaused(r.activity);
2503 }
2504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 return state;
2506 }
2507
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002508 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002509 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002510 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 }
2512
2513 private static class StopInfo {
2514 Bitmap thumbnail;
2515 CharSequence description;
2516 }
2517
2518 private final class ProviderRefCount {
2519 public int count;
2520 ProviderRefCount(int pCount) {
2521 count = pCount;
2522 }
2523 }
2524
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002525 /**
2526 * Core implementation of stopping an activity. Note this is a little
2527 * tricky because the server's meaning of stop is slightly different
2528 * than our client -- for the server, stop means to save state and give
2529 * it the result when it is done, but the window may still be visible.
2530 * For the client, we want to call onStop()/onStart() to indicate when
2531 * the activity's UI visibillity changes.
2532 */
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002533 private final void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002534 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002535 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002536 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 if (r != null) {
2538 if (!keepShown && r.stopped) {
2539 if (r.activity.mFinished) {
2540 // If we are finishing, we won't call onResume() in certain
2541 // cases. So here we likewise don't want to call onStop()
2542 // if the activity isn't resumed.
2543 return;
2544 }
2545 RuntimeException e = new RuntimeException(
2546 "Performing stop of activity that is not resumed: "
2547 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002548 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
2550
2551 if (info != null) {
2552 try {
2553 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002554 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 info.description = r.activity.onCreateDescription();
2556 } catch (Exception e) {
2557 if (!mInstrumentation.onException(r.activity, e)) {
2558 throw new RuntimeException(
2559 "Unable to save state of activity "
2560 + r.intent.getComponent().toShortString()
2561 + ": " + e.toString(), e);
2562 }
2563 }
2564 }
2565
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002566 // Next have the activity save its current state and managed dialogs...
2567 if (!r.activity.mFinished && saveState) {
2568 if (r.state == null) {
2569 state = new Bundle();
2570 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2571 r.state = state;
2572 } else {
2573 state = r.state;
2574 }
2575 }
2576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 if (!keepShown) {
2578 try {
2579 // Now we are idle.
2580 r.activity.performStop();
2581 } catch (Exception e) {
2582 if (!mInstrumentation.onException(r.activity, e)) {
2583 throw new RuntimeException(
2584 "Unable to stop activity "
2585 + r.intent.getComponent().toShortString()
2586 + ": " + e.toString(), e);
2587 }
2588 }
2589 r.stopped = true;
2590 }
2591
2592 r.paused = true;
2593 }
2594 }
2595
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002596 private final void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 View v = r.activity.mDecor;
2598 if (v != null) {
2599 if (show) {
2600 if (!r.activity.mVisibleFromServer) {
2601 r.activity.mVisibleFromServer = true;
2602 mNumVisibleActivities++;
2603 if (r.activity.mVisibleFromClient) {
2604 r.activity.makeVisible();
2605 }
2606 }
2607 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002608 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002609 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 performConfigurationChanged(r.activity, r.newConfig);
2611 r.newConfig = null;
2612 }
2613 } else {
2614 if (r.activity.mVisibleFromServer) {
2615 r.activity.mVisibleFromServer = false;
2616 mNumVisibleActivities--;
2617 v.setVisibility(View.INVISIBLE);
2618 }
2619 }
2620 }
2621 }
2622
2623 private final void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002624 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 r.activity.mConfigChangeFlags |= configChanges;
2626
2627 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002628 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002630 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 TAG, "Finishing stop of " + r + ": show=" + show
2632 + " win=" + r.window);
2633
2634 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002635
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002636 // Make sure any pending writes are now committed.
2637 if (!r.isPreHoneycomb()) {
2638 QueuedWork.waitToFinish();
2639 }
2640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 // Tell activity manager we have been stopped.
2642 try {
2643 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002644 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 } catch (RemoteException ex) {
2646 }
2647 }
2648
2649 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002650 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 if (r.stopped) {
2652 r.activity.performRestart();
2653 r.stopped = false;
2654 }
2655 }
2656
2657 private final void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002658 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002659
2660 if (r == null) {
2661 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2662 return;
2663 }
2664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002666 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 } else if (show && r.stopped) {
2668 // If we are getting ready to gc after going to the background, well
2669 // we are back active so skip it.
2670 unscheduleGcIdler();
2671
2672 r.activity.performRestart();
2673 r.stopped = false;
2674 }
2675 if (r.activity.mDecor != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002676 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 TAG, "Handle window " + r + " visibility: " + show);
2678 updateVisibility(r, show);
2679 }
2680 }
2681
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002682 private final void handleSleeping(IBinder token, boolean sleeping) {
2683 ActivityClientRecord r = mActivities.get(token);
2684
2685 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002686 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002687 return;
2688 }
2689
2690 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002691 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002692 try {
2693 // Now we are idle.
2694 r.activity.performStop();
2695 } catch (Exception e) {
2696 if (!mInstrumentation.onException(r.activity, e)) {
2697 throw new RuntimeException(
2698 "Unable to stop activity "
2699 + r.intent.getComponent().toShortString()
2700 + ": " + e.toString(), e);
2701 }
2702 }
2703 r.stopped = true;
2704 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002705
2706 // Make sure any pending writes are now committed.
2707 if (!r.isPreHoneycomb()) {
2708 QueuedWork.waitToFinish();
2709 }
2710
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002711 // Tell activity manager we slept.
2712 try {
2713 ActivityManagerNative.getDefault().activitySlept(r.token);
2714 } catch (RemoteException ex) {
2715 }
2716 } else {
2717 if (r.stopped && r.activity.mVisibleFromServer) {
2718 r.activity.performRestart();
2719 r.stopped = false;
2720 }
2721 }
2722 }
2723
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002724 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002725 synchronized (mPackages) {
2726 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002727 }
2728 }
2729
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002730 private final void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 final int N = results.size();
2732 for (int i=0; i<N; i++) {
2733 ResultInfo ri = results.get(i);
2734 try {
2735 if (ri.mData != null) {
2736 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2737 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002738 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002739 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 r.activity.dispatchActivityResult(ri.mResultWho,
2741 ri.mRequestCode, ri.mResultCode, ri.mData);
2742 } catch (Exception e) {
2743 if (!mInstrumentation.onException(r.activity, e)) {
2744 throw new RuntimeException(
2745 "Failure delivering result " + ri + " to activity "
2746 + r.intent.getComponent().toShortString()
2747 + ": " + e.toString(), e);
2748 }
2749 }
2750 }
2751 }
2752
2753 private final void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002754 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002755 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 if (r != null) {
2757 final boolean resumed = !r.paused;
2758 if (!r.activity.mFinished && r.activity.mDecor != null
2759 && r.hideForNow && resumed) {
2760 // We had hidden the activity because it started another
2761 // one... we have gotten a result back and we are not
2762 // paused, so make sure our window is visible.
2763 updateVisibility(r, true);
2764 }
2765 if (resumed) {
2766 try {
2767 // Now we are idle.
2768 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002769 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 mInstrumentation.callActivityOnPause(r.activity);
2771 if (!r.activity.mCalled) {
2772 throw new SuperNotCalledException(
2773 "Activity " + r.intent.getComponent().toShortString()
2774 + " did not call through to super.onPause()");
2775 }
2776 } catch (SuperNotCalledException e) {
2777 throw e;
2778 } catch (Exception e) {
2779 if (!mInstrumentation.onException(r.activity, e)) {
2780 throw new RuntimeException(
2781 "Unable to pause activity "
2782 + r.intent.getComponent().toShortString()
2783 + ": " + e.toString(), e);
2784 }
2785 }
2786 }
2787 deliverResults(r, res.results);
2788 if (resumed) {
2789 mInstrumentation.callActivityOnResume(r.activity);
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002790 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 }
2792 }
2793 }
2794
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002795 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 return performDestroyActivity(token, finishing, 0, false);
2797 }
2798
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002799 private final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002801 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002802 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002803 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002805 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 r.activity.mConfigChangeFlags |= configChanges;
2807 if (finishing) {
2808 r.activity.mFinished = true;
2809 }
2810 if (!r.paused) {
2811 try {
2812 r.activity.mCalled = false;
2813 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07002814 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 r.activity.getComponentName().getClassName());
2816 if (!r.activity.mCalled) {
2817 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002818 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 + " did not call through to super.onPause()");
2820 }
2821 } catch (SuperNotCalledException e) {
2822 throw e;
2823 } catch (Exception e) {
2824 if (!mInstrumentation.onException(r.activity, e)) {
2825 throw new RuntimeException(
2826 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002827 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 + ": " + e.toString(), e);
2829 }
2830 }
2831 r.paused = true;
2832 }
2833 if (!r.stopped) {
2834 try {
2835 r.activity.performStop();
2836 } catch (SuperNotCalledException e) {
2837 throw e;
2838 } catch (Exception e) {
2839 if (!mInstrumentation.onException(r.activity, e)) {
2840 throw new RuntimeException(
2841 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002842 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002843 + ": " + e.toString(), e);
2844 }
2845 }
2846 r.stopped = true;
2847 }
2848 if (getNonConfigInstance) {
2849 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002850 r.lastNonConfigurationInstances
2851 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 } catch (Exception e) {
2853 if (!mInstrumentation.onException(r.activity, e)) {
2854 throw new RuntimeException(
2855 "Unable to retain activity "
2856 + r.intent.getComponent().toShortString()
2857 + ": " + e.toString(), e);
2858 }
2859 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 }
2861 try {
2862 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07002863 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 if (!r.activity.mCalled) {
2865 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002866 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 " did not call through to super.onDestroy()");
2868 }
2869 if (r.window != null) {
2870 r.window.closeAllPanels();
2871 }
2872 } catch (SuperNotCalledException e) {
2873 throw e;
2874 } catch (Exception e) {
2875 if (!mInstrumentation.onException(r.activity, e)) {
2876 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002877 "Unable to destroy activity " + safeToComponentShortString(r.intent)
2878 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 }
2880 }
2881 }
2882 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002883 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 return r;
2885 }
2886
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07002887 private static String safeToComponentShortString(Intent intent) {
2888 ComponentName component = intent.getComponent();
2889 return component == null ? "[Unknown]" : component.toShortString();
2890 }
2891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 private final void handleDestroyActivity(IBinder token, boolean finishing,
2893 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002894 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 configChanges, getNonConfigInstance);
2896 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002897 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 WindowManager wm = r.activity.getWindowManager();
2899 View v = r.activity.mDecor;
2900 if (v != null) {
2901 if (r.activity.mVisibleFromServer) {
2902 mNumVisibleActivities--;
2903 }
2904 IBinder wtoken = v.getWindowToken();
2905 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002906 if (r.onlyLocalRequest) {
2907 // Hold off on removing this until the new activity's
2908 // window is being added.
2909 r.mPendingRemoveWindow = v;
2910 r.mPendingRemoveWindowManager = wm;
2911 } else {
2912 wm.removeViewImmediate(v);
2913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002915 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 WindowManagerImpl.getDefault().closeAll(wtoken,
2917 r.activity.getClass().getName(), "Activity");
2918 }
2919 r.activity.mDecor = null;
2920 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002921 if (r.mPendingRemoveWindow == null) {
2922 // If we are delaying the removal of the activity window, then
2923 // we can't clean up all windows here. Note that we can't do
2924 // so later either, which means any windows that aren't closed
2925 // by the app will leak. Well we try to warning them a lot
2926 // about leaking windows, because that is a bug, so if they are
2927 // using this recreate facility then they get to live with leaks.
2928 WindowManagerImpl.getDefault().closeAll(token,
2929 r.activity.getClass().getName(), "Activity");
2930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002931
2932 // Mocked out contexts won't be participating in the normal
2933 // process lifecycle, but if we're running with a proper
2934 // ApplicationContext we need to have it tear down things
2935 // cleanly.
2936 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002937 if (c instanceof ContextImpl) {
2938 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 r.activity.getClass().getName(), "Activity");
2940 }
2941 }
2942 if (finishing) {
2943 try {
2944 ActivityManagerNative.getDefault().activityDestroyed(token);
2945 } catch (RemoteException ex) {
2946 // If the system process has died, it's game over for everyone.
2947 }
2948 }
2949 }
2950
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002951 public final void requestRelaunchActivity(IBinder token,
2952 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
2953 int configChanges, boolean notResumed, Configuration config,
2954 boolean fromServer) {
2955 ActivityClientRecord target = null;
2956
2957 synchronized (mPackages) {
2958 for (int i=0; i<mRelaunchingActivities.size(); i++) {
2959 ActivityClientRecord r = mRelaunchingActivities.get(i);
2960 if (r.token == token) {
2961 target = r;
2962 if (pendingResults != null) {
2963 if (r.pendingResults != null) {
2964 r.pendingResults.addAll(pendingResults);
2965 } else {
2966 r.pendingResults = pendingResults;
2967 }
2968 }
2969 if (pendingNewIntents != null) {
2970 if (r.pendingIntents != null) {
2971 r.pendingIntents.addAll(pendingNewIntents);
2972 } else {
2973 r.pendingIntents = pendingNewIntents;
2974 }
2975 }
2976 break;
2977 }
2978 }
2979
2980 if (target == null) {
2981 target = new ActivityClientRecord();
2982 target.token = token;
2983 target.pendingResults = pendingResults;
2984 target.pendingIntents = pendingNewIntents;
2985 if (!fromServer) {
2986 ActivityClientRecord existing = mActivities.get(token);
2987 if (existing != null) {
2988 target.startsNotResumed = existing.paused;
2989 }
2990 target.onlyLocalRequest = true;
2991 }
2992 mRelaunchingActivities.add(target);
2993 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
2994 }
2995
2996 if (fromServer) {
2997 target.startsNotResumed = notResumed;
2998 target.onlyLocalRequest = false;
2999 }
3000 if (config != null) {
3001 target.createdConfig = config;
3002 }
3003 target.pendingConfigChanges |= configChanges;
3004 }
3005 }
3006
3007 private final void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 // If we are getting ready to gc after going to the background, well
3009 // we are back active so skip it.
3010 unscheduleGcIdler();
3011
3012 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003013 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 // First: make sure we have the most recent configuration and most
3016 // recent version of the activity, or skip it if some previous call
3017 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003018 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 int N = mRelaunchingActivities.size();
3020 IBinder token = tmp.token;
3021 tmp = null;
3022 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003023 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 if (r.token == token) {
3025 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003026 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 mRelaunchingActivities.remove(i);
3028 i--;
3029 N--;
3030 }
3031 }
Bob Leee5408332009-09-04 18:31:17 -07003032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003034 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 return;
3036 }
Bob Leee5408332009-09-04 18:31:17 -07003037
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003038 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3039 + tmp.token + " with configChanges=0x"
3040 + Integer.toHexString(configChanges));
3041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 if (mPendingConfiguration != null) {
3043 changedConfig = mPendingConfiguration;
3044 mPendingConfiguration = null;
3045 }
3046 }
Bob Leee5408332009-09-04 18:31:17 -07003047
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003048 if (tmp.createdConfig != null) {
3049 // If the activity manager is passing us its current config,
3050 // assume that is really what we want regardless of what we
3051 // may have pending.
3052 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003053 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3054 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3055 if (changedConfig == null
3056 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3057 changedConfig = tmp.createdConfig;
3058 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003059 }
3060 }
3061
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003062 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003063 + tmp.token + ": changedConfig=" + changedConfig);
3064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 // If there was a pending configuration change, execute it first.
3066 if (changedConfig != null) {
3067 handleConfigurationChanged(changedConfig);
3068 }
Bob Leee5408332009-09-04 18:31:17 -07003069
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003070 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003071 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 if (r == null) {
3073 return;
3074 }
Bob Leee5408332009-09-04 18:31:17 -07003075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003077 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003078 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003079
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003080 r.activity.mChangingConfigurations = true;
3081
Dianne Hackborne2b04802010-12-09 09:24:55 -08003082 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003084 performPauseActivity(r.token, false, r.isPreHoneycomb());
3085 }
3086 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3087 r.state = new Bundle();
3088 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003089 }
Bob Leee5408332009-09-04 18:31:17 -07003090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 r.activity = null;
3094 r.window = null;
3095 r.hideForNow = false;
3096 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003097 // Merge any pending results and pending intents; don't just replace them
3098 if (tmp.pendingResults != null) {
3099 if (r.pendingResults == null) {
3100 r.pendingResults = tmp.pendingResults;
3101 } else {
3102 r.pendingResults.addAll(tmp.pendingResults);
3103 }
3104 }
3105 if (tmp.pendingIntents != null) {
3106 if (r.pendingIntents == null) {
3107 r.pendingIntents = tmp.pendingIntents;
3108 } else {
3109 r.pendingIntents.addAll(tmp.pendingIntents);
3110 }
3111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003113
Christopher Tateb70f3df2009-04-07 16:07:59 -07003114 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
3116
3117 private final void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003118 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 Bitmap thumbnail = createThumbnailBitmap(r);
3120 CharSequence description = null;
3121 try {
3122 description = r.activity.onCreateDescription();
3123 } catch (Exception e) {
3124 if (!mInstrumentation.onException(r.activity, e)) {
3125 throw new RuntimeException(
3126 "Unable to create description of activity "
3127 + r.intent.getComponent().toShortString()
3128 + ": " + e.toString(), e);
3129 }
3130 }
3131 //System.out.println("Reporting top thumbnail " + thumbnail);
3132 try {
3133 ActivityManagerNative.getDefault().reportThumbnail(
3134 token, thumbnail, description);
3135 } catch (RemoteException ex) {
3136 }
3137 }
3138
3139 ArrayList<ComponentCallbacks> collectComponentCallbacksLocked(
3140 boolean allActivities, Configuration newConfig) {
3141 ArrayList<ComponentCallbacks> callbacks
3142 = new ArrayList<ComponentCallbacks>();
Bob Leee5408332009-09-04 18:31:17 -07003143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003145 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003146 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003147 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 Activity a = ar.activity;
3149 if (a != null) {
3150 if (!ar.activity.mFinished && (allActivities ||
3151 (a != null && !ar.paused))) {
3152 // If the activity is currently resumed, its configuration
3153 // needs to change right now.
3154 callbacks.add(a);
3155 } else if (newConfig != null) {
3156 // Otherwise, we will tell it about the change
3157 // the next time it is resumed or shown. Note that
3158 // the activity manager may, before then, decide the
3159 // activity needs to be destroyed to handle its new
3160 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003161 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003162 + ar.activityInfo.name + " newConfig=" + newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 ar.newConfig = newConfig;
3164 }
3165 }
3166 }
3167 }
3168 if (mServices.size() > 0) {
3169 Iterator<Service> it = mServices.values().iterator();
3170 while (it.hasNext()) {
3171 callbacks.add(it.next());
3172 }
3173 }
3174 synchronized (mProviderMap) {
3175 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003176 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 while (it.hasNext()) {
3178 callbacks.add(it.next().mLocalProvider);
3179 }
3180 }
3181 }
3182 final int N = mAllApplications.size();
3183 for (int i=0; i<N; i++) {
3184 callbacks.add(mAllApplications.get(i));
3185 }
Bob Leee5408332009-09-04 18:31:17 -07003186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 return callbacks;
3188 }
Bob Leee5408332009-09-04 18:31:17 -07003189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 private final void performConfigurationChanged(
3191 ComponentCallbacks cb, Configuration config) {
3192 // Only for Activity objects, check that they actually call up to their
3193 // superclass implementation. ComponentCallbacks is an interface, so
3194 // we check the runtime type and act accordingly.
3195 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3196 if (activity != null) {
3197 activity.mCalled = false;
3198 }
Bob Leee5408332009-09-04 18:31:17 -07003199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 boolean shouldChangeConfig = false;
3201 if ((activity == null) || (activity.mCurrentConfig == null)) {
3202 shouldChangeConfig = true;
3203 } else {
Bob Leee5408332009-09-04 18:31:17 -07003204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 // If the new config is the same as the config this Activity
3206 // is already running with then don't bother calling
3207 // onConfigurationChanged
3208 int diff = activity.mCurrentConfig.diff(config);
3209 if (diff != 0) {
Bob Leee5408332009-09-04 18:31:17 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 // If this activity doesn't handle any of the config changes
3212 // then don't bother calling onConfigurationChanged as we're
3213 // going to destroy it.
3214 if ((~activity.mActivityInfo.configChanges & diff) == 0) {
3215 shouldChangeConfig = true;
3216 }
3217 }
3218 }
Bob Leee5408332009-09-04 18:31:17 -07003219
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003220 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003221 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 if (shouldChangeConfig) {
3223 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225 if (activity != null) {
3226 if (!activity.mCalled) {
3227 throw new SuperNotCalledException(
3228 "Activity " + activity.getLocalClassName() +
3229 " did not call through to super.onConfigurationChanged()");
3230 }
3231 activity.mConfigChangeFlags = 0;
3232 activity.mCurrentConfig = new Configuration(config);
3233 }
3234 }
3235 }
3236
Dianne Hackbornae078162010-03-18 11:29:37 -07003237 final boolean applyConfigurationToResourcesLocked(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003238 if (mResConfiguration == null) {
3239 mResConfiguration = new Configuration();
3240 }
3241 if (!mResConfiguration.isOtherSeqNewer(config)) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003242 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003243 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003244 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003245 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003246 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003247 DisplayMetrics dm = getDisplayMetricsLocked(true);
Bob Leee5408332009-09-04 18:31:17 -07003248
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003249 // set it for java, this also affects newly created Resources
3250 if (config.locale != null) {
3251 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 }
Bob Leee5408332009-09-04 18:31:17 -07003253
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003254 Resources.updateSystemConfiguration(config, dm);
Bob Leee5408332009-09-04 18:31:17 -07003255
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003256 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003257 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003258
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003259 Iterator<WeakReference<Resources>> it =
3260 mActiveResources.values().iterator();
3261 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3262 // mActiveResources.entrySet().iterator();
3263 while (it.hasNext()) {
3264 WeakReference<Resources> v = it.next();
3265 Resources r = v.get();
3266 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003267 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003268 + r + " config to: " + config);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003269 r.updateConfiguration(config, dm);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003270 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003271 // + " " + r + ": " + r.getConfiguration());
3272 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003273 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003274 it.remove();
3275 }
3276 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003277
3278 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003279 }
3280
3281 final void handleConfigurationChanged(Configuration config) {
3282
3283 ArrayList<ComponentCallbacks> callbacks = null;
3284
3285 synchronized (mPackages) {
3286 if (mPendingConfiguration != null) {
3287 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3288 config = mPendingConfiguration;
3289 }
3290 mPendingConfiguration = null;
3291 }
3292
3293 if (config == null) {
3294 return;
3295 }
3296
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003297 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003298 + config);
3299
3300 applyConfigurationToResourcesLocked(config);
3301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 if (mConfiguration == null) {
3303 mConfiguration = new Configuration();
3304 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003305 if (!mConfiguration.isOtherSeqNewer(config)) {
3306 return;
3307 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 mConfiguration.updateFrom(config);
Bob Leee5408332009-09-04 18:31:17 -07003309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 callbacks = collectComponentCallbacksLocked(false, config);
3311 }
Bob Leee5408332009-09-04 18:31:17 -07003312
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003313 if (callbacks != null) {
3314 final int N = callbacks.size();
3315 for (int i=0; i<N; i++) {
3316 performConfigurationChanged(callbacks.get(i), config);
3317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
3319 }
3320
3321 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003322 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 if (r == null || r.activity == null) {
3324 return;
3325 }
Bob Leee5408332009-09-04 18:31:17 -07003326
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003327 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003328 + r.activityInfo.name);
3329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 performConfigurationChanged(r.activity, mConfiguration);
3331 }
3332
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003333 final void handleProfilerControl(boolean start, ProfilerControlData pcd) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003334 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003335 try {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003336 Debug.startMethodTracing(pcd.path, pcd.fd.getFileDescriptor(),
3337 8 * 1024 * 1024, 0);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003338 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003339 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003340 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003341 } finally {
3342 try {
3343 pcd.fd.close();
3344 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003345 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003346 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003347 }
3348 } else {
3349 Debug.stopMethodTracing();
3350 }
3351 }
Bob Leee5408332009-09-04 18:31:17 -07003352
Andy McFadden824c5102010-07-09 16:26:57 -07003353 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3354 if (managed) {
3355 try {
3356 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3357 } catch (IOException e) {
3358 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3359 + " -- can the process access this path?");
3360 } finally {
3361 try {
3362 dhd.fd.close();
3363 } catch (IOException e) {
3364 Slog.w(TAG, "Failure closing profile fd", e);
3365 }
3366 }
3367 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003368 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003369 }
3370 }
3371
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003372 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3373 boolean hasPkgInfo = false;
3374 if (packages != null) {
3375 for (int i=packages.length-1; i>=0; i--) {
3376 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3377 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003378 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003379 ref = mPackages.get(packages[i]);
3380 if (ref != null && ref.get() != null) {
3381 hasPkgInfo = true;
3382 } else {
3383 ref = mResourcePackages.get(packages[i]);
3384 if (ref != null && ref.get() != null) {
3385 hasPkgInfo = true;
3386 }
3387 }
3388 }
3389 mPackages.remove(packages[i]);
3390 mResourcePackages.remove(packages[i]);
3391 }
3392 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003393 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003394 hasPkgInfo);
3395 }
3396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 final void handleLowMemory() {
3398 ArrayList<ComponentCallbacks> callbacks
3399 = new ArrayList<ComponentCallbacks>();
3400
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003401 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 callbacks = collectComponentCallbacksLocked(true, null);
3403 }
Bob Leee5408332009-09-04 18:31:17 -07003404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 final int N = callbacks.size();
3406 for (int i=0; i<N; i++) {
3407 callbacks.get(i).onLowMemory();
3408 }
3409
Chris Tatece229052009-03-25 16:44:52 -07003410 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3411 if (Process.myUid() != Process.SYSTEM_UID) {
3412 int sqliteReleased = SQLiteDatabase.releaseMemory();
3413 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3414 }
Bob Leee5408332009-09-04 18:31:17 -07003415
Mike Reedcaf0df12009-04-27 14:32:05 -04003416 // Ask graphics to free up as much as possible (font/image caches)
3417 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418
3419 BinderInternal.forceGc("mem");
3420 }
3421
3422 private final void handleBindApplication(AppBindData data) {
3423 mBoundApplication = data;
3424 mConfiguration = new Configuration(data.config);
3425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003427 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 android.ddm.DdmHandleAppName.setAppName(data.processName);
3429
3430 /*
3431 * Before spawning a new process, reset the time zone to be the system time zone.
3432 * This needs to be done because the system time zone could have changed after the
3433 * the spawning of this process. Without doing this this process would have the incorrect
3434 * system time zone.
3435 */
3436 TimeZone.setDefault(null);
3437
3438 /*
3439 * Initialize the default locale in this process for the reasons we set the time zone.
3440 */
3441 Locale.setDefault(data.config.locale);
3442
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003443 /*
3444 * Update the system configuration since its preloaded and might not
3445 * reflect configuration changes. The configuration object passed
3446 * in AppBindData can be safely assumed to be up to date
3447 */
3448 Resources.getSystem().updateConfiguration(mConfiguration, null);
3449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 data.info = getPackageInfoNoCheck(data.appInfo);
3451
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003452 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003453 * For system applications on userdebug/eng builds, log stack
3454 * traces of disk and network access to dropbox for analysis.
3455 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003456 if ((data.appInfo.flags &
3457 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003458 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3459 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003460 }
3461
3462 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003463 * For apps targetting SDK Honeycomb or later, we don't allow
3464 * network usage on the main event loop / UI thread.
3465 *
3466 * Note to those grepping: this is what ultimately throws
3467 * NetworkOnMainThreadException ...
3468 */
3469 if (data.appInfo.targetSdkVersion > 9) {
3470 StrictMode.enableDeathOnNetwork();
3471 }
3472
3473 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003474 * Switch this process to density compatibility mode if needed.
3475 */
3476 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3477 == 0) {
3478 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3479 }
Bob Leee5408332009-09-04 18:31:17 -07003480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003481 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3482 // XXX should have option to change the port.
3483 Debug.changeDebugPort(8100);
3484 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003485 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 + " is waiting for the debugger on port 8100...");
3487
3488 IActivityManager mgr = ActivityManagerNative.getDefault();
3489 try {
3490 mgr.showWaitingForDebugger(mAppThread, true);
3491 } catch (RemoteException ex) {
3492 }
3493
3494 Debug.waitForDebugger();
3495
3496 try {
3497 mgr.showWaitingForDebugger(mAppThread, false);
3498 } catch (RemoteException ex) {
3499 }
3500
3501 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003502 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 + " can be debugged on port 8100...");
3504 }
3505 }
3506
Robert Greenwalt434203a2010-10-11 16:00:27 -07003507 /**
3508 * Initialize the default http proxy in this process for the reasons we set the time zone.
3509 */
3510 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3511 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3512 try {
3513 ProxyProperties proxyProperties = service.getProxy();
3514 Proxy.setHttpProxySystemProperty(proxyProperties);
3515 } catch (RemoteException e) {}
3516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003518 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 appContext.init(data.info, null, this);
3520 InstrumentationInfo ii = null;
3521 try {
3522 ii = appContext.getPackageManager().
3523 getInstrumentationInfo(data.instrumentationName, 0);
3524 } catch (PackageManager.NameNotFoundException e) {
3525 }
3526 if (ii == null) {
3527 throw new RuntimeException(
3528 "Unable to find instrumentation info for: "
3529 + data.instrumentationName);
3530 }
3531
3532 mInstrumentationAppDir = ii.sourceDir;
3533 mInstrumentationAppPackage = ii.packageName;
3534 mInstrumentedAppDir = data.info.getAppDir();
3535
3536 ApplicationInfo instrApp = new ApplicationInfo();
3537 instrApp.packageName = ii.packageName;
3538 instrApp.sourceDir = ii.sourceDir;
3539 instrApp.publicSourceDir = ii.publicSourceDir;
3540 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003541 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003542 LoadedApk pi = getPackageInfo(instrApp,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003544 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 instrContext.init(pi, null, this);
3546
3547 try {
3548 java.lang.ClassLoader cl = instrContext.getClassLoader();
3549 mInstrumentation = (Instrumentation)
3550 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3551 } catch (Exception e) {
3552 throw new RuntimeException(
3553 "Unable to instantiate instrumentation "
3554 + data.instrumentationName + ": " + e.toString(), e);
3555 }
3556
3557 mInstrumentation.init(this, instrContext, appContext,
3558 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3559
3560 if (data.profileFile != null && !ii.handleProfiling) {
3561 data.handlingProfiling = true;
3562 File file = new File(data.profileFile);
3563 file.getParentFile().mkdirs();
3564 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3565 }
3566
3567 try {
3568 mInstrumentation.onCreate(data.instrumentationArgs);
3569 }
3570 catch (Exception e) {
3571 throw new RuntimeException(
3572 "Exception thrown in onCreate() of "
3573 + data.instrumentationName + ": " + e.toString(), e);
3574 }
3575
3576 } else {
3577 mInstrumentation = new Instrumentation();
3578 }
3579
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003580 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003581 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003582 }
3583
Christopher Tate181fafa2009-05-14 11:12:14 -07003584 // If the app is being launched for full backup or restore, bring it up in
3585 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003586 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 mInitialApplication = app;
3588
3589 List<ProviderInfo> providers = data.providers;
3590 if (providers != null) {
3591 installContentProviders(app, providers);
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003592 // For process that contains content providers, we want to
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003593 // ensure that the JIT is enabled "at some point".
3594 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 }
3596
3597 try {
3598 mInstrumentation.callApplicationOnCreate(app);
3599 } catch (Exception e) {
3600 if (!mInstrumentation.onException(app, e)) {
3601 throw new RuntimeException(
3602 "Unable to create application " + app.getClass().getName()
3603 + ": " + e.toString(), e);
3604 }
3605 }
3606 }
3607
3608 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3609 IActivityManager am = ActivityManagerNative.getDefault();
3610 if (mBoundApplication.profileFile != null && mBoundApplication.handlingProfiling) {
3611 Debug.stopMethodTracing();
3612 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003613 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 // + ", app thr: " + mAppThread);
3615 try {
3616 am.finishInstrumentation(mAppThread, resultCode, results);
3617 } catch (RemoteException ex) {
3618 }
3619 }
3620
3621 private final void installContentProviders(
3622 Context context, List<ProviderInfo> providers) {
3623 final ArrayList<IActivityManager.ContentProviderHolder> results =
3624 new ArrayList<IActivityManager.ContentProviderHolder>();
3625
3626 Iterator<ProviderInfo> i = providers.iterator();
3627 while (i.hasNext()) {
3628 ProviderInfo cpi = i.next();
3629 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003630 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 buf.append(cpi.authority);
3632 buf.append(": ");
3633 buf.append(cpi.name);
3634 Log.i(TAG, buf.toString());
3635 IContentProvider cp = installProvider(context, null, cpi, false);
3636 if (cp != null) {
3637 IActivityManager.ContentProviderHolder cph =
3638 new IActivityManager.ContentProviderHolder(cpi);
3639 cph.provider = cp;
3640 results.add(cph);
3641 // Don't ever unload this provider from the process.
3642 synchronized(mProviderMap) {
3643 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3644 }
3645 }
3646 }
3647
3648 try {
3649 ActivityManagerNative.getDefault().publishContentProviders(
3650 getApplicationThread(), results);
3651 } catch (RemoteException ex) {
3652 }
3653 }
3654
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003655 private final IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003657 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 if (pr != null) {
3659 return pr.mProvider;
3660 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003661 return null;
3662 }
3663 }
3664
3665 private final IContentProvider getProvider(Context context, String name) {
3666 IContentProvider existing = getExistingProvider(context, name);
3667 if (existing != null) {
3668 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670
3671 IActivityManager.ContentProviderHolder holder = null;
3672 try {
3673 holder = ActivityManagerNative.getDefault().getContentProvider(
3674 getApplicationThread(), name);
3675 } catch (RemoteException ex) {
3676 }
3677 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003678 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 return null;
3680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003681
3682 IContentProvider prov = installProvider(context, holder.provider,
3683 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003684 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 if (holder.noReleaseNeeded || holder.provider == null) {
3686 // We are not going to release the provider if it is an external
3687 // provider that doesn't care about being released, or if it is
3688 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003689 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 synchronized(mProviderMap) {
3691 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
3692 }
3693 }
3694 return prov;
3695 }
3696
3697 public final IContentProvider acquireProvider(Context c, String name) {
3698 IContentProvider provider = getProvider(c, name);
3699 if(provider == null)
3700 return null;
3701 IBinder jBinder = provider.asBinder();
3702 synchronized(mProviderMap) {
3703 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3704 if(prc == null) {
3705 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3706 } else {
3707 prc.count++;
3708 } //end else
3709 } //end synchronized
3710 return provider;
3711 }
3712
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003713 public final IContentProvider acquireExistingProvider(Context c, String name) {
3714 IContentProvider provider = getExistingProvider(c, name);
3715 if(provider == null)
3716 return null;
3717 IBinder jBinder = provider.asBinder();
3718 synchronized(mProviderMap) {
3719 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3720 if(prc == null) {
3721 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
3722 } else {
3723 prc.count++;
3724 } //end else
3725 } //end synchronized
3726 return provider;
3727 }
3728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 public final boolean releaseProvider(IContentProvider provider) {
3730 if(provider == null) {
3731 return false;
3732 }
3733 IBinder jBinder = provider.asBinder();
3734 synchronized(mProviderMap) {
3735 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3736 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003737 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 return false;
3739 } else {
3740 prc.count--;
3741 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003742 // Schedule the actual remove asynchronously, since we
3743 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003744 // TODO: it would be nice to post a delayed message, so
3745 // if we come back and need the same provider quickly
3746 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003747 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
3748 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749 } //end if
3750 } //end else
3751 } //end synchronized
3752 return true;
3753 }
3754
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003755 final void completeRemoveProvider(IContentProvider provider) {
3756 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003757 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003758 synchronized(mProviderMap) {
3759 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
3760 if(prc != null && prc.count == 0) {
3761 mProviderRefCountMap.remove(jBinder);
3762 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003763 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003764 }
3765 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003766
3767 if (name != null) {
3768 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003769 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003770 "ActivityManagerNative.removeContentProvider(" + name);
3771 ActivityManagerNative.getDefault().removeContentProvider(
3772 getApplicationThread(), name);
3773 } catch (RemoteException e) {
3774 //do nothing content provider object is dead any way
3775 } //end catch
3776 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07003777 }
3778
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003779 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003781 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 }
3783 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003785 String name = null;
3786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003788 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003790 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003791 IBinder myBinder = pr.mProvider.asBinder();
3792 if (myBinder == providerBinder) {
3793 //find if its published by this process itself
3794 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003795 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003796 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003798 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 "death recipient");
3800 //content provider is in another process
3801 myBinder.unlinkToDeath(pr, 0);
3802 iter.remove();
3803 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003804 if(name == null) {
3805 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807 } //end if myBinder
3808 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07003809
3810 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 }
3812
3813 final void removeDeadProvider(String name, IContentProvider provider) {
3814 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003815 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003817 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003818 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003819 if (removed != null) {
3820 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
3823 }
3824 }
3825
3826 final void removeDeadProviderLocked(String name, IContentProvider provider) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003827 ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 if (pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003829 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003830 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07003831 if (removed != null) {
3832 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
3833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 }
3835 }
3836
3837 private final IContentProvider installProvider(Context context,
3838 IContentProvider provider, ProviderInfo info, boolean noisy) {
3839 ContentProvider localProvider = null;
3840 if (provider == null) {
3841 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003842 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 + info.name);
3844 }
3845 Context c = null;
3846 ApplicationInfo ai = info.applicationInfo;
3847 if (context.getPackageName().equals(ai.packageName)) {
3848 c = context;
3849 } else if (mInitialApplication != null &&
3850 mInitialApplication.getPackageName().equals(ai.packageName)) {
3851 c = mInitialApplication;
3852 } else {
3853 try {
3854 c = context.createPackageContext(ai.packageName,
3855 Context.CONTEXT_INCLUDE_CODE);
3856 } catch (PackageManager.NameNotFoundException e) {
3857 }
3858 }
3859 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003860 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 ai.packageName +
3862 " while loading content provider " +
3863 info.name);
3864 return null;
3865 }
3866 try {
3867 final java.lang.ClassLoader cl = c.getClassLoader();
3868 localProvider = (ContentProvider)cl.
3869 loadClass(info.name).newInstance();
3870 provider = localProvider.getIContentProvider();
3871 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003872 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 info.name + " from sourceDir " +
3874 info.applicationInfo.sourceDir);
3875 return null;
3876 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003877 if (Config.LOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 TAG, "Instantiating local provider " + info.name);
3879 // XXX Need to create the correct context for this provider.
3880 localProvider.attachInfo(c, info);
3881 } catch (java.lang.Exception e) {
3882 if (!mInstrumentation.onException(null, e)) {
3883 throw new RuntimeException(
3884 "Unable to get provider " + info.name
3885 + ": " + e.toString(), e);
3886 }
3887 return null;
3888 }
3889 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003890 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 + info.name);
3892 }
3893
3894 synchronized (mProviderMap) {
3895 // Cache the pointer for the remote provider.
3896 String names[] = PATTERN_SEMICOLON.split(info.authority);
3897 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003898 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 localProvider);
3900 try {
3901 provider.asBinder().linkToDeath(pr, 0);
3902 mProviderMap.put(names[i], pr);
3903 } catch (RemoteException e) {
3904 return null;
3905 }
3906 }
3907 if (localProvider != null) {
3908 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003909 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 }
3911 }
3912
3913 return provider;
3914 }
3915
3916 private final void attach(boolean system) {
3917 sThreadLocal.set(this);
3918 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 if (!system) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003920 ViewRoot.addFirstDrawHandler(new Runnable() {
3921 public void run() {
3922 ensureJitEnabled();
3923 }
3924 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
3926 RuntimeInit.setApplicationObject(mAppThread.asBinder());
3927 IActivityManager mgr = ActivityManagerNative.getDefault();
3928 try {
3929 mgr.attachApplication(mAppThread);
3930 } catch (RemoteException ex) {
3931 }
3932 } else {
3933 // Don't set application object here -- if the system crashes,
3934 // we can't display an alert, we just want to die die die.
3935 android.ddm.DdmHandleAppName.setAppName("system_process");
3936 try {
3937 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08003938 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 context.init(getSystemContext().mPackageInfo, null, this);
3940 Application app = Instrumentation.newApplication(Application.class, context);
3941 mAllApplications.add(app);
3942 mInitialApplication = app;
3943 app.onCreate();
3944 } catch (Exception e) {
3945 throw new RuntimeException(
3946 "Unable to instantiate Application():" + e.toString(), e);
3947 }
3948 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003949
3950 ViewRoot.addConfigCallback(new ComponentCallbacks() {
3951 public void onConfigurationChanged(Configuration newConfig) {
3952 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07003953 // We need to apply this change to the resources
3954 // immediately, because upon returning the view
3955 // hierarchy will be informed about it.
3956 if (applyConfigurationToResourcesLocked(newConfig)) {
3957 // This actually changed the resources! Tell
3958 // everyone about it.
3959 if (mPendingConfiguration == null ||
3960 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
3961 mPendingConfiguration = newConfig;
3962
3963 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
3964 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003965 }
3966 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003967 }
3968 public void onLowMemory() {
3969 }
3970 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 }
3972
3973 private final void detach()
3974 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 sThreadLocal.set(null);
3976 }
3977
3978 public static final ActivityThread systemMain() {
Romain Guy52339202010-09-03 16:04:46 -07003979 HardwareRenderer.disable();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 ActivityThread thread = new ActivityThread();
3981 thread.attach(true);
3982 return thread;
3983 }
3984
3985 public final void installSystemProviders(List providers) {
3986 if (providers != null) {
3987 installContentProviders(mInitialApplication,
3988 (List<ProviderInfo>)providers);
3989 }
3990 }
3991
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003992 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003993 synchronized (mPackages) {
3994 if (mCoreSettings != null) {
3995 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003996 } else {
3997 return defaultValue;
3998 }
3999 }
4000 }
4001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 public static final void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004003 SamplingProfilerIntegration.start();
4004
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004005 // CloseGuard defaults to true and can be quite spammy. We
4006 // disable it here, but selectively enable it later (via
4007 // StrictMode) on debug builds, but using DropBox, not logs.
4008 CloseGuard.setEnabled(false);
4009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 Process.setArgV0("<pre-initialized>");
4011
4012 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004013 if (sMainThreadHandler == null) {
4014 sMainThreadHandler = new Handler();
4015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004016
4017 ActivityThread thread = new ActivityThread();
4018 thread.attach(false);
4019
Dianne Hackborn287952c2010-09-22 22:34:31 -07004020 if (false) {
4021 Looper.myLooper().setMessageLogging(new
4022 LogPrinter(Log.DEBUG, "ActivityThread"));
4023 }
4024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004025 Looper.loop();
4026
4027 if (Process.supportsProcesses()) {
4028 throw new RuntimeException("Main thread loop unexpectedly exited");
4029 }
4030
4031 thread.detach();
Bob Leeeec2f412009-09-10 11:01:24 +02004032 String name = (thread.mInitialApplication != null)
4033 ? thread.mInitialApplication.getPackageName()
4034 : "<unknown>";
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004035 Slog.i(TAG, "Main thread of " + name + " is now exiting");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004036 }
4037}