blob: 4d4b19b12b4ecb8609909deeb7d7a22caec674de [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;
Romain Guya9582652011-11-10 14:20:10 -080048import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070049import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070050import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Bundle;
52import android.os.Debug;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.Looper;
56import android.os.Message;
57import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070058import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
60import android.os.RemoteException;
61import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070062import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070064import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070065import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.DisplayMetrics;
68import android.util.EventLog;
69import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070070import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080071import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080072import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070073import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070075import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.View;
77import android.view.ViewDebug;
78import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070079import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.view.Window;
81import android.view.WindowManager;
82import android.view.WindowManagerImpl;
Jason Samsa6f338c2012-02-24 16:22:16 -080083import android.renderscript.RenderScript;
Kenny Roote29df162012-08-10 08:28:37 -070084import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86import com.android.internal.os.BinderInternal;
87import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070088import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
91
92import java.io.File;
93import java.io.FileDescriptor;
94import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070095import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import java.io.PrintWriter;
97import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070098import java.net.InetAddress;
Kenny Roote29df162012-08-10 08:28:37 -070099import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.util.ArrayList;
101import java.util.HashMap;
102import java.util.Iterator;
103import java.util.List;
104import java.util.Locale;
105import java.util.Map;
106import java.util.TimeZone;
107import java.util.regex.Pattern;
108
Jeff Sharkeye861b422012-03-01 20:59:22 -0800109import libcore.io.IoUtils;
110
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800111import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113final class SuperNotCalledException extends AndroidRuntimeException {
114 public SuperNotCalledException(String msg) {
115 super(msg);
116 }
117}
118
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700119final class RemoteServiceException extends AndroidRuntimeException {
120 public RemoteServiceException(String msg) {
121 super(msg);
122 }
123}
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125/**
126 * This manages the execution of the main thread in an
127 * application process, scheduling and executing activities,
128 * broadcasts, and other operations on it as the activity
129 * manager requests.
130 *
131 * {@hide}
132 */
133public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700134 /** @hide */
135 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700136 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700137 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700138 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700139 /** @hide */
140 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700141 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700142 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700143 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800144 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700145 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700146 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
148 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
149 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
150 private static final int LOG_ON_PAUSE_CALLED = 30021;
151 private static final int LOG_ON_RESUME_CALLED = 30022;
152
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700153 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700154
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700155 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700157 final ApplicationThread mAppThread = new ApplicationThread();
158 final Looper mLooper = Looper.myLooper();
159 final H mH = new H();
160 final HashMap<IBinder, ActivityClientRecord> mActivities
161 = new HashMap<IBinder, ActivityClientRecord>();
162 // List of new activities (via ActivityRecord.nextIdle) that should
163 // be reported when next we idle.
164 ActivityClientRecord mNewActivities = null;
165 // Number of activities that are currently visible on-screen.
166 int mNumVisibleActivities = 0;
167 final HashMap<IBinder, Service> mServices
168 = new HashMap<IBinder, Service>();
169 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700170 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700171 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700172 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700173 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700174 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700176 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700177 Application mInitialApplication;
178 final ArrayList<Application> mAllApplications
179 = new ArrayList<Application>();
180 // set of instantiated backup agents, keyed by package name
181 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700182 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700183 Instrumentation mInstrumentation;
184 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700185 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700186 String mInstrumentationAppPackage = null;
187 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700188 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700189 boolean mSystemThread = false;
190 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700192 // These can be accessed by multiple threads; mPackages is the lock.
193 // XXX For now we keep around information about all packages we have
194 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800195 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700196 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800197 // which means this lock gets held while the activity and window managers
198 // holds their own lock. Thus you MUST NEVER call back into the activity manager
199 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 final HashMap<String, WeakReference<LoadedApk>> mPackages
201 = new HashMap<String, WeakReference<LoadedApk>>();
202 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
203 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700204 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
205 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700206 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
207 = new HashMap<ResourcesKey, WeakReference<Resources> >();
208 final ArrayList<ActivityClientRecord> mRelaunchingActivities
209 = new ArrayList<ActivityClientRecord>();
210 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700212 // The lock of mProviderMap protects the following variables.
213 final HashMap<String, ProviderClientRecord> mProviderMap
214 = new HashMap<String, ProviderClientRecord>();
215 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
216 = new HashMap<IBinder, ProviderRefCount>();
217 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
218 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700219 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
220 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
Jeff Hamilton52d32032011-01-08 15:31:26 -0600222 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
223 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
224
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700225 final GcIdler mGcIdler = new GcIdler();
226 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700228 static Handler sMainThreadHandler; // set once in main()
229
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800230 Bundle mCoreSettings = null;
231
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400232 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700234 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 Intent intent;
236 Bundle state;
237 Activity activity;
238 Window window;
239 Activity parent;
240 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700241 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 boolean paused;
243 boolean stopped;
244 boolean hideForNow;
245 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700246 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700249 String profileFile;
250 ParcelFileDescriptor profileFd;
251 boolean autoStopProfiler;
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400254 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700255 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
257 List<ResultInfo> pendingResults;
258 List<Intent> pendingIntents;
259
260 boolean startsNotResumed;
261 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800262 int pendingConfigChanges;
263 boolean onlyLocalRequest;
264
265 View mPendingRemoveWindow;
266 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700268 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 parent = null;
270 embeddedID = null;
271 paused = false;
272 stopped = false;
273 hideForNow = false;
274 nextIdle = null;
275 }
276
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800277 public boolean isPreHoneycomb() {
278 if (activity != null) {
279 return activity.getApplicationInfo().targetSdkVersion
280 < android.os.Build.VERSION_CODES.HONEYCOMB;
281 }
282 return false;
283 }
284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700286 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 return "ActivityRecord{"
288 + Integer.toHexString(System.identityHashCode(this))
289 + " token=" + token + " " + (componentName == null
290 ? "no component name" : componentName.toShortString())
291 + "}";
292 }
293 }
294
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700295 final class ProviderClientRecord {
296 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 final IContentProvider mProvider;
298 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700299 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700301 ProviderClientRecord(String[] names, IContentProvider provider,
302 ContentProvider localProvider,
303 IActivityManager.ContentProviderHolder holder) {
304 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 mProvider = provider;
306 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700307 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 }
309 }
310
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400311 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 List<Intent> intents;
313 IBinder token;
314 public String toString() {
315 return "NewIntentData{intents=" + intents + " token=" + token + "}";
316 }
317 }
318
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400319 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700320 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
321 boolean ordered, boolean sticky, IBinder token) {
322 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
323 this.intent = intent;
324 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 Intent intent;
327 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400328 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 public String toString() {
330 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700331 info.packageName + " resultCode=" + getResultCode()
332 + " resultData=" + getResultData() + " resultExtras="
333 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 }
335 }
336
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400337 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700338 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400339 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700340 int backupMode;
341 public String toString() {
342 return "CreateBackupAgentData{appInfo=" + appInfo
343 + " backupAgent=" + appInfo.backupAgentName
344 + " mode=" + backupMode + "}";
345 }
346 }
Bob Leee5408332009-09-04 18:31:17 -0700347
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400348 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 IBinder token;
350 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400351 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 Intent intent;
353 public String toString() {
354 return "CreateServiceData{token=" + token + " className="
355 + info.name + " packageName=" + info.packageName
356 + " intent=" + intent + "}";
357 }
358 }
359
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400360 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 IBinder token;
362 Intent intent;
363 boolean rebind;
364 public String toString() {
365 return "BindServiceData{token=" + token + " intent=" + intent + "}";
366 }
367 }
368
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400369 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700371 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700373 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 Intent args;
375 public String toString() {
376 return "ServiceArgsData{token=" + token + " startId=" + startId
377 + " args=" + args + "}";
378 }
379 }
380
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400381 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700382 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 String processName;
384 ApplicationInfo appInfo;
385 List<ProviderInfo> providers;
386 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 Bundle instrumentationArgs;
388 IInstrumentationWatcher instrumentationWatcher;
389 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800390 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700391 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700392 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400394 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700395
396 /** Initial values for {@link Profiler}. */
397 String initProfileFile;
398 ParcelFileDescriptor initProfileFd;
399 boolean initAutoStopProfiler;
400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 public String toString() {
402 return "AppBindData{appInfo=" + appInfo + "}";
403 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700404 }
405
406 static final class Profiler {
407 String profileFile;
408 ParcelFileDescriptor profileFd;
409 boolean autoStopProfiler;
410 boolean profiling;
411 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700412 public void setProfiler(String file, ParcelFileDescriptor fd) {
413 if (profiling) {
414 if (fd != null) {
415 try {
416 fd.close();
417 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700418 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700419 }
420 }
421 return;
422 }
423 if (profileFd != null) {
424 try {
425 profileFd.close();
426 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700427 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700428 }
429 }
430 profileFile = file;
431 profileFd = fd;
432 }
433 public void startProfiling() {
434 if (profileFd == null || profiling) {
435 return;
436 }
437 try {
438 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
439 8 * 1024 * 1024, 0);
440 profiling = true;
441 } catch (RuntimeException e) {
442 Slog.w(TAG, "Profiling failed on path " + profileFile);
443 try {
444 profileFd.close();
445 profileFd = null;
446 } catch (IOException e2) {
447 Slog.w(TAG, "Failure closing profile fd", e2);
448 }
449 }
450 }
451 public void stopProfiling() {
452 if (profiling) {
453 profiling = false;
454 Debug.stopMethodTracing();
455 if (profileFd != null) {
456 try {
457 profileFd.close();
458 } catch (IOException e) {
459 }
460 }
461 profileFd = null;
462 profileFile = null;
463 }
464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700468 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700469 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800470 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 }
473
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400474 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 IBinder token;
476 List<ResultInfo> results;
477 public String toString() {
478 return "ResultData{token=" + token + " results" + results + "}";
479 }
480 }
481
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400482 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800483 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 String what;
485 String who;
486 }
487
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400488 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700489 String path;
490 ParcelFileDescriptor fd;
491 }
492
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400493 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700494 String path;
495 ParcelFileDescriptor fd;
496 }
497
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400498 static final class UpdateCompatibilityData {
499 String pkg;
500 CompatibilityInfo info;
501 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700502
Romain Guy65b345f2011-07-27 18:51:50 -0700503 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700504
Romain Guy65b345f2011-07-27 18:51:50 -0700505 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700506 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
507 private static final String ONE_COUNT_COLUMN = "%21s %8d";
508 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700509 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 // Formatting for checkin service - update version if row format changes
512 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700513
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700514 private void updatePendingConfiguration(Configuration config) {
515 synchronized (mPackages) {
516 if (mPendingConfiguration == null ||
517 mPendingConfiguration.isOtherSeqNewer(config)) {
518 mPendingConfiguration = config;
519 }
520 }
521 }
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 public final void schedulePauseActivity(IBinder token, boolean finished,
524 boolean userLeaving, int configChanges) {
525 queueOrSendMessage(
526 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
527 token,
528 (userLeaving ? 1 : 0),
529 configChanges);
530 }
531
532 public final void scheduleStopActivity(IBinder token, boolean showWindow,
533 int configChanges) {
534 queueOrSendMessage(
535 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
536 token, 0, configChanges);
537 }
538
539 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
540 queueOrSendMessage(
541 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
542 token);
543 }
544
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800545 public final void scheduleSleeping(IBinder token, boolean sleeping) {
546 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
547 }
548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
550 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
551 }
552
553 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
554 ResultData res = new ResultData();
555 res.token = token;
556 res.results = results;
557 queueOrSendMessage(H.SEND_RESULT, res);
558 }
559
560 // we use token to identify this activity without having to send the
561 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700562 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700563 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
564 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700565 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
566 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700567 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568
569 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700570 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 r.intent = intent;
572 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400573 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 r.state = state;
575
576 r.pendingResults = pendingResults;
577 r.pendingIntents = pendingNewIntents;
578
579 r.startsNotResumed = notResumed;
580 r.isForward = isForward;
581
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700582 r.profileFile = profileName;
583 r.profileFd = profileFd;
584 r.autoStopProfiler = autoStopProfiler;
585
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700586 updatePendingConfiguration(curConfig);
587
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
589 }
590
591 public final void scheduleRelaunchActivity(IBinder token,
592 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800593 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800594 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
595 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
598 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
599 NewIntentData data = new NewIntentData();
600 data.intents = intents;
601 data.token = token;
602
603 queueOrSendMessage(H.NEW_INTENT, data);
604 }
605
606 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
607 int configChanges) {
608 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
609 configChanges);
610 }
611
612 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400613 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
614 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700615 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
616 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400618 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 queueOrSendMessage(H.RECEIVER, r);
620 }
621
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400622 public final void scheduleCreateBackupAgent(ApplicationInfo app,
623 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700624 CreateBackupAgentData d = new CreateBackupAgentData();
625 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400626 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700627 d.backupMode = backupMode;
628
629 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
630 }
631
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400632 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
633 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700634 CreateBackupAgentData d = new CreateBackupAgentData();
635 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400636 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700637
638 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
639 }
640
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400642 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 CreateServiceData s = new CreateServiceData();
644 s.token = token;
645 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400646 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
648 queueOrSendMessage(H.CREATE_SERVICE, s);
649 }
650
651 public final void scheduleBindService(IBinder token, Intent intent,
652 boolean rebind) {
653 BindServiceData s = new BindServiceData();
654 s.token = token;
655 s.intent = intent;
656 s.rebind = rebind;
657
Amith Yamasani742a6712011-05-04 14:49:28 -0700658 if (DEBUG_SERVICE)
659 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
660 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 queueOrSendMessage(H.BIND_SERVICE, s);
662 }
663
664 public final void scheduleUnbindService(IBinder token, Intent intent) {
665 BindServiceData s = new BindServiceData();
666 s.token = token;
667 s.intent = intent;
668
669 queueOrSendMessage(H.UNBIND_SERVICE, s);
670 }
671
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700672 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700673 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 ServiceArgsData s = new ServiceArgsData();
675 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700676 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700678 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 s.args = args;
680
681 queueOrSendMessage(H.SERVICE_ARGS, s);
682 }
683
684 public final void scheduleStopService(IBinder token) {
685 queueOrSendMessage(H.STOP_SERVICE, token);
686 }
687
688 public final void bindApplication(String processName,
689 ApplicationInfo appInfo, List<ProviderInfo> providers,
690 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700691 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800693 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
694 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700695 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 if (services != null) {
698 // Setup the service cache in the ServiceManager
699 ServiceManager.initServiceCache(services);
700 }
701
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800702 setCoreSettings(coreSettings);
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 AppBindData data = new AppBindData();
705 data.processName = processName;
706 data.appInfo = appInfo;
707 data.providers = providers;
708 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 data.instrumentationArgs = instrumentationArgs;
710 data.instrumentationWatcher = instrumentationWatcher;
711 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800712 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700713 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700714 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400716 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700717 data.initProfileFile = profileFile;
718 data.initProfileFd = profileFd;
719 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 queueOrSendMessage(H.BIND_APPLICATION, data);
721 }
722
723 public final void scheduleExit() {
724 queueOrSendMessage(H.EXIT_APPLICATION, null);
725 }
726
Christopher Tate5e1ab332009-09-01 20:32:49 -0700727 public final void scheduleSuicide() {
728 queueOrSendMessage(H.SUICIDE, null);
729 }
730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 public void requestThumbnail(IBinder token) {
732 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
733 }
734
735 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700736 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
738 }
739
740 public void updateTimeZone() {
741 TimeZone.setDefault(null);
742 }
743
Robert Greenwalt03595d02010-11-02 14:08:23 -0700744 public void clearDnsCache() {
745 // a non-standard API to get this to libcore
746 InetAddress.clearDnsCache();
747 }
748
Robert Greenwalt434203a2010-10-11 16:00:27 -0700749 public void setHttpProxy(String host, String port, String exclList) {
750 Proxy.setHttpProxySystemProperty(host, port, exclList);
751 }
752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 public void processInBackground() {
754 mH.removeMessages(H.GC_WHEN_IDLE);
755 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
756 }
757
758 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700759 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700760 try {
761 data.fd = ParcelFileDescriptor.dup(fd);
762 data.token = servicetoken;
763 data.args = args;
764 queueOrSendMessage(H.DUMP_SERVICE, data);
765 } catch (IOException e) {
766 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 }
768 }
769
770 // This function exists to make sure all receiver dispatching is
771 // correctly ordered, since these are one-way calls and the binder driver
772 // applies transaction ordering per object for such calls.
773 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700774 int resultCode, String dataStr, Bundle extras, boolean ordered,
775 boolean sticky) throws RemoteException {
776 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 }
Bob Leee5408332009-09-04 18:31:17 -0700778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 public void scheduleLowMemory() {
780 queueOrSendMessage(H.LOW_MEMORY, null);
781 }
782
783 public void scheduleActivityConfigurationChanged(IBinder token) {
784 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
785 }
786
Romain Guy7eabe552011-07-21 14:56:34 -0700787 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
788 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700789 ProfilerControlData pcd = new ProfilerControlData();
790 pcd.path = path;
791 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700792 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800793 }
794
Andy McFadden824c5102010-07-09 16:26:57 -0700795 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
796 DumpHeapData dhd = new DumpHeapData();
797 dhd.path = path;
798 dhd.fd = fd;
799 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
800 }
801
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700802 public void setSchedulingGroup(int group) {
803 // Note: do this immediately, since going into the foreground
804 // should happen regardless of what pending work we have to do
805 // and the activity manager will wait for us to report back that
806 // we are done before sending us to the background.
807 try {
808 Process.setProcessGroup(Process.myPid(), group);
809 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800810 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700811 }
812 }
Bob Leee5408332009-09-04 18:31:17 -0700813
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700814 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
815 Debug.getMemoryInfo(outInfo);
816 }
Bob Leee5408332009-09-04 18:31:17 -0700817
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700818 public void dispatchPackageBroadcast(int cmd, String[] packages) {
819 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
820 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700821
822 public void scheduleCrash(String msg) {
823 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
824 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700825
Dianne Hackborn30d71892010-12-11 10:37:55 -0800826 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
827 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700828 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700829 try {
830 data.fd = ParcelFileDescriptor.dup(fd);
831 data.token = activitytoken;
832 data.prefix = prefix;
833 data.args = args;
834 queueOrSendMessage(H.DUMP_ACTIVITY, data);
835 } catch (IOException e) {
836 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700837 }
838 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700839
Marco Nelissen18cb2872011-11-15 11:19:53 -0800840 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
841 String[] args) {
842 DumpComponentInfo data = new DumpComponentInfo();
843 try {
844 data.fd = ParcelFileDescriptor.dup(fd);
845 data.token = providertoken;
846 data.args = args;
847 queueOrSendMessage(H.DUMP_PROVIDER, data);
848 } catch (IOException e) {
849 Slog.w(TAG, "dumpProvider failed", e);
850 }
851 }
852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700854 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
855 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700856 FileOutputStream fout = new FileOutputStream(fd);
857 PrintWriter pw = new PrintWriter(fout);
858 try {
Romain Guya998dff2012-03-23 18:58:36 -0700859 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700860 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700861 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700862 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700863 }
864
Romain Guya998dff2012-03-23 18:58:36 -0700865 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 long nativeMax = Debug.getNativeHeapSize() / 1024;
867 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
868 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
869
870 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
871 Debug.getMemoryInfo(memInfo);
872
Dianne Hackbornb437e092011-08-05 17:50:29 -0700873 if (!all) {
874 return memInfo;
875 }
876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 Runtime runtime = Runtime.getRuntime();
878
879 long dalvikMax = runtime.totalMemory() / 1024;
880 long dalvikFree = runtime.freeMemory() / 1024;
881 long dalvikAllocated = dalvikMax - dalvikFree;
882 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700883 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700884 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
885 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 int globalAssetCount = AssetManager.getGlobalAssetCount();
887 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
888 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
889 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
890 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700891 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800892 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700895 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // NOTE: if you change anything significant below, also consider changing
897 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700898 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 // Header
902 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
903 pw.print(Process.myPid()); pw.print(',');
904 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 // Heap info - max
907 pw.print(nativeMax); pw.print(',');
908 pw.print(dalvikMax); pw.print(',');
909 pw.print("N/A,");
910 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 // Heap info - allocated
913 pw.print(nativeAllocated); pw.print(',');
914 pw.print(dalvikAllocated); pw.print(',');
915 pw.print("N/A,");
916 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 // Heap info - free
919 pw.print(nativeFree); pw.print(',');
920 pw.print(dalvikFree); pw.print(',');
921 pw.print("N/A,");
922 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 // Heap info - proportional set size
925 pw.print(memInfo.nativePss); pw.print(',');
926 pw.print(memInfo.dalvikPss); pw.print(',');
927 pw.print(memInfo.otherPss); pw.print(',');
928 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700931 pw.print(memInfo.nativeSharedDirty); pw.print(',');
932 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
933 pw.print(memInfo.otherSharedDirty); pw.print(',');
934 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
935 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 pw.print(memInfo.nativePrivateDirty); pw.print(',');
939 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
940 pw.print(memInfo.otherPrivateDirty); pw.print(',');
941 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
942 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 // Object counts
945 pw.print(viewInstanceCount); pw.print(',');
946 pw.print(viewRootInstanceCount); pw.print(',');
947 pw.print(appContextInstanceCount); pw.print(',');
948 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 pw.print(globalAssetCount); pw.print(',');
951 pw.print(globalAssetManagerCount); pw.print(',');
952 pw.print(binderLocalObjectCount); pw.print(',');
953 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 pw.print(binderDeathObjectCount); pw.print(',');
956 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800959 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800960 pw.print(stats.memoryUsed / 1024); pw.print(',');
961 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700962 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800963 for (int i = 0; i < stats.dbStats.size(); i++) {
964 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700965 pw.print(','); pw.print(dbStats.dbName);
966 pw.print(','); pw.print(dbStats.pageSize);
967 pw.print(','); pw.print(dbStats.dbSize);
968 pw.print(','); pw.print(dbStats.lookaside);
969 pw.print(','); pw.print(dbStats.cache);
970 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800971 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700972 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700973
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700974 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
Bob Leee5408332009-09-04 18:31:17 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700978 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
979 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
980 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
981 "------");
982 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
983 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
984 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
985 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700987 int otherPss = memInfo.otherPss;
988 int otherSharedDirty = memInfo.otherSharedDirty;
989 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700991 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700992 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700993 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
994 memInfo.getOtherPrivateDirty(i), "", "", "");
995 otherPss -= memInfo.getOtherPss(i);
996 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
997 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
998 }
999
1000 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1001 otherPrivateDirty, "", "", "");
1002 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1003 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1004 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1005 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006
1007 pw.println(" ");
1008 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001009 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 viewRootInstanceCount);
1011
1012 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1013 "Activities:", activityInstanceCount);
1014
1015 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1016 "AssetManagers:", globalAssetManagerCount);
1017
1018 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1019 "Proxy Binders:", binderProxyObjectCount);
1020 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1021
1022 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 // SQLite mem info
1025 pw.println(" ");
1026 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001027 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1028 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1029 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001030 pw.println(" ");
1031 int N = stats.dbStats.size();
1032 if (N > 0) {
1033 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001034 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1035 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001036 for (int i = 0; i < N; i++) {
1037 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001038 printRow(pw, DB_INFO_FORMAT,
1039 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1040 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1041 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1042 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001043 }
1044 }
Bob Leee5408332009-09-04 18:31:17 -07001045
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001046 // Asset details.
1047 String assetAlloc = AssetManager.getAssetAllocations();
1048 if (assetAlloc != null) {
1049 pw.println(" ");
1050 pw.println(" Asset Allocations");
1051 pw.print(assetAlloc);
1052 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001053
1054 return memInfo;
1055 }
1056
1057 @Override
1058 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1059 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001060 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 }
1062
Jeff Brown6754ba22011-12-14 20:20:01 -08001063 @Override
1064 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1065 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1066 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1067 SQLiteDebug.dump(printer, args);
1068 pw.flush();
1069 }
1070
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001071 @Override
1072 public void unstableProviderDied(IBinder provider) {
1073 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1074 }
1075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 private void printRow(PrintWriter pw, String format, Object...objs) {
1077 pw.println(String.format(format, objs));
1078 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001079
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001080 public void setCoreSettings(Bundle coreSettings) {
1081 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001082 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001083
1084 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1085 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1086 ucd.pkg = pkg;
1087 ucd.info = info;
1088 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1089 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001090
1091 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001092 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001093 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 }
1096
Romain Guy65b345f2011-07-27 18:51:50 -07001097 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 public static final int LAUNCH_ACTIVITY = 100;
1099 public static final int PAUSE_ACTIVITY = 101;
1100 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1101 public static final int STOP_ACTIVITY_SHOW = 103;
1102 public static final int STOP_ACTIVITY_HIDE = 104;
1103 public static final int SHOW_WINDOW = 105;
1104 public static final int HIDE_WINDOW = 106;
1105 public static final int RESUME_ACTIVITY = 107;
1106 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001107 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 public static final int BIND_APPLICATION = 110;
1109 public static final int EXIT_APPLICATION = 111;
1110 public static final int NEW_INTENT = 112;
1111 public static final int RECEIVER = 113;
1112 public static final int CREATE_SERVICE = 114;
1113 public static final int SERVICE_ARGS = 115;
1114 public static final int STOP_SERVICE = 116;
1115 public static final int REQUEST_THUMBNAIL = 117;
1116 public static final int CONFIGURATION_CHANGED = 118;
1117 public static final int CLEAN_UP_CONTEXT = 119;
1118 public static final int GC_WHEN_IDLE = 120;
1119 public static final int BIND_SERVICE = 121;
1120 public static final int UNBIND_SERVICE = 122;
1121 public static final int DUMP_SERVICE = 123;
1122 public static final int LOW_MEMORY = 124;
1123 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1124 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001125 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001126 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001127 public static final int DESTROY_BACKUP_AGENT = 129;
1128 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001129 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001130 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001131 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001132 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001133 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001134 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001135 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001136 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001137 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001138 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001139 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001140 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001142 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 switch (code) {
1144 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1145 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1146 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1147 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1148 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1149 case SHOW_WINDOW: return "SHOW_WINDOW";
1150 case HIDE_WINDOW: return "HIDE_WINDOW";
1151 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1152 case SEND_RESULT: return "SEND_RESULT";
1153 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1154 case BIND_APPLICATION: return "BIND_APPLICATION";
1155 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1156 case NEW_INTENT: return "NEW_INTENT";
1157 case RECEIVER: return "RECEIVER";
1158 case CREATE_SERVICE: return "CREATE_SERVICE";
1159 case SERVICE_ARGS: return "SERVICE_ARGS";
1160 case STOP_SERVICE: return "STOP_SERVICE";
1161 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1162 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1163 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1164 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1165 case BIND_SERVICE: return "BIND_SERVICE";
1166 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1167 case DUMP_SERVICE: return "DUMP_SERVICE";
1168 case LOW_MEMORY: return "LOW_MEMORY";
1169 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1170 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001171 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001172 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1173 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001174 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001175 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001176 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001177 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001178 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001179 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001180 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001181 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001182 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001183 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001184 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001185 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001186 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 }
1188 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001189 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
1191 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001192 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 switch (msg.what) {
1194 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001195 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001196 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197
1198 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001199 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001200 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001201 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 } break;
1203 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001204 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001205 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001206 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001207 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 } break;
1209 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001210 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001212 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001213 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 break;
1215 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001216 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001218 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 break;
1220 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001221 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001223 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 break;
1225 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001226 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001228 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 break;
1230 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001231 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 break;
1235 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001236 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001238 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 break;
1240 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001241 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 handleResumeActivity((IBinder)msg.obj, true,
1243 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001244 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 break;
1246 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001247 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001249 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 break;
1251 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001252 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1254 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001255 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 break;
1257 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001258 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 AppBindData data = (AppBindData)msg.obj;
1260 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001261 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 break;
1263 case EXIT_APPLICATION:
1264 if (mInitialApplication != null) {
1265 mInitialApplication.onTerminate();
1266 }
1267 Looper.myLooper().quit();
1268 break;
1269 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001270 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001272 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 break;
1274 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001275 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001277 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001278 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 break;
1280 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001281 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001283 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001286 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 break;
1290 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 break;
1295 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001301 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001303 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001304 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 break;
1306 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001307 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 break;
1311 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001312 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001313 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001314 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001315 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 case CLEAN_UP_CONTEXT:
1318 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1319 cci.context.performFinalCleanup(cci.who, cci.what);
1320 break;
1321 case GC_WHEN_IDLE:
1322 scheduleGcIdler();
1323 break;
1324 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001325 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 break;
1327 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001328 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001330 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 break;
1332 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001337 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001338 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001339 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001340 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001341 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001342 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001343 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001344 break;
1345 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001347 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001349 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001350 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001351 Process.killProcess(Process.myPid());
1352 break;
1353 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001354 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001355 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001356 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001357 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001358 case ENABLE_JIT:
1359 ensureJitEnabled();
1360 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001361 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001362 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001363 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001364 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001365 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001366 case SCHEDULE_CRASH:
1367 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001368 case DUMP_HEAP:
1369 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1370 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001371 case DUMP_ACTIVITY:
1372 handleDumpActivity((DumpComponentInfo)msg.obj);
1373 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001374 case DUMP_PROVIDER:
1375 handleDumpProvider((DumpComponentInfo)msg.obj);
1376 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001377 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001378 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001379 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001380 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001381 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001382 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001384 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001386 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001387 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1388 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001389 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001390 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001391 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001392 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001393 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001394 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001395 case UNSTABLE_PROVIDER_DIED:
1396 handleUnstableProviderDied((IBinder)msg.obj, false);
1397 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001399 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
Bob Leee5408332009-09-04 18:31:17 -07001401
Brian Carlstromed7e0072011-03-24 13:27:57 -07001402 private void maybeSnapshot() {
1403 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001404 // convert the *private* ActivityThread.PackageInfo to *public* known
1405 // android.content.pm.PackageInfo
1406 String packageName = mBoundApplication.info.mPackageName;
1407 android.content.pm.PackageInfo packageInfo = null;
1408 try {
1409 Context context = getSystemContext();
1410 if(context == null) {
1411 Log.e(TAG, "cannot get a valid context");
1412 return;
1413 }
1414 PackageManager pm = context.getPackageManager();
1415 if(pm == null) {
1416 Log.e(TAG, "cannot get a valid PackageManager");
1417 return;
1418 }
1419 packageInfo = pm.getPackageInfo(
1420 packageName, PackageManager.GET_ACTIVITIES);
1421 } catch (NameNotFoundException e) {
1422 Log.e(TAG, "cannot get package info for " + packageName, e);
1423 }
1424 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001425 }
1426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 }
1428
Romain Guy65b345f2011-07-27 18:51:50 -07001429 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001431 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001432 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001433 if (mBoundApplication != null && mProfiler.profileFd != null
1434 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001435 stopProfiling = true;
1436 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (a != null) {
1438 mNewActivities = null;
1439 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001440 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001442 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 TAG, "Reporting idle of " + a +
1444 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001445 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 if (a.activity != null && !a.activity.mFinished) {
1447 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001448 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001449 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001451 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 }
1453 }
1454 prev = a;
1455 a = a.nextIdle;
1456 prev.nextIdle = null;
1457 } while (a != null);
1458 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001459 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001460 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001461 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001462 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 return false;
1464 }
1465 }
1466
1467 final class GcIdler implements MessageQueue.IdleHandler {
1468 public final boolean queueIdle() {
1469 doGcIfNeeded();
1470 return false;
1471 }
1472 }
1473
Romain Guy65b345f2011-07-27 18:51:50 -07001474 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001475 final private String mResDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001476 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001477 final private float mScale;
1478 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001479
Dianne Hackborn756220b2012-08-14 16:45:30 -07001480 ResourcesKey(String resDir, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001481 mResDir = resDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001482 if (overrideConfiguration != null) {
1483 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1484 overrideConfiguration = null;
1485 }
1486 }
1487 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001488 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001489 int hash = 17;
1490 hash = 31 * hash + mResDir.hashCode();
1491 hash = 31 * hash + (mOverrideConfiguration != null
1492 ? mOverrideConfiguration.hashCode() : 0);
1493 hash = 31 * hash + Float.floatToIntBits(mScale);
1494 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001495 }
Bob Leee5408332009-09-04 18:31:17 -07001496
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001497 @Override
1498 public int hashCode() {
1499 return mHash;
1500 }
1501
1502 @Override
1503 public boolean equals(Object obj) {
1504 if (!(obj instanceof ResourcesKey)) {
1505 return false;
1506 }
1507 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001508 if (!mResDir.equals(peer.mResDir)) {
1509 return false;
1510 }
1511 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1512 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1513 return false;
1514 }
1515 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1516 return false;
1517 }
1518 }
1519 if (mScale != peer.mScale) {
1520 return false;
1521 }
1522 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001523 }
1524 }
1525
Romain Guy65b345f2011-07-27 18:51:50 -07001526 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001527 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529
Romain Guy65b345f2011-07-27 18:51:50 -07001530 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001531 ActivityThread am = currentActivityThread();
1532 return (am != null && am.mBoundApplication != null)
1533 ? am.mBoundApplication.processName : null;
1534 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535
Romain Guy65b345f2011-07-27 18:51:50 -07001536 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 ActivityThread am = currentActivityThread();
1538 return am != null ? am.mInitialApplication : null;
1539 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001541 public static IPackageManager getPackageManager() {
1542 if (sPackageManager != null) {
1543 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1544 return sPackageManager;
1545 }
1546 IBinder b = ServiceManager.getService("package");
1547 //Slog.v("PackageManager", "default service binder = " + b);
1548 sPackageManager = IPackageManager.Stub.asInterface(b);
1549 //Slog.v("PackageManager", "default service = " + sPackageManager);
1550 return sPackageManager;
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001553 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1554 DisplayMetrics dm = mDisplayMetrics.get(ci);
1555 if (dm != null && !forceUpdate) {
1556 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001557 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001558 if (dm == null) {
1559 dm = new DisplayMetrics();
1560 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001561 }
Jeff Brownd32460c2012-07-20 16:15:36 -07001562 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1563 cih.set(ci);
1564 Display d = WindowManagerImpl.getDefault().makeCompatible(cih).getDefaultDisplay();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001565 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001566 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1567 // + metrics.heightPixels + " den=" + metrics.density
1568 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001569 return dm;
1570 }
1571
Romain Guy65b345f2011-07-27 18:51:50 -07001572 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001573 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1574 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001575 if (config == null) {
1576 return null;
1577 }
1578 if (compat != null && !compat.supportsScreen()) {
1579 mMainThreadConfig.setTo(config);
1580 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001581 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001582 }
1583 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001586 /**
1587 * Creates the top level Resources for applications with the given compatibility info.
1588 *
1589 * @param resDir the resource directory.
1590 * @param compInfo the compability info. It will use the default compatibility info when it's
1591 * null.
1592 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001593 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1594 CompatibilityInfo compInfo) {
1595 ResourcesKey key = new ResourcesKey(resDir, overrideConfiguration,
1596 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001597 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001599 // Resources is app scale dependent.
1600 if (false) {
1601 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1602 + compInfo.applicationScale);
1603 }
1604 WeakReference<Resources> wr = mActiveResources.get(key);
1605 r = wr != null ? wr.get() : null;
1606 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1607 if (r != null && r.getAssets().isUpToDate()) {
1608 if (false) {
1609 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1610 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1611 }
1612 return r;
1613 }
1614 }
1615
1616 //if (r != null) {
1617 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1618 // + r + " " + resDir);
1619 //}
1620
1621 AssetManager assets = new AssetManager();
1622 if (assets.addAssetPath(resDir) == 0) {
1623 return null;
1624 }
1625
1626 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001627 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001628 Configuration config;
1629 if (key.mOverrideConfiguration != null) {
1630 config = new Configuration(getConfiguration());
1631 config.updateFrom(key.mOverrideConfiguration);
1632 } else {
1633 config = getConfiguration();
1634 }
1635 r = new Resources(assets, metrics, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001636 if (false) {
1637 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1638 + r.getConfiguration() + " appScale="
1639 + r.getCompatibilityInfo().applicationScale);
1640 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001641
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001642 synchronized (mPackages) {
1643 WeakReference<Resources> wr = mActiveResources.get(key);
1644 Resources existing = wr != null ? wr.get() : null;
1645 if (existing != null && existing.getAssets().isUpToDate()) {
1646 // Someone else already created the resources while we were
1647 // unlocked; go ahead and use theirs.
1648 r.getAssets().close();
1649 return existing;
1650 }
1651
1652 // XXX need to remove entries when weak references go away
1653 mActiveResources.put(key, new WeakReference<Resources>(r));
1654 return r;
1655 }
1656 }
1657
1658 /**
1659 * Creates the top level resources for the given package.
1660 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001661 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1662 LoadedApk pkgInfo) {
1663 return getTopLevelResources(resDir, overrideConfiguration,
1664 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001665 }
1666
1667 final Handler getHandler() {
1668 return mH;
1669 }
1670
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001671 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1672 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001673 synchronized (mPackages) {
1674 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1676 ref = mPackages.get(packageName);
1677 } else {
1678 ref = mResourcePackages.get(packageName);
1679 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001680 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001681 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001682 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1683 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if (packageInfo != null && (packageInfo.mResources == null
1685 || packageInfo.mResources.getAssets().isUpToDate())) {
1686 if (packageInfo.isSecurityViolation()
1687 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1688 throw new SecurityException(
1689 "Requesting code from " + packageName
1690 + " to be run in process "
1691 + mBoundApplication.processName
1692 + "/" + mBoundApplication.appInfo.uid);
1693 }
1694 return packageInfo;
1695 }
1696 }
1697
1698 ApplicationInfo ai = null;
1699 try {
1700 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001701 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001703 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705
1706 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001707 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 }
1709
1710 return null;
1711 }
1712
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001713 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1714 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1716 boolean securityViolation = includeCode && ai.uid != 0
1717 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001718 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001719 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1721 |Context.CONTEXT_IGNORE_SECURITY))
1722 == Context.CONTEXT_INCLUDE_CODE) {
1723 if (securityViolation) {
1724 String msg = "Requesting code from " + ai.packageName
1725 + " (with uid " + ai.uid + ")";
1726 if (mBoundApplication != null) {
1727 msg = msg + " to be run in process "
1728 + mBoundApplication.processName + " (with uid "
1729 + mBoundApplication.appInfo.uid + ")";
1730 }
1731 throw new SecurityException(msg);
1732 }
1733 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001734 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001737 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1738 CompatibilityInfo compatInfo) {
1739 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001740 }
1741
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001742 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1743 synchronized (mPackages) {
1744 WeakReference<LoadedApk> ref;
1745 if (includeCode) {
1746 ref = mPackages.get(packageName);
1747 } else {
1748 ref = mResourcePackages.get(packageName);
1749 }
1750 return ref != null ? ref.get() : null;
1751 }
1752 }
1753
Romain Guy65b345f2011-07-27 18:51:50 -07001754 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1756 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001757 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 if (includeCode) {
1759 ref = mPackages.get(aInfo.packageName);
1760 } else {
1761 ref = mResourcePackages.get(aInfo.packageName);
1762 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001763 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (packageInfo == null || (packageInfo.mResources != null
1765 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001766 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 : "Loading resource-only package ") + aInfo.packageName
1768 + " (in " + (mBoundApplication != null
1769 ? mBoundApplication.processName : null)
1770 + ")");
1771 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001772 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 securityViolation, includeCode &&
1774 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1775 if (includeCode) {
1776 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001777 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 } else {
1779 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001780 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 }
1782 }
1783 return packageInfo;
1784 }
1785 }
1786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 ActivityThread() {
1788 }
1789
1790 public ApplicationThread getApplicationThread()
1791 {
1792 return mAppThread;
1793 }
1794
1795 public Instrumentation getInstrumentation()
1796 {
1797 return mInstrumentation;
1798 }
1799
1800 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001801 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 }
1803
1804 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001805 return mProfiler != null && mProfiler.profileFile != null
1806 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808
1809 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001810 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812
1813 public Looper getLooper() {
1814 return mLooper;
1815 }
1816
1817 public Application getApplication() {
1818 return mInitialApplication;
1819 }
Bob Leee5408332009-09-04 18:31:17 -07001820
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001821 public String getProcessName() {
1822 return mBoundApplication.processName;
1823 }
Bob Leee5408332009-09-04 18:31:17 -07001824
Dianne Hackborn21556372010-02-04 16:34:40 -08001825 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 synchronized (this) {
1827 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001828 ContextImpl context =
1829 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001830 LoadedApk info = new LoadedApk(this, "android", context, null,
1831 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 context.init(info, null, this);
1833 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001834 getConfiguration(), getDisplayMetricsLocked(
1835 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001837 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 // + ": " + context.getResources().getConfiguration());
1839 }
1840 }
1841 return mSystemContext;
1842 }
1843
Mike Cleron432b7132009-09-24 15:28:29 -07001844 public void installSystemApplicationInfo(ApplicationInfo info) {
1845 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001846 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001847 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001848 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001849
1850 // give ourselves a default profiler
1851 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001852 }
1853 }
1854
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001855 void ensureJitEnabled() {
1856 if (!mJitEnabled) {
1857 mJitEnabled = true;
1858 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1859 }
1860 }
1861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 void scheduleGcIdler() {
1863 if (!mGcIdlerScheduled) {
1864 mGcIdlerScheduled = true;
1865 Looper.myQueue().addIdleHandler(mGcIdler);
1866 }
1867 mH.removeMessages(H.GC_WHEN_IDLE);
1868 }
1869
1870 void unscheduleGcIdler() {
1871 if (mGcIdlerScheduled) {
1872 mGcIdlerScheduled = false;
1873 Looper.myQueue().removeIdleHandler(mGcIdler);
1874 }
1875 mH.removeMessages(H.GC_WHEN_IDLE);
1876 }
1877
1878 void doGcIfNeeded() {
1879 mGcIdlerScheduled = false;
1880 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001881 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001882 // + "m now=" + now);
1883 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001884 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 BinderInternal.forceGc("bg");
1886 }
1887 }
1888
Jeff Hamilton52d32032011-01-08 15:31:26 -06001889 public void registerOnActivityPausedListener(Activity activity,
1890 OnActivityPausedListener listener) {
1891 synchronized (mOnPauseListeners) {
1892 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1893 if (list == null) {
1894 list = new ArrayList<OnActivityPausedListener>();
1895 mOnPauseListeners.put(activity, list);
1896 }
1897 list.add(listener);
1898 }
1899 }
1900
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001901 public void unregisterOnActivityPausedListener(Activity activity,
1902 OnActivityPausedListener listener) {
1903 synchronized (mOnPauseListeners) {
1904 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1905 if (list != null) {
1906 list.remove(listener);
1907 }
1908 }
1909 }
1910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 public final ActivityInfo resolveActivityInfo(Intent intent) {
1912 ActivityInfo aInfo = intent.resolveActivityInfo(
1913 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1914 if (aInfo == null) {
1915 // Throw an exception.
1916 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001917 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 }
1919 return aInfo;
1920 }
Bob Leee5408332009-09-04 18:31:17 -07001921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001924 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001925 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001927 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 r.intent = intent;
1929 r.state = state;
1930 r.parent = parent;
1931 r.embeddedID = id;
1932 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001933 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 if (localLOGV) {
1935 ComponentName compname = intent.getComponent();
1936 String name;
1937 if (compname != null) {
1938 name = compname.toShortString();
1939 } else {
1940 name = "(Intent " + intent + ").getComponent() returned null";
1941 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001942 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 + ", comp=" + name
1944 + ", token=" + token);
1945 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001946 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 }
1948
1949 public final Activity getActivity(IBinder token) {
1950 return mActivities.get(token).activity;
1951 }
1952
1953 public final void sendActivityResult(
1954 IBinder token, String id, int requestCode,
1955 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001956 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001957 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1959 list.add(new ResultInfo(id, requestCode, resultCode, data));
1960 mAppThread.scheduleSendResult(token, list);
1961 }
1962
1963 // if the thread hasn't started yet, we don't have the handler, so just
1964 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001965 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 queueOrSendMessage(what, obj, 0, 0);
1967 }
1968
Romain Guy65b345f2011-07-27 18:51:50 -07001969 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 queueOrSendMessage(what, obj, arg1, 0);
1971 }
1972
Romain Guy65b345f2011-07-27 18:51:50 -07001973 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001975 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1977 + ": " + arg1 + " / " + obj);
1978 Message msg = Message.obtain();
1979 msg.what = what;
1980 msg.obj = obj;
1981 msg.arg1 = arg1;
1982 msg.arg2 = arg2;
1983 mH.sendMessage(msg);
1984 }
1985 }
1986
Dianne Hackborn21556372010-02-04 16:34:40 -08001987 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 String what) {
1989 ContextCleanupInfo cci = new ContextCleanupInfo();
1990 cci.context = context;
1991 cci.who = who;
1992 cci.what = what;
1993 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1994 }
1995
Romain Guy65b345f2011-07-27 18:51:50 -07001996 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1998
1999 ActivityInfo aInfo = r.activityInfo;
2000 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002001 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 Context.CONTEXT_INCLUDE_CODE);
2003 }
Bob Leee5408332009-09-04 18:31:17 -07002004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 ComponentName component = r.intent.getComponent();
2006 if (component == null) {
2007 component = r.intent.resolveActivity(
2008 mInitialApplication.getPackageManager());
2009 r.intent.setComponent(component);
2010 }
2011
2012 if (r.activityInfo.targetActivity != null) {
2013 component = new ComponentName(r.activityInfo.packageName,
2014 r.activityInfo.targetActivity);
2015 }
2016
2017 Activity activity = null;
2018 try {
2019 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2020 activity = mInstrumentation.newActivity(
2021 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002022 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 r.intent.setExtrasClassLoader(cl);
2024 if (r.state != null) {
2025 r.state.setClassLoader(cl);
2026 }
2027 } catch (Exception e) {
2028 if (!mInstrumentation.onException(activity, e)) {
2029 throw new RuntimeException(
2030 "Unable to instantiate activity " + component
2031 + ": " + e.toString(), e);
2032 }
2033 }
2034
2035 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002036 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002037
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002038 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2039 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 TAG, r + ": app=" + app
2041 + ", appName=" + app.getPackageName()
2042 + ", pkg=" + r.packageInfo.getPackageName()
2043 + ", comp=" + r.intent.getComponent().toShortString()
2044 + ", dir=" + r.packageInfo.getAppDir());
2045
2046 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002047 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 appContext.init(r.packageInfo, r.token, this);
2049 appContext.setOuterContext(activity);
2050 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002051 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002052 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002053 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002054 activity.attach(appContext, this, getInstrumentation(), r.token,
2055 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002056 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002057
Christopher Tateb70f3df2009-04-07 16:07:59 -07002058 if (customIntent != null) {
2059 activity.mIntent = customIntent;
2060 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002061 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 activity.mStartedActivity = false;
2063 int theme = r.activityInfo.getThemeResource();
2064 if (theme != 0) {
2065 activity.setTheme(theme);
2066 }
2067
2068 activity.mCalled = false;
2069 mInstrumentation.callActivityOnCreate(activity, r.state);
2070 if (!activity.mCalled) {
2071 throw new SuperNotCalledException(
2072 "Activity " + r.intent.getComponent().toShortString() +
2073 " did not call through to super.onCreate()");
2074 }
2075 r.activity = activity;
2076 r.stopped = true;
2077 if (!r.activity.mFinished) {
2078 activity.performStart();
2079 r.stopped = false;
2080 }
2081 if (!r.activity.mFinished) {
2082 if (r.state != null) {
2083 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2084 }
2085 }
2086 if (!r.activity.mFinished) {
2087 activity.mCalled = false;
2088 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2089 if (!activity.mCalled) {
2090 throw new SuperNotCalledException(
2091 "Activity " + r.intent.getComponent().toShortString() +
2092 " did not call through to super.onPostCreate()");
2093 }
2094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096 r.paused = true;
2097
2098 mActivities.put(r.token, r);
2099
2100 } catch (SuperNotCalledException e) {
2101 throw e;
2102
2103 } catch (Exception e) {
2104 if (!mInstrumentation.onException(activity, e)) {
2105 throw new RuntimeException(
2106 "Unable to start activity " + component
2107 + ": " + e.toString(), e);
2108 }
2109 }
2110
2111 return activity;
2112 }
2113
Romain Guy65b345f2011-07-27 18:51:50 -07002114 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 // If we are getting ready to gc after going to the background, well
2116 // we are back active so skip it.
2117 unscheduleGcIdler();
2118
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002119 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002120 mProfiler.setProfiler(r.profileFile, r.profileFd);
2121 mProfiler.startProfiling();
2122 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002123 }
2124
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002125 // Make sure we are running with the most recent config.
2126 handleConfigurationChanged(null, null);
2127
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002128 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002130 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131
2132 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002133 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002134 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 handleResumeActivity(r.token, false, r.isForward);
2136
2137 if (!r.activity.mFinished && r.startsNotResumed) {
2138 // The activity manager actually wants this one to start out
2139 // paused, because it needs to be visible but isn't in the
2140 // foreground. We accomplish this by going through the
2141 // normal startup (because activities expect to go through
2142 // onResume() the first time they run, before their window
2143 // is displayed), and then pausing it. However, in this case
2144 // we do -not- need to do the full pause cycle (of freezing
2145 // and such) because the activity manager assumes it can just
2146 // retain the current state it has.
2147 try {
2148 r.activity.mCalled = false;
2149 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002150 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002151 // we need to be created again. But we only do this
2152 // for pre-Honeycomb apps, which always save their state
2153 // when pausing, so we can not have them save their state
2154 // when restarting from a paused state. For HC and later,
2155 // we want to (and can) let the state be saved as the normal
2156 // part of stopping the activity.
2157 if (r.isPreHoneycomb()) {
2158 r.state = oldState;
2159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 if (!r.activity.mCalled) {
2161 throw new SuperNotCalledException(
2162 "Activity " + r.intent.getComponent().toShortString() +
2163 " did not call through to super.onPause()");
2164 }
2165
2166 } catch (SuperNotCalledException e) {
2167 throw e;
2168
2169 } catch (Exception e) {
2170 if (!mInstrumentation.onException(r.activity, e)) {
2171 throw new RuntimeException(
2172 "Unable to pause activity "
2173 + r.intent.getComponent().toShortString()
2174 + ": " + e.toString(), e);
2175 }
2176 }
2177 r.paused = true;
2178 }
2179 } else {
2180 // If there was an error, for any reason, tell the activity
2181 // manager to stop us.
2182 try {
2183 ActivityManagerNative.getDefault()
2184 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2185 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002186 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188 }
2189 }
2190
Romain Guy65b345f2011-07-27 18:51:50 -07002191 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 List<Intent> intents) {
2193 final int N = intents.size();
2194 for (int i=0; i<N; i++) {
2195 Intent intent = intents.get(i);
2196 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002197 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002198 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2199 }
2200 }
2201
2202 public final void performNewIntents(IBinder token,
2203 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002204 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 if (r != null) {
2206 final boolean resumed = !r.paused;
2207 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002208 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 mInstrumentation.callActivityOnPause(r.activity);
2210 }
2211 deliverNewIntents(r, intents);
2212 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002213 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002214 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
2216 }
2217 }
Bob Leee5408332009-09-04 18:31:17 -07002218
Romain Guy65b345f2011-07-27 18:51:50 -07002219 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 performNewIntents(data.token, data.intents);
2221 }
2222
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002223 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2224
2225 /**
2226 * Return the Intent that's currently being handled by a
2227 * BroadcastReceiver on this thread, or null if none.
2228 * @hide
2229 */
2230 public static Intent getIntentBeingBroadcast() {
2231 return sCurrentBroadcastIntent.get();
2232 }
2233
Romain Guy65b345f2011-07-27 18:51:50 -07002234 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235 // If we are getting ready to gc after going to the background, well
2236 // we are back active so skip it.
2237 unscheduleGcIdler();
2238
2239 String component = data.intent.getComponent().getClassName();
2240
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002241 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002242 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243
2244 IActivityManager mgr = ActivityManagerNative.getDefault();
2245
Romain Guy65b345f2011-07-27 18:51:50 -07002246 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 try {
2248 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2249 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002250 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2252 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002253 if (DEBUG_BROADCAST) Slog.i(TAG,
2254 "Finishing failed broadcast to " + data.intent.getComponent());
2255 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 throw new RuntimeException(
2257 "Unable to instantiate receiver " + component
2258 + ": " + e.toString(), e);
2259 }
2260
2261 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002262 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002263
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002264 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 TAG, "Performing receive of " + data.intent
2266 + ": app=" + app
2267 + ", appName=" + app.getPackageName()
2268 + ", pkg=" + packageInfo.getPackageName()
2269 + ", comp=" + data.intent.getComponent().toShortString()
2270 + ", dir=" + packageInfo.getAppDir());
2271
Dianne Hackborn21556372010-02-04 16:34:40 -08002272 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002273 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002274 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275 receiver.onReceive(context.getReceiverRestrictedContext(),
2276 data.intent);
2277 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002278 if (DEBUG_BROADCAST) Slog.i(TAG,
2279 "Finishing failed broadcast to " + data.intent.getComponent());
2280 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 if (!mInstrumentation.onException(receiver, e)) {
2282 throw new RuntimeException(
2283 "Unable to start receiver " + component
2284 + ": " + e.toString(), e);
2285 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002286 } finally {
2287 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289
Dianne Hackborne829fef2010-10-26 17:44:01 -07002290 if (receiver.getPendingResult() != null) {
2291 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 }
2293 }
2294
Christopher Tate181fafa2009-05-14 11:12:14 -07002295 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002296 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002297 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002298
2299 // no longer idle; we have backup work to do
2300 unscheduleGcIdler();
2301
2302 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002303 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002304 String packageName = packageInfo.mPackageName;
2305 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002306 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002307 + " already exists");
2308 return;
2309 }
Bob Leee5408332009-09-04 18:31:17 -07002310
Christopher Tate181fafa2009-05-14 11:12:14 -07002311 BackupAgent agent = null;
2312 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002313
Christopher Tate79ec80d2011-06-24 14:58:49 -07002314 // full backup operation but no app-supplied agent? use the default implementation
2315 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2316 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002317 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002318 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002319
Christopher Tate181fafa2009-05-14 11:12:14 -07002320 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002321 IBinder binder = null;
2322 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002323 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2324
Christopher Tated1475e02009-07-09 15:36:17 -07002325 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002326 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002327
2328 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002329 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002330 context.init(packageInfo, null, this);
2331 context.setOuterContext(agent);
2332 agent.attach(context);
2333
2334 agent.onCreate();
2335 binder = agent.onBind();
2336 mBackupAgents.put(packageName, agent);
2337 } catch (Exception e) {
2338 // If this is during restore, fail silently; otherwise go
2339 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002340 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002341 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2342 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002343 throw e;
2344 }
2345 // falling through with 'binder' still null
2346 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002347
2348 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002349 try {
2350 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2351 } catch (RemoteException e) {
2352 // nothing to do.
2353 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002354 } catch (Exception e) {
2355 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002356 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002357 }
2358 }
2359
2360 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002361 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002362 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002363
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002364 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002365 String packageName = packageInfo.mPackageName;
2366 BackupAgent agent = mBackupAgents.get(packageName);
2367 if (agent != null) {
2368 try {
2369 agent.onDestroy();
2370 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002371 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002372 e.printStackTrace();
2373 }
2374 mBackupAgents.remove(packageName);
2375 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002376 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002377 }
2378 }
2379
Romain Guy65b345f2011-07-27 18:51:50 -07002380 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 // If we are getting ready to gc after going to the background, well
2382 // we are back active so skip it.
2383 unscheduleGcIdler();
2384
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002385 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002386 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 Service service = null;
2388 try {
2389 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2390 service = (Service) cl.loadClass(data.info.name).newInstance();
2391 } catch (Exception e) {
2392 if (!mInstrumentation.onException(service, e)) {
2393 throw new RuntimeException(
2394 "Unable to instantiate service " + data.info.name
2395 + ": " + e.toString(), e);
2396 }
2397 }
2398
2399 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002400 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401
Dianne Hackborn21556372010-02-04 16:34:40 -08002402 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 context.init(packageInfo, null, this);
2404
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002405 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 context.setOuterContext(service);
2407 service.attach(context, this, data.info.name, data.token, app,
2408 ActivityManagerNative.getDefault());
2409 service.onCreate();
2410 mServices.put(data.token, service);
2411 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002412 ActivityManagerNative.getDefault().serviceDoneExecuting(
2413 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 } catch (RemoteException e) {
2415 // nothing to do.
2416 }
2417 } catch (Exception e) {
2418 if (!mInstrumentation.onException(service, e)) {
2419 throw new RuntimeException(
2420 "Unable to create service " + data.info.name
2421 + ": " + e.toString(), e);
2422 }
2423 }
2424 }
2425
Romain Guy65b345f2011-07-27 18:51:50 -07002426 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002428 if (DEBUG_SERVICE)
2429 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 if (s != null) {
2431 try {
2432 data.intent.setExtrasClassLoader(s.getClassLoader());
2433 try {
2434 if (!data.rebind) {
2435 IBinder binder = s.onBind(data.intent);
2436 ActivityManagerNative.getDefault().publishService(
2437 data.token, data.intent, binder);
2438 } else {
2439 s.onRebind(data.intent);
2440 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002441 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002443 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 } catch (RemoteException ex) {
2445 }
2446 } catch (Exception e) {
2447 if (!mInstrumentation.onException(s, e)) {
2448 throw new RuntimeException(
2449 "Unable to bind to service " + s
2450 + " with " + data.intent + ": " + e.toString(), e);
2451 }
2452 }
2453 }
2454 }
2455
Romain Guy65b345f2011-07-27 18:51:50 -07002456 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 Service s = mServices.get(data.token);
2458 if (s != null) {
2459 try {
2460 data.intent.setExtrasClassLoader(s.getClassLoader());
2461 boolean doRebind = s.onUnbind(data.intent);
2462 try {
2463 if (doRebind) {
2464 ActivityManagerNative.getDefault().unbindFinished(
2465 data.token, data.intent, doRebind);
2466 } else {
2467 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002468 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 }
2470 } catch (RemoteException ex) {
2471 }
2472 } catch (Exception e) {
2473 if (!mInstrumentation.onException(s, e)) {
2474 throw new RuntimeException(
2475 "Unable to unbind to service " + s
2476 + " with " + data.intent + ": " + e.toString(), e);
2477 }
2478 }
2479 }
2480 }
2481
Dianne Hackborn625ac272010-09-17 18:29:22 -07002482 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002483 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2484 try {
2485 Service s = mServices.get(info.token);
2486 if (s != null) {
2487 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2488 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2489 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002491 } finally {
2492 IoUtils.closeQuietly(info.fd);
2493 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495 }
2496
Dianne Hackborn625ac272010-09-17 18:29:22 -07002497 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002498 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2499 try {
2500 ActivityClientRecord r = mActivities.get(info.token);
2501 if (r != null && r.activity != null) {
2502 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2503 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2504 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002505 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002506 } finally {
2507 IoUtils.closeQuietly(info.fd);
2508 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002509 }
2510 }
2511
Marco Nelissen18cb2872011-11-15 11:19:53 -08002512 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002513 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2514 try {
2515 ProviderClientRecord r = mLocalProviders.get(info.token);
2516 if (r != null && r.mLocalProvider != null) {
2517 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2518 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2519 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002520 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002521 } finally {
2522 IoUtils.closeQuietly(info.fd);
2523 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002524 }
2525 }
2526
Romain Guy65b345f2011-07-27 18:51:50 -07002527 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 Service s = mServices.get(data.token);
2529 if (s != null) {
2530 try {
2531 if (data.args != null) {
2532 data.args.setExtrasClassLoader(s.getClassLoader());
2533 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002534 int res;
2535 if (!data.taskRemoved) {
2536 res = s.onStartCommand(data.args, data.flags, data.startId);
2537 } else {
2538 s.onTaskRemoved(data.args);
2539 res = Service.START_TASK_REMOVED_COMPLETE;
2540 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002541
2542 QueuedWork.waitToFinish();
2543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002545 ActivityManagerNative.getDefault().serviceDoneExecuting(
2546 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002547 } catch (RemoteException e) {
2548 // nothing to do.
2549 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002550 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 } catch (Exception e) {
2552 if (!mInstrumentation.onException(s, e)) {
2553 throw new RuntimeException(
2554 "Unable to start service " + s
2555 + " with " + data.args + ": " + e.toString(), e);
2556 }
2557 }
2558 }
2559 }
2560
Romain Guy65b345f2011-07-27 18:51:50 -07002561 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 Service s = mServices.remove(token);
2563 if (s != null) {
2564 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002565 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 s.onDestroy();
2567 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002568 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002570 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002572
2573 QueuedWork.waitToFinish();
2574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002576 ActivityManagerNative.getDefault().serviceDoneExecuting(
2577 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 } catch (RemoteException e) {
2579 // nothing to do.
2580 }
2581 } catch (Exception e) {
2582 if (!mInstrumentation.onException(s, e)) {
2583 throw new RuntimeException(
2584 "Unable to stop service " + s
2585 + ": " + e.toString(), e);
2586 }
2587 }
2588 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002589 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 }
2591
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002592 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002594 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002595 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 + " finished=" + r.activity.mFinished);
2597 if (r != null && !r.activity.mFinished) {
2598 if (clearHide) {
2599 r.hideForNow = false;
2600 r.activity.mStartedActivity = false;
2601 }
2602 try {
2603 if (r.pendingIntents != null) {
2604 deliverNewIntents(r, r.pendingIntents);
2605 r.pendingIntents = null;
2606 }
2607 if (r.pendingResults != null) {
2608 deliverResults(r, r.pendingResults);
2609 r.pendingResults = null;
2610 }
2611 r.activity.performResume();
2612
Bob Leee5408332009-09-04 18:31:17 -07002613 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002615
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 r.paused = false;
2617 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 r.state = null;
2619 } catch (Exception e) {
2620 if (!mInstrumentation.onException(r.activity, e)) {
2621 throw new RuntimeException(
2622 "Unable to resume activity "
2623 + r.intent.getComponent().toShortString()
2624 + ": " + e.toString(), e);
2625 }
2626 }
2627 }
2628 return r;
2629 }
2630
Romain Guya998dff2012-03-23 18:58:36 -07002631 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002632 if (r.mPendingRemoveWindow != null) {
2633 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2634 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2635 if (wtoken != null) {
2636 WindowManagerImpl.getDefault().closeAll(wtoken,
2637 r.activity.getClass().getName(), "Activity");
2638 }
2639 }
2640 r.mPendingRemoveWindow = null;
2641 r.mPendingRemoveWindowManager = null;
2642 }
2643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2645 // If we are getting ready to gc after going to the background, well
2646 // we are back active so skip it.
2647 unscheduleGcIdler();
2648
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002649 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650
2651 if (r != null) {
2652 final Activity a = r.activity;
2653
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002654 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 TAG, "Resume " + r + " started activity: " +
2656 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2657 + ", finished: " + a.mFinished);
2658
2659 final int forwardBit = isForward ?
2660 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002662 // If the window hasn't yet been added to the window manager,
2663 // and this guy didn't finish itself or start another activity,
2664 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002665 boolean willBeVisible = !a.mStartedActivity;
2666 if (!willBeVisible) {
2667 try {
2668 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2669 a.getActivityToken());
2670 } catch (RemoteException e) {
2671 }
2672 }
2673 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 r.window = r.activity.getWindow();
2675 View decor = r.window.getDecorView();
2676 decor.setVisibility(View.INVISIBLE);
2677 ViewManager wm = a.getWindowManager();
2678 WindowManager.LayoutParams l = r.window.getAttributes();
2679 a.mDecor = decor;
2680 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2681 l.softInputMode |= forwardBit;
2682 if (a.mVisibleFromClient) {
2683 a.mWindowAdded = true;
2684 wm.addView(decor, l);
2685 }
2686
2687 // If the window has already been added, but during resume
2688 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002689 // window visible.
2690 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002691 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 TAG, "Launch " + r + " mStartedActivity set");
2693 r.hideForNow = true;
2694 }
2695
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002696 // Get rid of anything left hanging around.
2697 cleanUpPendingRemoveWindows(r);
2698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 // The window is now visible if it has been added, we are not
2700 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002701 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002702 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002704 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002705 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002707 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 r.newConfig = null;
2709 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002710 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 + isForward);
2712 WindowManager.LayoutParams l = r.window.getAttributes();
2713 if ((l.softInputMode
2714 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2715 != forwardBit) {
2716 l.softInputMode = (l.softInputMode
2717 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2718 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002719 if (r.activity.mVisibleFromClient) {
2720 ViewManager wm = a.getWindowManager();
2721 View decor = r.window.getDecorView();
2722 wm.updateViewLayout(decor, l);
2723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 }
2725 r.activity.mVisibleFromServer = true;
2726 mNumVisibleActivities++;
2727 if (r.activity.mVisibleFromClient) {
2728 r.activity.makeVisible();
2729 }
2730 }
2731
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002732 if (!r.onlyLocalRequest) {
2733 r.nextIdle = mNewActivities;
2734 mNewActivities = r;
2735 if (localLOGV) Slog.v(
2736 TAG, "Scheduling idle handler for " + r);
2737 Looper.myQueue().addIdleHandler(new Idler());
2738 }
2739 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740
2741 } else {
2742 // If an exception was thrown when trying to resume, then
2743 // just end this activity.
2744 try {
2745 ActivityManagerNative.getDefault()
2746 .finishActivity(token, Activity.RESULT_CANCELED, null);
2747 } catch (RemoteException ex) {
2748 }
2749 }
2750 }
2751
2752 private int mThumbnailWidth = -1;
2753 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002754 private Bitmap mAvailThumbnailBitmap = null;
2755 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756
Romain Guy65b345f2011-07-27 18:51:50 -07002757 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002758 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002760 if (thumbnail == null) {
2761 int w = mThumbnailWidth;
2762 int h;
2763 if (w < 0) {
2764 Resources res = r.activity.getResources();
2765 mThumbnailHeight = h =
2766 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002768 mThumbnailWidth = w =
2769 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2770 } else {
2771 h = mThumbnailHeight;
2772 }
2773
2774 // On platforms where we don't want thumbnails, set dims to (0,0)
2775 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002776 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2777 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002778 thumbnail.eraseColor(0);
2779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002782 if (thumbnail != null) {
2783 Canvas cv = mThumbnailCanvas;
2784 if (cv == null) {
2785 mThumbnailCanvas = cv = new Canvas();
2786 }
2787
2788 cv.setBitmap(thumbnail);
2789 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2790 mAvailThumbnailBitmap = thumbnail;
2791 thumbnail = null;
2792 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002793 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 } catch (Exception e) {
2797 if (!mInstrumentation.onException(r.activity, e)) {
2798 throw new RuntimeException(
2799 "Unable to create thumbnail of "
2800 + r.intent.getComponent().toShortString()
2801 + ": " + e.toString(), e);
2802 }
2803 thumbnail = null;
2804 }
2805
2806 return thumbnail;
2807 }
2808
Romain Guy65b345f2011-07-27 18:51:50 -07002809 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002810 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002811 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002813 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 if (userLeaving) {
2815 performUserLeavingActivity(r);
2816 }
Bob Leee5408332009-09-04 18:31:17 -07002817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002819 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002821 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002822 if (r.isPreHoneycomb()) {
2823 QueuedWork.waitToFinish();
2824 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 // Tell the activity manager we have paused.
2827 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002828 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829 } catch (RemoteException ex) {
2830 }
2831 }
2832 }
2833
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002834 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 mInstrumentation.callActivityOnUserLeaving(r.activity);
2836 }
2837
2838 final Bundle performPauseActivity(IBinder token, boolean finished,
2839 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002840 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002841 return r != null ? performPauseActivity(r, finished, saveState) : null;
2842 }
2843
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002844 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002845 boolean saveState) {
2846 if (r.paused) {
2847 if (r.activity.mFinished) {
2848 // If we are finishing, we won't call onResume() in certain cases.
2849 // So here we likewise don't want to call onPause() if the activity
2850 // isn't resumed.
2851 return null;
2852 }
2853 RuntimeException e = new RuntimeException(
2854 "Performing pause of activity that is not resumed: "
2855 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002856 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857 }
2858 Bundle state = null;
2859 if (finished) {
2860 r.activity.mFinished = true;
2861 }
2862 try {
2863 // Next have the activity save its current state and managed dialogs...
2864 if (!r.activity.mFinished && saveState) {
2865 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002866 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2868 r.state = state;
2869 }
2870 // Now we are idle.
2871 r.activity.mCalled = false;
2872 mInstrumentation.callActivityOnPause(r.activity);
2873 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2874 if (!r.activity.mCalled) {
2875 throw new SuperNotCalledException(
2876 "Activity " + r.intent.getComponent().toShortString() +
2877 " did not call through to super.onPause()");
2878 }
2879
2880 } catch (SuperNotCalledException e) {
2881 throw e;
2882
2883 } catch (Exception e) {
2884 if (!mInstrumentation.onException(r.activity, e)) {
2885 throw new RuntimeException(
2886 "Unable to pause activity "
2887 + r.intent.getComponent().toShortString()
2888 + ": " + e.toString(), e);
2889 }
2890 }
2891 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002892
2893 // Notify any outstanding on paused listeners
2894 ArrayList<OnActivityPausedListener> listeners;
2895 synchronized (mOnPauseListeners) {
2896 listeners = mOnPauseListeners.remove(r.activity);
2897 }
2898 int size = (listeners != null ? listeners.size() : 0);
2899 for (int i = 0; i < size; i++) {
2900 listeners.get(i).onPaused(r.activity);
2901 }
2902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 return state;
2904 }
2905
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002906 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002907 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002908 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002909 }
2910
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002911 private static class StopInfo implements Runnable {
2912 ActivityClientRecord activity;
2913 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002914 Bitmap thumbnail;
2915 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002916
2917 @Override public void run() {
2918 // Tell activity manager we have been stopped.
2919 try {
2920 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2921 ActivityManagerNative.getDefault().activityStopped(
2922 activity.token, state, thumbnail, description);
2923 } catch (RemoteException ex) {
2924 }
2925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002928 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002929 public final IActivityManager.ContentProviderHolder holder;
2930 public final ProviderClientRecord client;
2931 public int stableCount;
2932 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002933
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002934 // When this is set, the stable and unstable ref counts are 0 and
2935 // we have a pending operation scheduled to remove the ref count
2936 // from the activity manager. On the activity manager we are still
2937 // holding an unstable ref, though it is not reflected in the counts
2938 // here.
2939 public boolean removePending;
2940
2941 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2942 ProviderClientRecord inClient, int sCount, int uCount) {
2943 holder = inHolder;
2944 client = inClient;
2945 stableCount = sCount;
2946 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
2948 }
2949
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002950 /**
2951 * Core implementation of stopping an activity. Note this is a little
2952 * tricky because the server's meaning of stop is slightly different
2953 * than our client -- for the server, stop means to save state and give
2954 * it the result when it is done, but the window may still be visible.
2955 * For the client, we want to call onStop()/onStart() to indicate when
2956 * the activity's UI visibillity changes.
2957 */
Romain Guy65b345f2011-07-27 18:51:50 -07002958 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002959 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002960 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002961 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 if (r != null) {
2963 if (!keepShown && r.stopped) {
2964 if (r.activity.mFinished) {
2965 // If we are finishing, we won't call onResume() in certain
2966 // cases. So here we likewise don't want to call onStop()
2967 // if the activity isn't resumed.
2968 return;
2969 }
2970 RuntimeException e = new RuntimeException(
2971 "Performing stop of activity that is not resumed: "
2972 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002973 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
2975
2976 if (info != null) {
2977 try {
2978 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002979 // For now, don't create the thumbnail here; we are
2980 // doing that by doing a screen snapshot.
2981 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 info.description = r.activity.onCreateDescription();
2983 } catch (Exception e) {
2984 if (!mInstrumentation.onException(r.activity, e)) {
2985 throw new RuntimeException(
2986 "Unable to save state of activity "
2987 + r.intent.getComponent().toShortString()
2988 + ": " + e.toString(), e);
2989 }
2990 }
2991 }
2992
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002993 // Next have the activity save its current state and managed dialogs...
2994 if (!r.activity.mFinished && saveState) {
2995 if (r.state == null) {
2996 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002997 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002998 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2999 r.state = state;
3000 } else {
3001 state = r.state;
3002 }
3003 }
3004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003005 if (!keepShown) {
3006 try {
3007 // Now we are idle.
3008 r.activity.performStop();
3009 } catch (Exception e) {
3010 if (!mInstrumentation.onException(r.activity, e)) {
3011 throw new RuntimeException(
3012 "Unable to stop activity "
3013 + r.intent.getComponent().toShortString()
3014 + ": " + e.toString(), e);
3015 }
3016 }
3017 r.stopped = true;
3018 }
3019
3020 r.paused = true;
3021 }
3022 }
3023
Romain Guy65b345f2011-07-27 18:51:50 -07003024 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 View v = r.activity.mDecor;
3026 if (v != null) {
3027 if (show) {
3028 if (!r.activity.mVisibleFromServer) {
3029 r.activity.mVisibleFromServer = true;
3030 mNumVisibleActivities++;
3031 if (r.activity.mVisibleFromClient) {
3032 r.activity.makeVisible();
3033 }
3034 }
3035 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003036 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003037 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003039 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 r.newConfig = null;
3041 }
3042 } else {
3043 if (r.activity.mVisibleFromServer) {
3044 r.activity.mVisibleFromServer = false;
3045 mNumVisibleActivities--;
3046 v.setVisibility(View.INVISIBLE);
3047 }
3048 }
3049 }
3050 }
3051
Romain Guy65b345f2011-07-27 18:51:50 -07003052 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003053 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 r.activity.mConfigChangeFlags |= configChanges;
3055
3056 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003057 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003059 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 TAG, "Finishing stop of " + r + ": show=" + show
3061 + " win=" + r.window);
3062
3063 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003064
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003065 // Make sure any pending writes are now committed.
3066 if (!r.isPreHoneycomb()) {
3067 QueuedWork.waitToFinish();
3068 }
3069
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003070 // Schedule the call to tell the activity manager we have
3071 // stopped. We don't do this immediately, because we want to
3072 // have a chance for any other pending work (in particular memory
3073 // trim requests) to complete before you tell the activity
3074 // manager to proceed and allow us to go fully into the background.
3075 info.activity = r;
3076 info.state = r.state;
3077 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079
3080 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003081 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 if (r.stopped) {
3083 r.activity.performRestart();
3084 r.stopped = false;
3085 }
3086 }
3087
Romain Guy65b345f2011-07-27 18:51:50 -07003088 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003089 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003090
3091 if (r == null) {
3092 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3093 return;
3094 }
3095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003097 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 } else if (show && r.stopped) {
3099 // If we are getting ready to gc after going to the background, well
3100 // we are back active so skip it.
3101 unscheduleGcIdler();
3102
3103 r.activity.performRestart();
3104 r.stopped = false;
3105 }
3106 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003107 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 TAG, "Handle window " + r + " visibility: " + show);
3109 updateVisibility(r, show);
3110 }
3111 }
3112
Romain Guy65b345f2011-07-27 18:51:50 -07003113 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003114 ActivityClientRecord r = mActivities.get(token);
3115
3116 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003117 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003118 return;
3119 }
3120
3121 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003122 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003123 try {
3124 // Now we are idle.
3125 r.activity.performStop();
3126 } catch (Exception e) {
3127 if (!mInstrumentation.onException(r.activity, e)) {
3128 throw new RuntimeException(
3129 "Unable to stop activity "
3130 + r.intent.getComponent().toShortString()
3131 + ": " + e.toString(), e);
3132 }
3133 }
3134 r.stopped = true;
3135 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003136
3137 // Make sure any pending writes are now committed.
3138 if (!r.isPreHoneycomb()) {
3139 QueuedWork.waitToFinish();
3140 }
3141
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003142 // Tell activity manager we slept.
3143 try {
3144 ActivityManagerNative.getDefault().activitySlept(r.token);
3145 } catch (RemoteException ex) {
3146 }
3147 } else {
3148 if (r.stopped && r.activity.mVisibleFromServer) {
3149 r.activity.performRestart();
3150 r.stopped = false;
3151 }
3152 }
3153 }
3154
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003155 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003156 synchronized (mPackages) {
3157 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003158 }
3159 }
3160
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003161 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3162 LoadedApk apk = peekPackageInfo(data.pkg, false);
3163 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003164 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003165 }
3166 apk = peekPackageInfo(data.pkg, true);
3167 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003168 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003169 }
3170 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003171 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003172 }
3173
Romain Guy65b345f2011-07-27 18:51:50 -07003174 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 final int N = results.size();
3176 for (int i=0; i<N; i++) {
3177 ResultInfo ri = results.get(i);
3178 try {
3179 if (ri.mData != null) {
3180 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3181 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003182 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003183 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 r.activity.dispatchActivityResult(ri.mResultWho,
3185 ri.mRequestCode, ri.mResultCode, ri.mData);
3186 } catch (Exception e) {
3187 if (!mInstrumentation.onException(r.activity, e)) {
3188 throw new RuntimeException(
3189 "Failure delivering result " + ri + " to activity "
3190 + r.intent.getComponent().toShortString()
3191 + ": " + e.toString(), e);
3192 }
3193 }
3194 }
3195 }
3196
Romain Guy65b345f2011-07-27 18:51:50 -07003197 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003198 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003199 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 if (r != null) {
3201 final boolean resumed = !r.paused;
3202 if (!r.activity.mFinished && r.activity.mDecor != null
3203 && r.hideForNow && resumed) {
3204 // We had hidden the activity because it started another
3205 // one... we have gotten a result back and we are not
3206 // paused, so make sure our window is visible.
3207 updateVisibility(r, true);
3208 }
3209 if (resumed) {
3210 try {
3211 // Now we are idle.
3212 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003213 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 mInstrumentation.callActivityOnPause(r.activity);
3215 if (!r.activity.mCalled) {
3216 throw new SuperNotCalledException(
3217 "Activity " + r.intent.getComponent().toShortString()
3218 + " did not call through to super.onPause()");
3219 }
3220 } catch (SuperNotCalledException e) {
3221 throw e;
3222 } catch (Exception e) {
3223 if (!mInstrumentation.onException(r.activity, e)) {
3224 throw new RuntimeException(
3225 "Unable to pause activity "
3226 + r.intent.getComponent().toShortString()
3227 + ": " + e.toString(), e);
3228 }
3229 }
3230 }
3231 deliverResults(r, res.results);
3232 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003233 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003234 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
3236 }
3237 }
3238
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003239 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 return performDestroyActivity(token, finishing, 0, false);
3241 }
3242
Romain Guy65b345f2011-07-27 18:51:50 -07003243 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003245 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003246 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003247 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003249 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003250 r.activity.mConfigChangeFlags |= configChanges;
3251 if (finishing) {
3252 r.activity.mFinished = true;
3253 }
3254 if (!r.paused) {
3255 try {
3256 r.activity.mCalled = false;
3257 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003258 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 r.activity.getComponentName().getClassName());
3260 if (!r.activity.mCalled) {
3261 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003262 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 + " did not call through to super.onPause()");
3264 }
3265 } catch (SuperNotCalledException e) {
3266 throw e;
3267 } catch (Exception e) {
3268 if (!mInstrumentation.onException(r.activity, e)) {
3269 throw new RuntimeException(
3270 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003271 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 + ": " + e.toString(), e);
3273 }
3274 }
3275 r.paused = true;
3276 }
3277 if (!r.stopped) {
3278 try {
3279 r.activity.performStop();
3280 } catch (SuperNotCalledException e) {
3281 throw e;
3282 } catch (Exception e) {
3283 if (!mInstrumentation.onException(r.activity, e)) {
3284 throw new RuntimeException(
3285 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003286 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 + ": " + e.toString(), e);
3288 }
3289 }
3290 r.stopped = true;
3291 }
3292 if (getNonConfigInstance) {
3293 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003294 r.lastNonConfigurationInstances
3295 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 } catch (Exception e) {
3297 if (!mInstrumentation.onException(r.activity, e)) {
3298 throw new RuntimeException(
3299 "Unable to retain activity "
3300 + r.intent.getComponent().toShortString()
3301 + ": " + e.toString(), e);
3302 }
3303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 }
3305 try {
3306 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003307 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 if (!r.activity.mCalled) {
3309 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003310 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 " did not call through to super.onDestroy()");
3312 }
3313 if (r.window != null) {
3314 r.window.closeAllPanels();
3315 }
3316 } catch (SuperNotCalledException e) {
3317 throw e;
3318 } catch (Exception e) {
3319 if (!mInstrumentation.onException(r.activity, e)) {
3320 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003321 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3322 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 }
3324 }
3325 }
3326 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003327 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 return r;
3329 }
3330
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003331 private static String safeToComponentShortString(Intent intent) {
3332 ComponentName component = intent.getComponent();
3333 return component == null ? "[Unknown]" : component.toShortString();
3334 }
3335
Romain Guy65b345f2011-07-27 18:51:50 -07003336 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003338 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 configChanges, getNonConfigInstance);
3340 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003341 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 WindowManager wm = r.activity.getWindowManager();
3343 View v = r.activity.mDecor;
3344 if (v != null) {
3345 if (r.activity.mVisibleFromServer) {
3346 mNumVisibleActivities--;
3347 }
3348 IBinder wtoken = v.getWindowToken();
3349 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003350 if (r.onlyLocalRequest) {
3351 // Hold off on removing this until the new activity's
3352 // window is being added.
3353 r.mPendingRemoveWindow = v;
3354 r.mPendingRemoveWindowManager = wm;
3355 } else {
3356 wm.removeViewImmediate(v);
3357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003359 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 WindowManagerImpl.getDefault().closeAll(wtoken,
3361 r.activity.getClass().getName(), "Activity");
3362 }
3363 r.activity.mDecor = null;
3364 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003365 if (r.mPendingRemoveWindow == null) {
3366 // If we are delaying the removal of the activity window, then
3367 // we can't clean up all windows here. Note that we can't do
3368 // so later either, which means any windows that aren't closed
3369 // by the app will leak. Well we try to warning them a lot
3370 // about leaking windows, because that is a bug, so if they are
3371 // using this recreate facility then they get to live with leaks.
3372 WindowManagerImpl.getDefault().closeAll(token,
3373 r.activity.getClass().getName(), "Activity");
3374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375
3376 // Mocked out contexts won't be participating in the normal
3377 // process lifecycle, but if we're running with a proper
3378 // ApplicationContext we need to have it tear down things
3379 // cleanly.
3380 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003381 if (c instanceof ContextImpl) {
3382 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 r.activity.getClass().getName(), "Activity");
3384 }
3385 }
3386 if (finishing) {
3387 try {
3388 ActivityManagerNative.getDefault().activityDestroyed(token);
3389 } catch (RemoteException ex) {
3390 // If the system process has died, it's game over for everyone.
3391 }
3392 }
3393 }
3394
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003395 public final void requestRelaunchActivity(IBinder token,
3396 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3397 int configChanges, boolean notResumed, Configuration config,
3398 boolean fromServer) {
3399 ActivityClientRecord target = null;
3400
3401 synchronized (mPackages) {
3402 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3403 ActivityClientRecord r = mRelaunchingActivities.get(i);
3404 if (r.token == token) {
3405 target = r;
3406 if (pendingResults != null) {
3407 if (r.pendingResults != null) {
3408 r.pendingResults.addAll(pendingResults);
3409 } else {
3410 r.pendingResults = pendingResults;
3411 }
3412 }
3413 if (pendingNewIntents != null) {
3414 if (r.pendingIntents != null) {
3415 r.pendingIntents.addAll(pendingNewIntents);
3416 } else {
3417 r.pendingIntents = pendingNewIntents;
3418 }
3419 }
3420 break;
3421 }
3422 }
3423
3424 if (target == null) {
3425 target = new ActivityClientRecord();
3426 target.token = token;
3427 target.pendingResults = pendingResults;
3428 target.pendingIntents = pendingNewIntents;
3429 if (!fromServer) {
3430 ActivityClientRecord existing = mActivities.get(token);
3431 if (existing != null) {
3432 target.startsNotResumed = existing.paused;
3433 }
3434 target.onlyLocalRequest = true;
3435 }
3436 mRelaunchingActivities.add(target);
3437 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3438 }
3439
3440 if (fromServer) {
3441 target.startsNotResumed = notResumed;
3442 target.onlyLocalRequest = false;
3443 }
3444 if (config != null) {
3445 target.createdConfig = config;
3446 }
3447 target.pendingConfigChanges |= configChanges;
3448 }
3449 }
3450
Romain Guy65b345f2011-07-27 18:51:50 -07003451 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 // If we are getting ready to gc after going to the background, well
3453 // we are back active so skip it.
3454 unscheduleGcIdler();
3455
3456 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003457 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 // First: make sure we have the most recent configuration and most
3460 // recent version of the activity, or skip it if some previous call
3461 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003462 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 int N = mRelaunchingActivities.size();
3464 IBinder token = tmp.token;
3465 tmp = null;
3466 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003467 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 if (r.token == token) {
3469 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003470 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 mRelaunchingActivities.remove(i);
3472 i--;
3473 N--;
3474 }
3475 }
Bob Leee5408332009-09-04 18:31:17 -07003476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003478 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 return;
3480 }
Bob Leee5408332009-09-04 18:31:17 -07003481
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003482 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3483 + tmp.token + " with configChanges=0x"
3484 + Integer.toHexString(configChanges));
3485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if (mPendingConfiguration != null) {
3487 changedConfig = mPendingConfiguration;
3488 mPendingConfiguration = null;
3489 }
3490 }
Bob Leee5408332009-09-04 18:31:17 -07003491
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003492 if (tmp.createdConfig != null) {
3493 // If the activity manager is passing us its current config,
3494 // assume that is really what we want regardless of what we
3495 // may have pending.
3496 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003497 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3498 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3499 if (changedConfig == null
3500 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3501 changedConfig = tmp.createdConfig;
3502 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003503 }
3504 }
3505
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003506 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003507 + tmp.token + ": changedConfig=" + changedConfig);
3508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 // If there was a pending configuration change, execute it first.
3510 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003511 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003512 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003513 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 }
Bob Leee5408332009-09-04 18:31:17 -07003515
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003516 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003517 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 if (r == null) {
3519 return;
3520 }
Bob Leee5408332009-09-04 18:31:17 -07003521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003523 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003524 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003525
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003526 r.activity.mChangingConfigurations = true;
3527
Dianne Hackborne2b04802010-12-09 09:24:55 -08003528 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003529 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003530 performPauseActivity(r.token, false, r.isPreHoneycomb());
3531 }
3532 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3533 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003534 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003535 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
Bob Leee5408332009-09-04 18:31:17 -07003537
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 r.activity = null;
3541 r.window = null;
3542 r.hideForNow = false;
3543 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003544 // Merge any pending results and pending intents; don't just replace them
3545 if (tmp.pendingResults != null) {
3546 if (r.pendingResults == null) {
3547 r.pendingResults = tmp.pendingResults;
3548 } else {
3549 r.pendingResults.addAll(tmp.pendingResults);
3550 }
3551 }
3552 if (tmp.pendingIntents != null) {
3553 if (r.pendingIntents == null) {
3554 r.pendingIntents = tmp.pendingIntents;
3555 } else {
3556 r.pendingIntents.addAll(tmp.pendingIntents);
3557 }
3558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003560
Christopher Tateb70f3df2009-04-07 16:07:59 -07003561 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 }
3563
Romain Guy65b345f2011-07-27 18:51:50 -07003564 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003565 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 Bitmap thumbnail = createThumbnailBitmap(r);
3567 CharSequence description = null;
3568 try {
3569 description = r.activity.onCreateDescription();
3570 } catch (Exception e) {
3571 if (!mInstrumentation.onException(r.activity, e)) {
3572 throw new RuntimeException(
3573 "Unable to create description of activity "
3574 + r.intent.getComponent().toShortString()
3575 + ": " + e.toString(), e);
3576 }
3577 }
3578 //System.out.println("Reporting top thumbnail " + thumbnail);
3579 try {
3580 ActivityManagerNative.getDefault().reportThumbnail(
3581 token, thumbnail, description);
3582 } catch (RemoteException ex) {
3583 }
3584 }
3585
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003586 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003588 ArrayList<ComponentCallbacks2> callbacks
3589 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003592 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 Activity a = ar.activity;
3594 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003595 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3596 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003597 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 // If the activity is currently resumed, its configuration
3599 // needs to change right now.
3600 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003601 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 // Otherwise, we will tell it about the change
3603 // the next time it is resumed or shown. Note that
3604 // the activity manager may, before then, decide the
3605 // activity needs to be destroyed to handle its new
3606 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003607 if (DEBUG_CONFIGURATION) {
3608 Slog.v(TAG, "Setting activity "
3609 + ar.activityInfo.name + " newConfig=" + thisConfig);
3610 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003611 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 }
3613 }
3614 }
3615 }
3616 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003617 for (Service service : mServices.values()) {
3618 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 }
3620 }
3621 synchronized (mProviderMap) {
3622 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003623 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3624 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 }
3626 }
3627 }
3628 final int N = mAllApplications.size();
3629 for (int i=0; i<N; i++) {
3630 callbacks.add(mAllApplications.get(i));
3631 }
Bob Leee5408332009-09-04 18:31:17 -07003632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 return callbacks;
3634 }
Bob Leee5408332009-09-04 18:31:17 -07003635
Romain Guya998dff2012-03-23 18:58:36 -07003636 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003638 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 // we check the runtime type and act accordingly.
3640 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3641 if (activity != null) {
3642 activity.mCalled = false;
3643 }
Bob Leee5408332009-09-04 18:31:17 -07003644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 boolean shouldChangeConfig = false;
3646 if ((activity == null) || (activity.mCurrentConfig == null)) {
3647 shouldChangeConfig = true;
3648 } else {
Bob Leee5408332009-09-04 18:31:17 -07003649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 // If the new config is the same as the config this Activity
3651 // is already running with then don't bother calling
3652 // onConfigurationChanged
3653 int diff = activity.mCurrentConfig.diff(config);
3654 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 // If this activity doesn't handle any of the config changes
3656 // then don't bother calling onConfigurationChanged as we're
3657 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003658 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 shouldChangeConfig = true;
3660 }
3661 }
3662 }
Bob Leee5408332009-09-04 18:31:17 -07003663
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003664 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003665 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 if (shouldChangeConfig) {
3667 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 if (activity != null) {
3670 if (!activity.mCalled) {
3671 throw new SuperNotCalledException(
3672 "Activity " + activity.getLocalClassName() +
3673 " did not call through to super.onConfigurationChanged()");
3674 }
3675 activity.mConfigChangeFlags = 0;
3676 activity.mCurrentConfig = new Configuration(config);
3677 }
3678 }
3679 }
3680
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003681 public final void applyConfigurationToResources(Configuration config) {
3682 synchronized (mPackages) {
3683 applyConfigurationToResourcesLocked(config, null);
3684 }
3685 }
3686
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003687 final boolean applyConfigurationToResourcesLocked(Configuration config,
3688 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003689 if (mResConfiguration == null) {
3690 mResConfiguration = new Configuration();
3691 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003692 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003693 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003694 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003695 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003696 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003697 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003698 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003699
3700 if (compat != null && (mResCompatibilityInfo == null ||
3701 !mResCompatibilityInfo.equals(compat))) {
3702 mResCompatibilityInfo = compat;
3703 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3704 | ActivityInfo.CONFIG_SCREEN_SIZE
3705 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3706 }
Bob Leee5408332009-09-04 18:31:17 -07003707
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003708 // set it for java, this also affects newly created Resources
3709 if (config.locale != null) {
3710 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 }
Bob Leee5408332009-09-04 18:31:17 -07003712
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003713 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003714
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003715 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003716 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003717
3718 Configuration tmpConfig = null;
3719
3720 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3721 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003722 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003723 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3724 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003725 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003726 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003727 + r + " config to: " + config);
Dianne Hackborn756220b2012-08-14 16:45:30 -07003728 Configuration override = entry.getKey().mOverrideConfiguration;
3729 if (override != null) {
3730 if (tmpConfig == null) {
3731 tmpConfig = new Configuration();
3732 }
3733 tmpConfig.setTo(config);
3734 tmpConfig.updateFrom(override);
3735 r.updateConfiguration(tmpConfig, dm, compat);
3736 } else {
3737 r.updateConfiguration(config, dm, compat);
3738 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003739 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003740 // + " " + r + ": " + r.getConfiguration());
3741 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003742 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003743 it.remove();
3744 }
3745 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003746
3747 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003748 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003749
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003750 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003751 Configuration config = mConfiguration;
3752 if (mCompatConfiguration == null) {
3753 mCompatConfiguration = new Configuration();
3754 }
3755 mCompatConfiguration.setTo(mConfiguration);
3756 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003757 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003758 config = mCompatConfiguration;
3759 }
3760 return config;
3761 }
3762
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003763 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003764
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003765 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003766 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003767
3768 synchronized (mPackages) {
3769 if (mPendingConfiguration != null) {
3770 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3771 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003772 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003773 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003774 }
3775 mPendingConfiguration = null;
3776 }
3777
3778 if (config == null) {
3779 return;
3780 }
3781
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003782 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003783 + config);
3784
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003785 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 if (mConfiguration == null) {
3788 mConfiguration = new Configuration();
3789 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003790 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003791 return;
3792 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003793 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003795 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 callbacks = collectComponentCallbacksLocked(false, config);
3797 }
Romain Guy65b345f2011-07-27 18:51:50 -07003798
3799 // Cleanup hardware accelerated stuff
3800 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003801
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003802 freeTextLayoutCachesIfNeeded(configDiff);
3803
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003804 if (callbacks != null) {
3805 final int N = callbacks.size();
3806 for (int i=0; i<N; i++) {
3807 performConfigurationChanged(callbacks.get(i), config);
3808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 }
3810 }
3811
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003812 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3813 if (configDiff != 0) {
3814 // Ask text layout engine to free its caches if there is a locale change
3815 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3816 if (hasLocaleConfigChange) {
3817 Canvas.freeTextLayoutCaches();
3818 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3819 }
3820 }
3821 }
3822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003824 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 if (r == null || r.activity == null) {
3826 return;
3827 }
Bob Leee5408332009-09-04 18:31:17 -07003828
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003829 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003830 + r.activityInfo.name);
3831
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003832 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003833
3834 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 }
3836
Romain Guy7eabe552011-07-21 14:56:34 -07003837 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003838 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003839 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003840 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003841 default:
3842 mProfiler.setProfiler(pcd.path, pcd.fd);
3843 mProfiler.autoStopProfiler = false;
3844 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003845 break;
3846 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003847 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003848 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003849 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003850 } finally {
3851 try {
3852 pcd.fd.close();
3853 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003854 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003855 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003856 }
3857 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003858 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003859 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003860 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003861 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003862 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003863 }
3864 }
Bob Leee5408332009-09-04 18:31:17 -07003865
Romain Guya998dff2012-03-23 18:58:36 -07003866 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003867 if (managed) {
3868 try {
3869 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3870 } catch (IOException e) {
3871 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3872 + " -- can the process access this path?");
3873 } finally {
3874 try {
3875 dhd.fd.close();
3876 } catch (IOException e) {
3877 Slog.w(TAG, "Failure closing profile fd", e);
3878 }
3879 }
3880 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003881 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003882 }
3883 }
3884
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003885 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3886 boolean hasPkgInfo = false;
3887 if (packages != null) {
3888 for (int i=packages.length-1; i>=0; i--) {
3889 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3890 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003891 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003892 ref = mPackages.get(packages[i]);
3893 if (ref != null && ref.get() != null) {
3894 hasPkgInfo = true;
3895 } else {
3896 ref = mResourcePackages.get(packages[i]);
3897 if (ref != null && ref.get() != null) {
3898 hasPkgInfo = true;
3899 }
3900 }
3901 }
3902 mPackages.remove(packages[i]);
3903 mResourcePackages.remove(packages[i]);
3904 }
3905 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003906 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003907 hasPkgInfo);
3908 }
3909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003911 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003913 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 callbacks = collectComponentCallbacksLocked(true, null);
3915 }
Bob Leee5408332009-09-04 18:31:17 -07003916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 final int N = callbacks.size();
3918 for (int i=0; i<N; i++) {
3919 callbacks.get(i).onLowMemory();
3920 }
3921
Chris Tatece229052009-03-25 16:44:52 -07003922 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3923 if (Process.myUid() != Process.SYSTEM_UID) {
3924 int sqliteReleased = SQLiteDatabase.releaseMemory();
3925 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3926 }
Bob Leee5408332009-09-04 18:31:17 -07003927
Mike Reedcaf0df12009-04-27 14:32:05 -04003928 // Ask graphics to free up as much as possible (font/image caches)
3929 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003930
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003931 // Ask text layout engine to free also as much as possible
3932 Canvas.freeTextLayoutCaches();
3933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 BinderInternal.forceGc("mem");
3935 }
3936
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003937 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003938 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003939
Romain Guy19f86e82012-04-23 15:19:07 -07003940 final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3941 windowManager.startTrimMemory(level);
3942
3943 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003944 synchronized (mPackages) {
3945 callbacks = collectComponentCallbacksLocked(true, null);
3946 }
3947
3948 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003949 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003950 callbacks.get(i).onTrimMemory(level);
3951 }
Romain Guy19f86e82012-04-23 15:19:07 -07003952
3953 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003954 }
3955
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003956 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003957 if (Process.isIsolated()) {
3958 // Isolated processes aren't going to do UI.
3959 return;
3960 }
Romain Guya9582652011-11-10 14:20:10 -08003961 try {
3962 int uid = Process.myUid();
3963 String[] packages = getPackageManager().getPackagesForUid(uid);
3964
3965 // If there are several packages in this application we won't
3966 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003967 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003968 HardwareRenderer.setupDiskCache(cacheDir);
3969 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003970 }
3971 } catch (RemoteException e) {
3972 // Ignore
3973 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07003974 }
3975
3976 private void updateDefaultDensity() {
3977 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
3978 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
3979 && !mDensityCompatMode) {
3980 Slog.i(TAG, "Switching default density from "
3981 + DisplayMetrics.DENSITY_DEVICE + " to "
3982 + mCurDefaultDisplayDpi);
3983 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
3984 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3985 }
3986 }
3987
Romain Guy65b345f2011-07-27 18:51:50 -07003988 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 mBoundApplication = data;
3990 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003991 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003993 mProfiler = new Profiler();
3994 mProfiler.profileFile = data.initProfileFile;
3995 mProfiler.profileFd = data.initProfileFd;
3996 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003999 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 android.ddm.DdmHandleAppName.setAppName(data.processName);
4001
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004002 if (data.persistent) {
4003 // Persistent processes on low-memory devices do not get to
4004 // use hardware accelerated drawing, since this can add too much
4005 // overhead to the process.
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004006 final Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004007 if (!ActivityManager.isHighEndGfx(display)) {
4008 HardwareRenderer.disable(false);
4009 }
4010 }
Romain Guya9582652011-11-10 14:20:10 -08004011
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004012 if (mProfiler.profileFd != null) {
4013 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004014 }
4015
Joe Onoratod630f102011-03-17 18:42:26 -07004016 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4017 // implementation to use the pool executor. Normally, we use the
4018 // serialized executor as the default. This has to happen in the
4019 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004020 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004021 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4022 }
4023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 /*
4025 * Before spawning a new process, reset the time zone to be the system time zone.
4026 * This needs to be done because the system time zone could have changed after the
4027 * the spawning of this process. Without doing this this process would have the incorrect
4028 * system time zone.
4029 */
4030 TimeZone.setDefault(null);
4031
4032 /*
4033 * Initialize the default locale in this process for the reasons we set the time zone.
4034 */
4035 Locale.setDefault(data.config.locale);
4036
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004037 /*
4038 * Update the system configuration since its preloaded and might not
4039 * reflect configuration changes. The configuration object passed
4040 * in AppBindData can be safely assumed to be up to date
4041 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004042 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004043 mCurDefaultDisplayDpi = data.config.densityDpi;
4044 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004045
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004046 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047
Dianne Hackborndde331c2012-08-03 14:01:57 -07004048 /**
4049 * Switch this process to density compatibility mode if needed.
4050 */
4051 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4052 == 0) {
4053 mDensityCompatMode = true;
4054 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4055 }
4056 updateDefaultDensity();
4057
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004058 final ContextImpl appContext = new ContextImpl();
4059 appContext.init(data.info, null, this);
4060 final File cacheDir = appContext.getCacheDir();
4061
4062 // Provide a usable directory for temporary files
4063 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4064
4065 setupGraphicsSupport(data.info, cacheDir);
4066
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004067 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004068 * For system applications on userdebug/eng builds, log stack
4069 * traces of disk and network access to dropbox for analysis.
4070 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004071 if ((data.appInfo.flags &
4072 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004073 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4074 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004075 }
4076
4077 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004078 * For apps targetting SDK Honeycomb or later, we don't allow
4079 * network usage on the main event loop / UI thread.
4080 *
4081 * Note to those grepping: this is what ultimately throws
4082 * NetworkOnMainThreadException ...
4083 */
4084 if (data.appInfo.targetSdkVersion > 9) {
4085 StrictMode.enableDeathOnNetwork();
4086 }
4087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004088 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4089 // XXX should have option to change the port.
4090 Debug.changeDebugPort(8100);
4091 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004092 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 + " is waiting for the debugger on port 8100...");
4094
4095 IActivityManager mgr = ActivityManagerNative.getDefault();
4096 try {
4097 mgr.showWaitingForDebugger(mAppThread, true);
4098 } catch (RemoteException ex) {
4099 }
4100
4101 Debug.waitForDebugger();
4102
4103 try {
4104 mgr.showWaitingForDebugger(mAppThread, false);
4105 } catch (RemoteException ex) {
4106 }
4107
4108 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004109 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 + " can be debugged on port 8100...");
4111 }
4112 }
4113
Siva Velusamy92a8b222012-03-09 16:24:04 -08004114 // Enable OpenGL tracing if required
4115 if (data.enableOpenGlTrace) {
4116 GLUtils.enableTracing();
4117 }
4118
Robert Greenwalt434203a2010-10-11 16:00:27 -07004119 /**
4120 * Initialize the default http proxy in this process for the reasons we set the time zone.
4121 */
4122 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004123 if (b != null) {
4124 // In pre-boot mode (doing initial launch to collect password), not
4125 // all system is up. This includes the connectivity service, so don't
4126 // crash if we can't get it.
4127 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4128 try {
4129 ProxyProperties proxyProperties = service.getProxy();
4130 Proxy.setHttpProxySystemProperty(proxyProperties);
4131 } catch (RemoteException e) {}
4132 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 InstrumentationInfo ii = null;
4136 try {
4137 ii = appContext.getPackageManager().
4138 getInstrumentationInfo(data.instrumentationName, 0);
4139 } catch (PackageManager.NameNotFoundException e) {
4140 }
4141 if (ii == null) {
4142 throw new RuntimeException(
4143 "Unable to find instrumentation info for: "
4144 + data.instrumentationName);
4145 }
4146
4147 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004148 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 mInstrumentationAppPackage = ii.packageName;
4150 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004151 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152
4153 ApplicationInfo instrApp = new ApplicationInfo();
4154 instrApp.packageName = ii.packageName;
4155 instrApp.sourceDir = ii.sourceDir;
4156 instrApp.publicSourceDir = ii.publicSourceDir;
4157 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004158 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004159 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004161 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 instrContext.init(pi, null, this);
4163
4164 try {
4165 java.lang.ClassLoader cl = instrContext.getClassLoader();
4166 mInstrumentation = (Instrumentation)
4167 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4168 } catch (Exception e) {
4169 throw new RuntimeException(
4170 "Unable to instantiate instrumentation "
4171 + data.instrumentationName + ": " + e.toString(), e);
4172 }
4173
4174 mInstrumentation.init(this, instrContext, appContext,
4175 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4176
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004177 if (mProfiler.profileFile != null && !ii.handleProfiling
4178 && mProfiler.profileFd == null) {
4179 mProfiler.handlingProfiling = true;
4180 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004181 file.getParentFile().mkdirs();
4182 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4183 }
4184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 } else {
4186 mInstrumentation = new Instrumentation();
4187 }
4188
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004189 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004190 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004191 }
4192
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004193 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004194 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004195 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004196 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004198 // If the app is being launched for full backup or restore, bring it up in
4199 // a restricted environment with the base application class.
4200 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4201 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004202
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004203 // don't bring up providers in restricted mode; they may depend on the
4204 // app's custom Application class
4205 if (!data.restrictedBackupMode) {
4206 List<ProviderInfo> providers = data.providers;
4207 if (providers != null) {
4208 installContentProviders(app, providers);
4209 // For process that contains content providers, we want to
4210 // ensure that the JIT is enabled "at some point".
4211 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4212 }
4213 }
4214
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004215 // Do this after providers, since instrumentation tests generally start their
4216 // test thread at this point, and we don't want that racing.
4217 try {
4218 mInstrumentation.onCreate(data.instrumentationArgs);
4219 }
4220 catch (Exception e) {
4221 throw new RuntimeException(
4222 "Exception thrown in onCreate() of "
4223 + data.instrumentationName + ": " + e.toString(), e);
4224 }
4225
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004226 try {
4227 mInstrumentation.callApplicationOnCreate(app);
4228 } catch (Exception e) {
4229 if (!mInstrumentation.onException(app, e)) {
4230 throw new RuntimeException(
4231 "Unable to create application " + app.getClass().getName()
4232 + ": " + e.toString(), e);
4233 }
4234 }
4235 } finally {
4236 StrictMode.setThreadPolicy(savedPolicy);
4237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 }
4239
4240 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4241 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004242 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4243 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 Debug.stopMethodTracing();
4245 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004246 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 // + ", app thr: " + mAppThread);
4248 try {
4249 am.finishInstrumentation(mAppThread, resultCode, results);
4250 } catch (RemoteException ex) {
4251 }
4252 }
4253
Romain Guy65b345f2011-07-27 18:51:50 -07004254 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 Context context, List<ProviderInfo> providers) {
4256 final ArrayList<IActivityManager.ContentProviderHolder> results =
4257 new ArrayList<IActivityManager.ContentProviderHolder>();
4258
Romain Guya998dff2012-03-23 18:58:36 -07004259 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004261 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 buf.append(cpi.authority);
4263 buf.append(": ");
4264 buf.append(cpi.name);
4265 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004266 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4267 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4268 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004269 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 }
4272 }
4273
4274 try {
4275 ActivityManagerNative.getDefault().publishContentProviders(
4276 getApplicationThread(), results);
4277 } catch (RemoteException ex) {
4278 }
4279 }
4280
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004281 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4282 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004283 if (provider != null) {
4284 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004285 }
4286
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004287 // There is a possible race here. Another thread may try to acquire
4288 // the same provider at the same time. When this happens, we want to ensure
4289 // that the first one wins.
4290 // Note that we cannot hold the lock while acquiring and installing the
4291 // provider since it might take a long time to run and it could also potentially
4292 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293 IActivityManager.ContentProviderHolder holder = null;
4294 try {
4295 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004296 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 } catch (RemoteException ex) {
4298 }
4299 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004300 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 return null;
4302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004304 // Install provider will increment the reference count for us, and break
4305 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004306 holder = installProvider(c, holder, holder.info,
4307 true /*noisy*/, holder.noReleaseNeeded, stable);
4308 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 }
4310
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004311 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4312 if (stable) {
4313 prc.stableCount += 1;
4314 if (prc.stableCount == 1) {
4315 // We are acquiring a new stable reference on the provider.
4316 int unstableDelta;
4317 if (prc.removePending) {
4318 // We have a pending remove operation, which is holding the
4319 // last unstable reference. At this point we are converting
4320 // that unstable reference to our new stable reference.
4321 unstableDelta = -1;
4322 // Cancel the removal of the provider.
4323 if (DEBUG_PROVIDER) {
4324 Slog.v(TAG, "incProviderRef: stable "
4325 + "snatched provider from the jaws of death");
4326 }
4327 prc.removePending = false;
4328 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4329 } else {
4330 unstableDelta = 0;
4331 }
4332 try {
4333 if (DEBUG_PROVIDER) {
4334 Slog.v(TAG, "incProviderRef Now stable - "
4335 + prc.holder.info.name + ": unstableDelta="
4336 + unstableDelta);
4337 }
4338 ActivityManagerNative.getDefault().refContentProvider(
4339 prc.holder.connection, 1, unstableDelta);
4340 } catch (RemoteException e) {
4341 //do nothing content provider object is dead any way
4342 }
4343 }
4344 } else {
4345 prc.unstableCount += 1;
4346 if (prc.unstableCount == 1) {
4347 // We are acquiring a new unstable reference on the provider.
4348 if (prc.removePending) {
4349 // Oh look, we actually have a remove pending for the
4350 // provider, which is still holding the last unstable
4351 // reference. We just need to cancel that to take new
4352 // ownership of the reference.
4353 if (DEBUG_PROVIDER) {
4354 Slog.v(TAG, "incProviderRef: unstable "
4355 + "snatched provider from the jaws of death");
4356 }
4357 prc.removePending = false;
4358 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4359 } else {
4360 // First unstable ref, increment our count in the
4361 // activity manager.
4362 try {
4363 if (DEBUG_PROVIDER) {
4364 Slog.v(TAG, "incProviderRef: Now unstable - "
4365 + prc.holder.info.name);
4366 }
4367 ActivityManagerNative.getDefault().refContentProvider(
4368 prc.holder.connection, 0, 1);
4369 } catch (RemoteException e) {
4370 //do nothing content provider object is dead any way
4371 }
4372 }
4373 }
4374 }
4375 }
4376
4377 public final IContentProvider acquireExistingProvider(Context c, String name,
4378 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004379 synchronized (mProviderMap) {
4380 ProviderClientRecord pr = mProviderMap.get(name);
4381 if (pr == null) {
4382 return null;
4383 }
4384
4385 IContentProvider provider = pr.mProvider;
4386 IBinder jBinder = provider.asBinder();
4387
4388 // Only increment the ref count if we have one. If we don't then the
4389 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004390 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004391 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004392 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004393 }
4394 return provider;
4395 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004396 }
4397
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004398 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4399 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 return false;
4401 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004404 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004406 if (prc == null) {
4407 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004409 }
4410
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004411 boolean lastRef = false;
4412 if (stable) {
4413 if (prc.stableCount == 0) {
4414 if (DEBUG_PROVIDER) Slog.v(TAG,
4415 "releaseProvider: stable ref count already 0, how?");
4416 return false;
4417 }
4418 prc.stableCount -= 1;
4419 if (prc.stableCount == 0) {
4420 // What we do at this point depends on whether there are
4421 // any unstable refs left: if there are, we just tell the
4422 // activity manager to decrement its stable count; if there
4423 // aren't, we need to enqueue this provider to be removed,
4424 // and convert to holding a single unstable ref while
4425 // doing so.
4426 lastRef = prc.unstableCount == 0;
4427 try {
4428 if (DEBUG_PROVIDER) {
4429 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4430 + lastRef + " - " + prc.holder.info.name);
4431 }
4432 ActivityManagerNative.getDefault().refContentProvider(
4433 prc.holder.connection, -1, lastRef ? 1 : 0);
4434 } catch (RemoteException e) {
4435 //do nothing content provider object is dead any way
4436 }
4437 }
4438 } else {
4439 if (prc.unstableCount == 0) {
4440 if (DEBUG_PROVIDER) Slog.v(TAG,
4441 "releaseProvider: unstable ref count already 0, how?");
4442 return false;
4443 }
4444 prc.unstableCount -= 1;
4445 if (prc.unstableCount == 0) {
4446 // If this is the last reference, we need to enqueue
4447 // this provider to be removed instead of telling the
4448 // activity manager to remove it at this point.
4449 lastRef = prc.stableCount == 0;
4450 if (!lastRef) {
4451 try {
4452 if (DEBUG_PROVIDER) {
4453 Slog.v(TAG, "releaseProvider: No longer unstable - "
4454 + prc.holder.info.name);
4455 }
4456 ActivityManagerNative.getDefault().refContentProvider(
4457 prc.holder.connection, 0, -1);
4458 } catch (RemoteException e) {
4459 //do nothing content provider object is dead any way
4460 }
4461 }
4462 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004463 }
4464
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004465 if (lastRef) {
4466 if (!prc.removePending) {
4467 // Schedule the actual remove asynchronously, since we don't know the context
4468 // this will be called in.
4469 // TODO: it would be nice to post a delayed message, so
4470 // if we come back and need the same provider quickly
4471 // we will still have it available.
4472 if (DEBUG_PROVIDER) {
4473 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4474 + prc.holder.info.name);
4475 }
4476 prc.removePending = true;
4477 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4478 mH.sendMessage(msg);
4479 } else {
4480 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4481 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004482 }
4483 return true;
4484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 }
4486
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004487 final void completeRemoveProvider(ProviderRefCount prc) {
4488 synchronized (mProviderMap) {
4489 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004490 // There was a race! Some other client managed to acquire
4491 // the provider before the removal was completed.
4492 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004493 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004494 + "provider still in use");
4495 return;
4496 }
4497
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004498 final IBinder jBinder = prc.holder.provider.asBinder();
4499 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4500 if (existingPrc == prc) {
4501 mProviderRefCountMap.remove(jBinder);
4502 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004503
4504 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4505 while (iter.hasNext()) {
4506 ProviderClientRecord pr = iter.next();
4507 IBinder myBinder = pr.mProvider.asBinder();
4508 if (myBinder == jBinder) {
4509 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004510 }
4511 }
4512 }
4513
4514 try {
4515 if (DEBUG_PROVIDER) {
4516 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4517 + "removeContentProvider(" + prc.holder.info.name + ")");
4518 }
4519 ActivityManagerNative.getDefault().removeContentProvider(
4520 prc.holder.connection, false);
4521 } catch (RemoteException e) {
4522 //do nothing content provider object is dead any way
4523 }
4524 }
4525
4526 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4527 synchronized(mProviderMap) {
4528 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4529 if (prc != null) {
4530 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4531 + provider + " " + prc.holder.info.name);
4532 mProviderRefCountMap.remove(provider);
4533 if (prc.client != null && prc.client.mNames != null) {
4534 for (String name : prc.client.mNames) {
4535 ProviderClientRecord pr = mProviderMap.get(name);
4536 if (pr != null && pr.mProvider.asBinder() == provider) {
4537 Slog.i(TAG, "Removing dead content provider: " + name);
4538 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004539 }
4540 }
4541 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004542 if (fromClient) {
4543 // We found out about this due to execution in our client
4544 // code. Tell the activity manager about it now, to ensure
4545 // that the next time we go to do anything with the provider
4546 // it knows it is dead (so we don't race with its death
4547 // notification).
4548 try {
4549 ActivityManagerNative.getDefault().unstableProviderDied(
4550 prc.holder.connection);
4551 } catch (RemoteException e) {
4552 //do nothing content provider object is dead any way
4553 }
4554 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004555 }
4556 }
4557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004559 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4560 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4561 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4562 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4563 localProvider, holder);
4564 for (int i = 0; i < names.length; i++) {
4565 ProviderClientRecord existing = mProviderMap.get(names[i]);
4566 if (existing != null) {
4567 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4568 + " already published as " + names[i]);
4569 } else {
4570 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 }
4572 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004573 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 }
4575
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004576 /**
4577 * Installs the provider.
4578 *
4579 * Providers that are local to the process or that come from the system server
4580 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4581 * Other remote providers are reference counted. The initial reference count
4582 * for all reference counted providers is one. Providers that are not reference
4583 * counted do not have a reference count (at all).
4584 *
4585 * This method detects when a provider has already been installed. When this happens,
4586 * it increments the reference count of the existing provider (if appropriate)
4587 * and returns the existing provider. This can happen due to concurrent
4588 * attempts to acquire the same provider.
4589 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004590 private IActivityManager.ContentProviderHolder installProvider(Context context,
4591 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4592 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004594 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004595 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004596 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004597 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 + info.name);
4599 }
4600 Context c = null;
4601 ApplicationInfo ai = info.applicationInfo;
4602 if (context.getPackageName().equals(ai.packageName)) {
4603 c = context;
4604 } else if (mInitialApplication != null &&
4605 mInitialApplication.getPackageName().equals(ai.packageName)) {
4606 c = mInitialApplication;
4607 } else {
4608 try {
4609 c = context.createPackageContext(ai.packageName,
4610 Context.CONTEXT_INCLUDE_CODE);
4611 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004612 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 }
4614 }
4615 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004616 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 ai.packageName +
4618 " while loading content provider " +
4619 info.name);
4620 return null;
4621 }
4622 try {
4623 final java.lang.ClassLoader cl = c.getClassLoader();
4624 localProvider = (ContentProvider)cl.
4625 loadClass(info.name).newInstance();
4626 provider = localProvider.getIContentProvider();
4627 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004628 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 info.name + " from sourceDir " +
4630 info.applicationInfo.sourceDir);
4631 return null;
4632 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004633 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 TAG, "Instantiating local provider " + info.name);
4635 // XXX Need to create the correct context for this provider.
4636 localProvider.attachInfo(c, info);
4637 } catch (java.lang.Exception e) {
4638 if (!mInstrumentation.onException(null, e)) {
4639 throw new RuntimeException(
4640 "Unable to get provider " + info.name
4641 + ": " + e.toString(), e);
4642 }
4643 return null;
4644 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004645 } else {
4646 provider = holder.provider;
4647 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 + info.name);
4649 }
4650
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004651 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004652
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004653 synchronized (mProviderMap) {
4654 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4655 + " / " + info.name);
4656 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004658 ComponentName cname = new ComponentName(info.packageName, info.name);
4659 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004660 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004661 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004662 Slog.v(TAG, "installProvider: lost the race, "
4663 + "using existing local provider");
4664 }
4665 provider = pr.mProvider;
4666 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004667 holder = new IActivityManager.ContentProviderHolder(info);
4668 holder.provider = provider;
4669 holder.noReleaseNeeded = true;
4670 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004671 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004672 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004673 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004674 retHolder = pr.mHolder;
4675 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004676 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4677 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004678 if (DEBUG_PROVIDER) {
4679 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004680 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004681 // We need to transfer our new reference to the existing
4682 // ref count, releasing the old one... but only if
4683 // release is needed (that is, it is not running in the
4684 // system process).
4685 if (!noReleaseNeeded) {
4686 incProviderRefLocked(prc, stable);
4687 try {
4688 ActivityManagerNative.getDefault().removeContentProvider(
4689 holder.connection, stable);
4690 } catch (RemoteException e) {
4691 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004692 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004693 }
4694 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004695 ProviderClientRecord client = installProviderAuthoritiesLocked(
4696 provider, localProvider, holder);
4697 if (noReleaseNeeded) {
4698 prc = new ProviderRefCount(holder, client, 1000, 1000);
4699 } else {
4700 prc = stable
4701 ? new ProviderRefCount(holder, client, 1, 0)
4702 : new ProviderRefCount(holder, client, 0, 1);
4703 }
4704 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004705 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004706 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 }
4708 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004709
4710 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 }
4712
Romain Guy65b345f2011-07-27 18:51:50 -07004713 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 sThreadLocal.set(this);
4715 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004717 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004718 public void run() {
4719 ensureJitEnabled();
4720 }
4721 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4723 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4724 IActivityManager mgr = ActivityManagerNative.getDefault();
4725 try {
4726 mgr.attachApplication(mAppThread);
4727 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004728 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 }
4730 } else {
4731 // Don't set application object here -- if the system crashes,
4732 // we can't display an alert, we just want to die die die.
4733 android.ddm.DdmHandleAppName.setAppName("system_process");
4734 try {
4735 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004736 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737 context.init(getSystemContext().mPackageInfo, null, this);
4738 Application app = Instrumentation.newApplication(Application.class, context);
4739 mAllApplications.add(app);
4740 mInitialApplication = app;
4741 app.onCreate();
4742 } catch (Exception e) {
4743 throw new RuntimeException(
4744 "Unable to instantiate Application():" + e.toString(), e);
4745 }
4746 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004747
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004748 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004749 public void onConfigurationChanged(Configuration newConfig) {
4750 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004751 // We need to apply this change to the resources
4752 // immediately, because upon returning the view
4753 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004754 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004755 // This actually changed the resources! Tell
4756 // everyone about it.
4757 if (mPendingConfiguration == null ||
4758 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4759 mPendingConfiguration = newConfig;
4760
4761 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4762 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004763 }
4764 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004765 }
4766 public void onLowMemory() {
4767 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004768 public void onTrimMemory(int level) {
4769 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004770 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004771 }
4772
Romain Guy5e9120d2012-01-30 12:17:22 -08004773 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004774 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 ActivityThread thread = new ActivityThread();
4776 thread.attach(true);
4777 return thread;
4778 }
4779
Jeff Brown10e89712011-07-08 18:52:57 -07004780 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004782 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004783 }
4784 }
4785
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004786 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004787 synchronized (mPackages) {
4788 if (mCoreSettings != null) {
4789 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004790 } else {
4791 return defaultValue;
4792 }
4793 }
4794 }
4795
Romain Guy65b345f2011-07-27 18:51:50 -07004796 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004797 SamplingProfilerIntegration.start();
4798
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004799 // CloseGuard defaults to true and can be quite spammy. We
4800 // disable it here, but selectively enable it later (via
4801 // StrictMode) on debug builds, but using DropBox, not logs.
4802 CloseGuard.setEnabled(false);
4803
Kenny Roote29df162012-08-10 08:28:37 -07004804 Security.addProvider(new AndroidKeyStoreProvider());
4805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004806 Process.setArgV0("<pre-initialized>");
4807
4808 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004809 if (sMainThreadHandler == null) {
4810 sMainThreadHandler = new Handler();
4811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812
4813 ActivityThread thread = new ActivityThread();
4814 thread.attach(false);
4815
Romain Guy5e9120d2012-01-30 12:17:22 -08004816 AsyncTask.init();
4817
Dianne Hackborn287952c2010-09-22 22:34:31 -07004818 if (false) {
4819 Looper.myLooper().setMessageLogging(new
4820 LogPrinter(Log.DEBUG, "ActivityThread"));
4821 }
4822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 Looper.loop();
4824
Jeff Brown10e89712011-07-08 18:52:57 -07004825 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826 }
4827}