blob: a3880f3d87860a09b64a2d100563ec6df72e67cc [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;
Jeff Brown98365d72012-08-19 20:30:52 -070045import android.hardware.display.DisplayManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070046import android.net.IConnectivityManager;
47import android.net.Proxy;
48import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080049import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070050import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070051import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Bundle;
53import android.os.Debug;
54import android.os.Handler;
55import android.os.IBinder;
56import android.os.Looper;
57import android.os.Message;
58import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070059import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Process;
61import android.os.RemoteException;
62import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070063import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070065import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070066import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.DisplayMetrics;
69import android.util.EventLog;
70import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070071import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080072import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080073import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070074import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070076import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.view.View;
78import android.view.ViewDebug;
79import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070080import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import android.view.Window;
82import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070083import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080084import android.renderscript.RenderScript;
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import java.util.ArrayList;
100import java.util.HashMap;
101import java.util.Iterator;
102import java.util.List;
103import java.util.Locale;
104import java.util.Map;
105import java.util.TimeZone;
106import java.util.regex.Pattern;
107
Jeff Sharkeye861b422012-03-01 20:59:22 -0800108import libcore.io.IoUtils;
109
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800110import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112final class SuperNotCalledException extends AndroidRuntimeException {
113 public SuperNotCalledException(String msg) {
114 super(msg);
115 }
116}
117
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700118final class RemoteServiceException extends AndroidRuntimeException {
119 public RemoteServiceException(String msg) {
120 super(msg);
121 }
122}
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124/**
125 * This manages the execution of the main thread in an
126 * application process, scheduling and executing activities,
127 * broadcasts, and other operations on it as the activity
128 * manager requests.
129 *
130 * {@hide}
131 */
132public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700133 /** @hide */
134 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700135 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700136 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700137 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700138 /** @hide */
139 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700140 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700141 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700142 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800143 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700144 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700145 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
147 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
148 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
149 private static final int LOG_ON_PAUSE_CALLED = 30021;
150 private static final int LOG_ON_RESUME_CALLED = 30022;
151
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700152 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700153
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700154 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700156 final ApplicationThread mAppThread = new ApplicationThread();
157 final Looper mLooper = Looper.myLooper();
158 final H mH = new H();
159 final HashMap<IBinder, ActivityClientRecord> mActivities
160 = new HashMap<IBinder, ActivityClientRecord>();
161 // List of new activities (via ActivityRecord.nextIdle) that should
162 // be reported when next we idle.
163 ActivityClientRecord mNewActivities = null;
164 // Number of activities that are currently visible on-screen.
165 int mNumVisibleActivities = 0;
166 final HashMap<IBinder, Service> mServices
167 = new HashMap<IBinder, Service>();
168 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700169 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700170 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700171 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700172 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700173 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700174 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700175 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700176 Application mInitialApplication;
177 final ArrayList<Application> mAllApplications
178 = new ArrayList<Application>();
179 // set of instantiated backup agents, keyed by package name
180 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700181 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 Instrumentation mInstrumentation;
183 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700184 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700185 String mInstrumentationAppPackage = null;
186 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700187 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 boolean mSystemThread = false;
189 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700191 // These can be accessed by multiple threads; mPackages is the lock.
192 // XXX For now we keep around information about all packages we have
193 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800194 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700195 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800196 // which means this lock gets held while the activity and window managers
197 // holds their own lock. Thus you MUST NEVER call back into the activity manager
198 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 final HashMap<String, WeakReference<LoadedApk>> mPackages
200 = new HashMap<String, WeakReference<LoadedApk>>();
201 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
202 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700203 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
204 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
206 = new HashMap<ResourcesKey, WeakReference<Resources> >();
207 final ArrayList<ActivityClientRecord> mRelaunchingActivities
208 = new ArrayList<ActivityClientRecord>();
209 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700211 // The lock of mProviderMap protects the following variables.
212 final HashMap<String, ProviderClientRecord> mProviderMap
213 = new HashMap<String, ProviderClientRecord>();
214 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
215 = new HashMap<IBinder, ProviderRefCount>();
216 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
217 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700218 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
219 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
Jeff Hamilton52d32032011-01-08 15:31:26 -0600221 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
222 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
223
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700224 final GcIdler mGcIdler = new GcIdler();
225 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700227 static Handler sMainThreadHandler; // set once in main()
228
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800229 Bundle mCoreSettings = null;
230
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400231 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700233 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 Intent intent;
235 Bundle state;
236 Activity activity;
237 Window window;
238 Activity parent;
239 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700240 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 boolean paused;
242 boolean stopped;
243 boolean hideForNow;
244 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700245 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700246 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700248 String profileFile;
249 ParcelFileDescriptor profileFd;
250 boolean autoStopProfiler;
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400253 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700254 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
256 List<ResultInfo> pendingResults;
257 List<Intent> pendingIntents;
258
259 boolean startsNotResumed;
260 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800261 int pendingConfigChanges;
262 boolean onlyLocalRequest;
263
264 View mPendingRemoveWindow;
265 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700267 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 parent = null;
269 embeddedID = null;
270 paused = false;
271 stopped = false;
272 hideForNow = false;
273 nextIdle = null;
274 }
275
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800276 public boolean isPreHoneycomb() {
277 if (activity != null) {
278 return activity.getApplicationInfo().targetSdkVersion
279 < android.os.Build.VERSION_CODES.HONEYCOMB;
280 }
281 return false;
282 }
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700285 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 return "ActivityRecord{"
287 + Integer.toHexString(System.identityHashCode(this))
288 + " token=" + token + " " + (componentName == null
289 ? "no component name" : componentName.toShortString())
290 + "}";
291 }
292 }
293
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700294 final class ProviderClientRecord {
295 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 final IContentProvider mProvider;
297 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700298 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700300 ProviderClientRecord(String[] names, IContentProvider provider,
301 ContentProvider localProvider,
302 IActivityManager.ContentProviderHolder holder) {
303 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mProvider = provider;
305 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700306 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 }
308 }
309
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400310 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 List<Intent> intents;
312 IBinder token;
313 public String toString() {
314 return "NewIntentData{intents=" + intents + " token=" + token + "}";
315 }
316 }
317
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400318 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700319 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
320 boolean ordered, boolean sticky, IBinder token) {
321 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
322 this.intent = intent;
323 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 Intent intent;
326 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400327 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public String toString() {
329 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700330 info.packageName + " resultCode=" + getResultCode()
331 + " resultData=" + getResultData() + " resultExtras="
332 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 }
334 }
335
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400336 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700337 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400338 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700339 int backupMode;
340 public String toString() {
341 return "CreateBackupAgentData{appInfo=" + appInfo
342 + " backupAgent=" + appInfo.backupAgentName
343 + " mode=" + backupMode + "}";
344 }
345 }
Bob Leee5408332009-09-04 18:31:17 -0700346
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400347 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 IBinder token;
349 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 Intent intent;
352 public String toString() {
353 return "CreateServiceData{token=" + token + " className="
354 + info.name + " packageName=" + info.packageName
355 + " intent=" + intent + "}";
356 }
357 }
358
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400359 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 IBinder token;
361 Intent intent;
362 boolean rebind;
363 public String toString() {
364 return "BindServiceData{token=" + token + " intent=" + intent + "}";
365 }
366 }
367
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400368 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700370 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700372 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 Intent args;
374 public String toString() {
375 return "ServiceArgsData{token=" + token + " startId=" + startId
376 + " args=" + args + "}";
377 }
378 }
379
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400380 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700381 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 String processName;
383 ApplicationInfo appInfo;
384 List<ProviderInfo> providers;
385 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 Bundle instrumentationArgs;
387 IInstrumentationWatcher instrumentationWatcher;
388 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800389 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700390 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700391 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400393 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700394
395 /** Initial values for {@link Profiler}. */
396 String initProfileFile;
397 ParcelFileDescriptor initProfileFd;
398 boolean initAutoStopProfiler;
399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 public String toString() {
401 return "AppBindData{appInfo=" + appInfo + "}";
402 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700403 }
404
405 static final class Profiler {
406 String profileFile;
407 ParcelFileDescriptor profileFd;
408 boolean autoStopProfiler;
409 boolean profiling;
410 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700411 public void setProfiler(String file, ParcelFileDescriptor fd) {
412 if (profiling) {
413 if (fd != null) {
414 try {
415 fd.close();
416 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700417 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700418 }
419 }
420 return;
421 }
422 if (profileFd != null) {
423 try {
424 profileFd.close();
425 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700426 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700427 }
428 }
429 profileFile = file;
430 profileFd = fd;
431 }
432 public void startProfiling() {
433 if (profileFd == null || profiling) {
434 return;
435 }
436 try {
437 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
438 8 * 1024 * 1024, 0);
439 profiling = true;
440 } catch (RuntimeException e) {
441 Slog.w(TAG, "Profiling failed on path " + profileFile);
442 try {
443 profileFd.close();
444 profileFd = null;
445 } catch (IOException e2) {
446 Slog.w(TAG, "Failure closing profile fd", e2);
447 }
448 }
449 }
450 public void stopProfiling() {
451 if (profiling) {
452 profiling = false;
453 Debug.stopMethodTracing();
454 if (profileFd != null) {
455 try {
456 profileFd.close();
457 } catch (IOException e) {
458 }
459 }
460 profileFd = null;
461 profileFile = null;
462 }
463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
465
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700467 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700468 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800469 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400473 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 IBinder token;
475 List<ResultInfo> results;
476 public String toString() {
477 return "ResultData{token=" + token + " results" + results + "}";
478 }
479 }
480
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400481 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800482 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 String what;
484 String who;
485 }
486
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400487 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700488 String path;
489 ParcelFileDescriptor fd;
490 }
491
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400492 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700493 String path;
494 ParcelFileDescriptor fd;
495 }
496
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400497 static final class UpdateCompatibilityData {
498 String pkg;
499 CompatibilityInfo info;
500 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700501
Romain Guy65b345f2011-07-27 18:51:50 -0700502 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700503
Romain Guy65b345f2011-07-27 18:51:50 -0700504 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700505 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
506 private static final String ONE_COUNT_COLUMN = "%21s %8d";
507 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700508 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 // Formatting for checkin service - update version if row format changes
511 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700512
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700513 private void updatePendingConfiguration(Configuration config) {
514 synchronized (mPackages) {
515 if (mPendingConfiguration == null ||
516 mPendingConfiguration.isOtherSeqNewer(config)) {
517 mPendingConfiguration = config;
518 }
519 }
520 }
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 public final void schedulePauseActivity(IBinder token, boolean finished,
523 boolean userLeaving, int configChanges) {
524 queueOrSendMessage(
525 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
526 token,
527 (userLeaving ? 1 : 0),
528 configChanges);
529 }
530
531 public final void scheduleStopActivity(IBinder token, boolean showWindow,
532 int configChanges) {
533 queueOrSendMessage(
534 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
535 token, 0, configChanges);
536 }
537
538 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
539 queueOrSendMessage(
540 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
541 token);
542 }
543
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800544 public final void scheduleSleeping(IBinder token, boolean sleeping) {
545 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
546 }
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
549 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
550 }
551
552 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
553 ResultData res = new ResultData();
554 res.token = token;
555 res.results = results;
556 queueOrSendMessage(H.SEND_RESULT, res);
557 }
558
559 // we use token to identify this activity without having to send the
560 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700561 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700562 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
563 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700564 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
565 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700566 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567
568 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700569 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 r.intent = intent;
571 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400572 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 r.state = state;
574
575 r.pendingResults = pendingResults;
576 r.pendingIntents = pendingNewIntents;
577
578 r.startsNotResumed = notResumed;
579 r.isForward = isForward;
580
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700581 r.profileFile = profileName;
582 r.profileFd = profileFd;
583 r.autoStopProfiler = autoStopProfiler;
584
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700585 updatePendingConfiguration(curConfig);
586
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
588 }
589
590 public final void scheduleRelaunchActivity(IBinder token,
591 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800592 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800593 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
594 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
597 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
598 NewIntentData data = new NewIntentData();
599 data.intents = intents;
600 data.token = token;
601
602 queueOrSendMessage(H.NEW_INTENT, data);
603 }
604
605 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
606 int configChanges) {
607 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
608 configChanges);
609 }
610
611 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400612 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
613 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700614 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
615 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400617 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 queueOrSendMessage(H.RECEIVER, r);
619 }
620
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400621 public final void scheduleCreateBackupAgent(ApplicationInfo app,
622 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700623 CreateBackupAgentData d = new CreateBackupAgentData();
624 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700626 d.backupMode = backupMode;
627
628 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
629 }
630
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400631 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
632 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700633 CreateBackupAgentData d = new CreateBackupAgentData();
634 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400635 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700636
637 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
638 }
639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400641 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 CreateServiceData s = new CreateServiceData();
643 s.token = token;
644 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400645 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646
647 queueOrSendMessage(H.CREATE_SERVICE, s);
648 }
649
650 public final void scheduleBindService(IBinder token, Intent intent,
651 boolean rebind) {
652 BindServiceData s = new BindServiceData();
653 s.token = token;
654 s.intent = intent;
655 s.rebind = rebind;
656
Amith Yamasani742a6712011-05-04 14:49:28 -0700657 if (DEBUG_SERVICE)
658 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
659 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 queueOrSendMessage(H.BIND_SERVICE, s);
661 }
662
663 public final void scheduleUnbindService(IBinder token, Intent intent) {
664 BindServiceData s = new BindServiceData();
665 s.token = token;
666 s.intent = intent;
667
668 queueOrSendMessage(H.UNBIND_SERVICE, s);
669 }
670
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700671 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700672 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 ServiceArgsData s = new ServiceArgsData();
674 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700675 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700677 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 s.args = args;
679
680 queueOrSendMessage(H.SERVICE_ARGS, s);
681 }
682
683 public final void scheduleStopService(IBinder token) {
684 queueOrSendMessage(H.STOP_SERVICE, token);
685 }
686
687 public final void bindApplication(String processName,
688 ApplicationInfo appInfo, List<ProviderInfo> providers,
689 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700690 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800692 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
693 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700694 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
696 if (services != null) {
697 // Setup the service cache in the ServiceManager
698 ServiceManager.initServiceCache(services);
699 }
700
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800701 setCoreSettings(coreSettings);
702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 AppBindData data = new AppBindData();
704 data.processName = processName;
705 data.appInfo = appInfo;
706 data.providers = providers;
707 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 data.instrumentationArgs = instrumentationArgs;
709 data.instrumentationWatcher = instrumentationWatcher;
710 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800711 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700712 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700713 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400715 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700716 data.initProfileFile = profileFile;
717 data.initProfileFd = profileFd;
718 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 queueOrSendMessage(H.BIND_APPLICATION, data);
720 }
721
722 public final void scheduleExit() {
723 queueOrSendMessage(H.EXIT_APPLICATION, null);
724 }
725
Christopher Tate5e1ab332009-09-01 20:32:49 -0700726 public final void scheduleSuicide() {
727 queueOrSendMessage(H.SUICIDE, null);
728 }
729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 public void requestThumbnail(IBinder token) {
731 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
732 }
733
734 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700735 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
737 }
738
739 public void updateTimeZone() {
740 TimeZone.setDefault(null);
741 }
742
Robert Greenwalt03595d02010-11-02 14:08:23 -0700743 public void clearDnsCache() {
744 // a non-standard API to get this to libcore
745 InetAddress.clearDnsCache();
746 }
747
Robert Greenwalt434203a2010-10-11 16:00:27 -0700748 public void setHttpProxy(String host, String port, String exclList) {
749 Proxy.setHttpProxySystemProperty(host, port, exclList);
750 }
751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 public void processInBackground() {
753 mH.removeMessages(H.GC_WHEN_IDLE);
754 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
755 }
756
757 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700758 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700759 try {
760 data.fd = ParcelFileDescriptor.dup(fd);
761 data.token = servicetoken;
762 data.args = args;
763 queueOrSendMessage(H.DUMP_SERVICE, data);
764 } catch (IOException e) {
765 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767 }
768
769 // This function exists to make sure all receiver dispatching is
770 // correctly ordered, since these are one-way calls and the binder driver
771 // applies transaction ordering per object for such calls.
772 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700773 int resultCode, String dataStr, Bundle extras, boolean ordered,
774 boolean sticky) throws RemoteException {
775 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
Bob Leee5408332009-09-04 18:31:17 -0700777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void scheduleLowMemory() {
779 queueOrSendMessage(H.LOW_MEMORY, null);
780 }
781
782 public void scheduleActivityConfigurationChanged(IBinder token) {
783 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
784 }
785
Romain Guy7eabe552011-07-21 14:56:34 -0700786 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
787 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700788 ProfilerControlData pcd = new ProfilerControlData();
789 pcd.path = path;
790 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700791 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800792 }
793
Andy McFadden824c5102010-07-09 16:26:57 -0700794 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
795 DumpHeapData dhd = new DumpHeapData();
796 dhd.path = path;
797 dhd.fd = fd;
798 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
799 }
800
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700801 public void setSchedulingGroup(int group) {
802 // Note: do this immediately, since going into the foreground
803 // should happen regardless of what pending work we have to do
804 // and the activity manager will wait for us to report back that
805 // we are done before sending us to the background.
806 try {
807 Process.setProcessGroup(Process.myPid(), group);
808 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800809 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700810 }
811 }
Bob Leee5408332009-09-04 18:31:17 -0700812
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700813 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
814 Debug.getMemoryInfo(outInfo);
815 }
Bob Leee5408332009-09-04 18:31:17 -0700816
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700817 public void dispatchPackageBroadcast(int cmd, String[] packages) {
818 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
819 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700820
821 public void scheduleCrash(String msg) {
822 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
823 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700824
Dianne Hackborn30d71892010-12-11 10:37:55 -0800825 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
826 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700827 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700828 try {
829 data.fd = ParcelFileDescriptor.dup(fd);
830 data.token = activitytoken;
831 data.prefix = prefix;
832 data.args = args;
833 queueOrSendMessage(H.DUMP_ACTIVITY, data);
834 } catch (IOException e) {
835 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700836 }
837 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700838
Marco Nelissen18cb2872011-11-15 11:19:53 -0800839 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
840 String[] args) {
841 DumpComponentInfo data = new DumpComponentInfo();
842 try {
843 data.fd = ParcelFileDescriptor.dup(fd);
844 data.token = providertoken;
845 data.args = args;
846 queueOrSendMessage(H.DUMP_PROVIDER, data);
847 } catch (IOException e) {
848 Slog.w(TAG, "dumpProvider failed", e);
849 }
850 }
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700853 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
854 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700855 FileOutputStream fout = new FileOutputStream(fd);
856 PrintWriter pw = new PrintWriter(fout);
857 try {
Romain Guya998dff2012-03-23 18:58:36 -0700858 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700859 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700860 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700861 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700862 }
863
Romain Guya998dff2012-03-23 18:58:36 -0700864 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 long nativeMax = Debug.getNativeHeapSize() / 1024;
866 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
867 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
868
869 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
870 Debug.getMemoryInfo(memInfo);
871
Dianne Hackbornb437e092011-08-05 17:50:29 -0700872 if (!all) {
873 return memInfo;
874 }
875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 Runtime runtime = Runtime.getRuntime();
877
878 long dalvikMax = runtime.totalMemory() / 1024;
879 long dalvikFree = runtime.freeMemory() / 1024;
880 long dalvikAllocated = dalvikMax - dalvikFree;
881 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700882 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700883 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
884 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 int globalAssetCount = AssetManager.getGlobalAssetCount();
886 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
887 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
888 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
889 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700890 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800891 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700894 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 // NOTE: if you change anything significant below, also consider changing
896 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700897 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // Header
901 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
902 pw.print(Process.myPid()); pw.print(',');
903 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // Heap info - max
906 pw.print(nativeMax); pw.print(',');
907 pw.print(dalvikMax); pw.print(',');
908 pw.print("N/A,");
909 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Heap info - allocated
912 pw.print(nativeAllocated); pw.print(',');
913 pw.print(dalvikAllocated); pw.print(',');
914 pw.print("N/A,");
915 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 // Heap info - free
918 pw.print(nativeFree); pw.print(',');
919 pw.print(dalvikFree); pw.print(',');
920 pw.print("N/A,");
921 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Heap info - proportional set size
924 pw.print(memInfo.nativePss); pw.print(',');
925 pw.print(memInfo.dalvikPss); pw.print(',');
926 pw.print(memInfo.otherPss); pw.print(',');
927 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700930 pw.print(memInfo.nativeSharedDirty); pw.print(',');
931 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
932 pw.print(memInfo.otherSharedDirty); pw.print(',');
933 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
934 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700937 pw.print(memInfo.nativePrivateDirty); pw.print(',');
938 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
939 pw.print(memInfo.otherPrivateDirty); pw.print(',');
940 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
941 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 // Object counts
944 pw.print(viewInstanceCount); pw.print(',');
945 pw.print(viewRootInstanceCount); pw.print(',');
946 pw.print(appContextInstanceCount); pw.print(',');
947 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 pw.print(globalAssetCount); pw.print(',');
950 pw.print(globalAssetManagerCount); pw.print(',');
951 pw.print(binderLocalObjectCount); pw.print(',');
952 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 pw.print(binderDeathObjectCount); pw.print(',');
955 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800958 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800959 pw.print(stats.memoryUsed / 1024); pw.print(',');
960 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700961 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800962 for (int i = 0; i < stats.dbStats.size(); i++) {
963 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700964 pw.print(','); pw.print(dbStats.dbName);
965 pw.print(','); pw.print(dbStats.pageSize);
966 pw.print(','); pw.print(dbStats.dbSize);
967 pw.print(','); pw.print(dbStats.lookaside);
968 pw.print(','); pw.print(dbStats.cache);
969 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800970 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700971 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700972
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700973 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 }
Bob Leee5408332009-09-04 18:31:17 -0700975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700977 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
978 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
979 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
980 "------");
981 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
982 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
983 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
984 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 int otherPss = memInfo.otherPss;
987 int otherSharedDirty = memInfo.otherSharedDirty;
988 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700990 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700991 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700992 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
993 memInfo.getOtherPrivateDirty(i), "", "", "");
994 otherPss -= memInfo.getOtherPss(i);
995 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
996 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
997 }
998
999 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1000 otherPrivateDirty, "", "", "");
1001 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1002 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1003 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1004 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005
1006 pw.println(" ");
1007 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001008 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 viewRootInstanceCount);
1010
1011 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1012 "Activities:", activityInstanceCount);
1013
1014 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1015 "AssetManagers:", globalAssetManagerCount);
1016
1017 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1018 "Proxy Binders:", binderProxyObjectCount);
1019 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1020
1021 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 // SQLite mem info
1024 pw.println(" ");
1025 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001026 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1027 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1028 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001029 pw.println(" ");
1030 int N = stats.dbStats.size();
1031 if (N > 0) {
1032 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001033 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1034 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001035 for (int i = 0; i < N; i++) {
1036 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001037 printRow(pw, DB_INFO_FORMAT,
1038 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1039 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1040 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1041 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001042 }
1043 }
Bob Leee5408332009-09-04 18:31:17 -07001044
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001045 // Asset details.
1046 String assetAlloc = AssetManager.getAssetAllocations();
1047 if (assetAlloc != null) {
1048 pw.println(" ");
1049 pw.println(" Asset Allocations");
1050 pw.print(assetAlloc);
1051 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001052
1053 return memInfo;
1054 }
1055
1056 @Override
1057 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1058 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001059 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061
Jeff Brown6754ba22011-12-14 20:20:01 -08001062 @Override
1063 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1064 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1065 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1066 SQLiteDebug.dump(printer, args);
1067 pw.flush();
1068 }
1069
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001070 @Override
1071 public void unstableProviderDied(IBinder provider) {
1072 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1073 }
1074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 private void printRow(PrintWriter pw, String format, Object...objs) {
1076 pw.println(String.format(format, objs));
1077 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001078
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001079 public void setCoreSettings(Bundle coreSettings) {
1080 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001081 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001082
1083 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1084 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1085 ucd.pkg = pkg;
1086 ucd.info = info;
1087 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1088 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001089
1090 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001091 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001092 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
Romain Guy65b345f2011-07-27 18:51:50 -07001096 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 public static final int LAUNCH_ACTIVITY = 100;
1098 public static final int PAUSE_ACTIVITY = 101;
1099 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1100 public static final int STOP_ACTIVITY_SHOW = 103;
1101 public static final int STOP_ACTIVITY_HIDE = 104;
1102 public static final int SHOW_WINDOW = 105;
1103 public static final int HIDE_WINDOW = 106;
1104 public static final int RESUME_ACTIVITY = 107;
1105 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001106 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 public static final int BIND_APPLICATION = 110;
1108 public static final int EXIT_APPLICATION = 111;
1109 public static final int NEW_INTENT = 112;
1110 public static final int RECEIVER = 113;
1111 public static final int CREATE_SERVICE = 114;
1112 public static final int SERVICE_ARGS = 115;
1113 public static final int STOP_SERVICE = 116;
1114 public static final int REQUEST_THUMBNAIL = 117;
1115 public static final int CONFIGURATION_CHANGED = 118;
1116 public static final int CLEAN_UP_CONTEXT = 119;
1117 public static final int GC_WHEN_IDLE = 120;
1118 public static final int BIND_SERVICE = 121;
1119 public static final int UNBIND_SERVICE = 122;
1120 public static final int DUMP_SERVICE = 123;
1121 public static final int LOW_MEMORY = 124;
1122 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1123 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001124 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001125 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001126 public static final int DESTROY_BACKUP_AGENT = 129;
1127 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001128 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001129 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001130 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001131 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001132 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001133 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001134 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001135 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001136 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001137 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001138 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001139 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001141 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 switch (code) {
1143 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1144 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1145 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1146 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1147 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1148 case SHOW_WINDOW: return "SHOW_WINDOW";
1149 case HIDE_WINDOW: return "HIDE_WINDOW";
1150 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1151 case SEND_RESULT: return "SEND_RESULT";
1152 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1153 case BIND_APPLICATION: return "BIND_APPLICATION";
1154 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1155 case NEW_INTENT: return "NEW_INTENT";
1156 case RECEIVER: return "RECEIVER";
1157 case CREATE_SERVICE: return "CREATE_SERVICE";
1158 case SERVICE_ARGS: return "SERVICE_ARGS";
1159 case STOP_SERVICE: return "STOP_SERVICE";
1160 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1161 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1162 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1163 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1164 case BIND_SERVICE: return "BIND_SERVICE";
1165 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1166 case DUMP_SERVICE: return "DUMP_SERVICE";
1167 case LOW_MEMORY: return "LOW_MEMORY";
1168 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1169 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001170 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001171 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1172 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001173 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001174 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001175 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001176 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001177 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001178 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001179 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001180 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001181 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001182 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001183 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001184 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001185 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
1187 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001188 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001191 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 switch (msg.what) {
1193 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001194 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001195 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196
1197 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001198 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001199 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001200 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 } break;
1202 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001203 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001204 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001205 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001206 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 } break;
1208 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001209 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001211 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001212 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 break;
1214 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001215 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 break;
1219 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001220 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 break;
1224 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001225 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001232 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 break;
1234 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 handleResumeActivity((IBinder)msg.obj, true,
1242 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001243 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 break;
1245 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001246 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001248 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 break;
1250 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001251 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1253 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001254 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 break;
1256 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001257 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 AppBindData data = (AppBindData)msg.obj;
1259 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001260 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 break;
1262 case EXIT_APPLICATION:
1263 if (mInitialApplication != null) {
1264 mInitialApplication.onTerminate();
1265 }
1266 Looper.myLooper().quit();
1267 break;
1268 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001269 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001271 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 break;
1273 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001276 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 break;
1279 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001280 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 break;
1284 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001285 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 break;
1289 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001292 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001297 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 break;
1299 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001302 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 break;
1305 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001308 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 break;
1310 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001312 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001313 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001314 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 break;
1316 case CLEAN_UP_CONTEXT:
1317 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1318 cci.context.performFinalCleanup(cci.who, cci.what);
1319 break;
1320 case GC_WHEN_IDLE:
1321 scheduleGcIdler();
1322 break;
1323 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001324 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001327 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 break;
1331 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001332 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001336 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001337 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001338 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001339 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001341 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001343 break;
1344 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001345 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001346 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001348 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001349 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001350 Process.killProcess(Process.myPid());
1351 break;
1352 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001353 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001354 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001355 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001356 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001357 case ENABLE_JIT:
1358 ensureJitEnabled();
1359 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001360 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001361 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001362 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001364 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001365 case SCHEDULE_CRASH:
1366 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001367 case DUMP_HEAP:
1368 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1369 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001370 case DUMP_ACTIVITY:
1371 handleDumpActivity((DumpComponentInfo)msg.obj);
1372 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001373 case DUMP_PROVIDER:
1374 handleDumpProvider((DumpComponentInfo)msg.obj);
1375 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001376 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001377 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001378 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001379 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001380 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001381 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001382 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001383 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001384 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001385 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001386 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1387 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001388 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001389 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001390 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001391 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001392 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001393 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001394 case UNSTABLE_PROVIDER_DIED:
1395 handleUnstableProviderDied((IBinder)msg.obj, false);
1396 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001398 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
Bob Leee5408332009-09-04 18:31:17 -07001400
Brian Carlstromed7e0072011-03-24 13:27:57 -07001401 private void maybeSnapshot() {
1402 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001403 // convert the *private* ActivityThread.PackageInfo to *public* known
1404 // android.content.pm.PackageInfo
1405 String packageName = mBoundApplication.info.mPackageName;
1406 android.content.pm.PackageInfo packageInfo = null;
1407 try {
1408 Context context = getSystemContext();
1409 if(context == null) {
1410 Log.e(TAG, "cannot get a valid context");
1411 return;
1412 }
1413 PackageManager pm = context.getPackageManager();
1414 if(pm == null) {
1415 Log.e(TAG, "cannot get a valid PackageManager");
1416 return;
1417 }
1418 packageInfo = pm.getPackageInfo(
1419 packageName, PackageManager.GET_ACTIVITIES);
1420 } catch (NameNotFoundException e) {
1421 Log.e(TAG, "cannot get package info for " + packageName, e);
1422 }
1423 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001424 }
1425 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427
Romain Guy65b345f2011-07-27 18:51:50 -07001428 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001430 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001431 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001432 if (mBoundApplication != null && mProfiler.profileFd != null
1433 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001434 stopProfiling = true;
1435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436 if (a != null) {
1437 mNewActivities = null;
1438 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001439 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001441 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 TAG, "Reporting idle of " + a +
1443 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001444 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 if (a.activity != null && !a.activity.mFinished) {
1446 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001447 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001448 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001450 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 }
1452 }
1453 prev = a;
1454 a = a.nextIdle;
1455 prev.nextIdle = null;
1456 } while (a != null);
1457 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001458 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001459 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001460 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001461 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 return false;
1463 }
1464 }
1465
1466 final class GcIdler implements MessageQueue.IdleHandler {
1467 public final boolean queueIdle() {
1468 doGcIfNeeded();
1469 return false;
1470 }
1471 }
1472
Romain Guy65b345f2011-07-27 18:51:50 -07001473 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001474 final private String mResDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001475 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001476 final private float mScale;
1477 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001478
Dianne Hackborn756220b2012-08-14 16:45:30 -07001479 ResourcesKey(String resDir, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001480 mResDir = resDir;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001481 if (overrideConfiguration != null) {
1482 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1483 overrideConfiguration = null;
1484 }
1485 }
1486 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001487 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001488 int hash = 17;
1489 hash = 31 * hash + mResDir.hashCode();
1490 hash = 31 * hash + (mOverrideConfiguration != null
1491 ? mOverrideConfiguration.hashCode() : 0);
1492 hash = 31 * hash + Float.floatToIntBits(mScale);
1493 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001494 }
Bob Leee5408332009-09-04 18:31:17 -07001495
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001496 @Override
1497 public int hashCode() {
1498 return mHash;
1499 }
1500
1501 @Override
1502 public boolean equals(Object obj) {
1503 if (!(obj instanceof ResourcesKey)) {
1504 return false;
1505 }
1506 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001507 if (!mResDir.equals(peer.mResDir)) {
1508 return false;
1509 }
1510 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1511 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1512 return false;
1513 }
1514 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1515 return false;
1516 }
1517 }
1518 if (mScale != peer.mScale) {
1519 return false;
1520 }
1521 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001522 }
1523 }
1524
Romain Guy65b345f2011-07-27 18:51:50 -07001525 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001526 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
Romain Guy65b345f2011-07-27 18:51:50 -07001529 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001530 ActivityThread am = currentActivityThread();
1531 return (am != null && am.mBoundApplication != null)
1532 ? am.mBoundApplication.processName : null;
1533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534
Romain Guy65b345f2011-07-27 18:51:50 -07001535 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001536 ActivityThread am = currentActivityThread();
1537 return am != null ? am.mInitialApplication : null;
1538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001540 public static IPackageManager getPackageManager() {
1541 if (sPackageManager != null) {
1542 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1543 return sPackageManager;
1544 }
1545 IBinder b = ServiceManager.getService("package");
1546 //Slog.v("PackageManager", "default service binder = " + b);
1547 sPackageManager = IPackageManager.Stub.asInterface(b);
1548 //Slog.v("PackageManager", "default service = " + sPackageManager);
1549 return sPackageManager;
1550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001552 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1553 DisplayMetrics dm = mDisplayMetrics.get(ci);
1554 if (dm != null && !forceUpdate) {
1555 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001556 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001557
1558 DisplayManager displayManager = DisplayManager.getInstance();
1559 if (displayManager == null) {
1560 // may be null early in system startup
1561 dm = new DisplayMetrics();
1562 dm.setToDefaults();
1563 return dm;
1564 }
1565
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001566 if (dm == null) {
1567 dm = new DisplayMetrics();
1568 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001569 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001570
Jeff Brownd32460c2012-07-20 16:15:36 -07001571 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1572 cih.set(ci);
Jeff Brown98365d72012-08-19 20:30:52 -07001573 Display d = displayManager.getCompatibleDisplay(Display.DEFAULT_DISPLAY, cih);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001574 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001575 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1576 // + metrics.heightPixels + " den=" + metrics.density
1577 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001578 return dm;
1579 }
1580
Romain Guy65b345f2011-07-27 18:51:50 -07001581 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001582 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1583 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001584 if (config == null) {
1585 return null;
1586 }
1587 if (compat != null && !compat.supportsScreen()) {
1588 mMainThreadConfig.setTo(config);
1589 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001590 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001591 }
1592 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001595 /**
1596 * Creates the top level Resources for applications with the given compatibility info.
1597 *
1598 * @param resDir the resource directory.
1599 * @param compInfo the compability info. It will use the default compatibility info when it's
1600 * null.
1601 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001602 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1603 CompatibilityInfo compInfo) {
1604 ResourcesKey key = new ResourcesKey(resDir, overrideConfiguration,
1605 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001606 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001608 // Resources is app scale dependent.
1609 if (false) {
1610 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1611 + compInfo.applicationScale);
1612 }
1613 WeakReference<Resources> wr = mActiveResources.get(key);
1614 r = wr != null ? wr.get() : null;
1615 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1616 if (r != null && r.getAssets().isUpToDate()) {
1617 if (false) {
1618 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1619 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1620 }
1621 return r;
1622 }
1623 }
1624
1625 //if (r != null) {
1626 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1627 // + r + " " + resDir);
1628 //}
1629
1630 AssetManager assets = new AssetManager();
1631 if (assets.addAssetPath(resDir) == 0) {
1632 return null;
1633 }
1634
1635 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001636 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001637 Configuration config;
1638 if (key.mOverrideConfiguration != null) {
1639 config = new Configuration(getConfiguration());
1640 config.updateFrom(key.mOverrideConfiguration);
1641 } else {
1642 config = getConfiguration();
1643 }
1644 r = new Resources(assets, metrics, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001645 if (false) {
1646 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1647 + r.getConfiguration() + " appScale="
1648 + r.getCompatibilityInfo().applicationScale);
1649 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001650
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001651 synchronized (mPackages) {
1652 WeakReference<Resources> wr = mActiveResources.get(key);
1653 Resources existing = wr != null ? wr.get() : null;
1654 if (existing != null && existing.getAssets().isUpToDate()) {
1655 // Someone else already created the resources while we were
1656 // unlocked; go ahead and use theirs.
1657 r.getAssets().close();
1658 return existing;
1659 }
1660
1661 // XXX need to remove entries when weak references go away
1662 mActiveResources.put(key, new WeakReference<Resources>(r));
1663 return r;
1664 }
1665 }
1666
1667 /**
1668 * Creates the top level resources for the given package.
1669 */
Dianne Hackborn756220b2012-08-14 16:45:30 -07001670 Resources getTopLevelResources(String resDir, Configuration overrideConfiguration,
1671 LoadedApk pkgInfo) {
1672 return getTopLevelResources(resDir, overrideConfiguration,
1673 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001674 }
1675
1676 final Handler getHandler() {
1677 return mH;
1678 }
1679
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001680 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1681 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001682 synchronized (mPackages) {
1683 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1685 ref = mPackages.get(packageName);
1686 } else {
1687 ref = mResourcePackages.get(packageName);
1688 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001689 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001690 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001691 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1692 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 if (packageInfo != null && (packageInfo.mResources == null
1694 || packageInfo.mResources.getAssets().isUpToDate())) {
1695 if (packageInfo.isSecurityViolation()
1696 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1697 throw new SecurityException(
1698 "Requesting code from " + packageName
1699 + " to be run in process "
1700 + mBoundApplication.processName
1701 + "/" + mBoundApplication.appInfo.uid);
1702 }
1703 return packageInfo;
1704 }
1705 }
1706
1707 ApplicationInfo ai = null;
1708 try {
1709 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001710 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001712 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
1714
1715 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001716 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718
1719 return null;
1720 }
1721
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001722 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1723 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1725 boolean securityViolation = includeCode && ai.uid != 0
1726 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001727 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001728 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1730 |Context.CONTEXT_IGNORE_SECURITY))
1731 == Context.CONTEXT_INCLUDE_CODE) {
1732 if (securityViolation) {
1733 String msg = "Requesting code from " + ai.packageName
1734 + " (with uid " + ai.uid + ")";
1735 if (mBoundApplication != null) {
1736 msg = msg + " to be run in process "
1737 + mBoundApplication.processName + " (with uid "
1738 + mBoundApplication.appInfo.uid + ")";
1739 }
1740 throw new SecurityException(msg);
1741 }
1742 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001743 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001746 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1747 CompatibilityInfo compatInfo) {
1748 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001751 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1752 synchronized (mPackages) {
1753 WeakReference<LoadedApk> ref;
1754 if (includeCode) {
1755 ref = mPackages.get(packageName);
1756 } else {
1757 ref = mResourcePackages.get(packageName);
1758 }
1759 return ref != null ? ref.get() : null;
1760 }
1761 }
1762
Romain Guy65b345f2011-07-27 18:51:50 -07001763 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1765 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001766 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 if (includeCode) {
1768 ref = mPackages.get(aInfo.packageName);
1769 } else {
1770 ref = mResourcePackages.get(aInfo.packageName);
1771 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001772 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (packageInfo == null || (packageInfo.mResources != null
1774 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001775 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 : "Loading resource-only package ") + aInfo.packageName
1777 + " (in " + (mBoundApplication != null
1778 ? mBoundApplication.processName : null)
1779 + ")");
1780 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001781 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 securityViolation, includeCode &&
1783 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1784 if (includeCode) {
1785 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001786 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 } else {
1788 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001789 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
1791 }
1792 return packageInfo;
1793 }
1794 }
1795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 ActivityThread() {
1797 }
1798
1799 public ApplicationThread getApplicationThread()
1800 {
1801 return mAppThread;
1802 }
1803
1804 public Instrumentation getInstrumentation()
1805 {
1806 return mInstrumentation;
1807 }
1808
1809 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001810 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 }
1812
1813 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001814 return mProfiler != null && mProfiler.profileFile != null
1815 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 }
1817
1818 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001819 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
1821
1822 public Looper getLooper() {
1823 return mLooper;
1824 }
1825
1826 public Application getApplication() {
1827 return mInitialApplication;
1828 }
Bob Leee5408332009-09-04 18:31:17 -07001829
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001830 public String getProcessName() {
1831 return mBoundApplication.processName;
1832 }
Bob Leee5408332009-09-04 18:31:17 -07001833
Dianne Hackborn21556372010-02-04 16:34:40 -08001834 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 synchronized (this) {
1836 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001837 ContextImpl context =
1838 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001839 LoadedApk info = new LoadedApk(this, "android", context, null,
1840 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 context.init(info, null, this);
1842 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001843 getConfiguration(), getDisplayMetricsLocked(
1844 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001846 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 // + ": " + context.getResources().getConfiguration());
1848 }
1849 }
1850 return mSystemContext;
1851 }
1852
Mike Cleron432b7132009-09-24 15:28:29 -07001853 public void installSystemApplicationInfo(ApplicationInfo info) {
1854 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001855 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001856 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001857 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001858
1859 // give ourselves a default profiler
1860 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001861 }
1862 }
1863
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001864 void ensureJitEnabled() {
1865 if (!mJitEnabled) {
1866 mJitEnabled = true;
1867 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1868 }
1869 }
1870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 void scheduleGcIdler() {
1872 if (!mGcIdlerScheduled) {
1873 mGcIdlerScheduled = true;
1874 Looper.myQueue().addIdleHandler(mGcIdler);
1875 }
1876 mH.removeMessages(H.GC_WHEN_IDLE);
1877 }
1878
1879 void unscheduleGcIdler() {
1880 if (mGcIdlerScheduled) {
1881 mGcIdlerScheduled = false;
1882 Looper.myQueue().removeIdleHandler(mGcIdler);
1883 }
1884 mH.removeMessages(H.GC_WHEN_IDLE);
1885 }
1886
1887 void doGcIfNeeded() {
1888 mGcIdlerScheduled = false;
1889 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001890 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 // + "m now=" + now);
1892 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001893 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 BinderInternal.forceGc("bg");
1895 }
1896 }
1897
Jeff Hamilton52d32032011-01-08 15:31:26 -06001898 public void registerOnActivityPausedListener(Activity activity,
1899 OnActivityPausedListener listener) {
1900 synchronized (mOnPauseListeners) {
1901 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1902 if (list == null) {
1903 list = new ArrayList<OnActivityPausedListener>();
1904 mOnPauseListeners.put(activity, list);
1905 }
1906 list.add(listener);
1907 }
1908 }
1909
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001910 public void unregisterOnActivityPausedListener(Activity activity,
1911 OnActivityPausedListener listener) {
1912 synchronized (mOnPauseListeners) {
1913 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1914 if (list != null) {
1915 list.remove(listener);
1916 }
1917 }
1918 }
1919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 public final ActivityInfo resolveActivityInfo(Intent intent) {
1921 ActivityInfo aInfo = intent.resolveActivityInfo(
1922 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1923 if (aInfo == null) {
1924 // Throw an exception.
1925 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001926 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 }
1928 return aInfo;
1929 }
Bob Leee5408332009-09-04 18:31:17 -07001930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001933 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001934 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001936 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 r.intent = intent;
1938 r.state = state;
1939 r.parent = parent;
1940 r.embeddedID = id;
1941 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001942 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 if (localLOGV) {
1944 ComponentName compname = intent.getComponent();
1945 String name;
1946 if (compname != null) {
1947 name = compname.toShortString();
1948 } else {
1949 name = "(Intent " + intent + ").getComponent() returned null";
1950 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001951 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 + ", comp=" + name
1953 + ", token=" + token);
1954 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001955 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957
1958 public final Activity getActivity(IBinder token) {
1959 return mActivities.get(token).activity;
1960 }
1961
1962 public final void sendActivityResult(
1963 IBinder token, String id, int requestCode,
1964 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001965 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001966 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1968 list.add(new ResultInfo(id, requestCode, resultCode, data));
1969 mAppThread.scheduleSendResult(token, list);
1970 }
1971
1972 // if the thread hasn't started yet, we don't have the handler, so just
1973 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001974 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 queueOrSendMessage(what, obj, 0, 0);
1976 }
1977
Romain Guy65b345f2011-07-27 18:51:50 -07001978 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 queueOrSendMessage(what, obj, arg1, 0);
1980 }
1981
Romain Guy65b345f2011-07-27 18:51:50 -07001982 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001984 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1986 + ": " + arg1 + " / " + obj);
1987 Message msg = Message.obtain();
1988 msg.what = what;
1989 msg.obj = obj;
1990 msg.arg1 = arg1;
1991 msg.arg2 = arg2;
1992 mH.sendMessage(msg);
1993 }
1994 }
1995
Dianne Hackborn21556372010-02-04 16:34:40 -08001996 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 String what) {
1998 ContextCleanupInfo cci = new ContextCleanupInfo();
1999 cci.context = context;
2000 cci.who = who;
2001 cci.what = what;
2002 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2003 }
2004
Romain Guy65b345f2011-07-27 18:51:50 -07002005 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2007
2008 ActivityInfo aInfo = r.activityInfo;
2009 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002010 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 Context.CONTEXT_INCLUDE_CODE);
2012 }
Bob Leee5408332009-09-04 18:31:17 -07002013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 ComponentName component = r.intent.getComponent();
2015 if (component == null) {
2016 component = r.intent.resolveActivity(
2017 mInitialApplication.getPackageManager());
2018 r.intent.setComponent(component);
2019 }
2020
2021 if (r.activityInfo.targetActivity != null) {
2022 component = new ComponentName(r.activityInfo.packageName,
2023 r.activityInfo.targetActivity);
2024 }
2025
2026 Activity activity = null;
2027 try {
2028 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2029 activity = mInstrumentation.newActivity(
2030 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002031 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 r.intent.setExtrasClassLoader(cl);
2033 if (r.state != null) {
2034 r.state.setClassLoader(cl);
2035 }
2036 } catch (Exception e) {
2037 if (!mInstrumentation.onException(activity, e)) {
2038 throw new RuntimeException(
2039 "Unable to instantiate activity " + component
2040 + ": " + e.toString(), e);
2041 }
2042 }
2043
2044 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002045 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002046
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002047 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2048 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 TAG, r + ": app=" + app
2050 + ", appName=" + app.getPackageName()
2051 + ", pkg=" + r.packageInfo.getPackageName()
2052 + ", comp=" + r.intent.getComponent().toShortString()
2053 + ", dir=" + r.packageInfo.getAppDir());
2054
2055 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002056 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 appContext.init(r.packageInfo, r.token, this);
2058 appContext.setOuterContext(activity);
2059 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002060 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002061 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002062 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002063 activity.attach(appContext, this, getInstrumentation(), r.token,
2064 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002065 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002066
Christopher Tateb70f3df2009-04-07 16:07:59 -07002067 if (customIntent != null) {
2068 activity.mIntent = customIntent;
2069 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002070 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 activity.mStartedActivity = false;
2072 int theme = r.activityInfo.getThemeResource();
2073 if (theme != 0) {
2074 activity.setTheme(theme);
2075 }
2076
2077 activity.mCalled = false;
2078 mInstrumentation.callActivityOnCreate(activity, r.state);
2079 if (!activity.mCalled) {
2080 throw new SuperNotCalledException(
2081 "Activity " + r.intent.getComponent().toShortString() +
2082 " did not call through to super.onCreate()");
2083 }
2084 r.activity = activity;
2085 r.stopped = true;
2086 if (!r.activity.mFinished) {
2087 activity.performStart();
2088 r.stopped = false;
2089 }
2090 if (!r.activity.mFinished) {
2091 if (r.state != null) {
2092 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2093 }
2094 }
2095 if (!r.activity.mFinished) {
2096 activity.mCalled = false;
2097 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2098 if (!activity.mCalled) {
2099 throw new SuperNotCalledException(
2100 "Activity " + r.intent.getComponent().toShortString() +
2101 " did not call through to super.onPostCreate()");
2102 }
2103 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 }
2105 r.paused = true;
2106
2107 mActivities.put(r.token, r);
2108
2109 } catch (SuperNotCalledException e) {
2110 throw e;
2111
2112 } catch (Exception e) {
2113 if (!mInstrumentation.onException(activity, e)) {
2114 throw new RuntimeException(
2115 "Unable to start activity " + component
2116 + ": " + e.toString(), e);
2117 }
2118 }
2119
2120 return activity;
2121 }
2122
Romain Guy65b345f2011-07-27 18:51:50 -07002123 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 // If we are getting ready to gc after going to the background, well
2125 // we are back active so skip it.
2126 unscheduleGcIdler();
2127
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002128 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002129 mProfiler.setProfiler(r.profileFile, r.profileFd);
2130 mProfiler.startProfiling();
2131 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002132 }
2133
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002134 // Make sure we are running with the most recent config.
2135 handleConfigurationChanged(null, null);
2136
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002137 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002139 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140
2141 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002142 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002143 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 handleResumeActivity(r.token, false, r.isForward);
2145
2146 if (!r.activity.mFinished && r.startsNotResumed) {
2147 // The activity manager actually wants this one to start out
2148 // paused, because it needs to be visible but isn't in the
2149 // foreground. We accomplish this by going through the
2150 // normal startup (because activities expect to go through
2151 // onResume() the first time they run, before their window
2152 // is displayed), and then pausing it. However, in this case
2153 // we do -not- need to do the full pause cycle (of freezing
2154 // and such) because the activity manager assumes it can just
2155 // retain the current state it has.
2156 try {
2157 r.activity.mCalled = false;
2158 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002159 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002160 // we need to be created again. But we only do this
2161 // for pre-Honeycomb apps, which always save their state
2162 // when pausing, so we can not have them save their state
2163 // when restarting from a paused state. For HC and later,
2164 // we want to (and can) let the state be saved as the normal
2165 // part of stopping the activity.
2166 if (r.isPreHoneycomb()) {
2167 r.state = oldState;
2168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 if (!r.activity.mCalled) {
2170 throw new SuperNotCalledException(
2171 "Activity " + r.intent.getComponent().toShortString() +
2172 " did not call through to super.onPause()");
2173 }
2174
2175 } catch (SuperNotCalledException e) {
2176 throw e;
2177
2178 } catch (Exception e) {
2179 if (!mInstrumentation.onException(r.activity, e)) {
2180 throw new RuntimeException(
2181 "Unable to pause activity "
2182 + r.intent.getComponent().toShortString()
2183 + ": " + e.toString(), e);
2184 }
2185 }
2186 r.paused = true;
2187 }
2188 } else {
2189 // If there was an error, for any reason, tell the activity
2190 // manager to stop us.
2191 try {
2192 ActivityManagerNative.getDefault()
2193 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2194 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002195 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
2197 }
2198 }
2199
Romain Guy65b345f2011-07-27 18:51:50 -07002200 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002201 List<Intent> intents) {
2202 final int N = intents.size();
2203 for (int i=0; i<N; i++) {
2204 Intent intent = intents.get(i);
2205 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002206 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2208 }
2209 }
2210
2211 public final void performNewIntents(IBinder token,
2212 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002213 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 if (r != null) {
2215 final boolean resumed = !r.paused;
2216 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002217 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 mInstrumentation.callActivityOnPause(r.activity);
2219 }
2220 deliverNewIntents(r, intents);
2221 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002222 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002223 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 }
2225 }
2226 }
Bob Leee5408332009-09-04 18:31:17 -07002227
Romain Guy65b345f2011-07-27 18:51:50 -07002228 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 performNewIntents(data.token, data.intents);
2230 }
2231
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002232 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2233
2234 /**
2235 * Return the Intent that's currently being handled by a
2236 * BroadcastReceiver on this thread, or null if none.
2237 * @hide
2238 */
2239 public static Intent getIntentBeingBroadcast() {
2240 return sCurrentBroadcastIntent.get();
2241 }
2242
Romain Guy65b345f2011-07-27 18:51:50 -07002243 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 // If we are getting ready to gc after going to the background, well
2245 // we are back active so skip it.
2246 unscheduleGcIdler();
2247
2248 String component = data.intent.getComponent().getClassName();
2249
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002250 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002251 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252
2253 IActivityManager mgr = ActivityManagerNative.getDefault();
2254
Romain Guy65b345f2011-07-27 18:51:50 -07002255 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002256 try {
2257 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2258 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002259 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2261 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002262 if (DEBUG_BROADCAST) Slog.i(TAG,
2263 "Finishing failed broadcast to " + data.intent.getComponent());
2264 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 throw new RuntimeException(
2266 "Unable to instantiate receiver " + component
2267 + ": " + e.toString(), e);
2268 }
2269
2270 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002271 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002272
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002273 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 TAG, "Performing receive of " + data.intent
2275 + ": app=" + app
2276 + ", appName=" + app.getPackageName()
2277 + ", pkg=" + packageInfo.getPackageName()
2278 + ", comp=" + data.intent.getComponent().toShortString()
2279 + ", dir=" + packageInfo.getAppDir());
2280
Dianne Hackborn21556372010-02-04 16:34:40 -08002281 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002282 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002283 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 receiver.onReceive(context.getReceiverRestrictedContext(),
2285 data.intent);
2286 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002287 if (DEBUG_BROADCAST) Slog.i(TAG,
2288 "Finishing failed broadcast to " + data.intent.getComponent());
2289 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 if (!mInstrumentation.onException(receiver, e)) {
2291 throw new RuntimeException(
2292 "Unable to start receiver " + component
2293 + ": " + e.toString(), e);
2294 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002295 } finally {
2296 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 }
2298
Dianne Hackborne829fef2010-10-26 17:44:01 -07002299 if (receiver.getPendingResult() != null) {
2300 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 }
2302 }
2303
Christopher Tate181fafa2009-05-14 11:12:14 -07002304 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002305 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002306 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002307
2308 // no longer idle; we have backup work to do
2309 unscheduleGcIdler();
2310
2311 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002312 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002313 String packageName = packageInfo.mPackageName;
2314 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002315 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002316 + " already exists");
2317 return;
2318 }
Bob Leee5408332009-09-04 18:31:17 -07002319
Christopher Tate181fafa2009-05-14 11:12:14 -07002320 BackupAgent agent = null;
2321 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002322
Christopher Tate79ec80d2011-06-24 14:58:49 -07002323 // full backup operation but no app-supplied agent? use the default implementation
2324 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2325 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002326 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002327 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002328
Christopher Tate181fafa2009-05-14 11:12:14 -07002329 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002330 IBinder binder = null;
2331 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002332 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2333
Christopher Tated1475e02009-07-09 15:36:17 -07002334 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002335 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002336
2337 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002338 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002339 context.init(packageInfo, null, this);
2340 context.setOuterContext(agent);
2341 agent.attach(context);
2342
2343 agent.onCreate();
2344 binder = agent.onBind();
2345 mBackupAgents.put(packageName, agent);
2346 } catch (Exception e) {
2347 // If this is during restore, fail silently; otherwise go
2348 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002349 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002350 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2351 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002352 throw e;
2353 }
2354 // falling through with 'binder' still null
2355 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002356
2357 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002358 try {
2359 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2360 } catch (RemoteException e) {
2361 // nothing to do.
2362 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002363 } catch (Exception e) {
2364 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002365 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002366 }
2367 }
2368
2369 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002370 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002371 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002372
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002373 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002374 String packageName = packageInfo.mPackageName;
2375 BackupAgent agent = mBackupAgents.get(packageName);
2376 if (agent != null) {
2377 try {
2378 agent.onDestroy();
2379 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002380 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002381 e.printStackTrace();
2382 }
2383 mBackupAgents.remove(packageName);
2384 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002385 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002386 }
2387 }
2388
Romain Guy65b345f2011-07-27 18:51:50 -07002389 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 // If we are getting ready to gc after going to the background, well
2391 // we are back active so skip it.
2392 unscheduleGcIdler();
2393
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002394 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002395 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 Service service = null;
2397 try {
2398 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2399 service = (Service) cl.loadClass(data.info.name).newInstance();
2400 } catch (Exception e) {
2401 if (!mInstrumentation.onException(service, e)) {
2402 throw new RuntimeException(
2403 "Unable to instantiate service " + data.info.name
2404 + ": " + e.toString(), e);
2405 }
2406 }
2407
2408 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002409 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410
Dianne Hackborn21556372010-02-04 16:34:40 -08002411 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 context.init(packageInfo, null, this);
2413
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002414 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 context.setOuterContext(service);
2416 service.attach(context, this, data.info.name, data.token, app,
2417 ActivityManagerNative.getDefault());
2418 service.onCreate();
2419 mServices.put(data.token, service);
2420 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002421 ActivityManagerNative.getDefault().serviceDoneExecuting(
2422 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 } catch (RemoteException e) {
2424 // nothing to do.
2425 }
2426 } catch (Exception e) {
2427 if (!mInstrumentation.onException(service, e)) {
2428 throw new RuntimeException(
2429 "Unable to create service " + data.info.name
2430 + ": " + e.toString(), e);
2431 }
2432 }
2433 }
2434
Romain Guy65b345f2011-07-27 18:51:50 -07002435 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002437 if (DEBUG_SERVICE)
2438 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 if (s != null) {
2440 try {
2441 data.intent.setExtrasClassLoader(s.getClassLoader());
2442 try {
2443 if (!data.rebind) {
2444 IBinder binder = s.onBind(data.intent);
2445 ActivityManagerNative.getDefault().publishService(
2446 data.token, data.intent, binder);
2447 } else {
2448 s.onRebind(data.intent);
2449 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002450 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002452 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 } catch (RemoteException ex) {
2454 }
2455 } catch (Exception e) {
2456 if (!mInstrumentation.onException(s, e)) {
2457 throw new RuntimeException(
2458 "Unable to bind to service " + s
2459 + " with " + data.intent + ": " + e.toString(), e);
2460 }
2461 }
2462 }
2463 }
2464
Romain Guy65b345f2011-07-27 18:51:50 -07002465 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 Service s = mServices.get(data.token);
2467 if (s != null) {
2468 try {
2469 data.intent.setExtrasClassLoader(s.getClassLoader());
2470 boolean doRebind = s.onUnbind(data.intent);
2471 try {
2472 if (doRebind) {
2473 ActivityManagerNative.getDefault().unbindFinished(
2474 data.token, data.intent, doRebind);
2475 } else {
2476 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002477 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 }
2479 } catch (RemoteException ex) {
2480 }
2481 } catch (Exception e) {
2482 if (!mInstrumentation.onException(s, e)) {
2483 throw new RuntimeException(
2484 "Unable to unbind to service " + s
2485 + " with " + data.intent + ": " + e.toString(), e);
2486 }
2487 }
2488 }
2489 }
2490
Dianne Hackborn625ac272010-09-17 18:29:22 -07002491 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002492 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2493 try {
2494 Service s = mServices.get(info.token);
2495 if (s != null) {
2496 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2497 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2498 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002500 } finally {
2501 IoUtils.closeQuietly(info.fd);
2502 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 }
2504 }
2505
Dianne Hackborn625ac272010-09-17 18:29:22 -07002506 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002507 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2508 try {
2509 ActivityClientRecord r = mActivities.get(info.token);
2510 if (r != null && r.activity != null) {
2511 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2512 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2513 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002514 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002515 } finally {
2516 IoUtils.closeQuietly(info.fd);
2517 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002518 }
2519 }
2520
Marco Nelissen18cb2872011-11-15 11:19:53 -08002521 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002522 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2523 try {
2524 ProviderClientRecord r = mLocalProviders.get(info.token);
2525 if (r != null && r.mLocalProvider != null) {
2526 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2527 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2528 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002529 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002530 } finally {
2531 IoUtils.closeQuietly(info.fd);
2532 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002533 }
2534 }
2535
Romain Guy65b345f2011-07-27 18:51:50 -07002536 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 Service s = mServices.get(data.token);
2538 if (s != null) {
2539 try {
2540 if (data.args != null) {
2541 data.args.setExtrasClassLoader(s.getClassLoader());
2542 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002543 int res;
2544 if (!data.taskRemoved) {
2545 res = s.onStartCommand(data.args, data.flags, data.startId);
2546 } else {
2547 s.onTaskRemoved(data.args);
2548 res = Service.START_TASK_REMOVED_COMPLETE;
2549 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002550
2551 QueuedWork.waitToFinish();
2552
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002554 ActivityManagerNative.getDefault().serviceDoneExecuting(
2555 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 } catch (RemoteException e) {
2557 // nothing to do.
2558 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002559 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 } catch (Exception e) {
2561 if (!mInstrumentation.onException(s, e)) {
2562 throw new RuntimeException(
2563 "Unable to start service " + s
2564 + " with " + data.args + ": " + e.toString(), e);
2565 }
2566 }
2567 }
2568 }
2569
Romain Guy65b345f2011-07-27 18:51:50 -07002570 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 Service s = mServices.remove(token);
2572 if (s != null) {
2573 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002574 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 s.onDestroy();
2576 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002577 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002579 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002581
2582 QueuedWork.waitToFinish();
2583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002585 ActivityManagerNative.getDefault().serviceDoneExecuting(
2586 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 } catch (RemoteException e) {
2588 // nothing to do.
2589 }
2590 } catch (Exception e) {
2591 if (!mInstrumentation.onException(s, e)) {
2592 throw new RuntimeException(
2593 "Unable to stop service " + s
2594 + ": " + e.toString(), e);
2595 }
2596 }
2597 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002598 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
2600
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002601 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002603 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002604 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 + " finished=" + r.activity.mFinished);
2606 if (r != null && !r.activity.mFinished) {
2607 if (clearHide) {
2608 r.hideForNow = false;
2609 r.activity.mStartedActivity = false;
2610 }
2611 try {
2612 if (r.pendingIntents != null) {
2613 deliverNewIntents(r, r.pendingIntents);
2614 r.pendingIntents = null;
2615 }
2616 if (r.pendingResults != null) {
2617 deliverResults(r, r.pendingResults);
2618 r.pendingResults = null;
2619 }
2620 r.activity.performResume();
2621
Bob Leee5408332009-09-04 18:31:17 -07002622 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 r.paused = false;
2626 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 r.state = null;
2628 } catch (Exception e) {
2629 if (!mInstrumentation.onException(r.activity, e)) {
2630 throw new RuntimeException(
2631 "Unable to resume activity "
2632 + r.intent.getComponent().toShortString()
2633 + ": " + e.toString(), e);
2634 }
2635 }
2636 }
2637 return r;
2638 }
2639
Romain Guya998dff2012-03-23 18:58:36 -07002640 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002641 if (r.mPendingRemoveWindow != null) {
2642 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2643 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2644 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002645 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002646 r.activity.getClass().getName(), "Activity");
2647 }
2648 }
2649 r.mPendingRemoveWindow = null;
2650 r.mPendingRemoveWindowManager = null;
2651 }
2652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2654 // If we are getting ready to gc after going to the background, well
2655 // we are back active so skip it.
2656 unscheduleGcIdler();
2657
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002658 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659
2660 if (r != null) {
2661 final Activity a = r.activity;
2662
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002663 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 TAG, "Resume " + r + " started activity: " +
2665 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2666 + ", finished: " + a.mFinished);
2667
2668 final int forwardBit = isForward ?
2669 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002671 // If the window hasn't yet been added to the window manager,
2672 // and this guy didn't finish itself or start another activity,
2673 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002674 boolean willBeVisible = !a.mStartedActivity;
2675 if (!willBeVisible) {
2676 try {
2677 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2678 a.getActivityToken());
2679 } catch (RemoteException e) {
2680 }
2681 }
2682 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683 r.window = r.activity.getWindow();
2684 View decor = r.window.getDecorView();
2685 decor.setVisibility(View.INVISIBLE);
2686 ViewManager wm = a.getWindowManager();
2687 WindowManager.LayoutParams l = r.window.getAttributes();
2688 a.mDecor = decor;
2689 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2690 l.softInputMode |= forwardBit;
2691 if (a.mVisibleFromClient) {
2692 a.mWindowAdded = true;
2693 wm.addView(decor, l);
2694 }
2695
2696 // If the window has already been added, but during resume
2697 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002698 // window visible.
2699 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002700 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 TAG, "Launch " + r + " mStartedActivity set");
2702 r.hideForNow = true;
2703 }
2704
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002705 // Get rid of anything left hanging around.
2706 cleanUpPendingRemoveWindows(r);
2707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 // The window is now visible if it has been added, we are not
2709 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002710 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002711 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002713 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002714 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002716 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 r.newConfig = null;
2718 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002719 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 + isForward);
2721 WindowManager.LayoutParams l = r.window.getAttributes();
2722 if ((l.softInputMode
2723 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2724 != forwardBit) {
2725 l.softInputMode = (l.softInputMode
2726 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2727 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002728 if (r.activity.mVisibleFromClient) {
2729 ViewManager wm = a.getWindowManager();
2730 View decor = r.window.getDecorView();
2731 wm.updateViewLayout(decor, l);
2732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 }
2734 r.activity.mVisibleFromServer = true;
2735 mNumVisibleActivities++;
2736 if (r.activity.mVisibleFromClient) {
2737 r.activity.makeVisible();
2738 }
2739 }
2740
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002741 if (!r.onlyLocalRequest) {
2742 r.nextIdle = mNewActivities;
2743 mNewActivities = r;
2744 if (localLOGV) Slog.v(
2745 TAG, "Scheduling idle handler for " + r);
2746 Looper.myQueue().addIdleHandler(new Idler());
2747 }
2748 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749
2750 } else {
2751 // If an exception was thrown when trying to resume, then
2752 // just end this activity.
2753 try {
2754 ActivityManagerNative.getDefault()
2755 .finishActivity(token, Activity.RESULT_CANCELED, null);
2756 } catch (RemoteException ex) {
2757 }
2758 }
2759 }
2760
2761 private int mThumbnailWidth = -1;
2762 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002763 private Bitmap mAvailThumbnailBitmap = null;
2764 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765
Romain Guy65b345f2011-07-27 18:51:50 -07002766 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002767 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002769 if (thumbnail == null) {
2770 int w = mThumbnailWidth;
2771 int h;
2772 if (w < 0) {
2773 Resources res = r.activity.getResources();
2774 mThumbnailHeight = h =
2775 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002776
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002777 mThumbnailWidth = w =
2778 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2779 } else {
2780 h = mThumbnailHeight;
2781 }
2782
2783 // On platforms where we don't want thumbnails, set dims to (0,0)
2784 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002785 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2786 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002787 thumbnail.eraseColor(0);
2788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 }
2790
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002791 if (thumbnail != null) {
2792 Canvas cv = mThumbnailCanvas;
2793 if (cv == null) {
2794 mThumbnailCanvas = cv = new Canvas();
2795 }
2796
2797 cv.setBitmap(thumbnail);
2798 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2799 mAvailThumbnailBitmap = thumbnail;
2800 thumbnail = null;
2801 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002802 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 } catch (Exception e) {
2806 if (!mInstrumentation.onException(r.activity, e)) {
2807 throw new RuntimeException(
2808 "Unable to create thumbnail of "
2809 + r.intent.getComponent().toShortString()
2810 + ": " + e.toString(), e);
2811 }
2812 thumbnail = null;
2813 }
2814
2815 return thumbnail;
2816 }
2817
Romain Guy65b345f2011-07-27 18:51:50 -07002818 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002820 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002822 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002823 if (userLeaving) {
2824 performUserLeavingActivity(r);
2825 }
Bob Leee5408332009-09-04 18:31:17 -07002826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002828 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002829
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002830 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002831 if (r.isPreHoneycomb()) {
2832 QueuedWork.waitToFinish();
2833 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 // Tell the activity manager we have paused.
2836 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002837 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 } catch (RemoteException ex) {
2839 }
2840 }
2841 }
2842
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002843 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 mInstrumentation.callActivityOnUserLeaving(r.activity);
2845 }
2846
2847 final Bundle performPauseActivity(IBinder token, boolean finished,
2848 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002849 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 return r != null ? performPauseActivity(r, finished, saveState) : null;
2851 }
2852
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002853 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 boolean saveState) {
2855 if (r.paused) {
2856 if (r.activity.mFinished) {
2857 // If we are finishing, we won't call onResume() in certain cases.
2858 // So here we likewise don't want to call onPause() if the activity
2859 // isn't resumed.
2860 return null;
2861 }
2862 RuntimeException e = new RuntimeException(
2863 "Performing pause of activity that is not resumed: "
2864 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002865 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 }
2867 Bundle state = null;
2868 if (finished) {
2869 r.activity.mFinished = true;
2870 }
2871 try {
2872 // Next have the activity save its current state and managed dialogs...
2873 if (!r.activity.mFinished && saveState) {
2874 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002875 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2877 r.state = state;
2878 }
2879 // Now we are idle.
2880 r.activity.mCalled = false;
2881 mInstrumentation.callActivityOnPause(r.activity);
2882 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2883 if (!r.activity.mCalled) {
2884 throw new SuperNotCalledException(
2885 "Activity " + r.intent.getComponent().toShortString() +
2886 " did not call through to super.onPause()");
2887 }
2888
2889 } catch (SuperNotCalledException e) {
2890 throw e;
2891
2892 } catch (Exception e) {
2893 if (!mInstrumentation.onException(r.activity, e)) {
2894 throw new RuntimeException(
2895 "Unable to pause activity "
2896 + r.intent.getComponent().toShortString()
2897 + ": " + e.toString(), e);
2898 }
2899 }
2900 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002901
2902 // Notify any outstanding on paused listeners
2903 ArrayList<OnActivityPausedListener> listeners;
2904 synchronized (mOnPauseListeners) {
2905 listeners = mOnPauseListeners.remove(r.activity);
2906 }
2907 int size = (listeners != null ? listeners.size() : 0);
2908 for (int i = 0; i < size; i++) {
2909 listeners.get(i).onPaused(r.activity);
2910 }
2911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002912 return state;
2913 }
2914
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002915 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002916 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002917 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002920 private static class StopInfo implements Runnable {
2921 ActivityClientRecord activity;
2922 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 Bitmap thumbnail;
2924 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002925
2926 @Override public void run() {
2927 // Tell activity manager we have been stopped.
2928 try {
2929 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2930 ActivityManagerNative.getDefault().activityStopped(
2931 activity.token, state, thumbnail, description);
2932 } catch (RemoteException ex) {
2933 }
2934 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002937 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002938 public final IActivityManager.ContentProviderHolder holder;
2939 public final ProviderClientRecord client;
2940 public int stableCount;
2941 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002942
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002943 // When this is set, the stable and unstable ref counts are 0 and
2944 // we have a pending operation scheduled to remove the ref count
2945 // from the activity manager. On the activity manager we are still
2946 // holding an unstable ref, though it is not reflected in the counts
2947 // here.
2948 public boolean removePending;
2949
2950 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2951 ProviderClientRecord inClient, int sCount, int uCount) {
2952 holder = inHolder;
2953 client = inClient;
2954 stableCount = sCount;
2955 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
2957 }
2958
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002959 /**
2960 * Core implementation of stopping an activity. Note this is a little
2961 * tricky because the server's meaning of stop is slightly different
2962 * than our client -- for the server, stop means to save state and give
2963 * it the result when it is done, but the window may still be visible.
2964 * For the client, we want to call onStop()/onStart() to indicate when
2965 * the activity's UI visibillity changes.
2966 */
Romain Guy65b345f2011-07-27 18:51:50 -07002967 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002968 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002969 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002970 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 if (r != null) {
2972 if (!keepShown && r.stopped) {
2973 if (r.activity.mFinished) {
2974 // If we are finishing, we won't call onResume() in certain
2975 // cases. So here we likewise don't want to call onStop()
2976 // if the activity isn't resumed.
2977 return;
2978 }
2979 RuntimeException e = new RuntimeException(
2980 "Performing stop of activity that is not resumed: "
2981 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002982 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 }
2984
2985 if (info != null) {
2986 try {
2987 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002988 // For now, don't create the thumbnail here; we are
2989 // doing that by doing a screen snapshot.
2990 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 info.description = r.activity.onCreateDescription();
2992 } catch (Exception e) {
2993 if (!mInstrumentation.onException(r.activity, e)) {
2994 throw new RuntimeException(
2995 "Unable to save state of activity "
2996 + r.intent.getComponent().toShortString()
2997 + ": " + e.toString(), e);
2998 }
2999 }
3000 }
3001
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003002 // Next have the activity save its current state and managed dialogs...
3003 if (!r.activity.mFinished && saveState) {
3004 if (r.state == null) {
3005 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003006 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003007 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3008 r.state = state;
3009 } else {
3010 state = r.state;
3011 }
3012 }
3013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 if (!keepShown) {
3015 try {
3016 // Now we are idle.
3017 r.activity.performStop();
3018 } catch (Exception e) {
3019 if (!mInstrumentation.onException(r.activity, e)) {
3020 throw new RuntimeException(
3021 "Unable to stop activity "
3022 + r.intent.getComponent().toShortString()
3023 + ": " + e.toString(), e);
3024 }
3025 }
3026 r.stopped = true;
3027 }
3028
3029 r.paused = true;
3030 }
3031 }
3032
Romain Guy65b345f2011-07-27 18:51:50 -07003033 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003034 View v = r.activity.mDecor;
3035 if (v != null) {
3036 if (show) {
3037 if (!r.activity.mVisibleFromServer) {
3038 r.activity.mVisibleFromServer = true;
3039 mNumVisibleActivities++;
3040 if (r.activity.mVisibleFromClient) {
3041 r.activity.makeVisible();
3042 }
3043 }
3044 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003045 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003046 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003048 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003049 r.newConfig = null;
3050 }
3051 } else {
3052 if (r.activity.mVisibleFromServer) {
3053 r.activity.mVisibleFromServer = false;
3054 mNumVisibleActivities--;
3055 v.setVisibility(View.INVISIBLE);
3056 }
3057 }
3058 }
3059 }
3060
Romain Guy65b345f2011-07-27 18:51:50 -07003061 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003062 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 r.activity.mConfigChangeFlags |= configChanges;
3064
3065 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003066 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003068 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 TAG, "Finishing stop of " + r + ": show=" + show
3070 + " win=" + r.window);
3071
3072 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003073
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003074 // Make sure any pending writes are now committed.
3075 if (!r.isPreHoneycomb()) {
3076 QueuedWork.waitToFinish();
3077 }
3078
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003079 // Schedule the call to tell the activity manager we have
3080 // stopped. We don't do this immediately, because we want to
3081 // have a chance for any other pending work (in particular memory
3082 // trim requests) to complete before you tell the activity
3083 // manager to proceed and allow us to go fully into the background.
3084 info.activity = r;
3085 info.state = r.state;
3086 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003087 }
3088
3089 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003090 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 if (r.stopped) {
3092 r.activity.performRestart();
3093 r.stopped = false;
3094 }
3095 }
3096
Romain Guy65b345f2011-07-27 18:51:50 -07003097 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003098 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003099
3100 if (r == null) {
3101 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3102 return;
3103 }
3104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003106 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 } else if (show && r.stopped) {
3108 // If we are getting ready to gc after going to the background, well
3109 // we are back active so skip it.
3110 unscheduleGcIdler();
3111
3112 r.activity.performRestart();
3113 r.stopped = false;
3114 }
3115 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003116 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 TAG, "Handle window " + r + " visibility: " + show);
3118 updateVisibility(r, show);
3119 }
3120 }
3121
Romain Guy65b345f2011-07-27 18:51:50 -07003122 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003123 ActivityClientRecord r = mActivities.get(token);
3124
3125 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003126 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003127 return;
3128 }
3129
3130 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003131 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003132 try {
3133 // Now we are idle.
3134 r.activity.performStop();
3135 } catch (Exception e) {
3136 if (!mInstrumentation.onException(r.activity, e)) {
3137 throw new RuntimeException(
3138 "Unable to stop activity "
3139 + r.intent.getComponent().toShortString()
3140 + ": " + e.toString(), e);
3141 }
3142 }
3143 r.stopped = true;
3144 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003145
3146 // Make sure any pending writes are now committed.
3147 if (!r.isPreHoneycomb()) {
3148 QueuedWork.waitToFinish();
3149 }
3150
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003151 // Tell activity manager we slept.
3152 try {
3153 ActivityManagerNative.getDefault().activitySlept(r.token);
3154 } catch (RemoteException ex) {
3155 }
3156 } else {
3157 if (r.stopped && r.activity.mVisibleFromServer) {
3158 r.activity.performRestart();
3159 r.stopped = false;
3160 }
3161 }
3162 }
3163
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003164 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003165 synchronized (mPackages) {
3166 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003167 }
3168 }
3169
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003170 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3171 LoadedApk apk = peekPackageInfo(data.pkg, false);
3172 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003173 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003174 }
3175 apk = peekPackageInfo(data.pkg, true);
3176 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003177 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003178 }
3179 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003180 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003181 }
3182
Romain Guy65b345f2011-07-27 18:51:50 -07003183 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 final int N = results.size();
3185 for (int i=0; i<N; i++) {
3186 ResultInfo ri = results.get(i);
3187 try {
3188 if (ri.mData != null) {
3189 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3190 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003191 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003192 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 r.activity.dispatchActivityResult(ri.mResultWho,
3194 ri.mRequestCode, ri.mResultCode, ri.mData);
3195 } catch (Exception e) {
3196 if (!mInstrumentation.onException(r.activity, e)) {
3197 throw new RuntimeException(
3198 "Failure delivering result " + ri + " to activity "
3199 + r.intent.getComponent().toShortString()
3200 + ": " + e.toString(), e);
3201 }
3202 }
3203 }
3204 }
3205
Romain Guy65b345f2011-07-27 18:51:50 -07003206 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003207 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003208 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 if (r != null) {
3210 final boolean resumed = !r.paused;
3211 if (!r.activity.mFinished && r.activity.mDecor != null
3212 && r.hideForNow && resumed) {
3213 // We had hidden the activity because it started another
3214 // one... we have gotten a result back and we are not
3215 // paused, so make sure our window is visible.
3216 updateVisibility(r, true);
3217 }
3218 if (resumed) {
3219 try {
3220 // Now we are idle.
3221 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003222 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 mInstrumentation.callActivityOnPause(r.activity);
3224 if (!r.activity.mCalled) {
3225 throw new SuperNotCalledException(
3226 "Activity " + r.intent.getComponent().toShortString()
3227 + " did not call through to super.onPause()");
3228 }
3229 } catch (SuperNotCalledException e) {
3230 throw e;
3231 } catch (Exception e) {
3232 if (!mInstrumentation.onException(r.activity, e)) {
3233 throw new RuntimeException(
3234 "Unable to pause activity "
3235 + r.intent.getComponent().toShortString()
3236 + ": " + e.toString(), e);
3237 }
3238 }
3239 }
3240 deliverResults(r, res.results);
3241 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003242 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003243 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
3245 }
3246 }
3247
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003248 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 return performDestroyActivity(token, finishing, 0, false);
3250 }
3251
Romain Guy65b345f2011-07-27 18:51:50 -07003252 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003254 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003255 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003256 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003258 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003259 r.activity.mConfigChangeFlags |= configChanges;
3260 if (finishing) {
3261 r.activity.mFinished = true;
3262 }
3263 if (!r.paused) {
3264 try {
3265 r.activity.mCalled = false;
3266 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003267 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 r.activity.getComponentName().getClassName());
3269 if (!r.activity.mCalled) {
3270 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003271 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 + " did not call through to super.onPause()");
3273 }
3274 } catch (SuperNotCalledException e) {
3275 throw e;
3276 } catch (Exception e) {
3277 if (!mInstrumentation.onException(r.activity, e)) {
3278 throw new RuntimeException(
3279 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003280 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 + ": " + e.toString(), e);
3282 }
3283 }
3284 r.paused = true;
3285 }
3286 if (!r.stopped) {
3287 try {
3288 r.activity.performStop();
3289 } catch (SuperNotCalledException e) {
3290 throw e;
3291 } catch (Exception e) {
3292 if (!mInstrumentation.onException(r.activity, e)) {
3293 throw new RuntimeException(
3294 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003295 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 + ": " + e.toString(), e);
3297 }
3298 }
3299 r.stopped = true;
3300 }
3301 if (getNonConfigInstance) {
3302 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003303 r.lastNonConfigurationInstances
3304 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 } catch (Exception e) {
3306 if (!mInstrumentation.onException(r.activity, e)) {
3307 throw new RuntimeException(
3308 "Unable to retain activity "
3309 + r.intent.getComponent().toShortString()
3310 + ": " + e.toString(), e);
3311 }
3312 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
3314 try {
3315 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003316 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 if (!r.activity.mCalled) {
3318 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003319 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 " did not call through to super.onDestroy()");
3321 }
3322 if (r.window != null) {
3323 r.window.closeAllPanels();
3324 }
3325 } catch (SuperNotCalledException e) {
3326 throw e;
3327 } catch (Exception e) {
3328 if (!mInstrumentation.onException(r.activity, e)) {
3329 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003330 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3331 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 }
3333 }
3334 }
3335 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003336 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 return r;
3338 }
3339
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003340 private static String safeToComponentShortString(Intent intent) {
3341 ComponentName component = intent.getComponent();
3342 return component == null ? "[Unknown]" : component.toShortString();
3343 }
3344
Romain Guy65b345f2011-07-27 18:51:50 -07003345 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003347 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 configChanges, getNonConfigInstance);
3349 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003350 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 WindowManager wm = r.activity.getWindowManager();
3352 View v = r.activity.mDecor;
3353 if (v != null) {
3354 if (r.activity.mVisibleFromServer) {
3355 mNumVisibleActivities--;
3356 }
3357 IBinder wtoken = v.getWindowToken();
3358 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003359 if (r.onlyLocalRequest) {
3360 // Hold off on removing this until the new activity's
3361 // window is being added.
3362 r.mPendingRemoveWindow = v;
3363 r.mPendingRemoveWindowManager = wm;
3364 } else {
3365 wm.removeViewImmediate(v);
3366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003368 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003369 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 r.activity.getClass().getName(), "Activity");
3371 }
3372 r.activity.mDecor = null;
3373 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003374 if (r.mPendingRemoveWindow == null) {
3375 // If we are delaying the removal of the activity window, then
3376 // we can't clean up all windows here. Note that we can't do
3377 // so later either, which means any windows that aren't closed
3378 // by the app will leak. Well we try to warning them a lot
3379 // about leaking windows, because that is a bug, so if they are
3380 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003381 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003382 r.activity.getClass().getName(), "Activity");
3383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384
3385 // Mocked out contexts won't be participating in the normal
3386 // process lifecycle, but if we're running with a proper
3387 // ApplicationContext we need to have it tear down things
3388 // cleanly.
3389 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003390 if (c instanceof ContextImpl) {
3391 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 r.activity.getClass().getName(), "Activity");
3393 }
3394 }
3395 if (finishing) {
3396 try {
3397 ActivityManagerNative.getDefault().activityDestroyed(token);
3398 } catch (RemoteException ex) {
3399 // If the system process has died, it's game over for everyone.
3400 }
3401 }
3402 }
3403
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003404 public final void requestRelaunchActivity(IBinder token,
3405 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3406 int configChanges, boolean notResumed, Configuration config,
3407 boolean fromServer) {
3408 ActivityClientRecord target = null;
3409
3410 synchronized (mPackages) {
3411 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3412 ActivityClientRecord r = mRelaunchingActivities.get(i);
3413 if (r.token == token) {
3414 target = r;
3415 if (pendingResults != null) {
3416 if (r.pendingResults != null) {
3417 r.pendingResults.addAll(pendingResults);
3418 } else {
3419 r.pendingResults = pendingResults;
3420 }
3421 }
3422 if (pendingNewIntents != null) {
3423 if (r.pendingIntents != null) {
3424 r.pendingIntents.addAll(pendingNewIntents);
3425 } else {
3426 r.pendingIntents = pendingNewIntents;
3427 }
3428 }
3429 break;
3430 }
3431 }
3432
3433 if (target == null) {
3434 target = new ActivityClientRecord();
3435 target.token = token;
3436 target.pendingResults = pendingResults;
3437 target.pendingIntents = pendingNewIntents;
3438 if (!fromServer) {
3439 ActivityClientRecord existing = mActivities.get(token);
3440 if (existing != null) {
3441 target.startsNotResumed = existing.paused;
3442 }
3443 target.onlyLocalRequest = true;
3444 }
3445 mRelaunchingActivities.add(target);
3446 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3447 }
3448
3449 if (fromServer) {
3450 target.startsNotResumed = notResumed;
3451 target.onlyLocalRequest = false;
3452 }
3453 if (config != null) {
3454 target.createdConfig = config;
3455 }
3456 target.pendingConfigChanges |= configChanges;
3457 }
3458 }
3459
Romain Guy65b345f2011-07-27 18:51:50 -07003460 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 // If we are getting ready to gc after going to the background, well
3462 // we are back active so skip it.
3463 unscheduleGcIdler();
3464
3465 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003466 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 // First: make sure we have the most recent configuration and most
3469 // recent version of the activity, or skip it if some previous call
3470 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003471 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 int N = mRelaunchingActivities.size();
3473 IBinder token = tmp.token;
3474 tmp = null;
3475 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003476 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 if (r.token == token) {
3478 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003479 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 mRelaunchingActivities.remove(i);
3481 i--;
3482 N--;
3483 }
3484 }
Bob Leee5408332009-09-04 18:31:17 -07003485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003487 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 return;
3489 }
Bob Leee5408332009-09-04 18:31:17 -07003490
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003491 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3492 + tmp.token + " with configChanges=0x"
3493 + Integer.toHexString(configChanges));
3494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 if (mPendingConfiguration != null) {
3496 changedConfig = mPendingConfiguration;
3497 mPendingConfiguration = null;
3498 }
3499 }
Bob Leee5408332009-09-04 18:31:17 -07003500
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003501 if (tmp.createdConfig != null) {
3502 // If the activity manager is passing us its current config,
3503 // assume that is really what we want regardless of what we
3504 // may have pending.
3505 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003506 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3507 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3508 if (changedConfig == null
3509 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3510 changedConfig = tmp.createdConfig;
3511 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003512 }
3513 }
3514
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003515 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003516 + tmp.token + ": changedConfig=" + changedConfig);
3517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 // If there was a pending configuration change, execute it first.
3519 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003520 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003521 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003522 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
Bob Leee5408332009-09-04 18:31:17 -07003524
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003525 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003526 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 if (r == null) {
3528 return;
3529 }
Bob Leee5408332009-09-04 18:31:17 -07003530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003532 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003533 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003534
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003535 r.activity.mChangingConfigurations = true;
3536
Dianne Hackborne2b04802010-12-09 09:24:55 -08003537 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003539 performPauseActivity(r.token, false, r.isPreHoneycomb());
3540 }
3541 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3542 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003543 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003544 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 }
Bob Leee5408332009-09-04 18:31:17 -07003546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 r.activity = null;
3550 r.window = null;
3551 r.hideForNow = false;
3552 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003553 // Merge any pending results and pending intents; don't just replace them
3554 if (tmp.pendingResults != null) {
3555 if (r.pendingResults == null) {
3556 r.pendingResults = tmp.pendingResults;
3557 } else {
3558 r.pendingResults.addAll(tmp.pendingResults);
3559 }
3560 }
3561 if (tmp.pendingIntents != null) {
3562 if (r.pendingIntents == null) {
3563 r.pendingIntents = tmp.pendingIntents;
3564 } else {
3565 r.pendingIntents.addAll(tmp.pendingIntents);
3566 }
3567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003569
Christopher Tateb70f3df2009-04-07 16:07:59 -07003570 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 }
3572
Romain Guy65b345f2011-07-27 18:51:50 -07003573 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003574 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 Bitmap thumbnail = createThumbnailBitmap(r);
3576 CharSequence description = null;
3577 try {
3578 description = r.activity.onCreateDescription();
3579 } catch (Exception e) {
3580 if (!mInstrumentation.onException(r.activity, e)) {
3581 throw new RuntimeException(
3582 "Unable to create description of activity "
3583 + r.intent.getComponent().toShortString()
3584 + ": " + e.toString(), e);
3585 }
3586 }
3587 //System.out.println("Reporting top thumbnail " + thumbnail);
3588 try {
3589 ActivityManagerNative.getDefault().reportThumbnail(
3590 token, thumbnail, description);
3591 } catch (RemoteException ex) {
3592 }
3593 }
3594
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003595 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003597 ArrayList<ComponentCallbacks2> callbacks
3598 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003601 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 Activity a = ar.activity;
3603 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003604 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3605 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003606 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 // If the activity is currently resumed, its configuration
3608 // needs to change right now.
3609 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003610 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 // Otherwise, we will tell it about the change
3612 // the next time it is resumed or shown. Note that
3613 // the activity manager may, before then, decide the
3614 // activity needs to be destroyed to handle its new
3615 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003616 if (DEBUG_CONFIGURATION) {
3617 Slog.v(TAG, "Setting activity "
3618 + ar.activityInfo.name + " newConfig=" + thisConfig);
3619 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003620 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 }
3622 }
3623 }
3624 }
3625 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003626 for (Service service : mServices.values()) {
3627 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 }
3629 }
3630 synchronized (mProviderMap) {
3631 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003632 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3633 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 }
3635 }
3636 }
3637 final int N = mAllApplications.size();
3638 for (int i=0; i<N; i++) {
3639 callbacks.add(mAllApplications.get(i));
3640 }
Bob Leee5408332009-09-04 18:31:17 -07003641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 return callbacks;
3643 }
Bob Leee5408332009-09-04 18:31:17 -07003644
Romain Guya998dff2012-03-23 18:58:36 -07003645 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003647 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 // we check the runtime type and act accordingly.
3649 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3650 if (activity != null) {
3651 activity.mCalled = false;
3652 }
Bob Leee5408332009-09-04 18:31:17 -07003653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 boolean shouldChangeConfig = false;
3655 if ((activity == null) || (activity.mCurrentConfig == null)) {
3656 shouldChangeConfig = true;
3657 } else {
Bob Leee5408332009-09-04 18:31:17 -07003658
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 // If the new config is the same as the config this Activity
3660 // is already running with then don't bother calling
3661 // onConfigurationChanged
3662 int diff = activity.mCurrentConfig.diff(config);
3663 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 // If this activity doesn't handle any of the config changes
3665 // then don't bother calling onConfigurationChanged as we're
3666 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003667 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 shouldChangeConfig = true;
3669 }
3670 }
3671 }
Bob Leee5408332009-09-04 18:31:17 -07003672
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003673 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003674 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 if (shouldChangeConfig) {
3676 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 if (activity != null) {
3679 if (!activity.mCalled) {
3680 throw new SuperNotCalledException(
3681 "Activity " + activity.getLocalClassName() +
3682 " did not call through to super.onConfigurationChanged()");
3683 }
3684 activity.mConfigChangeFlags = 0;
3685 activity.mCurrentConfig = new Configuration(config);
3686 }
3687 }
3688 }
3689
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003690 public final void applyConfigurationToResources(Configuration config) {
3691 synchronized (mPackages) {
3692 applyConfigurationToResourcesLocked(config, null);
3693 }
3694 }
3695
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003696 final boolean applyConfigurationToResourcesLocked(Configuration config,
3697 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003698 if (mResConfiguration == null) {
3699 mResConfiguration = new Configuration();
3700 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003701 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003702 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003703 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003704 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003705 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003706 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003707 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003708
3709 if (compat != null && (mResCompatibilityInfo == null ||
3710 !mResCompatibilityInfo.equals(compat))) {
3711 mResCompatibilityInfo = compat;
3712 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3713 | ActivityInfo.CONFIG_SCREEN_SIZE
3714 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3715 }
Bob Leee5408332009-09-04 18:31:17 -07003716
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003717 // set it for java, this also affects newly created Resources
3718 if (config.locale != null) {
3719 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 }
Bob Leee5408332009-09-04 18:31:17 -07003721
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003722 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003723
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003724 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003725 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003726
3727 Configuration tmpConfig = null;
3728
3729 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3730 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003731 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003732 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3733 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003734 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003735 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003736 + r + " config to: " + config);
Dianne Hackborn756220b2012-08-14 16:45:30 -07003737 Configuration override = entry.getKey().mOverrideConfiguration;
3738 if (override != null) {
3739 if (tmpConfig == null) {
3740 tmpConfig = new Configuration();
3741 }
3742 tmpConfig.setTo(config);
3743 tmpConfig.updateFrom(override);
3744 r.updateConfiguration(tmpConfig, dm, compat);
3745 } else {
3746 r.updateConfiguration(config, dm, compat);
3747 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003748 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003749 // + " " + r + ": " + r.getConfiguration());
3750 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003751 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003752 it.remove();
3753 }
3754 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003755
3756 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003757 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003758
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003759 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003760 Configuration config = mConfiguration;
3761 if (mCompatConfiguration == null) {
3762 mCompatConfiguration = new Configuration();
3763 }
3764 mCompatConfiguration.setTo(mConfiguration);
3765 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003766 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003767 config = mCompatConfiguration;
3768 }
3769 return config;
3770 }
3771
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003772 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003773
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003774 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003775 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003776
3777 synchronized (mPackages) {
3778 if (mPendingConfiguration != null) {
3779 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3780 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003781 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003782 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003783 }
3784 mPendingConfiguration = null;
3785 }
3786
3787 if (config == null) {
3788 return;
3789 }
3790
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003791 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003792 + config);
3793
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003794 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 if (mConfiguration == null) {
3797 mConfiguration = new Configuration();
3798 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003799 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003800 return;
3801 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003802 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003804 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 callbacks = collectComponentCallbacksLocked(false, config);
3806 }
Romain Guy65b345f2011-07-27 18:51:50 -07003807
3808 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003809 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003810
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003811 freeTextLayoutCachesIfNeeded(configDiff);
3812
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003813 if (callbacks != null) {
3814 final int N = callbacks.size();
3815 for (int i=0; i<N; i++) {
3816 performConfigurationChanged(callbacks.get(i), config);
3817 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 }
3819 }
3820
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003821 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3822 if (configDiff != 0) {
3823 // Ask text layout engine to free its caches if there is a locale change
3824 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3825 if (hasLocaleConfigChange) {
3826 Canvas.freeTextLayoutCaches();
3827 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3828 }
3829 }
3830 }
3831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003833 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 if (r == null || r.activity == null) {
3835 return;
3836 }
Bob Leee5408332009-09-04 18:31:17 -07003837
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003838 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003839 + r.activityInfo.name);
3840
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003841 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003842
3843 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 }
3845
Romain Guy7eabe552011-07-21 14:56:34 -07003846 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003847 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003848 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003849 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003850 default:
3851 mProfiler.setProfiler(pcd.path, pcd.fd);
3852 mProfiler.autoStopProfiler = false;
3853 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003854 break;
3855 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003856 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003857 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003858 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003859 } finally {
3860 try {
3861 pcd.fd.close();
3862 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003863 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003864 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003865 }
3866 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003867 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003868 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003869 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003870 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003871 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003872 }
3873 }
Bob Leee5408332009-09-04 18:31:17 -07003874
Romain Guya998dff2012-03-23 18:58:36 -07003875 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003876 if (managed) {
3877 try {
3878 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3879 } catch (IOException e) {
3880 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3881 + " -- can the process access this path?");
3882 } finally {
3883 try {
3884 dhd.fd.close();
3885 } catch (IOException e) {
3886 Slog.w(TAG, "Failure closing profile fd", e);
3887 }
3888 }
3889 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003890 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003891 }
3892 }
3893
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003894 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3895 boolean hasPkgInfo = false;
3896 if (packages != null) {
3897 for (int i=packages.length-1; i>=0; i--) {
3898 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3899 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003900 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003901 ref = mPackages.get(packages[i]);
3902 if (ref != null && ref.get() != null) {
3903 hasPkgInfo = true;
3904 } else {
3905 ref = mResourcePackages.get(packages[i]);
3906 if (ref != null && ref.get() != null) {
3907 hasPkgInfo = true;
3908 }
3909 }
3910 }
3911 mPackages.remove(packages[i]);
3912 mResourcePackages.remove(packages[i]);
3913 }
3914 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003915 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003916 hasPkgInfo);
3917 }
3918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003920 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003922 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 callbacks = collectComponentCallbacksLocked(true, null);
3924 }
Bob Leee5408332009-09-04 18:31:17 -07003925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 final int N = callbacks.size();
3927 for (int i=0; i<N; i++) {
3928 callbacks.get(i).onLowMemory();
3929 }
3930
Chris Tatece229052009-03-25 16:44:52 -07003931 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3932 if (Process.myUid() != Process.SYSTEM_UID) {
3933 int sqliteReleased = SQLiteDatabase.releaseMemory();
3934 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3935 }
Bob Leee5408332009-09-04 18:31:17 -07003936
Mike Reedcaf0df12009-04-27 14:32:05 -04003937 // Ask graphics to free up as much as possible (font/image caches)
3938 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003940 // Ask text layout engine to free also as much as possible
3941 Canvas.freeTextLayoutCaches();
3942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943 BinderInternal.forceGc("mem");
3944 }
3945
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003946 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003947 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003948
Jeff Brown98365d72012-08-19 20:30:52 -07003949 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07003950 windowManager.startTrimMemory(level);
3951
3952 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003953 synchronized (mPackages) {
3954 callbacks = collectComponentCallbacksLocked(true, null);
3955 }
3956
3957 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003958 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003959 callbacks.get(i).onTrimMemory(level);
3960 }
Romain Guy19f86e82012-04-23 15:19:07 -07003961
Jeff Brown98365d72012-08-19 20:30:52 -07003962 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003963 }
3964
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003965 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003966 if (Process.isIsolated()) {
3967 // Isolated processes aren't going to do UI.
3968 return;
3969 }
Romain Guya9582652011-11-10 14:20:10 -08003970 try {
3971 int uid = Process.myUid();
3972 String[] packages = getPackageManager().getPackagesForUid(uid);
3973
3974 // If there are several packages in this application we won't
3975 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003976 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003977 HardwareRenderer.setupDiskCache(cacheDir);
3978 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003979 }
3980 } catch (RemoteException e) {
3981 // Ignore
3982 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07003983 }
3984
3985 private void updateDefaultDensity() {
3986 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
3987 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
3988 && !mDensityCompatMode) {
3989 Slog.i(TAG, "Switching default density from "
3990 + DisplayMetrics.DENSITY_DEVICE + " to "
3991 + mCurDefaultDisplayDpi);
3992 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
3993 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3994 }
3995 }
3996
Romain Guy65b345f2011-07-27 18:51:50 -07003997 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 mBoundApplication = data;
3999 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004000 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004002 mProfiler = new Profiler();
4003 mProfiler.profileFile = data.initProfileFile;
4004 mProfiler.profileFd = data.initProfileFd;
4005 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004007 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004008 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 android.ddm.DdmHandleAppName.setAppName(data.processName);
4010
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004011 if (data.persistent) {
4012 // Persistent processes on low-memory devices do not get to
4013 // use hardware accelerated drawing, since this can add too much
4014 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004015 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004016 HardwareRenderer.disable(false);
4017 }
4018 }
Romain Guya9582652011-11-10 14:20:10 -08004019
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004020 if (mProfiler.profileFd != null) {
4021 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004022 }
4023
Joe Onoratod630f102011-03-17 18:42:26 -07004024 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4025 // implementation to use the pool executor. Normally, we use the
4026 // serialized executor as the default. This has to happen in the
4027 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004028 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004029 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4030 }
4031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 /*
4033 * Before spawning a new process, reset the time zone to be the system time zone.
4034 * This needs to be done because the system time zone could have changed after the
4035 * the spawning of this process. Without doing this this process would have the incorrect
4036 * system time zone.
4037 */
4038 TimeZone.setDefault(null);
4039
4040 /*
4041 * Initialize the default locale in this process for the reasons we set the time zone.
4042 */
4043 Locale.setDefault(data.config.locale);
4044
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004045 /*
4046 * Update the system configuration since its preloaded and might not
4047 * reflect configuration changes. The configuration object passed
4048 * in AppBindData can be safely assumed to be up to date
4049 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004050 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004051 mCurDefaultDisplayDpi = data.config.densityDpi;
4052 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004053
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004054 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055
Dianne Hackborndde331c2012-08-03 14:01:57 -07004056 /**
4057 * Switch this process to density compatibility mode if needed.
4058 */
4059 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4060 == 0) {
4061 mDensityCompatMode = true;
4062 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4063 }
4064 updateDefaultDensity();
4065
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004066 final ContextImpl appContext = new ContextImpl();
4067 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004068 if (!Process.isIsolated()) {
4069 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004070
Amith Yamasani11de39a2012-08-17 18:00:52 -07004071 // Provide a usable directory for temporary files
4072 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004073
Amith Yamasani11de39a2012-08-17 18:00:52 -07004074 setupGraphicsSupport(data.info, cacheDir);
4075 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004076 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004077 * For system applications on userdebug/eng builds, log stack
4078 * traces of disk and network access to dropbox for analysis.
4079 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004080 if ((data.appInfo.flags &
4081 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004082 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4083 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004084 }
4085
4086 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004087 * For apps targetting SDK Honeycomb or later, we don't allow
4088 * network usage on the main event loop / UI thread.
4089 *
4090 * Note to those grepping: this is what ultimately throws
4091 * NetworkOnMainThreadException ...
4092 */
4093 if (data.appInfo.targetSdkVersion > 9) {
4094 StrictMode.enableDeathOnNetwork();
4095 }
4096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4098 // XXX should have option to change the port.
4099 Debug.changeDebugPort(8100);
4100 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004101 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 + " is waiting for the debugger on port 8100...");
4103
4104 IActivityManager mgr = ActivityManagerNative.getDefault();
4105 try {
4106 mgr.showWaitingForDebugger(mAppThread, true);
4107 } catch (RemoteException ex) {
4108 }
4109
4110 Debug.waitForDebugger();
4111
4112 try {
4113 mgr.showWaitingForDebugger(mAppThread, false);
4114 } catch (RemoteException ex) {
4115 }
4116
4117 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004118 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 + " can be debugged on port 8100...");
4120 }
4121 }
4122
Siva Velusamy92a8b222012-03-09 16:24:04 -08004123 // Enable OpenGL tracing if required
4124 if (data.enableOpenGlTrace) {
4125 GLUtils.enableTracing();
4126 }
4127
Robert Greenwalt434203a2010-10-11 16:00:27 -07004128 /**
4129 * Initialize the default http proxy in this process for the reasons we set the time zone.
4130 */
4131 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004132 if (b != null) {
4133 // In pre-boot mode (doing initial launch to collect password), not
4134 // all system is up. This includes the connectivity service, so don't
4135 // crash if we can't get it.
4136 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4137 try {
4138 ProxyProperties proxyProperties = service.getProxy();
4139 Proxy.setHttpProxySystemProperty(proxyProperties);
4140 } catch (RemoteException e) {}
4141 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004143 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 InstrumentationInfo ii = null;
4145 try {
4146 ii = appContext.getPackageManager().
4147 getInstrumentationInfo(data.instrumentationName, 0);
4148 } catch (PackageManager.NameNotFoundException e) {
4149 }
4150 if (ii == null) {
4151 throw new RuntimeException(
4152 "Unable to find instrumentation info for: "
4153 + data.instrumentationName);
4154 }
4155
4156 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004157 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 mInstrumentationAppPackage = ii.packageName;
4159 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004160 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161
4162 ApplicationInfo instrApp = new ApplicationInfo();
4163 instrApp.packageName = ii.packageName;
4164 instrApp.sourceDir = ii.sourceDir;
4165 instrApp.publicSourceDir = ii.publicSourceDir;
4166 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004167 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004168 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004170 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 instrContext.init(pi, null, this);
4172
4173 try {
4174 java.lang.ClassLoader cl = instrContext.getClassLoader();
4175 mInstrumentation = (Instrumentation)
4176 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4177 } catch (Exception e) {
4178 throw new RuntimeException(
4179 "Unable to instantiate instrumentation "
4180 + data.instrumentationName + ": " + e.toString(), e);
4181 }
4182
4183 mInstrumentation.init(this, instrContext, appContext,
4184 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4185
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004186 if (mProfiler.profileFile != null && !ii.handleProfiling
4187 && mProfiler.profileFd == null) {
4188 mProfiler.handlingProfiling = true;
4189 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004190 file.getParentFile().mkdirs();
4191 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4192 }
4193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 } else {
4195 mInstrumentation = new Instrumentation();
4196 }
4197
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004198 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004199 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004200 }
4201
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004202 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004203 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004204 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004205 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004207 // If the app is being launched for full backup or restore, bring it up in
4208 // a restricted environment with the base application class.
4209 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4210 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004211
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004212 // don't bring up providers in restricted mode; they may depend on the
4213 // app's custom Application class
4214 if (!data.restrictedBackupMode) {
4215 List<ProviderInfo> providers = data.providers;
4216 if (providers != null) {
4217 installContentProviders(app, providers);
4218 // For process that contains content providers, we want to
4219 // ensure that the JIT is enabled "at some point".
4220 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4221 }
4222 }
4223
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004224 // Do this after providers, since instrumentation tests generally start their
4225 // test thread at this point, and we don't want that racing.
4226 try {
4227 mInstrumentation.onCreate(data.instrumentationArgs);
4228 }
4229 catch (Exception e) {
4230 throw new RuntimeException(
4231 "Exception thrown in onCreate() of "
4232 + data.instrumentationName + ": " + e.toString(), e);
4233 }
4234
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004235 try {
4236 mInstrumentation.callApplicationOnCreate(app);
4237 } catch (Exception e) {
4238 if (!mInstrumentation.onException(app, e)) {
4239 throw new RuntimeException(
4240 "Unable to create application " + app.getClass().getName()
4241 + ": " + e.toString(), e);
4242 }
4243 }
4244 } finally {
4245 StrictMode.setThreadPolicy(savedPolicy);
4246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 }
4248
4249 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4250 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004251 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4252 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 Debug.stopMethodTracing();
4254 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004255 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256 // + ", app thr: " + mAppThread);
4257 try {
4258 am.finishInstrumentation(mAppThread, resultCode, results);
4259 } catch (RemoteException ex) {
4260 }
4261 }
4262
Romain Guy65b345f2011-07-27 18:51:50 -07004263 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 Context context, List<ProviderInfo> providers) {
4265 final ArrayList<IActivityManager.ContentProviderHolder> results =
4266 new ArrayList<IActivityManager.ContentProviderHolder>();
4267
Romain Guya998dff2012-03-23 18:58:36 -07004268 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004269 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004270 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271 buf.append(cpi.authority);
4272 buf.append(": ");
4273 buf.append(cpi.name);
4274 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004275 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4276 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4277 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004278 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 }
4281 }
4282
4283 try {
4284 ActivityManagerNative.getDefault().publishContentProviders(
4285 getApplicationThread(), results);
4286 } catch (RemoteException ex) {
4287 }
4288 }
4289
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004290 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4291 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004292 if (provider != null) {
4293 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 }
4295
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004296 // There is a possible race here. Another thread may try to acquire
4297 // the same provider at the same time. When this happens, we want to ensure
4298 // that the first one wins.
4299 // Note that we cannot hold the lock while acquiring and installing the
4300 // provider since it might take a long time to run and it could also potentially
4301 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 IActivityManager.ContentProviderHolder holder = null;
4303 try {
4304 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004305 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306 } catch (RemoteException ex) {
4307 }
4308 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004309 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 return null;
4311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004313 // Install provider will increment the reference count for us, and break
4314 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004315 holder = installProvider(c, holder, holder.info,
4316 true /*noisy*/, holder.noReleaseNeeded, stable);
4317 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 }
4319
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004320 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4321 if (stable) {
4322 prc.stableCount += 1;
4323 if (prc.stableCount == 1) {
4324 // We are acquiring a new stable reference on the provider.
4325 int unstableDelta;
4326 if (prc.removePending) {
4327 // We have a pending remove operation, which is holding the
4328 // last unstable reference. At this point we are converting
4329 // that unstable reference to our new stable reference.
4330 unstableDelta = -1;
4331 // Cancel the removal of the provider.
4332 if (DEBUG_PROVIDER) {
4333 Slog.v(TAG, "incProviderRef: stable "
4334 + "snatched provider from the jaws of death");
4335 }
4336 prc.removePending = false;
4337 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4338 } else {
4339 unstableDelta = 0;
4340 }
4341 try {
4342 if (DEBUG_PROVIDER) {
4343 Slog.v(TAG, "incProviderRef Now stable - "
4344 + prc.holder.info.name + ": unstableDelta="
4345 + unstableDelta);
4346 }
4347 ActivityManagerNative.getDefault().refContentProvider(
4348 prc.holder.connection, 1, unstableDelta);
4349 } catch (RemoteException e) {
4350 //do nothing content provider object is dead any way
4351 }
4352 }
4353 } else {
4354 prc.unstableCount += 1;
4355 if (prc.unstableCount == 1) {
4356 // We are acquiring a new unstable reference on the provider.
4357 if (prc.removePending) {
4358 // Oh look, we actually have a remove pending for the
4359 // provider, which is still holding the last unstable
4360 // reference. We just need to cancel that to take new
4361 // ownership of the reference.
4362 if (DEBUG_PROVIDER) {
4363 Slog.v(TAG, "incProviderRef: unstable "
4364 + "snatched provider from the jaws of death");
4365 }
4366 prc.removePending = false;
4367 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4368 } else {
4369 // First unstable ref, increment our count in the
4370 // activity manager.
4371 try {
4372 if (DEBUG_PROVIDER) {
4373 Slog.v(TAG, "incProviderRef: Now unstable - "
4374 + prc.holder.info.name);
4375 }
4376 ActivityManagerNative.getDefault().refContentProvider(
4377 prc.holder.connection, 0, 1);
4378 } catch (RemoteException e) {
4379 //do nothing content provider object is dead any way
4380 }
4381 }
4382 }
4383 }
4384 }
4385
4386 public final IContentProvider acquireExistingProvider(Context c, String name,
4387 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004388 synchronized (mProviderMap) {
4389 ProviderClientRecord pr = mProviderMap.get(name);
4390 if (pr == null) {
4391 return null;
4392 }
4393
4394 IContentProvider provider = pr.mProvider;
4395 IBinder jBinder = provider.asBinder();
4396
4397 // Only increment the ref count if we have one. If we don't then the
4398 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004399 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004400 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004401 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004402 }
4403 return provider;
4404 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004405 }
4406
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004407 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4408 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 return false;
4410 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004413 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004415 if (prc == null) {
4416 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004418 }
4419
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004420 boolean lastRef = false;
4421 if (stable) {
4422 if (prc.stableCount == 0) {
4423 if (DEBUG_PROVIDER) Slog.v(TAG,
4424 "releaseProvider: stable ref count already 0, how?");
4425 return false;
4426 }
4427 prc.stableCount -= 1;
4428 if (prc.stableCount == 0) {
4429 // What we do at this point depends on whether there are
4430 // any unstable refs left: if there are, we just tell the
4431 // activity manager to decrement its stable count; if there
4432 // aren't, we need to enqueue this provider to be removed,
4433 // and convert to holding a single unstable ref while
4434 // doing so.
4435 lastRef = prc.unstableCount == 0;
4436 try {
4437 if (DEBUG_PROVIDER) {
4438 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4439 + lastRef + " - " + prc.holder.info.name);
4440 }
4441 ActivityManagerNative.getDefault().refContentProvider(
4442 prc.holder.connection, -1, lastRef ? 1 : 0);
4443 } catch (RemoteException e) {
4444 //do nothing content provider object is dead any way
4445 }
4446 }
4447 } else {
4448 if (prc.unstableCount == 0) {
4449 if (DEBUG_PROVIDER) Slog.v(TAG,
4450 "releaseProvider: unstable ref count already 0, how?");
4451 return false;
4452 }
4453 prc.unstableCount -= 1;
4454 if (prc.unstableCount == 0) {
4455 // If this is the last reference, we need to enqueue
4456 // this provider to be removed instead of telling the
4457 // activity manager to remove it at this point.
4458 lastRef = prc.stableCount == 0;
4459 if (!lastRef) {
4460 try {
4461 if (DEBUG_PROVIDER) {
4462 Slog.v(TAG, "releaseProvider: No longer unstable - "
4463 + prc.holder.info.name);
4464 }
4465 ActivityManagerNative.getDefault().refContentProvider(
4466 prc.holder.connection, 0, -1);
4467 } catch (RemoteException e) {
4468 //do nothing content provider object is dead any way
4469 }
4470 }
4471 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004472 }
4473
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004474 if (lastRef) {
4475 if (!prc.removePending) {
4476 // Schedule the actual remove asynchronously, since we don't know the context
4477 // this will be called in.
4478 // TODO: it would be nice to post a delayed message, so
4479 // if we come back and need the same provider quickly
4480 // we will still have it available.
4481 if (DEBUG_PROVIDER) {
4482 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4483 + prc.holder.info.name);
4484 }
4485 prc.removePending = true;
4486 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4487 mH.sendMessage(msg);
4488 } else {
4489 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4490 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004491 }
4492 return true;
4493 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
4495
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004496 final void completeRemoveProvider(ProviderRefCount prc) {
4497 synchronized (mProviderMap) {
4498 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004499 // There was a race! Some other client managed to acquire
4500 // the provider before the removal was completed.
4501 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004502 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004503 + "provider still in use");
4504 return;
4505 }
4506
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004507 final IBinder jBinder = prc.holder.provider.asBinder();
4508 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4509 if (existingPrc == prc) {
4510 mProviderRefCountMap.remove(jBinder);
4511 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004512
4513 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4514 while (iter.hasNext()) {
4515 ProviderClientRecord pr = iter.next();
4516 IBinder myBinder = pr.mProvider.asBinder();
4517 if (myBinder == jBinder) {
4518 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004519 }
4520 }
4521 }
4522
4523 try {
4524 if (DEBUG_PROVIDER) {
4525 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4526 + "removeContentProvider(" + prc.holder.info.name + ")");
4527 }
4528 ActivityManagerNative.getDefault().removeContentProvider(
4529 prc.holder.connection, false);
4530 } catch (RemoteException e) {
4531 //do nothing content provider object is dead any way
4532 }
4533 }
4534
4535 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4536 synchronized(mProviderMap) {
4537 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4538 if (prc != null) {
4539 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4540 + provider + " " + prc.holder.info.name);
4541 mProviderRefCountMap.remove(provider);
4542 if (prc.client != null && prc.client.mNames != null) {
4543 for (String name : prc.client.mNames) {
4544 ProviderClientRecord pr = mProviderMap.get(name);
4545 if (pr != null && pr.mProvider.asBinder() == provider) {
4546 Slog.i(TAG, "Removing dead content provider: " + name);
4547 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004548 }
4549 }
4550 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004551 if (fromClient) {
4552 // We found out about this due to execution in our client
4553 // code. Tell the activity manager about it now, to ensure
4554 // that the next time we go to do anything with the provider
4555 // it knows it is dead (so we don't race with its death
4556 // notification).
4557 try {
4558 ActivityManagerNative.getDefault().unstableProviderDied(
4559 prc.holder.connection);
4560 } catch (RemoteException e) {
4561 //do nothing content provider object is dead any way
4562 }
4563 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004564 }
4565 }
4566 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004568 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4569 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4570 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4571 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4572 localProvider, holder);
4573 for (int i = 0; i < names.length; i++) {
4574 ProviderClientRecord existing = mProviderMap.get(names[i]);
4575 if (existing != null) {
4576 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4577 + " already published as " + names[i]);
4578 } else {
4579 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 }
4581 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004582 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004583 }
4584
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004585 /**
4586 * Installs the provider.
4587 *
4588 * Providers that are local to the process or that come from the system server
4589 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4590 * Other remote providers are reference counted. The initial reference count
4591 * for all reference counted providers is one. Providers that are not reference
4592 * counted do not have a reference count (at all).
4593 *
4594 * This method detects when a provider has already been installed. When this happens,
4595 * it increments the reference count of the existing provider (if appropriate)
4596 * and returns the existing provider. This can happen due to concurrent
4597 * attempts to acquire the same provider.
4598 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004599 private IActivityManager.ContentProviderHolder installProvider(Context context,
4600 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4601 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004602 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004603 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004604 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004606 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 + info.name);
4608 }
4609 Context c = null;
4610 ApplicationInfo ai = info.applicationInfo;
4611 if (context.getPackageName().equals(ai.packageName)) {
4612 c = context;
4613 } else if (mInitialApplication != null &&
4614 mInitialApplication.getPackageName().equals(ai.packageName)) {
4615 c = mInitialApplication;
4616 } else {
4617 try {
4618 c = context.createPackageContext(ai.packageName,
4619 Context.CONTEXT_INCLUDE_CODE);
4620 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004621 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 }
4623 }
4624 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004625 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 ai.packageName +
4627 " while loading content provider " +
4628 info.name);
4629 return null;
4630 }
4631 try {
4632 final java.lang.ClassLoader cl = c.getClassLoader();
4633 localProvider = (ContentProvider)cl.
4634 loadClass(info.name).newInstance();
4635 provider = localProvider.getIContentProvider();
4636 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004637 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 info.name + " from sourceDir " +
4639 info.applicationInfo.sourceDir);
4640 return null;
4641 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004642 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 TAG, "Instantiating local provider " + info.name);
4644 // XXX Need to create the correct context for this provider.
4645 localProvider.attachInfo(c, info);
4646 } catch (java.lang.Exception e) {
4647 if (!mInstrumentation.onException(null, e)) {
4648 throw new RuntimeException(
4649 "Unable to get provider " + info.name
4650 + ": " + e.toString(), e);
4651 }
4652 return null;
4653 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004654 } else {
4655 provider = holder.provider;
4656 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004657 + info.name);
4658 }
4659
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004660 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004661
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004662 synchronized (mProviderMap) {
4663 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4664 + " / " + info.name);
4665 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004667 ComponentName cname = new ComponentName(info.packageName, info.name);
4668 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004669 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004670 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004671 Slog.v(TAG, "installProvider: lost the race, "
4672 + "using existing local provider");
4673 }
4674 provider = pr.mProvider;
4675 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004676 holder = new IActivityManager.ContentProviderHolder(info);
4677 holder.provider = provider;
4678 holder.noReleaseNeeded = true;
4679 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004680 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004681 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004682 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004683 retHolder = pr.mHolder;
4684 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004685 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4686 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004687 if (DEBUG_PROVIDER) {
4688 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004689 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004690 // We need to transfer our new reference to the existing
4691 // ref count, releasing the old one... but only if
4692 // release is needed (that is, it is not running in the
4693 // system process).
4694 if (!noReleaseNeeded) {
4695 incProviderRefLocked(prc, stable);
4696 try {
4697 ActivityManagerNative.getDefault().removeContentProvider(
4698 holder.connection, stable);
4699 } catch (RemoteException e) {
4700 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004701 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004702 }
4703 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004704 ProviderClientRecord client = installProviderAuthoritiesLocked(
4705 provider, localProvider, holder);
4706 if (noReleaseNeeded) {
4707 prc = new ProviderRefCount(holder, client, 1000, 1000);
4708 } else {
4709 prc = stable
4710 ? new ProviderRefCount(holder, client, 1, 0)
4711 : new ProviderRefCount(holder, client, 0, 1);
4712 }
4713 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004714 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004715 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 }
4717 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004718
4719 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 }
4721
Romain Guy65b345f2011-07-27 18:51:50 -07004722 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 sThreadLocal.set(this);
4724 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004726 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004727 public void run() {
4728 ensureJitEnabled();
4729 }
4730 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4732 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4733 IActivityManager mgr = ActivityManagerNative.getDefault();
4734 try {
4735 mgr.attachApplication(mAppThread);
4736 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004737 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 }
4739 } else {
4740 // Don't set application object here -- if the system crashes,
4741 // we can't display an alert, we just want to die die die.
4742 android.ddm.DdmHandleAppName.setAppName("system_process");
4743 try {
4744 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004745 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 context.init(getSystemContext().mPackageInfo, null, this);
4747 Application app = Instrumentation.newApplication(Application.class, context);
4748 mAllApplications.add(app);
4749 mInitialApplication = app;
4750 app.onCreate();
4751 } catch (Exception e) {
4752 throw new RuntimeException(
4753 "Unable to instantiate Application():" + e.toString(), e);
4754 }
4755 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004756
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004757 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004758 public void onConfigurationChanged(Configuration newConfig) {
4759 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004760 // We need to apply this change to the resources
4761 // immediately, because upon returning the view
4762 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004763 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004764 // This actually changed the resources! Tell
4765 // everyone about it.
4766 if (mPendingConfiguration == null ||
4767 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4768 mPendingConfiguration = newConfig;
4769
4770 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4771 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004772 }
4773 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004774 }
4775 public void onLowMemory() {
4776 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004777 public void onTrimMemory(int level) {
4778 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004779 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 }
4781
Romain Guy5e9120d2012-01-30 12:17:22 -08004782 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004783 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 ActivityThread thread = new ActivityThread();
4785 thread.attach(true);
4786 return thread;
4787 }
4788
Jeff Brown10e89712011-07-08 18:52:57 -07004789 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004791 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 }
4793 }
4794
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004795 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004796 synchronized (mPackages) {
4797 if (mCoreSettings != null) {
4798 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004799 } else {
4800 return defaultValue;
4801 }
4802 }
4803 }
4804
Romain Guy65b345f2011-07-27 18:51:50 -07004805 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004806 SamplingProfilerIntegration.start();
4807
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004808 // CloseGuard defaults to true and can be quite spammy. We
4809 // disable it here, but selectively enable it later (via
4810 // StrictMode) on debug builds, but using DropBox, not logs.
4811 CloseGuard.setEnabled(false);
4812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 Process.setArgV0("<pre-initialized>");
4814
4815 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004816 if (sMainThreadHandler == null) {
4817 sMainThreadHandler = new Handler();
4818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819
4820 ActivityThread thread = new ActivityThread();
4821 thread.attach(false);
4822
Romain Guy5e9120d2012-01-30 12:17:22 -08004823 AsyncTask.init();
4824
Dianne Hackborn287952c2010-09-22 22:34:31 -07004825 if (false) {
4826 Looper.myLooper().setMessageLogging(new
4827 LogPrinter(Log.DEBUG, "ActivityThread"));
4828 }
4829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 Looper.loop();
4831
Jeff Brown10e89712011-07-08 18:52:57 -07004832 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004833 }
4834}