blob: 99aae37ff73d369549e921dc33f4154043022458 [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;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import 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;
Joe Onoratod630f102011-03-17 18:42:26 -070048import android.os.AsyncTask;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Bundle;
50import android.os.Debug;
51import android.os.Handler;
52import android.os.IBinder;
53import android.os.Looper;
54import android.os.Message;
55import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070056import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Process;
58import android.os.RemoteException;
59import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070060import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.SystemClock;
62import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import 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 Hackborn6dd005b2011-07-18 13:22:50 -070073import android.view.ViewRootImpl;
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;
Joe Onorato43a17652011-04-06 19:22:23 -0700126 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700127 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700128 /** @hide */
129 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700130 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700131 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700132 private static final boolean DEBUG_CONFIGURATION = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
134 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
135 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
136 private static final int LOG_ON_PAUSE_CALLED = 30021;
137 private static final int LOG_ON_RESUME_CALLED = 30022;
138
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700139 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700140
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700141 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700143 final ApplicationThread mAppThread = new ApplicationThread();
144 final Looper mLooper = Looper.myLooper();
145 final H mH = new H();
146 final HashMap<IBinder, ActivityClientRecord> mActivities
147 = new HashMap<IBinder, ActivityClientRecord>();
148 // List of new activities (via ActivityRecord.nextIdle) that should
149 // be reported when next we idle.
150 ActivityClientRecord mNewActivities = null;
151 // Number of activities that are currently visible on-screen.
152 int mNumVisibleActivities = 0;
153 final HashMap<IBinder, Service> mServices
154 = new HashMap<IBinder, Service>();
155 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700156 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700157 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700158 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700160 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700161 Application mInitialApplication;
162 final ArrayList<Application> mAllApplications
163 = new ArrayList<Application>();
164 // set of instantiated backup agents, keyed by package name
165 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700166 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Instrumentation mInstrumentation;
168 String mInstrumentationAppDir = null;
169 String mInstrumentationAppPackage = null;
170 String mInstrumentedAppDir = null;
171 boolean mSystemThread = false;
172 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 // These can be accessed by multiple threads; mPackages is the lock.
175 // XXX For now we keep around information about all packages we have
176 // seen, not removing entries from this map.
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700177 // NOTE: The activity manager in its process needs to call in to
178 // ActivityThread to do things like update resource configurations,
179 // which means this lock gets held while the activity manager holds its
180 // own lock. Thus you MUST NEVER call back into the activity manager
181 // or anything that depends on it while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 final HashMap<String, WeakReference<LoadedApk>> mPackages
183 = new HashMap<String, WeakReference<LoadedApk>>();
184 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
185 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700186 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
187 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
189 = new HashMap<ResourcesKey, WeakReference<Resources> >();
190 final ArrayList<ActivityClientRecord> mRelaunchingActivities
191 = new ArrayList<ActivityClientRecord>();
192 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 // The lock of mProviderMap protects the following variables.
195 final HashMap<String, ProviderClientRecord> mProviderMap
196 = new HashMap<String, ProviderClientRecord>();
197 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
198 = new HashMap<IBinder, ProviderRefCount>();
199 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
200 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201
Jeff Hamilton52d32032011-01-08 15:31:26 -0600202 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
203 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 final GcIdler mGcIdler = new GcIdler();
206 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700208 static Handler sMainThreadHandler; // set once in main()
209
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800210 Bundle mCoreSettings = null;
211
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400212 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700214 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 Intent intent;
216 Bundle state;
217 Activity activity;
218 Window window;
219 Activity parent;
220 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700221 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 boolean paused;
223 boolean stopped;
224 boolean hideForNow;
225 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700226 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700229 String profileFile;
230 ParcelFileDescriptor profileFd;
231 boolean autoStopProfiler;
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400234 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700235 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
237 List<ResultInfo> pendingResults;
238 List<Intent> pendingIntents;
239
240 boolean startsNotResumed;
241 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800242 int pendingConfigChanges;
243 boolean onlyLocalRequest;
244
245 View mPendingRemoveWindow;
246 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 parent = null;
250 embeddedID = null;
251 paused = false;
252 stopped = false;
253 hideForNow = false;
254 nextIdle = null;
255 }
256
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800257 public boolean isPreHoneycomb() {
258 if (activity != null) {
259 return activity.getApplicationInfo().targetSdkVersion
260 < android.os.Build.VERSION_CODES.HONEYCOMB;
261 }
262 return false;
263 }
264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 public String toString() {
266 ComponentName componentName = intent.getComponent();
267 return "ActivityRecord{"
268 + Integer.toHexString(System.identityHashCode(this))
269 + " token=" + token + " " + (componentName == null
270 ? "no component name" : componentName.toShortString())
271 + "}";
272 }
273 }
274
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400275 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 final String mName;
277 final IContentProvider mProvider;
278 final ContentProvider mLocalProvider;
279
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700280 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 ContentProvider localProvider) {
282 mName = name;
283 mProvider = provider;
284 mLocalProvider = localProvider;
285 }
286
287 public void binderDied() {
288 removeDeadProvider(mName, mProvider);
289 }
290 }
291
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400292 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 List<Intent> intents;
294 IBinder token;
295 public String toString() {
296 return "NewIntentData{intents=" + intents + " token=" + token + "}";
297 }
298 }
299
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400300 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700301 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
302 boolean ordered, boolean sticky, IBinder token) {
303 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
304 this.intent = intent;
305 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 Intent intent;
308 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400309 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 public String toString() {
311 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700312 info.packageName + " resultCode=" + getResultCode()
313 + " resultData=" + getResultData() + " resultExtras="
314 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 }
316 }
317
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400318 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700319 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400320 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700321 int backupMode;
322 public String toString() {
323 return "CreateBackupAgentData{appInfo=" + appInfo
324 + " backupAgent=" + appInfo.backupAgentName
325 + " mode=" + backupMode + "}";
326 }
327 }
Bob Leee5408332009-09-04 18:31:17 -0700328
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400329 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 IBinder token;
331 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 Intent intent;
334 public String toString() {
335 return "CreateServiceData{token=" + token + " className="
336 + info.name + " packageName=" + info.packageName
337 + " intent=" + intent + "}";
338 }
339 }
340
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 IBinder token;
343 Intent intent;
344 boolean rebind;
345 public String toString() {
346 return "BindServiceData{token=" + token + " intent=" + intent + "}";
347 }
348 }
349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700352 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700354 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Intent args;
356 public String toString() {
357 return "ServiceArgsData{token=" + token + " startId=" + startId
358 + " args=" + args + "}";
359 }
360 }
361
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700363 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 String processName;
365 ApplicationInfo appInfo;
366 List<ProviderInfo> providers;
367 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 Bundle instrumentationArgs;
369 IInstrumentationWatcher instrumentationWatcher;
370 int debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700371 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700372 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400374 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700375
376 /** Initial values for {@link Profiler}. */
377 String initProfileFile;
378 ParcelFileDescriptor initProfileFd;
379 boolean initAutoStopProfiler;
380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 public String toString() {
382 return "AppBindData{appInfo=" + appInfo + "}";
383 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700384 }
385
386 static final class Profiler {
387 String profileFile;
388 ParcelFileDescriptor profileFd;
389 boolean autoStopProfiler;
390 boolean profiling;
391 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700392 public void setProfiler(String file, ParcelFileDescriptor fd) {
393 if (profiling) {
394 if (fd != null) {
395 try {
396 fd.close();
397 } catch (IOException e) {
398 }
399 }
400 return;
401 }
402 if (profileFd != null) {
403 try {
404 profileFd.close();
405 } catch (IOException e) {
406 }
407 }
408 profileFile = file;
409 profileFd = fd;
410 }
411 public void startProfiling() {
412 if (profileFd == null || profiling) {
413 return;
414 }
415 try {
416 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
417 8 * 1024 * 1024, 0);
418 profiling = true;
419 } catch (RuntimeException e) {
420 Slog.w(TAG, "Profiling failed on path " + profileFile);
421 try {
422 profileFd.close();
423 profileFd = null;
424 } catch (IOException e2) {
425 Slog.w(TAG, "Failure closing profile fd", e2);
426 }
427 }
428 }
429 public void stopProfiling() {
430 if (profiling) {
431 profiling = false;
432 Debug.stopMethodTracing();
433 if (profileFd != null) {
434 try {
435 profileFd.close();
436 } catch (IOException e) {
437 }
438 }
439 profileFd = null;
440 profileFile = null;
441 }
442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400445 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700446 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700447 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800448 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400452 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 IBinder token;
454 List<ResultInfo> results;
455 public String toString() {
456 return "ResultData{token=" + token + " results" + results + "}";
457 }
458 }
459
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400460 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800461 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 String what;
463 String who;
464 }
465
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700467 String path;
468 ParcelFileDescriptor fd;
469 }
470
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700472 String path;
473 ParcelFileDescriptor fd;
474 }
475
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400476 static final class UpdateCompatibilityData {
477 String pkg;
478 CompatibilityInfo info;
479 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700480
Romain Guy65b345f2011-07-27 18:51:50 -0700481 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700482
Romain Guy65b345f2011-07-27 18:51:50 -0700483 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700484 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
485 private static final String ONE_COUNT_COLUMN = "%21s %8d";
486 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
487 private static final String TWO_COUNT_COLUMNS_DB = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700488 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 // Formatting for checkin service - update version if row format changes
491 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 public final void schedulePauseActivity(IBinder token, boolean finished,
494 boolean userLeaving, int configChanges) {
495 queueOrSendMessage(
496 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
497 token,
498 (userLeaving ? 1 : 0),
499 configChanges);
500 }
501
502 public final void scheduleStopActivity(IBinder token, boolean showWindow,
503 int configChanges) {
504 queueOrSendMessage(
505 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
506 token, 0, configChanges);
507 }
508
509 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
510 queueOrSendMessage(
511 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
512 token);
513 }
514
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800515 public final void scheduleSleeping(IBinder token, boolean sleeping) {
516 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
517 }
518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
520 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
521 }
522
523 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
524 ResultData res = new ResultData();
525 res.token = token;
526 res.results = results;
527 queueOrSendMessage(H.SEND_RESULT, res);
528 }
529
530 // we use token to identify this activity without having to send the
531 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700532 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400533 ActivityInfo info, CompatibilityInfo compatInfo, Bundle state,
534 List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700535 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
536 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700537 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
539 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700540 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541 r.intent = intent;
542 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400543 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 r.state = state;
545
546 r.pendingResults = pendingResults;
547 r.pendingIntents = pendingNewIntents;
548
549 r.startsNotResumed = notResumed;
550 r.isForward = isForward;
551
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700552 r.profileFile = profileName;
553 r.profileFd = profileFd;
554 r.autoStopProfiler = autoStopProfiler;
555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
557 }
558
559 public final void scheduleRelaunchActivity(IBinder token,
560 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800561 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800562 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
563 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
566 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
567 NewIntentData data = new NewIntentData();
568 data.intents = intents;
569 data.token = token;
570
571 queueOrSendMessage(H.NEW_INTENT, data);
572 }
573
574 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
575 int configChanges) {
576 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
577 configChanges);
578 }
579
580 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400581 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
582 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700583 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
584 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400586 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 queueOrSendMessage(H.RECEIVER, r);
588 }
589
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400590 public final void scheduleCreateBackupAgent(ApplicationInfo app,
591 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700592 CreateBackupAgentData d = new CreateBackupAgentData();
593 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400594 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700595 d.backupMode = backupMode;
596
597 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
598 }
599
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400600 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
601 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700602 CreateBackupAgentData d = new CreateBackupAgentData();
603 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400604 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700605
606 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
607 }
608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400610 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 CreateServiceData s = new CreateServiceData();
612 s.token = token;
613 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400614 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615
616 queueOrSendMessage(H.CREATE_SERVICE, s);
617 }
618
619 public final void scheduleBindService(IBinder token, Intent intent,
620 boolean rebind) {
621 BindServiceData s = new BindServiceData();
622 s.token = token;
623 s.intent = intent;
624 s.rebind = rebind;
625
626 queueOrSendMessage(H.BIND_SERVICE, s);
627 }
628
629 public final void scheduleUnbindService(IBinder token, Intent intent) {
630 BindServiceData s = new BindServiceData();
631 s.token = token;
632 s.intent = intent;
633
634 queueOrSendMessage(H.UNBIND_SERVICE, s);
635 }
636
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700637 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700638 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 ServiceArgsData s = new ServiceArgsData();
640 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700641 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700643 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 s.args = args;
645
646 queueOrSendMessage(H.SERVICE_ARGS, s);
647 }
648
649 public final void scheduleStopService(IBinder token) {
650 queueOrSendMessage(H.STOP_SERVICE, token);
651 }
652
653 public final void bindApplication(String processName,
654 ApplicationInfo appInfo, List<ProviderInfo> providers,
655 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700656 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700658 int debugMode, boolean isRestrictedBackupMode, boolean persistent,
659 Configuration config, CompatibilityInfo compatInfo,
660 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
662 if (services != null) {
663 // Setup the service cache in the ServiceManager
664 ServiceManager.initServiceCache(services);
665 }
666
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800667 setCoreSettings(coreSettings);
668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 AppBindData data = new AppBindData();
670 data.processName = processName;
671 data.appInfo = appInfo;
672 data.providers = providers;
673 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 data.instrumentationArgs = instrumentationArgs;
675 data.instrumentationWatcher = instrumentationWatcher;
676 data.debugMode = debugMode;
Christopher Tate181fafa2009-05-14 11:12:14 -0700677 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700678 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400680 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700681 data.initProfileFile = profileFile;
682 data.initProfileFd = profileFd;
683 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 queueOrSendMessage(H.BIND_APPLICATION, data);
685 }
686
687 public final void scheduleExit() {
688 queueOrSendMessage(H.EXIT_APPLICATION, null);
689 }
690
Christopher Tate5e1ab332009-09-01 20:32:49 -0700691 public final void scheduleSuicide() {
692 queueOrSendMessage(H.SUICIDE, null);
693 }
694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 public void requestThumbnail(IBinder token) {
696 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
697 }
698
699 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800700 synchronized (mPackages) {
701 if (mPendingConfiguration == null ||
702 mPendingConfiguration.isOtherSeqNewer(config)) {
703 mPendingConfiguration = config;
704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
707 }
708
709 public void updateTimeZone() {
710 TimeZone.setDefault(null);
711 }
712
Robert Greenwalt03595d02010-11-02 14:08:23 -0700713 public void clearDnsCache() {
714 // a non-standard API to get this to libcore
715 InetAddress.clearDnsCache();
716 }
717
Robert Greenwalt434203a2010-10-11 16:00:27 -0700718 public void setHttpProxy(String host, String port, String exclList) {
719 Proxy.setHttpProxySystemProperty(host, port, exclList);
720 }
721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 public void processInBackground() {
723 mH.removeMessages(H.GC_WHEN_IDLE);
724 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
725 }
726
727 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700728 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700729 try {
730 data.fd = ParcelFileDescriptor.dup(fd);
731 data.token = servicetoken;
732 data.args = args;
733 queueOrSendMessage(H.DUMP_SERVICE, data);
734 } catch (IOException e) {
735 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 }
737 }
738
739 // This function exists to make sure all receiver dispatching is
740 // correctly ordered, since these are one-way calls and the binder driver
741 // applies transaction ordering per object for such calls.
742 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700743 int resultCode, String dataStr, Bundle extras, boolean ordered,
744 boolean sticky) throws RemoteException {
745 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
Bob Leee5408332009-09-04 18:31:17 -0700747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 public void scheduleLowMemory() {
749 queueOrSendMessage(H.LOW_MEMORY, null);
750 }
751
752 public void scheduleActivityConfigurationChanged(IBinder token) {
753 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
754 }
755
Romain Guy7eabe552011-07-21 14:56:34 -0700756 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
757 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700758 ProfilerControlData pcd = new ProfilerControlData();
759 pcd.path = path;
760 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700761 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800762 }
763
Andy McFadden824c5102010-07-09 16:26:57 -0700764 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
765 DumpHeapData dhd = new DumpHeapData();
766 dhd.path = path;
767 dhd.fd = fd;
768 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
769 }
770
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700771 public void setSchedulingGroup(int group) {
772 // Note: do this immediately, since going into the foreground
773 // should happen regardless of what pending work we have to do
774 // and the activity manager will wait for us to report back that
775 // we are done before sending us to the background.
776 try {
777 Process.setProcessGroup(Process.myPid(), group);
778 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800779 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700780 }
781 }
Bob Leee5408332009-09-04 18:31:17 -0700782
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700783 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
784 Debug.getMemoryInfo(outInfo);
785 }
Bob Leee5408332009-09-04 18:31:17 -0700786
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700787 public void dispatchPackageBroadcast(int cmd, String[] packages) {
788 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
789 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700790
791 public void scheduleCrash(String msg) {
792 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
793 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700794
Dianne Hackborn30d71892010-12-11 10:37:55 -0800795 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
796 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700797 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700798 try {
799 data.fd = ParcelFileDescriptor.dup(fd);
800 data.token = activitytoken;
801 data.prefix = prefix;
802 data.args = args;
803 queueOrSendMessage(H.DUMP_ACTIVITY, data);
804 } catch (IOException e) {
805 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700806 }
807 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700810 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
811 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700812 FileOutputStream fout = new FileOutputStream(fd);
813 PrintWriter pw = new PrintWriter(fout);
814 try {
Dianne Hackbornb437e092011-08-05 17:50:29 -0700815 return dumpMemInfo(pw, checkin, all, args);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700816 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700817 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700818 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700819 }
820
Dianne Hackbornb437e092011-08-05 17:50:29 -0700821 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all,
822 String[] args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 long nativeMax = Debug.getNativeHeapSize() / 1024;
824 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
825 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
826
827 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
828 Debug.getMemoryInfo(memInfo);
829
Dianne Hackbornb437e092011-08-05 17:50:29 -0700830 if (!all) {
831 return memInfo;
832 }
833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 Runtime runtime = Runtime.getRuntime();
835
836 long dalvikMax = runtime.totalMemory() / 1024;
837 long dalvikFree = runtime.freeMemory() / 1024;
838 long dalvikAllocated = dalvikMax - dalvikFree;
839 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700840 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700841 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
842 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 int globalAssetCount = AssetManager.getGlobalAssetCount();
844 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
845 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
846 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
847 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700848 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 long sqliteAllocated = SQLiteDebug.getHeapAllocatedSize() / 1024;
Vasu Noric3849202010-03-09 10:47:25 -0800850 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700853 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 // NOTE: if you change anything significant below, also consider changing
855 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700856 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 // Header
860 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
861 pw.print(Process.myPid()); pw.print(',');
862 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 // Heap info - max
865 pw.print(nativeMax); pw.print(',');
866 pw.print(dalvikMax); pw.print(',');
867 pw.print("N/A,");
868 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 // Heap info - allocated
871 pw.print(nativeAllocated); pw.print(',');
872 pw.print(dalvikAllocated); pw.print(',');
873 pw.print("N/A,");
874 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 // Heap info - free
877 pw.print(nativeFree); pw.print(',');
878 pw.print(dalvikFree); pw.print(',');
879 pw.print("N/A,");
880 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 // Heap info - proportional set size
883 pw.print(memInfo.nativePss); pw.print(',');
884 pw.print(memInfo.dalvikPss); pw.print(',');
885 pw.print(memInfo.otherPss); pw.print(',');
886 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700889 pw.print(memInfo.nativeSharedDirty); pw.print(',');
890 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
891 pw.print(memInfo.otherSharedDirty); pw.print(',');
892 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
893 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700896 pw.print(memInfo.nativePrivateDirty); pw.print(',');
897 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
898 pw.print(memInfo.otherPrivateDirty); pw.print(',');
899 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
900 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 // Object counts
903 pw.print(viewInstanceCount); pw.print(',');
904 pw.print(viewRootInstanceCount); pw.print(',');
905 pw.print(appContextInstanceCount); pw.print(',');
906 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 pw.print(globalAssetCount); pw.print(',');
909 pw.print(globalAssetManagerCount); pw.print(',');
910 pw.print(binderLocalObjectCount); pw.print(',');
911 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 pw.print(binderDeathObjectCount); pw.print(',');
914 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 // SQL
917 pw.print(sqliteAllocated); pw.print(',');
Vasu Noric3849202010-03-09 10:47:25 -0800918 pw.print(stats.memoryUsed / 1024); pw.print(',');
919 pw.print(stats.pageCacheOverflo / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700920 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800921 for (int i = 0; i < stats.dbStats.size(); i++) {
922 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700923 pw.print(','); pw.print(dbStats.dbName);
924 pw.print(','); pw.print(dbStats.pageSize);
925 pw.print(','); pw.print(dbStats.dbSize);
926 pw.print(','); pw.print(dbStats.lookaside);
927 pw.print(','); pw.print(dbStats.cache);
928 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800929 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700930 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700931
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700932 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
Bob Leee5408332009-09-04 18:31:17 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
937 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
938 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
939 "------");
940 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
941 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
942 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
943 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700945 int otherPss = memInfo.otherPss;
946 int otherSharedDirty = memInfo.otherSharedDirty;
947 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700949 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700950 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700951 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
952 memInfo.getOtherPrivateDirty(i), "", "", "");
953 otherPss -= memInfo.getOtherPss(i);
954 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
955 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
956 }
957
958 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
959 otherPrivateDirty, "", "", "");
960 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
961 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
962 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
963 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
965 pw.println(" ");
966 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700967 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 viewRootInstanceCount);
969
970 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
971 "Activities:", activityInstanceCount);
972
973 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
974 "AssetManagers:", globalAssetManagerCount);
975
976 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
977 "Proxy Binders:", binderProxyObjectCount);
978 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
979
980 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 // SQLite mem info
983 pw.println(" ");
984 pw.println(" SQL");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700985 printRow(pw, TWO_COUNT_COLUMNS_DB, "heap:", sqliteAllocated, "MEMORY_USED:",
Vasu Noric3849202010-03-09 10:47:25 -0800986 stats.memoryUsed / 1024);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700987 printRow(pw, TWO_COUNT_COLUMNS_DB, "PAGECACHE_OVERFLOW:",
988 stats.pageCacheOverflo / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800989 pw.println(" ");
990 int N = stats.dbStats.size();
991 if (N > 0) {
992 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -0700993 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
994 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -0800995 for (int i = 0; i < N; i++) {
996 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -0700997 printRow(pw, DB_INFO_FORMAT,
998 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
999 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1000 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1001 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001002 }
1003 }
Bob Leee5408332009-09-04 18:31:17 -07001004
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001005 // Asset details.
1006 String assetAlloc = AssetManager.getAssetAllocations();
1007 if (assetAlloc != null) {
1008 pw.println(" ");
1009 pw.println(" Asset Allocations");
1010 pw.print(assetAlloc);
1011 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001012
1013 return memInfo;
1014 }
1015
1016 @Override
1017 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1018 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001019 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 }
1021
1022 private void printRow(PrintWriter pw, String format, Object...objs) {
1023 pw.println(String.format(format, objs));
1024 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001025
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001026 public void setCoreSettings(Bundle coreSettings) {
1027 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001028 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001029
1030 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1031 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1032 ucd.pkg = pkg;
1033 ucd.info = info;
1034 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1035 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001036
1037 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001038 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 }
1041
Romain Guy65b345f2011-07-27 18:51:50 -07001042 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 public static final int LAUNCH_ACTIVITY = 100;
1044 public static final int PAUSE_ACTIVITY = 101;
1045 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1046 public static final int STOP_ACTIVITY_SHOW = 103;
1047 public static final int STOP_ACTIVITY_HIDE = 104;
1048 public static final int SHOW_WINDOW = 105;
1049 public static final int HIDE_WINDOW = 106;
1050 public static final int RESUME_ACTIVITY = 107;
1051 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001052 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 public static final int BIND_APPLICATION = 110;
1054 public static final int EXIT_APPLICATION = 111;
1055 public static final int NEW_INTENT = 112;
1056 public static final int RECEIVER = 113;
1057 public static final int CREATE_SERVICE = 114;
1058 public static final int SERVICE_ARGS = 115;
1059 public static final int STOP_SERVICE = 116;
1060 public static final int REQUEST_THUMBNAIL = 117;
1061 public static final int CONFIGURATION_CHANGED = 118;
1062 public static final int CLEAN_UP_CONTEXT = 119;
1063 public static final int GC_WHEN_IDLE = 120;
1064 public static final int BIND_SERVICE = 121;
1065 public static final int UNBIND_SERVICE = 122;
1066 public static final int DUMP_SERVICE = 123;
1067 public static final int LOW_MEMORY = 124;
1068 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1069 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001070 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001071 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001072 public static final int DESTROY_BACKUP_AGENT = 129;
1073 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001074 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001075 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001076 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001077 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001078 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001079 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001080 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001081 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001082 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001083 public static final int TRIM_MEMORY = 140;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001085 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 switch (code) {
1087 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1088 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1089 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1090 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1091 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1092 case SHOW_WINDOW: return "SHOW_WINDOW";
1093 case HIDE_WINDOW: return "HIDE_WINDOW";
1094 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1095 case SEND_RESULT: return "SEND_RESULT";
1096 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1097 case BIND_APPLICATION: return "BIND_APPLICATION";
1098 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1099 case NEW_INTENT: return "NEW_INTENT";
1100 case RECEIVER: return "RECEIVER";
1101 case CREATE_SERVICE: return "CREATE_SERVICE";
1102 case SERVICE_ARGS: return "SERVICE_ARGS";
1103 case STOP_SERVICE: return "STOP_SERVICE";
1104 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1105 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1106 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1107 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1108 case BIND_SERVICE: return "BIND_SERVICE";
1109 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1110 case DUMP_SERVICE: return "DUMP_SERVICE";
1111 case LOW_MEMORY: return "LOW_MEMORY";
1112 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1113 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001114 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001115 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1116 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001117 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001118 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001119 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001120 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001121 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001122 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001123 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001124 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001125 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001126 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001127 case TRIM_MEMORY: return "TRIM_MEMORY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 }
1130 return "(unknown)";
1131 }
1132 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001133 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 switch (msg.what) {
1135 case LAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001136 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137
1138 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001139 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001140 handleLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 } break;
1142 case RELAUNCH_ACTIVITY: {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001143 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001144 handleRelaunchActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 } break;
1146 case PAUSE_ACTIVITY:
1147 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001148 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 break;
1150 case PAUSE_ACTIVITY_FINISHING:
1151 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
1152 break;
1153 case STOP_ACTIVITY_SHOW:
1154 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
1155 break;
1156 case STOP_ACTIVITY_HIDE:
1157 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
1158 break;
1159 case SHOW_WINDOW:
1160 handleWindowVisibility((IBinder)msg.obj, true);
1161 break;
1162 case HIDE_WINDOW:
1163 handleWindowVisibility((IBinder)msg.obj, false);
1164 break;
1165 case RESUME_ACTIVITY:
1166 handleResumeActivity((IBinder)msg.obj, true,
1167 msg.arg1 != 0);
1168 break;
1169 case SEND_RESULT:
1170 handleSendResult((ResultData)msg.obj);
1171 break;
1172 case DESTROY_ACTIVITY:
1173 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1174 msg.arg2, false);
1175 break;
1176 case BIND_APPLICATION:
1177 AppBindData data = (AppBindData)msg.obj;
1178 handleBindApplication(data);
1179 break;
1180 case EXIT_APPLICATION:
1181 if (mInitialApplication != null) {
1182 mInitialApplication.onTerminate();
1183 }
1184 Looper.myLooper().quit();
1185 break;
1186 case NEW_INTENT:
1187 handleNewIntent((NewIntentData)msg.obj);
1188 break;
1189 case RECEIVER:
1190 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001191 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 break;
1193 case CREATE_SERVICE:
1194 handleCreateService((CreateServiceData)msg.obj);
1195 break;
1196 case BIND_SERVICE:
1197 handleBindService((BindServiceData)msg.obj);
1198 break;
1199 case UNBIND_SERVICE:
1200 handleUnbindService((BindServiceData)msg.obj);
1201 break;
1202 case SERVICE_ARGS:
1203 handleServiceArgs((ServiceArgsData)msg.obj);
1204 break;
1205 case STOP_SERVICE:
1206 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001207 maybeSnapshot();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 break;
1209 case REQUEST_THUMBNAIL:
1210 handleRequestThumbnail((IBinder)msg.obj);
1211 break;
1212 case CONFIGURATION_CHANGED:
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001213 handleConfigurationChanged((Configuration)msg.obj, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 case CLEAN_UP_CONTEXT:
1216 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1217 cci.context.performFinalCleanup(cci.who, cci.what);
1218 break;
1219 case GC_WHEN_IDLE:
1220 scheduleGcIdler();
1221 break;
1222 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001223 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 break;
1225 case LOW_MEMORY:
1226 handleLowMemory();
1227 break;
1228 case ACTIVITY_CONFIGURATION_CHANGED:
1229 handleActivityConfigurationChanged((IBinder)msg.obj);
1230 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001231 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001232 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001233 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001234 case CREATE_BACKUP_AGENT:
1235 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
1236 break;
1237 case DESTROY_BACKUP_AGENT:
1238 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
1239 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001240 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001241 Process.killProcess(Process.myPid());
1242 break;
1243 case REMOVE_PROVIDER:
1244 completeRemoveProvider((IContentProvider)msg.obj);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001245 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001246 case ENABLE_JIT:
1247 ensureJitEnabled();
1248 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001249 case DISPATCH_PACKAGE_BROADCAST:
1250 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
1251 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001252 case SCHEDULE_CRASH:
1253 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001254 case DUMP_HEAP:
1255 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1256 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001257 case DUMP_ACTIVITY:
1258 handleDumpActivity((DumpComponentInfo)msg.obj);
1259 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001260 case SLEEPING:
1261 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
1262 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001263 case SET_CORE_SETTINGS:
1264 handleSetCoreSettings((Bundle) msg.obj);
1265 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001266 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1267 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001268 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001269 case TRIM_MEMORY:
1270 handleTrimMemory(msg.arg1);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001271 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001273 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 }
Bob Leee5408332009-09-04 18:31:17 -07001275
Brian Carlstromed7e0072011-03-24 13:27:57 -07001276 private void maybeSnapshot() {
1277 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001278 // convert the *private* ActivityThread.PackageInfo to *public* known
1279 // android.content.pm.PackageInfo
1280 String packageName = mBoundApplication.info.mPackageName;
1281 android.content.pm.PackageInfo packageInfo = null;
1282 try {
1283 Context context = getSystemContext();
1284 if(context == null) {
1285 Log.e(TAG, "cannot get a valid context");
1286 return;
1287 }
1288 PackageManager pm = context.getPackageManager();
1289 if(pm == null) {
1290 Log.e(TAG, "cannot get a valid PackageManager");
1291 return;
1292 }
1293 packageInfo = pm.getPackageInfo(
1294 packageName, PackageManager.GET_ACTIVITIES);
1295 } catch (NameNotFoundException e) {
1296 Log.e(TAG, "cannot get package info for " + packageName, e);
1297 }
1298 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001299 }
1300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 }
1302
Romain Guy65b345f2011-07-27 18:51:50 -07001303 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001305 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001306 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001307 if (mBoundApplication != null && mProfiler.profileFd != null
1308 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001309 stopProfiling = true;
1310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 if (a != null) {
1312 mNewActivities = null;
1313 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001314 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001316 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 TAG, "Reporting idle of " + a +
1318 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001319 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if (a.activity != null && !a.activity.mFinished) {
1321 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001322 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001323 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001325 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327 }
1328 prev = a;
1329 a = a.nextIdle;
1330 prev.nextIdle = null;
1331 } while (a != null);
1332 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001333 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001334 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001335 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001336 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 return false;
1338 }
1339 }
1340
1341 final class GcIdler implements MessageQueue.IdleHandler {
1342 public final boolean queueIdle() {
1343 doGcIfNeeded();
1344 return false;
1345 }
1346 }
1347
Romain Guy65b345f2011-07-27 18:51:50 -07001348 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001349 final private String mResDir;
1350 final private float mScale;
1351 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001352
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001353 ResourcesKey(String resDir, float scale) {
1354 mResDir = resDir;
1355 mScale = scale;
1356 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1357 }
Bob Leee5408332009-09-04 18:31:17 -07001358
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001359 @Override
1360 public int hashCode() {
1361 return mHash;
1362 }
1363
1364 @Override
1365 public boolean equals(Object obj) {
1366 if (!(obj instanceof ResourcesKey)) {
1367 return false;
1368 }
1369 ResourcesKey peer = (ResourcesKey) obj;
1370 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1371 }
1372 }
1373
Romain Guy65b345f2011-07-27 18:51:50 -07001374 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001375 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377
Romain Guy65b345f2011-07-27 18:51:50 -07001378 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001379 ActivityThread am = currentActivityThread();
1380 return (am != null && am.mBoundApplication != null)
1381 ? am.mBoundApplication.processName : null;
1382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383
Romain Guy65b345f2011-07-27 18:51:50 -07001384 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001385 ActivityThread am = currentActivityThread();
1386 return am != null ? am.mInitialApplication : null;
1387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001389 public static IPackageManager getPackageManager() {
1390 if (sPackageManager != null) {
1391 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1392 return sPackageManager;
1393 }
1394 IBinder b = ServiceManager.getService("package");
1395 //Slog.v("PackageManager", "default service binder = " + b);
1396 sPackageManager = IPackageManager.Stub.asInterface(b);
1397 //Slog.v("PackageManager", "default service = " + sPackageManager);
1398 return sPackageManager;
1399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001401 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1402 DisplayMetrics dm = mDisplayMetrics.get(ci);
1403 if (dm != null && !forceUpdate) {
1404 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001405 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001406 if (dm == null) {
1407 dm = new DisplayMetrics();
1408 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001409 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001410 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1411 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001412 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1413 // + metrics.heightPixels + " den=" + metrics.density
1414 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001415 return dm;
1416 }
1417
1418 static Configuration applyConfigCompat(Configuration config, CompatibilityInfo compat) {
1419 if (config == null) {
1420 return null;
1421 }
1422 if (compat != null && !compat.supportsScreen()) {
1423 config = new Configuration(config);
1424 compat.applyToConfiguration(config);
1425 }
1426 return config;
1427 }
1428
Romain Guy65b345f2011-07-27 18:51:50 -07001429 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001430 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1431 if (config == null) {
1432 return null;
1433 }
1434 if (compat != null && !compat.supportsScreen()) {
1435 mMainThreadConfig.setTo(config);
1436 config = mMainThreadConfig;
1437 compat.applyToConfiguration(config);
1438 }
1439 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001442 /**
1443 * Creates the top level Resources for applications with the given compatibility info.
1444 *
1445 * @param resDir the resource directory.
1446 * @param compInfo the compability info. It will use the default compatibility info when it's
1447 * null.
1448 */
1449 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1450 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1451 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001453 // Resources is app scale dependent.
1454 if (false) {
1455 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1456 + compInfo.applicationScale);
1457 }
1458 WeakReference<Resources> wr = mActiveResources.get(key);
1459 r = wr != null ? wr.get() : null;
1460 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1461 if (r != null && r.getAssets().isUpToDate()) {
1462 if (false) {
1463 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1464 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1465 }
1466 return r;
1467 }
1468 }
1469
1470 //if (r != null) {
1471 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1472 // + r + " " + resDir);
1473 //}
1474
1475 AssetManager assets = new AssetManager();
1476 if (assets.addAssetPath(resDir) == 0) {
1477 return null;
1478 }
1479
1480 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001481 DisplayMetrics metrics = getDisplayMetricsLocked(compInfo, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001482 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1483 if (false) {
1484 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1485 + r.getConfiguration() + " appScale="
1486 + r.getCompatibilityInfo().applicationScale);
1487 }
1488
1489 synchronized (mPackages) {
1490 WeakReference<Resources> wr = mActiveResources.get(key);
1491 Resources existing = wr != null ? wr.get() : null;
1492 if (existing != null && existing.getAssets().isUpToDate()) {
1493 // Someone else already created the resources while we were
1494 // unlocked; go ahead and use theirs.
1495 r.getAssets().close();
1496 return existing;
1497 }
1498
1499 // XXX need to remove entries when weak references go away
1500 mActiveResources.put(key, new WeakReference<Resources>(r));
1501 return r;
1502 }
1503 }
1504
1505 /**
1506 * Creates the top level resources for the given package.
1507 */
1508 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001509 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001510 }
1511
1512 final Handler getHandler() {
1513 return mH;
1514 }
1515
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001516 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1517 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001518 synchronized (mPackages) {
1519 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1521 ref = mPackages.get(packageName);
1522 } else {
1523 ref = mResourcePackages.get(packageName);
1524 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001525 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001526 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001527 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1528 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 if (packageInfo != null && (packageInfo.mResources == null
1530 || packageInfo.mResources.getAssets().isUpToDate())) {
1531 if (packageInfo.isSecurityViolation()
1532 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1533 throw new SecurityException(
1534 "Requesting code from " + packageName
1535 + " to be run in process "
1536 + mBoundApplication.processName
1537 + "/" + mBoundApplication.appInfo.uid);
1538 }
1539 return packageInfo;
1540 }
1541 }
1542
1543 ApplicationInfo ai = null;
1544 try {
1545 ai = getPackageManager().getApplicationInfo(packageName,
1546 PackageManager.GET_SHARED_LIBRARY_FILES);
1547 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001548 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550
1551 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001552 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 }
1554
1555 return null;
1556 }
1557
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001558 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1559 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1561 boolean securityViolation = includeCode && ai.uid != 0
1562 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
1563 ? ai.uid != mBoundApplication.appInfo.uid : true);
1564 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1565 |Context.CONTEXT_IGNORE_SECURITY))
1566 == Context.CONTEXT_INCLUDE_CODE) {
1567 if (securityViolation) {
1568 String msg = "Requesting code from " + ai.packageName
1569 + " (with uid " + ai.uid + ")";
1570 if (mBoundApplication != null) {
1571 msg = msg + " to be run in process "
1572 + mBoundApplication.processName + " (with uid "
1573 + mBoundApplication.appInfo.uid + ")";
1574 }
1575 throw new SecurityException(msg);
1576 }
1577 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001578 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
1580
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001581 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1582 CompatibilityInfo compatInfo) {
1583 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 }
1585
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001586 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1587 synchronized (mPackages) {
1588 WeakReference<LoadedApk> ref;
1589 if (includeCode) {
1590 ref = mPackages.get(packageName);
1591 } else {
1592 ref = mResourcePackages.get(packageName);
1593 }
1594 return ref != null ? ref.get() : null;
1595 }
1596 }
1597
Romain Guy65b345f2011-07-27 18:51:50 -07001598 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1600 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001601 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 if (includeCode) {
1603 ref = mPackages.get(aInfo.packageName);
1604 } else {
1605 ref = mResourcePackages.get(aInfo.packageName);
1606 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001607 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (packageInfo == null || (packageInfo.mResources != null
1609 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001610 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 : "Loading resource-only package ") + aInfo.packageName
1612 + " (in " + (mBoundApplication != null
1613 ? mBoundApplication.processName : null)
1614 + ")");
1615 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001616 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 securityViolation, includeCode &&
1618 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1619 if (includeCode) {
1620 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001621 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 } else {
1623 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001624 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626 }
1627 return packageInfo;
1628 }
1629 }
1630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001631 ActivityThread() {
1632 }
1633
1634 public ApplicationThread getApplicationThread()
1635 {
1636 return mAppThread;
1637 }
1638
1639 public Instrumentation getInstrumentation()
1640 {
1641 return mInstrumentation;
1642 }
1643
1644 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001645 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647
1648 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001649 return mProfiler != null && mProfiler.profileFile != null
1650 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
1652
1653 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001654 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 }
1656
1657 public Looper getLooper() {
1658 return mLooper;
1659 }
1660
1661 public Application getApplication() {
1662 return mInitialApplication;
1663 }
Bob Leee5408332009-09-04 18:31:17 -07001664
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001665 public String getProcessName() {
1666 return mBoundApplication.processName;
1667 }
Bob Leee5408332009-09-04 18:31:17 -07001668
Dianne Hackborn21556372010-02-04 16:34:40 -08001669 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 synchronized (this) {
1671 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001672 ContextImpl context =
1673 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001674 LoadedApk info = new LoadedApk(this, "android", context, null,
1675 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 context.init(info, null, this);
1677 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001678 getConfiguration(), getDisplayMetricsLocked(
1679 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001681 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 // + ": " + context.getResources().getConfiguration());
1683 }
1684 }
1685 return mSystemContext;
1686 }
1687
Mike Cleron432b7132009-09-24 15:28:29 -07001688 public void installSystemApplicationInfo(ApplicationInfo info) {
1689 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001690 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001691 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001692 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001693
1694 // give ourselves a default profiler
1695 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001696 }
1697 }
1698
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001699 void ensureJitEnabled() {
1700 if (!mJitEnabled) {
1701 mJitEnabled = true;
1702 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1703 }
1704 }
1705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 void scheduleGcIdler() {
1707 if (!mGcIdlerScheduled) {
1708 mGcIdlerScheduled = true;
1709 Looper.myQueue().addIdleHandler(mGcIdler);
1710 }
1711 mH.removeMessages(H.GC_WHEN_IDLE);
1712 }
1713
1714 void unscheduleGcIdler() {
1715 if (mGcIdlerScheduled) {
1716 mGcIdlerScheduled = false;
1717 Looper.myQueue().removeIdleHandler(mGcIdler);
1718 }
1719 mH.removeMessages(H.GC_WHEN_IDLE);
1720 }
1721
1722 void doGcIfNeeded() {
1723 mGcIdlerScheduled = false;
1724 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001725 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 // + "m now=" + now);
1727 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001728 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 BinderInternal.forceGc("bg");
1730 }
1731 }
1732
Jeff Hamilton52d32032011-01-08 15:31:26 -06001733 public void registerOnActivityPausedListener(Activity activity,
1734 OnActivityPausedListener listener) {
1735 synchronized (mOnPauseListeners) {
1736 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1737 if (list == null) {
1738 list = new ArrayList<OnActivityPausedListener>();
1739 mOnPauseListeners.put(activity, list);
1740 }
1741 list.add(listener);
1742 }
1743 }
1744
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001745 public void unregisterOnActivityPausedListener(Activity activity,
1746 OnActivityPausedListener listener) {
1747 synchronized (mOnPauseListeners) {
1748 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1749 if (list != null) {
1750 list.remove(listener);
1751 }
1752 }
1753 }
1754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 public final ActivityInfo resolveActivityInfo(Intent intent) {
1756 ActivityInfo aInfo = intent.resolveActivityInfo(
1757 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1758 if (aInfo == null) {
1759 // Throw an exception.
1760 Instrumentation.checkStartActivityResult(
1761 IActivityManager.START_CLASS_NOT_FOUND, intent);
1762 }
1763 return aInfo;
1764 }
Bob Leee5408332009-09-04 18:31:17 -07001765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001768 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001769 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001771 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 r.intent = intent;
1773 r.state = state;
1774 r.parent = parent;
1775 r.embeddedID = id;
1776 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001777 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 if (localLOGV) {
1779 ComponentName compname = intent.getComponent();
1780 String name;
1781 if (compname != null) {
1782 name = compname.toShortString();
1783 } else {
1784 name = "(Intent " + intent + ").getComponent() returned null";
1785 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001786 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 + ", comp=" + name
1788 + ", token=" + token);
1789 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001790 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 }
1792
1793 public final Activity getActivity(IBinder token) {
1794 return mActivities.get(token).activity;
1795 }
1796
1797 public final void sendActivityResult(
1798 IBinder token, String id, int requestCode,
1799 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001800 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001801 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1803 list.add(new ResultInfo(id, requestCode, resultCode, data));
1804 mAppThread.scheduleSendResult(token, list);
1805 }
1806
1807 // if the thread hasn't started yet, we don't have the handler, so just
1808 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001809 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 queueOrSendMessage(what, obj, 0, 0);
1811 }
1812
Romain Guy65b345f2011-07-27 18:51:50 -07001813 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 queueOrSendMessage(what, obj, arg1, 0);
1815 }
1816
Romain Guy65b345f2011-07-27 18:51:50 -07001817 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001819 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1821 + ": " + arg1 + " / " + obj);
1822 Message msg = Message.obtain();
1823 msg.what = what;
1824 msg.obj = obj;
1825 msg.arg1 = arg1;
1826 msg.arg2 = arg2;
1827 mH.sendMessage(msg);
1828 }
1829 }
1830
Dianne Hackborn21556372010-02-04 16:34:40 -08001831 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 String what) {
1833 ContextCleanupInfo cci = new ContextCleanupInfo();
1834 cci.context = context;
1835 cci.who = who;
1836 cci.what = what;
1837 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1838 }
1839
Romain Guy65b345f2011-07-27 18:51:50 -07001840 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1842
1843 ActivityInfo aInfo = r.activityInfo;
1844 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001845 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 Context.CONTEXT_INCLUDE_CODE);
1847 }
Bob Leee5408332009-09-04 18:31:17 -07001848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 ComponentName component = r.intent.getComponent();
1850 if (component == null) {
1851 component = r.intent.resolveActivity(
1852 mInitialApplication.getPackageManager());
1853 r.intent.setComponent(component);
1854 }
1855
1856 if (r.activityInfo.targetActivity != null) {
1857 component = new ComponentName(r.activityInfo.packageName,
1858 r.activityInfo.targetActivity);
1859 }
1860
1861 Activity activity = null;
1862 try {
1863 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1864 activity = mInstrumentation.newActivity(
1865 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001866 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 r.intent.setExtrasClassLoader(cl);
1868 if (r.state != null) {
1869 r.state.setClassLoader(cl);
1870 }
1871 } catch (Exception e) {
1872 if (!mInstrumentation.onException(activity, e)) {
1873 throw new RuntimeException(
1874 "Unable to instantiate activity " + component
1875 + ": " + e.toString(), e);
1876 }
1877 }
1878
1879 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001880 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001881
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001882 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1883 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 TAG, r + ": app=" + app
1885 + ", appName=" + app.getPackageName()
1886 + ", pkg=" + r.packageInfo.getPackageName()
1887 + ", comp=" + r.intent.getComponent().toShortString()
1888 + ", dir=" + r.packageInfo.getAppDir());
1889
1890 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001891 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 appContext.init(r.packageInfo, r.token, this);
1893 appContext.setOuterContext(activity);
1894 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001895 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001896 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001897 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001898 activity.attach(appContext, this, getInstrumentation(), r.token,
1899 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001900 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001901
Christopher Tateb70f3df2009-04-07 16:07:59 -07001902 if (customIntent != null) {
1903 activity.mIntent = customIntent;
1904 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001905 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 activity.mStartedActivity = false;
1907 int theme = r.activityInfo.getThemeResource();
1908 if (theme != 0) {
1909 activity.setTheme(theme);
1910 }
1911
1912 activity.mCalled = false;
1913 mInstrumentation.callActivityOnCreate(activity, r.state);
1914 if (!activity.mCalled) {
1915 throw new SuperNotCalledException(
1916 "Activity " + r.intent.getComponent().toShortString() +
1917 " did not call through to super.onCreate()");
1918 }
1919 r.activity = activity;
1920 r.stopped = true;
1921 if (!r.activity.mFinished) {
1922 activity.performStart();
1923 r.stopped = false;
1924 }
1925 if (!r.activity.mFinished) {
1926 if (r.state != null) {
1927 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
1928 }
1929 }
1930 if (!r.activity.mFinished) {
1931 activity.mCalled = false;
1932 mInstrumentation.callActivityOnPostCreate(activity, r.state);
1933 if (!activity.mCalled) {
1934 throw new SuperNotCalledException(
1935 "Activity " + r.intent.getComponent().toShortString() +
1936 " did not call through to super.onPostCreate()");
1937 }
1938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
1940 r.paused = true;
1941
1942 mActivities.put(r.token, r);
1943
1944 } catch (SuperNotCalledException e) {
1945 throw e;
1946
1947 } catch (Exception e) {
1948 if (!mInstrumentation.onException(activity, e)) {
1949 throw new RuntimeException(
1950 "Unable to start activity " + component
1951 + ": " + e.toString(), e);
1952 }
1953 }
1954
1955 return activity;
1956 }
1957
Romain Guy65b345f2011-07-27 18:51:50 -07001958 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 // If we are getting ready to gc after going to the background, well
1960 // we are back active so skip it.
1961 unscheduleGcIdler();
1962
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001963 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001964 mProfiler.setProfiler(r.profileFile, r.profileFd);
1965 mProfiler.startProfiling();
1966 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001967 }
1968
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001969 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001971 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972
1973 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08001974 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001975 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 handleResumeActivity(r.token, false, r.isForward);
1977
1978 if (!r.activity.mFinished && r.startsNotResumed) {
1979 // The activity manager actually wants this one to start out
1980 // paused, because it needs to be visible but isn't in the
1981 // foreground. We accomplish this by going through the
1982 // normal startup (because activities expect to go through
1983 // onResume() the first time they run, before their window
1984 // is displayed), and then pausing it. However, in this case
1985 // we do -not- need to do the full pause cycle (of freezing
1986 // and such) because the activity manager assumes it can just
1987 // retain the current state it has.
1988 try {
1989 r.activity.mCalled = false;
1990 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08001991 // We need to keep around the original state, in case
1992 // we need to be created again.
1993 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001994 if (!r.activity.mCalled) {
1995 throw new SuperNotCalledException(
1996 "Activity " + r.intent.getComponent().toShortString() +
1997 " did not call through to super.onPause()");
1998 }
1999
2000 } catch (SuperNotCalledException e) {
2001 throw e;
2002
2003 } catch (Exception e) {
2004 if (!mInstrumentation.onException(r.activity, e)) {
2005 throw new RuntimeException(
2006 "Unable to pause activity "
2007 + r.intent.getComponent().toShortString()
2008 + ": " + e.toString(), e);
2009 }
2010 }
2011 r.paused = true;
2012 }
2013 } else {
2014 // If there was an error, for any reason, tell the activity
2015 // manager to stop us.
2016 try {
2017 ActivityManagerNative.getDefault()
2018 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2019 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002020 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
2023 }
2024
Romain Guy65b345f2011-07-27 18:51:50 -07002025 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 List<Intent> intents) {
2027 final int N = intents.size();
2028 for (int i=0; i<N; i++) {
2029 Intent intent = intents.get(i);
2030 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002031 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2033 }
2034 }
2035
2036 public final void performNewIntents(IBinder token,
2037 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002038 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 if (r != null) {
2040 final boolean resumed = !r.paused;
2041 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002042 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043 mInstrumentation.callActivityOnPause(r.activity);
2044 }
2045 deliverNewIntents(r, intents);
2046 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002047 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002048 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 }
2050 }
2051 }
Bob Leee5408332009-09-04 18:31:17 -07002052
Romain Guy65b345f2011-07-27 18:51:50 -07002053 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 performNewIntents(data.token, data.intents);
2055 }
2056
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002057 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2058
2059 /**
2060 * Return the Intent that's currently being handled by a
2061 * BroadcastReceiver on this thread, or null if none.
2062 * @hide
2063 */
2064 public static Intent getIntentBeingBroadcast() {
2065 return sCurrentBroadcastIntent.get();
2066 }
2067
Romain Guy65b345f2011-07-27 18:51:50 -07002068 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 // If we are getting ready to gc after going to the background, well
2070 // we are back active so skip it.
2071 unscheduleGcIdler();
2072
2073 String component = data.intent.getComponent().getClassName();
2074
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002075 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002076 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077
2078 IActivityManager mgr = ActivityManagerNative.getDefault();
2079
Romain Guy65b345f2011-07-27 18:51:50 -07002080 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 try {
2082 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2083 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002084 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002085 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2086 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002087 if (DEBUG_BROADCAST) Slog.i(TAG,
2088 "Finishing failed broadcast to " + data.intent.getComponent());
2089 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 throw new RuntimeException(
2091 "Unable to instantiate receiver " + component
2092 + ": " + e.toString(), e);
2093 }
2094
2095 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002096 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002097
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002098 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 TAG, "Performing receive of " + data.intent
2100 + ": app=" + app
2101 + ", appName=" + app.getPackageName()
2102 + ", pkg=" + packageInfo.getPackageName()
2103 + ", comp=" + data.intent.getComponent().toShortString()
2104 + ", dir=" + packageInfo.getAppDir());
2105
Dianne Hackborn21556372010-02-04 16:34:40 -08002106 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002107 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002108 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 receiver.onReceive(context.getReceiverRestrictedContext(),
2110 data.intent);
2111 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002112 if (DEBUG_BROADCAST) Slog.i(TAG,
2113 "Finishing failed broadcast to " + data.intent.getComponent());
2114 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 if (!mInstrumentation.onException(receiver, e)) {
2116 throw new RuntimeException(
2117 "Unable to start receiver " + component
2118 + ": " + e.toString(), e);
2119 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002120 } finally {
2121 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123
Dianne Hackborne829fef2010-10-26 17:44:01 -07002124 if (receiver.getPendingResult() != null) {
2125 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127 }
2128
Christopher Tate181fafa2009-05-14 11:12:14 -07002129 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002130 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002131 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002132
2133 // no longer idle; we have backup work to do
2134 unscheduleGcIdler();
2135
2136 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002137 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002138 String packageName = packageInfo.mPackageName;
2139 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002140 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002141 + " already exists");
2142 return;
2143 }
Bob Leee5408332009-09-04 18:31:17 -07002144
Christopher Tate181fafa2009-05-14 11:12:14 -07002145 BackupAgent agent = null;
2146 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002147
Christopher Tate79ec80d2011-06-24 14:58:49 -07002148 // full backup operation but no app-supplied agent? use the default implementation
2149 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2150 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002151 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002152 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002153
Christopher Tate181fafa2009-05-14 11:12:14 -07002154 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002155 IBinder binder = null;
2156 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002157 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2158
Christopher Tated1475e02009-07-09 15:36:17 -07002159 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002160 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002161
2162 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002163 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002164 context.init(packageInfo, null, this);
2165 context.setOuterContext(agent);
2166 agent.attach(context);
2167
2168 agent.onCreate();
2169 binder = agent.onBind();
2170 mBackupAgents.put(packageName, agent);
2171 } catch (Exception e) {
2172 // If this is during restore, fail silently; otherwise go
2173 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002174 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002175 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2176 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002177 throw e;
2178 }
2179 // falling through with 'binder' still null
2180 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002181
2182 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002183 try {
2184 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2185 } catch (RemoteException e) {
2186 // nothing to do.
2187 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002188 } catch (Exception e) {
2189 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002190 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002191 }
2192 }
2193
2194 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002195 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002196 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002197
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002198 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002199 String packageName = packageInfo.mPackageName;
2200 BackupAgent agent = mBackupAgents.get(packageName);
2201 if (agent != null) {
2202 try {
2203 agent.onDestroy();
2204 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002205 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002206 e.printStackTrace();
2207 }
2208 mBackupAgents.remove(packageName);
2209 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002210 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002211 }
2212 }
2213
Romain Guy65b345f2011-07-27 18:51:50 -07002214 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 // If we are getting ready to gc after going to the background, well
2216 // we are back active so skip it.
2217 unscheduleGcIdler();
2218
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002219 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002220 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 Service service = null;
2222 try {
2223 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2224 service = (Service) cl.loadClass(data.info.name).newInstance();
2225 } catch (Exception e) {
2226 if (!mInstrumentation.onException(service, e)) {
2227 throw new RuntimeException(
2228 "Unable to instantiate service " + data.info.name
2229 + ": " + e.toString(), e);
2230 }
2231 }
2232
2233 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002234 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235
Dianne Hackborn21556372010-02-04 16:34:40 -08002236 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 context.init(packageInfo, null, this);
2238
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002239 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 context.setOuterContext(service);
2241 service.attach(context, this, data.info.name, data.token, app,
2242 ActivityManagerNative.getDefault());
2243 service.onCreate();
2244 mServices.put(data.token, service);
2245 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002246 ActivityManagerNative.getDefault().serviceDoneExecuting(
2247 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 } catch (RemoteException e) {
2249 // nothing to do.
2250 }
2251 } catch (Exception e) {
2252 if (!mInstrumentation.onException(service, e)) {
2253 throw new RuntimeException(
2254 "Unable to create service " + data.info.name
2255 + ": " + e.toString(), e);
2256 }
2257 }
2258 }
2259
Romain Guy65b345f2011-07-27 18:51:50 -07002260 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 Service s = mServices.get(data.token);
2262 if (s != null) {
2263 try {
2264 data.intent.setExtrasClassLoader(s.getClassLoader());
2265 try {
2266 if (!data.rebind) {
2267 IBinder binder = s.onBind(data.intent);
2268 ActivityManagerNative.getDefault().publishService(
2269 data.token, data.intent, binder);
2270 } else {
2271 s.onRebind(data.intent);
2272 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002273 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002275 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 } catch (RemoteException ex) {
2277 }
2278 } catch (Exception e) {
2279 if (!mInstrumentation.onException(s, e)) {
2280 throw new RuntimeException(
2281 "Unable to bind to service " + s
2282 + " with " + data.intent + ": " + e.toString(), e);
2283 }
2284 }
2285 }
2286 }
2287
Romain Guy65b345f2011-07-27 18:51:50 -07002288 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002289 Service s = mServices.get(data.token);
2290 if (s != null) {
2291 try {
2292 data.intent.setExtrasClassLoader(s.getClassLoader());
2293 boolean doRebind = s.onUnbind(data.intent);
2294 try {
2295 if (doRebind) {
2296 ActivityManagerNative.getDefault().unbindFinished(
2297 data.token, data.intent, doRebind);
2298 } else {
2299 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002300 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302 } catch (RemoteException ex) {
2303 }
2304 } catch (Exception e) {
2305 if (!mInstrumentation.onException(s, e)) {
2306 throw new RuntimeException(
2307 "Unable to unbind to service " + s
2308 + " with " + data.intent + ": " + e.toString(), e);
2309 }
2310 }
2311 }
2312 }
2313
Dianne Hackborn625ac272010-09-17 18:29:22 -07002314 private void handleDumpService(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002315 Service s = mServices.get(info.token);
2316 if (s != null) {
2317 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2318 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2319 pw.flush();
2320 try {
2321 info.fd.close();
2322 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 }
2324 }
2325 }
2326
Dianne Hackborn625ac272010-09-17 18:29:22 -07002327 private void handleDumpActivity(DumpComponentInfo info) {
Dianne Hackborne17aeb32011-04-07 15:11:57 -07002328 ActivityClientRecord r = mActivities.get(info.token);
2329 if (r != null && r.activity != null) {
2330 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2331 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2332 pw.flush();
2333 try {
2334 info.fd.close();
2335 } catch (IOException e) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07002336 }
2337 }
2338 }
2339
Romain Guy65b345f2011-07-27 18:51:50 -07002340 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 Service s = mServices.get(data.token);
2342 if (s != null) {
2343 try {
2344 if (data.args != null) {
2345 data.args.setExtrasClassLoader(s.getClassLoader());
2346 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002347 int res;
2348 if (!data.taskRemoved) {
2349 res = s.onStartCommand(data.args, data.flags, data.startId);
2350 } else {
2351 s.onTaskRemoved(data.args);
2352 res = Service.START_TASK_REMOVED_COMPLETE;
2353 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002354
2355 QueuedWork.waitToFinish();
2356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002358 ActivityManagerNative.getDefault().serviceDoneExecuting(
2359 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 } catch (RemoteException e) {
2361 // nothing to do.
2362 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002363 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 } catch (Exception e) {
2365 if (!mInstrumentation.onException(s, e)) {
2366 throw new RuntimeException(
2367 "Unable to start service " + s
2368 + " with " + data.args + ": " + e.toString(), e);
2369 }
2370 }
2371 }
2372 }
2373
Romain Guy65b345f2011-07-27 18:51:50 -07002374 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 Service s = mServices.remove(token);
2376 if (s != null) {
2377 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002378 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 s.onDestroy();
2380 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002381 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002383 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002385
2386 QueuedWork.waitToFinish();
2387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002389 ActivityManagerNative.getDefault().serviceDoneExecuting(
2390 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 } catch (RemoteException e) {
2392 // nothing to do.
2393 }
2394 } catch (Exception e) {
2395 if (!mInstrumentation.onException(s, e)) {
2396 throw new RuntimeException(
2397 "Unable to stop service " + s
2398 + ": " + e.toString(), e);
2399 }
2400 }
2401 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002402 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002405 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002407 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002408 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 + " finished=" + r.activity.mFinished);
2410 if (r != null && !r.activity.mFinished) {
2411 if (clearHide) {
2412 r.hideForNow = false;
2413 r.activity.mStartedActivity = false;
2414 }
2415 try {
2416 if (r.pendingIntents != null) {
2417 deliverNewIntents(r, r.pendingIntents);
2418 r.pendingIntents = null;
2419 }
2420 if (r.pendingResults != null) {
2421 deliverResults(r, r.pendingResults);
2422 r.pendingResults = null;
2423 }
2424 r.activity.performResume();
2425
Bob Leee5408332009-09-04 18:31:17 -07002426 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 r.paused = false;
2430 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 r.state = null;
2432 } catch (Exception e) {
2433 if (!mInstrumentation.onException(r.activity, e)) {
2434 throw new RuntimeException(
2435 "Unable to resume activity "
2436 + r.intent.getComponent().toShortString()
2437 + ": " + e.toString(), e);
2438 }
2439 }
2440 }
2441 return r;
2442 }
2443
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002444 final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
2445 if (r.mPendingRemoveWindow != null) {
2446 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2447 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2448 if (wtoken != null) {
2449 WindowManagerImpl.getDefault().closeAll(wtoken,
2450 r.activity.getClass().getName(), "Activity");
2451 }
2452 }
2453 r.mPendingRemoveWindow = null;
2454 r.mPendingRemoveWindowManager = null;
2455 }
2456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2458 // If we are getting ready to gc after going to the background, well
2459 // we are back active so skip it.
2460 unscheduleGcIdler();
2461
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002462 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463
2464 if (r != null) {
2465 final Activity a = r.activity;
2466
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002467 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 TAG, "Resume " + r + " started activity: " +
2469 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2470 + ", finished: " + a.mFinished);
2471
2472 final int forwardBit = isForward ?
2473 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 // If the window hasn't yet been added to the window manager,
2476 // and this guy didn't finish itself or start another activity,
2477 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002478 boolean willBeVisible = !a.mStartedActivity;
2479 if (!willBeVisible) {
2480 try {
2481 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2482 a.getActivityToken());
2483 } catch (RemoteException e) {
2484 }
2485 }
2486 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 r.window = r.activity.getWindow();
2488 View decor = r.window.getDecorView();
2489 decor.setVisibility(View.INVISIBLE);
2490 ViewManager wm = a.getWindowManager();
2491 WindowManager.LayoutParams l = r.window.getAttributes();
2492 a.mDecor = decor;
2493 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2494 l.softInputMode |= forwardBit;
2495 if (a.mVisibleFromClient) {
2496 a.mWindowAdded = true;
2497 wm.addView(decor, l);
2498 }
2499
2500 // If the window has already been added, but during resume
2501 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002502 // window visible.
2503 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002504 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 TAG, "Launch " + r + " mStartedActivity set");
2506 r.hideForNow = true;
2507 }
2508
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002509 // Get rid of anything left hanging around.
2510 cleanUpPendingRemoveWindows(r);
2511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 // The window is now visible if it has been added, we are not
2513 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002514 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002515 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002516 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002517 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002518 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 performConfigurationChanged(r.activity, r.newConfig);
2520 r.newConfig = null;
2521 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002522 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 + isForward);
2524 WindowManager.LayoutParams l = r.window.getAttributes();
2525 if ((l.softInputMode
2526 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2527 != forwardBit) {
2528 l.softInputMode = (l.softInputMode
2529 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2530 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002531 if (r.activity.mVisibleFromClient) {
2532 ViewManager wm = a.getWindowManager();
2533 View decor = r.window.getDecorView();
2534 wm.updateViewLayout(decor, l);
2535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 }
2537 r.activity.mVisibleFromServer = true;
2538 mNumVisibleActivities++;
2539 if (r.activity.mVisibleFromClient) {
2540 r.activity.makeVisible();
2541 }
2542 }
2543
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002544 if (!r.onlyLocalRequest) {
2545 r.nextIdle = mNewActivities;
2546 mNewActivities = r;
2547 if (localLOGV) Slog.v(
2548 TAG, "Scheduling idle handler for " + r);
2549 Looper.myQueue().addIdleHandler(new Idler());
2550 }
2551 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552
2553 } else {
2554 // If an exception was thrown when trying to resume, then
2555 // just end this activity.
2556 try {
2557 ActivityManagerNative.getDefault()
2558 .finishActivity(token, Activity.RESULT_CANCELED, null);
2559 } catch (RemoteException ex) {
2560 }
2561 }
2562 }
2563
2564 private int mThumbnailWidth = -1;
2565 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002566 private Bitmap mAvailThumbnailBitmap = null;
2567 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568
Romain Guy65b345f2011-07-27 18:51:50 -07002569 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002570 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002572 if (thumbnail == null) {
2573 int w = mThumbnailWidth;
2574 int h;
2575 if (w < 0) {
2576 Resources res = r.activity.getResources();
2577 mThumbnailHeight = h =
2578 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002580 mThumbnailWidth = w =
2581 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2582 } else {
2583 h = mThumbnailHeight;
2584 }
2585
2586 // On platforms where we don't want thumbnails, set dims to (0,0)
2587 if ((w > 0) && (h > 0)) {
2588 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2589 thumbnail.eraseColor(0);
2590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002591 }
2592
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002593 if (thumbnail != null) {
2594 Canvas cv = mThumbnailCanvas;
2595 if (cv == null) {
2596 mThumbnailCanvas = cv = new Canvas();
2597 }
2598
2599 cv.setBitmap(thumbnail);
2600 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2601 mAvailThumbnailBitmap = thumbnail;
2602 thumbnail = null;
2603 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002604 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 } catch (Exception e) {
2608 if (!mInstrumentation.onException(r.activity, e)) {
2609 throw new RuntimeException(
2610 "Unable to create thumbnail of "
2611 + r.intent.getComponent().toShortString()
2612 + ": " + e.toString(), e);
2613 }
2614 thumbnail = null;
2615 }
2616
2617 return thumbnail;
2618 }
2619
Romain Guy65b345f2011-07-27 18:51:50 -07002620 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002622 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002624 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 if (userLeaving) {
2626 performUserLeavingActivity(r);
2627 }
Bob Leee5408332009-09-04 18:31:17 -07002628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002630 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002632 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002633 if (r.isPreHoneycomb()) {
2634 QueuedWork.waitToFinish();
2635 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002637 // Tell the activity manager we have paused.
2638 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002639 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640 } catch (RemoteException ex) {
2641 }
2642 }
2643 }
2644
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002645 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 mInstrumentation.callActivityOnUserLeaving(r.activity);
2647 }
2648
2649 final Bundle performPauseActivity(IBinder token, boolean finished,
2650 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002651 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 return r != null ? performPauseActivity(r, finished, saveState) : null;
2653 }
2654
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002655 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 boolean saveState) {
2657 if (r.paused) {
2658 if (r.activity.mFinished) {
2659 // If we are finishing, we won't call onResume() in certain cases.
2660 // So here we likewise don't want to call onPause() if the activity
2661 // isn't resumed.
2662 return null;
2663 }
2664 RuntimeException e = new RuntimeException(
2665 "Performing pause of activity that is not resumed: "
2666 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002667 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002668 }
2669 Bundle state = null;
2670 if (finished) {
2671 r.activity.mFinished = true;
2672 }
2673 try {
2674 // Next have the activity save its current state and managed dialogs...
2675 if (!r.activity.mFinished && saveState) {
2676 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002677 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2679 r.state = state;
2680 }
2681 // Now we are idle.
2682 r.activity.mCalled = false;
2683 mInstrumentation.callActivityOnPause(r.activity);
2684 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2685 if (!r.activity.mCalled) {
2686 throw new SuperNotCalledException(
2687 "Activity " + r.intent.getComponent().toShortString() +
2688 " did not call through to super.onPause()");
2689 }
2690
2691 } catch (SuperNotCalledException e) {
2692 throw e;
2693
2694 } catch (Exception e) {
2695 if (!mInstrumentation.onException(r.activity, e)) {
2696 throw new RuntimeException(
2697 "Unable to pause activity "
2698 + r.intent.getComponent().toShortString()
2699 + ": " + e.toString(), e);
2700 }
2701 }
2702 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002703
2704 // Notify any outstanding on paused listeners
2705 ArrayList<OnActivityPausedListener> listeners;
2706 synchronized (mOnPauseListeners) {
2707 listeners = mOnPauseListeners.remove(r.activity);
2708 }
2709 int size = (listeners != null ? listeners.size() : 0);
2710 for (int i = 0; i < size; i++) {
2711 listeners.get(i).onPaused(r.activity);
2712 }
2713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 return state;
2715 }
2716
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002717 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002718 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002719 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
2721
2722 private static class StopInfo {
2723 Bitmap thumbnail;
2724 CharSequence description;
2725 }
2726
Romain Guy65b345f2011-07-27 18:51:50 -07002727 private class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 public int count;
2729 ProviderRefCount(int pCount) {
2730 count = pCount;
2731 }
2732 }
2733
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002734 /**
2735 * Core implementation of stopping an activity. Note this is a little
2736 * tricky because the server's meaning of stop is slightly different
2737 * than our client -- for the server, stop means to save state and give
2738 * it the result when it is done, but the window may still be visible.
2739 * For the client, we want to call onStop()/onStart() to indicate when
2740 * the activity's UI visibillity changes.
2741 */
Romain Guy65b345f2011-07-27 18:51:50 -07002742 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002743 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002744 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002745 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 if (r != null) {
2747 if (!keepShown && r.stopped) {
2748 if (r.activity.mFinished) {
2749 // If we are finishing, we won't call onResume() in certain
2750 // cases. So here we likewise don't want to call onStop()
2751 // if the activity isn't resumed.
2752 return;
2753 }
2754 RuntimeException e = new RuntimeException(
2755 "Performing stop of activity that is not resumed: "
2756 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002757 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
2759
2760 if (info != null) {
2761 try {
2762 // First create a thumbnail for the activity...
Jim Miller0b2a6d02010-07-13 18:01:29 -07002763 info.thumbnail = createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 info.description = r.activity.onCreateDescription();
2765 } catch (Exception e) {
2766 if (!mInstrumentation.onException(r.activity, e)) {
2767 throw new RuntimeException(
2768 "Unable to save state of activity "
2769 + r.intent.getComponent().toShortString()
2770 + ": " + e.toString(), e);
2771 }
2772 }
2773 }
2774
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002775 // Next have the activity save its current state and managed dialogs...
2776 if (!r.activity.mFinished && saveState) {
2777 if (r.state == null) {
2778 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002779 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002780 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2781 r.state = state;
2782 } else {
2783 state = r.state;
2784 }
2785 }
2786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 if (!keepShown) {
2788 try {
2789 // Now we are idle.
2790 r.activity.performStop();
2791 } catch (Exception e) {
2792 if (!mInstrumentation.onException(r.activity, e)) {
2793 throw new RuntimeException(
2794 "Unable to stop activity "
2795 + r.intent.getComponent().toShortString()
2796 + ": " + e.toString(), e);
2797 }
2798 }
2799 r.stopped = true;
2800 }
2801
2802 r.paused = true;
2803 }
2804 }
2805
Romain Guy65b345f2011-07-27 18:51:50 -07002806 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002807 View v = r.activity.mDecor;
2808 if (v != null) {
2809 if (show) {
2810 if (!r.activity.mVisibleFromServer) {
2811 r.activity.mVisibleFromServer = true;
2812 mNumVisibleActivities++;
2813 if (r.activity.mVisibleFromClient) {
2814 r.activity.makeVisible();
2815 }
2816 }
2817 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002818 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002819 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 performConfigurationChanged(r.activity, r.newConfig);
2821 r.newConfig = null;
2822 }
2823 } else {
2824 if (r.activity.mVisibleFromServer) {
2825 r.activity.mVisibleFromServer = false;
2826 mNumVisibleActivities--;
2827 v.setVisibility(View.INVISIBLE);
2828 }
2829 }
2830 }
2831 }
2832
Romain Guy65b345f2011-07-27 18:51:50 -07002833 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002834 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 r.activity.mConfigChangeFlags |= configChanges;
2836
2837 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002838 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002840 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 TAG, "Finishing stop of " + r + ": show=" + show
2842 + " win=" + r.window);
2843
2844 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002845
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002846 // Make sure any pending writes are now committed.
2847 if (!r.isPreHoneycomb()) {
2848 QueuedWork.waitToFinish();
2849 }
2850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002851 // Tell activity manager we have been stopped.
2852 try {
2853 ActivityManagerNative.getDefault().activityStopped(
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002854 r.token, r.state, info.thumbnail, info.description);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 } catch (RemoteException ex) {
2856 }
2857 }
2858
2859 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002860 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 if (r.stopped) {
2862 r.activity.performRestart();
2863 r.stopped = false;
2864 }
2865 }
2866
Romain Guy65b345f2011-07-27 18:51:50 -07002867 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002868 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08002869
2870 if (r == null) {
2871 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
2872 return;
2873 }
2874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002876 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 } else if (show && r.stopped) {
2878 // If we are getting ready to gc after going to the background, well
2879 // we are back active so skip it.
2880 unscheduleGcIdler();
2881
2882 r.activity.performRestart();
2883 r.stopped = false;
2884 }
2885 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07002886 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 TAG, "Handle window " + r + " visibility: " + show);
2888 updateVisibility(r, show);
2889 }
2890 }
2891
Romain Guy65b345f2011-07-27 18:51:50 -07002892 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002893 ActivityClientRecord r = mActivities.get(token);
2894
2895 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002896 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002897 return;
2898 }
2899
2900 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08002901 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002902 try {
2903 // Now we are idle.
2904 r.activity.performStop();
2905 } catch (Exception e) {
2906 if (!mInstrumentation.onException(r.activity, e)) {
2907 throw new RuntimeException(
2908 "Unable to stop activity "
2909 + r.intent.getComponent().toShortString()
2910 + ": " + e.toString(), e);
2911 }
2912 }
2913 r.stopped = true;
2914 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002915
2916 // Make sure any pending writes are now committed.
2917 if (!r.isPreHoneycomb()) {
2918 QueuedWork.waitToFinish();
2919 }
2920
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08002921 // Tell activity manager we slept.
2922 try {
2923 ActivityManagerNative.getDefault().activitySlept(r.token);
2924 } catch (RemoteException ex) {
2925 }
2926 } else {
2927 if (r.stopped && r.activity.mVisibleFromServer) {
2928 r.activity.performRestart();
2929 r.stopped = false;
2930 }
2931 }
2932 }
2933
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002934 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08002935 synchronized (mPackages) {
2936 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08002937 }
2938 }
2939
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002940 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
2941 LoadedApk apk = peekPackageInfo(data.pkg, false);
2942 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002943 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002944 }
2945 apk = peekPackageInfo(data.pkg, true);
2946 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002947 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002948 }
2949 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002950 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002951 }
2952
Romain Guy65b345f2011-07-27 18:51:50 -07002953 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 final int N = results.size();
2955 for (int i=0; i<N; i++) {
2956 ResultInfo ri = results.get(i);
2957 try {
2958 if (ri.mData != null) {
2959 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
2960 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002961 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07002962 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 r.activity.dispatchActivityResult(ri.mResultWho,
2964 ri.mRequestCode, ri.mResultCode, ri.mData);
2965 } catch (Exception e) {
2966 if (!mInstrumentation.onException(r.activity, e)) {
2967 throw new RuntimeException(
2968 "Failure delivering result " + ri + " to activity "
2969 + r.intent.getComponent().toShortString()
2970 + ": " + e.toString(), e);
2971 }
2972 }
2973 }
2974 }
2975
Romain Guy65b345f2011-07-27 18:51:50 -07002976 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002977 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002978 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (r != null) {
2980 final boolean resumed = !r.paused;
2981 if (!r.activity.mFinished && r.activity.mDecor != null
2982 && r.hideForNow && resumed) {
2983 // We had hidden the activity because it started another
2984 // one... we have gotten a result back and we are not
2985 // paused, so make sure our window is visible.
2986 updateVisibility(r, true);
2987 }
2988 if (resumed) {
2989 try {
2990 // Now we are idle.
2991 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002992 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 mInstrumentation.callActivityOnPause(r.activity);
2994 if (!r.activity.mCalled) {
2995 throw new SuperNotCalledException(
2996 "Activity " + r.intent.getComponent().toShortString()
2997 + " did not call through to super.onPause()");
2998 }
2999 } catch (SuperNotCalledException e) {
3000 throw e;
3001 } catch (Exception e) {
3002 if (!mInstrumentation.onException(r.activity, e)) {
3003 throw new RuntimeException(
3004 "Unable to pause activity "
3005 + r.intent.getComponent().toShortString()
3006 + ": " + e.toString(), e);
3007 }
3008 }
3009 }
3010 deliverResults(r, res.results);
3011 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003012 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003013 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 }
3015 }
3016 }
3017
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003018 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019 return performDestroyActivity(token, finishing, 0, false);
3020 }
3021
Romain Guy65b345f2011-07-27 18:51:50 -07003022 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003024 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003025 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003026 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003028 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 r.activity.mConfigChangeFlags |= configChanges;
3030 if (finishing) {
3031 r.activity.mFinished = true;
3032 }
3033 if (!r.paused) {
3034 try {
3035 r.activity.mCalled = false;
3036 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003037 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 r.activity.getComponentName().getClassName());
3039 if (!r.activity.mCalled) {
3040 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003041 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 + " did not call through to super.onPause()");
3043 }
3044 } catch (SuperNotCalledException e) {
3045 throw e;
3046 } catch (Exception e) {
3047 if (!mInstrumentation.onException(r.activity, e)) {
3048 throw new RuntimeException(
3049 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003050 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051 + ": " + e.toString(), e);
3052 }
3053 }
3054 r.paused = true;
3055 }
3056 if (!r.stopped) {
3057 try {
3058 r.activity.performStop();
3059 } catch (SuperNotCalledException e) {
3060 throw e;
3061 } catch (Exception e) {
3062 if (!mInstrumentation.onException(r.activity, e)) {
3063 throw new RuntimeException(
3064 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003065 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 + ": " + e.toString(), e);
3067 }
3068 }
3069 r.stopped = true;
3070 }
3071 if (getNonConfigInstance) {
3072 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003073 r.lastNonConfigurationInstances
3074 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003075 } catch (Exception e) {
3076 if (!mInstrumentation.onException(r.activity, e)) {
3077 throw new RuntimeException(
3078 "Unable to retain activity "
3079 + r.intent.getComponent().toShortString()
3080 + ": " + e.toString(), e);
3081 }
3082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084 try {
3085 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003086 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 if (!r.activity.mCalled) {
3088 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003089 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 " did not call through to super.onDestroy()");
3091 }
3092 if (r.window != null) {
3093 r.window.closeAllPanels();
3094 }
3095 } catch (SuperNotCalledException e) {
3096 throw e;
3097 } catch (Exception e) {
3098 if (!mInstrumentation.onException(r.activity, e)) {
3099 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003100 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3101 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 }
3103 }
3104 }
3105 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003106 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 return r;
3108 }
3109
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003110 private static String safeToComponentShortString(Intent intent) {
3111 ComponentName component = intent.getComponent();
3112 return component == null ? "[Unknown]" : component.toShortString();
3113 }
3114
Romain Guy65b345f2011-07-27 18:51:50 -07003115 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003117 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 configChanges, getNonConfigInstance);
3119 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003120 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 WindowManager wm = r.activity.getWindowManager();
3122 View v = r.activity.mDecor;
3123 if (v != null) {
3124 if (r.activity.mVisibleFromServer) {
3125 mNumVisibleActivities--;
3126 }
3127 IBinder wtoken = v.getWindowToken();
3128 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003129 if (r.onlyLocalRequest) {
3130 // Hold off on removing this until the new activity's
3131 // window is being added.
3132 r.mPendingRemoveWindow = v;
3133 r.mPendingRemoveWindowManager = wm;
3134 } else {
3135 wm.removeViewImmediate(v);
3136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003138 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139 WindowManagerImpl.getDefault().closeAll(wtoken,
3140 r.activity.getClass().getName(), "Activity");
3141 }
3142 r.activity.mDecor = null;
3143 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003144 if (r.mPendingRemoveWindow == null) {
3145 // If we are delaying the removal of the activity window, then
3146 // we can't clean up all windows here. Note that we can't do
3147 // so later either, which means any windows that aren't closed
3148 // by the app will leak. Well we try to warning them a lot
3149 // about leaking windows, because that is a bug, so if they are
3150 // using this recreate facility then they get to live with leaks.
3151 WindowManagerImpl.getDefault().closeAll(token,
3152 r.activity.getClass().getName(), "Activity");
3153 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154
3155 // Mocked out contexts won't be participating in the normal
3156 // process lifecycle, but if we're running with a proper
3157 // ApplicationContext we need to have it tear down things
3158 // cleanly.
3159 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003160 if (c instanceof ContextImpl) {
3161 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 r.activity.getClass().getName(), "Activity");
3163 }
3164 }
3165 if (finishing) {
3166 try {
3167 ActivityManagerNative.getDefault().activityDestroyed(token);
3168 } catch (RemoteException ex) {
3169 // If the system process has died, it's game over for everyone.
3170 }
3171 }
3172 }
3173
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003174 public final void requestRelaunchActivity(IBinder token,
3175 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3176 int configChanges, boolean notResumed, Configuration config,
3177 boolean fromServer) {
3178 ActivityClientRecord target = null;
3179
3180 synchronized (mPackages) {
3181 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3182 ActivityClientRecord r = mRelaunchingActivities.get(i);
3183 if (r.token == token) {
3184 target = r;
3185 if (pendingResults != null) {
3186 if (r.pendingResults != null) {
3187 r.pendingResults.addAll(pendingResults);
3188 } else {
3189 r.pendingResults = pendingResults;
3190 }
3191 }
3192 if (pendingNewIntents != null) {
3193 if (r.pendingIntents != null) {
3194 r.pendingIntents.addAll(pendingNewIntents);
3195 } else {
3196 r.pendingIntents = pendingNewIntents;
3197 }
3198 }
3199 break;
3200 }
3201 }
3202
3203 if (target == null) {
3204 target = new ActivityClientRecord();
3205 target.token = token;
3206 target.pendingResults = pendingResults;
3207 target.pendingIntents = pendingNewIntents;
3208 if (!fromServer) {
3209 ActivityClientRecord existing = mActivities.get(token);
3210 if (existing != null) {
3211 target.startsNotResumed = existing.paused;
3212 }
3213 target.onlyLocalRequest = true;
3214 }
3215 mRelaunchingActivities.add(target);
3216 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3217 }
3218
3219 if (fromServer) {
3220 target.startsNotResumed = notResumed;
3221 target.onlyLocalRequest = false;
3222 }
3223 if (config != null) {
3224 target.createdConfig = config;
3225 }
3226 target.pendingConfigChanges |= configChanges;
3227 }
3228 }
3229
Romain Guy65b345f2011-07-27 18:51:50 -07003230 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 // If we are getting ready to gc after going to the background, well
3232 // we are back active so skip it.
3233 unscheduleGcIdler();
3234
3235 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003236 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 // First: make sure we have the most recent configuration and most
3239 // recent version of the activity, or skip it if some previous call
3240 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003241 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 int N = mRelaunchingActivities.size();
3243 IBinder token = tmp.token;
3244 tmp = null;
3245 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003246 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 if (r.token == token) {
3248 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003249 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 mRelaunchingActivities.remove(i);
3251 i--;
3252 N--;
3253 }
3254 }
Bob Leee5408332009-09-04 18:31:17 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003257 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 return;
3259 }
Bob Leee5408332009-09-04 18:31:17 -07003260
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003261 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3262 + tmp.token + " with configChanges=0x"
3263 + Integer.toHexString(configChanges));
3264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 if (mPendingConfiguration != null) {
3266 changedConfig = mPendingConfiguration;
3267 mPendingConfiguration = null;
3268 }
3269 }
Bob Leee5408332009-09-04 18:31:17 -07003270
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003271 if (tmp.createdConfig != null) {
3272 // If the activity manager is passing us its current config,
3273 // assume that is really what we want regardless of what we
3274 // may have pending.
3275 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003276 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3277 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3278 if (changedConfig == null
3279 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3280 changedConfig = tmp.createdConfig;
3281 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003282 }
3283 }
3284
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003285 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003286 + tmp.token + ": changedConfig=" + changedConfig);
3287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 // If there was a pending configuration change, execute it first.
3289 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003290 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 }
Bob Leee5408332009-09-04 18:31:17 -07003292
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003293 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003294 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 if (r == null) {
3296 return;
3297 }
Bob Leee5408332009-09-04 18:31:17 -07003298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003300 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003301 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003302
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003303 r.activity.mChangingConfigurations = true;
3304
Dianne Hackborne2b04802010-12-09 09:24:55 -08003305 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003307 performPauseActivity(r.token, false, r.isPreHoneycomb());
3308 }
3309 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3310 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003311 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003312 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
Bob Leee5408332009-09-04 18:31:17 -07003314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 r.activity = null;
3318 r.window = null;
3319 r.hideForNow = false;
3320 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003321 // Merge any pending results and pending intents; don't just replace them
3322 if (tmp.pendingResults != null) {
3323 if (r.pendingResults == null) {
3324 r.pendingResults = tmp.pendingResults;
3325 } else {
3326 r.pendingResults.addAll(tmp.pendingResults);
3327 }
3328 }
3329 if (tmp.pendingIntents != null) {
3330 if (r.pendingIntents == null) {
3331 r.pendingIntents = tmp.pendingIntents;
3332 } else {
3333 r.pendingIntents.addAll(tmp.pendingIntents);
3334 }
3335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003337
Christopher Tateb70f3df2009-04-07 16:07:59 -07003338 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 }
3340
Romain Guy65b345f2011-07-27 18:51:50 -07003341 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003342 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 Bitmap thumbnail = createThumbnailBitmap(r);
3344 CharSequence description = null;
3345 try {
3346 description = r.activity.onCreateDescription();
3347 } catch (Exception e) {
3348 if (!mInstrumentation.onException(r.activity, e)) {
3349 throw new RuntimeException(
3350 "Unable to create description of activity "
3351 + r.intent.getComponent().toShortString()
3352 + ": " + e.toString(), e);
3353 }
3354 }
3355 //System.out.println("Reporting top thumbnail " + thumbnail);
3356 try {
3357 ActivityManagerNative.getDefault().reportThumbnail(
3358 token, thumbnail, description);
3359 } catch (RemoteException ex) {
3360 }
3361 }
3362
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003363 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003365 ArrayList<ComponentCallbacks2> callbacks
3366 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 if (mActivities.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003369 Iterator<ActivityClientRecord> it = mActivities.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 while (it.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003371 ActivityClientRecord ar = it.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 Activity a = ar.activity;
3373 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003374 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3375 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 if (!ar.activity.mFinished && (allActivities ||
3377 (a != null && !ar.paused))) {
3378 // If the activity is currently resumed, its configuration
3379 // needs to change right now.
3380 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003381 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 // Otherwise, we will tell it about the change
3383 // the next time it is resumed or shown. Note that
3384 // the activity manager may, before then, decide the
3385 // activity needs to be destroyed to handle its new
3386 // configuration.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003387 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Setting activity "
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003388 + ar.activityInfo.name + " newConfig=" + thisConfig);
3389 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
3391 }
3392 }
3393 }
3394 if (mServices.size() > 0) {
3395 Iterator<Service> it = mServices.values().iterator();
3396 while (it.hasNext()) {
3397 callbacks.add(it.next());
3398 }
3399 }
3400 synchronized (mProviderMap) {
3401 if (mLocalProviders.size() > 0) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003402 Iterator<ProviderClientRecord> it = mLocalProviders.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 while (it.hasNext()) {
3404 callbacks.add(it.next().mLocalProvider);
3405 }
3406 }
3407 }
3408 final int N = mAllApplications.size();
3409 for (int i=0; i<N; i++) {
3410 callbacks.add(mAllApplications.get(i));
3411 }
Bob Leee5408332009-09-04 18:31:17 -07003412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 return callbacks;
3414 }
Bob Leee5408332009-09-04 18:31:17 -07003415
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003416 private final void performConfigurationChanged(
3417 ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003419 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 // we check the runtime type and act accordingly.
3421 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3422 if (activity != null) {
3423 activity.mCalled = false;
3424 }
Bob Leee5408332009-09-04 18:31:17 -07003425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 boolean shouldChangeConfig = false;
3427 if ((activity == null) || (activity.mCurrentConfig == null)) {
3428 shouldChangeConfig = true;
3429 } else {
Bob Leee5408332009-09-04 18:31:17 -07003430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 // If the new config is the same as the config this Activity
3432 // is already running with then don't bother calling
3433 // onConfigurationChanged
3434 int diff = activity.mCurrentConfig.diff(config);
3435 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 // If this activity doesn't handle any of the config changes
3437 // then don't bother calling onConfigurationChanged as we're
3438 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003439 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 shouldChangeConfig = true;
3441 }
3442 }
3443 }
Bob Leee5408332009-09-04 18:31:17 -07003444
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003445 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003446 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 if (shouldChangeConfig) {
3448 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 if (activity != null) {
3451 if (!activity.mCalled) {
3452 throw new SuperNotCalledException(
3453 "Activity " + activity.getLocalClassName() +
3454 " did not call through to super.onConfigurationChanged()");
3455 }
3456 activity.mConfigChangeFlags = 0;
3457 activity.mCurrentConfig = new Configuration(config);
3458 }
3459 }
3460 }
3461
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003462 public final void applyConfigurationToResources(Configuration config) {
3463 synchronized (mPackages) {
3464 applyConfigurationToResourcesLocked(config, null);
3465 }
3466 }
3467
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003468 final boolean applyConfigurationToResourcesLocked(Configuration config,
3469 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003470 if (mResConfiguration == null) {
3471 mResConfiguration = new Configuration();
3472 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003473 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003474 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003475 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003476 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003477 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003478 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003479 DisplayMetrics dm = getDisplayMetricsLocked(compat, true);
3480
3481 if (compat != null && (mResCompatibilityInfo == null ||
3482 !mResCompatibilityInfo.equals(compat))) {
3483 mResCompatibilityInfo = compat;
3484 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3485 | ActivityInfo.CONFIG_SCREEN_SIZE
3486 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3487 }
Bob Leee5408332009-09-04 18:31:17 -07003488
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003489 // set it for java, this also affects newly created Resources
3490 if (config.locale != null) {
3491 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 }
Bob Leee5408332009-09-04 18:31:17 -07003493
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003494 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003495
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003496 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003497 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003498
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003499 Iterator<WeakReference<Resources>> it =
3500 mActiveResources.values().iterator();
3501 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3502 // mActiveResources.entrySet().iterator();
3503 while (it.hasNext()) {
3504 WeakReference<Resources> v = it.next();
3505 Resources r = v.get();
3506 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003507 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003508 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003509 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003510 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003511 // + " " + r + ": " + r.getConfiguration());
3512 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003513 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003514 it.remove();
3515 }
3516 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003517
3518 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003519 }
3520
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003521 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003522
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003523 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003524
3525 synchronized (mPackages) {
3526 if (mPendingConfiguration != null) {
3527 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3528 config = mPendingConfiguration;
3529 }
3530 mPendingConfiguration = null;
3531 }
3532
3533 if (config == null) {
3534 return;
3535 }
3536
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003537 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003538 + config);
3539
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003540 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 if (mConfiguration == null) {
3543 mConfiguration = new Configuration();
3544 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003545 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003546 return;
3547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 mConfiguration.updateFrom(config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003549 if (mCompatConfiguration == null) {
3550 mCompatConfiguration = new Configuration();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003551 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003552 mCompatConfiguration.setTo(mConfiguration);
3553 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3554 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3555 config = mCompatConfiguration;
3556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 callbacks = collectComponentCallbacksLocked(false, config);
3558 }
Romain Guy65b345f2011-07-27 18:51:50 -07003559
3560 // Cleanup hardware accelerated stuff
3561 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003562
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003563 if (callbacks != null) {
3564 final int N = callbacks.size();
3565 for (int i=0; i<N; i++) {
3566 performConfigurationChanged(callbacks.get(i), config);
3567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
3569 }
3570
3571 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003572 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 if (r == null || r.activity == null) {
3574 return;
3575 }
Bob Leee5408332009-09-04 18:31:17 -07003576
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003577 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003578 + r.activityInfo.name);
3579
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003580 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
3582
Romain Guy7eabe552011-07-21 14:56:34 -07003583 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003584 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003585 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003586 switch (profileType) {
3587 case 1:
3588 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3589 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003590 default:
3591 mProfiler.setProfiler(pcd.path, pcd.fd);
3592 mProfiler.autoStopProfiler = false;
3593 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003594 break;
3595 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003596 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003597 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003598 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003599 } finally {
3600 try {
3601 pcd.fd.close();
3602 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003603 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003604 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003605 }
3606 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003607 switch (profileType) {
3608 case 1:
3609 ViewDebug.stopLooperProfiling();
3610 break;
3611 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003612 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003613 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003614 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003615 }
3616 }
Bob Leee5408332009-09-04 18:31:17 -07003617
Andy McFadden824c5102010-07-09 16:26:57 -07003618 final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
3619 if (managed) {
3620 try {
3621 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3622 } catch (IOException e) {
3623 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3624 + " -- can the process access this path?");
3625 } finally {
3626 try {
3627 dhd.fd.close();
3628 } catch (IOException e) {
3629 Slog.w(TAG, "Failure closing profile fd", e);
3630 }
3631 }
3632 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003633 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003634 }
3635 }
3636
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003637 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3638 boolean hasPkgInfo = false;
3639 if (packages != null) {
3640 for (int i=packages.length-1; i>=0; i--) {
3641 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3642 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003643 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003644 ref = mPackages.get(packages[i]);
3645 if (ref != null && ref.get() != null) {
3646 hasPkgInfo = true;
3647 } else {
3648 ref = mResourcePackages.get(packages[i]);
3649 if (ref != null && ref.get() != null) {
3650 hasPkgInfo = true;
3651 }
3652 }
3653 }
3654 mPackages.remove(packages[i]);
3655 mResourcePackages.remove(packages[i]);
3656 }
3657 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003658 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003659 hasPkgInfo);
3660 }
3661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003663 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003665 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 callbacks = collectComponentCallbacksLocked(true, null);
3667 }
Bob Leee5408332009-09-04 18:31:17 -07003668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 final int N = callbacks.size();
3670 for (int i=0; i<N; i++) {
3671 callbacks.get(i).onLowMemory();
3672 }
3673
Chris Tatece229052009-03-25 16:44:52 -07003674 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3675 if (Process.myUid() != Process.SYSTEM_UID) {
3676 int sqliteReleased = SQLiteDatabase.releaseMemory();
3677 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3678 }
Bob Leee5408332009-09-04 18:31:17 -07003679
Mike Reedcaf0df12009-04-27 14:32:05 -04003680 // Ask graphics to free up as much as possible (font/image caches)
3681 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682
3683 BinderInternal.forceGc("mem");
3684 }
3685
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003686 final void handleTrimMemory(int level) {
Romain Guybdf76092011-07-18 15:00:43 -07003687 WindowManagerImpl.getDefault().trimMemory(level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003688 ArrayList<ComponentCallbacks2> callbacks;
3689
3690 synchronized (mPackages) {
3691 callbacks = collectComponentCallbacksLocked(true, null);
3692 }
3693
3694 final int N = callbacks.size();
3695 for (int i=0; i<N; i++) {
3696 callbacks.get(i).onTrimMemory(level);
3697 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003698 }
3699
Romain Guy65b345f2011-07-27 18:51:50 -07003700 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 mBoundApplication = data;
3702 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003703 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003705 mProfiler = new Profiler();
3706 mProfiler.profileFile = data.initProfileFile;
3707 mProfiler.profileFd = data.initProfileFd;
3708 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3709
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003711 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 android.ddm.DdmHandleAppName.setAppName(data.processName);
3713
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003714 if (data.persistent) {
3715 // Persistent processes on low-memory devices do not get to
3716 // use hardware accelerated drawing, since this can add too much
3717 // overhead to the process.
3718 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3719 if (!ActivityManager.isHighEndGfx(display)) {
3720 HardwareRenderer.disable(false);
3721 }
3722 }
3723
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003724 if (mProfiler.profileFd != null) {
3725 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003726 }
3727
Joe Onoratod630f102011-03-17 18:42:26 -07003728 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3729 // implementation to use the pool executor. Normally, we use the
3730 // serialized executor as the default. This has to happen in the
3731 // main thread so the main looper is set right.
3732 if (data.appInfo.targetSdkVersion <= 12) {
3733 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3734 }
3735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003736 /*
3737 * Before spawning a new process, reset the time zone to be the system time zone.
3738 * This needs to be done because the system time zone could have changed after the
3739 * the spawning of this process. Without doing this this process would have the incorrect
3740 * system time zone.
3741 */
3742 TimeZone.setDefault(null);
3743
3744 /*
3745 * Initialize the default locale in this process for the reasons we set the time zone.
3746 */
3747 Locale.setDefault(data.config.locale);
3748
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003749 /*
3750 * Update the system configuration since its preloaded and might not
3751 * reflect configuration changes. The configuration object passed
3752 * in AppBindData can be safely assumed to be up to date
3753 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003754 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003755
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003756 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003758 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003759 * For system applications on userdebug/eng builds, log stack
3760 * traces of disk and network access to dropbox for analysis.
3761 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003762 if ((data.appInfo.flags &
3763 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003764 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3765 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003766 }
3767
3768 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003769 * For apps targetting SDK Honeycomb or later, we don't allow
3770 * network usage on the main event loop / UI thread.
3771 *
3772 * Note to those grepping: this is what ultimately throws
3773 * NetworkOnMainThreadException ...
3774 */
3775 if (data.appInfo.targetSdkVersion > 9) {
3776 StrictMode.enableDeathOnNetwork();
3777 }
3778
3779 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003780 * Switch this process to density compatibility mode if needed.
3781 */
3782 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3783 == 0) {
3784 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3785 }
Bob Leee5408332009-09-04 18:31:17 -07003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3788 // XXX should have option to change the port.
3789 Debug.changeDebugPort(8100);
3790 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003791 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 + " is waiting for the debugger on port 8100...");
3793
3794 IActivityManager mgr = ActivityManagerNative.getDefault();
3795 try {
3796 mgr.showWaitingForDebugger(mAppThread, true);
3797 } catch (RemoteException ex) {
3798 }
3799
3800 Debug.waitForDebugger();
3801
3802 try {
3803 mgr.showWaitingForDebugger(mAppThread, false);
3804 } catch (RemoteException ex) {
3805 }
3806
3807 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003808 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 + " can be debugged on port 8100...");
3810 }
3811 }
3812
Robert Greenwalt434203a2010-10-11 16:00:27 -07003813 /**
3814 * Initialize the default http proxy in this process for the reasons we set the time zone.
3815 */
3816 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3817 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
3818 try {
3819 ProxyProperties proxyProperties = service.getProxy();
3820 Proxy.setHttpProxySystemProperty(proxyProperties);
3821 } catch (RemoteException e) {}
3822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 if (data.instrumentationName != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08003824 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 appContext.init(data.info, null, this);
3826 InstrumentationInfo ii = null;
3827 try {
3828 ii = appContext.getPackageManager().
3829 getInstrumentationInfo(data.instrumentationName, 0);
3830 } catch (PackageManager.NameNotFoundException e) {
3831 }
3832 if (ii == null) {
3833 throw new RuntimeException(
3834 "Unable to find instrumentation info for: "
3835 + data.instrumentationName);
3836 }
3837
3838 mInstrumentationAppDir = ii.sourceDir;
3839 mInstrumentationAppPackage = ii.packageName;
3840 mInstrumentedAppDir = data.info.getAppDir();
3841
3842 ApplicationInfo instrApp = new ApplicationInfo();
3843 instrApp.packageName = ii.packageName;
3844 instrApp.sourceDir = ii.sourceDir;
3845 instrApp.publicSourceDir = ii.publicSourceDir;
3846 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07003847 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003848 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003849 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08003850 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 instrContext.init(pi, null, this);
3852
3853 try {
3854 java.lang.ClassLoader cl = instrContext.getClassLoader();
3855 mInstrumentation = (Instrumentation)
3856 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
3857 } catch (Exception e) {
3858 throw new RuntimeException(
3859 "Unable to instantiate instrumentation "
3860 + data.instrumentationName + ": " + e.toString(), e);
3861 }
3862
3863 mInstrumentation.init(this, instrContext, appContext,
3864 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
3865
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003866 if (mProfiler.profileFile != null && !ii.handleProfiling
3867 && mProfiler.profileFd == null) {
3868 mProfiler.handlingProfiling = true;
3869 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 file.getParentFile().mkdirs();
3871 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
3872 }
3873
3874 try {
3875 mInstrumentation.onCreate(data.instrumentationArgs);
3876 }
3877 catch (Exception e) {
3878 throw new RuntimeException(
3879 "Exception thrown in onCreate() of "
3880 + data.instrumentationName + ": " + e.toString(), e);
3881 }
3882
3883 } else {
3884 mInstrumentation = new Instrumentation();
3885 }
3886
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003887 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08003888 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08003889 }
3890
Christopher Tate181fafa2009-05-14 11:12:14 -07003891 // If the app is being launched for full backup or restore, bring it up in
3892 // a restricted environment with the base application class.
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003893 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 mInitialApplication = app;
3895
Christopher Tate75a99702011-05-18 16:28:19 -07003896 // don't bring up providers in restricted mode; they may depend on the
3897 // app's custom Application class
3898 if (!data.restrictedBackupMode){
3899 List<ProviderInfo> providers = data.providers;
3900 if (providers != null) {
3901 installContentProviders(app, providers);
3902 // For process that contains content providers, we want to
3903 // ensure that the JIT is enabled "at some point".
3904 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
3905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003906 }
3907
3908 try {
3909 mInstrumentation.callApplicationOnCreate(app);
3910 } catch (Exception e) {
3911 if (!mInstrumentation.onException(app, e)) {
3912 throw new RuntimeException(
3913 "Unable to create application " + app.getClass().getName()
3914 + ": " + e.toString(), e);
3915 }
3916 }
3917 }
3918
3919 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
3920 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003921 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
3922 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 Debug.stopMethodTracing();
3924 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003925 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 // + ", app thr: " + mAppThread);
3927 try {
3928 am.finishInstrumentation(mAppThread, resultCode, results);
3929 } catch (RemoteException ex) {
3930 }
3931 }
3932
Romain Guy65b345f2011-07-27 18:51:50 -07003933 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 Context context, List<ProviderInfo> providers) {
3935 final ArrayList<IActivityManager.ContentProviderHolder> results =
3936 new ArrayList<IActivityManager.ContentProviderHolder>();
3937
3938 Iterator<ProviderInfo> i = providers.iterator();
3939 while (i.hasNext()) {
3940 ProviderInfo cpi = i.next();
3941 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07003942 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 buf.append(cpi.authority);
3944 buf.append(": ");
3945 buf.append(cpi.name);
3946 Log.i(TAG, buf.toString());
3947 IContentProvider cp = installProvider(context, null, cpi, false);
3948 if (cp != null) {
3949 IActivityManager.ContentProviderHolder cph =
3950 new IActivityManager.ContentProviderHolder(cpi);
3951 cph.provider = cp;
3952 results.add(cph);
3953 // Don't ever unload this provider from the process.
3954 synchronized(mProviderMap) {
3955 mProviderRefCountMap.put(cp.asBinder(), new ProviderRefCount(10000));
3956 }
3957 }
3958 }
3959
3960 try {
3961 ActivityManagerNative.getDefault().publishContentProviders(
3962 getApplicationThread(), results);
3963 } catch (RemoteException ex) {
3964 }
3965 }
3966
Romain Guy65b345f2011-07-27 18:51:50 -07003967 private IContentProvider getExistingProvider(Context context, String name) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003969 final ProviderClientRecord pr = mProviderMap.get(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 if (pr != null) {
3971 return pr.mProvider;
3972 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003973 return null;
3974 }
3975 }
3976
Romain Guy65b345f2011-07-27 18:51:50 -07003977 private IContentProvider getProvider(Context context, String name) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07003978 IContentProvider existing = getExistingProvider(context, name);
3979 if (existing != null) {
3980 return existing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 }
3982
3983 IActivityManager.ContentProviderHolder holder = null;
3984 try {
3985 holder = ActivityManagerNative.getDefault().getContentProvider(
3986 getApplicationThread(), name);
3987 } catch (RemoteException ex) {
3988 }
3989 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003990 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 return null;
3992 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993
3994 IContentProvider prov = installProvider(context, holder.provider,
3995 holder.info, true);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003996 //Slog.i(TAG, "noReleaseNeeded=" + holder.noReleaseNeeded);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 if (holder.noReleaseNeeded || holder.provider == null) {
3998 // We are not going to release the provider if it is an external
3999 // provider that doesn't care about being released, or if it is
4000 // a local provider running in this process.
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004001 //Slog.i(TAG, "*** NO RELEASE NEEDED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 synchronized(mProviderMap) {
4003 mProviderRefCountMap.put(prov.asBinder(), new ProviderRefCount(10000));
4004 }
4005 }
4006 return prov;
4007 }
4008
4009 public final IContentProvider acquireProvider(Context c, String name) {
4010 IContentProvider provider = getProvider(c, name);
4011 if(provider == null)
4012 return null;
4013 IBinder jBinder = provider.asBinder();
4014 synchronized(mProviderMap) {
4015 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4016 if(prc == null) {
4017 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4018 } else {
4019 prc.count++;
4020 } //end else
4021 } //end synchronized
4022 return provider;
4023 }
4024
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004025 public final IContentProvider acquireExistingProvider(Context c, String name) {
4026 IContentProvider provider = getExistingProvider(c, name);
4027 if(provider == null)
4028 return null;
4029 IBinder jBinder = provider.asBinder();
4030 synchronized(mProviderMap) {
4031 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4032 if(prc == null) {
4033 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4034 } else {
4035 prc.count++;
4036 } //end else
4037 } //end synchronized
4038 return provider;
4039 }
4040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 public final boolean releaseProvider(IContentProvider provider) {
4042 if(provider == null) {
4043 return false;
4044 }
4045 IBinder jBinder = provider.asBinder();
4046 synchronized(mProviderMap) {
4047 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4048 if(prc == null) {
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004049 if(localLOGV) Slog.v(TAG, "releaseProvider::Weird shouldn't be here");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 return false;
4051 } else {
4052 prc.count--;
4053 if(prc.count == 0) {
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004054 // Schedule the actual remove asynchronously, since we
4055 // don't know the context this will be called in.
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004056 // TODO: it would be nice to post a delayed message, so
4057 // if we come back and need the same provider quickly
4058 // we will still have it available.
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004059 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4060 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 } //end if
4062 } //end else
4063 } //end synchronized
4064 return true;
4065 }
4066
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004067 final void completeRemoveProvider(IContentProvider provider) {
4068 IBinder jBinder = provider.asBinder();
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004069 String name = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004070 synchronized(mProviderMap) {
4071 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4072 if(prc != null && prc.count == 0) {
4073 mProviderRefCountMap.remove(jBinder);
4074 //invoke removeProvider to dereference provider
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004075 name = removeProviderLocked(provider);
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004076 }
4077 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004078
4079 if (name != null) {
4080 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004081 if(localLOGV) Slog.v(TAG, "removeProvider::Invoking " +
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004082 "ActivityManagerNative.removeContentProvider(" + name);
4083 ActivityManagerNative.getDefault().removeContentProvider(
4084 getApplicationThread(), name);
4085 } catch (RemoteException e) {
4086 //do nothing content provider object is dead any way
4087 } //end catch
4088 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004089 }
4090
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004091 public final String removeProviderLocked(IContentProvider provider) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 if (provider == null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004093 return null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
4095 IBinder providerBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004097 String name = null;
4098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 // remove the provider from mProviderMap
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004100 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 while (iter.hasNext()) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004102 ProviderClientRecord pr = iter.next();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 IBinder myBinder = pr.mProvider.asBinder();
4104 if (myBinder == providerBinder) {
4105 //find if its published by this process itself
4106 if(pr.mLocalProvider != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004107 if(localLOGV) Slog.i(TAG, "removeProvider::found local provider returning");
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004108 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004110 if(localLOGV) Slog.v(TAG, "removeProvider::Not local provider Unlinking " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 "death recipient");
4112 //content provider is in another process
4113 myBinder.unlinkToDeath(pr, 0);
4114 iter.remove();
4115 //invoke remove only once for the very first name seen
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004116 if(name == null) {
4117 name = pr.mName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 }
4119 } //end if myBinder
4120 } //end while iter
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004121
4122 return name;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 }
4124
4125 final void removeDeadProvider(String name, IContentProvider provider) {
4126 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004127 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004128 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004129 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004130 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004131 if (removed != null) {
4132 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 }
4135 }
4136 }
4137
Romain Guy65b345f2011-07-27 18:51:50 -07004138 private IContentProvider installProvider(Context context,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 IContentProvider provider, ProviderInfo info, boolean noisy) {
4140 ContentProvider localProvider = null;
4141 if (provider == null) {
4142 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004143 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 + info.name);
4145 }
4146 Context c = null;
4147 ApplicationInfo ai = info.applicationInfo;
4148 if (context.getPackageName().equals(ai.packageName)) {
4149 c = context;
4150 } else if (mInitialApplication != null &&
4151 mInitialApplication.getPackageName().equals(ai.packageName)) {
4152 c = mInitialApplication;
4153 } else {
4154 try {
4155 c = context.createPackageContext(ai.packageName,
4156 Context.CONTEXT_INCLUDE_CODE);
4157 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004158 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 }
4160 }
4161 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004162 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 ai.packageName +
4164 " while loading content provider " +
4165 info.name);
4166 return null;
4167 }
4168 try {
4169 final java.lang.ClassLoader cl = c.getClassLoader();
4170 localProvider = (ContentProvider)cl.
4171 loadClass(info.name).newInstance();
4172 provider = localProvider.getIContentProvider();
4173 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004174 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 info.name + " from sourceDir " +
4176 info.applicationInfo.sourceDir);
4177 return null;
4178 }
Joe Onorato43a17652011-04-06 19:22:23 -07004179 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 TAG, "Instantiating local provider " + info.name);
4181 // XXX Need to create the correct context for this provider.
4182 localProvider.attachInfo(c, info);
4183 } catch (java.lang.Exception e) {
4184 if (!mInstrumentation.onException(null, e)) {
4185 throw new RuntimeException(
4186 "Unable to get provider " + info.name
4187 + ": " + e.toString(), e);
4188 }
4189 return null;
4190 }
4191 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004192 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 + info.name);
4194 }
4195
4196 synchronized (mProviderMap) {
4197 // Cache the pointer for the remote provider.
4198 String names[] = PATTERN_SEMICOLON.split(info.authority);
4199 for (int i=0; i<names.length; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004200 ProviderClientRecord pr = new ProviderClientRecord(names[i], provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 localProvider);
4202 try {
4203 provider.asBinder().linkToDeath(pr, 0);
4204 mProviderMap.put(names[i], pr);
4205 } catch (RemoteException e) {
4206 return null;
4207 }
4208 }
4209 if (localProvider != null) {
4210 mLocalProviders.put(provider.asBinder(),
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004211 new ProviderClientRecord(null, provider, localProvider));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 }
4213 }
4214
4215 return provider;
4216 }
4217
Romain Guy65b345f2011-07-27 18:51:50 -07004218 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219 sThreadLocal.set(this);
4220 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004222 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004223 public void run() {
4224 ensureJitEnabled();
4225 }
4226 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4228 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4229 IActivityManager mgr = ActivityManagerNative.getDefault();
4230 try {
4231 mgr.attachApplication(mAppThread);
4232 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004233 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 }
4235 } else {
4236 // Don't set application object here -- if the system crashes,
4237 // we can't display an alert, we just want to die die die.
4238 android.ddm.DdmHandleAppName.setAppName("system_process");
4239 try {
4240 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004241 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004242 context.init(getSystemContext().mPackageInfo, null, this);
4243 Application app = Instrumentation.newApplication(Application.class, context);
4244 mAllApplications.add(app);
4245 mInitialApplication = app;
4246 app.onCreate();
4247 } catch (Exception e) {
4248 throw new RuntimeException(
4249 "Unable to instantiate Application():" + e.toString(), e);
4250 }
4251 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004252
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004253 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004254 public void onConfigurationChanged(Configuration newConfig) {
4255 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004256 // We need to apply this change to the resources
4257 // immediately, because upon returning the view
4258 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004259 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004260 // This actually changed the resources! Tell
4261 // everyone about it.
4262 if (mPendingConfiguration == null ||
4263 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4264 mPendingConfiguration = newConfig;
4265
4266 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4267 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004268 }
4269 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004270 }
4271 public void onLowMemory() {
4272 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004273 public void onTrimMemory(int level) {
4274 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004275 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004276 }
4277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 public static final ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004279 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 ActivityThread thread = new ActivityThread();
4281 thread.attach(true);
4282 return thread;
4283 }
4284
Jeff Brown10e89712011-07-08 18:52:57 -07004285 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004287 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 }
4289 }
4290
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004291 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004292 synchronized (mPackages) {
4293 if (mCoreSettings != null) {
4294 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004295 } else {
4296 return defaultValue;
4297 }
4298 }
4299 }
4300
Romain Guy65b345f2011-07-27 18:51:50 -07004301 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004302 SamplingProfilerIntegration.start();
4303
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004304 // CloseGuard defaults to true and can be quite spammy. We
4305 // disable it here, but selectively enable it later (via
4306 // StrictMode) on debug builds, but using DropBox, not logs.
4307 CloseGuard.setEnabled(false);
4308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 Process.setArgV0("<pre-initialized>");
4310
4311 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004312 if (sMainThreadHandler == null) {
4313 sMainThreadHandler = new Handler();
4314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315
4316 ActivityThread thread = new ActivityThread();
4317 thread.attach(false);
4318
Dianne Hackborn287952c2010-09-22 22:34:31 -07004319 if (false) {
4320 Looper.myLooper().setMessageLogging(new
4321 LogPrinter(Log.DEBUG, "ActivityThread"));
4322 }
4323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 Looper.loop();
4325
Jeff Brown10e89712011-07-08 18:52:57 -07004326 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 }
4328}