blob: b55ee26a500b39c2d2a412741a911fa765102580 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070074import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.view.View;
76import android.view.ViewDebug;
77import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070078import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.view.Window;
80import android.view.WindowManager;
81import android.view.WindowManagerImpl;
Jason Samsa6f338c2012-02-24 16:22:16 -080082import android.renderscript.RenderScript;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84import com.android.internal.os.BinderInternal;
85import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070086import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
89
90import java.io.File;
91import java.io.FileDescriptor;
92import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070093import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import java.io.PrintWriter;
95import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -070096import java.net.InetAddress;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import java.util.ArrayList;
98import java.util.HashMap;
99import java.util.Iterator;
100import java.util.List;
101import java.util.Locale;
102import java.util.Map;
103import java.util.TimeZone;
104import java.util.regex.Pattern;
105
Jeff Sharkeye861b422012-03-01 20:59:22 -0800106import libcore.io.IoUtils;
107
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800108import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110final class SuperNotCalledException extends AndroidRuntimeException {
111 public SuperNotCalledException(String msg) {
112 super(msg);
113 }
114}
115
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700116final class RemoteServiceException extends AndroidRuntimeException {
117 public RemoteServiceException(String msg) {
118 super(msg);
119 }
120}
121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122/**
123 * This manages the execution of the main thread in an
124 * application process, scheduling and executing activities,
125 * broadcasts, and other operations on it as the activity
126 * manager requests.
127 *
128 * {@hide}
129 */
130public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700131 /** @hide */
132 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700133 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700134 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700135 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700136 /** @hide */
137 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700138 private static final boolean DEBUG_RESULTS = false;
Christopher Tate4a627c72011-04-01 14:43:32 -0700139 private static final boolean DEBUG_BACKUP = true;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700140 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800141 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700142 private static final boolean DEBUG_MEMORY_TRIM = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
144 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
145 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
146 private static final int LOG_ON_PAUSE_CALLED = 30021;
147 private static final int LOG_ON_RESUME_CALLED = 30022;
148
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700149 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700150
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700151 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700153 final ApplicationThread mAppThread = new ApplicationThread();
154 final Looper mLooper = Looper.myLooper();
155 final H mH = new H();
156 final HashMap<IBinder, ActivityClientRecord> mActivities
157 = new HashMap<IBinder, ActivityClientRecord>();
158 // List of new activities (via ActivityRecord.nextIdle) that should
159 // be reported when next we idle.
160 ActivityClientRecord mNewActivities = null;
161 // Number of activities that are currently visible on-screen.
162 int mNumVisibleActivities = 0;
163 final HashMap<IBinder, Service> mServices
164 = new HashMap<IBinder, Service>();
165 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700166 Profiler mProfiler;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700167 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700168 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700169 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700170 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700171 Application mInitialApplication;
172 final ArrayList<Application> mAllApplications
173 = new ArrayList<Application>();
174 // set of instantiated backup agents, keyed by package name
175 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700176 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700177 Instrumentation mInstrumentation;
178 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700179 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 String mInstrumentationAppPackage = null;
181 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700182 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700183 boolean mSystemThread = false;
184 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700186 // These can be accessed by multiple threads; mPackages is the lock.
187 // XXX For now we keep around information about all packages we have
188 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800189 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700190 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800191 // which means this lock gets held while the activity and window managers
192 // holds their own lock. Thus you MUST NEVER call back into the activity manager
193 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 final HashMap<String, WeakReference<LoadedApk>> mPackages
195 = new HashMap<String, WeakReference<LoadedApk>>();
196 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
197 = new HashMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700198 final HashMap<CompatibilityInfo, DisplayMetrics> mDisplayMetrics
199 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
201 = new HashMap<ResourcesKey, WeakReference<Resources> >();
202 final ArrayList<ActivityClientRecord> mRelaunchingActivities
203 = new ArrayList<ActivityClientRecord>();
204 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700206 // The lock of mProviderMap protects the following variables.
207 final HashMap<String, ProviderClientRecord> mProviderMap
208 = new HashMap<String, ProviderClientRecord>();
209 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
210 = new HashMap<IBinder, ProviderRefCount>();
211 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
212 = new HashMap<IBinder, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Jeff Hamilton52d32032011-01-08 15:31:26 -0600214 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
215 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
216
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700217 final GcIdler mGcIdler = new GcIdler();
218 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700220 static Handler sMainThreadHandler; // set once in main()
221
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800222 Bundle mCoreSettings = null;
223
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400224 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700226 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 Intent intent;
228 Bundle state;
229 Activity activity;
230 Window window;
231 Activity parent;
232 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700233 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 boolean paused;
235 boolean stopped;
236 boolean hideForNow;
237 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700238 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700241 String profileFile;
242 ParcelFileDescriptor profileFd;
243 boolean autoStopProfiler;
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400246 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
249 List<ResultInfo> pendingResults;
250 List<Intent> pendingIntents;
251
252 boolean startsNotResumed;
253 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800254 int pendingConfigChanges;
255 boolean onlyLocalRequest;
256
257 View mPendingRemoveWindow;
258 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700260 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 parent = null;
262 embeddedID = null;
263 paused = false;
264 stopped = false;
265 hideForNow = false;
266 nextIdle = null;
267 }
268
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800269 public boolean isPreHoneycomb() {
270 if (activity != null) {
271 return activity.getApplicationInfo().targetSdkVersion
272 < android.os.Build.VERSION_CODES.HONEYCOMB;
273 }
274 return false;
275 }
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 public String toString() {
278 ComponentName componentName = intent.getComponent();
279 return "ActivityRecord{"
280 + Integer.toHexString(System.identityHashCode(this))
281 + " token=" + token + " " + (componentName == null
282 ? "no component name" : componentName.toShortString())
283 + "}";
284 }
285 }
286
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400287 final class ProviderClientRecord implements IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 final String mName;
289 final IContentProvider mProvider;
290 final ContentProvider mLocalProvider;
291
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700292 ProviderClientRecord(String name, IContentProvider provider,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 ContentProvider localProvider) {
294 mName = name;
295 mProvider = provider;
296 mLocalProvider = localProvider;
297 }
298
299 public void binderDied() {
300 removeDeadProvider(mName, mProvider);
301 }
302 }
303
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400304 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 List<Intent> intents;
306 IBinder token;
307 public String toString() {
308 return "NewIntentData{intents=" + intents + " token=" + token + "}";
309 }
310 }
311
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400312 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700313 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
314 boolean ordered, boolean sticky, IBinder token) {
315 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
316 this.intent = intent;
317 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 Intent intent;
320 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 public String toString() {
323 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700324 info.packageName + " resultCode=" + getResultCode()
325 + " resultData=" + getResultData() + " resultExtras="
326 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328 }
329
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700331 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700333 int backupMode;
334 public String toString() {
335 return "CreateBackupAgentData{appInfo=" + appInfo
336 + " backupAgent=" + appInfo.backupAgentName
337 + " mode=" + backupMode + "}";
338 }
339 }
Bob Leee5408332009-09-04 18:31:17 -0700340
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 IBinder token;
343 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400344 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 Intent intent;
346 public String toString() {
347 return "CreateServiceData{token=" + token + " className="
348 + info.name + " packageName=" + info.packageName
349 + " intent=" + intent + "}";
350 }
351 }
352
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400353 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 IBinder token;
355 Intent intent;
356 boolean rebind;
357 public String toString() {
358 return "BindServiceData{token=" + token + " intent=" + intent + "}";
359 }
360 }
361
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700364 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700366 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 Intent args;
368 public String toString() {
369 return "ServiceArgsData{token=" + token + " startId=" + startId
370 + " args=" + args + "}";
371 }
372 }
373
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400374 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700375 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 String processName;
377 ApplicationInfo appInfo;
378 List<ProviderInfo> providers;
379 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 Bundle instrumentationArgs;
381 IInstrumentationWatcher instrumentationWatcher;
382 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800383 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700384 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700385 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400387 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700388
389 /** Initial values for {@link Profiler}. */
390 String initProfileFile;
391 ParcelFileDescriptor initProfileFd;
392 boolean initAutoStopProfiler;
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 public String toString() {
395 return "AppBindData{appInfo=" + appInfo + "}";
396 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700397 }
398
399 static final class Profiler {
400 String profileFile;
401 ParcelFileDescriptor profileFd;
402 boolean autoStopProfiler;
403 boolean profiling;
404 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700405 public void setProfiler(String file, ParcelFileDescriptor fd) {
406 if (profiling) {
407 if (fd != null) {
408 try {
409 fd.close();
410 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700411 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700412 }
413 }
414 return;
415 }
416 if (profileFd != null) {
417 try {
418 profileFd.close();
419 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700420 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700421 }
422 }
423 profileFile = file;
424 profileFd = fd;
425 }
426 public void startProfiling() {
427 if (profileFd == null || profiling) {
428 return;
429 }
430 try {
431 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
432 8 * 1024 * 1024, 0);
433 profiling = true;
434 } catch (RuntimeException e) {
435 Slog.w(TAG, "Profiling failed on path " + profileFile);
436 try {
437 profileFd.close();
438 profileFd = null;
439 } catch (IOException e2) {
440 Slog.w(TAG, "Failure closing profile fd", e2);
441 }
442 }
443 }
444 public void stopProfiling() {
445 if (profiling) {
446 profiling = false;
447 Debug.stopMethodTracing();
448 if (profileFd != null) {
449 try {
450 profileFd.close();
451 } catch (IOException e) {
452 }
453 }
454 profileFd = null;
455 profileFile = null;
456 }
457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400460 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700461 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700462 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800463 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 IBinder token;
469 List<ResultInfo> results;
470 public String toString() {
471 return "ResultData{token=" + token + " results" + results + "}";
472 }
473 }
474
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400475 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800476 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 String what;
478 String who;
479 }
480
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400481 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700482 String path;
483 ParcelFileDescriptor fd;
484 }
485
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700487 String path;
488 ParcelFileDescriptor fd;
489 }
490
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400491 static final class UpdateCompatibilityData {
492 String pkg;
493 CompatibilityInfo info;
494 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700495
Romain Guy65b345f2011-07-27 18:51:50 -0700496 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700497
Romain Guy65b345f2011-07-27 18:51:50 -0700498 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700499 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
500 private static final String ONE_COUNT_COLUMN = "%21s %8d";
501 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700502 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 // Formatting for checkin service - update version if row format changes
505 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700506
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700507 private void updatePendingConfiguration(Configuration config) {
508 synchronized (mPackages) {
509 if (mPendingConfiguration == null ||
510 mPendingConfiguration.isOtherSeqNewer(config)) {
511 mPendingConfiguration = config;
512 }
513 }
514 }
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public final void schedulePauseActivity(IBinder token, boolean finished,
517 boolean userLeaving, int configChanges) {
518 queueOrSendMessage(
519 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
520 token,
521 (userLeaving ? 1 : 0),
522 configChanges);
523 }
524
525 public final void scheduleStopActivity(IBinder token, boolean showWindow,
526 int configChanges) {
527 queueOrSendMessage(
528 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
529 token, 0, configChanges);
530 }
531
532 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
533 queueOrSendMessage(
534 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
535 token);
536 }
537
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800538 public final void scheduleSleeping(IBinder token, boolean sleeping) {
539 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
540 }
541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
543 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
544 }
545
546 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
547 ResultData res = new ResultData();
548 res.token = token;
549 res.results = results;
550 queueOrSendMessage(H.SEND_RESULT, res);
551 }
552
553 // we use token to identify this activity without having to send the
554 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700555 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700556 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
557 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700558 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
559 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700560 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561
562 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700563 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 r.intent = intent;
565 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400566 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 r.state = state;
568
569 r.pendingResults = pendingResults;
570 r.pendingIntents = pendingNewIntents;
571
572 r.startsNotResumed = notResumed;
573 r.isForward = isForward;
574
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700575 r.profileFile = profileName;
576 r.profileFd = profileFd;
577 r.autoStopProfiler = autoStopProfiler;
578
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700579 updatePendingConfiguration(curConfig);
580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
582 }
583
584 public final void scheduleRelaunchActivity(IBinder token,
585 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800586 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800587 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
588 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590
591 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
592 NewIntentData data = new NewIntentData();
593 data.intents = intents;
594 data.token = token;
595
596 queueOrSendMessage(H.NEW_INTENT, data);
597 }
598
599 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
600 int configChanges) {
601 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
602 configChanges);
603 }
604
605 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
607 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700608 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
609 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400611 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 queueOrSendMessage(H.RECEIVER, r);
613 }
614
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400615 public final void scheduleCreateBackupAgent(ApplicationInfo app,
616 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700617 CreateBackupAgentData d = new CreateBackupAgentData();
618 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400619 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700620 d.backupMode = backupMode;
621
622 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
623 }
624
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
626 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700627 CreateBackupAgentData d = new CreateBackupAgentData();
628 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400629 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700630
631 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
632 }
633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400635 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 CreateServiceData s = new CreateServiceData();
637 s.token = token;
638 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400639 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640
641 queueOrSendMessage(H.CREATE_SERVICE, s);
642 }
643
644 public final void scheduleBindService(IBinder token, Intent intent,
645 boolean rebind) {
646 BindServiceData s = new BindServiceData();
647 s.token = token;
648 s.intent = intent;
649 s.rebind = rebind;
650
Amith Yamasani742a6712011-05-04 14:49:28 -0700651 if (DEBUG_SERVICE)
652 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
653 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 queueOrSendMessage(H.BIND_SERVICE, s);
655 }
656
657 public final void scheduleUnbindService(IBinder token, Intent intent) {
658 BindServiceData s = new BindServiceData();
659 s.token = token;
660 s.intent = intent;
661
662 queueOrSendMessage(H.UNBIND_SERVICE, s);
663 }
664
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700665 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700666 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 ServiceArgsData s = new ServiceArgsData();
668 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700669 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700671 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 s.args = args;
673
674 queueOrSendMessage(H.SERVICE_ARGS, s);
675 }
676
677 public final void scheduleStopService(IBinder token) {
678 queueOrSendMessage(H.STOP_SERVICE, token);
679 }
680
681 public final void bindApplication(String processName,
682 ApplicationInfo appInfo, List<ProviderInfo> providers,
683 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700684 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800686 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
687 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700688 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689
690 if (services != null) {
691 // Setup the service cache in the ServiceManager
692 ServiceManager.initServiceCache(services);
693 }
694
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800695 setCoreSettings(coreSettings);
696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 AppBindData data = new AppBindData();
698 data.processName = processName;
699 data.appInfo = appInfo;
700 data.providers = providers;
701 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 data.instrumentationArgs = instrumentationArgs;
703 data.instrumentationWatcher = instrumentationWatcher;
704 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800705 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700706 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700707 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400709 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700710 data.initProfileFile = profileFile;
711 data.initProfileFd = profileFd;
712 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 queueOrSendMessage(H.BIND_APPLICATION, data);
714 }
715
716 public final void scheduleExit() {
717 queueOrSendMessage(H.EXIT_APPLICATION, null);
718 }
719
Christopher Tate5e1ab332009-09-01 20:32:49 -0700720 public final void scheduleSuicide() {
721 queueOrSendMessage(H.SUICIDE, null);
722 }
723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 public void requestThumbnail(IBinder token) {
725 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
726 }
727
728 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700729 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
731 }
732
733 public void updateTimeZone() {
734 TimeZone.setDefault(null);
735 }
736
Robert Greenwalt03595d02010-11-02 14:08:23 -0700737 public void clearDnsCache() {
738 // a non-standard API to get this to libcore
739 InetAddress.clearDnsCache();
740 }
741
Robert Greenwalt434203a2010-10-11 16:00:27 -0700742 public void setHttpProxy(String host, String port, String exclList) {
743 Proxy.setHttpProxySystemProperty(host, port, exclList);
744 }
745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public void processInBackground() {
747 mH.removeMessages(H.GC_WHEN_IDLE);
748 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
749 }
750
751 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700752 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700753 try {
754 data.fd = ParcelFileDescriptor.dup(fd);
755 data.token = servicetoken;
756 data.args = args;
757 queueOrSendMessage(H.DUMP_SERVICE, data);
758 } catch (IOException e) {
759 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 }
761 }
762
763 // This function exists to make sure all receiver dispatching is
764 // correctly ordered, since these are one-way calls and the binder driver
765 // applies transaction ordering per object for such calls.
766 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700767 int resultCode, String dataStr, Bundle extras, boolean ordered,
768 boolean sticky) throws RemoteException {
769 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
Bob Leee5408332009-09-04 18:31:17 -0700771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 public void scheduleLowMemory() {
773 queueOrSendMessage(H.LOW_MEMORY, null);
774 }
775
776 public void scheduleActivityConfigurationChanged(IBinder token) {
777 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
778 }
779
Romain Guy7eabe552011-07-21 14:56:34 -0700780 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
781 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700782 ProfilerControlData pcd = new ProfilerControlData();
783 pcd.path = path;
784 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700785 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800786 }
787
Andy McFadden824c5102010-07-09 16:26:57 -0700788 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
789 DumpHeapData dhd = new DumpHeapData();
790 dhd.path = path;
791 dhd.fd = fd;
792 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
793 }
794
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700795 public void setSchedulingGroup(int group) {
796 // Note: do this immediately, since going into the foreground
797 // should happen regardless of what pending work we have to do
798 // and the activity manager will wait for us to report back that
799 // we are done before sending us to the background.
800 try {
801 Process.setProcessGroup(Process.myPid(), group);
802 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800803 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700804 }
805 }
Bob Leee5408332009-09-04 18:31:17 -0700806
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700807 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
808 Debug.getMemoryInfo(outInfo);
809 }
Bob Leee5408332009-09-04 18:31:17 -0700810
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700811 public void dispatchPackageBroadcast(int cmd, String[] packages) {
812 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
813 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700814
815 public void scheduleCrash(String msg) {
816 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
817 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700818
Dianne Hackborn30d71892010-12-11 10:37:55 -0800819 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
820 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700821 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700822 try {
823 data.fd = ParcelFileDescriptor.dup(fd);
824 data.token = activitytoken;
825 data.prefix = prefix;
826 data.args = args;
827 queueOrSendMessage(H.DUMP_ACTIVITY, data);
828 } catch (IOException e) {
829 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700830 }
831 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700832
Marco Nelissen18cb2872011-11-15 11:19:53 -0800833 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
834 String[] args) {
835 DumpComponentInfo data = new DumpComponentInfo();
836 try {
837 data.fd = ParcelFileDescriptor.dup(fd);
838 data.token = providertoken;
839 data.args = args;
840 queueOrSendMessage(H.DUMP_PROVIDER, data);
841 } catch (IOException e) {
842 Slog.w(TAG, "dumpProvider failed", e);
843 }
844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700847 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
848 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700849 FileOutputStream fout = new FileOutputStream(fd);
850 PrintWriter pw = new PrintWriter(fout);
851 try {
Romain Guya998dff2012-03-23 18:58:36 -0700852 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700853 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700854 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700855 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700856 }
857
Romain Guya998dff2012-03-23 18:58:36 -0700858 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 long nativeMax = Debug.getNativeHeapSize() / 1024;
860 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
861 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
862
863 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
864 Debug.getMemoryInfo(memInfo);
865
Dianne Hackbornb437e092011-08-05 17:50:29 -0700866 if (!all) {
867 return memInfo;
868 }
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 Runtime runtime = Runtime.getRuntime();
871
872 long dalvikMax = runtime.totalMemory() / 1024;
873 long dalvikFree = runtime.freeMemory() / 1024;
874 long dalvikAllocated = dalvikMax - dalvikFree;
875 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700876 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700877 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
878 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 int globalAssetCount = AssetManager.getGlobalAssetCount();
880 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
881 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
882 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
883 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700884 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800885 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700888 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 // NOTE: if you change anything significant below, also consider changing
890 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700891 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 // Header
895 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
896 pw.print(Process.myPid()); pw.print(',');
897 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 // Heap info - max
900 pw.print(nativeMax); pw.print(',');
901 pw.print(dalvikMax); pw.print(',');
902 pw.print("N/A,");
903 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // Heap info - allocated
906 pw.print(nativeAllocated); pw.print(',');
907 pw.print(dalvikAllocated); pw.print(',');
908 pw.print("N/A,");
909 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 // Heap info - free
912 pw.print(nativeFree); pw.print(',');
913 pw.print(dalvikFree); pw.print(',');
914 pw.print("N/A,");
915 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700916
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 // Heap info - proportional set size
918 pw.print(memInfo.nativePss); pw.print(',');
919 pw.print(memInfo.dalvikPss); pw.print(',');
920 pw.print(memInfo.otherPss); pw.print(',');
921 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700924 pw.print(memInfo.nativeSharedDirty); pw.print(',');
925 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
926 pw.print(memInfo.otherSharedDirty); pw.print(',');
927 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
928 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700931 pw.print(memInfo.nativePrivateDirty); pw.print(',');
932 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
933 pw.print(memInfo.otherPrivateDirty); pw.print(',');
934 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
935 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 // Object counts
938 pw.print(viewInstanceCount); pw.print(',');
939 pw.print(viewRootInstanceCount); pw.print(',');
940 pw.print(appContextInstanceCount); pw.print(',');
941 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800943 pw.print(globalAssetCount); pw.print(',');
944 pw.print(globalAssetManagerCount); pw.print(',');
945 pw.print(binderLocalObjectCount); pw.print(',');
946 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 pw.print(binderDeathObjectCount); pw.print(',');
949 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800952 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800953 pw.print(stats.memoryUsed / 1024); pw.print(',');
954 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700955 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800956 for (int i = 0; i < stats.dbStats.size(); i++) {
957 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700958 pw.print(','); pw.print(dbStats.dbName);
959 pw.print(','); pw.print(dbStats.pageSize);
960 pw.print(','); pw.print(dbStats.dbSize);
961 pw.print(','); pw.print(dbStats.lookaside);
962 pw.print(','); pw.print(dbStats.cache);
963 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800964 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700965 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700966
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700967 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
Bob Leee5408332009-09-04 18:31:17 -0700969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700971 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
972 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
973 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
974 "------");
975 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
976 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
977 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
978 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700980 int otherPss = memInfo.otherPss;
981 int otherSharedDirty = memInfo.otherSharedDirty;
982 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700984 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700985 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
987 memInfo.getOtherPrivateDirty(i), "", "", "");
988 otherPss -= memInfo.getOtherPss(i);
989 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
990 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
991 }
992
993 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
994 otherPrivateDirty, "", "", "");
995 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
996 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
997 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
998 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999
1000 pw.println(" ");
1001 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001002 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 viewRootInstanceCount);
1004
1005 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1006 "Activities:", activityInstanceCount);
1007
1008 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1009 "AssetManagers:", globalAssetManagerCount);
1010
1011 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1012 "Proxy Binders:", binderProxyObjectCount);
1013 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1014
1015 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001016
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 // SQLite mem info
1018 pw.println(" ");
1019 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001020 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1021 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1022 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001023 pw.println(" ");
1024 int N = stats.dbStats.size();
1025 if (N > 0) {
1026 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001027 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1028 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001029 for (int i = 0; i < N; i++) {
1030 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001031 printRow(pw, DB_INFO_FORMAT,
1032 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1033 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1034 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1035 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001036 }
1037 }
Bob Leee5408332009-09-04 18:31:17 -07001038
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001039 // Asset details.
1040 String assetAlloc = AssetManager.getAssetAllocations();
1041 if (assetAlloc != null) {
1042 pw.println(" ");
1043 pw.println(" Asset Allocations");
1044 pw.print(assetAlloc);
1045 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001046
1047 return memInfo;
1048 }
1049
1050 @Override
1051 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1052 dumpGraphicsInfo(fd);
Romain Guy65b345f2011-07-27 18:51:50 -07001053 WindowManagerImpl.getDefault().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 }
1055
Jeff Brown6754ba22011-12-14 20:20:01 -08001056 @Override
1057 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1058 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1059 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1060 SQLiteDebug.dump(printer, args);
1061 pw.flush();
1062 }
1063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 private void printRow(PrintWriter pw, String format, Object...objs) {
1065 pw.println(String.format(format, objs));
1066 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001067
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001068 public void setCoreSettings(Bundle coreSettings) {
1069 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001070 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001071
1072 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1073 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1074 ucd.pkg = pkg;
1075 ucd.info = info;
1076 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1077 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001078
1079 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001080 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001081 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084
Romain Guy65b345f2011-07-27 18:51:50 -07001085 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 public static final int LAUNCH_ACTIVITY = 100;
1087 public static final int PAUSE_ACTIVITY = 101;
1088 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1089 public static final int STOP_ACTIVITY_SHOW = 103;
1090 public static final int STOP_ACTIVITY_HIDE = 104;
1091 public static final int SHOW_WINDOW = 105;
1092 public static final int HIDE_WINDOW = 106;
1093 public static final int RESUME_ACTIVITY = 107;
1094 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001095 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 public static final int BIND_APPLICATION = 110;
1097 public static final int EXIT_APPLICATION = 111;
1098 public static final int NEW_INTENT = 112;
1099 public static final int RECEIVER = 113;
1100 public static final int CREATE_SERVICE = 114;
1101 public static final int SERVICE_ARGS = 115;
1102 public static final int STOP_SERVICE = 116;
1103 public static final int REQUEST_THUMBNAIL = 117;
1104 public static final int CONFIGURATION_CHANGED = 118;
1105 public static final int CLEAN_UP_CONTEXT = 119;
1106 public static final int GC_WHEN_IDLE = 120;
1107 public static final int BIND_SERVICE = 121;
1108 public static final int UNBIND_SERVICE = 122;
1109 public static final int DUMP_SERVICE = 123;
1110 public static final int LOW_MEMORY = 124;
1111 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1112 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001113 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001114 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001115 public static final int DESTROY_BACKUP_AGENT = 129;
1116 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001117 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001118 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001119 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001120 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001121 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001122 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001123 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001124 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001125 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001126 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001127 public static final int DUMP_PROVIDER = 141;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001129 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 switch (code) {
1131 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1132 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1133 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1134 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1135 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1136 case SHOW_WINDOW: return "SHOW_WINDOW";
1137 case HIDE_WINDOW: return "HIDE_WINDOW";
1138 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1139 case SEND_RESULT: return "SEND_RESULT";
1140 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1141 case BIND_APPLICATION: return "BIND_APPLICATION";
1142 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1143 case NEW_INTENT: return "NEW_INTENT";
1144 case RECEIVER: return "RECEIVER";
1145 case CREATE_SERVICE: return "CREATE_SERVICE";
1146 case SERVICE_ARGS: return "SERVICE_ARGS";
1147 case STOP_SERVICE: return "STOP_SERVICE";
1148 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1149 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1150 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1151 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1152 case BIND_SERVICE: return "BIND_SERVICE";
1153 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1154 case DUMP_SERVICE: return "DUMP_SERVICE";
1155 case LOW_MEMORY: return "LOW_MEMORY";
1156 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1157 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001158 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001159 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1160 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001161 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001162 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001163 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001164 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001165 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001166 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001167 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001168 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001169 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001170 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001171 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001172 case DUMP_PROVIDER: return "DUMP_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
1175 return "(unknown)";
1176 }
1177 public void handleMessage(Message msg) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001178 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 switch (msg.what) {
1180 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001181 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001182 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183
1184 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001185 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001186 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001187 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 } break;
1189 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001190 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001191 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001192 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001193 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 } break;
1195 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001196 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001198 maybeSnapshot();
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 PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001202 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001204 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 break;
1206 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001207 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001209 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 break;
1211 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001212 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 break;
1216 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001219 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 break;
1221 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001224 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 break;
1226 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 handleResumeActivity((IBinder)msg.obj, true,
1229 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 break;
1232 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 break;
1237 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001238 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1240 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001241 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 break;
1243 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001244 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 AppBindData data = (AppBindData)msg.obj;
1246 handleBindApplication(data);
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 EXIT_APPLICATION:
1250 if (mInitialApplication != null) {
1251 mInitialApplication.onTerminate();
1252 }
1253 Looper.myLooper().quit();
1254 break;
1255 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001258 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 break;
1260 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001261 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001263 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001264 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 break;
1266 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001267 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001269 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 break;
1271 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001272 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 break;
1276 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 break;
1281 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001284 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 break;
1286 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001289 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 break;
1292 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 break;
1297 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001299 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 break;
1302 case CLEAN_UP_CONTEXT:
1303 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1304 cci.context.performFinalCleanup(cci.who, cci.what);
1305 break;
1306 case GC_WHEN_IDLE:
1307 scheduleGcIdler();
1308 break;
1309 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001310 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 break;
1312 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001315 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001318 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001320 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001322 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001323 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001324 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001325 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001327 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001328 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001329 break;
1330 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001332 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001334 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001335 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001336 Process.killProcess(Process.myPid());
1337 break;
1338 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001340 completeRemoveProvider((IContentProvider)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001341 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001342 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001343 case ENABLE_JIT:
1344 ensureJitEnabled();
1345 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001346 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001348 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001350 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001351 case SCHEDULE_CRASH:
1352 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001353 case DUMP_HEAP:
1354 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1355 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001356 case DUMP_ACTIVITY:
1357 handleDumpActivity((DumpComponentInfo)msg.obj);
1358 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001359 case DUMP_PROVIDER:
1360 handleDumpProvider((DumpComponentInfo)msg.obj);
1361 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001362 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001364 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001366 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001367 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001368 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001369 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001370 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001371 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001372 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1373 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001374 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001375 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001376 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001377 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001378 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001379 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 }
Dianne Hackborn287952c2010-09-22 22:34:31 -07001381 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + msg.what);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 }
Bob Leee5408332009-09-04 18:31:17 -07001383
Brian Carlstromed7e0072011-03-24 13:27:57 -07001384 private void maybeSnapshot() {
1385 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001386 // convert the *private* ActivityThread.PackageInfo to *public* known
1387 // android.content.pm.PackageInfo
1388 String packageName = mBoundApplication.info.mPackageName;
1389 android.content.pm.PackageInfo packageInfo = null;
1390 try {
1391 Context context = getSystemContext();
1392 if(context == null) {
1393 Log.e(TAG, "cannot get a valid context");
1394 return;
1395 }
1396 PackageManager pm = context.getPackageManager();
1397 if(pm == null) {
1398 Log.e(TAG, "cannot get a valid PackageManager");
1399 return;
1400 }
1401 packageInfo = pm.getPackageInfo(
1402 packageName, PackageManager.GET_ACTIVITIES);
1403 } catch (NameNotFoundException e) {
1404 Log.e(TAG, "cannot get package info for " + packageName, e);
1405 }
1406 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001407 }
1408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 }
1410
Romain Guy65b345f2011-07-27 18:51:50 -07001411 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001413 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001414 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001415 if (mBoundApplication != null && mProfiler.profileFd != null
1416 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001417 stopProfiling = true;
1418 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (a != null) {
1420 mNewActivities = null;
1421 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001422 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001424 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 TAG, "Reporting idle of " + a +
1426 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001427 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 if (a.activity != null && !a.activity.mFinished) {
1429 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001430 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001431 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001433 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 }
1436 prev = a;
1437 a = a.nextIdle;
1438 prev.nextIdle = null;
1439 } while (a != null);
1440 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001441 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001442 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001443 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001444 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 return false;
1446 }
1447 }
1448
1449 final class GcIdler implements MessageQueue.IdleHandler {
1450 public final boolean queueIdle() {
1451 doGcIfNeeded();
1452 return false;
1453 }
1454 }
1455
Romain Guy65b345f2011-07-27 18:51:50 -07001456 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001457 final private String mResDir;
1458 final private float mScale;
1459 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001460
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001461 ResourcesKey(String resDir, float scale) {
1462 mResDir = resDir;
1463 mScale = scale;
1464 mHash = mResDir.hashCode() << 2 + (int) (mScale * 2);
1465 }
Bob Leee5408332009-09-04 18:31:17 -07001466
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001467 @Override
1468 public int hashCode() {
1469 return mHash;
1470 }
1471
1472 @Override
1473 public boolean equals(Object obj) {
1474 if (!(obj instanceof ResourcesKey)) {
1475 return false;
1476 }
1477 ResourcesKey peer = (ResourcesKey) obj;
1478 return mResDir.equals(peer.mResDir) && mScale == peer.mScale;
1479 }
1480 }
1481
Romain Guy65b345f2011-07-27 18:51:50 -07001482 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001483 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001484 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485
Romain Guy65b345f2011-07-27 18:51:50 -07001486 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001487 ActivityThread am = currentActivityThread();
1488 return (am != null && am.mBoundApplication != null)
1489 ? am.mBoundApplication.processName : null;
1490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491
Romain Guy65b345f2011-07-27 18:51:50 -07001492 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001493 ActivityThread am = currentActivityThread();
1494 return am != null ? am.mInitialApplication : null;
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001497 public static IPackageManager getPackageManager() {
1498 if (sPackageManager != null) {
1499 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1500 return sPackageManager;
1501 }
1502 IBinder b = ServiceManager.getService("package");
1503 //Slog.v("PackageManager", "default service binder = " + b);
1504 sPackageManager = IPackageManager.Stub.asInterface(b);
1505 //Slog.v("PackageManager", "default service = " + sPackageManager);
1506 return sPackageManager;
1507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001509 DisplayMetrics getDisplayMetricsLocked(CompatibilityInfo ci, boolean forceUpdate) {
1510 DisplayMetrics dm = mDisplayMetrics.get(ci);
1511 if (dm != null && !forceUpdate) {
1512 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001513 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001514 if (dm == null) {
1515 dm = new DisplayMetrics();
1516 mDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001517 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001518 Display d = WindowManagerImpl.getDefault(ci).getDefaultDisplay();
1519 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001520 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1521 // + metrics.heightPixels + " den=" + metrics.density
1522 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001523 return dm;
1524 }
1525
Romain Guy65b345f2011-07-27 18:51:50 -07001526 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001527 Configuration applyConfigCompatMainThread(Configuration config, CompatibilityInfo compat) {
1528 if (config == null) {
1529 return null;
1530 }
1531 if (compat != null && !compat.supportsScreen()) {
1532 mMainThreadConfig.setTo(config);
1533 config = mMainThreadConfig;
1534 compat.applyToConfiguration(config);
1535 }
1536 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001539 /**
1540 * Creates the top level Resources for applications with the given compatibility info.
1541 *
1542 * @param resDir the resource directory.
1543 * @param compInfo the compability info. It will use the default compatibility info when it's
1544 * null.
1545 */
1546 Resources getTopLevelResources(String resDir, CompatibilityInfo compInfo) {
1547 ResourcesKey key = new ResourcesKey(resDir, compInfo.applicationScale);
1548 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001550 // Resources is app scale dependent.
1551 if (false) {
1552 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1553 + compInfo.applicationScale);
1554 }
1555 WeakReference<Resources> wr = mActiveResources.get(key);
1556 r = wr != null ? wr.get() : null;
1557 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1558 if (r != null && r.getAssets().isUpToDate()) {
1559 if (false) {
1560 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1561 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1562 }
1563 return r;
1564 }
1565 }
1566
1567 //if (r != null) {
1568 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1569 // + r + " " + resDir);
1570 //}
1571
1572 AssetManager assets = new AssetManager();
1573 if (assets.addAssetPath(resDir) == 0) {
1574 return null;
1575 }
1576
1577 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Dianne Hackborn836e2622011-10-04 18:32:39 -07001578 DisplayMetrics metrics = getDisplayMetricsLocked(null, false);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001579 r = new Resources(assets, metrics, getConfiguration(), compInfo);
1580 if (false) {
1581 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1582 + r.getConfiguration() + " appScale="
1583 + r.getCompatibilityInfo().applicationScale);
1584 }
1585
1586 synchronized (mPackages) {
1587 WeakReference<Resources> wr = mActiveResources.get(key);
1588 Resources existing = wr != null ? wr.get() : null;
1589 if (existing != null && existing.getAssets().isUpToDate()) {
1590 // Someone else already created the resources while we were
1591 // unlocked; go ahead and use theirs.
1592 r.getAssets().close();
1593 return existing;
1594 }
1595
1596 // XXX need to remove entries when weak references go away
1597 mActiveResources.put(key, new WeakReference<Resources>(r));
1598 return r;
1599 }
1600 }
1601
1602 /**
1603 * Creates the top level resources for the given package.
1604 */
1605 Resources getTopLevelResources(String resDir, LoadedApk pkgInfo) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001606 return getTopLevelResources(resDir, pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001607 }
1608
1609 final Handler getHandler() {
1610 return mH;
1611 }
1612
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001613 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1614 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001615 synchronized (mPackages) {
1616 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1618 ref = mPackages.get(packageName);
1619 } else {
1620 ref = mResourcePackages.get(packageName);
1621 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001622 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001623 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001624 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1625 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 if (packageInfo != null && (packageInfo.mResources == null
1627 || packageInfo.mResources.getAssets().isUpToDate())) {
1628 if (packageInfo.isSecurityViolation()
1629 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1630 throw new SecurityException(
1631 "Requesting code from " + packageName
1632 + " to be run in process "
1633 + mBoundApplication.processName
1634 + "/" + mBoundApplication.appInfo.uid);
1635 }
1636 return packageInfo;
1637 }
1638 }
1639
1640 ApplicationInfo ai = null;
1641 try {
1642 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani483f3b02012-03-13 16:08:00 -07001643 PackageManager.GET_SHARED_LIBRARY_FILES, UserId.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001645 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 }
1647
1648 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001649 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 }
1651
1652 return null;
1653 }
1654
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001655 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1656 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1658 boolean securityViolation = includeCode && ai.uid != 0
1659 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Amith Yamasani742a6712011-05-04 14:49:28 -07001660 ? !UserId.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
1661 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001662 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1663 |Context.CONTEXT_IGNORE_SECURITY))
1664 == Context.CONTEXT_INCLUDE_CODE) {
1665 if (securityViolation) {
1666 String msg = "Requesting code from " + ai.packageName
1667 + " (with uid " + ai.uid + ")";
1668 if (mBoundApplication != null) {
1669 msg = msg + " to be run in process "
1670 + mBoundApplication.processName + " (with uid "
1671 + mBoundApplication.appInfo.uid + ")";
1672 }
1673 throw new SecurityException(msg);
1674 }
1675 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001676 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001679 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1680 CompatibilityInfo compatInfo) {
1681 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 }
1683
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001684 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1685 synchronized (mPackages) {
1686 WeakReference<LoadedApk> ref;
1687 if (includeCode) {
1688 ref = mPackages.get(packageName);
1689 } else {
1690 ref = mResourcePackages.get(packageName);
1691 }
1692 return ref != null ? ref.get() : null;
1693 }
1694 }
1695
Romain Guy65b345f2011-07-27 18:51:50 -07001696 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1698 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001699 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 if (includeCode) {
1701 ref = mPackages.get(aInfo.packageName);
1702 } else {
1703 ref = mResourcePackages.get(aInfo.packageName);
1704 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001705 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 if (packageInfo == null || (packageInfo.mResources != null
1707 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001708 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 : "Loading resource-only package ") + aInfo.packageName
1710 + " (in " + (mBoundApplication != null
1711 ? mBoundApplication.processName : null)
1712 + ")");
1713 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001714 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 securityViolation, includeCode &&
1716 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1717 if (includeCode) {
1718 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001719 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 } else {
1721 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001722 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 }
1724 }
1725 return packageInfo;
1726 }
1727 }
1728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 ActivityThread() {
1730 }
1731
1732 public ApplicationThread getApplicationThread()
1733 {
1734 return mAppThread;
1735 }
1736
1737 public Instrumentation getInstrumentation()
1738 {
1739 return mInstrumentation;
1740 }
1741
1742 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001743 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745
1746 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001747 return mProfiler != null && mProfiler.profileFile != null
1748 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750
1751 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001752 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
1755 public Looper getLooper() {
1756 return mLooper;
1757 }
1758
1759 public Application getApplication() {
1760 return mInitialApplication;
1761 }
Bob Leee5408332009-09-04 18:31:17 -07001762
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001763 public String getProcessName() {
1764 return mBoundApplication.processName;
1765 }
Bob Leee5408332009-09-04 18:31:17 -07001766
Dianne Hackborn21556372010-02-04 16:34:40 -08001767 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 synchronized (this) {
1769 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001770 ContextImpl context =
1771 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001772 LoadedApk info = new LoadedApk(this, "android", context, null,
1773 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 context.init(info, null, this);
1775 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001776 getConfiguration(), getDisplayMetricsLocked(
1777 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, false));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001779 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 // + ": " + context.getResources().getConfiguration());
1781 }
1782 }
1783 return mSystemContext;
1784 }
1785
Mike Cleron432b7132009-09-24 15:28:29 -07001786 public void installSystemApplicationInfo(ApplicationInfo info) {
1787 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001788 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001789 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001790 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001791
1792 // give ourselves a default profiler
1793 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001794 }
1795 }
1796
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001797 void ensureJitEnabled() {
1798 if (!mJitEnabled) {
1799 mJitEnabled = true;
1800 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1801 }
1802 }
1803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 void scheduleGcIdler() {
1805 if (!mGcIdlerScheduled) {
1806 mGcIdlerScheduled = true;
1807 Looper.myQueue().addIdleHandler(mGcIdler);
1808 }
1809 mH.removeMessages(H.GC_WHEN_IDLE);
1810 }
1811
1812 void unscheduleGcIdler() {
1813 if (mGcIdlerScheduled) {
1814 mGcIdlerScheduled = false;
1815 Looper.myQueue().removeIdleHandler(mGcIdler);
1816 }
1817 mH.removeMessages(H.GC_WHEN_IDLE);
1818 }
1819
1820 void doGcIfNeeded() {
1821 mGcIdlerScheduled = false;
1822 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001823 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 // + "m now=" + now);
1825 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001826 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 BinderInternal.forceGc("bg");
1828 }
1829 }
1830
Jeff Hamilton52d32032011-01-08 15:31:26 -06001831 public void registerOnActivityPausedListener(Activity activity,
1832 OnActivityPausedListener listener) {
1833 synchronized (mOnPauseListeners) {
1834 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1835 if (list == null) {
1836 list = new ArrayList<OnActivityPausedListener>();
1837 mOnPauseListeners.put(activity, list);
1838 }
1839 list.add(listener);
1840 }
1841 }
1842
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001843 public void unregisterOnActivityPausedListener(Activity activity,
1844 OnActivityPausedListener listener) {
1845 synchronized (mOnPauseListeners) {
1846 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1847 if (list != null) {
1848 list.remove(listener);
1849 }
1850 }
1851 }
1852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 public final ActivityInfo resolveActivityInfo(Intent intent) {
1854 ActivityInfo aInfo = intent.resolveActivityInfo(
1855 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1856 if (aInfo == null) {
1857 // Throw an exception.
1858 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001859 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 return aInfo;
1862 }
Bob Leee5408332009-09-04 18:31:17 -07001863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001866 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001867 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001869 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 r.intent = intent;
1871 r.state = state;
1872 r.parent = parent;
1873 r.embeddedID = id;
1874 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001875 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 if (localLOGV) {
1877 ComponentName compname = intent.getComponent();
1878 String name;
1879 if (compname != null) {
1880 name = compname.toShortString();
1881 } else {
1882 name = "(Intent " + intent + ").getComponent() returned null";
1883 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001884 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 + ", comp=" + name
1886 + ", token=" + token);
1887 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001888 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 }
1890
1891 public final Activity getActivity(IBinder token) {
1892 return mActivities.get(token).activity;
1893 }
1894
1895 public final void sendActivityResult(
1896 IBinder token, String id, int requestCode,
1897 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001898 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001899 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1901 list.add(new ResultInfo(id, requestCode, resultCode, data));
1902 mAppThread.scheduleSendResult(token, list);
1903 }
1904
1905 // if the thread hasn't started yet, we don't have the handler, so just
1906 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001907 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 queueOrSendMessage(what, obj, 0, 0);
1909 }
1910
Romain Guy65b345f2011-07-27 18:51:50 -07001911 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 queueOrSendMessage(what, obj, arg1, 0);
1913 }
1914
Romain Guy65b345f2011-07-27 18:51:50 -07001915 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001917 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
1919 + ": " + arg1 + " / " + obj);
1920 Message msg = Message.obtain();
1921 msg.what = what;
1922 msg.obj = obj;
1923 msg.arg1 = arg1;
1924 msg.arg2 = arg2;
1925 mH.sendMessage(msg);
1926 }
1927 }
1928
Dianne Hackborn21556372010-02-04 16:34:40 -08001929 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 String what) {
1931 ContextCleanupInfo cci = new ContextCleanupInfo();
1932 cci.context = context;
1933 cci.who = who;
1934 cci.what = what;
1935 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
1936 }
1937
Romain Guy65b345f2011-07-27 18:51:50 -07001938 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
1940
1941 ActivityInfo aInfo = r.activityInfo;
1942 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001943 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 Context.CONTEXT_INCLUDE_CODE);
1945 }
Bob Leee5408332009-09-04 18:31:17 -07001946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 ComponentName component = r.intent.getComponent();
1948 if (component == null) {
1949 component = r.intent.resolveActivity(
1950 mInitialApplication.getPackageManager());
1951 r.intent.setComponent(component);
1952 }
1953
1954 if (r.activityInfo.targetActivity != null) {
1955 component = new ComponentName(r.activityInfo.packageName,
1956 r.activityInfo.targetActivity);
1957 }
1958
1959 Activity activity = null;
1960 try {
1961 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
1962 activity = mInstrumentation.newActivity(
1963 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08001964 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 r.intent.setExtrasClassLoader(cl);
1966 if (r.state != null) {
1967 r.state.setClassLoader(cl);
1968 }
1969 } catch (Exception e) {
1970 if (!mInstrumentation.onException(activity, e)) {
1971 throw new RuntimeException(
1972 "Unable to instantiate activity " + component
1973 + ": " + e.toString(), e);
1974 }
1975 }
1976
1977 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08001978 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07001979
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001980 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
1981 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 TAG, r + ": app=" + app
1983 + ", appName=" + app.getPackageName()
1984 + ", pkg=" + r.packageInfo.getPackageName()
1985 + ", comp=" + r.intent.getComponent().toShortString()
1986 + ", dir=" + r.packageInfo.getAppDir());
1987
1988 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001989 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 appContext.init(r.packageInfo, r.token, this);
1991 appContext.setOuterContext(activity);
1992 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001993 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001994 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07001995 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001996 activity.attach(appContext, this, getInstrumentation(), r.token,
1997 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001998 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07001999
Christopher Tateb70f3df2009-04-07 16:07:59 -07002000 if (customIntent != null) {
2001 activity.mIntent = customIntent;
2002 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002003 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 activity.mStartedActivity = false;
2005 int theme = r.activityInfo.getThemeResource();
2006 if (theme != 0) {
2007 activity.setTheme(theme);
2008 }
2009
2010 activity.mCalled = false;
2011 mInstrumentation.callActivityOnCreate(activity, r.state);
2012 if (!activity.mCalled) {
2013 throw new SuperNotCalledException(
2014 "Activity " + r.intent.getComponent().toShortString() +
2015 " did not call through to super.onCreate()");
2016 }
2017 r.activity = activity;
2018 r.stopped = true;
2019 if (!r.activity.mFinished) {
2020 activity.performStart();
2021 r.stopped = false;
2022 }
2023 if (!r.activity.mFinished) {
2024 if (r.state != null) {
2025 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2026 }
2027 }
2028 if (!r.activity.mFinished) {
2029 activity.mCalled = false;
2030 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2031 if (!activity.mCalled) {
2032 throw new SuperNotCalledException(
2033 "Activity " + r.intent.getComponent().toShortString() +
2034 " did not call through to super.onPostCreate()");
2035 }
2036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 }
2038 r.paused = true;
2039
2040 mActivities.put(r.token, r);
2041
2042 } catch (SuperNotCalledException e) {
2043 throw e;
2044
2045 } catch (Exception e) {
2046 if (!mInstrumentation.onException(activity, e)) {
2047 throw new RuntimeException(
2048 "Unable to start activity " + component
2049 + ": " + e.toString(), e);
2050 }
2051 }
2052
2053 return activity;
2054 }
2055
Romain Guy65b345f2011-07-27 18:51:50 -07002056 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 // If we are getting ready to gc after going to the background, well
2058 // we are back active so skip it.
2059 unscheduleGcIdler();
2060
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002061 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002062 mProfiler.setProfiler(r.profileFile, r.profileFd);
2063 mProfiler.startProfiling();
2064 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002065 }
2066
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002067 // Make sure we are running with the most recent config.
2068 handleConfigurationChanged(null, null);
2069
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002070 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002072 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073
2074 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002075 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002076 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002077 handleResumeActivity(r.token, false, r.isForward);
2078
2079 if (!r.activity.mFinished && r.startsNotResumed) {
2080 // The activity manager actually wants this one to start out
2081 // paused, because it needs to be visible but isn't in the
2082 // foreground. We accomplish this by going through the
2083 // normal startup (because activities expect to go through
2084 // onResume() the first time they run, before their window
2085 // is displayed), and then pausing it. However, in this case
2086 // we do -not- need to do the full pause cycle (of freezing
2087 // and such) because the activity manager assumes it can just
2088 // retain the current state it has.
2089 try {
2090 r.activity.mCalled = false;
2091 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002092 // We need to keep around the original state, in case
2093 // we need to be created again.
2094 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 if (!r.activity.mCalled) {
2096 throw new SuperNotCalledException(
2097 "Activity " + r.intent.getComponent().toShortString() +
2098 " did not call through to super.onPause()");
2099 }
2100
2101 } catch (SuperNotCalledException e) {
2102 throw e;
2103
2104 } catch (Exception e) {
2105 if (!mInstrumentation.onException(r.activity, e)) {
2106 throw new RuntimeException(
2107 "Unable to pause activity "
2108 + r.intent.getComponent().toShortString()
2109 + ": " + e.toString(), e);
2110 }
2111 }
2112 r.paused = true;
2113 }
2114 } else {
2115 // If there was an error, for any reason, tell the activity
2116 // manager to stop us.
2117 try {
2118 ActivityManagerNative.getDefault()
2119 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2120 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002121 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 }
2123 }
2124 }
2125
Romain Guy65b345f2011-07-27 18:51:50 -07002126 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 List<Intent> intents) {
2128 final int N = intents.size();
2129 for (int i=0; i<N; i++) {
2130 Intent intent = intents.get(i);
2131 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002132 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2134 }
2135 }
2136
2137 public final void performNewIntents(IBinder token,
2138 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002139 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 if (r != null) {
2141 final boolean resumed = !r.paused;
2142 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002143 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 mInstrumentation.callActivityOnPause(r.activity);
2145 }
2146 deliverNewIntents(r, intents);
2147 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002148 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002149 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 }
2151 }
2152 }
Bob Leee5408332009-09-04 18:31:17 -07002153
Romain Guy65b345f2011-07-27 18:51:50 -07002154 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 performNewIntents(data.token, data.intents);
2156 }
2157
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002158 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2159
2160 /**
2161 * Return the Intent that's currently being handled by a
2162 * BroadcastReceiver on this thread, or null if none.
2163 * @hide
2164 */
2165 public static Intent getIntentBeingBroadcast() {
2166 return sCurrentBroadcastIntent.get();
2167 }
2168
Romain Guy65b345f2011-07-27 18:51:50 -07002169 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 // If we are getting ready to gc after going to the background, well
2171 // we are back active so skip it.
2172 unscheduleGcIdler();
2173
2174 String component = data.intent.getComponent().getClassName();
2175
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002176 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002177 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178
2179 IActivityManager mgr = ActivityManagerNative.getDefault();
2180
Romain Guy65b345f2011-07-27 18:51:50 -07002181 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002182 try {
2183 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2184 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002185 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2187 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002188 if (DEBUG_BROADCAST) Slog.i(TAG,
2189 "Finishing failed broadcast to " + data.intent.getComponent());
2190 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 throw new RuntimeException(
2192 "Unable to instantiate receiver " + component
2193 + ": " + e.toString(), e);
2194 }
2195
2196 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002197 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002198
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002199 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 TAG, "Performing receive of " + data.intent
2201 + ": app=" + app
2202 + ", appName=" + app.getPackageName()
2203 + ", pkg=" + packageInfo.getPackageName()
2204 + ", comp=" + data.intent.getComponent().toShortString()
2205 + ", dir=" + packageInfo.getAppDir());
2206
Dianne Hackborn21556372010-02-04 16:34:40 -08002207 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002208 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002209 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 receiver.onReceive(context.getReceiverRestrictedContext(),
2211 data.intent);
2212 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002213 if (DEBUG_BROADCAST) Slog.i(TAG,
2214 "Finishing failed broadcast to " + data.intent.getComponent());
2215 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 if (!mInstrumentation.onException(receiver, e)) {
2217 throw new RuntimeException(
2218 "Unable to start receiver " + component
2219 + ": " + e.toString(), e);
2220 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002221 } finally {
2222 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 }
2224
Dianne Hackborne829fef2010-10-26 17:44:01 -07002225 if (receiver.getPendingResult() != null) {
2226 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 }
2228 }
2229
Christopher Tate181fafa2009-05-14 11:12:14 -07002230 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002231 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002232 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002233
2234 // no longer idle; we have backup work to do
2235 unscheduleGcIdler();
2236
2237 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002238 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002239 String packageName = packageInfo.mPackageName;
2240 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002241 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002242 + " already exists");
2243 return;
2244 }
Bob Leee5408332009-09-04 18:31:17 -07002245
Christopher Tate181fafa2009-05-14 11:12:14 -07002246 BackupAgent agent = null;
2247 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002248
Christopher Tate79ec80d2011-06-24 14:58:49 -07002249 // full backup operation but no app-supplied agent? use the default implementation
2250 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2251 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002252 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002253 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002254
Christopher Tate181fafa2009-05-14 11:12:14 -07002255 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002256 IBinder binder = null;
2257 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002258 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2259
Christopher Tated1475e02009-07-09 15:36:17 -07002260 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002261 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002262
2263 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002264 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002265 context.init(packageInfo, null, this);
2266 context.setOuterContext(agent);
2267 agent.attach(context);
2268
2269 agent.onCreate();
2270 binder = agent.onBind();
2271 mBackupAgents.put(packageName, agent);
2272 } catch (Exception e) {
2273 // If this is during restore, fail silently; otherwise go
2274 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002275 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002276 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2277 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002278 throw e;
2279 }
2280 // falling through with 'binder' still null
2281 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002282
2283 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002284 try {
2285 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2286 } catch (RemoteException e) {
2287 // nothing to do.
2288 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002289 } catch (Exception e) {
2290 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002291 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002292 }
2293 }
2294
2295 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002296 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002297 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002298
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002299 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002300 String packageName = packageInfo.mPackageName;
2301 BackupAgent agent = mBackupAgents.get(packageName);
2302 if (agent != null) {
2303 try {
2304 agent.onDestroy();
2305 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002306 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002307 e.printStackTrace();
2308 }
2309 mBackupAgents.remove(packageName);
2310 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002311 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002312 }
2313 }
2314
Romain Guy65b345f2011-07-27 18:51:50 -07002315 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 // If we are getting ready to gc after going to the background, well
2317 // we are back active so skip it.
2318 unscheduleGcIdler();
2319
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002320 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002321 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 Service service = null;
2323 try {
2324 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2325 service = (Service) cl.loadClass(data.info.name).newInstance();
2326 } catch (Exception e) {
2327 if (!mInstrumentation.onException(service, e)) {
2328 throw new RuntimeException(
2329 "Unable to instantiate service " + data.info.name
2330 + ": " + e.toString(), e);
2331 }
2332 }
2333
2334 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002335 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336
Dianne Hackborn21556372010-02-04 16:34:40 -08002337 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 context.init(packageInfo, null, this);
2339
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002340 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 context.setOuterContext(service);
2342 service.attach(context, this, data.info.name, data.token, app,
2343 ActivityManagerNative.getDefault());
2344 service.onCreate();
2345 mServices.put(data.token, service);
2346 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002347 ActivityManagerNative.getDefault().serviceDoneExecuting(
2348 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 } catch (RemoteException e) {
2350 // nothing to do.
2351 }
2352 } catch (Exception e) {
2353 if (!mInstrumentation.onException(service, e)) {
2354 throw new RuntimeException(
2355 "Unable to create service " + data.info.name
2356 + ": " + e.toString(), e);
2357 }
2358 }
2359 }
2360
Romain Guy65b345f2011-07-27 18:51:50 -07002361 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002363 if (DEBUG_SERVICE)
2364 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 if (s != null) {
2366 try {
2367 data.intent.setExtrasClassLoader(s.getClassLoader());
2368 try {
2369 if (!data.rebind) {
2370 IBinder binder = s.onBind(data.intent);
2371 ActivityManagerNative.getDefault().publishService(
2372 data.token, data.intent, binder);
2373 } else {
2374 s.onRebind(data.intent);
2375 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002376 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002378 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 } catch (RemoteException ex) {
2380 }
2381 } catch (Exception e) {
2382 if (!mInstrumentation.onException(s, e)) {
2383 throw new RuntimeException(
2384 "Unable to bind to service " + s
2385 + " with " + data.intent + ": " + e.toString(), e);
2386 }
2387 }
2388 }
2389 }
2390
Romain Guy65b345f2011-07-27 18:51:50 -07002391 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 Service s = mServices.get(data.token);
2393 if (s != null) {
2394 try {
2395 data.intent.setExtrasClassLoader(s.getClassLoader());
2396 boolean doRebind = s.onUnbind(data.intent);
2397 try {
2398 if (doRebind) {
2399 ActivityManagerNative.getDefault().unbindFinished(
2400 data.token, data.intent, doRebind);
2401 } else {
2402 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002403 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 }
2405 } catch (RemoteException ex) {
2406 }
2407 } catch (Exception e) {
2408 if (!mInstrumentation.onException(s, e)) {
2409 throw new RuntimeException(
2410 "Unable to unbind to service " + s
2411 + " with " + data.intent + ": " + e.toString(), e);
2412 }
2413 }
2414 }
2415 }
2416
Dianne Hackborn625ac272010-09-17 18:29:22 -07002417 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002418 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2419 try {
2420 Service s = mServices.get(info.token);
2421 if (s != null) {
2422 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2423 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2424 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002426 } finally {
2427 IoUtils.closeQuietly(info.fd);
2428 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430 }
2431
Dianne Hackborn625ac272010-09-17 18:29:22 -07002432 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002433 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2434 try {
2435 ActivityClientRecord r = mActivities.get(info.token);
2436 if (r != null && r.activity != null) {
2437 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2438 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2439 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002440 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002441 } finally {
2442 IoUtils.closeQuietly(info.fd);
2443 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002444 }
2445 }
2446
Marco Nelissen18cb2872011-11-15 11:19:53 -08002447 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002448 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2449 try {
2450 ProviderClientRecord r = mLocalProviders.get(info.token);
2451 if (r != null && r.mLocalProvider != null) {
2452 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2453 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2454 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002455 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002456 } finally {
2457 IoUtils.closeQuietly(info.fd);
2458 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002459 }
2460 }
2461
Romain Guy65b345f2011-07-27 18:51:50 -07002462 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 Service s = mServices.get(data.token);
2464 if (s != null) {
2465 try {
2466 if (data.args != null) {
2467 data.args.setExtrasClassLoader(s.getClassLoader());
2468 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002469 int res;
2470 if (!data.taskRemoved) {
2471 res = s.onStartCommand(data.args, data.flags, data.startId);
2472 } else {
2473 s.onTaskRemoved(data.args);
2474 res = Service.START_TASK_REMOVED_COMPLETE;
2475 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002476
2477 QueuedWork.waitToFinish();
2478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002480 ActivityManagerNative.getDefault().serviceDoneExecuting(
2481 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 } catch (RemoteException e) {
2483 // nothing to do.
2484 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002485 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002486 } catch (Exception e) {
2487 if (!mInstrumentation.onException(s, e)) {
2488 throw new RuntimeException(
2489 "Unable to start service " + s
2490 + " with " + data.args + ": " + e.toString(), e);
2491 }
2492 }
2493 }
2494 }
2495
Romain Guy65b345f2011-07-27 18:51:50 -07002496 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 Service s = mServices.remove(token);
2498 if (s != null) {
2499 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002500 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 s.onDestroy();
2502 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002503 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002505 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002507
2508 QueuedWork.waitToFinish();
2509
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002511 ActivityManagerNative.getDefault().serviceDoneExecuting(
2512 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002513 } catch (RemoteException e) {
2514 // nothing to do.
2515 }
2516 } catch (Exception e) {
2517 if (!mInstrumentation.onException(s, e)) {
2518 throw new RuntimeException(
2519 "Unable to stop service " + s
2520 + ": " + e.toString(), e);
2521 }
2522 }
2523 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002524 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 }
2526
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002527 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002529 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002530 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 + " finished=" + r.activity.mFinished);
2532 if (r != null && !r.activity.mFinished) {
2533 if (clearHide) {
2534 r.hideForNow = false;
2535 r.activity.mStartedActivity = false;
2536 }
2537 try {
2538 if (r.pendingIntents != null) {
2539 deliverNewIntents(r, r.pendingIntents);
2540 r.pendingIntents = null;
2541 }
2542 if (r.pendingResults != null) {
2543 deliverResults(r, r.pendingResults);
2544 r.pendingResults = null;
2545 }
2546 r.activity.performResume();
2547
Bob Leee5408332009-09-04 18:31:17 -07002548 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 r.paused = false;
2552 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 r.state = null;
2554 } catch (Exception e) {
2555 if (!mInstrumentation.onException(r.activity, e)) {
2556 throw new RuntimeException(
2557 "Unable to resume activity "
2558 + r.intent.getComponent().toShortString()
2559 + ": " + e.toString(), e);
2560 }
2561 }
2562 }
2563 return r;
2564 }
2565
Romain Guya998dff2012-03-23 18:58:36 -07002566 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002567 if (r.mPendingRemoveWindow != null) {
2568 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2569 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2570 if (wtoken != null) {
2571 WindowManagerImpl.getDefault().closeAll(wtoken,
2572 r.activity.getClass().getName(), "Activity");
2573 }
2574 }
2575 r.mPendingRemoveWindow = null;
2576 r.mPendingRemoveWindowManager = null;
2577 }
2578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2580 // If we are getting ready to gc after going to the background, well
2581 // we are back active so skip it.
2582 unscheduleGcIdler();
2583
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002584 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585
2586 if (r != null) {
2587 final Activity a = r.activity;
2588
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002589 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 TAG, "Resume " + r + " started activity: " +
2591 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2592 + ", finished: " + a.mFinished);
2593
2594 final int forwardBit = isForward ?
2595 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 // If the window hasn't yet been added to the window manager,
2598 // and this guy didn't finish itself or start another activity,
2599 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002600 boolean willBeVisible = !a.mStartedActivity;
2601 if (!willBeVisible) {
2602 try {
2603 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2604 a.getActivityToken());
2605 } catch (RemoteException e) {
2606 }
2607 }
2608 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 r.window = r.activity.getWindow();
2610 View decor = r.window.getDecorView();
2611 decor.setVisibility(View.INVISIBLE);
2612 ViewManager wm = a.getWindowManager();
2613 WindowManager.LayoutParams l = r.window.getAttributes();
2614 a.mDecor = decor;
2615 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2616 l.softInputMode |= forwardBit;
2617 if (a.mVisibleFromClient) {
2618 a.mWindowAdded = true;
2619 wm.addView(decor, l);
2620 }
2621
2622 // If the window has already been added, but during resume
2623 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002624 // window visible.
2625 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002626 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 TAG, "Launch " + r + " mStartedActivity set");
2628 r.hideForNow = true;
2629 }
2630
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002631 // Get rid of anything left hanging around.
2632 cleanUpPendingRemoveWindows(r);
2633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634 // The window is now visible if it has been added, we are not
2635 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002636 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002637 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002638 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002639 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002640 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641 performConfigurationChanged(r.activity, r.newConfig);
2642 r.newConfig = null;
2643 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002644 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 + isForward);
2646 WindowManager.LayoutParams l = r.window.getAttributes();
2647 if ((l.softInputMode
2648 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2649 != forwardBit) {
2650 l.softInputMode = (l.softInputMode
2651 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2652 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002653 if (r.activity.mVisibleFromClient) {
2654 ViewManager wm = a.getWindowManager();
2655 View decor = r.window.getDecorView();
2656 wm.updateViewLayout(decor, l);
2657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
2659 r.activity.mVisibleFromServer = true;
2660 mNumVisibleActivities++;
2661 if (r.activity.mVisibleFromClient) {
2662 r.activity.makeVisible();
2663 }
2664 }
2665
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002666 if (!r.onlyLocalRequest) {
2667 r.nextIdle = mNewActivities;
2668 mNewActivities = r;
2669 if (localLOGV) Slog.v(
2670 TAG, "Scheduling idle handler for " + r);
2671 Looper.myQueue().addIdleHandler(new Idler());
2672 }
2673 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674
2675 } else {
2676 // If an exception was thrown when trying to resume, then
2677 // just end this activity.
2678 try {
2679 ActivityManagerNative.getDefault()
2680 .finishActivity(token, Activity.RESULT_CANCELED, null);
2681 } catch (RemoteException ex) {
2682 }
2683 }
2684 }
2685
2686 private int mThumbnailWidth = -1;
2687 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002688 private Bitmap mAvailThumbnailBitmap = null;
2689 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690
Romain Guy65b345f2011-07-27 18:51:50 -07002691 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002692 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002694 if (thumbnail == null) {
2695 int w = mThumbnailWidth;
2696 int h;
2697 if (w < 0) {
2698 Resources res = r.activity.getResources();
2699 mThumbnailHeight = h =
2700 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002702 mThumbnailWidth = w =
2703 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2704 } else {
2705 h = mThumbnailHeight;
2706 }
2707
2708 // On platforms where we don't want thumbnails, set dims to (0,0)
2709 if ((w > 0) && (h > 0)) {
2710 thumbnail = Bitmap.createBitmap(w, h, THUMBNAIL_FORMAT);
2711 thumbnail.eraseColor(0);
2712 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002713 }
2714
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002715 if (thumbnail != null) {
2716 Canvas cv = mThumbnailCanvas;
2717 if (cv == null) {
2718 mThumbnailCanvas = cv = new Canvas();
2719 }
2720
2721 cv.setBitmap(thumbnail);
2722 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2723 mAvailThumbnailBitmap = thumbnail;
2724 thumbnail = null;
2725 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002726 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 } catch (Exception e) {
2730 if (!mInstrumentation.onException(r.activity, e)) {
2731 throw new RuntimeException(
2732 "Unable to create thumbnail of "
2733 + r.intent.getComponent().toShortString()
2734 + ": " + e.toString(), e);
2735 }
2736 thumbnail = null;
2737 }
2738
2739 return thumbnail;
2740 }
2741
Romain Guy65b345f2011-07-27 18:51:50 -07002742 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002744 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002746 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 if (userLeaving) {
2748 performUserLeavingActivity(r);
2749 }
Bob Leee5408332009-09-04 18:31:17 -07002750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002752 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002754 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002755 if (r.isPreHoneycomb()) {
2756 QueuedWork.waitToFinish();
2757 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 // Tell the activity manager we have paused.
2760 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002761 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 } catch (RemoteException ex) {
2763 }
2764 }
2765 }
2766
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002767 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 mInstrumentation.callActivityOnUserLeaving(r.activity);
2769 }
2770
2771 final Bundle performPauseActivity(IBinder token, boolean finished,
2772 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002773 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 return r != null ? performPauseActivity(r, finished, saveState) : null;
2775 }
2776
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002777 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 boolean saveState) {
2779 if (r.paused) {
2780 if (r.activity.mFinished) {
2781 // If we are finishing, we won't call onResume() in certain cases.
2782 // So here we likewise don't want to call onPause() if the activity
2783 // isn't resumed.
2784 return null;
2785 }
2786 RuntimeException e = new RuntimeException(
2787 "Performing pause of activity that is not resumed: "
2788 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002789 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
2791 Bundle state = null;
2792 if (finished) {
2793 r.activity.mFinished = true;
2794 }
2795 try {
2796 // Next have the activity save its current state and managed dialogs...
2797 if (!r.activity.mFinished && saveState) {
2798 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002799 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2801 r.state = state;
2802 }
2803 // Now we are idle.
2804 r.activity.mCalled = false;
2805 mInstrumentation.callActivityOnPause(r.activity);
2806 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2807 if (!r.activity.mCalled) {
2808 throw new SuperNotCalledException(
2809 "Activity " + r.intent.getComponent().toShortString() +
2810 " did not call through to super.onPause()");
2811 }
2812
2813 } catch (SuperNotCalledException e) {
2814 throw e;
2815
2816 } catch (Exception e) {
2817 if (!mInstrumentation.onException(r.activity, e)) {
2818 throw new RuntimeException(
2819 "Unable to pause activity "
2820 + r.intent.getComponent().toShortString()
2821 + ": " + e.toString(), e);
2822 }
2823 }
2824 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002825
2826 // Notify any outstanding on paused listeners
2827 ArrayList<OnActivityPausedListener> listeners;
2828 synchronized (mOnPauseListeners) {
2829 listeners = mOnPauseListeners.remove(r.activity);
2830 }
2831 int size = (listeners != null ? listeners.size() : 0);
2832 for (int i = 0; i < size; i++) {
2833 listeners.get(i).onPaused(r.activity);
2834 }
2835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 return state;
2837 }
2838
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002839 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002840 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002841 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002844 private static class StopInfo implements Runnable {
2845 ActivityClientRecord activity;
2846 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002847 Bitmap thumbnail;
2848 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002849
2850 @Override public void run() {
2851 // Tell activity manager we have been stopped.
2852 try {
2853 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2854 ActivityManagerNative.getDefault().activityStopped(
2855 activity.token, state, thumbnail, description);
2856 } catch (RemoteException ex) {
2857 }
2858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002859 }
2860
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002861 private static final class ProviderRefCount {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 public int count;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002863
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864 ProviderRefCount(int pCount) {
2865 count = pCount;
2866 }
2867 }
2868
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002869 /**
2870 * Core implementation of stopping an activity. Note this is a little
2871 * tricky because the server's meaning of stop is slightly different
2872 * than our client -- for the server, stop means to save state and give
2873 * it the result when it is done, but the window may still be visible.
2874 * For the client, we want to call onStop()/onStart() to indicate when
2875 * the activity's UI visibillity changes.
2876 */
Romain Guy65b345f2011-07-27 18:51:50 -07002877 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002878 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002879 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002880 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881 if (r != null) {
2882 if (!keepShown && r.stopped) {
2883 if (r.activity.mFinished) {
2884 // If we are finishing, we won't call onResume() in certain
2885 // cases. So here we likewise don't want to call onStop()
2886 // if the activity isn't resumed.
2887 return;
2888 }
2889 RuntimeException e = new RuntimeException(
2890 "Performing stop of activity that is not resumed: "
2891 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002892 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 }
2894
2895 if (info != null) {
2896 try {
2897 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07002898 // For now, don't create the thumbnail here; we are
2899 // doing that by doing a screen snapshot.
2900 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 info.description = r.activity.onCreateDescription();
2902 } catch (Exception e) {
2903 if (!mInstrumentation.onException(r.activity, e)) {
2904 throw new RuntimeException(
2905 "Unable to save state of activity "
2906 + r.intent.getComponent().toShortString()
2907 + ": " + e.toString(), e);
2908 }
2909 }
2910 }
2911
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002912 // Next have the activity save its current state and managed dialogs...
2913 if (!r.activity.mFinished && saveState) {
2914 if (r.state == null) {
2915 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002916 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002917 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2918 r.state = state;
2919 } else {
2920 state = r.state;
2921 }
2922 }
2923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 if (!keepShown) {
2925 try {
2926 // Now we are idle.
2927 r.activity.performStop();
2928 } catch (Exception e) {
2929 if (!mInstrumentation.onException(r.activity, e)) {
2930 throw new RuntimeException(
2931 "Unable to stop activity "
2932 + r.intent.getComponent().toShortString()
2933 + ": " + e.toString(), e);
2934 }
2935 }
2936 r.stopped = true;
2937 }
2938
2939 r.paused = true;
2940 }
2941 }
2942
Romain Guy65b345f2011-07-27 18:51:50 -07002943 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 View v = r.activity.mDecor;
2945 if (v != null) {
2946 if (show) {
2947 if (!r.activity.mVisibleFromServer) {
2948 r.activity.mVisibleFromServer = true;
2949 mNumVisibleActivities++;
2950 if (r.activity.mVisibleFromClient) {
2951 r.activity.makeVisible();
2952 }
2953 }
2954 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002955 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002956 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 performConfigurationChanged(r.activity, r.newConfig);
2958 r.newConfig = null;
2959 }
2960 } else {
2961 if (r.activity.mVisibleFromServer) {
2962 r.activity.mVisibleFromServer = false;
2963 mNumVisibleActivities--;
2964 v.setVisibility(View.INVISIBLE);
2965 }
2966 }
2967 }
2968 }
2969
Romain Guy65b345f2011-07-27 18:51:50 -07002970 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002971 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 r.activity.mConfigChangeFlags |= configChanges;
2973
2974 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002975 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002977 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 TAG, "Finishing stop of " + r + ": show=" + show
2979 + " win=" + r.window);
2980
2981 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07002982
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002983 // Make sure any pending writes are now committed.
2984 if (!r.isPreHoneycomb()) {
2985 QueuedWork.waitToFinish();
2986 }
2987
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002988 // Schedule the call to tell the activity manager we have
2989 // stopped. We don't do this immediately, because we want to
2990 // have a chance for any other pending work (in particular memory
2991 // trim requests) to complete before you tell the activity
2992 // manager to proceed and allow us to go fully into the background.
2993 info.activity = r;
2994 info.state = r.state;
2995 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 }
2997
2998 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002999 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 if (r.stopped) {
3001 r.activity.performRestart();
3002 r.stopped = false;
3003 }
3004 }
3005
Romain Guy65b345f2011-07-27 18:51:50 -07003006 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003007 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003008
3009 if (r == null) {
3010 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3011 return;
3012 }
3013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003015 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 } else if (show && r.stopped) {
3017 // If we are getting ready to gc after going to the background, well
3018 // we are back active so skip it.
3019 unscheduleGcIdler();
3020
3021 r.activity.performRestart();
3022 r.stopped = false;
3023 }
3024 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003025 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 TAG, "Handle window " + r + " visibility: " + show);
3027 updateVisibility(r, show);
3028 }
3029 }
3030
Romain Guy65b345f2011-07-27 18:51:50 -07003031 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003032 ActivityClientRecord r = mActivities.get(token);
3033
3034 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003035 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003036 return;
3037 }
3038
3039 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003040 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003041 try {
3042 // Now we are idle.
3043 r.activity.performStop();
3044 } catch (Exception e) {
3045 if (!mInstrumentation.onException(r.activity, e)) {
3046 throw new RuntimeException(
3047 "Unable to stop activity "
3048 + r.intent.getComponent().toShortString()
3049 + ": " + e.toString(), e);
3050 }
3051 }
3052 r.stopped = true;
3053 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003054
3055 // Make sure any pending writes are now committed.
3056 if (!r.isPreHoneycomb()) {
3057 QueuedWork.waitToFinish();
3058 }
3059
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003060 // Tell activity manager we slept.
3061 try {
3062 ActivityManagerNative.getDefault().activitySlept(r.token);
3063 } catch (RemoteException ex) {
3064 }
3065 } else {
3066 if (r.stopped && r.activity.mVisibleFromServer) {
3067 r.activity.performRestart();
3068 r.stopped = false;
3069 }
3070 }
3071 }
3072
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003073 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003074 synchronized (mPackages) {
3075 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003076 }
3077 }
3078
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003079 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3080 LoadedApk apk = peekPackageInfo(data.pkg, false);
3081 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003082 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003083 }
3084 apk = peekPackageInfo(data.pkg, true);
3085 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003086 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003087 }
3088 handleConfigurationChanged(mConfiguration, data.info);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003089 WindowManagerImpl.getDefault().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003090 }
3091
Romain Guy65b345f2011-07-27 18:51:50 -07003092 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 final int N = results.size();
3094 for (int i=0; i<N; i++) {
3095 ResultInfo ri = results.get(i);
3096 try {
3097 if (ri.mData != null) {
3098 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3099 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003100 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003101 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 r.activity.dispatchActivityResult(ri.mResultWho,
3103 ri.mRequestCode, ri.mResultCode, ri.mData);
3104 } catch (Exception e) {
3105 if (!mInstrumentation.onException(r.activity, e)) {
3106 throw new RuntimeException(
3107 "Failure delivering result " + ri + " to activity "
3108 + r.intent.getComponent().toShortString()
3109 + ": " + e.toString(), e);
3110 }
3111 }
3112 }
3113 }
3114
Romain Guy65b345f2011-07-27 18:51:50 -07003115 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003116 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003117 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 if (r != null) {
3119 final boolean resumed = !r.paused;
3120 if (!r.activity.mFinished && r.activity.mDecor != null
3121 && r.hideForNow && resumed) {
3122 // We had hidden the activity because it started another
3123 // one... we have gotten a result back and we are not
3124 // paused, so make sure our window is visible.
3125 updateVisibility(r, true);
3126 }
3127 if (resumed) {
3128 try {
3129 // Now we are idle.
3130 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003131 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 mInstrumentation.callActivityOnPause(r.activity);
3133 if (!r.activity.mCalled) {
3134 throw new SuperNotCalledException(
3135 "Activity " + r.intent.getComponent().toShortString()
3136 + " did not call through to super.onPause()");
3137 }
3138 } catch (SuperNotCalledException e) {
3139 throw e;
3140 } catch (Exception e) {
3141 if (!mInstrumentation.onException(r.activity, e)) {
3142 throw new RuntimeException(
3143 "Unable to pause activity "
3144 + r.intent.getComponent().toShortString()
3145 + ": " + e.toString(), e);
3146 }
3147 }
3148 }
3149 deliverResults(r, res.results);
3150 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003151 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003152 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 }
3154 }
3155 }
3156
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003157 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 return performDestroyActivity(token, finishing, 0, false);
3159 }
3160
Romain Guy65b345f2011-07-27 18:51:50 -07003161 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003163 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003164 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003165 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003167 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168 r.activity.mConfigChangeFlags |= configChanges;
3169 if (finishing) {
3170 r.activity.mFinished = true;
3171 }
3172 if (!r.paused) {
3173 try {
3174 r.activity.mCalled = false;
3175 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003176 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 r.activity.getComponentName().getClassName());
3178 if (!r.activity.mCalled) {
3179 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003180 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 + " did not call through to super.onPause()");
3182 }
3183 } catch (SuperNotCalledException e) {
3184 throw e;
3185 } catch (Exception e) {
3186 if (!mInstrumentation.onException(r.activity, e)) {
3187 throw new RuntimeException(
3188 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003189 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 + ": " + e.toString(), e);
3191 }
3192 }
3193 r.paused = true;
3194 }
3195 if (!r.stopped) {
3196 try {
3197 r.activity.performStop();
3198 } catch (SuperNotCalledException e) {
3199 throw e;
3200 } catch (Exception e) {
3201 if (!mInstrumentation.onException(r.activity, e)) {
3202 throw new RuntimeException(
3203 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003204 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 + ": " + e.toString(), e);
3206 }
3207 }
3208 r.stopped = true;
3209 }
3210 if (getNonConfigInstance) {
3211 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003212 r.lastNonConfigurationInstances
3213 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 } catch (Exception e) {
3215 if (!mInstrumentation.onException(r.activity, e)) {
3216 throw new RuntimeException(
3217 "Unable to retain activity "
3218 + r.intent.getComponent().toShortString()
3219 + ": " + e.toString(), e);
3220 }
3221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 }
3223 try {
3224 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003225 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 if (!r.activity.mCalled) {
3227 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003228 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 " did not call through to super.onDestroy()");
3230 }
3231 if (r.window != null) {
3232 r.window.closeAllPanels();
3233 }
3234 } catch (SuperNotCalledException e) {
3235 throw e;
3236 } catch (Exception e) {
3237 if (!mInstrumentation.onException(r.activity, e)) {
3238 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003239 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3240 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
3242 }
3243 }
3244 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003245 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 return r;
3247 }
3248
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003249 private static String safeToComponentShortString(Intent intent) {
3250 ComponentName component = intent.getComponent();
3251 return component == null ? "[Unknown]" : component.toShortString();
3252 }
3253
Romain Guy65b345f2011-07-27 18:51:50 -07003254 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003256 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 configChanges, getNonConfigInstance);
3258 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003259 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 WindowManager wm = r.activity.getWindowManager();
3261 View v = r.activity.mDecor;
3262 if (v != null) {
3263 if (r.activity.mVisibleFromServer) {
3264 mNumVisibleActivities--;
3265 }
3266 IBinder wtoken = v.getWindowToken();
3267 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003268 if (r.onlyLocalRequest) {
3269 // Hold off on removing this until the new activity's
3270 // window is being added.
3271 r.mPendingRemoveWindow = v;
3272 r.mPendingRemoveWindowManager = wm;
3273 } else {
3274 wm.removeViewImmediate(v);
3275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003277 if (wtoken != null && r.mPendingRemoveWindow == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 WindowManagerImpl.getDefault().closeAll(wtoken,
3279 r.activity.getClass().getName(), "Activity");
3280 }
3281 r.activity.mDecor = null;
3282 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003283 if (r.mPendingRemoveWindow == null) {
3284 // If we are delaying the removal of the activity window, then
3285 // we can't clean up all windows here. Note that we can't do
3286 // so later either, which means any windows that aren't closed
3287 // by the app will leak. Well we try to warning them a lot
3288 // about leaking windows, because that is a bug, so if they are
3289 // using this recreate facility then they get to live with leaks.
3290 WindowManagerImpl.getDefault().closeAll(token,
3291 r.activity.getClass().getName(), "Activity");
3292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293
3294 // Mocked out contexts won't be participating in the normal
3295 // process lifecycle, but if we're running with a proper
3296 // ApplicationContext we need to have it tear down things
3297 // cleanly.
3298 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003299 if (c instanceof ContextImpl) {
3300 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 r.activity.getClass().getName(), "Activity");
3302 }
3303 }
3304 if (finishing) {
3305 try {
3306 ActivityManagerNative.getDefault().activityDestroyed(token);
3307 } catch (RemoteException ex) {
3308 // If the system process has died, it's game over for everyone.
3309 }
3310 }
3311 }
3312
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003313 public final void requestRelaunchActivity(IBinder token,
3314 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3315 int configChanges, boolean notResumed, Configuration config,
3316 boolean fromServer) {
3317 ActivityClientRecord target = null;
3318
3319 synchronized (mPackages) {
3320 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3321 ActivityClientRecord r = mRelaunchingActivities.get(i);
3322 if (r.token == token) {
3323 target = r;
3324 if (pendingResults != null) {
3325 if (r.pendingResults != null) {
3326 r.pendingResults.addAll(pendingResults);
3327 } else {
3328 r.pendingResults = pendingResults;
3329 }
3330 }
3331 if (pendingNewIntents != null) {
3332 if (r.pendingIntents != null) {
3333 r.pendingIntents.addAll(pendingNewIntents);
3334 } else {
3335 r.pendingIntents = pendingNewIntents;
3336 }
3337 }
3338 break;
3339 }
3340 }
3341
3342 if (target == null) {
3343 target = new ActivityClientRecord();
3344 target.token = token;
3345 target.pendingResults = pendingResults;
3346 target.pendingIntents = pendingNewIntents;
3347 if (!fromServer) {
3348 ActivityClientRecord existing = mActivities.get(token);
3349 if (existing != null) {
3350 target.startsNotResumed = existing.paused;
3351 }
3352 target.onlyLocalRequest = true;
3353 }
3354 mRelaunchingActivities.add(target);
3355 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3356 }
3357
3358 if (fromServer) {
3359 target.startsNotResumed = notResumed;
3360 target.onlyLocalRequest = false;
3361 }
3362 if (config != null) {
3363 target.createdConfig = config;
3364 }
3365 target.pendingConfigChanges |= configChanges;
3366 }
3367 }
3368
Romain Guy65b345f2011-07-27 18:51:50 -07003369 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 // If we are getting ready to gc after going to the background, well
3371 // we are back active so skip it.
3372 unscheduleGcIdler();
3373
3374 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003375 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 // First: make sure we have the most recent configuration and most
3378 // recent version of the activity, or skip it if some previous call
3379 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003380 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 int N = mRelaunchingActivities.size();
3382 IBinder token = tmp.token;
3383 tmp = null;
3384 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003385 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 if (r.token == token) {
3387 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003388 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 mRelaunchingActivities.remove(i);
3390 i--;
3391 N--;
3392 }
3393 }
Bob Leee5408332009-09-04 18:31:17 -07003394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003396 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 return;
3398 }
Bob Leee5408332009-09-04 18:31:17 -07003399
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003400 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3401 + tmp.token + " with configChanges=0x"
3402 + Integer.toHexString(configChanges));
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 if (mPendingConfiguration != null) {
3405 changedConfig = mPendingConfiguration;
3406 mPendingConfiguration = null;
3407 }
3408 }
Bob Leee5408332009-09-04 18:31:17 -07003409
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003410 if (tmp.createdConfig != null) {
3411 // If the activity manager is passing us its current config,
3412 // assume that is really what we want regardless of what we
3413 // may have pending.
3414 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003415 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3416 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3417 if (changedConfig == null
3418 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3419 changedConfig = tmp.createdConfig;
3420 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003421 }
3422 }
3423
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003424 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003425 + tmp.token + ": changedConfig=" + changedConfig);
3426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 // If there was a pending configuration change, execute it first.
3428 if (changedConfig != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003429 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 }
Bob Leee5408332009-09-04 18:31:17 -07003431
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003432 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003433 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 if (r == null) {
3435 return;
3436 }
Bob Leee5408332009-09-04 18:31:17 -07003437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003439 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003440 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003441
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003442 r.activity.mChangingConfigurations = true;
3443
Dianne Hackborne2b04802010-12-09 09:24:55 -08003444 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003446 performPauseActivity(r.token, false, r.isPreHoneycomb());
3447 }
3448 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3449 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003450 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003451 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 }
Bob Leee5408332009-09-04 18:31:17 -07003453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 r.activity = null;
3457 r.window = null;
3458 r.hideForNow = false;
3459 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003460 // Merge any pending results and pending intents; don't just replace them
3461 if (tmp.pendingResults != null) {
3462 if (r.pendingResults == null) {
3463 r.pendingResults = tmp.pendingResults;
3464 } else {
3465 r.pendingResults.addAll(tmp.pendingResults);
3466 }
3467 }
3468 if (tmp.pendingIntents != null) {
3469 if (r.pendingIntents == null) {
3470 r.pendingIntents = tmp.pendingIntents;
3471 } else {
3472 r.pendingIntents.addAll(tmp.pendingIntents);
3473 }
3474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003476
Christopher Tateb70f3df2009-04-07 16:07:59 -07003477 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 }
3479
Romain Guy65b345f2011-07-27 18:51:50 -07003480 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003481 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 Bitmap thumbnail = createThumbnailBitmap(r);
3483 CharSequence description = null;
3484 try {
3485 description = r.activity.onCreateDescription();
3486 } catch (Exception e) {
3487 if (!mInstrumentation.onException(r.activity, e)) {
3488 throw new RuntimeException(
3489 "Unable to create description of activity "
3490 + r.intent.getComponent().toShortString()
3491 + ": " + e.toString(), e);
3492 }
3493 }
3494 //System.out.println("Reporting top thumbnail " + thumbnail);
3495 try {
3496 ActivityManagerNative.getDefault().reportThumbnail(
3497 token, thumbnail, description);
3498 } catch (RemoteException ex) {
3499 }
3500 }
3501
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003502 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003504 ArrayList<ComponentCallbacks2> callbacks
3505 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003507 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003508 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 Activity a = ar.activity;
3510 if (a != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003511 Configuration thisConfig = applyConfigCompatMainThread(newConfig,
3512 ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003513 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 // If the activity is currently resumed, its configuration
3515 // needs to change right now.
3516 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003517 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 // Otherwise, we will tell it about the change
3519 // the next time it is resumed or shown. Note that
3520 // the activity manager may, before then, decide the
3521 // activity needs to be destroyed to handle its new
3522 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003523 if (DEBUG_CONFIGURATION) {
3524 Slog.v(TAG, "Setting activity "
3525 + ar.activityInfo.name + " newConfig=" + thisConfig);
3526 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003527 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 }
3529 }
3530 }
3531 }
3532 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003533 for (Service service : mServices.values()) {
3534 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 }
3536 }
3537 synchronized (mProviderMap) {
3538 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003539 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3540 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 }
3542 }
3543 }
3544 final int N = mAllApplications.size();
3545 for (int i=0; i<N; i++) {
3546 callbacks.add(mAllApplications.get(i));
3547 }
Bob Leee5408332009-09-04 18:31:17 -07003548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003549 return callbacks;
3550 }
Bob Leee5408332009-09-04 18:31:17 -07003551
Romain Guya998dff2012-03-23 18:58:36 -07003552 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003554 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 // we check the runtime type and act accordingly.
3556 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3557 if (activity != null) {
3558 activity.mCalled = false;
3559 }
Bob Leee5408332009-09-04 18:31:17 -07003560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 boolean shouldChangeConfig = false;
3562 if ((activity == null) || (activity.mCurrentConfig == null)) {
3563 shouldChangeConfig = true;
3564 } else {
Bob Leee5408332009-09-04 18:31:17 -07003565
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 // If the new config is the same as the config this Activity
3567 // is already running with then don't bother calling
3568 // onConfigurationChanged
3569 int diff = activity.mCurrentConfig.diff(config);
3570 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003571 // If this activity doesn't handle any of the config changes
3572 // then don't bother calling onConfigurationChanged as we're
3573 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003574 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 shouldChangeConfig = true;
3576 }
3577 }
3578 }
Bob Leee5408332009-09-04 18:31:17 -07003579
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003580 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003581 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 if (shouldChangeConfig) {
3583 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 if (activity != null) {
3586 if (!activity.mCalled) {
3587 throw new SuperNotCalledException(
3588 "Activity " + activity.getLocalClassName() +
3589 " did not call through to super.onConfigurationChanged()");
3590 }
3591 activity.mConfigChangeFlags = 0;
3592 activity.mCurrentConfig = new Configuration(config);
3593 }
3594 }
3595 }
3596
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003597 public final void applyConfigurationToResources(Configuration config) {
3598 synchronized (mPackages) {
3599 applyConfigurationToResourcesLocked(config, null);
3600 }
3601 }
3602
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003603 final boolean applyConfigurationToResourcesLocked(Configuration config,
3604 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003605 if (mResConfiguration == null) {
3606 mResConfiguration = new Configuration();
3607 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003608 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003609 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003610 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003611 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003612 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003613 int changes = mResConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003614 DisplayMetrics dm = getDisplayMetricsLocked(null, true);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003615
3616 if (compat != null && (mResCompatibilityInfo == null ||
3617 !mResCompatibilityInfo.equals(compat))) {
3618 mResCompatibilityInfo = compat;
3619 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3620 | ActivityInfo.CONFIG_SCREEN_SIZE
3621 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3622 }
Bob Leee5408332009-09-04 18:31:17 -07003623
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003624 // set it for java, this also affects newly created Resources
3625 if (config.locale != null) {
3626 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 }
Bob Leee5408332009-09-04 18:31:17 -07003628
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003629 Resources.updateSystemConfiguration(config, dm, compat);
Bob Leee5408332009-09-04 18:31:17 -07003630
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003631 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003632 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003633
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003634 Iterator<WeakReference<Resources>> it =
3635 mActiveResources.values().iterator();
3636 //Iterator<Map.Entry<String, WeakReference<Resources>>> it =
3637 // mActiveResources.entrySet().iterator();
3638 while (it.hasNext()) {
3639 WeakReference<Resources> v = it.next();
3640 Resources r = v.get();
3641 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003642 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003643 + r + " config to: " + config);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003644 r.updateConfiguration(config, dm, compat);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003645 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003646 // + " " + r + ": " + r.getConfiguration());
3647 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003648 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003649 it.remove();
3650 }
3651 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003652
3653 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003654 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003655
3656 final Configuration applyCompatConfiguration() {
3657 Configuration config = mConfiguration;
3658 if (mCompatConfiguration == null) {
3659 mCompatConfiguration = new Configuration();
3660 }
3661 mCompatConfiguration.setTo(mConfiguration);
3662 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
3663 mResCompatibilityInfo.applyToConfiguration(mCompatConfiguration);
3664 config = mCompatConfiguration;
3665 }
3666 return config;
3667 }
3668
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003669 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003670
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003671 ArrayList<ComponentCallbacks2> callbacks = null;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003672
3673 synchronized (mPackages) {
3674 if (mPendingConfiguration != null) {
3675 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3676 config = mPendingConfiguration;
3677 }
3678 mPendingConfiguration = null;
3679 }
3680
3681 if (config == null) {
3682 return;
3683 }
3684
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003685 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003686 + config);
3687
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003688 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 if (mConfiguration == null) {
3691 mConfiguration = new Configuration();
3692 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003693 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003694 return;
3695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 mConfiguration.updateFrom(config);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003697 config = applyCompatConfiguration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 callbacks = collectComponentCallbacksLocked(false, config);
3699 }
Romain Guy65b345f2011-07-27 18:51:50 -07003700
3701 // Cleanup hardware accelerated stuff
3702 WindowManagerImpl.getDefault().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003703
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003704 if (callbacks != null) {
3705 final int N = callbacks.size();
3706 for (int i=0; i<N; i++) {
3707 performConfigurationChanged(callbacks.get(i), config);
3708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 }
3710 }
3711
3712 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003713 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 if (r == null || r.activity == null) {
3715 return;
3716 }
Bob Leee5408332009-09-04 18:31:17 -07003717
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003718 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003719 + r.activityInfo.name);
3720
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003721 performConfigurationChanged(r.activity, mCompatConfiguration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723
Romain Guy7eabe552011-07-21 14:56:34 -07003724 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003725 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003726 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003727 switch (profileType) {
3728 case 1:
3729 ViewDebug.startLooperProfiling(pcd.path, pcd.fd.getFileDescriptor());
3730 break;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003731 default:
3732 mProfiler.setProfiler(pcd.path, pcd.fd);
3733 mProfiler.autoStopProfiler = false;
3734 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003735 break;
3736 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003737 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003738 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003739 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003740 } finally {
3741 try {
3742 pcd.fd.close();
3743 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003744 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003745 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003746 }
3747 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003748 switch (profileType) {
3749 case 1:
3750 ViewDebug.stopLooperProfiling();
3751 break;
3752 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003753 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003754 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003755 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003756 }
3757 }
Bob Leee5408332009-09-04 18:31:17 -07003758
Romain Guya998dff2012-03-23 18:58:36 -07003759 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003760 if (managed) {
3761 try {
3762 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3763 } catch (IOException e) {
3764 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3765 + " -- can the process access this path?");
3766 } finally {
3767 try {
3768 dhd.fd.close();
3769 } catch (IOException e) {
3770 Slog.w(TAG, "Failure closing profile fd", e);
3771 }
3772 }
3773 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003774 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003775 }
3776 }
3777
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003778 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3779 boolean hasPkgInfo = false;
3780 if (packages != null) {
3781 for (int i=packages.length-1; i>=0; i--) {
3782 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3783 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003784 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003785 ref = mPackages.get(packages[i]);
3786 if (ref != null && ref.get() != null) {
3787 hasPkgInfo = true;
3788 } else {
3789 ref = mResourcePackages.get(packages[i]);
3790 if (ref != null && ref.get() != null) {
3791 hasPkgInfo = true;
3792 }
3793 }
3794 }
3795 mPackages.remove(packages[i]);
3796 mResourcePackages.remove(packages[i]);
3797 }
3798 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003799 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003800 hasPkgInfo);
3801 }
3802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003804 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003806 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 callbacks = collectComponentCallbacksLocked(true, null);
3808 }
Bob Leee5408332009-09-04 18:31:17 -07003809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 final int N = callbacks.size();
3811 for (int i=0; i<N; i++) {
3812 callbacks.get(i).onLowMemory();
3813 }
3814
Chris Tatece229052009-03-25 16:44:52 -07003815 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3816 if (Process.myUid() != Process.SYSTEM_UID) {
3817 int sqliteReleased = SQLiteDatabase.releaseMemory();
3818 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3819 }
Bob Leee5408332009-09-04 18:31:17 -07003820
Mike Reedcaf0df12009-04-27 14:32:05 -04003821 // Ask graphics to free up as much as possible (font/image caches)
3822 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823
3824 BinderInternal.forceGc("mem");
3825 }
3826
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003827 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003828 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003829
Romain Guy19f86e82012-04-23 15:19:07 -07003830 final WindowManagerImpl windowManager = WindowManagerImpl.getDefault();
3831 windowManager.startTrimMemory(level);
3832
3833 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003834 synchronized (mPackages) {
3835 callbacks = collectComponentCallbacksLocked(true, null);
3836 }
3837
3838 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07003839 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003840 callbacks.get(i).onTrimMemory(level);
3841 }
Romain Guy19f86e82012-04-23 15:19:07 -07003842
3843 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003844 }
3845
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003846 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003847 if (Process.isIsolated()) {
3848 // Isolated processes aren't going to do UI.
3849 return;
3850 }
Romain Guya9582652011-11-10 14:20:10 -08003851 try {
3852 int uid = Process.myUid();
3853 String[] packages = getPackageManager().getPackagesForUid(uid);
3854
3855 // If there are several packages in this application we won't
3856 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08003857 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003858 HardwareRenderer.setupDiskCache(cacheDir);
3859 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08003860 }
3861 } catch (RemoteException e) {
3862 // Ignore
3863 }
3864 }
3865
Romain Guy65b345f2011-07-27 18:51:50 -07003866 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 mBoundApplication = data;
3868 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003869 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003871 mProfiler = new Profiler();
3872 mProfiler.profileFile = data.initProfileFile;
3873 mProfiler.profileFd = data.initProfileFd;
3874 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
3875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08003877 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003878 android.ddm.DdmHandleAppName.setAppName(data.processName);
3879
Dianne Hackborn5d927c22011-09-02 12:22:18 -07003880 if (data.persistent) {
3881 // Persistent processes on low-memory devices do not get to
3882 // use hardware accelerated drawing, since this can add too much
3883 // overhead to the process.
3884 Display display = WindowManagerImpl.getDefault().getDefaultDisplay();
3885 if (!ActivityManager.isHighEndGfx(display)) {
3886 HardwareRenderer.disable(false);
3887 }
3888 }
Romain Guya9582652011-11-10 14:20:10 -08003889
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003890 if (mProfiler.profileFd != null) {
3891 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07003892 }
3893
Joe Onoratod630f102011-03-17 18:42:26 -07003894 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
3895 // implementation to use the pool executor. Normally, we use the
3896 // serialized executor as the default. This has to happen in the
3897 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07003898 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07003899 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
3900 }
3901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 /*
3903 * Before spawning a new process, reset the time zone to be the system time zone.
3904 * This needs to be done because the system time zone could have changed after the
3905 * the spawning of this process. Without doing this this process would have the incorrect
3906 * system time zone.
3907 */
3908 TimeZone.setDefault(null);
3909
3910 /*
3911 * Initialize the default locale in this process for the reasons we set the time zone.
3912 */
3913 Locale.setDefault(data.config.locale);
3914
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003915 /*
3916 * Update the system configuration since its preloaded and might not
3917 * reflect configuration changes. The configuration object passed
3918 * in AppBindData can be safely assumed to be up to date
3919 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003920 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003921 applyCompatConfiguration();
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07003922
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003923 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003924
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07003925 final ContextImpl appContext = new ContextImpl();
3926 appContext.init(data.info, null, this);
3927 final File cacheDir = appContext.getCacheDir();
3928
3929 // Provide a usable directory for temporary files
3930 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
3931
3932 setupGraphicsSupport(data.info, cacheDir);
3933
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003934 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003935 * For system applications on userdebug/eng builds, log stack
3936 * traces of disk and network access to dropbox for analysis.
3937 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07003938 if ((data.appInfo.flags &
3939 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07003940 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
3941 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07003942 }
3943
3944 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07003945 * For apps targetting SDK Honeycomb or later, we don't allow
3946 * network usage on the main event loop / UI thread.
3947 *
3948 * Note to those grepping: this is what ultimately throws
3949 * NetworkOnMainThreadException ...
3950 */
3951 if (data.appInfo.targetSdkVersion > 9) {
3952 StrictMode.enableDeathOnNetwork();
3953 }
3954
3955 /**
Dianne Hackborn96e240f2009-07-26 17:42:30 -07003956 * Switch this process to density compatibility mode if needed.
3957 */
3958 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
3959 == 0) {
3960 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
3961 }
Bob Leee5408332009-09-04 18:31:17 -07003962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
3964 // XXX should have option to change the port.
3965 Debug.changeDebugPort(8100);
3966 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003967 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 + " is waiting for the debugger on port 8100...");
3969
3970 IActivityManager mgr = ActivityManagerNative.getDefault();
3971 try {
3972 mgr.showWaitingForDebugger(mAppThread, true);
3973 } catch (RemoteException ex) {
3974 }
3975
3976 Debug.waitForDebugger();
3977
3978 try {
3979 mgr.showWaitingForDebugger(mAppThread, false);
3980 } catch (RemoteException ex) {
3981 }
3982
3983 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003984 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003985 + " can be debugged on port 8100...");
3986 }
3987 }
3988
Siva Velusamy92a8b222012-03-09 16:24:04 -08003989 // Enable OpenGL tracing if required
3990 if (data.enableOpenGlTrace) {
3991 GLUtils.enableTracing();
3992 }
3993
Robert Greenwalt434203a2010-10-11 16:00:27 -07003994 /**
3995 * Initialize the default http proxy in this process for the reasons we set the time zone.
3996 */
3997 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08003998 if (b != null) {
3999 // In pre-boot mode (doing initial launch to collect password), not
4000 // all system is up. This includes the connectivity service, so don't
4001 // crash if we can't get it.
4002 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4003 try {
4004 ProxyProperties proxyProperties = service.getProxy();
4005 Proxy.setHttpProxySystemProperty(proxyProperties);
4006 } catch (RemoteException e) {}
4007 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 InstrumentationInfo ii = null;
4011 try {
4012 ii = appContext.getPackageManager().
4013 getInstrumentationInfo(data.instrumentationName, 0);
4014 } catch (PackageManager.NameNotFoundException e) {
4015 }
4016 if (ii == null) {
4017 throw new RuntimeException(
4018 "Unable to find instrumentation info for: "
4019 + data.instrumentationName);
4020 }
4021
4022 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004023 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 mInstrumentationAppPackage = ii.packageName;
4025 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004026 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027
4028 ApplicationInfo instrApp = new ApplicationInfo();
4029 instrApp.packageName = ii.packageName;
4030 instrApp.sourceDir = ii.sourceDir;
4031 instrApp.publicSourceDir = ii.publicSourceDir;
4032 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004033 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004034 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004036 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 instrContext.init(pi, null, this);
4038
4039 try {
4040 java.lang.ClassLoader cl = instrContext.getClassLoader();
4041 mInstrumentation = (Instrumentation)
4042 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4043 } catch (Exception e) {
4044 throw new RuntimeException(
4045 "Unable to instantiate instrumentation "
4046 + data.instrumentationName + ": " + e.toString(), e);
4047 }
4048
4049 mInstrumentation.init(this, instrContext, appContext,
4050 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4051
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004052 if (mProfiler.profileFile != null && !ii.handleProfiling
4053 && mProfiler.profileFd == null) {
4054 mProfiler.handlingProfiling = true;
4055 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004056 file.getParentFile().mkdirs();
4057 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4058 }
4059
4060 try {
4061 mInstrumentation.onCreate(data.instrumentationArgs);
4062 }
4063 catch (Exception e) {
4064 throw new RuntimeException(
4065 "Exception thrown in onCreate() of "
4066 + data.instrumentationName + ": " + e.toString(), e);
4067 }
4068
4069 } else {
4070 mInstrumentation = new Instrumentation();
4071 }
4072
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004073 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004074 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004075 }
4076
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004077 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004078 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004079 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004080 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004082 // If the app is being launched for full backup or restore, bring it up in
4083 // a restricted environment with the base application class.
4084 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4085 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004086
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004087 // don't bring up providers in restricted mode; they may depend on the
4088 // app's custom Application class
4089 if (!data.restrictedBackupMode) {
4090 List<ProviderInfo> providers = data.providers;
4091 if (providers != null) {
4092 installContentProviders(app, providers);
4093 // For process that contains content providers, we want to
4094 // ensure that the JIT is enabled "at some point".
4095 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4096 }
4097 }
4098
4099 try {
4100 mInstrumentation.callApplicationOnCreate(app);
4101 } catch (Exception e) {
4102 if (!mInstrumentation.onException(app, e)) {
4103 throw new RuntimeException(
4104 "Unable to create application " + app.getClass().getName()
4105 + ": " + e.toString(), e);
4106 }
4107 }
4108 } finally {
4109 StrictMode.setThreadPolicy(savedPolicy);
4110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 }
4112
4113 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4114 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004115 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4116 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 Debug.stopMethodTracing();
4118 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004119 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004120 // + ", app thr: " + mAppThread);
4121 try {
4122 am.finishInstrumentation(mAppThread, resultCode, results);
4123 } catch (RemoteException ex) {
4124 }
4125 }
4126
Romain Guy65b345f2011-07-27 18:51:50 -07004127 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004128 Context context, List<ProviderInfo> providers) {
4129 final ArrayList<IActivityManager.ContentProviderHolder> results =
4130 new ArrayList<IActivityManager.ContentProviderHolder>();
4131
Romain Guya998dff2012-03-23 18:58:36 -07004132 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004134 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 buf.append(cpi.authority);
4136 buf.append(": ");
4137 buf.append(cpi.name);
4138 Log.i(TAG, buf.toString());
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004139 IContentProvider cp = installProvider(context, null, cpi,
4140 false /*noisy*/, true /*noReleaseNeeded*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 if (cp != null) {
4142 IActivityManager.ContentProviderHolder cph =
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004143 new IActivityManager.ContentProviderHolder(cpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 cph.provider = cp;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004145 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 }
4148 }
4149
4150 try {
4151 ActivityManagerNative.getDefault().publishContentProviders(
4152 getApplicationThread(), results);
4153 } catch (RemoteException ex) {
4154 }
4155 }
4156
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004157 public final IContentProvider acquireProvider(Context c, String name) {
4158 IContentProvider provider = acquireExistingProvider(c, name);
4159 if (provider != null) {
4160 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 }
4162
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004163 // There is a possible race here. Another thread may try to acquire
4164 // the same provider at the same time. When this happens, we want to ensure
4165 // that the first one wins.
4166 // Note that we cannot hold the lock while acquiring and installing the
4167 // provider since it might take a long time to run and it could also potentially
4168 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 IActivityManager.ContentProviderHolder holder = null;
4170 try {
4171 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004172 getApplicationThread(), name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 } catch (RemoteException ex) {
4174 }
4175 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004176 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 return null;
4178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004179
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004180 // Install provider will increment the reference count for us, and break
4181 // any ties in the race.
4182 provider = installProvider(c, holder.provider, holder.info,
4183 true /*noisy*/, holder.noReleaseNeeded);
4184 if (holder.provider != null && provider != holder.provider) {
4185 if (localLOGV) {
4186 Slog.v(TAG, "acquireProvider: lost the race, releasing extraneous "
4187 + "reference to the content provider");
4188 }
4189 try {
4190 ActivityManagerNative.getDefault().removeContentProvider(
4191 getApplicationThread(), name);
4192 } catch (RemoteException ex) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
4194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 return provider;
4196 }
4197
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004198 public final IContentProvider acquireExistingProvider(Context c, String name) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004199 synchronized (mProviderMap) {
4200 ProviderClientRecord pr = mProviderMap.get(name);
4201 if (pr == null) {
4202 return null;
4203 }
4204
4205 IContentProvider provider = pr.mProvider;
4206 IBinder jBinder = provider.asBinder();
4207
4208 // Only increment the ref count if we have one. If we don't then the
4209 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004210 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004211 if (prc != null) {
4212 prc.count += 1;
4213 if (prc.count == 1) {
4214 if (localLOGV) {
4215 Slog.v(TAG, "acquireExistingProvider: "
4216 + "snatched provider from the jaws of death");
4217 }
4218 // Because the provider previously had a reference count of zero,
4219 // it was scheduled to be removed. Cancel that.
4220 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4221 }
4222 }
4223 return provider;
4224 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004225 }
4226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 public final boolean releaseProvider(IContentProvider provider) {
4228 if(provider == null) {
4229 return false;
4230 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004233 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004235 if (prc == null) {
4236 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004238 }
4239
4240 if (prc.count == 0) {
4241 if (localLOGV) Slog.v(TAG, "releaseProvider: ref count already 0, how?");
4242 return false;
4243 }
4244
4245 prc.count -= 1;
4246 if (prc.count == 0) {
4247 // Schedule the actual remove asynchronously, since we don't know the context
4248 // this will be called in.
4249 // TODO: it would be nice to post a delayed message, so
4250 // if we come back and need the same provider quickly
4251 // we will still have it available.
4252 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, provider);
4253 mH.sendMessage(msg);
4254 }
4255 return true;
4256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
4258
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004259 final void completeRemoveProvider(IContentProvider provider) {
4260 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004261 String remoteProviderName = null;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004262 synchronized(mProviderMap) {
4263 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004264 if (prc == null) {
4265 // Either no release is needed (so we shouldn't be here) or the
4266 // provider was already released.
4267 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: release not needed");
4268 return;
4269 }
4270
4271 if (prc.count != 0) {
4272 // There was a race! Some other client managed to acquire
4273 // the provider before the removal was completed.
4274 // Abort the removal. We will do it later.
4275 if (localLOGV) Slog.v(TAG, "completeRemoveProvider: lost the race, "
4276 + "provider still in use");
4277 return;
4278 }
4279
4280 mProviderRefCountMap.remove(jBinder);
4281
4282 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4283 while (iter.hasNext()) {
4284 ProviderClientRecord pr = iter.next();
4285 IBinder myBinder = pr.mProvider.asBinder();
4286 if (myBinder == jBinder) {
4287 iter.remove();
4288 if (pr.mLocalProvider == null) {
4289 myBinder.unlinkToDeath(pr, 0);
4290 if (remoteProviderName == null) {
4291 remoteProviderName = pr.mName;
4292 }
4293 }
4294 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004295 }
4296 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004297
4298 if (remoteProviderName != null) {
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004299 try {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004300 if (localLOGV) {
4301 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4302 + "removeContentProvider(" + remoteProviderName + ")");
4303 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004304 ActivityManagerNative.getDefault().removeContentProvider(
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004305 getApplicationThread(), remoteProviderName);
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004306 } catch (RemoteException e) {
4307 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004308 }
Dianne Hackborn0c3154d2009-10-06 17:18:05 -07004309 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311
4312 final void removeDeadProvider(String name, IContentProvider provider) {
4313 synchronized(mProviderMap) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004314 ProviderClientRecord pr = mProviderMap.get(name);
Jeff Brown2a4c5ac2011-08-23 11:58:26 -07004315 if (pr != null && pr.mProvider.asBinder() == provider.asBinder()) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004316 Slog.i(TAG, "Removing dead content provider: " + name);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004317 ProviderClientRecord removed = mProviderMap.remove(name);
Suchi Amalapurapufff2fda2009-06-30 21:36:16 -07004318 if (removed != null) {
4319 removed.mProvider.asBinder().unlinkToDeath(removed, 0);
4320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322 }
4323 }
4324
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004325 /**
4326 * Installs the provider.
4327 *
4328 * Providers that are local to the process or that come from the system server
4329 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4330 * Other remote providers are reference counted. The initial reference count
4331 * for all reference counted providers is one. Providers that are not reference
4332 * counted do not have a reference count (at all).
4333 *
4334 * This method detects when a provider has already been installed. When this happens,
4335 * it increments the reference count of the existing provider (if appropriate)
4336 * and returns the existing provider. This can happen due to concurrent
4337 * attempts to acquire the same provider.
4338 */
Romain Guy65b345f2011-07-27 18:51:50 -07004339 private IContentProvider installProvider(Context context,
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004340 IContentProvider provider, ProviderInfo info,
4341 boolean noisy, boolean noReleaseNeeded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 ContentProvider localProvider = null;
4343 if (provider == null) {
4344 if (noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004345 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 + info.name);
4347 }
4348 Context c = null;
4349 ApplicationInfo ai = info.applicationInfo;
4350 if (context.getPackageName().equals(ai.packageName)) {
4351 c = context;
4352 } else if (mInitialApplication != null &&
4353 mInitialApplication.getPackageName().equals(ai.packageName)) {
4354 c = mInitialApplication;
4355 } else {
4356 try {
4357 c = context.createPackageContext(ai.packageName,
4358 Context.CONTEXT_INCLUDE_CODE);
4359 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004360 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 }
4362 }
4363 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004364 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 ai.packageName +
4366 " while loading content provider " +
4367 info.name);
4368 return null;
4369 }
4370 try {
4371 final java.lang.ClassLoader cl = c.getClassLoader();
4372 localProvider = (ContentProvider)cl.
4373 loadClass(info.name).newInstance();
4374 provider = localProvider.getIContentProvider();
4375 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004376 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 info.name + " from sourceDir " +
4378 info.applicationInfo.sourceDir);
4379 return null;
4380 }
Joe Onorato43a17652011-04-06 19:22:23 -07004381 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 TAG, "Instantiating local provider " + info.name);
4383 // XXX Need to create the correct context for this provider.
4384 localProvider.attachInfo(c, info);
4385 } catch (java.lang.Exception e) {
4386 if (!mInstrumentation.onException(null, e)) {
4387 throw new RuntimeException(
4388 "Unable to get provider " + info.name
4389 + ": " + e.toString(), e);
4390 }
4391 return null;
4392 }
4393 } else if (localLOGV) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004394 Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 + info.name);
4396 }
4397
4398 synchronized (mProviderMap) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004399 // There is a possibility that this thread raced with another thread to
4400 // add the provider. If we find another thread got there first then we
4401 // just get out of the way and return the original provider.
4402 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 String names[] = PATTERN_SEMICOLON.split(info.authority);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004404 for (int i = 0; i < names.length; i++) {
4405 ProviderClientRecord pr = mProviderMap.get(names[i]);
4406 if (pr != null) {
4407 if (localLOGV) {
4408 Slog.v(TAG, "installProvider: lost the race, "
4409 + "using existing named provider");
4410 }
4411 provider = pr.mProvider;
4412 } else {
4413 pr = new ProviderClientRecord(names[i], provider, localProvider);
4414 if (localProvider == null) {
4415 try {
4416 jBinder.linkToDeath(pr, 0);
4417 } catch (RemoteException e) {
4418 // Provider already dead. Bail out of here without making
4419 // any changes to the provider map or other data structures.
4420 return null;
4421 }
4422 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 mProviderMap.put(names[i], pr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 }
4425 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427 if (localProvider != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004428 ProviderClientRecord pr = mLocalProviders.get(jBinder);
4429 if (pr != null) {
4430 if (localLOGV) {
4431 Slog.v(TAG, "installProvider: lost the race, "
4432 + "using existing local provider");
4433 }
4434 provider = pr.mProvider;
4435 } else {
4436 pr = new ProviderClientRecord(null, provider, localProvider);
4437 mLocalProviders.put(jBinder, pr);
4438 }
4439 }
4440
4441 if (!noReleaseNeeded) {
4442 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4443 if (prc != null) {
4444 if (localLOGV) {
4445 Slog.v(TAG, "installProvider: lost the race, incrementing ref count");
4446 }
4447 prc.count += 1;
4448 if (prc.count == 1) {
4449 if (localLOGV) {
4450 Slog.v(TAG, "installProvider: "
4451 + "snatched provider from the jaws of death");
4452 }
4453 // Because the provider previously had a reference count of zero,
4454 // it was scheduled to be removed. Cancel that.
4455 mH.removeMessages(H.REMOVE_PROVIDER, provider);
4456 }
4457 } else {
4458 mProviderRefCountMap.put(jBinder, new ProviderRefCount(1));
4459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004460 }
4461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 return provider;
4463 }
4464
Romain Guy65b345f2011-07-27 18:51:50 -07004465 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 sThreadLocal.set(this);
4467 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004469 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004470 public void run() {
4471 ensureJitEnabled();
4472 }
4473 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4475 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4476 IActivityManager mgr = ActivityManagerNative.getDefault();
4477 try {
4478 mgr.attachApplication(mAppThread);
4479 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004480 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 }
4482 } else {
4483 // Don't set application object here -- if the system crashes,
4484 // we can't display an alert, we just want to die die die.
4485 android.ddm.DdmHandleAppName.setAppName("system_process");
4486 try {
4487 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004488 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 context.init(getSystemContext().mPackageInfo, null, this);
4490 Application app = Instrumentation.newApplication(Application.class, context);
4491 mAllApplications.add(app);
4492 mInitialApplication = app;
4493 app.onCreate();
4494 } catch (Exception e) {
4495 throw new RuntimeException(
4496 "Unable to instantiate Application():" + e.toString(), e);
4497 }
4498 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004499
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004500 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004501 public void onConfigurationChanged(Configuration newConfig) {
4502 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004503 // We need to apply this change to the resources
4504 // immediately, because upon returning the view
4505 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004506 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004507 // This actually changed the resources! Tell
4508 // everyone about it.
4509 if (mPendingConfiguration == null ||
4510 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4511 mPendingConfiguration = newConfig;
4512
4513 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4514 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004515 }
4516 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004517 }
4518 public void onLowMemory() {
4519 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004520 public void onTrimMemory(int level) {
4521 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004522 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
4524
Romain Guy5e9120d2012-01-30 12:17:22 -08004525 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004526 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 ActivityThread thread = new ActivityThread();
4528 thread.attach(true);
4529 return thread;
4530 }
4531
Jeff Brown10e89712011-07-08 18:52:57 -07004532 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004533 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004534 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 }
4536 }
4537
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004538 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004539 synchronized (mPackages) {
4540 if (mCoreSettings != null) {
4541 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004542 } else {
4543 return defaultValue;
4544 }
4545 }
4546 }
4547
Romain Guy65b345f2011-07-27 18:51:50 -07004548 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004549 SamplingProfilerIntegration.start();
4550
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004551 // CloseGuard defaults to true and can be quite spammy. We
4552 // disable it here, but selectively enable it later (via
4553 // StrictMode) on debug builds, but using DropBox, not logs.
4554 CloseGuard.setEnabled(false);
4555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 Process.setArgV0("<pre-initialized>");
4557
4558 Looper.prepareMainLooper();
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07004559 if (sMainThreadHandler == null) {
4560 sMainThreadHandler = new Handler();
4561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562
4563 ActivityThread thread = new ActivityThread();
4564 thread.attach(false);
4565
Romain Guy5e9120d2012-01-30 12:17:22 -08004566 AsyncTask.init();
4567
Dianne Hackborn287952c2010-09-22 22:34:31 -07004568 if (false) {
4569 Looper.myLooper().setMessageLogging(new
4570 LogPrinter(Log.DEBUG, "ActivityThread"));
4571 }
4572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 Looper.loop();
4574
Jeff Brown10e89712011-07-08 18:52:57 -07004575 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 }
4577}