blob: bb35ddd33d511266f5440ddecd75b1ea764a8468 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
32import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070037import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080042import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Bitmap;
44import android.graphics.Canvas;
Robert Greenwalt434203a2010-10-11 16:00:27 -070045import android.net.IConnectivityManager;
46import android.net.Proxy;
47import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080048import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070049import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070050import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Bundle;
52import android.os.Debug;
53import android.os.Handler;
54import android.os.IBinder;
55import android.os.Looper;
56import android.os.Message;
57import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070058import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Process;
60import android.os.RemoteException;
61import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070062import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070064import android.os.Trace;
Amith Yamasani742a6712011-05-04 14:49:28 -070065import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.util.DisplayMetrics;
68import android.util.EventLog;
69import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070070import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080071import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080072import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070073import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070075import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.View;
77import android.view.ViewDebug;
78import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070079import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.view.Window;
81import android.view.WindowManager;
82import android.view.WindowManagerImpl;
Jason Samsa6f338c2012-02-24 16:22:16 -080083import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85import com.android.internal.os.BinderInternal;
86import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070087import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
90
91import java.io.File;
92import java.io.FileDescriptor;
93import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070094import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import java.io.PrintWriter;
96import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070097import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.util.ArrayList;
99import java.util.HashMap;
100import java.util.Iterator;
101import java.util.List;
102import java.util.Locale;
103import java.util.Map;
104import java.util.TimeZone;
105import java.util.regex.Pattern;
106
Jeff Sharkeye861b422012-03-01 20:59:22 -0800107import libcore.io.IoUtils;
108
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800109import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111final class SuperNotCalledException extends AndroidRuntimeException {
112 public SuperNotCalledException(String msg) {
113 super(msg);
114 }
115}
116
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700117final class RemoteServiceException extends AndroidRuntimeException {
118 public RemoteServiceException(String msg) {
119 super(msg);
120 }
121}
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123/**
124 * This manages the execution of the main thread in an
125 * application process, scheduling and executing activities,
126 * broadcasts, and other operations on it as the activity
127 * manager requests.
128 *
129 * {@hide}
130 */
131public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700132 /** @hide */
133 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700134 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700135 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700136 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700137 /** @hide */
138 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700139 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700140 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700141 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800142 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700143 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700144 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
146 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
147 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
148 private static final int LOG_ON_PAUSE_CALLED = 30021;
149 private static final int LOG_ON_RESUME_CALLED = 30022;
150
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700151 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700152
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700153 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700155 final ApplicationThread mAppThread = new ApplicationThread();
156 final Looper mLooper = Looper.myLooper();
157 final H mH = new H();
158 final HashMap<IBinder, ActivityClientRecord> mActivities
159 = new HashMap<IBinder, ActivityClientRecord>();
160 // List of new activities (via ActivityRecord.nextIdle) that should
161 // be reported when next we idle.
162 ActivityClientRecord mNewActivities = null;
163 // Number of activities that are currently visible on-screen.
164 int mNumVisibleActivities = 0;
165 final HashMap<IBinder, Service> mServices
166 = new HashMap<IBinder, Service>();
167 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700168 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700169 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700170 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700171 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700172 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700173 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700174 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 Application mInitialApplication;
176 final ArrayList<Application> mAllApplications
177 = new ArrayList<Application>();
178 // set of instantiated backup agents, keyed by package name
179 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700180 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 Instrumentation mInstrumentation;
182 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700183 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 String mInstrumentationAppPackage = null;
185 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700186 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700187 boolean mSystemThread = false;
188 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700190 // These can be accessed by multiple threads; mPackages is the lock.
191 // XXX For now we keep around information about all packages we have
192 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800193 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700194 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800195 // which means this lock gets held while the activity and window managers
196 // holds their own lock. Thus you MUST NEVER call back into the activity manager
197 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700198 final HashMap<String, WeakReference<LoadedApk>> mPackages
199 = new HashMap<String, WeakReference<LoadedApk>>();
200 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
201 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700202 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
203 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700204 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
205 = new HashMap<ResourcesKey, WeakReference<Resources> >();
206 final ArrayList<ActivityClientRecord> mRelaunchingActivities
207 = new ArrayList<ActivityClientRecord>();
208 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700210 // The lock of mProviderMap protects the following variables.
211 final HashMap<String, ProviderClientRecord> mProviderMap
212 = new HashMap<String, ProviderClientRecord>();
213 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
214 = new HashMap<IBinder, ProviderRefCount>();
215 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
216 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700217 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
218 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Jeff Hamilton52d32032011-01-08 15:31:26 -0600220 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
221 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
222
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 final GcIdler mGcIdler = new GcIdler();
224 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700226 static Handler sMainThreadHandler; // set once in main()
227
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800228 Bundle mCoreSettings = null;
229
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400230 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700232 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 Intent intent;
234 Bundle state;
235 Activity activity;
236 Window window;
237 Activity parent;
238 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700239 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 boolean paused;
241 boolean stopped;
242 boolean hideForNow;
243 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700244 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700245 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700247 String profileFile;
248 ParcelFileDescriptor profileFd;
249 boolean autoStopProfiler;
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400252 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700253 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
255 List<ResultInfo> pendingResults;
256 List<Intent> pendingIntents;
257
258 boolean startsNotResumed;
259 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800260 int pendingConfigChanges;
261 boolean onlyLocalRequest;
262
263 View mPendingRemoveWindow;
264 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700266 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 parent = null;
268 embeddedID = null;
269 paused = false;
270 stopped = false;
271 hideForNow = false;
272 nextIdle = null;
273 }
274
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800275 public boolean isPreHoneycomb() {
276 if (activity != null) {
277 return activity.getApplicationInfo().targetSdkVersion
278 < android.os.Build.VERSION_CODES.HONEYCOMB;
279 }
280 return false;
281 }
282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700284 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 return "ActivityRecord{"
286 + Integer.toHexString(System.identityHashCode(this))
287 + " token=" + token + " " + (componentName == null
288 ? "no component name" : componentName.toShortString())
289 + "}";
290 }
291 }
292
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700293 final class ProviderClientRecord {
294 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 final IContentProvider mProvider;
296 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700297 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700299 ProviderClientRecord(String[] names, IContentProvider provider,
300 ContentProvider localProvider,
301 IActivityManager.ContentProviderHolder holder) {
302 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mProvider = provider;
304 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700305 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 }
307 }
308
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400309 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 List<Intent> intents;
311 IBinder token;
312 public String toString() {
313 return "NewIntentData{intents=" + intents + " token=" + token + "}";
314 }
315 }
316
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400317 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700318 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
319 boolean ordered, boolean sticky, IBinder token) {
320 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
321 this.intent = intent;
322 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 Intent intent;
325 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400326 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 public String toString() {
328 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700329 info.packageName + " resultCode=" + getResultCode()
330 + " resultData=" + getResultData() + " resultExtras="
331 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 }
333 }
334
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400335 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700336 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400337 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700338 int backupMode;
339 public String toString() {
340 return "CreateBackupAgentData{appInfo=" + appInfo
341 + " backupAgent=" + appInfo.backupAgentName
342 + " mode=" + backupMode + "}";
343 }
344 }
Bob Leee5408332009-09-04 18:31:17 -0700345
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400346 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 IBinder token;
348 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400349 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 Intent intent;
351 public String toString() {
352 return "CreateServiceData{token=" + token + " className="
353 + info.name + " packageName=" + info.packageName
354 + " intent=" + intent + "}";
355 }
356 }
357
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400358 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 IBinder token;
360 Intent intent;
361 boolean rebind;
362 public String toString() {
363 return "BindServiceData{token=" + token + " intent=" + intent + "}";
364 }
365 }
366
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400367 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700369 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700371 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 Intent args;
373 public String toString() {
374 return "ServiceArgsData{token=" + token + " startId=" + startId
375 + " args=" + args + "}";
376 }
377 }
378
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400379 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700380 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 String processName;
382 ApplicationInfo appInfo;
383 List<ProviderInfo> providers;
384 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 Bundle instrumentationArgs;
386 IInstrumentationWatcher instrumentationWatcher;
387 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800388 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700389 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700390 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400392 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700393
394 /** Initial values for {@link Profiler}. */
395 String initProfileFile;
396 ParcelFileDescriptor initProfileFd;
397 boolean initAutoStopProfiler;
398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 public String toString() {
400 return "AppBindData{appInfo=" + appInfo + "}";
401 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700402 }
403
404 static final class Profiler {
405 String profileFile;
406 ParcelFileDescriptor profileFd;
407 boolean autoStopProfiler;
408 boolean profiling;
409 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700410 public void setProfiler(String file, ParcelFileDescriptor fd) {
411 if (profiling) {
412 if (fd != null) {
413 try {
414 fd.close();
415 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700416 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700417 }
418 }
419 return;
420 }
421 if (profileFd != null) {
422 try {
423 profileFd.close();
424 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700425 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700426 }
427 }
428 profileFile = file;
429 profileFd = fd;
430 }
431 public void startProfiling() {
432 if (profileFd == null || profiling) {
433 return;
434 }
435 try {
436 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
437 8 * 1024 * 1024, 0);
438 profiling = true;
439 } catch (RuntimeException e) {
440 Slog.w(TAG, "Profiling failed on path " + profileFile);
441 try {
442 profileFd.close();
443 profileFd = null;
444 } catch (IOException e2) {
445 Slog.w(TAG, "Failure closing profile fd", e2);
446 }
447 }
448 }
449 public void stopProfiling() {
450 if (profiling) {
451 profiling = false;
452 Debug.stopMethodTracing();
453 if (profileFd != null) {
454 try {
455 profileFd.close();
456 } catch (IOException e) {
457 }
458 }
459 profileFd = null;
460 profileFile = null;
461 }
462 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700466 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700467 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800468 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 }
471
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400472 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 IBinder token;
474 List<ResultInfo> results;
475 public String toString() {
476 return "ResultData{token=" + token + " results" + results + "}";
477 }
478 }
479
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400480 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800481 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 String what;
483 String who;
484 }
485
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700487 String path;
488 ParcelFileDescriptor fd;
489 }
490
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400491 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700492 String path;
493 ParcelFileDescriptor fd;
494 }
495
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400496 static final class UpdateCompatibilityData {
497 String pkg;
498 CompatibilityInfo info;
499 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700500
Romain Guy65b345f2011-07-27 18:51:50 -0700501 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700502
Romain Guy65b345f2011-07-27 18:51:50 -0700503 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700504 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
505 private static final String ONE_COUNT_COLUMN = "%21s %8d";
506 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700507 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 // Formatting for checkin service - update version if row format changes
510 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700511
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700512 private void updatePendingConfiguration(Configuration config) {
513 synchronized (mPackages) {
514 if (mPendingConfiguration == null ||
515 mPendingConfiguration.isOtherSeqNewer(config)) {
516 mPendingConfiguration = config;
517 }
518 }
519 }
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public final void schedulePauseActivity(IBinder token, boolean finished,
522 boolean userLeaving, int configChanges) {
523 queueOrSendMessage(
524 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
525 token,
526 (userLeaving ? 1 : 0),
527 configChanges);
528 }
529
530 public final void scheduleStopActivity(IBinder token, boolean showWindow,
531 int configChanges) {
532 queueOrSendMessage(
533 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
534 token, 0, configChanges);
535 }
536
537 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
538 queueOrSendMessage(
539 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
540 token);
541 }
542
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800543 public final void scheduleSleeping(IBinder token, boolean sleeping) {
544 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
545 }
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
548 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
549 }
550
551 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
552 ResultData res = new ResultData();
553 res.token = token;
554 res.results = results;
555 queueOrSendMessage(H.SEND_RESULT, res);
556 }
557
558 // we use token to identify this activity without having to send the
559 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700560 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700561 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
562 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700563 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
564 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700565 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566
567 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700568 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 r.intent = intent;
570 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400571 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 r.state = state;
573
574 r.pendingResults = pendingResults;
575 r.pendingIntents = pendingNewIntents;
576
577 r.startsNotResumed = notResumed;
578 r.isForward = isForward;
579
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700580 r.profileFile = profileName;
581 r.profileFd = profileFd;
582 r.autoStopProfiler = autoStopProfiler;
583
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700584 updatePendingConfiguration(curConfig);
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
587 }
588
589 public final void scheduleRelaunchActivity(IBinder token,
590 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800591 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800592 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
593 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 }
595
596 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
597 NewIntentData data = new NewIntentData();
598 data.intents = intents;
599 data.token = token;
600
601 queueOrSendMessage(H.NEW_INTENT, data);
602 }
603
604 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
605 int configChanges) {
606 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
607 configChanges);
608 }
609
610 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400611 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
612 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700613 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
614 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400616 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 queueOrSendMessage(H.RECEIVER, r);
618 }
619
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400620 public final void scheduleCreateBackupAgent(ApplicationInfo app,
621 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700622 CreateBackupAgentData d = new CreateBackupAgentData();
623 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400624 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700625 d.backupMode = backupMode;
626
627 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
628 }
629
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400630 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
631 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700632 CreateBackupAgentData d = new CreateBackupAgentData();
633 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400634 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700635
636 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
637 }
638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400640 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 CreateServiceData s = new CreateServiceData();
642 s.token = token;
643 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400644 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645
646 queueOrSendMessage(H.CREATE_SERVICE, s);
647 }
648
649 public final void scheduleBindService(IBinder token, Intent intent,
650 boolean rebind) {
651 BindServiceData s = new BindServiceData();
652 s.token = token;
653 s.intent = intent;
654 s.rebind = rebind;
655
Amith Yamasani742a6712011-05-04 14:49:28 -0700656 if (DEBUG_SERVICE)
657 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
658 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 queueOrSendMessage(H.BIND_SERVICE, s);
660 }
661
662 public final void scheduleUnbindService(IBinder token, Intent intent) {
663 BindServiceData s = new BindServiceData();
664 s.token = token;
665 s.intent = intent;
666
667 queueOrSendMessage(H.UNBIND_SERVICE, s);
668 }
669
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700670 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700671 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 ServiceArgsData s = new ServiceArgsData();
673 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700674 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700676 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 s.args = args;
678
679 queueOrSendMessage(H.SERVICE_ARGS, s);
680 }
681
682 public final void scheduleStopService(IBinder token) {
683 queueOrSendMessage(H.STOP_SERVICE, token);
684 }
685
686 public final void bindApplication(String processName,
687 ApplicationInfo appInfo, List<ProviderInfo> providers,
688 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700689 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800691 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
692 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700693 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
695 if (services != null) {
696 // Setup the service cache in the ServiceManager
697 ServiceManager.initServiceCache(services);
698 }
699
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800700 setCoreSettings(coreSettings);
701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 AppBindData data = new AppBindData();
703 data.processName = processName;
704 data.appInfo = appInfo;
705 data.providers = providers;
706 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 data.instrumentationArgs = instrumentationArgs;
708 data.instrumentationWatcher = instrumentationWatcher;
709 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800710 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700711 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700712 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400714 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700715 data.initProfileFile = profileFile;
716 data.initProfileFd = profileFd;
717 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 queueOrSendMessage(H.BIND_APPLICATION, data);
719 }
720
721 public final void scheduleExit() {
722 queueOrSendMessage(H.EXIT_APPLICATION, null);
723 }
724
Christopher Tate5e1ab332009-09-01 20:32:49 -0700725 public final void scheduleSuicide() {
726 queueOrSendMessage(H.SUICIDE, null);
727 }
728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 public void requestThumbnail(IBinder token) {
730 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
731 }
732
733 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700734 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
736 }
737
738 public void updateTimeZone() {
739 TimeZone.setDefault(null);
740 }
741
Robert Greenwalt03595d02010-11-02 14:08:23 -0700742 public void clearDnsCache() {
743 // a non-standard API to get this to libcore
744 InetAddress.clearDnsCache();
745 }
746
Robert Greenwalt434203a2010-10-11 16:00:27 -0700747 public void setHttpProxy(String host, String port, String exclList) {
748 Proxy.setHttpProxySystemProperty(host, port, exclList);
749 }
750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public void processInBackground() {
752 mH.removeMessages(H.GC_WHEN_IDLE);
753 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
754 }
755
756 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700757 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700758 try {
759 data.fd = ParcelFileDescriptor.dup(fd);
760 data.token = servicetoken;
761 data.args = args;
762 queueOrSendMessage(H.DUMP_SERVICE, data);
763 } catch (IOException e) {
764 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 }
766 }
767
768 // This function exists to make sure all receiver dispatching is
769 // correctly ordered, since these are one-way calls and the binder driver
770 // applies transaction ordering per object for such calls.
771 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700772 int resultCode, String dataStr, Bundle extras, boolean ordered,
773 boolean sticky) throws RemoteException {
774 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 }
Bob Leee5408332009-09-04 18:31:17 -0700776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 public void scheduleLowMemory() {
778 queueOrSendMessage(H.LOW_MEMORY, null);
779 }
780
781 public void scheduleActivityConfigurationChanged(IBinder token) {
782 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
783 }
784
Romain Guy7eabe552011-07-21 14:56:34 -0700785 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
786 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700787 ProfilerControlData pcd = new ProfilerControlData();
788 pcd.path = path;
789 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700790 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800791 }
792
Andy McFadden824c5102010-07-09 16:26:57 -0700793 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
794 DumpHeapData dhd = new DumpHeapData();
795 dhd.path = path;
796 dhd.fd = fd;
797 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
798 }
799
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700800 public void setSchedulingGroup(int group) {
801 // Note: do this immediately, since going into the foreground
802 // should happen regardless of what pending work we have to do
803 // and the activity manager will wait for us to report back that
804 // we are done before sending us to the background.
805 try {
806 Process.setProcessGroup(Process.myPid(), group);
807 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800808 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700809 }
810 }
Bob Leee5408332009-09-04 18:31:17 -0700811
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700812 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
813 Debug.getMemoryInfo(outInfo);
814 }
Bob Leee5408332009-09-04 18:31:17 -0700815
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700816 public void dispatchPackageBroadcast(int cmd, String[] packages) {
817 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
818 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700819
820 public void scheduleCrash(String msg) {
821 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
822 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700823
Dianne Hackborn30d71892010-12-11 10:37:55 -0800824 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
825 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700826 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700827 try {
828 data.fd = ParcelFileDescriptor.dup(fd);
829 data.token = activitytoken;
830 data.prefix = prefix;
831 data.args = args;
832 queueOrSendMessage(H.DUMP_ACTIVITY, data);
833 } catch (IOException e) {
834 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700835 }
836 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700837
Marco Nelissen18cb2872011-11-15 11:19:53 -0800838 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
839 String[] args) {
840 DumpComponentInfo data = new DumpComponentInfo();
841 try {
842 data.fd = ParcelFileDescriptor.dup(fd);
843 data.token = providertoken;
844 data.args = args;
845 queueOrSendMessage(H.DUMP_PROVIDER, data);
846 } catch (IOException e) {
847 Slog.w(TAG, "dumpProvider failed", e);
848 }
849 }
850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700852 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
853 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700854 FileOutputStream fout = new FileOutputStream(fd);
855 PrintWriter pw = new PrintWriter(fout);
856 try {
Romain Guya998dff2012-03-23 18:58:36 -0700857 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700858 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700859 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700860 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700861 }
862
Romain Guya998dff2012-03-23 18:58:36 -0700863 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 long nativeMax = Debug.getNativeHeapSize() / 1024;
865 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
866 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
867
868 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
869 Debug.getMemoryInfo(memInfo);
870
Dianne Hackbornb437e092011-08-05 17:50:29 -0700871 if (!all) {
872 return memInfo;
873 }
874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 Runtime runtime = Runtime.getRuntime();
876
877 long dalvikMax = runtime.totalMemory() / 1024;
878 long dalvikFree = runtime.freeMemory() / 1024;
879 long dalvikAllocated = dalvikMax - dalvikFree;
880 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700881 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700882 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
883 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 int globalAssetCount = AssetManager.getGlobalAssetCount();
885 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
886 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
887 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
888 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700889 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800890 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700893 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // NOTE: if you change anything significant below, also consider changing
895 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700896 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 // Header
900 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
901 pw.print(Process.myPid()); pw.print(',');
902 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 // Heap info - max
905 pw.print(nativeMax); pw.print(',');
906 pw.print(dalvikMax); pw.print(',');
907 pw.print("N/A,");
908 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 // Heap info - allocated
911 pw.print(nativeAllocated); pw.print(',');
912 pw.print(dalvikAllocated); pw.print(',');
913 pw.print("N/A,");
914 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 // Heap info - free
917 pw.print(nativeFree); pw.print(',');
918 pw.print(dalvikFree); pw.print(',');
919 pw.print("N/A,");
920 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // Heap info - proportional set size
923 pw.print(memInfo.nativePss); pw.print(',');
924 pw.print(memInfo.dalvikPss); pw.print(',');
925 pw.print(memInfo.otherPss); pw.print(',');
926 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700929 pw.print(memInfo.nativeSharedDirty); pw.print(',');
930 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
931 pw.print(memInfo.otherSharedDirty); pw.print(',');
932 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
933 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 pw.print(memInfo.nativePrivateDirty); pw.print(',');
937 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
938 pw.print(memInfo.otherPrivateDirty); pw.print(',');
939 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
940 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // Object counts
943 pw.print(viewInstanceCount); pw.print(',');
944 pw.print(viewRootInstanceCount); pw.print(',');
945 pw.print(appContextInstanceCount); pw.print(',');
946 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 pw.print(globalAssetCount); pw.print(',');
949 pw.print(globalAssetManagerCount); pw.print(',');
950 pw.print(binderLocalObjectCount); pw.print(',');
951 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 pw.print(binderDeathObjectCount); pw.print(',');
954 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800957 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800958 pw.print(stats.memoryUsed / 1024); pw.print(',');
959 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700960 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800961 for (int i = 0; i < stats.dbStats.size(); i++) {
962 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700963 pw.print(','); pw.print(dbStats.dbName);
964 pw.print(','); pw.print(dbStats.pageSize);
965 pw.print(','); pw.print(dbStats.dbSize);
966 pw.print(','); pw.print(dbStats.lookaside);
967 pw.print(','); pw.print(dbStats.cache);
968 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800969 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700970 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700971
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700972 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
Bob Leee5408332009-09-04 18:31:17 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700976 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
977 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
978 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
979 "------");
980 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
981 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
982 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
983 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700985 int otherPss = memInfo.otherPss;
986 int otherSharedDirty = memInfo.otherSharedDirty;
987 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700989 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700990 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700991 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
992 memInfo.getOtherPrivateDirty(i), "", "", "");
993 otherPss -= memInfo.getOtherPss(i);
994 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
995 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
996 }
997
998 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
999 otherPrivateDirty, "", "", "");
1000 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1001 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1002 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1003 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004
1005 pw.println(" ");
1006 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001007 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 viewRootInstanceCount);
1009
1010 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1011 "Activities:", activityInstanceCount);
1012
1013 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1014 "AssetManagers:", globalAssetManagerCount);
1015
1016 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1017 "Proxy Binders:", binderProxyObjectCount);
1018 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1019
1020 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 // SQLite mem info
1023 pw.println(" ");
1024 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001025 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1026 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1027 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001028 pw.println(" ");
1029 int N = stats.dbStats.size();
1030 if (N > 0) {
1031 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001032 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1033 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001034 for (int i = 0; i < N; i++) {
1035 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001036 printRow(pw, DB_INFO_FORMAT,
1037 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1038 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1039 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1040 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001041 }
1042 }
Bob Leee5408332009-09-04 18:31:17 -07001043
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001044 // Asset details.
1045 String assetAlloc = AssetManager.getAssetAllocations();
1046 if (assetAlloc != null) {
1047 pw.println(" ");
1048 pw.println(" Asset Allocations");
1049 pw.print(assetAlloc);
1050 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001051
1052 return memInfo;
1053 }
1054
1055 @Override
1056 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1057 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001058 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
1060
Jeff Brown6754ba22011-12-14 20:20:01 -08001061 @Override
1062 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1063 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1064 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1065 SQLiteDebug.dump(printer, args);
1066 pw.flush();
1067 }
1068
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001069 @Override
1070 public void unstableProviderDied(IBinder provider) {
1071 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1072 }
1073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 private void printRow(PrintWriter pw, String format, Object...objs) {
1075 pw.println(String.format(format, objs));
1076 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001077
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001078 public void setCoreSettings(Bundle coreSettings) {
1079 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001080 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001081
1082 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1083 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1084 ucd.pkg = pkg;
1085 ucd.info = info;
1086 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1087 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001088
1089 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001090 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001091 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
Romain Guy65b345f2011-07-27 18:51:50 -07001095 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 public static final int LAUNCH_ACTIVITY = 100;
1097 public static final int PAUSE_ACTIVITY = 101;
1098 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1099 public static final int STOP_ACTIVITY_SHOW = 103;
1100 public static final int STOP_ACTIVITY_HIDE = 104;
1101 public static final int SHOW_WINDOW = 105;
1102 public static final int HIDE_WINDOW = 106;
1103 public static final int RESUME_ACTIVITY = 107;
1104 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001105 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 public static final int BIND_APPLICATION = 110;
1107 public static final int EXIT_APPLICATION = 111;
1108 public static final int NEW_INTENT = 112;
1109 public static final int RECEIVER = 113;
1110 public static final int CREATE_SERVICE = 114;
1111 public static final int SERVICE_ARGS = 115;
1112 public static final int STOP_SERVICE = 116;
1113 public static final int REQUEST_THUMBNAIL = 117;
1114 public static final int CONFIGURATION_CHANGED = 118;
1115 public static final int CLEAN_UP_CONTEXT = 119;
1116 public static final int GC_WHEN_IDLE = 120;
1117 public static final int BIND_SERVICE = 121;
1118 public static final int UNBIND_SERVICE = 122;
1119 public static final int DUMP_SERVICE = 123;
1120 public static final int LOW_MEMORY = 124;
1121 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1122 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001123 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001124 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001125 public static final int DESTROY_BACKUP_AGENT = 129;
1126 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001127 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001128 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001129 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001130 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001131 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001132 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001133 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001134 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001135 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001136 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001137 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001138 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001140 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 switch (code) {
1142 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1143 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1144 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1145 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1146 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1147 case SHOW_WINDOW: return "SHOW_WINDOW";
1148 case HIDE_WINDOW: return "HIDE_WINDOW";
1149 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1150 case SEND_RESULT: return "SEND_RESULT";
1151 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1152 case BIND_APPLICATION: return "BIND_APPLICATION";
1153 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1154 case NEW_INTENT: return "NEW_INTENT";
1155 case RECEIVER: return "RECEIVER";
1156 case CREATE_SERVICE: return "CREATE_SERVICE";
1157 case SERVICE_ARGS: return "SERVICE_ARGS";
1158 case STOP_SERVICE: return "STOP_SERVICE";
1159 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1160 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1161 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1162 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1163 case BIND_SERVICE: return "BIND_SERVICE";
1164 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1165 case DUMP_SERVICE: return "DUMP_SERVICE";
1166 case LOW_MEMORY: return "LOW_MEMORY";
1167 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1168 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001169 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001170 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1171 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001172 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001173 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001174 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001175 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001176 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001177 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001178 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001179 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001180 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001181 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001182 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001183 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001184 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 }
1186 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001187 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 }
1189 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001190 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 switch (msg.what) {
1192 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001193 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001194 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195
1196 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001197 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001198 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001199 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 } break;
1201 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001202 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001203 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001204 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001205 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } break;
1207 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001208 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001210 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001211 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 break;
1213 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001216 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217 break;
1218 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001219 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001221 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 break;
1223 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001224 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001226 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 break;
1228 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001229 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001231 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 break;
1233 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001234 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001236 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 break;
1238 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001239 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 handleResumeActivity((IBinder)msg.obj, true,
1241 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 break;
1244 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001245 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001247 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 break;
1249 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001250 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1252 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001253 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 break;
1255 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 AppBindData data = (AppBindData)msg.obj;
1258 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001259 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 break;
1261 case EXIT_APPLICATION:
1262 if (mInitialApplication != null) {
1263 mInitialApplication.onTerminate();
1264 }
1265 Looper.myLooper().quit();
1266 break;
1267 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001268 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001270 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 break;
1272 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001273 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001275 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001276 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 break;
1278 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001281 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 break;
1283 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001286 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 break;
1288 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001289 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 break;
1293 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001294 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 break;
1298 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001299 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001301 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 break;
1304 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001307 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 break;
1309 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001310 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001311 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001312 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case CLEAN_UP_CONTEXT:
1316 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1317 cci.context.performFinalCleanup(cci.who, cci.what);
1318 break;
1319 case GC_WHEN_IDLE:
1320 scheduleGcIdler();
1321 break;
1322 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001323 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 break;
1325 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001328 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 break;
1330 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001335 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001336 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001337 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001338 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001340 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001341 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001342 break;
1343 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001344 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001345 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001347 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001348 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001349 Process.killProcess(Process.myPid());
1350 break;
1351 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001352 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001353 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001354 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001355 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001356 case ENABLE_JIT:
1357 ensureJitEnabled();
1358 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001359 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001360 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001361 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001362 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001363 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001364 case SCHEDULE_CRASH:
1365 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001366 case DUMP_HEAP:
1367 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1368 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001369 case DUMP_ACTIVITY:
1370 handleDumpActivity((DumpComponentInfo)msg.obj);
1371 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001372 case DUMP_PROVIDER:
1373 handleDumpProvider((DumpComponentInfo)msg.obj);
1374 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001375 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001376 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001377 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001378 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001379 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001380 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001381 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001382 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001384 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001385 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1386 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001387 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001388 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001390 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001391 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001392 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001393 case UNSTABLE_PROVIDER_DIED:
1394 handleUnstableProviderDied((IBinder)msg.obj, false);
1395 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001397 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
Bob Leee5408332009-09-04 18:31:17 -07001399
Brian Carlstromed7e0072011-03-24 13:27:57 -07001400 private void maybeSnapshot() {
1401 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001402 // convert the *private* ActivityThread.PackageInfo to *public* known
1403 // android.content.pm.PackageInfo
1404 String packageName = mBoundApplication.info.mPackageName;
1405 android.content.pm.PackageInfo packageInfo = null;
1406 try {
1407 Context context = getSystemContext();
1408 if(context == null) {
1409 Log.e(TAG, "cannot get a valid context");
1410 return;
1411 }
1412 PackageManager pm = context.getPackageManager();
1413 if(pm == null) {
1414 Log.e(TAG, "cannot get a valid PackageManager");
1415 return;
1416 }
1417 packageInfo = pm.getPackageInfo(
1418 packageName, PackageManager.GET_ACTIVITIES);
1419 } catch (NameNotFoundException e) {
1420 Log.e(TAG, "cannot get package info for " + packageName, e);
1421 }
1422 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001423 }
1424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426
Romain Guy65b345f2011-07-27 18:51:50 -07001427 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001429 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001430 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001431 if (mBoundApplication != null && mProfiler.profileFd != null
1432 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001433 stopProfiling = true;
1434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 if (a != null) {
1436 mNewActivities = null;
1437 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001438 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001440 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 TAG, "Reporting idle of " + a +
1442 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001443 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 if (a.activity != null && !a.activity.mFinished) {
1445 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001446 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001447 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001449 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451 }
1452 prev = a;
1453 a = a.nextIdle;
1454 prev.nextIdle = null;
1455 } while (a != null);
1456 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001457 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001458 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001459 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001460 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 return false;
1462 }
1463 }
1464
1465 final class GcIdler implements MessageQueue.IdleHandler {
1466 public final boolean queueIdle() {
1467 doGcIfNeeded();
1468 return false;
1469 }
1470 }
1471
Romain Guy65b345f2011-07-27 18:51:50 -07001472 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001473 final private String mResDir;
1474 final private float mScale;
1475 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001476
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001477 ResourcesKey(String resDir, float scale) {
1478 mResDir = resDir;
1479 mScale = scale;
1480 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1481 }
Bob Leee5408332009-09-04 18:31:17 -07001482
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001483 @Override
1484 public int hashCode() {
1485 return mHash;
1486 }
1487
1488 @Override
1489 public boolean equals(Object obj) {
1490 if (!(obj instanceof ResourcesKey)) {
1491 return false;
1492 }
1493 ResourcesKey peer = (ResourcesKey) obj;
1494 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1495 }
1496 }
1497
Romain Guy65b345f2011-07-27 18:51:50 -07001498 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001499 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501
Romain Guy65b345f2011-07-27 18:51:50 -07001502 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001503 ActivityThread am = currentActivityThread();
1504 return (am != null && am.mBoundApplication != null)
1505 ? am.mBoundApplication.processName : null;
1506 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507
Romain Guy65b345f2011-07-27 18:51:50 -07001508 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001509 ActivityThread am = currentActivityThread();
1510 return am != null ? am.mInitialApplication : null;
1511 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001513 public static IPackageManager getPackageManager() {
1514 if (sPackageManager != null) {
1515 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1516 return sPackageManager;
1517 }
1518 IBinder b = ServiceManager.getService("package");
1519 //Slog.v("PackageManager", "default service binder = " + b);
1520 sPackageManager = IPackageManager.Stub.asInterface(b);
1521 //Slog.v("PackageManager", "default service = " + sPackageManager);
1522 return sPackageManager;
1523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001525 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1526 DisplayMetrics dm = mDisplayMetrics.get(ci);
1527 if (dm != null && !forceUpdate) {
1528 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001529 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001530 if (dm == null) {
1531 dm = new DisplayMetrics();
1532 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001533 }
Jeff Brownd32460c2012-07-20 16:15:36 -07001534 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1535 cih.set(ci);
1536 Display d = WindowManagerImpl.getDefault().makeCompatible(cih).getDefaultDisplay();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001537 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001538 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1539 // + metrics.heightPixels + " den=" + metrics.density
1540 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001541 return dm;
1542 }
1543
Romain Guy65b345f2011-07-27 18:51:50 -07001544 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001545 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1546 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001547 if (config == null) {
1548 return null;
1549 }
1550 if (compat != null && !compat.supportsScreen()) {
1551 mMainThreadConfig.setTo(config);
1552 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001553 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001554 }
1555 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001558 /**
1559 * Creates the top level Resources for applications with the given compatibility info.
1560 *
1561 * @param resDir the resource directory.
1562 * @param compInfo the compability info. It will use the default compatibility info when it's
1563 * null.
1564 */
1565 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1566 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1567 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001569 // Resources is app scale dependent.
1570 if (false) {
1571 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1572 + compInfo.applicationScale);
1573 }
1574 WeakReference<Resources> wr = mActiveResources.get(key);
1575 r = wr != null ? wr.get() : null;
1576 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1577 if (r != null && r.getAssets().isUpToDate()) {
1578 if (false) {
1579 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1580 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1581 }
1582 return r;
1583 }
1584 }
1585
1586 //if (r != null) {
1587 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1588 // + r + " " + resDir);
1589 //}
1590
1591 AssetManager assets = new AssetManager();
1592 if (assets.addAssetPath(resDir) == 0) {
1593 return null;
1594 }
1595
1596 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001597 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001598 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1599 if (false) {
1600 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1601 + r.getConfiguration() + " appScale="
1602 + r.getCompatibilityInfo().applicationScale);
1603 }
1604
1605 synchronized (mPackages) {
1606 WeakReference<Resources> wr = mActiveResources.get(key);
1607 Resources existing = wr != null ? wr.get() : null;
1608 if (existing != null && existing.getAssets().isUpToDate()) {
1609 // Someone else already created the resources while we were
1610 // unlocked; go ahead and use theirs.
1611 r.getAssets().close();
1612 return existing;
1613 }
1614
1615 // XXX need to remove entries when weak references go away
1616 mActiveResources.put(key, new WeakReference<Resources>(r));
1617 return r;
1618 }
1619 }
1620
1621 /**
1622 * Creates the top level resources for the given package.
1623 */
1624 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001625 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001626 }
1627
1628 final Handler getHandler() {
1629 return mH;
1630 }
1631
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001632 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1633 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001634 synchronized (mPackages) {
1635 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1637 ref = mPackages.get(packageName);
1638 } else {
1639 ref = mResourcePackages.get(packageName);
1640 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001641 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001642 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001643 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1644 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 if (packageInfo != null && (packageInfo.mResources == null
1646 || packageInfo.mResources.getAssets().isUpToDate())) {
1647 if (packageInfo.isSecurityViolation()
1648 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1649 throw new SecurityException(
1650 "Requesting code from " + packageName
1651 + " to be run in process "
1652 + mBoundApplication.processName
1653 + "/" + mBoundApplication.appInfo.uid);
1654 }
1655 return packageInfo;
1656 }
1657 }
1658
1659 ApplicationInfo ai = null;
1660 try {
1661 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001662 PackageManager.GET_SHARED_LIBRARY_FILES, UserId.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001664 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 }
1666
1667 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001668 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
1670
1671 return null;
1672 }
1673
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001674 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1675 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1677 boolean securityViolation = includeCode && ai.uid != 0
1678 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Amith Yamasani742a6712011-05-04 14:49:28 -07001679 ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
1680 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1682 |Context.CONTEXT_IGNORE_SECURITY))
1683 == Context.CONTEXT_INCLUDE_CODE) {
1684 if (securityViolation) {
1685 String msg = "Requesting code from " + ai.packageName
1686 + " (with uid " + ai.uid + ")";
1687 if (mBoundApplication != null) {
1688 msg = msg + " to be run in process "
1689 + mBoundApplication.processName + " (with uid "
1690 + mBoundApplication.appInfo.uid + ")";
1691 }
1692 throw new SecurityException(msg);
1693 }
1694 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001695 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001698 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1699 CompatibilityInfo compatInfo) {
1700 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 }
1702
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001703 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1704 synchronized (mPackages) {
1705 WeakReference<LoadedApk> ref;
1706 if (includeCode) {
1707 ref = mPackages.get(packageName);
1708 } else {
1709 ref = mResourcePackages.get(packageName);
1710 }
1711 return ref != null ? ref.get() : null;
1712 }
1713 }
1714
Romain Guy65b345f2011-07-27 18:51:50 -07001715 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1717 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001718 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (includeCode) {
1720 ref = mPackages.get(aInfo.packageName);
1721 } else {
1722 ref = mResourcePackages.get(aInfo.packageName);
1723 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001724 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 if (packageInfo == null || (packageInfo.mResources != null
1726 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001727 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 : "Loading resource-only package ") + aInfo.packageName
1729 + " (in " + (mBoundApplication != null
1730 ? mBoundApplication.processName : null)
1731 + ")");
1732 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001733 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 securityViolation, includeCode &&
1735 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1736 if (includeCode) {
1737 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001738 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 } else {
1740 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001741 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 }
1743 }
1744 return packageInfo;
1745 }
1746 }
1747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 ActivityThread() {
1749 }
1750
1751 public ApplicationThread getApplicationThread()
1752 {
1753 return mAppThread;
1754 }
1755
1756 public Instrumentation getInstrumentation()
1757 {
1758 return mInstrumentation;
1759 }
1760
1761 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001762 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 }
1764
1765 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001766 return mProfiler != null && mProfiler.profileFile != null
1767 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769
1770 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001771 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
1773
1774 public Looper getLooper() {
1775 return mLooper;
1776 }
1777
1778 public Application getApplication() {
1779 return mInitialApplication;
1780 }
Bob Leee5408332009-09-04 18:31:17 -07001781
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001782 public String getProcessName() {
1783 return mBoundApplication.processName;
1784 }
Bob Leee5408332009-09-04 18:31:17 -07001785
Dianne Hackborn21556372010-02-04 16:34:40 -08001786 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001787 synchronized (this) {
1788 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001789 ContextImpl context =
1790 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001791 LoadedApk info = new LoadedApk(this, "android", context, null,
1792 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 context.init(info, null, this);
1794 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001795 getConfiguration(), getDisplayMetricsLocked(
1796 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001798 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 // + ": " + context.getResources().getConfiguration());
1800 }
1801 }
1802 return mSystemContext;
1803 }
1804
Mike Cleron432b7132009-09-24 15:28:29 -07001805 public void installSystemApplicationInfo(ApplicationInfo info) {
1806 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001807 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001808 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001809 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001810
1811 // give ourselves a default profiler
1812 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001813 }
1814 }
1815
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001816 void ensureJitEnabled() {
1817 if (!mJitEnabled) {
1818 mJitEnabled = true;
1819 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1820 }
1821 }
1822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 void scheduleGcIdler() {
1824 if (!mGcIdlerScheduled) {
1825 mGcIdlerScheduled = true;
1826 Looper.myQueue().addIdleHandler(mGcIdler);
1827 }
1828 mH.removeMessages(H.GC_WHEN_IDLE);
1829 }
1830
1831 void unscheduleGcIdler() {
1832 if (mGcIdlerScheduled) {
1833 mGcIdlerScheduled = false;
1834 Looper.myQueue().removeIdleHandler(mGcIdler);
1835 }
1836 mH.removeMessages(H.GC_WHEN_IDLE);
1837 }
1838
1839 void doGcIfNeeded() {
1840 mGcIdlerScheduled = false;
1841 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001842 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 // + "m now=" + now);
1844 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001845 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 BinderInternal.forceGc("bg");
1847 }
1848 }
1849
Jeff Hamilton52d32032011-01-08 15:31:26 -06001850 public void registerOnActivityPausedListener(Activity activity,
1851 OnActivityPausedListener listener) {
1852 synchronized (mOnPauseListeners) {
1853 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1854 if (list == null) {
1855 list = new ArrayList<OnActivityPausedListener>();
1856 mOnPauseListeners.put(activity, list);
1857 }
1858 list.add(listener);
1859 }
1860 }
1861
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001862 public void unregisterOnActivityPausedListener(Activity activity,
1863 OnActivityPausedListener listener) {
1864 synchronized (mOnPauseListeners) {
1865 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1866 if (list != null) {
1867 list.remove(listener);
1868 }
1869 }
1870 }
1871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 public final ActivityInfo resolveActivityInfo(Intent intent) {
1873 ActivityInfo aInfo = intent.resolveActivityInfo(
1874 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1875 if (aInfo == null) {
1876 // Throw an exception.
1877 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001878 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
1880 return aInfo;
1881 }
Bob Leee5408332009-09-04 18:31:17 -07001882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001885 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001886 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001888 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 r.intent = intent;
1890 r.state = state;
1891 r.parent = parent;
1892 r.embeddedID = id;
1893 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001894 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 if (localLOGV) {
1896 ComponentName compname = intent.getComponent();
1897 String name;
1898 if (compname != null) {
1899 name = compname.toShortString();
1900 } else {
1901 name = "(Intent " + intent + ").getComponent() returned null";
1902 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001903 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 + ", comp=" + name
1905 + ", token=" + token);
1906 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001907 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 }
1909
1910 public final Activity getActivity(IBinder token) {
1911 return mActivities.get(token).activity;
1912 }
1913
1914 public final void sendActivityResult(
1915 IBinder token, String id, int requestCode,
1916 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001917 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001918 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1920 list.add(new ResultInfo(id, requestCode, resultCode, data));
1921 mAppThread.scheduleSendResult(token, list);
1922 }
1923
1924 // if the thread hasn't started yet, we don't have the handler, so just
1925 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001926 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 queueOrSendMessage(what, obj, 0, 0);
1928 }
1929
Romain Guy65b345f2011-07-27 18:51:50 -07001930 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 queueOrSendMessage(what, obj, arg1, 0);
1932 }
1933
Romain Guy65b345f2011-07-27 18:51:50 -07001934 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001936 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1938 + ": " + arg1 + " / " + obj);
1939 Message msg = Message.obtain();
1940 msg.what = what;
1941 msg.obj = obj;
1942 msg.arg1 = arg1;
1943 msg.arg2 = arg2;
1944 mH.sendMessage(msg);
1945 }
1946 }
1947
Dianne Hackborn21556372010-02-04 16:34:40 -08001948 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 String what) {
1950 ContextCleanupInfo cci = new ContextCleanupInfo();
1951 cci.context = context;
1952 cci.who = who;
1953 cci.what = what;
1954 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1955 }
1956
Romain Guy65b345f2011-07-27 18:51:50 -07001957 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1959
1960 ActivityInfo aInfo = r.activityInfo;
1961 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001962 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 Context.CONTEXT_INCLUDE_CODE);
1964 }
Bob Leee5408332009-09-04 18:31:17 -07001965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 ComponentName component = r.intent.getComponent();
1967 if (component == null) {
1968 component = r.intent.resolveActivity(
1969 mInitialApplication.getPackageManager());
1970 r.intent.setComponent(component);
1971 }
1972
1973 if (r.activityInfo.targetActivity != null) {
1974 component = new ComponentName(r.activityInfo.packageName,
1975 r.activityInfo.targetActivity);
1976 }
1977
1978 Activity activity = null;
1979 try {
1980 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1981 activity = mInstrumentation.newActivity(
1982 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001983 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 r.intent.setExtrasClassLoader(cl);
1985 if (r.state != null) {
1986 r.state.setClassLoader(cl);
1987 }
1988 } catch (Exception e) {
1989 if (!mInstrumentation.onException(activity, e)) {
1990 throw new RuntimeException(
1991 "Unable to instantiate activity " + component
1992 + ": " + e.toString(), e);
1993 }
1994 }
1995
1996 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001997 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001998
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001999 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2000 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 TAG, r + ": app=" + app
2002 + ", appName=" + app.getPackageName()
2003 + ", pkg=" + r.packageInfo.getPackageName()
2004 + ", comp=" + r.intent.getComponent().toShortString()
2005 + ", dir=" + r.packageInfo.getAppDir());
2006
2007 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002008 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 appContext.init(r.packageInfo, r.token, this);
2010 appContext.setOuterContext(activity);
2011 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002012 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002013 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002014 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002015 activity.attach(appContext, this, getInstrumentation(), r.token,
2016 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002017 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002018
Christopher Tateb70f3df2009-04-07 16:07:59 -07002019 if (customIntent != null) {
2020 activity.mIntent = customIntent;
2021 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002022 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 activity.mStartedActivity = false;
2024 int theme = r.activityInfo.getThemeResource();
2025 if (theme != 0) {
2026 activity.setTheme(theme);
2027 }
2028
2029 activity.mCalled = false;
2030 mInstrumentation.callActivityOnCreate(activity, r.state);
2031 if (!activity.mCalled) {
2032 throw new SuperNotCalledException(
2033 "Activity " + r.intent.getComponent().toShortString() +
2034 " did not call through to super.onCreate()");
2035 }
2036 r.activity = activity;
2037 r.stopped = true;
2038 if (!r.activity.mFinished) {
2039 activity.performStart();
2040 r.stopped = false;
2041 }
2042 if (!r.activity.mFinished) {
2043 if (r.state != null) {
2044 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2045 }
2046 }
2047 if (!r.activity.mFinished) {
2048 activity.mCalled = false;
2049 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2050 if (!activity.mCalled) {
2051 throw new SuperNotCalledException(
2052 "Activity " + r.intent.getComponent().toShortString() +
2053 " did not call through to super.onPostCreate()");
2054 }
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057 r.paused = true;
2058
2059 mActivities.put(r.token, r);
2060
2061 } catch (SuperNotCalledException e) {
2062 throw e;
2063
2064 } catch (Exception e) {
2065 if (!mInstrumentation.onException(activity, e)) {
2066 throw new RuntimeException(
2067 "Unable to start activity " + component
2068 + ": " + e.toString(), e);
2069 }
2070 }
2071
2072 return activity;
2073 }
2074
Romain Guy65b345f2011-07-27 18:51:50 -07002075 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 // If we are getting ready to gc after going to the background, well
2077 // we are back active so skip it.
2078 unscheduleGcIdler();
2079
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002080 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002081 mProfiler.setProfiler(r.profileFile, r.profileFd);
2082 mProfiler.startProfiling();
2083 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002084 }
2085
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002086 // Make sure we are running with the most recent config.
2087 handleConfigurationChanged(null, null);
2088
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002089 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002091 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002092
2093 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002094 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002095 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 handleResumeActivity(r.token, false, r.isForward);
2097
2098 if (!r.activity.mFinished && r.startsNotResumed) {
2099 // The activity manager actually wants this one to start out
2100 // paused, because it needs to be visible but isn't in the
2101 // foreground. We accomplish this by going through the
2102 // normal startup (because activities expect to go through
2103 // onResume() the first time they run, before their window
2104 // is displayed), and then pausing it. However, in this case
2105 // we do -not- need to do the full pause cycle (of freezing
2106 // and such) because the activity manager assumes it can just
2107 // retain the current state it has.
2108 try {
2109 r.activity.mCalled = false;
2110 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002111 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002112 // we need to be created again. But we only do this
2113 // for pre-Honeycomb apps, which always save their state
2114 // when pausing, so we can not have them save their state
2115 // when restarting from a paused state. For HC and later,
2116 // we want to (and can) let the state be saved as the normal
2117 // part of stopping the activity.
2118 if (r.isPreHoneycomb()) {
2119 r.state = oldState;
2120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 if (!r.activity.mCalled) {
2122 throw new SuperNotCalledException(
2123 "Activity " + r.intent.getComponent().toShortString() +
2124 " did not call through to super.onPause()");
2125 }
2126
2127 } catch (SuperNotCalledException e) {
2128 throw e;
2129
2130 } catch (Exception e) {
2131 if (!mInstrumentation.onException(r.activity, e)) {
2132 throw new RuntimeException(
2133 "Unable to pause activity "
2134 + r.intent.getComponent().toShortString()
2135 + ": " + e.toString(), e);
2136 }
2137 }
2138 r.paused = true;
2139 }
2140 } else {
2141 // If there was an error, for any reason, tell the activity
2142 // manager to stop us.
2143 try {
2144 ActivityManagerNative.getDefault()
2145 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2146 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002147 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 }
2149 }
2150 }
2151
Romain Guy65b345f2011-07-27 18:51:50 -07002152 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 List<Intent> intents) {
2154 final int N = intents.size();
2155 for (int i=0; i<N; i++) {
2156 Intent intent = intents.get(i);
2157 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002158 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2160 }
2161 }
2162
2163 public final void performNewIntents(IBinder token,
2164 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002165 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (r != null) {
2167 final boolean resumed = !r.paused;
2168 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002169 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 mInstrumentation.callActivityOnPause(r.activity);
2171 }
2172 deliverNewIntents(r, intents);
2173 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002174 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002175 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 }
2178 }
Bob Leee5408332009-09-04 18:31:17 -07002179
Romain Guy65b345f2011-07-27 18:51:50 -07002180 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 performNewIntents(data.token, data.intents);
2182 }
2183
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002184 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2185
2186 /**
2187 * Return the Intent that's currently being handled by a
2188 * BroadcastReceiver on this thread, or null if none.
2189 * @hide
2190 */
2191 public static Intent getIntentBeingBroadcast() {
2192 return sCurrentBroadcastIntent.get();
2193 }
2194
Romain Guy65b345f2011-07-27 18:51:50 -07002195 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 // If we are getting ready to gc after going to the background, well
2197 // we are back active so skip it.
2198 unscheduleGcIdler();
2199
2200 String component = data.intent.getComponent().getClassName();
2201
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002202 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002203 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204
2205 IActivityManager mgr = ActivityManagerNative.getDefault();
2206
Romain Guy65b345f2011-07-27 18:51:50 -07002207 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 try {
2209 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2210 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002211 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2213 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002214 if (DEBUG_BROADCAST) Slog.i(TAG,
2215 "Finishing failed broadcast to " + data.intent.getComponent());
2216 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 throw new RuntimeException(
2218 "Unable to instantiate receiver " + component
2219 + ": " + e.toString(), e);
2220 }
2221
2222 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002223 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002224
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002225 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 TAG, "Performing receive of " + data.intent
2227 + ": app=" + app
2228 + ", appName=" + app.getPackageName()
2229 + ", pkg=" + packageInfo.getPackageName()
2230 + ", comp=" + data.intent.getComponent().toShortString()
2231 + ", dir=" + packageInfo.getAppDir());
2232
Dianne Hackborn21556372010-02-04 16:34:40 -08002233 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002234 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002235 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 receiver.onReceive(context.getReceiverRestrictedContext(),
2237 data.intent);
2238 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002239 if (DEBUG_BROADCAST) Slog.i(TAG,
2240 "Finishing failed broadcast to " + data.intent.getComponent());
2241 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002242 if (!mInstrumentation.onException(receiver, e)) {
2243 throw new RuntimeException(
2244 "Unable to start receiver " + component
2245 + ": " + e.toString(), e);
2246 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002247 } finally {
2248 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250
Dianne Hackborne829fef2010-10-26 17:44:01 -07002251 if (receiver.getPendingResult() != null) {
2252 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 }
2254 }
2255
Christopher Tate181fafa2009-05-14 11:12:14 -07002256 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002257 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002258 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002259
2260 // no longer idle; we have backup work to do
2261 unscheduleGcIdler();
2262
2263 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002264 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002265 String packageName = packageInfo.mPackageName;
2266 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002267 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002268 + " already exists");
2269 return;
2270 }
Bob Leee5408332009-09-04 18:31:17 -07002271
Christopher Tate181fafa2009-05-14 11:12:14 -07002272 BackupAgent agent = null;
2273 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002274
Christopher Tate79ec80d2011-06-24 14:58:49 -07002275 // full backup operation but no app-supplied agent? use the default implementation
2276 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2277 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002278 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002279 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002280
Christopher Tate181fafa2009-05-14 11:12:14 -07002281 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002282 IBinder binder = null;
2283 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002284 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2285
Christopher Tated1475e02009-07-09 15:36:17 -07002286 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002287 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002288
2289 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002290 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002291 context.init(packageInfo, null, this);
2292 context.setOuterContext(agent);
2293 agent.attach(context);
2294
2295 agent.onCreate();
2296 binder = agent.onBind();
2297 mBackupAgents.put(packageName, agent);
2298 } catch (Exception e) {
2299 // If this is during restore, fail silently; otherwise go
2300 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002301 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002302 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2303 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002304 throw e;
2305 }
2306 // falling through with 'binder' still null
2307 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002308
2309 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002310 try {
2311 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2312 } catch (RemoteException e) {
2313 // nothing to do.
2314 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002315 } catch (Exception e) {
2316 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002317 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002318 }
2319 }
2320
2321 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002322 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002323 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002324
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002325 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002326 String packageName = packageInfo.mPackageName;
2327 BackupAgent agent = mBackupAgents.get(packageName);
2328 if (agent != null) {
2329 try {
2330 agent.onDestroy();
2331 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002332 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002333 e.printStackTrace();
2334 }
2335 mBackupAgents.remove(packageName);
2336 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002337 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002338 }
2339 }
2340
Romain Guy65b345f2011-07-27 18:51:50 -07002341 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 // If we are getting ready to gc after going to the background, well
2343 // we are back active so skip it.
2344 unscheduleGcIdler();
2345
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002346 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002347 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 Service service = null;
2349 try {
2350 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2351 service = (Service) cl.loadClass(data.info.name).newInstance();
2352 } catch (Exception e) {
2353 if (!mInstrumentation.onException(service, e)) {
2354 throw new RuntimeException(
2355 "Unable to instantiate service " + data.info.name
2356 + ": " + e.toString(), e);
2357 }
2358 }
2359
2360 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002361 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362
Dianne Hackborn21556372010-02-04 16:34:40 -08002363 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 context.init(packageInfo, null, this);
2365
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002366 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 context.setOuterContext(service);
2368 service.attach(context, this, data.info.name, data.token, app,
2369 ActivityManagerNative.getDefault());
2370 service.onCreate();
2371 mServices.put(data.token, service);
2372 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002373 ActivityManagerNative.getDefault().serviceDoneExecuting(
2374 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002375 } catch (RemoteException e) {
2376 // nothing to do.
2377 }
2378 } catch (Exception e) {
2379 if (!mInstrumentation.onException(service, e)) {
2380 throw new RuntimeException(
2381 "Unable to create service " + data.info.name
2382 + ": " + e.toString(), e);
2383 }
2384 }
2385 }
2386
Romain Guy65b345f2011-07-27 18:51:50 -07002387 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002389 if (DEBUG_SERVICE)
2390 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 if (s != null) {
2392 try {
2393 data.intent.setExtrasClassLoader(s.getClassLoader());
2394 try {
2395 if (!data.rebind) {
2396 IBinder binder = s.onBind(data.intent);
2397 ActivityManagerNative.getDefault().publishService(
2398 data.token, data.intent, binder);
2399 } else {
2400 s.onRebind(data.intent);
2401 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002402 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002404 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 } catch (RemoteException ex) {
2406 }
2407 } catch (Exception e) {
2408 if (!mInstrumentation.onException(s, e)) {
2409 throw new RuntimeException(
2410 "Unable to bind to service " + s
2411 + " with " + data.intent + ": " + e.toString(), e);
2412 }
2413 }
2414 }
2415 }
2416
Romain Guy65b345f2011-07-27 18:51:50 -07002417 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 Service s = mServices.get(data.token);
2419 if (s != null) {
2420 try {
2421 data.intent.setExtrasClassLoader(s.getClassLoader());
2422 boolean doRebind = s.onUnbind(data.intent);
2423 try {
2424 if (doRebind) {
2425 ActivityManagerNative.getDefault().unbindFinished(
2426 data.token, data.intent, doRebind);
2427 } else {
2428 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002429 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 }
2431 } catch (RemoteException ex) {
2432 }
2433 } catch (Exception e) {
2434 if (!mInstrumentation.onException(s, e)) {
2435 throw new RuntimeException(
2436 "Unable to unbind to service " + s
2437 + " with " + data.intent + ": " + e.toString(), e);
2438 }
2439 }
2440 }
2441 }
2442
Dianne Hackborn625ac272010-09-17 18:29:22 -07002443 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002444 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2445 try {
2446 Service s = mServices.get(info.token);
2447 if (s != null) {
2448 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2449 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2450 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002452 } finally {
2453 IoUtils.closeQuietly(info.fd);
2454 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 }
2456 }
2457
Dianne Hackborn625ac272010-09-17 18:29:22 -07002458 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002459 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2460 try {
2461 ActivityClientRecord r = mActivities.get(info.token);
2462 if (r != null && r.activity != null) {
2463 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2464 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2465 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002466 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002467 } finally {
2468 IoUtils.closeQuietly(info.fd);
2469 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002470 }
2471 }
2472
Marco Nelissen18cb2872011-11-15 11:19:53 -08002473 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002474 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2475 try {
2476 ProviderClientRecord r = mLocalProviders.get(info.token);
2477 if (r != null && r.mLocalProvider != null) {
2478 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2479 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2480 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002481 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002482 } finally {
2483 IoUtils.closeQuietly(info.fd);
2484 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002485 }
2486 }
2487
Romain Guy65b345f2011-07-27 18:51:50 -07002488 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 Service s = mServices.get(data.token);
2490 if (s != null) {
2491 try {
2492 if (data.args != null) {
2493 data.args.setExtrasClassLoader(s.getClassLoader());
2494 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002495 int res;
2496 if (!data.taskRemoved) {
2497 res = s.onStartCommand(data.args, data.flags, data.startId);
2498 } else {
2499 s.onTaskRemoved(data.args);
2500 res = Service.START_TASK_REMOVED_COMPLETE;
2501 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002502
2503 QueuedWork.waitToFinish();
2504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002506 ActivityManagerNative.getDefault().serviceDoneExecuting(
2507 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 } catch (RemoteException e) {
2509 // nothing to do.
2510 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002511 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002512 } catch (Exception e) {
2513 if (!mInstrumentation.onException(s, e)) {
2514 throw new RuntimeException(
2515 "Unable to start service " + s
2516 + " with " + data.args + ": " + e.toString(), e);
2517 }
2518 }
2519 }
2520 }
2521
Romain Guy65b345f2011-07-27 18:51:50 -07002522 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002523 Service s = mServices.remove(token);
2524 if (s != null) {
2525 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002526 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 s.onDestroy();
2528 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002529 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002530 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002531 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002533
2534 QueuedWork.waitToFinish();
2535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002537 ActivityManagerNative.getDefault().serviceDoneExecuting(
2538 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 } catch (RemoteException e) {
2540 // nothing to do.
2541 }
2542 } catch (Exception e) {
2543 if (!mInstrumentation.onException(s, e)) {
2544 throw new RuntimeException(
2545 "Unable to stop service " + s
2546 + ": " + e.toString(), e);
2547 }
2548 }
2549 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002550 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 }
2552
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002553 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002555 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002556 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 + " finished=" + r.activity.mFinished);
2558 if (r != null && !r.activity.mFinished) {
2559 if (clearHide) {
2560 r.hideForNow = false;
2561 r.activity.mStartedActivity = false;
2562 }
2563 try {
2564 if (r.pendingIntents != null) {
2565 deliverNewIntents(r, r.pendingIntents);
2566 r.pendingIntents = null;
2567 }
2568 if (r.pendingResults != null) {
2569 deliverResults(r, r.pendingResults);
2570 r.pendingResults = null;
2571 }
2572 r.activity.performResume();
2573
Bob Leee5408332009-09-04 18:31:17 -07002574 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 r.paused = false;
2578 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 r.state = null;
2580 } catch (Exception e) {
2581 if (!mInstrumentation.onException(r.activity, e)) {
2582 throw new RuntimeException(
2583 "Unable to resume activity "
2584 + r.intent.getComponent().toShortString()
2585 + ": " + e.toString(), e);
2586 }
2587 }
2588 }
2589 return r;
2590 }
2591
Romain Guya998dff2012-03-23 18:58:36 -07002592 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002593 if (r.mPendingRemoveWindow != null) {
2594 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2595 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2596 if (wtoken != null) {
2597 WindowManagerImpl.getDefault().closeAll(wtoken,
2598 r.activity.getClass().getName(), "Activity");
2599 }
2600 }
2601 r.mPendingRemoveWindow = null;
2602 r.mPendingRemoveWindowManager = null;
2603 }
2604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2606 // If we are getting ready to gc after going to the background, well
2607 // we are back active so skip it.
2608 unscheduleGcIdler();
2609
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002610 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611
2612 if (r != null) {
2613 final Activity a = r.activity;
2614
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002615 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 TAG, "Resume " + r + " started activity: " +
2617 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2618 + ", finished: " + a.mFinished);
2619
2620 final int forwardBit = isForward ?
2621 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 // If the window hasn't yet been added to the window manager,
2624 // and this guy didn't finish itself or start another activity,
2625 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002626 boolean willBeVisible = !a.mStartedActivity;
2627 if (!willBeVisible) {
2628 try {
2629 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2630 a.getActivityToken());
2631 } catch (RemoteException e) {
2632 }
2633 }
2634 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635 r.window = r.activity.getWindow();
2636 View decor = r.window.getDecorView();
2637 decor.setVisibility(View.INVISIBLE);
2638 ViewManager wm = a.getWindowManager();
2639 WindowManager.LayoutParams l = r.window.getAttributes();
2640 a.mDecor = decor;
2641 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2642 l.softInputMode |= forwardBit;
2643 if (a.mVisibleFromClient) {
2644 a.mWindowAdded = true;
2645 wm.addView(decor, l);
2646 }
2647
2648 // If the window has already been added, but during resume
2649 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002650 // window visible.
2651 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002652 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 TAG, "Launch " + r + " mStartedActivity set");
2654 r.hideForNow = true;
2655 }
2656
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002657 // Get rid of anything left hanging around.
2658 cleanUpPendingRemoveWindows(r);
2659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660 // The window is now visible if it has been added, we are not
2661 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002662 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002663 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002665 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002666 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002668 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 r.newConfig = null;
2670 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002671 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 + isForward);
2673 WindowManager.LayoutParams l = r.window.getAttributes();
2674 if ((l.softInputMode
2675 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2676 != forwardBit) {
2677 l.softInputMode = (l.softInputMode
2678 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2679 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002680 if (r.activity.mVisibleFromClient) {
2681 ViewManager wm = a.getWindowManager();
2682 View decor = r.window.getDecorView();
2683 wm.updateViewLayout(decor, l);
2684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 }
2686 r.activity.mVisibleFromServer = true;
2687 mNumVisibleActivities++;
2688 if (r.activity.mVisibleFromClient) {
2689 r.activity.makeVisible();
2690 }
2691 }
2692
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002693 if (!r.onlyLocalRequest) {
2694 r.nextIdle = mNewActivities;
2695 mNewActivities = r;
2696 if (localLOGV) Slog.v(
2697 TAG, "Scheduling idle handler for " + r);
2698 Looper.myQueue().addIdleHandler(new Idler());
2699 }
2700 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
2702 } else {
2703 // If an exception was thrown when trying to resume, then
2704 // just end this activity.
2705 try {
2706 ActivityManagerNative.getDefault()
2707 .finishActivity(token, Activity.RESULT_CANCELED, null);
2708 } catch (RemoteException ex) {
2709 }
2710 }
2711 }
2712
2713 private int mThumbnailWidth = -1;
2714 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002715 private Bitmap mAvailThumbnailBitmap = null;
2716 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717
Romain Guy65b345f2011-07-27 18:51:50 -07002718 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002719 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002721 if (thumbnail == null) {
2722 int w = mThumbnailWidth;
2723 int h;
2724 if (w < 0) {
2725 Resources res = r.activity.getResources();
2726 mThumbnailHeight = h =
2727 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002729 mThumbnailWidth = w =
2730 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2731 } else {
2732 h = mThumbnailHeight;
2733 }
2734
2735 // On platforms where we don't want thumbnails, set dims to (0,0)
2736 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002737 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2738 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002739 thumbnail.eraseColor(0);
2740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 }
2742
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002743 if (thumbnail != null) {
2744 Canvas cv = mThumbnailCanvas;
2745 if (cv == null) {
2746 mThumbnailCanvas = cv = new Canvas();
2747 }
2748
2749 cv.setBitmap(thumbnail);
2750 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2751 mAvailThumbnailBitmap = thumbnail;
2752 thumbnail = null;
2753 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002754 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 } catch (Exception e) {
2758 if (!mInstrumentation.onException(r.activity, e)) {
2759 throw new RuntimeException(
2760 "Unable to create thumbnail of "
2761 + r.intent.getComponent().toShortString()
2762 + ": " + e.toString(), e);
2763 }
2764 thumbnail = null;
2765 }
2766
2767 return thumbnail;
2768 }
2769
Romain Guy65b345f2011-07-27 18:51:50 -07002770 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002771 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002772 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002774 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 if (userLeaving) {
2776 performUserLeavingActivity(r);
2777 }
Bob Leee5408332009-09-04 18:31:17 -07002778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002780 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002782 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002783 if (r.isPreHoneycomb()) {
2784 QueuedWork.waitToFinish();
2785 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 // Tell the activity manager we have paused.
2788 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002789 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 } catch (RemoteException ex) {
2791 }
2792 }
2793 }
2794
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002795 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 mInstrumentation.callActivityOnUserLeaving(r.activity);
2797 }
2798
2799 final Bundle performPauseActivity(IBinder token, boolean finished,
2800 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002801 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002802 return r != null ? performPauseActivity(r, finished, saveState) : null;
2803 }
2804
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002805 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 boolean saveState) {
2807 if (r.paused) {
2808 if (r.activity.mFinished) {
2809 // If we are finishing, we won't call onResume() in certain cases.
2810 // So here we likewise don't want to call onPause() if the activity
2811 // isn't resumed.
2812 return null;
2813 }
2814 RuntimeException e = new RuntimeException(
2815 "Performing pause of activity that is not resumed: "
2816 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002817 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002818 }
2819 Bundle state = null;
2820 if (finished) {
2821 r.activity.mFinished = true;
2822 }
2823 try {
2824 // Next have the activity save its current state and managed dialogs...
2825 if (!r.activity.mFinished && saveState) {
2826 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002827 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2829 r.state = state;
2830 }
2831 // Now we are idle.
2832 r.activity.mCalled = false;
2833 mInstrumentation.callActivityOnPause(r.activity);
2834 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2835 if (!r.activity.mCalled) {
2836 throw new SuperNotCalledException(
2837 "Activity " + r.intent.getComponent().toShortString() +
2838 " did not call through to super.onPause()");
2839 }
2840
2841 } catch (SuperNotCalledException e) {
2842 throw e;
2843
2844 } catch (Exception e) {
2845 if (!mInstrumentation.onException(r.activity, e)) {
2846 throw new RuntimeException(
2847 "Unable to pause activity "
2848 + r.intent.getComponent().toShortString()
2849 + ": " + e.toString(), e);
2850 }
2851 }
2852 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002853
2854 // Notify any outstanding on paused listeners
2855 ArrayList<OnActivityPausedListener> listeners;
2856 synchronized (mOnPauseListeners) {
2857 listeners = mOnPauseListeners.remove(r.activity);
2858 }
2859 int size = (listeners != null ? listeners.size() : 0);
2860 for (int i = 0; i < size; i++) {
2861 listeners.get(i).onPaused(r.activity);
2862 }
2863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 return state;
2865 }
2866
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002867 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002868 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002869 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 }
2871
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002872 private static class StopInfo implements Runnable {
2873 ActivityClientRecord activity;
2874 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 Bitmap thumbnail;
2876 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002877
2878 @Override public void run() {
2879 // Tell activity manager we have been stopped.
2880 try {
2881 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2882 ActivityManagerNative.getDefault().activityStopped(
2883 activity.token, state, thumbnail, description);
2884 } catch (RemoteException ex) {
2885 }
2886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 }
2888
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002889 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002890 public final IActivityManager.ContentProviderHolder holder;
2891 public final ProviderClientRecord client;
2892 public int stableCount;
2893 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002894
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002895 // When this is set, the stable and unstable ref counts are 0 and
2896 // we have a pending operation scheduled to remove the ref count
2897 // from the activity manager. On the activity manager we are still
2898 // holding an unstable ref, though it is not reflected in the counts
2899 // here.
2900 public boolean removePending;
2901
2902 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2903 ProviderClientRecord inClient, int sCount, int uCount) {
2904 holder = inHolder;
2905 client = inClient;
2906 stableCount = sCount;
2907 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 }
2909 }
2910
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002911 /**
2912 * Core implementation of stopping an activity. Note this is a little
2913 * tricky because the server's meaning of stop is slightly different
2914 * than our client -- for the server, stop means to save state and give
2915 * it the result when it is done, but the window may still be visible.
2916 * For the client, we want to call onStop()/onStart() to indicate when
2917 * the activity's UI visibillity changes.
2918 */
Romain Guy65b345f2011-07-27 18:51:50 -07002919 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002920 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002921 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002922 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002923 if (r != null) {
2924 if (!keepShown && r.stopped) {
2925 if (r.activity.mFinished) {
2926 // If we are finishing, we won't call onResume() in certain
2927 // cases. So here we likewise don't want to call onStop()
2928 // if the activity isn't resumed.
2929 return;
2930 }
2931 RuntimeException e = new RuntimeException(
2932 "Performing stop of activity that is not resumed: "
2933 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002934 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 }
2936
2937 if (info != null) {
2938 try {
2939 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002940 // For now, don't create the thumbnail here; we are
2941 // doing that by doing a screen snapshot.
2942 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 info.description = r.activity.onCreateDescription();
2944 } catch (Exception e) {
2945 if (!mInstrumentation.onException(r.activity, e)) {
2946 throw new RuntimeException(
2947 "Unable to save state of activity "
2948 + r.intent.getComponent().toShortString()
2949 + ": " + e.toString(), e);
2950 }
2951 }
2952 }
2953
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002954 // Next have the activity save its current state and managed dialogs...
2955 if (!r.activity.mFinished && saveState) {
2956 if (r.state == null) {
2957 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002958 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002959 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2960 r.state = state;
2961 } else {
2962 state = r.state;
2963 }
2964 }
2965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 if (!keepShown) {
2967 try {
2968 // Now we are idle.
2969 r.activity.performStop();
2970 } catch (Exception e) {
2971 if (!mInstrumentation.onException(r.activity, e)) {
2972 throw new RuntimeException(
2973 "Unable to stop activity "
2974 + r.intent.getComponent().toShortString()
2975 + ": " + e.toString(), e);
2976 }
2977 }
2978 r.stopped = true;
2979 }
2980
2981 r.paused = true;
2982 }
2983 }
2984
Romain Guy65b345f2011-07-27 18:51:50 -07002985 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 View v = r.activity.mDecor;
2987 if (v != null) {
2988 if (show) {
2989 if (!r.activity.mVisibleFromServer) {
2990 r.activity.mVisibleFromServer = true;
2991 mNumVisibleActivities++;
2992 if (r.activity.mVisibleFromClient) {
2993 r.activity.makeVisible();
2994 }
2995 }
2996 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002997 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002998 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003000 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 r.newConfig = null;
3002 }
3003 } else {
3004 if (r.activity.mVisibleFromServer) {
3005 r.activity.mVisibleFromServer = false;
3006 mNumVisibleActivities--;
3007 v.setVisibility(View.INVISIBLE);
3008 }
3009 }
3010 }
3011 }
3012
Romain Guy65b345f2011-07-27 18:51:50 -07003013 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003014 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 r.activity.mConfigChangeFlags |= configChanges;
3016
3017 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003018 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003019
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003020 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 TAG, "Finishing stop of " + r + ": show=" + show
3022 + " win=" + r.window);
3023
3024 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003025
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003026 // Make sure any pending writes are now committed.
3027 if (!r.isPreHoneycomb()) {
3028 QueuedWork.waitToFinish();
3029 }
3030
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003031 // Schedule the call to tell the activity manager we have
3032 // stopped. We don't do this immediately, because we want to
3033 // have a chance for any other pending work (in particular memory
3034 // trim requests) to complete before you tell the activity
3035 // manager to proceed and allow us to go fully into the background.
3036 info.activity = r;
3037 info.state = r.state;
3038 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 }
3040
3041 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003042 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003043 if (r.stopped) {
3044 r.activity.performRestart();
3045 r.stopped = false;
3046 }
3047 }
3048
Romain Guy65b345f2011-07-27 18:51:50 -07003049 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003050 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003051
3052 if (r == null) {
3053 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3054 return;
3055 }
3056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003058 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 } else if (show && r.stopped) {
3060 // If we are getting ready to gc after going to the background, well
3061 // we are back active so skip it.
3062 unscheduleGcIdler();
3063
3064 r.activity.performRestart();
3065 r.stopped = false;
3066 }
3067 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003068 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 TAG, "Handle window " + r + " visibility: " + show);
3070 updateVisibility(r, show);
3071 }
3072 }
3073
Romain Guy65b345f2011-07-27 18:51:50 -07003074 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003075 ActivityClientRecord r = mActivities.get(token);
3076
3077 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003078 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003079 return;
3080 }
3081
3082 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003083 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003084 try {
3085 // Now we are idle.
3086 r.activity.performStop();
3087 } catch (Exception e) {
3088 if (!mInstrumentation.onException(r.activity, e)) {
3089 throw new RuntimeException(
3090 "Unable to stop activity "
3091 + r.intent.getComponent().toShortString()
3092 + ": " + e.toString(), e);
3093 }
3094 }
3095 r.stopped = true;
3096 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003097
3098 // Make sure any pending writes are now committed.
3099 if (!r.isPreHoneycomb()) {
3100 QueuedWork.waitToFinish();
3101 }
3102
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003103 // Tell activity manager we slept.
3104 try {
3105 ActivityManagerNative.getDefault().activitySlept(r.token);
3106 } catch (RemoteException ex) {
3107 }
3108 } else {
3109 if (r.stopped && r.activity.mVisibleFromServer) {
3110 r.activity.performRestart();
3111 r.stopped = false;
3112 }
3113 }
3114 }
3115
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003116 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003117 synchronized (mPackages) {
3118 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003119 }
3120 }
3121
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003122 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3123 LoadedApk apk = peekPackageInfo(data.pkg, false);
3124 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003125 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003126 }
3127 apk = peekPackageInfo(data.pkg, true);
3128 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003129 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003130 }
3131 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003132 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003133 }
3134
Romain Guy65b345f2011-07-27 18:51:50 -07003135 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 final int N = results.size();
3137 for (int i=0; i<N; i++) {
3138 ResultInfo ri = results.get(i);
3139 try {
3140 if (ri.mData != null) {
3141 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3142 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003143 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003144 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 r.activity.dispatchActivityResult(ri.mResultWho,
3146 ri.mRequestCode, ri.mResultCode, ri.mData);
3147 } catch (Exception e) {
3148 if (!mInstrumentation.onException(r.activity, e)) {
3149 throw new RuntimeException(
3150 "Failure delivering result " + ri + " to activity "
3151 + r.intent.getComponent().toShortString()
3152 + ": " + e.toString(), e);
3153 }
3154 }
3155 }
3156 }
3157
Romain Guy65b345f2011-07-27 18:51:50 -07003158 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003159 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003160 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 if (r != null) {
3162 final boolean resumed = !r.paused;
3163 if (!r.activity.mFinished && r.activity.mDecor != null
3164 && r.hideForNow && resumed) {
3165 // We had hidden the activity because it started another
3166 // one... we have gotten a result back and we are not
3167 // paused, so make sure our window is visible.
3168 updateVisibility(r, true);
3169 }
3170 if (resumed) {
3171 try {
3172 // Now we are idle.
3173 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003174 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 mInstrumentation.callActivityOnPause(r.activity);
3176 if (!r.activity.mCalled) {
3177 throw new SuperNotCalledException(
3178 "Activity " + r.intent.getComponent().toShortString()
3179 + " did not call through to super.onPause()");
3180 }
3181 } catch (SuperNotCalledException e) {
3182 throw e;
3183 } catch (Exception e) {
3184 if (!mInstrumentation.onException(r.activity, e)) {
3185 throw new RuntimeException(
3186 "Unable to pause activity "
3187 + r.intent.getComponent().toShortString()
3188 + ": " + e.toString(), e);
3189 }
3190 }
3191 }
3192 deliverResults(r, res.results);
3193 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003194 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003195 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197 }
3198 }
3199
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003200 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 return performDestroyActivity(token, finishing, 0, false);
3202 }
3203
Romain Guy65b345f2011-07-27 18:51:50 -07003204 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003206 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003207 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003208 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003210 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 r.activity.mConfigChangeFlags |= configChanges;
3212 if (finishing) {
3213 r.activity.mFinished = true;
3214 }
3215 if (!r.paused) {
3216 try {
3217 r.activity.mCalled = false;
3218 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003219 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 r.activity.getComponentName().getClassName());
3221 if (!r.activity.mCalled) {
3222 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003223 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 + " did not call through to super.onPause()");
3225 }
3226 } catch (SuperNotCalledException e) {
3227 throw e;
3228 } catch (Exception e) {
3229 if (!mInstrumentation.onException(r.activity, e)) {
3230 throw new RuntimeException(
3231 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003232 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 + ": " + e.toString(), e);
3234 }
3235 }
3236 r.paused = true;
3237 }
3238 if (!r.stopped) {
3239 try {
3240 r.activity.performStop();
3241 } catch (SuperNotCalledException e) {
3242 throw e;
3243 } catch (Exception e) {
3244 if (!mInstrumentation.onException(r.activity, e)) {
3245 throw new RuntimeException(
3246 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003247 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 + ": " + e.toString(), e);
3249 }
3250 }
3251 r.stopped = true;
3252 }
3253 if (getNonConfigInstance) {
3254 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003255 r.lastNonConfigurationInstances
3256 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 } catch (Exception e) {
3258 if (!mInstrumentation.onException(r.activity, e)) {
3259 throw new RuntimeException(
3260 "Unable to retain activity "
3261 + r.intent.getComponent().toShortString()
3262 + ": " + e.toString(), e);
3263 }
3264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266 try {
3267 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003268 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 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.onDestroy()");
3273 }
3274 if (r.window != null) {
3275 r.window.closeAllPanels();
3276 }
3277 } catch (SuperNotCalledException e) {
3278 throw e;
3279 } catch (Exception e) {
3280 if (!mInstrumentation.onException(r.activity, e)) {
3281 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003282 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3283 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 }
3285 }
3286 }
3287 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003288 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 return r;
3290 }
3291
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003292 private static String safeToComponentShortString(Intent intent) {
3293 ComponentName component = intent.getComponent();
3294 return component == null ? "[Unknown]" : component.toShortString();
3295 }
3296
Romain Guy65b345f2011-07-27 18:51:50 -07003297 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003298 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003299 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 configChanges, getNonConfigInstance);
3301 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003302 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 WindowManager wm = r.activity.getWindowManager();
3304 View v = r.activity.mDecor;
3305 if (v != null) {
3306 if (r.activity.mVisibleFromServer) {
3307 mNumVisibleActivities--;
3308 }
3309 IBinder wtoken = v.getWindowToken();
3310 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003311 if (r.onlyLocalRequest) {
3312 // Hold off on removing this until the new activity's
3313 // window is being added.
3314 r.mPendingRemoveWindow = v;
3315 r.mPendingRemoveWindowManager = wm;
3316 } else {
3317 wm.removeViewImmediate(v);
3318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003320 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 WindowManagerImpl.getDefault().closeAll(wtoken,
3322 r.activity.getClass().getName(), "Activity");
3323 }
3324 r.activity.mDecor = null;
3325 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003326 if (r.mPendingRemoveWindow == null) {
3327 // If we are delaying the removal of the activity window, then
3328 // we can't clean up all windows here. Note that we can't do
3329 // so later either, which means any windows that aren't closed
3330 // by the app will leak. Well we try to warning them a lot
3331 // about leaking windows, because that is a bug, so if they are
3332 // using this recreate facility then they get to live with leaks.
3333 WindowManagerImpl.getDefault().closeAll(token,
3334 r.activity.getClass().getName(), "Activity");
3335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336
3337 // Mocked out contexts won't be participating in the normal
3338 // process lifecycle, but if we're running with a proper
3339 // ApplicationContext we need to have it tear down things
3340 // cleanly.
3341 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003342 if (c instanceof ContextImpl) {
3343 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 r.activity.getClass().getName(), "Activity");
3345 }
3346 }
3347 if (finishing) {
3348 try {
3349 ActivityManagerNative.getDefault().activityDestroyed(token);
3350 } catch (RemoteException ex) {
3351 // If the system process has died, it's game over for everyone.
3352 }
3353 }
3354 }
3355
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003356 public final void requestRelaunchActivity(IBinder token,
3357 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3358 int configChanges, boolean notResumed, Configuration config,
3359 boolean fromServer) {
3360 ActivityClientRecord target = null;
3361
3362 synchronized (mPackages) {
3363 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3364 ActivityClientRecord r = mRelaunchingActivities.get(i);
3365 if (r.token == token) {
3366 target = r;
3367 if (pendingResults != null) {
3368 if (r.pendingResults != null) {
3369 r.pendingResults.addAll(pendingResults);
3370 } else {
3371 r.pendingResults = pendingResults;
3372 }
3373 }
3374 if (pendingNewIntents != null) {
3375 if (r.pendingIntents != null) {
3376 r.pendingIntents.addAll(pendingNewIntents);
3377 } else {
3378 r.pendingIntents = pendingNewIntents;
3379 }
3380 }
3381 break;
3382 }
3383 }
3384
3385 if (target == null) {
3386 target = new ActivityClientRecord();
3387 target.token = token;
3388 target.pendingResults = pendingResults;
3389 target.pendingIntents = pendingNewIntents;
3390 if (!fromServer) {
3391 ActivityClientRecord existing = mActivities.get(token);
3392 if (existing != null) {
3393 target.startsNotResumed = existing.paused;
3394 }
3395 target.onlyLocalRequest = true;
3396 }
3397 mRelaunchingActivities.add(target);
3398 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3399 }
3400
3401 if (fromServer) {
3402 target.startsNotResumed = notResumed;
3403 target.onlyLocalRequest = false;
3404 }
3405 if (config != null) {
3406 target.createdConfig = config;
3407 }
3408 target.pendingConfigChanges |= configChanges;
3409 }
3410 }
3411
Romain Guy65b345f2011-07-27 18:51:50 -07003412 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 // If we are getting ready to gc after going to the background, well
3414 // we are back active so skip it.
3415 unscheduleGcIdler();
3416
3417 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003418 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003420 // First: make sure we have the most recent configuration and most
3421 // recent version of the activity, or skip it if some previous call
3422 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003423 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 int N = mRelaunchingActivities.size();
3425 IBinder token = tmp.token;
3426 tmp = null;
3427 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003428 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 if (r.token == token) {
3430 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003431 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 mRelaunchingActivities.remove(i);
3433 i--;
3434 N--;
3435 }
3436 }
Bob Leee5408332009-09-04 18:31:17 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003439 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 return;
3441 }
Bob Leee5408332009-09-04 18:31:17 -07003442
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003443 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3444 + tmp.token + " with configChanges=0x"
3445 + Integer.toHexString(configChanges));
3446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 if (mPendingConfiguration != null) {
3448 changedConfig = mPendingConfiguration;
3449 mPendingConfiguration = null;
3450 }
3451 }
Bob Leee5408332009-09-04 18:31:17 -07003452
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003453 if (tmp.createdConfig != null) {
3454 // If the activity manager is passing us its current config,
3455 // assume that is really what we want regardless of what we
3456 // may have pending.
3457 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003458 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3459 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3460 if (changedConfig == null
3461 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3462 changedConfig = tmp.createdConfig;
3463 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003464 }
3465 }
3466
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003467 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003468 + tmp.token + ": changedConfig=" + changedConfig);
3469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 // If there was a pending configuration change, execute it first.
3471 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003472 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003473 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003474 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 }
Bob Leee5408332009-09-04 18:31:17 -07003476
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003477 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003478 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 if (r == null) {
3480 return;
3481 }
Bob Leee5408332009-09-04 18:31:17 -07003482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003484 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003485 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003486
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003487 r.activity.mChangingConfigurations = true;
3488
Dianne Hackborne2b04802010-12-09 09:24:55 -08003489 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003491 performPauseActivity(r.token, false, r.isPreHoneycomb());
3492 }
3493 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3494 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003495 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003496 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 }
Bob Leee5408332009-09-04 18:31:17 -07003498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003501 r.activity = null;
3502 r.window = null;
3503 r.hideForNow = false;
3504 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003505 // Merge any pending results and pending intents; don't just replace them
3506 if (tmp.pendingResults != null) {
3507 if (r.pendingResults == null) {
3508 r.pendingResults = tmp.pendingResults;
3509 } else {
3510 r.pendingResults.addAll(tmp.pendingResults);
3511 }
3512 }
3513 if (tmp.pendingIntents != null) {
3514 if (r.pendingIntents == null) {
3515 r.pendingIntents = tmp.pendingIntents;
3516 } else {
3517 r.pendingIntents.addAll(tmp.pendingIntents);
3518 }
3519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003521
Christopher Tateb70f3df2009-04-07 16:07:59 -07003522 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524
Romain Guy65b345f2011-07-27 18:51:50 -07003525 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003526 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 Bitmap thumbnail = createThumbnailBitmap(r);
3528 CharSequence description = null;
3529 try {
3530 description = r.activity.onCreateDescription();
3531 } catch (Exception e) {
3532 if (!mInstrumentation.onException(r.activity, e)) {
3533 throw new RuntimeException(
3534 "Unable to create description of activity "
3535 + r.intent.getComponent().toShortString()
3536 + ": " + e.toString(), e);
3537 }
3538 }
3539 //System.out.println("Reporting top thumbnail " + thumbnail);
3540 try {
3541 ActivityManagerNative.getDefault().reportThumbnail(
3542 token, thumbnail, description);
3543 } catch (RemoteException ex) {
3544 }
3545 }
3546
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003547 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003549 ArrayList<ComponentCallbacks2> callbacks
3550 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003553 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 Activity a = ar.activity;
3555 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003556 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3557 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003558 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 // If the activity is currently resumed, its configuration
3560 // needs to change right now.
3561 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003562 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 // Otherwise, we will tell it about the change
3564 // the next time it is resumed or shown. Note that
3565 // the activity manager may, before then, decide the
3566 // activity needs to be destroyed to handle its new
3567 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003568 if (DEBUG_CONFIGURATION) {
3569 Slog.v(TAG, "Setting activity "
3570 + ar.activityInfo.name + " newConfig=" + thisConfig);
3571 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003572 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 }
3574 }
3575 }
3576 }
3577 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003578 for (Service service : mServices.values()) {
3579 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 }
3581 }
3582 synchronized (mProviderMap) {
3583 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003584 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3585 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 }
3587 }
3588 }
3589 final int N = mAllApplications.size();
3590 for (int i=0; i<N; i++) {
3591 callbacks.add(mAllApplications.get(i));
3592 }
Bob Leee5408332009-09-04 18:31:17 -07003593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 return callbacks;
3595 }
Bob Leee5408332009-09-04 18:31:17 -07003596
Romain Guya998dff2012-03-23 18:58:36 -07003597 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003599 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 // we check the runtime type and act accordingly.
3601 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3602 if (activity != null) {
3603 activity.mCalled = false;
3604 }
Bob Leee5408332009-09-04 18:31:17 -07003605
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 boolean shouldChangeConfig = false;
3607 if ((activity == null) || (activity.mCurrentConfig == null)) {
3608 shouldChangeConfig = true;
3609 } else {
Bob Leee5408332009-09-04 18:31:17 -07003610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 // If the new config is the same as the config this Activity
3612 // is already running with then don't bother calling
3613 // onConfigurationChanged
3614 int diff = activity.mCurrentConfig.diff(config);
3615 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 // If this activity doesn't handle any of the config changes
3617 // then don't bother calling onConfigurationChanged as we're
3618 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003619 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 shouldChangeConfig = true;
3621 }
3622 }
3623 }
Bob Leee5408332009-09-04 18:31:17 -07003624
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003625 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003626 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 if (shouldChangeConfig) {
3628 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 if (activity != null) {
3631 if (!activity.mCalled) {
3632 throw new SuperNotCalledException(
3633 "Activity " + activity.getLocalClassName() +
3634 " did not call through to super.onConfigurationChanged()");
3635 }
3636 activity.mConfigChangeFlags = 0;
3637 activity.mCurrentConfig = new Configuration(config);
3638 }
3639 }
3640 }
3641
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003642 public final void applyConfigurationToResources(Configuration config) {
3643 synchronized (mPackages) {
3644 applyConfigurationToResourcesLocked(config, null);
3645 }
3646 }
3647
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003648 final boolean applyConfigurationToResourcesLocked(Configuration config,
3649 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003650 if (mResConfiguration == null) {
3651 mResConfiguration = new Configuration();
3652 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003653 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003654 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003655 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003656 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003657 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003658 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003659 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003660
3661 if (compat != null && (mResCompatibilityInfo == null ||
3662 !mResCompatibilityInfo.equals(compat))) {
3663 mResCompatibilityInfo = compat;
3664 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3665 | ActivityInfo.CONFIG_SCREEN_SIZE
3666 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3667 }
Bob Leee5408332009-09-04 18:31:17 -07003668
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003669 // set it for java, this also affects newly created Resources
3670 if (config.locale != null) {
3671 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003672 }
Bob Leee5408332009-09-04 18:31:17 -07003673
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003674 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003675
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003676 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003677 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003678
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003679 Iterator<WeakReference<Resources>> it =
3680 mActiveResources.values().iterator();
3681 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3682 // mActiveResources.entrySet().iterator();
3683 while (it.hasNext()) {
3684 WeakReference<Resources> v = it.next();
3685 Resources r = v.get();
3686 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003687 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003688 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003689 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003690 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003691 // + " " + r + ": " + r.getConfiguration());
3692 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003693 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003694 it.remove();
3695 }
3696 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003697
3698 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003699 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003700
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003701 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003702 Configuration config = mConfiguration;
3703 if (mCompatConfiguration == null) {
3704 mCompatConfiguration = new Configuration();
3705 }
3706 mCompatConfiguration.setTo(mConfiguration);
3707 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003708 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003709 config = mCompatConfiguration;
3710 }
3711 return config;
3712 }
3713
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003714 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003715
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003716 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003717 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003718
3719 synchronized (mPackages) {
3720 if (mPendingConfiguration != null) {
3721 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3722 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003723 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003724 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003725 }
3726 mPendingConfiguration = null;
3727 }
3728
3729 if (config == null) {
3730 return;
3731 }
3732
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003733 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003734 + config);
3735
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003736 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 if (mConfiguration == null) {
3739 mConfiguration = new Configuration();
3740 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003741 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003742 return;
3743 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003744 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003746 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 callbacks = collectComponentCallbacksLocked(false, config);
3748 }
Romain Guy65b345f2011-07-27 18:51:50 -07003749
3750 // Cleanup hardware accelerated stuff
3751 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003752
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003753 freeTextLayoutCachesIfNeeded(configDiff);
3754
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003755 if (callbacks != null) {
3756 final int N = callbacks.size();
3757 for (int i=0; i<N; i++) {
3758 performConfigurationChanged(callbacks.get(i), config);
3759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761 }
3762
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003763 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3764 if (configDiff != 0) {
3765 // Ask text layout engine to free its caches if there is a locale change
3766 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3767 if (hasLocaleConfigChange) {
3768 Canvas.freeTextLayoutCaches();
3769 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3770 }
3771 }
3772 }
3773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003775 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 if (r == null || r.activity == null) {
3777 return;
3778 }
Bob Leee5408332009-09-04 18:31:17 -07003779
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003780 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003781 + r.activityInfo.name);
3782
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003783 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003784
3785 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 }
3787
Romain Guy7eabe552011-07-21 14:56:34 -07003788 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003789 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003790 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003791 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003792 default:
3793 mProfiler.setProfiler(pcd.path, pcd.fd);
3794 mProfiler.autoStopProfiler = false;
3795 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003796 break;
3797 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003798 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003799 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003800 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003801 } finally {
3802 try {
3803 pcd.fd.close();
3804 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003805 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003806 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003807 }
3808 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003809 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003810 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003811 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003812 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003813 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003814 }
3815 }
Bob Leee5408332009-09-04 18:31:17 -07003816
Romain Guya998dff2012-03-23 18:58:36 -07003817 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003818 if (managed) {
3819 try {
3820 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3821 } catch (IOException e) {
3822 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3823 + " -- can the process access this path?");
3824 } finally {
3825 try {
3826 dhd.fd.close();
3827 } catch (IOException e) {
3828 Slog.w(TAG, "Failure closing profile fd", e);
3829 }
3830 }
3831 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003832 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003833 }
3834 }
3835
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003836 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3837 boolean hasPkgInfo = false;
3838 if (packages != null) {
3839 for (int i=packages.length-1; i>=0; i--) {
3840 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3841 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003842 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003843 ref = mPackages.get(packages[i]);
3844 if (ref != null && ref.get() != null) {
3845 hasPkgInfo = true;
3846 } else {
3847 ref = mResourcePackages.get(packages[i]);
3848 if (ref != null && ref.get() != null) {
3849 hasPkgInfo = true;
3850 }
3851 }
3852 }
3853 mPackages.remove(packages[i]);
3854 mResourcePackages.remove(packages[i]);
3855 }
3856 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003857 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003858 hasPkgInfo);
3859 }
3860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003862 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003864 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 callbacks = collectComponentCallbacksLocked(true, null);
3866 }
Bob Leee5408332009-09-04 18:31:17 -07003867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 final int N = callbacks.size();
3869 for (int i=0; i<N; i++) {
3870 callbacks.get(i).onLowMemory();
3871 }
3872
Chris Tatece229052009-03-25 16:44:52 -07003873 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3874 if (Process.myUid() != Process.SYSTEM_UID) {
3875 int sqliteReleased = SQLiteDatabase.releaseMemory();
3876 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3877 }
Bob Leee5408332009-09-04 18:31:17 -07003878
Mike Reedcaf0df12009-04-27 14:32:05 -04003879 // Ask graphics to free up as much as possible (font/image caches)
3880 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003882 // Ask text layout engine to free also as much as possible
3883 Canvas.freeTextLayoutCaches();
3884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 BinderInternal.forceGc("mem");
3886 }
3887
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003888 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003889 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003890
Romain Guy19f86e82012-04-23 15:19:07 -07003891 final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3892 windowManager.startTrimMemory(level);
3893
3894 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003895 synchronized (mPackages) {
3896 callbacks = collectComponentCallbacksLocked(true, null);
3897 }
3898
3899 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003900 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003901 callbacks.get(i).onTrimMemory(level);
3902 }
Romain Guy19f86e82012-04-23 15:19:07 -07003903
3904 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003905 }
3906
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003907 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003908 if (Process.isIsolated()) {
3909 // Isolated processes aren't going to do UI.
3910 return;
3911 }
Romain Guya9582652011-11-10 14:20:10 -08003912 try {
3913 int uid = Process.myUid();
3914 String[] packages = getPackageManager().getPackagesForUid(uid);
3915
3916 // If there are several packages in this application we won't
3917 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003918 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003919 HardwareRenderer.setupDiskCache(cacheDir);
3920 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003921 }
3922 } catch (RemoteException e) {
3923 // Ignore
3924 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07003925 }
3926
3927 private void updateDefaultDensity() {
3928 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
3929 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
3930 && !mDensityCompatMode) {
3931 Slog.i(TAG, "Switching default density from "
3932 + DisplayMetrics.DENSITY_DEVICE + " to "
3933 + mCurDefaultDisplayDpi);
3934 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
3935 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3936 }
3937 }
3938
Romain Guy65b345f2011-07-27 18:51:50 -07003939 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003940 mBoundApplication = data;
3941 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003942 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003943
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003944 mProfiler = new Profiler();
3945 mProfiler.profileFile = data.initProfileFile;
3946 mProfiler.profileFd = data.initProfileFd;
3947 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003950 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 android.ddm.DdmHandleAppName.setAppName(data.processName);
3952
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003953 if (data.persistent) {
3954 // Persistent processes on low-memory devices do not get to
3955 // use hardware accelerated drawing, since this can add too much
3956 // overhead to the process.
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003957 final Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003958 if (!ActivityManager.isHighEndGfx(display)) {
3959 HardwareRenderer.disable(false);
3960 }
3961 }
Romain Guya9582652011-11-10 14:20:10 -08003962
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003963 if (mProfiler.profileFd != null) {
3964 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003965 }
3966
Joe Onoratod630f102011-03-17 18:42:26 -07003967 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3968 // implementation to use the pool executor. Normally, we use the
3969 // serialized executor as the default. This has to happen in the
3970 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07003971 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07003972 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3973 }
3974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 /*
3976 * Before spawning a new process, reset the time zone to be the system time zone.
3977 * This needs to be done because the system time zone could have changed after the
3978 * the spawning of this process. Without doing this this process would have the incorrect
3979 * system time zone.
3980 */
3981 TimeZone.setDefault(null);
3982
3983 /*
3984 * Initialize the default locale in this process for the reasons we set the time zone.
3985 */
3986 Locale.setDefault(data.config.locale);
3987
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003988 /*
3989 * Update the system configuration since its preloaded and might not
3990 * reflect configuration changes. The configuration object passed
3991 * in AppBindData can be safely assumed to be up to date
3992 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003993 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003994 mCurDefaultDisplayDpi = data.config.densityDpi;
3995 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003996
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003997 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998
Dianne Hackborndde331c2012-08-03 14:01:57 -07003999 /**
4000 * Switch this process to density compatibility mode if needed.
4001 */
4002 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4003 == 0) {
4004 mDensityCompatMode = true;
4005 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4006 }
4007 updateDefaultDensity();
4008
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004009 final ContextImpl appContext = new ContextImpl();
4010 appContext.init(data.info, null, this);
4011 final File cacheDir = appContext.getCacheDir();
4012
4013 // Provide a usable directory for temporary files
4014 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4015
4016 setupGraphicsSupport(data.info, cacheDir);
4017
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004018 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004019 * For system applications on userdebug/eng builds, log stack
4020 * traces of disk and network access to dropbox for analysis.
4021 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004022 if ((data.appInfo.flags &
4023 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004024 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4025 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004026 }
4027
4028 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004029 * For apps targetting SDK Honeycomb or later, we don't allow
4030 * network usage on the main event loop / UI thread.
4031 *
4032 * Note to those grepping: this is what ultimately throws
4033 * NetworkOnMainThreadException ...
4034 */
4035 if (data.appInfo.targetSdkVersion > 9) {
4036 StrictMode.enableDeathOnNetwork();
4037 }
4038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4040 // XXX should have option to change the port.
4041 Debug.changeDebugPort(8100);
4042 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004043 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 + " is waiting for the debugger on port 8100...");
4045
4046 IActivityManager mgr = ActivityManagerNative.getDefault();
4047 try {
4048 mgr.showWaitingForDebugger(mAppThread, true);
4049 } catch (RemoteException ex) {
4050 }
4051
4052 Debug.waitForDebugger();
4053
4054 try {
4055 mgr.showWaitingForDebugger(mAppThread, false);
4056 } catch (RemoteException ex) {
4057 }
4058
4059 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004060 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 + " can be debugged on port 8100...");
4062 }
4063 }
4064
Siva Velusamy92a8b222012-03-09 16:24:04 -08004065 // Enable OpenGL tracing if required
4066 if (data.enableOpenGlTrace) {
4067 GLUtils.enableTracing();
4068 }
4069
Robert Greenwalt434203a2010-10-11 16:00:27 -07004070 /**
4071 * Initialize the default http proxy in this process for the reasons we set the time zone.
4072 */
4073 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004074 if (b != null) {
4075 // In pre-boot mode (doing initial launch to collect password), not
4076 // all system is up. This includes the connectivity service, so don't
4077 // crash if we can't get it.
4078 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4079 try {
4080 ProxyProperties proxyProperties = service.getProxy();
4081 Proxy.setHttpProxySystemProperty(proxyProperties);
4082 } catch (RemoteException e) {}
4083 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 InstrumentationInfo ii = null;
4087 try {
4088 ii = appContext.getPackageManager().
4089 getInstrumentationInfo(data.instrumentationName, 0);
4090 } catch (PackageManager.NameNotFoundException e) {
4091 }
4092 if (ii == null) {
4093 throw new RuntimeException(
4094 "Unable to find instrumentation info for: "
4095 + data.instrumentationName);
4096 }
4097
4098 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004099 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 mInstrumentationAppPackage = ii.packageName;
4101 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004102 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103
4104 ApplicationInfo instrApp = new ApplicationInfo();
4105 instrApp.packageName = ii.packageName;
4106 instrApp.sourceDir = ii.sourceDir;
4107 instrApp.publicSourceDir = ii.publicSourceDir;
4108 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004109 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004110 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004112 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113 instrContext.init(pi, null, this);
4114
4115 try {
4116 java.lang.ClassLoader cl = instrContext.getClassLoader();
4117 mInstrumentation = (Instrumentation)
4118 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4119 } catch (Exception e) {
4120 throw new RuntimeException(
4121 "Unable to instantiate instrumentation "
4122 + data.instrumentationName + ": " + e.toString(), e);
4123 }
4124
4125 mInstrumentation.init(this, instrContext, appContext,
4126 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4127
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004128 if (mProfiler.profileFile != null && !ii.handleProfiling
4129 && mProfiler.profileFd == null) {
4130 mProfiler.handlingProfiling = true;
4131 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 file.getParentFile().mkdirs();
4133 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4134 }
4135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136 } else {
4137 mInstrumentation = new Instrumentation();
4138 }
4139
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004140 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004141 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004142 }
4143
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004144 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004145 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004146 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004147 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004149 // If the app is being launched for full backup or restore, bring it up in
4150 // a restricted environment with the base application class.
4151 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4152 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004153
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004154 // don't bring up providers in restricted mode; they may depend on the
4155 // app's custom Application class
4156 if (!data.restrictedBackupMode) {
4157 List<ProviderInfo> providers = data.providers;
4158 if (providers != null) {
4159 installContentProviders(app, providers);
4160 // For process that contains content providers, we want to
4161 // ensure that the JIT is enabled "at some point".
4162 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4163 }
4164 }
4165
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004166 // Do this after providers, since instrumentation tests generally start their
4167 // test thread at this point, and we don't want that racing.
4168 try {
4169 mInstrumentation.onCreate(data.instrumentationArgs);
4170 }
4171 catch (Exception e) {
4172 throw new RuntimeException(
4173 "Exception thrown in onCreate() of "
4174 + data.instrumentationName + ": " + e.toString(), e);
4175 }
4176
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004177 try {
4178 mInstrumentation.callApplicationOnCreate(app);
4179 } catch (Exception e) {
4180 if (!mInstrumentation.onException(app, e)) {
4181 throw new RuntimeException(
4182 "Unable to create application " + app.getClass().getName()
4183 + ": " + e.toString(), e);
4184 }
4185 }
4186 } finally {
4187 StrictMode.setThreadPolicy(savedPolicy);
4188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 }
4190
4191 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4192 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004193 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4194 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 Debug.stopMethodTracing();
4196 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004197 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 // + ", app thr: " + mAppThread);
4199 try {
4200 am.finishInstrumentation(mAppThread, resultCode, results);
4201 } catch (RemoteException ex) {
4202 }
4203 }
4204
Romain Guy65b345f2011-07-27 18:51:50 -07004205 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 Context context, List<ProviderInfo> providers) {
4207 final ArrayList<IActivityManager.ContentProviderHolder> results =
4208 new ArrayList<IActivityManager.ContentProviderHolder>();
4209
Romain Guya998dff2012-03-23 18:58:36 -07004210 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004212 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 buf.append(cpi.authority);
4214 buf.append(": ");
4215 buf.append(cpi.name);
4216 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004217 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4218 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4219 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004220 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 }
4223 }
4224
4225 try {
4226 ActivityManagerNative.getDefault().publishContentProviders(
4227 getApplicationThread(), results);
4228 } catch (RemoteException ex) {
4229 }
4230 }
4231
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004232 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4233 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004234 if (provider != null) {
4235 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 }
4237
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004238 // There is a possible race here. Another thread may try to acquire
4239 // the same provider at the same time. When this happens, we want to ensure
4240 // that the first one wins.
4241 // Note that we cannot hold the lock while acquiring and installing the
4242 // provider since it might take a long time to run and it could also potentially
4243 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 IActivityManager.ContentProviderHolder holder = null;
4245 try {
4246 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004247 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 } catch (RemoteException ex) {
4249 }
4250 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004251 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 return null;
4253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004255 // Install provider will increment the reference count for us, and break
4256 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004257 holder = installProvider(c, holder, holder.info,
4258 true /*noisy*/, holder.noReleaseNeeded, stable);
4259 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 }
4261
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004262 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4263 if (stable) {
4264 prc.stableCount += 1;
4265 if (prc.stableCount == 1) {
4266 // We are acquiring a new stable reference on the provider.
4267 int unstableDelta;
4268 if (prc.removePending) {
4269 // We have a pending remove operation, which is holding the
4270 // last unstable reference. At this point we are converting
4271 // that unstable reference to our new stable reference.
4272 unstableDelta = -1;
4273 // Cancel the removal of the provider.
4274 if (DEBUG_PROVIDER) {
4275 Slog.v(TAG, "incProviderRef: stable "
4276 + "snatched provider from the jaws of death");
4277 }
4278 prc.removePending = false;
4279 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4280 } else {
4281 unstableDelta = 0;
4282 }
4283 try {
4284 if (DEBUG_PROVIDER) {
4285 Slog.v(TAG, "incProviderRef Now stable - "
4286 + prc.holder.info.name + ": unstableDelta="
4287 + unstableDelta);
4288 }
4289 ActivityManagerNative.getDefault().refContentProvider(
4290 prc.holder.connection, 1, unstableDelta);
4291 } catch (RemoteException e) {
4292 //do nothing content provider object is dead any way
4293 }
4294 }
4295 } else {
4296 prc.unstableCount += 1;
4297 if (prc.unstableCount == 1) {
4298 // We are acquiring a new unstable reference on the provider.
4299 if (prc.removePending) {
4300 // Oh look, we actually have a remove pending for the
4301 // provider, which is still holding the last unstable
4302 // reference. We just need to cancel that to take new
4303 // ownership of the reference.
4304 if (DEBUG_PROVIDER) {
4305 Slog.v(TAG, "incProviderRef: unstable "
4306 + "snatched provider from the jaws of death");
4307 }
4308 prc.removePending = false;
4309 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4310 } else {
4311 // First unstable ref, increment our count in the
4312 // activity manager.
4313 try {
4314 if (DEBUG_PROVIDER) {
4315 Slog.v(TAG, "incProviderRef: Now unstable - "
4316 + prc.holder.info.name);
4317 }
4318 ActivityManagerNative.getDefault().refContentProvider(
4319 prc.holder.connection, 0, 1);
4320 } catch (RemoteException e) {
4321 //do nothing content provider object is dead any way
4322 }
4323 }
4324 }
4325 }
4326 }
4327
4328 public final IContentProvider acquireExistingProvider(Context c, String name,
4329 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004330 synchronized (mProviderMap) {
4331 ProviderClientRecord pr = mProviderMap.get(name);
4332 if (pr == null) {
4333 return null;
4334 }
4335
4336 IContentProvider provider = pr.mProvider;
4337 IBinder jBinder = provider.asBinder();
4338
4339 // Only increment the ref count if we have one. If we don't then the
4340 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004341 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004342 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004343 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004344 }
4345 return provider;
4346 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004347 }
4348
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004349 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4350 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004351 return false;
4352 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004355 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004357 if (prc == null) {
4358 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004360 }
4361
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004362 boolean lastRef = false;
4363 if (stable) {
4364 if (prc.stableCount == 0) {
4365 if (DEBUG_PROVIDER) Slog.v(TAG,
4366 "releaseProvider: stable ref count already 0, how?");
4367 return false;
4368 }
4369 prc.stableCount -= 1;
4370 if (prc.stableCount == 0) {
4371 // What we do at this point depends on whether there are
4372 // any unstable refs left: if there are, we just tell the
4373 // activity manager to decrement its stable count; if there
4374 // aren't, we need to enqueue this provider to be removed,
4375 // and convert to holding a single unstable ref while
4376 // doing so.
4377 lastRef = prc.unstableCount == 0;
4378 try {
4379 if (DEBUG_PROVIDER) {
4380 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4381 + lastRef + " - " + prc.holder.info.name);
4382 }
4383 ActivityManagerNative.getDefault().refContentProvider(
4384 prc.holder.connection, -1, lastRef ? 1 : 0);
4385 } catch (RemoteException e) {
4386 //do nothing content provider object is dead any way
4387 }
4388 }
4389 } else {
4390 if (prc.unstableCount == 0) {
4391 if (DEBUG_PROVIDER) Slog.v(TAG,
4392 "releaseProvider: unstable ref count already 0, how?");
4393 return false;
4394 }
4395 prc.unstableCount -= 1;
4396 if (prc.unstableCount == 0) {
4397 // If this is the last reference, we need to enqueue
4398 // this provider to be removed instead of telling the
4399 // activity manager to remove it at this point.
4400 lastRef = prc.stableCount == 0;
4401 if (!lastRef) {
4402 try {
4403 if (DEBUG_PROVIDER) {
4404 Slog.v(TAG, "releaseProvider: No longer unstable - "
4405 + prc.holder.info.name);
4406 }
4407 ActivityManagerNative.getDefault().refContentProvider(
4408 prc.holder.connection, 0, -1);
4409 } catch (RemoteException e) {
4410 //do nothing content provider object is dead any way
4411 }
4412 }
4413 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004414 }
4415
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004416 if (lastRef) {
4417 if (!prc.removePending) {
4418 // Schedule the actual remove asynchronously, since we don't know the context
4419 // this will be called in.
4420 // TODO: it would be nice to post a delayed message, so
4421 // if we come back and need the same provider quickly
4422 // we will still have it available.
4423 if (DEBUG_PROVIDER) {
4424 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4425 + prc.holder.info.name);
4426 }
4427 prc.removePending = true;
4428 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4429 mH.sendMessage(msg);
4430 } else {
4431 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4432 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004433 }
4434 return true;
4435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 }
4437
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004438 final void completeRemoveProvider(ProviderRefCount prc) {
4439 synchronized (mProviderMap) {
4440 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004441 // There was a race! Some other client managed to acquire
4442 // the provider before the removal was completed.
4443 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004444 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004445 + "provider still in use");
4446 return;
4447 }
4448
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004449 final IBinder jBinder = prc.holder.provider.asBinder();
4450 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4451 if (existingPrc == prc) {
4452 mProviderRefCountMap.remove(jBinder);
4453 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004454
4455 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4456 while (iter.hasNext()) {
4457 ProviderClientRecord pr = iter.next();
4458 IBinder myBinder = pr.mProvider.asBinder();
4459 if (myBinder == jBinder) {
4460 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004461 }
4462 }
4463 }
4464
4465 try {
4466 if (DEBUG_PROVIDER) {
4467 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4468 + "removeContentProvider(" + prc.holder.info.name + ")");
4469 }
4470 ActivityManagerNative.getDefault().removeContentProvider(
4471 prc.holder.connection, false);
4472 } catch (RemoteException e) {
4473 //do nothing content provider object is dead any way
4474 }
4475 }
4476
4477 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4478 synchronized(mProviderMap) {
4479 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4480 if (prc != null) {
4481 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4482 + provider + " " + prc.holder.info.name);
4483 mProviderRefCountMap.remove(provider);
4484 if (prc.client != null && prc.client.mNames != null) {
4485 for (String name : prc.client.mNames) {
4486 ProviderClientRecord pr = mProviderMap.get(name);
4487 if (pr != null && pr.mProvider.asBinder() == provider) {
4488 Slog.i(TAG, "Removing dead content provider: " + name);
4489 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004490 }
4491 }
4492 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004493 if (fromClient) {
4494 // We found out about this due to execution in our client
4495 // code. Tell the activity manager about it now, to ensure
4496 // that the next time we go to do anything with the provider
4497 // it knows it is dead (so we don't race with its death
4498 // notification).
4499 try {
4500 ActivityManagerNative.getDefault().unstableProviderDied(
4501 prc.holder.connection);
4502 } catch (RemoteException e) {
4503 //do nothing content provider object is dead any way
4504 }
4505 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004506 }
4507 }
4508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004510 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4511 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4512 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4513 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4514 localProvider, holder);
4515 for (int i = 0; i < names.length; i++) {
4516 ProviderClientRecord existing = mProviderMap.get(names[i]);
4517 if (existing != null) {
4518 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4519 + " already published as " + names[i]);
4520 } else {
4521 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 }
4523 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004524 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 }
4526
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004527 /**
4528 * Installs the provider.
4529 *
4530 * Providers that are local to the process or that come from the system server
4531 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4532 * Other remote providers are reference counted. The initial reference count
4533 * for all reference counted providers is one. Providers that are not reference
4534 * counted do not have a reference count (at all).
4535 *
4536 * This method detects when a provider has already been installed. When this happens,
4537 * it increments the reference count of the existing provider (if appropriate)
4538 * and returns the existing provider. This can happen due to concurrent
4539 * attempts to acquire the same provider.
4540 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004541 private IActivityManager.ContentProviderHolder installProvider(Context context,
4542 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4543 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004545 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004546 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004547 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004548 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 + info.name);
4550 }
4551 Context c = null;
4552 ApplicationInfo ai = info.applicationInfo;
4553 if (context.getPackageName().equals(ai.packageName)) {
4554 c = context;
4555 } else if (mInitialApplication != null &&
4556 mInitialApplication.getPackageName().equals(ai.packageName)) {
4557 c = mInitialApplication;
4558 } else {
4559 try {
4560 c = context.createPackageContext(ai.packageName,
4561 Context.CONTEXT_INCLUDE_CODE);
4562 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004563 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 }
4565 }
4566 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004567 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 ai.packageName +
4569 " while loading content provider " +
4570 info.name);
4571 return null;
4572 }
4573 try {
4574 final java.lang.ClassLoader cl = c.getClassLoader();
4575 localProvider = (ContentProvider)cl.
4576 loadClass(info.name).newInstance();
4577 provider = localProvider.getIContentProvider();
4578 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004579 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 info.name + " from sourceDir " +
4581 info.applicationInfo.sourceDir);
4582 return null;
4583 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004584 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 TAG, "Instantiating local provider " + info.name);
4586 // XXX Need to create the correct context for this provider.
4587 localProvider.attachInfo(c, info);
4588 } catch (java.lang.Exception e) {
4589 if (!mInstrumentation.onException(null, e)) {
4590 throw new RuntimeException(
4591 "Unable to get provider " + info.name
4592 + ": " + e.toString(), e);
4593 }
4594 return null;
4595 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004596 } else {
4597 provider = holder.provider;
4598 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 + info.name);
4600 }
4601
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004602 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004603
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004604 synchronized (mProviderMap) {
4605 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4606 + " / " + info.name);
4607 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004609 ComponentName cname = new ComponentName(info.packageName, info.name);
4610 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004611 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004612 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004613 Slog.v(TAG, "installProvider: lost the race, "
4614 + "using existing local provider");
4615 }
4616 provider = pr.mProvider;
4617 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004618 holder = new IActivityManager.ContentProviderHolder(info);
4619 holder.provider = provider;
4620 holder.noReleaseNeeded = true;
4621 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004622 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004623 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004624 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004625 retHolder = pr.mHolder;
4626 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004627 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4628 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004629 if (DEBUG_PROVIDER) {
4630 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004631 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004632 // We need to transfer our new reference to the existing
4633 // ref count, releasing the old one... but only if
4634 // release is needed (that is, it is not running in the
4635 // system process).
4636 if (!noReleaseNeeded) {
4637 incProviderRefLocked(prc, stable);
4638 try {
4639 ActivityManagerNative.getDefault().removeContentProvider(
4640 holder.connection, stable);
4641 } catch (RemoteException e) {
4642 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004643 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004644 }
4645 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004646 ProviderClientRecord client = installProviderAuthoritiesLocked(
4647 provider, localProvider, holder);
4648 if (noReleaseNeeded) {
4649 prc = new ProviderRefCount(holder, client, 1000, 1000);
4650 } else {
4651 prc = stable
4652 ? new ProviderRefCount(holder, client, 1, 0)
4653 : new ProviderRefCount(holder, client, 0, 1);
4654 }
4655 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004656 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004657 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 }
4659 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004660
4661 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004662 }
4663
Romain Guy65b345f2011-07-27 18:51:50 -07004664 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004665 sThreadLocal.set(this);
4666 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004667 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004668 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004669 public void run() {
4670 ensureJitEnabled();
4671 }
4672 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4674 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4675 IActivityManager mgr = ActivityManagerNative.getDefault();
4676 try {
4677 mgr.attachApplication(mAppThread);
4678 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004679 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 }
4681 } else {
4682 // Don't set application object here -- if the system crashes,
4683 // we can't display an alert, we just want to die die die.
4684 android.ddm.DdmHandleAppName.setAppName("system_process");
4685 try {
4686 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004687 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 context.init(getSystemContext().mPackageInfo, null, this);
4689 Application app = Instrumentation.newApplication(Application.class, context);
4690 mAllApplications.add(app);
4691 mInitialApplication = app;
4692 app.onCreate();
4693 } catch (Exception e) {
4694 throw new RuntimeException(
4695 "Unable to instantiate Application():" + e.toString(), e);
4696 }
4697 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004698
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004699 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004700 public void onConfigurationChanged(Configuration newConfig) {
4701 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004702 // We need to apply this change to the resources
4703 // immediately, because upon returning the view
4704 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004705 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004706 // This actually changed the resources! Tell
4707 // everyone about it.
4708 if (mPendingConfiguration == null ||
4709 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4710 mPendingConfiguration = newConfig;
4711
4712 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4713 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004714 }
4715 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004716 }
4717 public void onLowMemory() {
4718 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004719 public void onTrimMemory(int level) {
4720 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004721 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 }
4723
Romain Guy5e9120d2012-01-30 12:17:22 -08004724 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004725 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 ActivityThread thread = new ActivityThread();
4727 thread.attach(true);
4728 return thread;
4729 }
4730
Jeff Brown10e89712011-07-08 18:52:57 -07004731 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004732 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004733 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004734 }
4735 }
4736
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004737 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004738 synchronized (mPackages) {
4739 if (mCoreSettings != null) {
4740 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004741 } else {
4742 return defaultValue;
4743 }
4744 }
4745 }
4746
Romain Guy65b345f2011-07-27 18:51:50 -07004747 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004748 SamplingProfilerIntegration.start();
4749
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004750 // CloseGuard defaults to true and can be quite spammy. We
4751 // disable it here, but selectively enable it later (via
4752 // StrictMode) on debug builds, but using DropBox, not logs.
4753 CloseGuard.setEnabled(false);
4754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 Process.setArgV0("<pre-initialized>");
4756
4757 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004758 if (sMainThreadHandler == null) {
4759 sMainThreadHandler = new Handler();
4760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761
4762 ActivityThread thread = new ActivityThread();
4763 thread.attach(false);
4764
Romain Guy5e9120d2012-01-30 12:17:22 -08004765 AsyncTask.init();
4766
Dianne Hackborn287952c2010-09-22 22:34:31 -07004767 if (false) {
4768 Looper.myLooper().setMessageLogging(new
4769 LogPrinter(Log.DEBUG, "ActivityThread"));
4770 }
4771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 Looper.loop();
4773
Jeff Brown10e89712011-07-08 18:52:57 -07004774 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004775 }
4776}