blob: 97dcec0498e13cd7357591cfc63466bf71bbd452 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
32import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070033import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ProviderInfo;
35import android.content.pm.ServiceInfo;
36import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070037import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.res.Configuration;
39import android.content.res.Resources;
40import android.database.sqlite.SQLiteDatabase;
41import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080042import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.Bitmap;
44import android.graphics.Canvas;
Jeff Brown98365d72012-08-19 20:30:52 -070045import android.hardware.display.DisplayManager;
Jeff Brownbd6e1502012-08-28 03:27:37 -070046import android.hardware.display.DisplayManagerGlobal;
Robert Greenwalt434203a2010-10-11 16:00:27 -070047import android.net.IConnectivityManager;
48import android.net.Proxy;
49import android.net.ProxyProperties;
Romain Guya9582652011-11-10 14:20:10 -080050import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070051import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070052import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Bundle;
54import android.os.Debug;
55import android.os.Handler;
56import android.os.IBinder;
57import android.os.Looper;
58import android.os.Message;
59import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070060import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Process;
62import android.os.RemoteException;
63import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070064import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.SystemClock;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070066import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070067import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.AndroidRuntimeException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.DisplayMetrics;
70import android.util.EventLog;
71import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070072import android.util.LogPrinter;
Jeff Brown6754ba22011-12-14 20:20:01 -080073import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080074import android.util.Slog;
Jeff Brownd32460c2012-07-20 16:15:36 -070075import android.view.CompatibilityInfoHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070077import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.view.View;
79import android.view.ViewDebug;
80import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070081import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.view.Window;
83import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070084import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080085import android.renderscript.RenderScript;
Kenny Roote29df162012-08-10 08:28:37 -070086import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
88import com.android.internal.os.BinderInternal;
89import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070090import com.android.internal.os.SamplingProfilerIntegration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091
92import org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl;
93
94import java.io.File;
95import java.io.FileDescriptor;
96import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070097import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import java.io.PrintWriter;
99import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700100import java.net.InetAddress;
Kenny Roote29df162012-08-10 08:28:37 -0700101import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.util.ArrayList;
103import java.util.HashMap;
104import java.util.Iterator;
105import java.util.List;
106import java.util.Locale;
107import java.util.Map;
108import java.util.TimeZone;
109import java.util.regex.Pattern;
110
Jeff Sharkeye861b422012-03-01 20:59:22 -0800111import libcore.io.IoUtils;
112
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800113import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115final class SuperNotCalledException extends AndroidRuntimeException {
116 public SuperNotCalledException(String msg) {
117 super(msg);
118 }
119}
120
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700121final class RemoteServiceException extends AndroidRuntimeException {
122 public RemoteServiceException(String msg) {
123 super(msg);
124 }
125}
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127/**
128 * This manages the execution of the main thread in an
129 * application process, scheduling and executing activities,
130 * broadcasts, and other operations on it as the activity
131 * manager requests.
132 *
133 * {@hide}
134 */
135public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700136 /** @hide */
137 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700138 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700139 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700140 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700141 /** @hide */
142 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700143 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700144 private static final boolean DEBUG_BACKUP = false;
Dianne Hackborndc6b6352009-09-30 14:20:09 -0700145 private static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800146 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700147 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700148 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
150 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
151 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
152 private static final int LOG_ON_PAUSE_CALLED = 30021;
153 private static final int LOG_ON_RESUME_CALLED = 30022;
154
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700155 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700156
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700157 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 final ApplicationThread mAppThread = new ApplicationThread();
160 final Looper mLooper = Looper.myLooper();
161 final H mH = new H();
162 final HashMap<IBinder, ActivityClientRecord> mActivities
163 = new HashMap<IBinder, ActivityClientRecord>();
164 // List of new activities (via ActivityRecord.nextIdle) that should
165 // be reported when next we idle.
166 ActivityClientRecord mNewActivities = null;
167 // Number of activities that are currently visible on-screen.
168 int mNumVisibleActivities = 0;
169 final HashMap<IBinder, Service> mServices
170 = new HashMap<IBinder, Service>();
171 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700172 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700173 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700174 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700176 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700177 Configuration mResConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700178 CompatibilityInfo mResCompatibilityInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700179 Application mInitialApplication;
180 final ArrayList<Application> mAllApplications
181 = new ArrayList<Application>();
182 // set of instantiated backup agents, keyed by package name
183 final HashMap<String, BackupAgent> mBackupAgents = new HashMap<String, BackupAgent>();
Romain Guy65b345f2011-07-27 18:51:50 -0700184 static final ThreadLocal<ActivityThread> sThreadLocal = new ThreadLocal<ActivityThread>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700185 Instrumentation mInstrumentation;
186 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700187 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 String mInstrumentationAppPackage = null;
189 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700190 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700191 boolean mSystemThread = false;
192 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 // These can be accessed by multiple threads; mPackages is the lock.
195 // XXX For now we keep around information about all packages we have
196 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800197 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700198 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800199 // which means this lock gets held while the activity and window managers
200 // holds their own lock. Thus you MUST NEVER call back into the activity manager
201 // or window manager or anything that depends on them while holding this lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700202 final HashMap<String, WeakReference<LoadedApk>> mPackages
203 = new HashMap<String, WeakReference<LoadedApk>>();
204 final HashMap<String, WeakReference<LoadedApk>> mResourcePackages
205 = new HashMap<String, WeakReference<LoadedApk>>();
Jeff Browna492c3a2012-08-23 19:48:44 -0700206 final HashMap<CompatibilityInfo, DisplayMetrics> mDefaultDisplayMetrics
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700207 = new HashMap<CompatibilityInfo, DisplayMetrics>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700208 final HashMap<ResourcesKey, WeakReference<Resources> > mActiveResources
209 = new HashMap<ResourcesKey, WeakReference<Resources> >();
210 final ArrayList<ActivityClientRecord> mRelaunchingActivities
211 = new ArrayList<ActivityClientRecord>();
212 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700214 // The lock of mProviderMap protects the following variables.
215 final HashMap<String, ProviderClientRecord> mProviderMap
216 = new HashMap<String, ProviderClientRecord>();
217 final HashMap<IBinder, ProviderRefCount> mProviderRefCountMap
218 = new HashMap<IBinder, ProviderRefCount>();
219 final HashMap<IBinder, ProviderClientRecord> mLocalProviders
220 = new HashMap<IBinder, ProviderClientRecord>();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700221 final HashMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
222 = new HashMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
Jeff Hamilton52d32032011-01-08 15:31:26 -0600224 final HashMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
225 = new HashMap<Activity, ArrayList<OnActivityPausedListener>>();
226
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 final GcIdler mGcIdler = new GcIdler();
228 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700230 static Handler sMainThreadHandler; // set once in main()
231
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800232 Bundle mCoreSettings = null;
233
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400234 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700236 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 Intent intent;
238 Bundle state;
239 Activity activity;
240 Window window;
241 Activity parent;
242 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700243 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 boolean paused;
245 boolean stopped;
246 boolean hideForNow;
247 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700248 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700249 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700251 String profileFile;
252 ParcelFileDescriptor profileFd;
253 boolean autoStopProfiler;
254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400256 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700257 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258
259 List<ResultInfo> pendingResults;
260 List<Intent> pendingIntents;
261
262 boolean startsNotResumed;
263 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800264 int pendingConfigChanges;
265 boolean onlyLocalRequest;
266
267 View mPendingRemoveWindow;
268 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700270 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 parent = null;
272 embeddedID = null;
273 paused = false;
274 stopped = false;
275 hideForNow = false;
276 nextIdle = null;
277 }
278
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800279 public boolean isPreHoneycomb() {
280 if (activity != null) {
281 return activity.getApplicationInfo().targetSdkVersion
282 < android.os.Build.VERSION_CODES.HONEYCOMB;
283 }
284 return false;
285 }
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700288 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 return "ActivityRecord{"
290 + Integer.toHexString(System.identityHashCode(this))
291 + " token=" + token + " " + (componentName == null
292 ? "no component name" : componentName.toShortString())
293 + "}";
294 }
295 }
296
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700297 final class ProviderClientRecord {
298 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 final IContentProvider mProvider;
300 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700301 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700303 ProviderClientRecord(String[] names, IContentProvider provider,
304 ContentProvider localProvider,
305 IActivityManager.ContentProviderHolder holder) {
306 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 mProvider = provider;
308 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700309 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 }
311 }
312
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400313 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 List<Intent> intents;
315 IBinder token;
316 public String toString() {
317 return "NewIntentData{intents=" + intents + " token=" + token + "}";
318 }
319 }
320
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400321 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700322 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700323 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
324 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
325 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700326 this.intent = intent;
327 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 Intent intent;
330 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400331 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 public String toString() {
333 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700334 info.packageName + " resultCode=" + getResultCode()
335 + " resultData=" + getResultData() + " resultExtras="
336 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 }
338 }
339
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400340 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700341 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400342 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700343 int backupMode;
344 public String toString() {
345 return "CreateBackupAgentData{appInfo=" + appInfo
346 + " backupAgent=" + appInfo.backupAgentName
347 + " mode=" + backupMode + "}";
348 }
349 }
Bob Leee5408332009-09-04 18:31:17 -0700350
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400351 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 IBinder token;
353 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400354 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Intent intent;
356 public String toString() {
357 return "CreateServiceData{token=" + token + " className="
358 + info.name + " packageName=" + info.packageName
359 + " intent=" + intent + "}";
360 }
361 }
362
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400363 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 IBinder token;
365 Intent intent;
366 boolean rebind;
367 public String toString() {
368 return "BindServiceData{token=" + token + " intent=" + intent + "}";
369 }
370 }
371
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400372 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700374 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700376 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 Intent args;
378 public String toString() {
379 return "ServiceArgsData{token=" + token + " startId=" + startId
380 + " args=" + args + "}";
381 }
382 }
383
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400384 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700385 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 String processName;
387 ApplicationInfo appInfo;
388 List<ProviderInfo> providers;
389 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 Bundle instrumentationArgs;
391 IInstrumentationWatcher instrumentationWatcher;
392 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800393 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700394 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700395 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400397 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700398
399 /** Initial values for {@link Profiler}. */
400 String initProfileFile;
401 ParcelFileDescriptor initProfileFd;
402 boolean initAutoStopProfiler;
403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 public String toString() {
405 return "AppBindData{appInfo=" + appInfo + "}";
406 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700407 }
408
409 static final class Profiler {
410 String profileFile;
411 ParcelFileDescriptor profileFd;
412 boolean autoStopProfiler;
413 boolean profiling;
414 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700415 public void setProfiler(String file, ParcelFileDescriptor fd) {
416 if (profiling) {
417 if (fd != null) {
418 try {
419 fd.close();
420 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700421 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700422 }
423 }
424 return;
425 }
426 if (profileFd != null) {
427 try {
428 profileFd.close();
429 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700430 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700431 }
432 }
433 profileFile = file;
434 profileFd = fd;
435 }
436 public void startProfiling() {
437 if (profileFd == null || profiling) {
438 return;
439 }
440 try {
441 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
442 8 * 1024 * 1024, 0);
443 profiling = true;
444 } catch (RuntimeException e) {
445 Slog.w(TAG, "Profiling failed on path " + profileFile);
446 try {
447 profileFd.close();
448 profileFd = null;
449 } catch (IOException e2) {
450 Slog.w(TAG, "Failure closing profile fd", e2);
451 }
452 }
453 }
454 public void stopProfiling() {
455 if (profiling) {
456 profiling = false;
457 Debug.stopMethodTracing();
458 if (profileFd != null) {
459 try {
460 profileFd.close();
461 } catch (IOException e) {
462 }
463 }
464 profileFd = null;
465 profileFile = null;
466 }
467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400470 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700471 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700472 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800473 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 }
476
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400477 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 IBinder token;
479 List<ResultInfo> results;
480 public String toString() {
481 return "ResultData{token=" + token + " results" + results + "}";
482 }
483 }
484
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400485 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800486 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 String what;
488 String who;
489 }
490
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400491 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700492 String path;
493 ParcelFileDescriptor fd;
494 }
495
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400496 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700497 String path;
498 ParcelFileDescriptor fd;
499 }
500
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400501 static final class UpdateCompatibilityData {
502 String pkg;
503 CompatibilityInfo info;
504 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700505
Romain Guy65b345f2011-07-27 18:51:50 -0700506 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700507
Romain Guy65b345f2011-07-27 18:51:50 -0700508 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700509 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
510 private static final String ONE_COUNT_COLUMN = "%21s %8d";
511 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700512 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 // Formatting for checkin service - update version if row format changes
515 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700516
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700517 private void updatePendingConfiguration(Configuration config) {
518 synchronized (mPackages) {
519 if (mPendingConfiguration == null ||
520 mPendingConfiguration.isOtherSeqNewer(config)) {
521 mPendingConfiguration = config;
522 }
523 }
524 }
525
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 public final void schedulePauseActivity(IBinder token, boolean finished,
527 boolean userLeaving, int configChanges) {
528 queueOrSendMessage(
529 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
530 token,
531 (userLeaving ? 1 : 0),
532 configChanges);
533 }
534
535 public final void scheduleStopActivity(IBinder token, boolean showWindow,
536 int configChanges) {
537 queueOrSendMessage(
538 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
539 token, 0, configChanges);
540 }
541
542 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
543 queueOrSendMessage(
544 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
545 token);
546 }
547
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800548 public final void scheduleSleeping(IBinder token, boolean sleeping) {
549 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
550 }
551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
553 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
554 }
555
556 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
557 ResultData res = new ResultData();
558 res.token = token;
559 res.results = results;
560 queueOrSendMessage(H.SEND_RESULT, res);
561 }
562
563 // we use token to identify this activity without having to send the
564 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700565 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700566 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
567 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700568 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
569 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700570 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571
572 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700573 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 r.intent = intent;
575 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400576 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 r.state = state;
578
579 r.pendingResults = pendingResults;
580 r.pendingIntents = pendingNewIntents;
581
582 r.startsNotResumed = notResumed;
583 r.isForward = isForward;
584
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700585 r.profileFile = profileName;
586 r.profileFd = profileFd;
587 r.autoStopProfiler = autoStopProfiler;
588
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700589 updatePendingConfiguration(curConfig);
590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
592 }
593
594 public final void scheduleRelaunchActivity(IBinder token,
595 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800596 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800597 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
598 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600
601 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
602 NewIntentData data = new NewIntentData();
603 data.intents = intents;
604 data.token = token;
605
606 queueOrSendMessage(H.NEW_INTENT, data);
607 }
608
609 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
610 int configChanges) {
611 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
612 configChanges);
613 }
614
615 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400616 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700617 boolean sync, int sendingUser) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700618 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700619 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400621 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 queueOrSendMessage(H.RECEIVER, r);
623 }
624
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 public final void scheduleCreateBackupAgent(ApplicationInfo app,
626 CompatibilityInfo compatInfo, int backupMode) {
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 d.backupMode = backupMode;
631
632 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
633 }
634
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400635 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
636 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700637 CreateBackupAgentData d = new CreateBackupAgentData();
638 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400639 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700640
641 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
642 }
643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400645 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 CreateServiceData s = new CreateServiceData();
647 s.token = token;
648 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400649 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650
651 queueOrSendMessage(H.CREATE_SERVICE, s);
652 }
653
654 public final void scheduleBindService(IBinder token, Intent intent,
655 boolean rebind) {
656 BindServiceData s = new BindServiceData();
657 s.token = token;
658 s.intent = intent;
659 s.rebind = rebind;
660
Amith Yamasani742a6712011-05-04 14:49:28 -0700661 if (DEBUG_SERVICE)
662 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
663 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 queueOrSendMessage(H.BIND_SERVICE, s);
665 }
666
667 public final void scheduleUnbindService(IBinder token, Intent intent) {
668 BindServiceData s = new BindServiceData();
669 s.token = token;
670 s.intent = intent;
671
672 queueOrSendMessage(H.UNBIND_SERVICE, s);
673 }
674
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700675 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700676 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 ServiceArgsData s = new ServiceArgsData();
678 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700679 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700681 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 s.args = args;
683
684 queueOrSendMessage(H.SERVICE_ARGS, s);
685 }
686
687 public final void scheduleStopService(IBinder token) {
688 queueOrSendMessage(H.STOP_SERVICE, token);
689 }
690
691 public final void bindApplication(String processName,
692 ApplicationInfo appInfo, List<ProviderInfo> providers,
693 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700694 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800696 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
697 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700698 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699
700 if (services != null) {
701 // Setup the service cache in the ServiceManager
702 ServiceManager.initServiceCache(services);
703 }
704
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800705 setCoreSettings(coreSettings);
706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 AppBindData data = new AppBindData();
708 data.processName = processName;
709 data.appInfo = appInfo;
710 data.providers = providers;
711 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 data.instrumentationArgs = instrumentationArgs;
713 data.instrumentationWatcher = instrumentationWatcher;
714 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800715 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700716 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700717 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400719 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700720 data.initProfileFile = profileFile;
721 data.initProfileFd = profileFd;
722 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 queueOrSendMessage(H.BIND_APPLICATION, data);
724 }
725
726 public final void scheduleExit() {
727 queueOrSendMessage(H.EXIT_APPLICATION, null);
728 }
729
Christopher Tate5e1ab332009-09-01 20:32:49 -0700730 public final void scheduleSuicide() {
731 queueOrSendMessage(H.SUICIDE, null);
732 }
733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 public void requestThumbnail(IBinder token) {
735 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
736 }
737
738 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700739 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
741 }
742
743 public void updateTimeZone() {
744 TimeZone.setDefault(null);
745 }
746
Robert Greenwalt03595d02010-11-02 14:08:23 -0700747 public void clearDnsCache() {
748 // a non-standard API to get this to libcore
749 InetAddress.clearDnsCache();
750 }
751
Robert Greenwalt434203a2010-10-11 16:00:27 -0700752 public void setHttpProxy(String host, String port, String exclList) {
753 Proxy.setHttpProxySystemProperty(host, port, exclList);
754 }
755
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 public void processInBackground() {
757 mH.removeMessages(H.GC_WHEN_IDLE);
758 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
759 }
760
761 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700762 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700763 try {
764 data.fd = ParcelFileDescriptor.dup(fd);
765 data.token = servicetoken;
766 data.args = args;
767 queueOrSendMessage(H.DUMP_SERVICE, data);
768 } catch (IOException e) {
769 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771 }
772
773 // This function exists to make sure all receiver dispatching is
774 // correctly ordered, since these are one-way calls and the binder driver
775 // applies transaction ordering per object for such calls.
776 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700777 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700778 boolean sticky, int sendingUser) throws RemoteException {
779 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
780 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
Bob Leee5408332009-09-04 18:31:17 -0700782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 public void scheduleLowMemory() {
784 queueOrSendMessage(H.LOW_MEMORY, null);
785 }
786
787 public void scheduleActivityConfigurationChanged(IBinder token) {
788 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
789 }
790
Romain Guy7eabe552011-07-21 14:56:34 -0700791 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
792 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700793 ProfilerControlData pcd = new ProfilerControlData();
794 pcd.path = path;
795 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700796 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800797 }
798
Andy McFadden824c5102010-07-09 16:26:57 -0700799 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
800 DumpHeapData dhd = new DumpHeapData();
801 dhd.path = path;
802 dhd.fd = fd;
803 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
804 }
805
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700806 public void setSchedulingGroup(int group) {
807 // Note: do this immediately, since going into the foreground
808 // should happen regardless of what pending work we have to do
809 // and the activity manager will wait for us to report back that
810 // we are done before sending us to the background.
811 try {
812 Process.setProcessGroup(Process.myPid(), group);
813 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800814 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700815 }
816 }
Bob Leee5408332009-09-04 18:31:17 -0700817
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700818 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
819 Debug.getMemoryInfo(outInfo);
820 }
Bob Leee5408332009-09-04 18:31:17 -0700821
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700822 public void dispatchPackageBroadcast(int cmd, String[] packages) {
823 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
824 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700825
826 public void scheduleCrash(String msg) {
827 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
828 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700829
Dianne Hackborn30d71892010-12-11 10:37:55 -0800830 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
831 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700832 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700833 try {
834 data.fd = ParcelFileDescriptor.dup(fd);
835 data.token = activitytoken;
836 data.prefix = prefix;
837 data.args = args;
838 queueOrSendMessage(H.DUMP_ACTIVITY, data);
839 } catch (IOException e) {
840 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700841 }
842 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700843
Marco Nelissen18cb2872011-11-15 11:19:53 -0800844 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
845 String[] args) {
846 DumpComponentInfo data = new DumpComponentInfo();
847 try {
848 data.fd = ParcelFileDescriptor.dup(fd);
849 data.token = providertoken;
850 data.args = args;
851 queueOrSendMessage(H.DUMP_PROVIDER, data);
852 } catch (IOException e) {
853 Slog.w(TAG, "dumpProvider failed", e);
854 }
855 }
856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700858 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
859 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700860 FileOutputStream fout = new FileOutputStream(fd);
861 PrintWriter pw = new PrintWriter(fout);
862 try {
Romain Guya998dff2012-03-23 18:58:36 -0700863 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700864 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700865 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700866 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700867 }
868
Romain Guya998dff2012-03-23 18:58:36 -0700869 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 long nativeMax = Debug.getNativeHeapSize() / 1024;
871 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
872 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
873
874 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
875 Debug.getMemoryInfo(memInfo);
876
Dianne Hackbornb437e092011-08-05 17:50:29 -0700877 if (!all) {
878 return memInfo;
879 }
880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 Runtime runtime = Runtime.getRuntime();
882
883 long dalvikMax = runtime.totalMemory() / 1024;
884 long dalvikFree = runtime.freeMemory() / 1024;
885 long dalvikAllocated = dalvikMax - dalvikFree;
886 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700887 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700888 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
889 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 int globalAssetCount = AssetManager.getGlobalAssetCount();
891 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
892 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
893 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
894 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700895 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800896 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700899 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 // NOTE: if you change anything significant below, also consider changing
901 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700902 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 // Header
906 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
907 pw.print(Process.myPid()); pw.print(',');
908 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 // Heap info - max
911 pw.print(nativeMax); pw.print(',');
912 pw.print(dalvikMax); pw.print(',');
913 pw.print("N/A,");
914 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 // Heap info - allocated
917 pw.print(nativeAllocated); pw.print(',');
918 pw.print(dalvikAllocated); pw.print(',');
919 pw.print("N/A,");
920 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 // Heap info - free
923 pw.print(nativeFree); pw.print(',');
924 pw.print(dalvikFree); pw.print(',');
925 pw.print("N/A,");
926 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 // Heap info - proportional set size
929 pw.print(memInfo.nativePss); pw.print(',');
930 pw.print(memInfo.dalvikPss); pw.print(',');
931 pw.print(memInfo.otherPss); pw.print(',');
932 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700935 pw.print(memInfo.nativeSharedDirty); pw.print(',');
936 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
937 pw.print(memInfo.otherSharedDirty); pw.print(',');
938 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
939 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700940
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700942 pw.print(memInfo.nativePrivateDirty); pw.print(',');
943 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
944 pw.print(memInfo.otherPrivateDirty); pw.print(',');
945 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
946 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 // Object counts
949 pw.print(viewInstanceCount); pw.print(',');
950 pw.print(viewRootInstanceCount); pw.print(',');
951 pw.print(appContextInstanceCount); pw.print(',');
952 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 pw.print(globalAssetCount); pw.print(',');
955 pw.print(globalAssetManagerCount); pw.print(',');
956 pw.print(binderLocalObjectCount); pw.print(',');
957 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700958
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 pw.print(binderDeathObjectCount); pw.print(',');
960 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800963 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800964 pw.print(stats.memoryUsed / 1024); pw.print(',');
965 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700966 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800967 for (int i = 0; i < stats.dbStats.size(); i++) {
968 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700969 pw.print(','); pw.print(dbStats.dbName);
970 pw.print(','); pw.print(dbStats.pageSize);
971 pw.print(','); pw.print(dbStats.dbSize);
972 pw.print(','); pw.print(dbStats.lookaside);
973 pw.print(','); pw.print(dbStats.cache);
974 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800975 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700976 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700977
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700978 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
Bob Leee5408332009-09-04 18:31:17 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700982 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
983 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
984 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
985 "------");
986 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
987 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
988 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
989 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700991 int otherPss = memInfo.otherPss;
992 int otherSharedDirty = memInfo.otherSharedDirty;
993 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700995 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700996 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700997 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
998 memInfo.getOtherPrivateDirty(i), "", "", "");
999 otherPss -= memInfo.getOtherPss(i);
1000 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
1001 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
1002 }
1003
1004 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1005 otherPrivateDirty, "", "", "");
1006 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1007 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1008 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1009 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010
1011 pw.println(" ");
1012 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001013 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 viewRootInstanceCount);
1015
1016 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1017 "Activities:", activityInstanceCount);
1018
1019 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1020 "AssetManagers:", globalAssetManagerCount);
1021
1022 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1023 "Proxy Binders:", binderProxyObjectCount);
1024 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1025
1026 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 // SQLite mem info
1029 pw.println(" ");
1030 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001031 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1032 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1033 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001034 pw.println(" ");
1035 int N = stats.dbStats.size();
1036 if (N > 0) {
1037 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001038 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1039 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001040 for (int i = 0; i < N; i++) {
1041 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001042 printRow(pw, DB_INFO_FORMAT,
1043 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1044 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1045 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1046 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001047 }
1048 }
Bob Leee5408332009-09-04 18:31:17 -07001049
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001050 // Asset details.
1051 String assetAlloc = AssetManager.getAssetAllocations();
1052 if (assetAlloc != null) {
1053 pw.println(" ");
1054 pw.println(" Asset Allocations");
1055 pw.print(assetAlloc);
1056 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001057
1058 return memInfo;
1059 }
1060
1061 @Override
1062 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1063 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001064 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
1066
Jeff Brown6754ba22011-12-14 20:20:01 -08001067 @Override
1068 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1069 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1070 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1071 SQLiteDebug.dump(printer, args);
1072 pw.flush();
1073 }
1074
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001075 @Override
1076 public void unstableProviderDied(IBinder provider) {
1077 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1078 }
1079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 private void printRow(PrintWriter pw, String format, Object...objs) {
1081 pw.println(String.format(format, objs));
1082 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001083
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001084 public void setCoreSettings(Bundle coreSettings) {
1085 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001086 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001087
1088 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1089 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1090 ucd.pkg = pkg;
1091 ucd.info = info;
1092 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1093 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001094
1095 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001096 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001097 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 }
1100
Romain Guy65b345f2011-07-27 18:51:50 -07001101 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 public static final int LAUNCH_ACTIVITY = 100;
1103 public static final int PAUSE_ACTIVITY = 101;
1104 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1105 public static final int STOP_ACTIVITY_SHOW = 103;
1106 public static final int STOP_ACTIVITY_HIDE = 104;
1107 public static final int SHOW_WINDOW = 105;
1108 public static final int HIDE_WINDOW = 106;
1109 public static final int RESUME_ACTIVITY = 107;
1110 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001111 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 public static final int BIND_APPLICATION = 110;
1113 public static final int EXIT_APPLICATION = 111;
1114 public static final int NEW_INTENT = 112;
1115 public static final int RECEIVER = 113;
1116 public static final int CREATE_SERVICE = 114;
1117 public static final int SERVICE_ARGS = 115;
1118 public static final int STOP_SERVICE = 116;
1119 public static final int REQUEST_THUMBNAIL = 117;
1120 public static final int CONFIGURATION_CHANGED = 118;
1121 public static final int CLEAN_UP_CONTEXT = 119;
1122 public static final int GC_WHEN_IDLE = 120;
1123 public static final int BIND_SERVICE = 121;
1124 public static final int UNBIND_SERVICE = 122;
1125 public static final int DUMP_SERVICE = 123;
1126 public static final int LOW_MEMORY = 124;
1127 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1128 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001129 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001130 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001131 public static final int DESTROY_BACKUP_AGENT = 129;
1132 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001133 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001134 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001135 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001136 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001137 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001138 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001139 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001140 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001141 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001142 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001143 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001144 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001146 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 switch (code) {
1148 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1149 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1150 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1151 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1152 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1153 case SHOW_WINDOW: return "SHOW_WINDOW";
1154 case HIDE_WINDOW: return "HIDE_WINDOW";
1155 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1156 case SEND_RESULT: return "SEND_RESULT";
1157 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1158 case BIND_APPLICATION: return "BIND_APPLICATION";
1159 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1160 case NEW_INTENT: return "NEW_INTENT";
1161 case RECEIVER: return "RECEIVER";
1162 case CREATE_SERVICE: return "CREATE_SERVICE";
1163 case SERVICE_ARGS: return "SERVICE_ARGS";
1164 case STOP_SERVICE: return "STOP_SERVICE";
1165 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1166 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1167 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1168 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1169 case BIND_SERVICE: return "BIND_SERVICE";
1170 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1171 case DUMP_SERVICE: return "DUMP_SERVICE";
1172 case LOW_MEMORY: return "LOW_MEMORY";
1173 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1174 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001175 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001176 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1177 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001178 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001179 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001180 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001181 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001182 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001183 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001184 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001185 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001186 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001187 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001188 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001189 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001190 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001193 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001196 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 switch (msg.what) {
1198 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001199 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001200 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201
1202 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001203 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001204 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001205 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } break;
1207 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001208 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001209 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001210 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001211 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } break;
1213 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001216 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 break;
1219 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001220 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 break;
1224 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001225 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001232 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 break;
1234 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 break;
1244 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001245 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 handleResumeActivity((IBinder)msg.obj, true,
1247 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001248 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 break;
1250 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001251 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001253 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 break;
1255 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1258 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001259 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 break;
1261 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001262 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 AppBindData data = (AppBindData)msg.obj;
1264 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001265 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 break;
1267 case EXIT_APPLICATION:
1268 if (mInitialApplication != null) {
1269 mInitialApplication.onTerminate();
1270 }
1271 Looper.myLooper().quit();
1272 break;
1273 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001276 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 break;
1278 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001281 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 break;
1284 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001285 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001287 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 break;
1289 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001292 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001297 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 break;
1299 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 break;
1304 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001307 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001308 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 break;
1310 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001316 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001317 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001318 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001319 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 break;
1321 case CLEAN_UP_CONTEXT:
1322 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1323 cci.context.performFinalCleanup(cci.who, cci.what);
1324 break;
1325 case GC_WHEN_IDLE:
1326 scheduleGcIdler();
1327 break;
1328 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001329 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 break;
1331 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001332 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 break;
1336 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001341 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001342 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001343 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001344 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001345 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001346 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001348 break;
1349 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001350 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001351 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001352 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001353 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001354 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001355 Process.killProcess(Process.myPid());
1356 break;
1357 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001358 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001359 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001360 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001361 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001362 case ENABLE_JIT:
1363 ensureJitEnabled();
1364 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001365 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001366 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001367 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001368 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001369 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001370 case SCHEDULE_CRASH:
1371 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001372 case DUMP_HEAP:
1373 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1374 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001375 case DUMP_ACTIVITY:
1376 handleDumpActivity((DumpComponentInfo)msg.obj);
1377 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001378 case DUMP_PROVIDER:
1379 handleDumpProvider((DumpComponentInfo)msg.obj);
1380 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001381 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001382 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001383 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001384 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001385 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001386 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001387 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001388 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001390 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001391 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1392 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001393 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001394 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001396 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001397 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001398 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001399 case UNSTABLE_PROVIDER_DIED:
1400 handleUnstableProviderDied((IBinder)msg.obj, false);
1401 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001403 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 }
Bob Leee5408332009-09-04 18:31:17 -07001405
Brian Carlstromed7e0072011-03-24 13:27:57 -07001406 private void maybeSnapshot() {
1407 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001408 // convert the *private* ActivityThread.PackageInfo to *public* known
1409 // android.content.pm.PackageInfo
1410 String packageName = mBoundApplication.info.mPackageName;
1411 android.content.pm.PackageInfo packageInfo = null;
1412 try {
1413 Context context = getSystemContext();
1414 if(context == null) {
1415 Log.e(TAG, "cannot get a valid context");
1416 return;
1417 }
1418 PackageManager pm = context.getPackageManager();
1419 if(pm == null) {
1420 Log.e(TAG, "cannot get a valid PackageManager");
1421 return;
1422 }
1423 packageInfo = pm.getPackageInfo(
1424 packageName, PackageManager.GET_ACTIVITIES);
1425 } catch (NameNotFoundException e) {
1426 Log.e(TAG, "cannot get package info for " + packageName, e);
1427 }
1428 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001429 }
1430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432
Romain Guy65b345f2011-07-27 18:51:50 -07001433 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001435 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001436 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001437 if (mBoundApplication != null && mProfiler.profileFd != null
1438 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001439 stopProfiling = true;
1440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 if (a != null) {
1442 mNewActivities = null;
1443 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001444 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001446 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 TAG, "Reporting idle of " + a +
1448 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001449 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 if (a.activity != null && !a.activity.mFinished) {
1451 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001452 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001453 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001455 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 }
1457 }
1458 prev = a;
1459 a = a.nextIdle;
1460 prev.nextIdle = null;
1461 } while (a != null);
1462 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001463 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001464 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001465 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001466 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 return false;
1468 }
1469 }
1470
1471 final class GcIdler implements MessageQueue.IdleHandler {
1472 public final boolean queueIdle() {
1473 doGcIfNeeded();
1474 return false;
1475 }
1476 }
1477
Romain Guy65b345f2011-07-27 18:51:50 -07001478 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001479 final private String mResDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001480 final private int mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001481 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001482 final private float mScale;
1483 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001484
Jeff Browna492c3a2012-08-23 19:48:44 -07001485 ResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001486 mResDir = resDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001487 mDisplayId = displayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001488 if (overrideConfiguration != null) {
1489 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1490 overrideConfiguration = null;
1491 }
1492 }
1493 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001494 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001495 int hash = 17;
1496 hash = 31 * hash + mResDir.hashCode();
Jeff Browna492c3a2012-08-23 19:48:44 -07001497 hash = 31 * hash + mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001498 hash = 31 * hash + (mOverrideConfiguration != null
1499 ? mOverrideConfiguration.hashCode() : 0);
1500 hash = 31 * hash + Float.floatToIntBits(mScale);
1501 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001502 }
Bob Leee5408332009-09-04 18:31:17 -07001503
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001504 @Override
1505 public int hashCode() {
1506 return mHash;
1507 }
1508
1509 @Override
1510 public boolean equals(Object obj) {
1511 if (!(obj instanceof ResourcesKey)) {
1512 return false;
1513 }
1514 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001515 if (!mResDir.equals(peer.mResDir)) {
1516 return false;
1517 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001518 if (mDisplayId != peer.mDisplayId) {
1519 return false;
1520 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001521 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1522 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1523 return false;
1524 }
1525 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1526 return false;
1527 }
1528 }
1529 if (mScale != peer.mScale) {
1530 return false;
1531 }
1532 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001533 }
1534 }
1535
Romain Guy65b345f2011-07-27 18:51:50 -07001536 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001537 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539
Romain Guy65b345f2011-07-27 18:51:50 -07001540 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001541 ActivityThread am = currentActivityThread();
1542 return (am != null && am.mBoundApplication != null)
1543 ? am.mBoundApplication.processName : null;
1544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545
Romain Guy65b345f2011-07-27 18:51:50 -07001546 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001547 ActivityThread am = currentActivityThread();
1548 return am != null ? am.mInitialApplication : null;
1549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001551 public static IPackageManager getPackageManager() {
1552 if (sPackageManager != null) {
1553 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1554 return sPackageManager;
1555 }
1556 IBinder b = ServiceManager.getService("package");
1557 //Slog.v("PackageManager", "default service binder = " + b);
1558 sPackageManager = IPackageManager.Stub.asInterface(b);
1559 //Slog.v("PackageManager", "default service = " + sPackageManager);
1560 return sPackageManager;
1561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562
Jeff Browna492c3a2012-08-23 19:48:44 -07001563 private void flushDisplayMetricsLocked() {
1564 mDefaultDisplayMetrics.clear();
1565 }
1566
1567 DisplayMetrics getDisplayMetricsLocked(int displayId, CompatibilityInfo ci) {
1568 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1569 DisplayMetrics dm = isDefaultDisplay ? mDefaultDisplayMetrics.get(ci) : null;
1570 if (dm != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001571 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001572 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001573 dm = new DisplayMetrics();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001574
Jeff Brownbd6e1502012-08-28 03:27:37 -07001575 DisplayManagerGlobal displayManager = DisplayManagerGlobal.getInstance();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001576 if (displayManager == null) {
1577 // may be null early in system startup
Jeff Brown848c2dc2012-08-19 20:18:08 -07001578 dm.setToDefaults();
1579 return dm;
1580 }
1581
Jeff Browna492c3a2012-08-23 19:48:44 -07001582 if (isDefaultDisplay) {
1583 mDefaultDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001584 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001585
Jeff Brownd32460c2012-07-20 16:15:36 -07001586 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1587 cih.set(ci);
Jeff Browna492c3a2012-08-23 19:48:44 -07001588 Display d = displayManager.getCompatibleDisplay(displayId, cih);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001589 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001590 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1591 // + metrics.heightPixels + " den=" + metrics.density
1592 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001593 return dm;
1594 }
1595
Romain Guy65b345f2011-07-27 18:51:50 -07001596 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001597 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1598 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001599 if (config == null) {
1600 return null;
1601 }
1602 if (compat != null && !compat.supportsScreen()) {
1603 mMainThreadConfig.setTo(config);
1604 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001605 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001606 }
1607 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001610 /**
1611 * Creates the top level Resources for applications with the given compatibility info.
1612 *
1613 * @param resDir the resource directory.
1614 * @param compInfo the compability info. It will use the default compatibility info when it's
1615 * null.
1616 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001617 Resources getTopLevelResources(String resDir,
1618 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001619 CompatibilityInfo compInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001620 ResourcesKey key = new ResourcesKey(resDir,
1621 displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001622 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001623 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001625 // Resources is app scale dependent.
1626 if (false) {
1627 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1628 + compInfo.applicationScale);
1629 }
1630 WeakReference<Resources> wr = mActiveResources.get(key);
1631 r = wr != null ? wr.get() : null;
1632 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1633 if (r != null && r.getAssets().isUpToDate()) {
1634 if (false) {
1635 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1636 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1637 }
1638 return r;
1639 }
1640 }
1641
1642 //if (r != null) {
1643 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1644 // + r + " " + resDir);
1645 //}
1646
1647 AssetManager assets = new AssetManager();
1648 if (assets.addAssetPath(resDir) == 0) {
1649 return null;
1650 }
1651
1652 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Jeff Browna492c3a2012-08-23 19:48:44 -07001653 DisplayMetrics dm = getDisplayMetricsLocked(displayId, null);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001654 Configuration config;
Jeff Browna492c3a2012-08-23 19:48:44 -07001655 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1656 if (!isDefaultDisplay || key.mOverrideConfiguration != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07001657 config = new Configuration(getConfiguration());
Jeff Browna492c3a2012-08-23 19:48:44 -07001658 if (!isDefaultDisplay) {
1659 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
1660 }
1661 if (key.mOverrideConfiguration != null) {
1662 config.updateFrom(key.mOverrideConfiguration);
1663 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001664 } else {
1665 config = getConfiguration();
1666 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001667 r = new Resources(assets, dm, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001668 if (false) {
1669 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1670 + r.getConfiguration() + " appScale="
1671 + r.getCompatibilityInfo().applicationScale);
1672 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001673
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001674 synchronized (mPackages) {
1675 WeakReference<Resources> wr = mActiveResources.get(key);
1676 Resources existing = wr != null ? wr.get() : null;
1677 if (existing != null && existing.getAssets().isUpToDate()) {
1678 // Someone else already created the resources while we were
1679 // unlocked; go ahead and use theirs.
1680 r.getAssets().close();
1681 return existing;
1682 }
1683
1684 // XXX need to remove entries when weak references go away
1685 mActiveResources.put(key, new WeakReference<Resources>(r));
1686 return r;
1687 }
1688 }
1689
1690 /**
1691 * Creates the top level resources for the given package.
1692 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001693 Resources getTopLevelResources(String resDir,
1694 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001695 LoadedApk pkgInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001696 return getTopLevelResources(resDir, displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001697 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001698 }
1699
1700 final Handler getHandler() {
1701 return mH;
1702 }
1703
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001704 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1705 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001706 synchronized (mPackages) {
1707 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1709 ref = mPackages.get(packageName);
1710 } else {
1711 ref = mResourcePackages.get(packageName);
1712 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001713 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001714 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001715 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1716 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 if (packageInfo != null && (packageInfo.mResources == null
1718 || packageInfo.mResources.getAssets().isUpToDate())) {
1719 if (packageInfo.isSecurityViolation()
1720 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1721 throw new SecurityException(
1722 "Requesting code from " + packageName
1723 + " to be run in process "
1724 + mBoundApplication.processName
1725 + "/" + mBoundApplication.appInfo.uid);
1726 }
1727 return packageInfo;
1728 }
1729 }
1730
1731 ApplicationInfo ai = null;
1732 try {
1733 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001734 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001736 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 }
1738
1739 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001740 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 }
1742
1743 return null;
1744 }
1745
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001746 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1747 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1749 boolean securityViolation = includeCode && ai.uid != 0
1750 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001751 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001752 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1754 |Context.CONTEXT_IGNORE_SECURITY))
1755 == Context.CONTEXT_INCLUDE_CODE) {
1756 if (securityViolation) {
1757 String msg = "Requesting code from " + ai.packageName
1758 + " (with uid " + ai.uid + ")";
1759 if (mBoundApplication != null) {
1760 msg = msg + " to be run in process "
1761 + mBoundApplication.processName + " (with uid "
1762 + mBoundApplication.appInfo.uid + ")";
1763 }
1764 throw new SecurityException(msg);
1765 }
1766 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001767 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001770 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1771 CompatibilityInfo compatInfo) {
1772 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 }
1774
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001775 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1776 synchronized (mPackages) {
1777 WeakReference<LoadedApk> ref;
1778 if (includeCode) {
1779 ref = mPackages.get(packageName);
1780 } else {
1781 ref = mResourcePackages.get(packageName);
1782 }
1783 return ref != null ? ref.get() : null;
1784 }
1785 }
1786
Romain Guy65b345f2011-07-27 18:51:50 -07001787 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1789 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001790 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 if (includeCode) {
1792 ref = mPackages.get(aInfo.packageName);
1793 } else {
1794 ref = mResourcePackages.get(aInfo.packageName);
1795 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001796 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 if (packageInfo == null || (packageInfo.mResources != null
1798 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001799 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 : "Loading resource-only package ") + aInfo.packageName
1801 + " (in " + (mBoundApplication != null
1802 ? mBoundApplication.processName : null)
1803 + ")");
1804 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001805 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 securityViolation, includeCode &&
1807 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1808 if (includeCode) {
1809 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001810 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811 } else {
1812 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001813 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815 }
1816 return packageInfo;
1817 }
1818 }
1819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 ActivityThread() {
1821 }
1822
1823 public ApplicationThread getApplicationThread()
1824 {
1825 return mAppThread;
1826 }
1827
1828 public Instrumentation getInstrumentation()
1829 {
1830 return mInstrumentation;
1831 }
1832
1833 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001834 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 }
1836
1837 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001838 return mProfiler != null && mProfiler.profileFile != null
1839 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
1841
1842 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001843 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845
1846 public Looper getLooper() {
1847 return mLooper;
1848 }
1849
1850 public Application getApplication() {
1851 return mInitialApplication;
1852 }
Bob Leee5408332009-09-04 18:31:17 -07001853
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001854 public String getProcessName() {
1855 return mBoundApplication.processName;
1856 }
Bob Leee5408332009-09-04 18:31:17 -07001857
Dianne Hackborn21556372010-02-04 16:34:40 -08001858 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 synchronized (this) {
1860 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001861 ContextImpl context =
1862 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001863 LoadedApk info = new LoadedApk(this, "android", context, null,
1864 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 context.init(info, null, this);
1866 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001867 getConfiguration(), getDisplayMetricsLocked(
Jeff Browna492c3a2012-08-23 19:48:44 -07001868 Display.DEFAULT_DISPLAY,
1869 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001871 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 // + ": " + context.getResources().getConfiguration());
1873 }
1874 }
1875 return mSystemContext;
1876 }
1877
Mike Cleron432b7132009-09-24 15:28:29 -07001878 public void installSystemApplicationInfo(ApplicationInfo info) {
1879 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001880 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001881 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001882 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001883
1884 // give ourselves a default profiler
1885 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001886 }
1887 }
1888
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001889 void ensureJitEnabled() {
1890 if (!mJitEnabled) {
1891 mJitEnabled = true;
1892 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1893 }
1894 }
1895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 void scheduleGcIdler() {
1897 if (!mGcIdlerScheduled) {
1898 mGcIdlerScheduled = true;
1899 Looper.myQueue().addIdleHandler(mGcIdler);
1900 }
1901 mH.removeMessages(H.GC_WHEN_IDLE);
1902 }
1903
1904 void unscheduleGcIdler() {
1905 if (mGcIdlerScheduled) {
1906 mGcIdlerScheduled = false;
1907 Looper.myQueue().removeIdleHandler(mGcIdler);
1908 }
1909 mH.removeMessages(H.GC_WHEN_IDLE);
1910 }
1911
1912 void doGcIfNeeded() {
1913 mGcIdlerScheduled = false;
1914 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001915 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 // + "m now=" + now);
1917 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001918 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 BinderInternal.forceGc("bg");
1920 }
1921 }
1922
Jeff Hamilton52d32032011-01-08 15:31:26 -06001923 public void registerOnActivityPausedListener(Activity activity,
1924 OnActivityPausedListener listener) {
1925 synchronized (mOnPauseListeners) {
1926 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1927 if (list == null) {
1928 list = new ArrayList<OnActivityPausedListener>();
1929 mOnPauseListeners.put(activity, list);
1930 }
1931 list.add(listener);
1932 }
1933 }
1934
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001935 public void unregisterOnActivityPausedListener(Activity activity,
1936 OnActivityPausedListener listener) {
1937 synchronized (mOnPauseListeners) {
1938 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1939 if (list != null) {
1940 list.remove(listener);
1941 }
1942 }
1943 }
1944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 public final ActivityInfo resolveActivityInfo(Intent intent) {
1946 ActivityInfo aInfo = intent.resolveActivityInfo(
1947 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1948 if (aInfo == null) {
1949 // Throw an exception.
1950 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001951 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
1953 return aInfo;
1954 }
Bob Leee5408332009-09-04 18:31:17 -07001955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001958 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001959 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001961 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 r.intent = intent;
1963 r.state = state;
1964 r.parent = parent;
1965 r.embeddedID = id;
1966 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001967 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 if (localLOGV) {
1969 ComponentName compname = intent.getComponent();
1970 String name;
1971 if (compname != null) {
1972 name = compname.toShortString();
1973 } else {
1974 name = "(Intent " + intent + ").getComponent() returned null";
1975 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001976 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 + ", comp=" + name
1978 + ", token=" + token);
1979 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001980 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982
1983 public final Activity getActivity(IBinder token) {
1984 return mActivities.get(token).activity;
1985 }
1986
1987 public final void sendActivityResult(
1988 IBinder token, String id, int requestCode,
1989 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001990 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001991 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1993 list.add(new ResultInfo(id, requestCode, resultCode, data));
1994 mAppThread.scheduleSendResult(token, list);
1995 }
1996
1997 // if the thread hasn't started yet, we don't have the handler, so just
1998 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001999 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000 queueOrSendMessage(what, obj, 0, 0);
2001 }
2002
Romain Guy65b345f2011-07-27 18:51:50 -07002003 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 queueOrSendMessage(what, obj, arg1, 0);
2005 }
2006
Romain Guy65b345f2011-07-27 18:51:50 -07002007 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002009 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2011 + ": " + arg1 + " / " + obj);
2012 Message msg = Message.obtain();
2013 msg.what = what;
2014 msg.obj = obj;
2015 msg.arg1 = arg1;
2016 msg.arg2 = arg2;
2017 mH.sendMessage(msg);
2018 }
2019 }
2020
Dianne Hackborn21556372010-02-04 16:34:40 -08002021 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 String what) {
2023 ContextCleanupInfo cci = new ContextCleanupInfo();
2024 cci.context = context;
2025 cci.who = who;
2026 cci.what = what;
2027 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2028 }
2029
Romain Guy65b345f2011-07-27 18:51:50 -07002030 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2032
2033 ActivityInfo aInfo = r.activityInfo;
2034 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002035 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 Context.CONTEXT_INCLUDE_CODE);
2037 }
Bob Leee5408332009-09-04 18:31:17 -07002038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 ComponentName component = r.intent.getComponent();
2040 if (component == null) {
2041 component = r.intent.resolveActivity(
2042 mInitialApplication.getPackageManager());
2043 r.intent.setComponent(component);
2044 }
2045
2046 if (r.activityInfo.targetActivity != null) {
2047 component = new ComponentName(r.activityInfo.packageName,
2048 r.activityInfo.targetActivity);
2049 }
2050
2051 Activity activity = null;
2052 try {
2053 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2054 activity = mInstrumentation.newActivity(
2055 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002056 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 r.intent.setExtrasClassLoader(cl);
2058 if (r.state != null) {
2059 r.state.setClassLoader(cl);
2060 }
2061 } catch (Exception e) {
2062 if (!mInstrumentation.onException(activity, e)) {
2063 throw new RuntimeException(
2064 "Unable to instantiate activity " + component
2065 + ": " + e.toString(), e);
2066 }
2067 }
2068
2069 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002070 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002071
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002072 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2073 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002074 TAG, r + ": app=" + app
2075 + ", appName=" + app.getPackageName()
2076 + ", pkg=" + r.packageInfo.getPackageName()
2077 + ", comp=" + r.intent.getComponent().toShortString()
2078 + ", dir=" + r.packageInfo.getAppDir());
2079
2080 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002081 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 appContext.init(r.packageInfo, r.token, this);
2083 appContext.setOuterContext(activity);
2084 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002085 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002086 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002087 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002088 activity.attach(appContext, this, getInstrumentation(), r.token,
2089 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002090 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002091
Christopher Tateb70f3df2009-04-07 16:07:59 -07002092 if (customIntent != null) {
2093 activity.mIntent = customIntent;
2094 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002095 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 activity.mStartedActivity = false;
2097 int theme = r.activityInfo.getThemeResource();
2098 if (theme != 0) {
2099 activity.setTheme(theme);
2100 }
2101
2102 activity.mCalled = false;
2103 mInstrumentation.callActivityOnCreate(activity, r.state);
2104 if (!activity.mCalled) {
2105 throw new SuperNotCalledException(
2106 "Activity " + r.intent.getComponent().toShortString() +
2107 " did not call through to super.onCreate()");
2108 }
2109 r.activity = activity;
2110 r.stopped = true;
2111 if (!r.activity.mFinished) {
2112 activity.performStart();
2113 r.stopped = false;
2114 }
2115 if (!r.activity.mFinished) {
2116 if (r.state != null) {
2117 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2118 }
2119 }
2120 if (!r.activity.mFinished) {
2121 activity.mCalled = false;
2122 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2123 if (!activity.mCalled) {
2124 throw new SuperNotCalledException(
2125 "Activity " + r.intent.getComponent().toShortString() +
2126 " did not call through to super.onPostCreate()");
2127 }
2128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002129 }
2130 r.paused = true;
2131
2132 mActivities.put(r.token, r);
2133
2134 } catch (SuperNotCalledException e) {
2135 throw e;
2136
2137 } catch (Exception e) {
2138 if (!mInstrumentation.onException(activity, e)) {
2139 throw new RuntimeException(
2140 "Unable to start activity " + component
2141 + ": " + e.toString(), e);
2142 }
2143 }
2144
2145 return activity;
2146 }
2147
Romain Guy65b345f2011-07-27 18:51:50 -07002148 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 // If we are getting ready to gc after going to the background, well
2150 // we are back active so skip it.
2151 unscheduleGcIdler();
2152
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002153 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002154 mProfiler.setProfiler(r.profileFile, r.profileFd);
2155 mProfiler.startProfiling();
2156 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002157 }
2158
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002159 // Make sure we are running with the most recent config.
2160 handleConfigurationChanged(null, null);
2161
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002162 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002164 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165
2166 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002167 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002168 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 handleResumeActivity(r.token, false, r.isForward);
2170
2171 if (!r.activity.mFinished && r.startsNotResumed) {
2172 // The activity manager actually wants this one to start out
2173 // paused, because it needs to be visible but isn't in the
2174 // foreground. We accomplish this by going through the
2175 // normal startup (because activities expect to go through
2176 // onResume() the first time they run, before their window
2177 // is displayed), and then pausing it. However, in this case
2178 // we do -not- need to do the full pause cycle (of freezing
2179 // and such) because the activity manager assumes it can just
2180 // retain the current state it has.
2181 try {
2182 r.activity.mCalled = false;
2183 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002184 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002185 // we need to be created again. But we only do this
2186 // for pre-Honeycomb apps, which always save their state
2187 // when pausing, so we can not have them save their state
2188 // when restarting from a paused state. For HC and later,
2189 // we want to (and can) let the state be saved as the normal
2190 // part of stopping the activity.
2191 if (r.isPreHoneycomb()) {
2192 r.state = oldState;
2193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 if (!r.activity.mCalled) {
2195 throw new SuperNotCalledException(
2196 "Activity " + r.intent.getComponent().toShortString() +
2197 " did not call through to super.onPause()");
2198 }
2199
2200 } catch (SuperNotCalledException e) {
2201 throw e;
2202
2203 } catch (Exception e) {
2204 if (!mInstrumentation.onException(r.activity, e)) {
2205 throw new RuntimeException(
2206 "Unable to pause activity "
2207 + r.intent.getComponent().toShortString()
2208 + ": " + e.toString(), e);
2209 }
2210 }
2211 r.paused = true;
2212 }
2213 } else {
2214 // If there was an error, for any reason, tell the activity
2215 // manager to stop us.
2216 try {
2217 ActivityManagerNative.getDefault()
2218 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2219 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002220 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 }
2222 }
2223 }
2224
Romain Guy65b345f2011-07-27 18:51:50 -07002225 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 List<Intent> intents) {
2227 final int N = intents.size();
2228 for (int i=0; i<N; i++) {
2229 Intent intent = intents.get(i);
2230 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002231 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2233 }
2234 }
2235
2236 public final void performNewIntents(IBinder token,
2237 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002238 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 if (r != null) {
2240 final boolean resumed = !r.paused;
2241 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002242 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 mInstrumentation.callActivityOnPause(r.activity);
2244 }
2245 deliverNewIntents(r, intents);
2246 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002247 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002248 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 }
2250 }
2251 }
Bob Leee5408332009-09-04 18:31:17 -07002252
Romain Guy65b345f2011-07-27 18:51:50 -07002253 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 performNewIntents(data.token, data.intents);
2255 }
2256
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002257 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2258
2259 /**
2260 * Return the Intent that's currently being handled by a
2261 * BroadcastReceiver on this thread, or null if none.
2262 * @hide
2263 */
2264 public static Intent getIntentBeingBroadcast() {
2265 return sCurrentBroadcastIntent.get();
2266 }
2267
Romain Guy65b345f2011-07-27 18:51:50 -07002268 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002269 // If we are getting ready to gc after going to the background, well
2270 // we are back active so skip it.
2271 unscheduleGcIdler();
2272
2273 String component = data.intent.getComponent().getClassName();
2274
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002275 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002276 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277
2278 IActivityManager mgr = ActivityManagerNative.getDefault();
2279
Romain Guy65b345f2011-07-27 18:51:50 -07002280 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 try {
2282 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2283 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002284 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2286 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002287 if (DEBUG_BROADCAST) Slog.i(TAG,
2288 "Finishing failed broadcast to " + data.intent.getComponent());
2289 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002290 throw new RuntimeException(
2291 "Unable to instantiate receiver " + component
2292 + ": " + e.toString(), e);
2293 }
2294
2295 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002296 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002297
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002298 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 TAG, "Performing receive of " + data.intent
2300 + ": app=" + app
2301 + ", appName=" + app.getPackageName()
2302 + ", pkg=" + packageInfo.getPackageName()
2303 + ", comp=" + data.intent.getComponent().toShortString()
2304 + ", dir=" + packageInfo.getAppDir());
2305
Dianne Hackborn21556372010-02-04 16:34:40 -08002306 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002307 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002308 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 receiver.onReceive(context.getReceiverRestrictedContext(),
2310 data.intent);
2311 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002312 if (DEBUG_BROADCAST) Slog.i(TAG,
2313 "Finishing failed broadcast to " + data.intent.getComponent());
2314 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 if (!mInstrumentation.onException(receiver, e)) {
2316 throw new RuntimeException(
2317 "Unable to start receiver " + component
2318 + ": " + e.toString(), e);
2319 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002320 } finally {
2321 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002322 }
2323
Dianne Hackborne829fef2010-10-26 17:44:01 -07002324 if (receiver.getPendingResult() != null) {
2325 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 }
2327 }
2328
Christopher Tate181fafa2009-05-14 11:12:14 -07002329 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002330 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002331 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002332
2333 // no longer idle; we have backup work to do
2334 unscheduleGcIdler();
2335
2336 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002337 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002338 String packageName = packageInfo.mPackageName;
2339 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002340 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002341 + " already exists");
2342 return;
2343 }
Bob Leee5408332009-09-04 18:31:17 -07002344
Christopher Tate181fafa2009-05-14 11:12:14 -07002345 BackupAgent agent = null;
2346 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002347
Christopher Tate79ec80d2011-06-24 14:58:49 -07002348 // full backup operation but no app-supplied agent? use the default implementation
2349 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2350 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002351 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002352 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002353
Christopher Tate181fafa2009-05-14 11:12:14 -07002354 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002355 IBinder binder = null;
2356 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002357 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2358
Christopher Tated1475e02009-07-09 15:36:17 -07002359 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002360 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002361
2362 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002363 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002364 context.init(packageInfo, null, this);
2365 context.setOuterContext(agent);
2366 agent.attach(context);
2367
2368 agent.onCreate();
2369 binder = agent.onBind();
2370 mBackupAgents.put(packageName, agent);
2371 } catch (Exception e) {
2372 // If this is during restore, fail silently; otherwise go
2373 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002374 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002375 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2376 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002377 throw e;
2378 }
2379 // falling through with 'binder' still null
2380 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002381
2382 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002383 try {
2384 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2385 } catch (RemoteException e) {
2386 // nothing to do.
2387 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002388 } catch (Exception e) {
2389 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002390 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002391 }
2392 }
2393
2394 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002395 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002396 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002397
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002398 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002399 String packageName = packageInfo.mPackageName;
2400 BackupAgent agent = mBackupAgents.get(packageName);
2401 if (agent != null) {
2402 try {
2403 agent.onDestroy();
2404 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002405 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002406 e.printStackTrace();
2407 }
2408 mBackupAgents.remove(packageName);
2409 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002410 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002411 }
2412 }
2413
Romain Guy65b345f2011-07-27 18:51:50 -07002414 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 // If we are getting ready to gc after going to the background, well
2416 // we are back active so skip it.
2417 unscheduleGcIdler();
2418
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002419 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002420 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 Service service = null;
2422 try {
2423 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2424 service = (Service) cl.loadClass(data.info.name).newInstance();
2425 } catch (Exception e) {
2426 if (!mInstrumentation.onException(service, e)) {
2427 throw new RuntimeException(
2428 "Unable to instantiate service " + data.info.name
2429 + ": " + e.toString(), e);
2430 }
2431 }
2432
2433 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002434 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435
Dianne Hackborn21556372010-02-04 16:34:40 -08002436 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 context.init(packageInfo, null, this);
2438
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002439 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 context.setOuterContext(service);
2441 service.attach(context, this, data.info.name, data.token, app,
2442 ActivityManagerNative.getDefault());
2443 service.onCreate();
2444 mServices.put(data.token, service);
2445 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002446 ActivityManagerNative.getDefault().serviceDoneExecuting(
2447 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 } catch (RemoteException e) {
2449 // nothing to do.
2450 }
2451 } catch (Exception e) {
2452 if (!mInstrumentation.onException(service, e)) {
2453 throw new RuntimeException(
2454 "Unable to create service " + data.info.name
2455 + ": " + e.toString(), e);
2456 }
2457 }
2458 }
2459
Romain Guy65b345f2011-07-27 18:51:50 -07002460 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002462 if (DEBUG_SERVICE)
2463 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (s != null) {
2465 try {
2466 data.intent.setExtrasClassLoader(s.getClassLoader());
2467 try {
2468 if (!data.rebind) {
2469 IBinder binder = s.onBind(data.intent);
2470 ActivityManagerNative.getDefault().publishService(
2471 data.token, data.intent, binder);
2472 } else {
2473 s.onRebind(data.intent);
2474 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002475 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002477 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 } catch (RemoteException ex) {
2479 }
2480 } catch (Exception e) {
2481 if (!mInstrumentation.onException(s, e)) {
2482 throw new RuntimeException(
2483 "Unable to bind to service " + s
2484 + " with " + data.intent + ": " + e.toString(), e);
2485 }
2486 }
2487 }
2488 }
2489
Romain Guy65b345f2011-07-27 18:51:50 -07002490 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 Service s = mServices.get(data.token);
2492 if (s != null) {
2493 try {
2494 data.intent.setExtrasClassLoader(s.getClassLoader());
2495 boolean doRebind = s.onUnbind(data.intent);
2496 try {
2497 if (doRebind) {
2498 ActivityManagerNative.getDefault().unbindFinished(
2499 data.token, data.intent, doRebind);
2500 } else {
2501 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002502 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 }
2504 } catch (RemoteException ex) {
2505 }
2506 } catch (Exception e) {
2507 if (!mInstrumentation.onException(s, e)) {
2508 throw new RuntimeException(
2509 "Unable to unbind to service " + s
2510 + " with " + data.intent + ": " + e.toString(), e);
2511 }
2512 }
2513 }
2514 }
2515
Dianne Hackborn625ac272010-09-17 18:29:22 -07002516 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002517 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2518 try {
2519 Service s = mServices.get(info.token);
2520 if (s != null) {
2521 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2522 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2523 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002525 } finally {
2526 IoUtils.closeQuietly(info.fd);
2527 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002528 }
2529 }
2530
Dianne Hackborn625ac272010-09-17 18:29:22 -07002531 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002532 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2533 try {
2534 ActivityClientRecord r = mActivities.get(info.token);
2535 if (r != null && r.activity != null) {
2536 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2537 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2538 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002539 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002540 } finally {
2541 IoUtils.closeQuietly(info.fd);
2542 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002543 }
2544 }
2545
Marco Nelissen18cb2872011-11-15 11:19:53 -08002546 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002547 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2548 try {
2549 ProviderClientRecord r = mLocalProviders.get(info.token);
2550 if (r != null && r.mLocalProvider != null) {
2551 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2552 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2553 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002554 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002555 } finally {
2556 IoUtils.closeQuietly(info.fd);
2557 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002558 }
2559 }
2560
Romain Guy65b345f2011-07-27 18:51:50 -07002561 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 Service s = mServices.get(data.token);
2563 if (s != null) {
2564 try {
2565 if (data.args != null) {
2566 data.args.setExtrasClassLoader(s.getClassLoader());
2567 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002568 int res;
2569 if (!data.taskRemoved) {
2570 res = s.onStartCommand(data.args, data.flags, data.startId);
2571 } else {
2572 s.onTaskRemoved(data.args);
2573 res = Service.START_TASK_REMOVED_COMPLETE;
2574 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002575
2576 QueuedWork.waitToFinish();
2577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002579 ActivityManagerNative.getDefault().serviceDoneExecuting(
2580 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 } catch (RemoteException e) {
2582 // nothing to do.
2583 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002584 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 } catch (Exception e) {
2586 if (!mInstrumentation.onException(s, e)) {
2587 throw new RuntimeException(
2588 "Unable to start service " + s
2589 + " with " + data.args + ": " + e.toString(), e);
2590 }
2591 }
2592 }
2593 }
2594
Romain Guy65b345f2011-07-27 18:51:50 -07002595 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 Service s = mServices.remove(token);
2597 if (s != null) {
2598 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002599 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 s.onDestroy();
2601 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002602 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002604 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002606
2607 QueuedWork.waitToFinish();
2608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002610 ActivityManagerNative.getDefault().serviceDoneExecuting(
2611 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 } catch (RemoteException e) {
2613 // nothing to do.
2614 }
2615 } catch (Exception e) {
2616 if (!mInstrumentation.onException(s, e)) {
2617 throw new RuntimeException(
2618 "Unable to stop service " + s
2619 + ": " + e.toString(), e);
2620 }
2621 }
2622 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002623 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 }
2625
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002626 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002627 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002628 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002629 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 + " finished=" + r.activity.mFinished);
2631 if (r != null && !r.activity.mFinished) {
2632 if (clearHide) {
2633 r.hideForNow = false;
2634 r.activity.mStartedActivity = false;
2635 }
2636 try {
2637 if (r.pendingIntents != null) {
2638 deliverNewIntents(r, r.pendingIntents);
2639 r.pendingIntents = null;
2640 }
2641 if (r.pendingResults != null) {
2642 deliverResults(r, r.pendingResults);
2643 r.pendingResults = null;
2644 }
2645 r.activity.performResume();
2646
Bob Leee5408332009-09-04 18:31:17 -07002647 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 r.paused = false;
2651 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002652 r.state = null;
2653 } catch (Exception e) {
2654 if (!mInstrumentation.onException(r.activity, e)) {
2655 throw new RuntimeException(
2656 "Unable to resume activity "
2657 + r.intent.getComponent().toShortString()
2658 + ": " + e.toString(), e);
2659 }
2660 }
2661 }
2662 return r;
2663 }
2664
Romain Guya998dff2012-03-23 18:58:36 -07002665 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002666 if (r.mPendingRemoveWindow != null) {
2667 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2668 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2669 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002670 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002671 r.activity.getClass().getName(), "Activity");
2672 }
2673 }
2674 r.mPendingRemoveWindow = null;
2675 r.mPendingRemoveWindowManager = null;
2676 }
2677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2679 // If we are getting ready to gc after going to the background, well
2680 // we are back active so skip it.
2681 unscheduleGcIdler();
2682
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002683 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684
2685 if (r != null) {
2686 final Activity a = r.activity;
2687
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002688 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 TAG, "Resume " + r + " started activity: " +
2690 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2691 + ", finished: " + a.mFinished);
2692
2693 final int forwardBit = isForward ?
2694 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 // If the window hasn't yet been added to the window manager,
2697 // and this guy didn't finish itself or start another activity,
2698 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002699 boolean willBeVisible = !a.mStartedActivity;
2700 if (!willBeVisible) {
2701 try {
2702 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2703 a.getActivityToken());
2704 } catch (RemoteException e) {
2705 }
2706 }
2707 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 r.window = r.activity.getWindow();
2709 View decor = r.window.getDecorView();
2710 decor.setVisibility(View.INVISIBLE);
2711 ViewManager wm = a.getWindowManager();
2712 WindowManager.LayoutParams l = r.window.getAttributes();
2713 a.mDecor = decor;
2714 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2715 l.softInputMode |= forwardBit;
2716 if (a.mVisibleFromClient) {
2717 a.mWindowAdded = true;
2718 wm.addView(decor, l);
2719 }
2720
2721 // If the window has already been added, but during resume
2722 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002723 // window visible.
2724 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002725 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 TAG, "Launch " + r + " mStartedActivity set");
2727 r.hideForNow = true;
2728 }
2729
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002730 // Get rid of anything left hanging around.
2731 cleanUpPendingRemoveWindows(r);
2732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 // The window is now visible if it has been added, we are not
2734 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002735 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002736 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002738 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002739 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002741 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 r.newConfig = null;
2743 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002744 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 + isForward);
2746 WindowManager.LayoutParams l = r.window.getAttributes();
2747 if ((l.softInputMode
2748 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2749 != forwardBit) {
2750 l.softInputMode = (l.softInputMode
2751 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2752 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002753 if (r.activity.mVisibleFromClient) {
2754 ViewManager wm = a.getWindowManager();
2755 View decor = r.window.getDecorView();
2756 wm.updateViewLayout(decor, l);
2757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 }
2759 r.activity.mVisibleFromServer = true;
2760 mNumVisibleActivities++;
2761 if (r.activity.mVisibleFromClient) {
2762 r.activity.makeVisible();
2763 }
2764 }
2765
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002766 if (!r.onlyLocalRequest) {
2767 r.nextIdle = mNewActivities;
2768 mNewActivities = r;
2769 if (localLOGV) Slog.v(
2770 TAG, "Scheduling idle handler for " + r);
2771 Looper.myQueue().addIdleHandler(new Idler());
2772 }
2773 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774
2775 } else {
2776 // If an exception was thrown when trying to resume, then
2777 // just end this activity.
2778 try {
2779 ActivityManagerNative.getDefault()
2780 .finishActivity(token, Activity.RESULT_CANCELED, null);
2781 } catch (RemoteException ex) {
2782 }
2783 }
2784 }
2785
2786 private int mThumbnailWidth = -1;
2787 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002788 private Bitmap mAvailThumbnailBitmap = null;
2789 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790
Romain Guy65b345f2011-07-27 18:51:50 -07002791 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002792 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002794 if (thumbnail == null) {
2795 int w = mThumbnailWidth;
2796 int h;
2797 if (w < 0) {
2798 Resources res = r.activity.getResources();
2799 mThumbnailHeight = h =
2800 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002802 mThumbnailWidth = w =
2803 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2804 } else {
2805 h = mThumbnailHeight;
2806 }
2807
2808 // On platforms where we don't want thumbnails, set dims to (0,0)
2809 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002810 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2811 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002812 thumbnail.eraseColor(0);
2813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 }
2815
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002816 if (thumbnail != null) {
2817 Canvas cv = mThumbnailCanvas;
2818 if (cv == null) {
2819 mThumbnailCanvas = cv = new Canvas();
2820 }
2821
2822 cv.setBitmap(thumbnail);
2823 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2824 mAvailThumbnailBitmap = thumbnail;
2825 thumbnail = null;
2826 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002827 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 } catch (Exception e) {
2831 if (!mInstrumentation.onException(r.activity, e)) {
2832 throw new RuntimeException(
2833 "Unable to create thumbnail of "
2834 + r.intent.getComponent().toShortString()
2835 + ": " + e.toString(), e);
2836 }
2837 thumbnail = null;
2838 }
2839
2840 return thumbnail;
2841 }
2842
Romain Guy65b345f2011-07-27 18:51:50 -07002843 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002845 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002847 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 if (userLeaving) {
2849 performUserLeavingActivity(r);
2850 }
Bob Leee5408332009-09-04 18:31:17 -07002851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002853 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002855 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002856 if (r.isPreHoneycomb()) {
2857 QueuedWork.waitToFinish();
2858 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 // Tell the activity manager we have paused.
2861 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002862 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 } catch (RemoteException ex) {
2864 }
2865 }
2866 }
2867
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002868 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002869 mInstrumentation.callActivityOnUserLeaving(r.activity);
2870 }
2871
2872 final Bundle performPauseActivity(IBinder token, boolean finished,
2873 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002874 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 return r != null ? performPauseActivity(r, finished, saveState) : null;
2876 }
2877
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002878 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 boolean saveState) {
2880 if (r.paused) {
2881 if (r.activity.mFinished) {
2882 // If we are finishing, we won't call onResume() in certain cases.
2883 // So here we likewise don't want to call onPause() if the activity
2884 // isn't resumed.
2885 return null;
2886 }
2887 RuntimeException e = new RuntimeException(
2888 "Performing pause of activity that is not resumed: "
2889 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002890 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 }
2892 Bundle state = null;
2893 if (finished) {
2894 r.activity.mFinished = true;
2895 }
2896 try {
2897 // Next have the activity save its current state and managed dialogs...
2898 if (!r.activity.mFinished && saveState) {
2899 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002900 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2902 r.state = state;
2903 }
2904 // Now we are idle.
2905 r.activity.mCalled = false;
2906 mInstrumentation.callActivityOnPause(r.activity);
2907 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2908 if (!r.activity.mCalled) {
2909 throw new SuperNotCalledException(
2910 "Activity " + r.intent.getComponent().toShortString() +
2911 " did not call through to super.onPause()");
2912 }
2913
2914 } catch (SuperNotCalledException e) {
2915 throw e;
2916
2917 } catch (Exception e) {
2918 if (!mInstrumentation.onException(r.activity, e)) {
2919 throw new RuntimeException(
2920 "Unable to pause activity "
2921 + r.intent.getComponent().toShortString()
2922 + ": " + e.toString(), e);
2923 }
2924 }
2925 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002926
2927 // Notify any outstanding on paused listeners
2928 ArrayList<OnActivityPausedListener> listeners;
2929 synchronized (mOnPauseListeners) {
2930 listeners = mOnPauseListeners.remove(r.activity);
2931 }
2932 int size = (listeners != null ? listeners.size() : 0);
2933 for (int i = 0; i < size; i++) {
2934 listeners.get(i).onPaused(r.activity);
2935 }
2936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 return state;
2938 }
2939
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002940 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002941 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002942 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 }
2944
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002945 private static class StopInfo implements Runnable {
2946 ActivityClientRecord activity;
2947 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 Bitmap thumbnail;
2949 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002950
2951 @Override public void run() {
2952 // Tell activity manager we have been stopped.
2953 try {
2954 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2955 ActivityManagerNative.getDefault().activityStopped(
2956 activity.token, state, thumbnail, description);
2957 } catch (RemoteException ex) {
2958 }
2959 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
2961
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002962 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002963 public final IActivityManager.ContentProviderHolder holder;
2964 public final ProviderClientRecord client;
2965 public int stableCount;
2966 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002967
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002968 // When this is set, the stable and unstable ref counts are 0 and
2969 // we have a pending operation scheduled to remove the ref count
2970 // from the activity manager. On the activity manager we are still
2971 // holding an unstable ref, though it is not reflected in the counts
2972 // here.
2973 public boolean removePending;
2974
2975 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2976 ProviderClientRecord inClient, int sCount, int uCount) {
2977 holder = inHolder;
2978 client = inClient;
2979 stableCount = sCount;
2980 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 }
2982 }
2983
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002984 /**
2985 * Core implementation of stopping an activity. Note this is a little
2986 * tricky because the server's meaning of stop is slightly different
2987 * than our client -- for the server, stop means to save state and give
2988 * it the result when it is done, but the window may still be visible.
2989 * For the client, we want to call onStop()/onStart() to indicate when
2990 * the activity's UI visibillity changes.
2991 */
Romain Guy65b345f2011-07-27 18:51:50 -07002992 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002993 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002994 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002995 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 if (r != null) {
2997 if (!keepShown && r.stopped) {
2998 if (r.activity.mFinished) {
2999 // If we are finishing, we won't call onResume() in certain
3000 // cases. So here we likewise don't want to call onStop()
3001 // if the activity isn't resumed.
3002 return;
3003 }
3004 RuntimeException e = new RuntimeException(
3005 "Performing stop of activity that is not resumed: "
3006 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003007 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 }
3009
3010 if (info != null) {
3011 try {
3012 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003013 // For now, don't create the thumbnail here; we are
3014 // doing that by doing a screen snapshot.
3015 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 info.description = r.activity.onCreateDescription();
3017 } catch (Exception e) {
3018 if (!mInstrumentation.onException(r.activity, e)) {
3019 throw new RuntimeException(
3020 "Unable to save state of activity "
3021 + r.intent.getComponent().toShortString()
3022 + ": " + e.toString(), e);
3023 }
3024 }
3025 }
3026
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003027 // Next have the activity save its current state and managed dialogs...
3028 if (!r.activity.mFinished && saveState) {
3029 if (r.state == null) {
3030 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003031 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003032 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3033 r.state = state;
3034 } else {
3035 state = r.state;
3036 }
3037 }
3038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 if (!keepShown) {
3040 try {
3041 // Now we are idle.
3042 r.activity.performStop();
3043 } catch (Exception e) {
3044 if (!mInstrumentation.onException(r.activity, e)) {
3045 throw new RuntimeException(
3046 "Unable to stop activity "
3047 + r.intent.getComponent().toShortString()
3048 + ": " + e.toString(), e);
3049 }
3050 }
3051 r.stopped = true;
3052 }
3053
3054 r.paused = true;
3055 }
3056 }
3057
Romain Guy65b345f2011-07-27 18:51:50 -07003058 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 View v = r.activity.mDecor;
3060 if (v != null) {
3061 if (show) {
3062 if (!r.activity.mVisibleFromServer) {
3063 r.activity.mVisibleFromServer = true;
3064 mNumVisibleActivities++;
3065 if (r.activity.mVisibleFromClient) {
3066 r.activity.makeVisible();
3067 }
3068 }
3069 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003070 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003071 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003073 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 r.newConfig = null;
3075 }
3076 } else {
3077 if (r.activity.mVisibleFromServer) {
3078 r.activity.mVisibleFromServer = false;
3079 mNumVisibleActivities--;
3080 v.setVisibility(View.INVISIBLE);
3081 }
3082 }
3083 }
3084 }
3085
Romain Guy65b345f2011-07-27 18:51:50 -07003086 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003087 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 r.activity.mConfigChangeFlags |= configChanges;
3089
3090 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003091 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003093 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 TAG, "Finishing stop of " + r + ": show=" + show
3095 + " win=" + r.window);
3096
3097 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003098
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003099 // Make sure any pending writes are now committed.
3100 if (!r.isPreHoneycomb()) {
3101 QueuedWork.waitToFinish();
3102 }
3103
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003104 // Schedule the call to tell the activity manager we have
3105 // stopped. We don't do this immediately, because we want to
3106 // have a chance for any other pending work (in particular memory
3107 // trim requests) to complete before you tell the activity
3108 // manager to proceed and allow us to go fully into the background.
3109 info.activity = r;
3110 info.state = r.state;
3111 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 }
3113
3114 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003115 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003116 if (r.stopped) {
3117 r.activity.performRestart();
3118 r.stopped = false;
3119 }
3120 }
3121
Romain Guy65b345f2011-07-27 18:51:50 -07003122 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003123 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003124
3125 if (r == null) {
3126 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3127 return;
3128 }
3129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003131 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003132 } else if (show && r.stopped) {
3133 // If we are getting ready to gc after going to the background, well
3134 // we are back active so skip it.
3135 unscheduleGcIdler();
3136
3137 r.activity.performRestart();
3138 r.stopped = false;
3139 }
3140 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003141 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 TAG, "Handle window " + r + " visibility: " + show);
3143 updateVisibility(r, show);
3144 }
3145 }
3146
Romain Guy65b345f2011-07-27 18:51:50 -07003147 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003148 ActivityClientRecord r = mActivities.get(token);
3149
3150 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003151 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003152 return;
3153 }
3154
3155 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003156 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003157 try {
3158 // Now we are idle.
3159 r.activity.performStop();
3160 } catch (Exception e) {
3161 if (!mInstrumentation.onException(r.activity, e)) {
3162 throw new RuntimeException(
3163 "Unable to stop activity "
3164 + r.intent.getComponent().toShortString()
3165 + ": " + e.toString(), e);
3166 }
3167 }
3168 r.stopped = true;
3169 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003170
3171 // Make sure any pending writes are now committed.
3172 if (!r.isPreHoneycomb()) {
3173 QueuedWork.waitToFinish();
3174 }
3175
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003176 // Tell activity manager we slept.
3177 try {
3178 ActivityManagerNative.getDefault().activitySlept(r.token);
3179 } catch (RemoteException ex) {
3180 }
3181 } else {
3182 if (r.stopped && r.activity.mVisibleFromServer) {
3183 r.activity.performRestart();
3184 r.stopped = false;
3185 }
3186 }
3187 }
3188
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003189 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003190 synchronized (mPackages) {
3191 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003192 }
3193 }
3194
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003195 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3196 LoadedApk apk = peekPackageInfo(data.pkg, false);
3197 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003198 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003199 }
3200 apk = peekPackageInfo(data.pkg, true);
3201 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003202 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003203 }
3204 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003205 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003206 }
3207
Romain Guy65b345f2011-07-27 18:51:50 -07003208 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 final int N = results.size();
3210 for (int i=0; i<N; i++) {
3211 ResultInfo ri = results.get(i);
3212 try {
3213 if (ri.mData != null) {
3214 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3215 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003216 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003217 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003218 r.activity.dispatchActivityResult(ri.mResultWho,
3219 ri.mRequestCode, ri.mResultCode, ri.mData);
3220 } catch (Exception e) {
3221 if (!mInstrumentation.onException(r.activity, e)) {
3222 throw new RuntimeException(
3223 "Failure delivering result " + ri + " to activity "
3224 + r.intent.getComponent().toShortString()
3225 + ": " + e.toString(), e);
3226 }
3227 }
3228 }
3229 }
3230
Romain Guy65b345f2011-07-27 18:51:50 -07003231 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003232 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003233 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 if (r != null) {
3235 final boolean resumed = !r.paused;
3236 if (!r.activity.mFinished && r.activity.mDecor != null
3237 && r.hideForNow && resumed) {
3238 // We had hidden the activity because it started another
3239 // one... we have gotten a result back and we are not
3240 // paused, so make sure our window is visible.
3241 updateVisibility(r, true);
3242 }
3243 if (resumed) {
3244 try {
3245 // Now we are idle.
3246 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003247 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 mInstrumentation.callActivityOnPause(r.activity);
3249 if (!r.activity.mCalled) {
3250 throw new SuperNotCalledException(
3251 "Activity " + r.intent.getComponent().toShortString()
3252 + " did not call through to super.onPause()");
3253 }
3254 } catch (SuperNotCalledException e) {
3255 throw e;
3256 } catch (Exception e) {
3257 if (!mInstrumentation.onException(r.activity, e)) {
3258 throw new RuntimeException(
3259 "Unable to pause activity "
3260 + r.intent.getComponent().toShortString()
3261 + ": " + e.toString(), e);
3262 }
3263 }
3264 }
3265 deliverResults(r, res.results);
3266 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003267 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003268 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 }
3270 }
3271 }
3272
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003273 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 return performDestroyActivity(token, finishing, 0, false);
3275 }
3276
Romain Guy65b345f2011-07-27 18:51:50 -07003277 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003279 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003280 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003281 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003283 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 r.activity.mConfigChangeFlags |= configChanges;
3285 if (finishing) {
3286 r.activity.mFinished = true;
3287 }
3288 if (!r.paused) {
3289 try {
3290 r.activity.mCalled = false;
3291 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003292 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 r.activity.getComponentName().getClassName());
3294 if (!r.activity.mCalled) {
3295 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003296 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 + " did not call through to super.onPause()");
3298 }
3299 } catch (SuperNotCalledException e) {
3300 throw e;
3301 } catch (Exception e) {
3302 if (!mInstrumentation.onException(r.activity, e)) {
3303 throw new RuntimeException(
3304 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003305 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 + ": " + e.toString(), e);
3307 }
3308 }
3309 r.paused = true;
3310 }
3311 if (!r.stopped) {
3312 try {
3313 r.activity.performStop();
3314 } catch (SuperNotCalledException e) {
3315 throw e;
3316 } catch (Exception e) {
3317 if (!mInstrumentation.onException(r.activity, e)) {
3318 throw new RuntimeException(
3319 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003320 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 + ": " + e.toString(), e);
3322 }
3323 }
3324 r.stopped = true;
3325 }
3326 if (getNonConfigInstance) {
3327 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003328 r.lastNonConfigurationInstances
3329 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003330 } catch (Exception e) {
3331 if (!mInstrumentation.onException(r.activity, e)) {
3332 throw new RuntimeException(
3333 "Unable to retain activity "
3334 + r.intent.getComponent().toShortString()
3335 + ": " + e.toString(), e);
3336 }
3337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339 try {
3340 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003341 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 if (!r.activity.mCalled) {
3343 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003344 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 " did not call through to super.onDestroy()");
3346 }
3347 if (r.window != null) {
3348 r.window.closeAllPanels();
3349 }
3350 } catch (SuperNotCalledException e) {
3351 throw e;
3352 } catch (Exception e) {
3353 if (!mInstrumentation.onException(r.activity, e)) {
3354 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003355 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3356 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 }
3358 }
3359 }
3360 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003361 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 return r;
3363 }
3364
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003365 private static String safeToComponentShortString(Intent intent) {
3366 ComponentName component = intent.getComponent();
3367 return component == null ? "[Unknown]" : component.toShortString();
3368 }
3369
Romain Guy65b345f2011-07-27 18:51:50 -07003370 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003372 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 configChanges, getNonConfigInstance);
3374 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003375 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 WindowManager wm = r.activity.getWindowManager();
3377 View v = r.activity.mDecor;
3378 if (v != null) {
3379 if (r.activity.mVisibleFromServer) {
3380 mNumVisibleActivities--;
3381 }
3382 IBinder wtoken = v.getWindowToken();
3383 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003384 if (r.onlyLocalRequest) {
3385 // Hold off on removing this until the new activity's
3386 // window is being added.
3387 r.mPendingRemoveWindow = v;
3388 r.mPendingRemoveWindowManager = wm;
3389 } else {
3390 wm.removeViewImmediate(v);
3391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003393 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003394 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003395 r.activity.getClass().getName(), "Activity");
3396 }
3397 r.activity.mDecor = null;
3398 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003399 if (r.mPendingRemoveWindow == null) {
3400 // If we are delaying the removal of the activity window, then
3401 // we can't clean up all windows here. Note that we can't do
3402 // so later either, which means any windows that aren't closed
3403 // by the app will leak. Well we try to warning them a lot
3404 // about leaking windows, because that is a bug, so if they are
3405 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003406 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003407 r.activity.getClass().getName(), "Activity");
3408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409
3410 // Mocked out contexts won't be participating in the normal
3411 // process lifecycle, but if we're running with a proper
3412 // ApplicationContext we need to have it tear down things
3413 // cleanly.
3414 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003415 if (c instanceof ContextImpl) {
3416 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 r.activity.getClass().getName(), "Activity");
3418 }
3419 }
3420 if (finishing) {
3421 try {
3422 ActivityManagerNative.getDefault().activityDestroyed(token);
3423 } catch (RemoteException ex) {
3424 // If the system process has died, it's game over for everyone.
3425 }
3426 }
3427 }
3428
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003429 public final void requestRelaunchActivity(IBinder token,
3430 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3431 int configChanges, boolean notResumed, Configuration config,
3432 boolean fromServer) {
3433 ActivityClientRecord target = null;
3434
3435 synchronized (mPackages) {
3436 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3437 ActivityClientRecord r = mRelaunchingActivities.get(i);
3438 if (r.token == token) {
3439 target = r;
3440 if (pendingResults != null) {
3441 if (r.pendingResults != null) {
3442 r.pendingResults.addAll(pendingResults);
3443 } else {
3444 r.pendingResults = pendingResults;
3445 }
3446 }
3447 if (pendingNewIntents != null) {
3448 if (r.pendingIntents != null) {
3449 r.pendingIntents.addAll(pendingNewIntents);
3450 } else {
3451 r.pendingIntents = pendingNewIntents;
3452 }
3453 }
3454 break;
3455 }
3456 }
3457
3458 if (target == null) {
3459 target = new ActivityClientRecord();
3460 target.token = token;
3461 target.pendingResults = pendingResults;
3462 target.pendingIntents = pendingNewIntents;
3463 if (!fromServer) {
3464 ActivityClientRecord existing = mActivities.get(token);
3465 if (existing != null) {
3466 target.startsNotResumed = existing.paused;
3467 }
3468 target.onlyLocalRequest = true;
3469 }
3470 mRelaunchingActivities.add(target);
3471 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3472 }
3473
3474 if (fromServer) {
3475 target.startsNotResumed = notResumed;
3476 target.onlyLocalRequest = false;
3477 }
3478 if (config != null) {
3479 target.createdConfig = config;
3480 }
3481 target.pendingConfigChanges |= configChanges;
3482 }
3483 }
3484
Romain Guy65b345f2011-07-27 18:51:50 -07003485 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003486 // If we are getting ready to gc after going to the background, well
3487 // we are back active so skip it.
3488 unscheduleGcIdler();
3489
3490 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003491 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 // First: make sure we have the most recent configuration and most
3494 // recent version of the activity, or skip it if some previous call
3495 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003496 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 int N = mRelaunchingActivities.size();
3498 IBinder token = tmp.token;
3499 tmp = null;
3500 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003501 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 if (r.token == token) {
3503 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003504 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 mRelaunchingActivities.remove(i);
3506 i--;
3507 N--;
3508 }
3509 }
Bob Leee5408332009-09-04 18:31:17 -07003510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003512 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 return;
3514 }
Bob Leee5408332009-09-04 18:31:17 -07003515
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003516 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3517 + tmp.token + " with configChanges=0x"
3518 + Integer.toHexString(configChanges));
3519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003520 if (mPendingConfiguration != null) {
3521 changedConfig = mPendingConfiguration;
3522 mPendingConfiguration = null;
3523 }
3524 }
Bob Leee5408332009-09-04 18:31:17 -07003525
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003526 if (tmp.createdConfig != null) {
3527 // If the activity manager is passing us its current config,
3528 // assume that is really what we want regardless of what we
3529 // may have pending.
3530 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003531 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3532 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3533 if (changedConfig == null
3534 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3535 changedConfig = tmp.createdConfig;
3536 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003537 }
3538 }
3539
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003540 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003541 + tmp.token + ": changedConfig=" + changedConfig);
3542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003543 // If there was a pending configuration change, execute it first.
3544 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003545 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003546 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003547 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 }
Bob Leee5408332009-09-04 18:31:17 -07003549
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003550 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003551 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 if (r == null) {
3553 return;
3554 }
Bob Leee5408332009-09-04 18:31:17 -07003555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003557 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003558 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003559
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003560 r.activity.mChangingConfigurations = true;
3561
Dianne Hackborne2b04802010-12-09 09:24:55 -08003562 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003564 performPauseActivity(r.token, false, r.isPreHoneycomb());
3565 }
3566 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3567 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003568 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003569 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 }
Bob Leee5408332009-09-04 18:31:17 -07003571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 r.activity = null;
3575 r.window = null;
3576 r.hideForNow = false;
3577 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003578 // Merge any pending results and pending intents; don't just replace them
3579 if (tmp.pendingResults != null) {
3580 if (r.pendingResults == null) {
3581 r.pendingResults = tmp.pendingResults;
3582 } else {
3583 r.pendingResults.addAll(tmp.pendingResults);
3584 }
3585 }
3586 if (tmp.pendingIntents != null) {
3587 if (r.pendingIntents == null) {
3588 r.pendingIntents = tmp.pendingIntents;
3589 } else {
3590 r.pendingIntents.addAll(tmp.pendingIntents);
3591 }
3592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003594
Christopher Tateb70f3df2009-04-07 16:07:59 -07003595 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 }
3597
Romain Guy65b345f2011-07-27 18:51:50 -07003598 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003599 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 Bitmap thumbnail = createThumbnailBitmap(r);
3601 CharSequence description = null;
3602 try {
3603 description = r.activity.onCreateDescription();
3604 } catch (Exception e) {
3605 if (!mInstrumentation.onException(r.activity, e)) {
3606 throw new RuntimeException(
3607 "Unable to create description of activity "
3608 + r.intent.getComponent().toShortString()
3609 + ": " + e.toString(), e);
3610 }
3611 }
3612 //System.out.println("Reporting top thumbnail " + thumbnail);
3613 try {
3614 ActivityManagerNative.getDefault().reportThumbnail(
3615 token, thumbnail, description);
3616 } catch (RemoteException ex) {
3617 }
3618 }
3619
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003620 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003622 ArrayList<ComponentCallbacks2> callbacks
3623 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003626 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 Activity a = ar.activity;
3628 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003629 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3630 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003631 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 // If the activity is currently resumed, its configuration
3633 // needs to change right now.
3634 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003635 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 // Otherwise, we will tell it about the change
3637 // the next time it is resumed or shown. Note that
3638 // the activity manager may, before then, decide the
3639 // activity needs to be destroyed to handle its new
3640 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003641 if (DEBUG_CONFIGURATION) {
3642 Slog.v(TAG, "Setting activity "
3643 + ar.activityInfo.name + " newConfig=" + thisConfig);
3644 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003645 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 }
3647 }
3648 }
3649 }
3650 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003651 for (Service service : mServices.values()) {
3652 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 }
3654 }
3655 synchronized (mProviderMap) {
3656 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003657 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3658 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 }
3660 }
3661 }
3662 final int N = mAllApplications.size();
3663 for (int i=0; i<N; i++) {
3664 callbacks.add(mAllApplications.get(i));
3665 }
Bob Leee5408332009-09-04 18:31:17 -07003666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 return callbacks;
3668 }
Bob Leee5408332009-09-04 18:31:17 -07003669
Romain Guya998dff2012-03-23 18:58:36 -07003670 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003672 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 // we check the runtime type and act accordingly.
3674 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3675 if (activity != null) {
3676 activity.mCalled = false;
3677 }
Bob Leee5408332009-09-04 18:31:17 -07003678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 boolean shouldChangeConfig = false;
3680 if ((activity == null) || (activity.mCurrentConfig == null)) {
3681 shouldChangeConfig = true;
3682 } else {
Bob Leee5408332009-09-04 18:31:17 -07003683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003684 // If the new config is the same as the config this Activity
3685 // is already running with then don't bother calling
3686 // onConfigurationChanged
3687 int diff = activity.mCurrentConfig.diff(config);
3688 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 // If this activity doesn't handle any of the config changes
3690 // then don't bother calling onConfigurationChanged as we're
3691 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003692 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 shouldChangeConfig = true;
3694 }
3695 }
3696 }
Bob Leee5408332009-09-04 18:31:17 -07003697
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003698 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003699 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 if (shouldChangeConfig) {
3701 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 if (activity != null) {
3704 if (!activity.mCalled) {
3705 throw new SuperNotCalledException(
3706 "Activity " + activity.getLocalClassName() +
3707 " did not call through to super.onConfigurationChanged()");
3708 }
3709 activity.mConfigChangeFlags = 0;
3710 activity.mCurrentConfig = new Configuration(config);
3711 }
3712 }
3713 }
3714
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003715 public final void applyConfigurationToResources(Configuration config) {
3716 synchronized (mPackages) {
3717 applyConfigurationToResourcesLocked(config, null);
3718 }
3719 }
3720
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003721 final boolean applyConfigurationToResourcesLocked(Configuration config,
3722 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003723 if (mResConfiguration == null) {
3724 mResConfiguration = new Configuration();
3725 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003726 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003727 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003728 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003729 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003730 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003731 int changes = mResConfiguration.updateFrom(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003732 flushDisplayMetricsLocked();
3733 DisplayMetrics defaultDisplayMetrics = getDisplayMetricsLocked(
3734 Display.DEFAULT_DISPLAY, null);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003735
3736 if (compat != null && (mResCompatibilityInfo == null ||
3737 !mResCompatibilityInfo.equals(compat))) {
3738 mResCompatibilityInfo = compat;
3739 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3740 | ActivityInfo.CONFIG_SCREEN_SIZE
3741 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3742 }
Bob Leee5408332009-09-04 18:31:17 -07003743
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003744 // set it for java, this also affects newly created Resources
3745 if (config.locale != null) {
3746 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 }
Bob Leee5408332009-09-04 18:31:17 -07003748
Jeff Browna492c3a2012-08-23 19:48:44 -07003749 Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
Bob Leee5408332009-09-04 18:31:17 -07003750
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003751 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003752 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003753
3754 Configuration tmpConfig = null;
3755
3756 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3757 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003758 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003759 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3760 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003761 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003762 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003763 + r + " config to: " + config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003764 int displayId = entry.getKey().mDisplayId;
3765 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
3766 DisplayMetrics dm = defaultDisplayMetrics;
3767 Configuration overrideConfig = entry.getKey().mOverrideConfiguration;
3768 if (!isDefaultDisplay || overrideConfig != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003769 if (tmpConfig == null) {
3770 tmpConfig = new Configuration();
3771 }
3772 tmpConfig.setTo(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003773 if (!isDefaultDisplay) {
3774 dm = getDisplayMetricsLocked(displayId, null);
3775 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, tmpConfig);
3776 }
3777 if (overrideConfig != null) {
3778 tmpConfig.updateFrom(overrideConfig);
3779 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07003780 r.updateConfiguration(tmpConfig, dm, compat);
3781 } else {
3782 r.updateConfiguration(config, dm, compat);
3783 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003784 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003785 // + " " + r + ": " + r.getConfiguration());
3786 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003787 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003788 it.remove();
3789 }
3790 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003791
3792 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003793 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003794
Jeff Browna492c3a2012-08-23 19:48:44 -07003795 final void applyNonDefaultDisplayMetricsToConfigurationLocked(
3796 DisplayMetrics dm, Configuration config) {
3797 config.screenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE
3798 | Configuration.SCREENLAYOUT_LONG_NO;
3799 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
3800 config.orientation = (dm.widthPixels >= dm.heightPixels) ?
3801 Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT;
3802 config.densityDpi = dm.densityDpi;
3803 config.screenWidthDp = (int)(dm.widthPixels / dm.density);
3804 config.screenHeightDp = (int)(dm.heightPixels / dm.density);
3805 config.smallestScreenWidthDp = config.screenWidthDp; // assume screen does not rotate
3806 config.compatScreenWidthDp = config.screenWidthDp;
3807 config.compatScreenHeightDp = config.screenHeightDp;
3808 config.compatSmallestScreenWidthDp = config.smallestScreenWidthDp;
3809 }
3810
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003811 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003812 Configuration config = mConfiguration;
3813 if (mCompatConfiguration == null) {
3814 mCompatConfiguration = new Configuration();
3815 }
3816 mCompatConfiguration.setTo(mConfiguration);
3817 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003818 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003819 config = mCompatConfiguration;
3820 }
3821 return config;
3822 }
3823
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003824 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003825
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003826 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003827 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003828
3829 synchronized (mPackages) {
3830 if (mPendingConfiguration != null) {
3831 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3832 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003833 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003834 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003835 }
3836 mPendingConfiguration = null;
3837 }
3838
3839 if (config == null) {
3840 return;
3841 }
3842
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003843 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003844 + config);
3845
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003846 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003847
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 if (mConfiguration == null) {
3849 mConfiguration = new Configuration();
3850 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003851 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003852 return;
3853 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003854 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003856 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 callbacks = collectComponentCallbacksLocked(false, config);
3858 }
Romain Guy65b345f2011-07-27 18:51:50 -07003859
3860 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003861 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003862
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003863 freeTextLayoutCachesIfNeeded(configDiff);
3864
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003865 if (callbacks != null) {
3866 final int N = callbacks.size();
3867 for (int i=0; i<N; i++) {
3868 performConfigurationChanged(callbacks.get(i), config);
3869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 }
3871 }
3872
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003873 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3874 if (configDiff != 0) {
3875 // Ask text layout engine to free its caches if there is a locale change
3876 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3877 if (hasLocaleConfigChange) {
3878 Canvas.freeTextLayoutCaches();
3879 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3880 }
3881 }
3882 }
3883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003885 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 if (r == null || r.activity == null) {
3887 return;
3888 }
Bob Leee5408332009-09-04 18:31:17 -07003889
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003890 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003891 + r.activityInfo.name);
3892
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003893 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003894
3895 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 }
3897
Romain Guy7eabe552011-07-21 14:56:34 -07003898 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003899 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003900 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003901 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003902 default:
3903 mProfiler.setProfiler(pcd.path, pcd.fd);
3904 mProfiler.autoStopProfiler = false;
3905 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003906 break;
3907 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003908 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003909 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003910 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003911 } finally {
3912 try {
3913 pcd.fd.close();
3914 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003915 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003916 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003917 }
3918 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003919 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003920 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003921 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003922 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003923 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003924 }
3925 }
Bob Leee5408332009-09-04 18:31:17 -07003926
Romain Guya998dff2012-03-23 18:58:36 -07003927 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003928 if (managed) {
3929 try {
3930 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3931 } catch (IOException e) {
3932 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3933 + " -- can the process access this path?");
3934 } finally {
3935 try {
3936 dhd.fd.close();
3937 } catch (IOException e) {
3938 Slog.w(TAG, "Failure closing profile fd", e);
3939 }
3940 }
3941 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003942 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003943 }
3944 }
3945
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003946 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3947 boolean hasPkgInfo = false;
3948 if (packages != null) {
3949 for (int i=packages.length-1; i>=0; i--) {
3950 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3951 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003952 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003953 ref = mPackages.get(packages[i]);
3954 if (ref != null && ref.get() != null) {
3955 hasPkgInfo = true;
3956 } else {
3957 ref = mResourcePackages.get(packages[i]);
3958 if (ref != null && ref.get() != null) {
3959 hasPkgInfo = true;
3960 }
3961 }
3962 }
3963 mPackages.remove(packages[i]);
3964 mResourcePackages.remove(packages[i]);
3965 }
3966 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003967 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003968 hasPkgInfo);
3969 }
3970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003972 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003974 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 callbacks = collectComponentCallbacksLocked(true, null);
3976 }
Bob Leee5408332009-09-04 18:31:17 -07003977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003978 final int N = callbacks.size();
3979 for (int i=0; i<N; i++) {
3980 callbacks.get(i).onLowMemory();
3981 }
3982
Chris Tatece229052009-03-25 16:44:52 -07003983 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3984 if (Process.myUid() != Process.SYSTEM_UID) {
3985 int sqliteReleased = SQLiteDatabase.releaseMemory();
3986 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3987 }
Bob Leee5408332009-09-04 18:31:17 -07003988
Mike Reedcaf0df12009-04-27 14:32:05 -04003989 // Ask graphics to free up as much as possible (font/image caches)
3990 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003992 // Ask text layout engine to free also as much as possible
3993 Canvas.freeTextLayoutCaches();
3994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 BinderInternal.forceGc("mem");
3996 }
3997
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003998 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003999 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004000
Jeff Brown98365d72012-08-19 20:30:52 -07004001 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004002 windowManager.startTrimMemory(level);
4003
4004 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004005 synchronized (mPackages) {
4006 callbacks = collectComponentCallbacksLocked(true, null);
4007 }
4008
4009 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004010 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004011 callbacks.get(i).onTrimMemory(level);
4012 }
Romain Guy19f86e82012-04-23 15:19:07 -07004013
Jeff Brown98365d72012-08-19 20:30:52 -07004014 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004015 }
4016
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004017 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004018 if (Process.isIsolated()) {
4019 // Isolated processes aren't going to do UI.
4020 return;
4021 }
Romain Guya9582652011-11-10 14:20:10 -08004022 try {
4023 int uid = Process.myUid();
4024 String[] packages = getPackageManager().getPackagesForUid(uid);
4025
4026 // If there are several packages in this application we won't
4027 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004028 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004029 HardwareRenderer.setupDiskCache(cacheDir);
4030 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004031 }
4032 } catch (RemoteException e) {
4033 // Ignore
4034 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004035 }
4036
4037 private void updateDefaultDensity() {
4038 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4039 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4040 && !mDensityCompatMode) {
4041 Slog.i(TAG, "Switching default density from "
4042 + DisplayMetrics.DENSITY_DEVICE + " to "
4043 + mCurDefaultDisplayDpi);
4044 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4045 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4046 }
4047 }
4048
Romain Guy65b345f2011-07-27 18:51:50 -07004049 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004050 mBoundApplication = data;
4051 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004052 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004054 mProfiler = new Profiler();
4055 mProfiler.profileFile = data.initProfileFile;
4056 mProfiler.profileFd = data.initProfileFd;
4057 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004060 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 android.ddm.DdmHandleAppName.setAppName(data.processName);
4062
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004063 if (data.persistent) {
4064 // Persistent processes on low-memory devices do not get to
4065 // use hardware accelerated drawing, since this can add too much
4066 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004067 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004068 HardwareRenderer.disable(false);
4069 }
4070 }
Romain Guya9582652011-11-10 14:20:10 -08004071
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004072 if (mProfiler.profileFd != null) {
4073 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004074 }
4075
Joe Onoratod630f102011-03-17 18:42:26 -07004076 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4077 // implementation to use the pool executor. Normally, we use the
4078 // serialized executor as the default. This has to happen in the
4079 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004080 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004081 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4082 }
4083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 /*
4085 * Before spawning a new process, reset the time zone to be the system time zone.
4086 * This needs to be done because the system time zone could have changed after the
4087 * the spawning of this process. Without doing this this process would have the incorrect
4088 * system time zone.
4089 */
4090 TimeZone.setDefault(null);
4091
4092 /*
4093 * Initialize the default locale in this process for the reasons we set the time zone.
4094 */
4095 Locale.setDefault(data.config.locale);
4096
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004097 /*
4098 * Update the system configuration since its preloaded and might not
4099 * reflect configuration changes. The configuration object passed
4100 * in AppBindData can be safely assumed to be up to date
4101 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004102 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004103 mCurDefaultDisplayDpi = data.config.densityDpi;
4104 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004105
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004106 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107
Dianne Hackborndde331c2012-08-03 14:01:57 -07004108 /**
4109 * Switch this process to density compatibility mode if needed.
4110 */
4111 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4112 == 0) {
4113 mDensityCompatMode = true;
4114 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4115 }
4116 updateDefaultDensity();
4117
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004118 final ContextImpl appContext = new ContextImpl();
4119 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004120 if (!Process.isIsolated()) {
4121 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004122
Amith Yamasani92d57052012-08-23 10:07:52 -07004123 if (cacheDir != null) {
4124 // Provide a usable directory for temporary files
4125 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4126
4127 setupGraphicsSupport(data.info, cacheDir);
4128 } else {
4129 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4130 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004131 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004132 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004133 * For system applications on userdebug/eng builds, log stack
4134 * traces of disk and network access to dropbox for analysis.
4135 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004136 if ((data.appInfo.flags &
4137 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004138 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4139 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004140 }
4141
4142 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004143 * For apps targetting SDK Honeycomb or later, we don't allow
4144 * network usage on the main event loop / UI thread.
4145 *
4146 * Note to those grepping: this is what ultimately throws
4147 * NetworkOnMainThreadException ...
4148 */
4149 if (data.appInfo.targetSdkVersion > 9) {
4150 StrictMode.enableDeathOnNetwork();
4151 }
4152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4154 // XXX should have option to change the port.
4155 Debug.changeDebugPort(8100);
4156 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004157 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 + " is waiting for the debugger on port 8100...");
4159
4160 IActivityManager mgr = ActivityManagerNative.getDefault();
4161 try {
4162 mgr.showWaitingForDebugger(mAppThread, true);
4163 } catch (RemoteException ex) {
4164 }
4165
4166 Debug.waitForDebugger();
4167
4168 try {
4169 mgr.showWaitingForDebugger(mAppThread, false);
4170 } catch (RemoteException ex) {
4171 }
4172
4173 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004174 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 + " can be debugged on port 8100...");
4176 }
4177 }
4178
Siva Velusamy92a8b222012-03-09 16:24:04 -08004179 // Enable OpenGL tracing if required
4180 if (data.enableOpenGlTrace) {
4181 GLUtils.enableTracing();
4182 }
4183
Robert Greenwalt434203a2010-10-11 16:00:27 -07004184 /**
4185 * Initialize the default http proxy in this process for the reasons we set the time zone.
4186 */
4187 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004188 if (b != null) {
4189 // In pre-boot mode (doing initial launch to collect password), not
4190 // all system is up. This includes the connectivity service, so don't
4191 // crash if we can't get it.
4192 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4193 try {
4194 ProxyProperties proxyProperties = service.getProxy();
4195 Proxy.setHttpProxySystemProperty(proxyProperties);
4196 } catch (RemoteException e) {}
4197 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 InstrumentationInfo ii = null;
4201 try {
4202 ii = appContext.getPackageManager().
4203 getInstrumentationInfo(data.instrumentationName, 0);
4204 } catch (PackageManager.NameNotFoundException e) {
4205 }
4206 if (ii == null) {
4207 throw new RuntimeException(
4208 "Unable to find instrumentation info for: "
4209 + data.instrumentationName);
4210 }
4211
4212 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004213 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 mInstrumentationAppPackage = ii.packageName;
4215 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004216 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217
4218 ApplicationInfo instrApp = new ApplicationInfo();
4219 instrApp.packageName = ii.packageName;
4220 instrApp.sourceDir = ii.sourceDir;
4221 instrApp.publicSourceDir = ii.publicSourceDir;
4222 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004223 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004224 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004226 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 instrContext.init(pi, null, this);
4228
4229 try {
4230 java.lang.ClassLoader cl = instrContext.getClassLoader();
4231 mInstrumentation = (Instrumentation)
4232 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4233 } catch (Exception e) {
4234 throw new RuntimeException(
4235 "Unable to instantiate instrumentation "
4236 + data.instrumentationName + ": " + e.toString(), e);
4237 }
4238
4239 mInstrumentation.init(this, instrContext, appContext,
4240 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4241
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004242 if (mProfiler.profileFile != null && !ii.handleProfiling
4243 && mProfiler.profileFd == null) {
4244 mProfiler.handlingProfiling = true;
4245 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 file.getParentFile().mkdirs();
4247 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4248 }
4249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004250 } else {
4251 mInstrumentation = new Instrumentation();
4252 }
4253
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004254 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004255 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004256 }
4257
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004258 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004259 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004260 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004261 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004263 // If the app is being launched for full backup or restore, bring it up in
4264 // a restricted environment with the base application class.
4265 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4266 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004267
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004268 // don't bring up providers in restricted mode; they may depend on the
4269 // app's custom Application class
4270 if (!data.restrictedBackupMode) {
4271 List<ProviderInfo> providers = data.providers;
4272 if (providers != null) {
4273 installContentProviders(app, providers);
4274 // For process that contains content providers, we want to
4275 // ensure that the JIT is enabled "at some point".
4276 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4277 }
4278 }
4279
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004280 // Do this after providers, since instrumentation tests generally start their
4281 // test thread at this point, and we don't want that racing.
4282 try {
4283 mInstrumentation.onCreate(data.instrumentationArgs);
4284 }
4285 catch (Exception e) {
4286 throw new RuntimeException(
4287 "Exception thrown in onCreate() of "
4288 + data.instrumentationName + ": " + e.toString(), e);
4289 }
4290
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004291 try {
4292 mInstrumentation.callApplicationOnCreate(app);
4293 } catch (Exception e) {
4294 if (!mInstrumentation.onException(app, e)) {
4295 throw new RuntimeException(
4296 "Unable to create application " + app.getClass().getName()
4297 + ": " + e.toString(), e);
4298 }
4299 }
4300 } finally {
4301 StrictMode.setThreadPolicy(savedPolicy);
4302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004303 }
4304
4305 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4306 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004307 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4308 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004309 Debug.stopMethodTracing();
4310 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004311 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 // + ", app thr: " + mAppThread);
4313 try {
4314 am.finishInstrumentation(mAppThread, resultCode, results);
4315 } catch (RemoteException ex) {
4316 }
4317 }
4318
Romain Guy65b345f2011-07-27 18:51:50 -07004319 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 Context context, List<ProviderInfo> providers) {
4321 final ArrayList<IActivityManager.ContentProviderHolder> results =
4322 new ArrayList<IActivityManager.ContentProviderHolder>();
4323
Romain Guya998dff2012-03-23 18:58:36 -07004324 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004326 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 buf.append(cpi.authority);
4328 buf.append(": ");
4329 buf.append(cpi.name);
4330 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004331 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4332 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4333 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004334 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 }
4337 }
4338
4339 try {
4340 ActivityManagerNative.getDefault().publishContentProviders(
4341 getApplicationThread(), results);
4342 } catch (RemoteException ex) {
4343 }
4344 }
4345
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004346 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4347 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004348 if (provider != null) {
4349 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 }
4351
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004352 // There is a possible race here. Another thread may try to acquire
4353 // the same provider at the same time. When this happens, we want to ensure
4354 // that the first one wins.
4355 // Note that we cannot hold the lock while acquiring and installing the
4356 // provider since it might take a long time to run and it could also potentially
4357 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 IActivityManager.ContentProviderHolder holder = null;
4359 try {
4360 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004361 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 } catch (RemoteException ex) {
4363 }
4364 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004365 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 return null;
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004369 // Install provider will increment the reference count for us, and break
4370 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004371 holder = installProvider(c, holder, holder.info,
4372 true /*noisy*/, holder.noReleaseNeeded, stable);
4373 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 }
4375
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004376 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4377 if (stable) {
4378 prc.stableCount += 1;
4379 if (prc.stableCount == 1) {
4380 // We are acquiring a new stable reference on the provider.
4381 int unstableDelta;
4382 if (prc.removePending) {
4383 // We have a pending remove operation, which is holding the
4384 // last unstable reference. At this point we are converting
4385 // that unstable reference to our new stable reference.
4386 unstableDelta = -1;
4387 // Cancel the removal of the provider.
4388 if (DEBUG_PROVIDER) {
4389 Slog.v(TAG, "incProviderRef: stable "
4390 + "snatched provider from the jaws of death");
4391 }
4392 prc.removePending = false;
4393 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4394 } else {
4395 unstableDelta = 0;
4396 }
4397 try {
4398 if (DEBUG_PROVIDER) {
4399 Slog.v(TAG, "incProviderRef Now stable - "
4400 + prc.holder.info.name + ": unstableDelta="
4401 + unstableDelta);
4402 }
4403 ActivityManagerNative.getDefault().refContentProvider(
4404 prc.holder.connection, 1, unstableDelta);
4405 } catch (RemoteException e) {
4406 //do nothing content provider object is dead any way
4407 }
4408 }
4409 } else {
4410 prc.unstableCount += 1;
4411 if (prc.unstableCount == 1) {
4412 // We are acquiring a new unstable reference on the provider.
4413 if (prc.removePending) {
4414 // Oh look, we actually have a remove pending for the
4415 // provider, which is still holding the last unstable
4416 // reference. We just need to cancel that to take new
4417 // ownership of the reference.
4418 if (DEBUG_PROVIDER) {
4419 Slog.v(TAG, "incProviderRef: unstable "
4420 + "snatched provider from the jaws of death");
4421 }
4422 prc.removePending = false;
4423 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4424 } else {
4425 // First unstable ref, increment our count in the
4426 // activity manager.
4427 try {
4428 if (DEBUG_PROVIDER) {
4429 Slog.v(TAG, "incProviderRef: Now unstable - "
4430 + prc.holder.info.name);
4431 }
4432 ActivityManagerNative.getDefault().refContentProvider(
4433 prc.holder.connection, 0, 1);
4434 } catch (RemoteException e) {
4435 //do nothing content provider object is dead any way
4436 }
4437 }
4438 }
4439 }
4440 }
4441
4442 public final IContentProvider acquireExistingProvider(Context c, String name,
4443 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004444 synchronized (mProviderMap) {
4445 ProviderClientRecord pr = mProviderMap.get(name);
4446 if (pr == null) {
4447 return null;
4448 }
4449
4450 IContentProvider provider = pr.mProvider;
4451 IBinder jBinder = provider.asBinder();
4452
4453 // Only increment the ref count if we have one. If we don't then the
4454 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004455 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004456 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004457 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004458 }
4459 return provider;
4460 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004461 }
4462
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004463 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4464 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 return false;
4466 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004469 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004471 if (prc == null) {
4472 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004474 }
4475
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004476 boolean lastRef = false;
4477 if (stable) {
4478 if (prc.stableCount == 0) {
4479 if (DEBUG_PROVIDER) Slog.v(TAG,
4480 "releaseProvider: stable ref count already 0, how?");
4481 return false;
4482 }
4483 prc.stableCount -= 1;
4484 if (prc.stableCount == 0) {
4485 // What we do at this point depends on whether there are
4486 // any unstable refs left: if there are, we just tell the
4487 // activity manager to decrement its stable count; if there
4488 // aren't, we need to enqueue this provider to be removed,
4489 // and convert to holding a single unstable ref while
4490 // doing so.
4491 lastRef = prc.unstableCount == 0;
4492 try {
4493 if (DEBUG_PROVIDER) {
4494 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4495 + lastRef + " - " + prc.holder.info.name);
4496 }
4497 ActivityManagerNative.getDefault().refContentProvider(
4498 prc.holder.connection, -1, lastRef ? 1 : 0);
4499 } catch (RemoteException e) {
4500 //do nothing content provider object is dead any way
4501 }
4502 }
4503 } else {
4504 if (prc.unstableCount == 0) {
4505 if (DEBUG_PROVIDER) Slog.v(TAG,
4506 "releaseProvider: unstable ref count already 0, how?");
4507 return false;
4508 }
4509 prc.unstableCount -= 1;
4510 if (prc.unstableCount == 0) {
4511 // If this is the last reference, we need to enqueue
4512 // this provider to be removed instead of telling the
4513 // activity manager to remove it at this point.
4514 lastRef = prc.stableCount == 0;
4515 if (!lastRef) {
4516 try {
4517 if (DEBUG_PROVIDER) {
4518 Slog.v(TAG, "releaseProvider: No longer unstable - "
4519 + prc.holder.info.name);
4520 }
4521 ActivityManagerNative.getDefault().refContentProvider(
4522 prc.holder.connection, 0, -1);
4523 } catch (RemoteException e) {
4524 //do nothing content provider object is dead any way
4525 }
4526 }
4527 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004528 }
4529
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004530 if (lastRef) {
4531 if (!prc.removePending) {
4532 // Schedule the actual remove asynchronously, since we don't know the context
4533 // this will be called in.
4534 // TODO: it would be nice to post a delayed message, so
4535 // if we come back and need the same provider quickly
4536 // we will still have it available.
4537 if (DEBUG_PROVIDER) {
4538 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4539 + prc.holder.info.name);
4540 }
4541 prc.removePending = true;
4542 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4543 mH.sendMessage(msg);
4544 } else {
4545 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4546 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004547 }
4548 return true;
4549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004550 }
4551
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004552 final void completeRemoveProvider(ProviderRefCount prc) {
4553 synchronized (mProviderMap) {
4554 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004555 // There was a race! Some other client managed to acquire
4556 // the provider before the removal was completed.
4557 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004558 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004559 + "provider still in use");
4560 return;
4561 }
4562
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004563 final IBinder jBinder = prc.holder.provider.asBinder();
4564 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4565 if (existingPrc == prc) {
4566 mProviderRefCountMap.remove(jBinder);
4567 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004568
4569 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4570 while (iter.hasNext()) {
4571 ProviderClientRecord pr = iter.next();
4572 IBinder myBinder = pr.mProvider.asBinder();
4573 if (myBinder == jBinder) {
4574 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004575 }
4576 }
4577 }
4578
4579 try {
4580 if (DEBUG_PROVIDER) {
4581 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4582 + "removeContentProvider(" + prc.holder.info.name + ")");
4583 }
4584 ActivityManagerNative.getDefault().removeContentProvider(
4585 prc.holder.connection, false);
4586 } catch (RemoteException e) {
4587 //do nothing content provider object is dead any way
4588 }
4589 }
4590
4591 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4592 synchronized(mProviderMap) {
4593 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4594 if (prc != null) {
4595 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4596 + provider + " " + prc.holder.info.name);
4597 mProviderRefCountMap.remove(provider);
4598 if (prc.client != null && prc.client.mNames != null) {
4599 for (String name : prc.client.mNames) {
4600 ProviderClientRecord pr = mProviderMap.get(name);
4601 if (pr != null && pr.mProvider.asBinder() == provider) {
4602 Slog.i(TAG, "Removing dead content provider: " + name);
4603 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004604 }
4605 }
4606 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004607 if (fromClient) {
4608 // We found out about this due to execution in our client
4609 // code. Tell the activity manager about it now, to ensure
4610 // that the next time we go to do anything with the provider
4611 // it knows it is dead (so we don't race with its death
4612 // notification).
4613 try {
4614 ActivityManagerNative.getDefault().unstableProviderDied(
4615 prc.holder.connection);
4616 } catch (RemoteException e) {
4617 //do nothing content provider object is dead any way
4618 }
4619 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004620 }
4621 }
4622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004624 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4625 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4626 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4627 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4628 localProvider, holder);
4629 for (int i = 0; i < names.length; i++) {
4630 ProviderClientRecord existing = mProviderMap.get(names[i]);
4631 if (existing != null) {
4632 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4633 + " already published as " + names[i]);
4634 } else {
4635 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 }
4637 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004638 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 }
4640
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004641 /**
4642 * Installs the provider.
4643 *
4644 * Providers that are local to the process or that come from the system server
4645 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4646 * Other remote providers are reference counted. The initial reference count
4647 * for all reference counted providers is one. Providers that are not reference
4648 * counted do not have a reference count (at all).
4649 *
4650 * This method detects when a provider has already been installed. When this happens,
4651 * it increments the reference count of the existing provider (if appropriate)
4652 * and returns the existing provider. This can happen due to concurrent
4653 * attempts to acquire the same provider.
4654 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004655 private IActivityManager.ContentProviderHolder installProvider(Context context,
4656 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4657 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004659 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004660 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004661 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004662 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 + info.name);
4664 }
4665 Context c = null;
4666 ApplicationInfo ai = info.applicationInfo;
4667 if (context.getPackageName().equals(ai.packageName)) {
4668 c = context;
4669 } else if (mInitialApplication != null &&
4670 mInitialApplication.getPackageName().equals(ai.packageName)) {
4671 c = mInitialApplication;
4672 } else {
4673 try {
4674 c = context.createPackageContext(ai.packageName,
4675 Context.CONTEXT_INCLUDE_CODE);
4676 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004677 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 }
4679 }
4680 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004681 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682 ai.packageName +
4683 " while loading content provider " +
4684 info.name);
4685 return null;
4686 }
4687 try {
4688 final java.lang.ClassLoader cl = c.getClassLoader();
4689 localProvider = (ContentProvider)cl.
4690 loadClass(info.name).newInstance();
4691 provider = localProvider.getIContentProvider();
4692 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004693 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004694 info.name + " from sourceDir " +
4695 info.applicationInfo.sourceDir);
4696 return null;
4697 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004698 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004699 TAG, "Instantiating local provider " + info.name);
4700 // XXX Need to create the correct context for this provider.
4701 localProvider.attachInfo(c, info);
4702 } catch (java.lang.Exception e) {
4703 if (!mInstrumentation.onException(null, e)) {
4704 throw new RuntimeException(
4705 "Unable to get provider " + info.name
4706 + ": " + e.toString(), e);
4707 }
4708 return null;
4709 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004710 } else {
4711 provider = holder.provider;
4712 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 + info.name);
4714 }
4715
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004716 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004717
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004718 synchronized (mProviderMap) {
4719 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4720 + " / " + info.name);
4721 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004723 ComponentName cname = new ComponentName(info.packageName, info.name);
4724 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004725 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004726 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004727 Slog.v(TAG, "installProvider: lost the race, "
4728 + "using existing local provider");
4729 }
4730 provider = pr.mProvider;
4731 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004732 holder = new IActivityManager.ContentProviderHolder(info);
4733 holder.provider = provider;
4734 holder.noReleaseNeeded = true;
4735 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004736 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004737 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004738 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004739 retHolder = pr.mHolder;
4740 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004741 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4742 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004743 if (DEBUG_PROVIDER) {
4744 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004745 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004746 // We need to transfer our new reference to the existing
4747 // ref count, releasing the old one... but only if
4748 // release is needed (that is, it is not running in the
4749 // system process).
4750 if (!noReleaseNeeded) {
4751 incProviderRefLocked(prc, stable);
4752 try {
4753 ActivityManagerNative.getDefault().removeContentProvider(
4754 holder.connection, stable);
4755 } catch (RemoteException e) {
4756 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004757 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004758 }
4759 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004760 ProviderClientRecord client = installProviderAuthoritiesLocked(
4761 provider, localProvider, holder);
4762 if (noReleaseNeeded) {
4763 prc = new ProviderRefCount(holder, client, 1000, 1000);
4764 } else {
4765 prc = stable
4766 ? new ProviderRefCount(holder, client, 1, 0)
4767 : new ProviderRefCount(holder, client, 0, 1);
4768 }
4769 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004770 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004771 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004772 }
4773 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004774
4775 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 }
4777
Romain Guy65b345f2011-07-27 18:51:50 -07004778 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 sThreadLocal.set(this);
4780 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004781 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004782 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004783 public void run() {
4784 ensureJitEnabled();
4785 }
4786 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4788 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4789 IActivityManager mgr = ActivityManagerNative.getDefault();
4790 try {
4791 mgr.attachApplication(mAppThread);
4792 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004793 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004794 }
4795 } else {
4796 // Don't set application object here -- if the system crashes,
4797 // we can't display an alert, we just want to die die die.
4798 android.ddm.DdmHandleAppName.setAppName("system_process");
4799 try {
4800 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004801 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004802 context.init(getSystemContext().mPackageInfo, null, this);
4803 Application app = Instrumentation.newApplication(Application.class, context);
4804 mAllApplications.add(app);
4805 mInitialApplication = app;
4806 app.onCreate();
4807 } catch (Exception e) {
4808 throw new RuntimeException(
4809 "Unable to instantiate Application():" + e.toString(), e);
4810 }
4811 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004812
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004813 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004814 public void onConfigurationChanged(Configuration newConfig) {
4815 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004816 // We need to apply this change to the resources
4817 // immediately, because upon returning the view
4818 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004819 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004820 // This actually changed the resources! Tell
4821 // everyone about it.
4822 if (mPendingConfiguration == null ||
4823 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4824 mPendingConfiguration = newConfig;
4825
4826 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4827 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004828 }
4829 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004830 }
4831 public void onLowMemory() {
4832 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004833 public void onTrimMemory(int level) {
4834 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004835 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 }
4837
Romain Guy5e9120d2012-01-30 12:17:22 -08004838 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004839 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 ActivityThread thread = new ActivityThread();
4841 thread.attach(true);
4842 return thread;
4843 }
4844
Jeff Brown10e89712011-07-08 18:52:57 -07004845 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004847 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004848 }
4849 }
4850
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004851 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004852 synchronized (mPackages) {
4853 if (mCoreSettings != null) {
4854 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004855 } else {
4856 return defaultValue;
4857 }
4858 }
4859 }
4860
Romain Guy65b345f2011-07-27 18:51:50 -07004861 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004862 SamplingProfilerIntegration.start();
4863
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004864 // CloseGuard defaults to true and can be quite spammy. We
4865 // disable it here, but selectively enable it later (via
4866 // StrictMode) on debug builds, but using DropBox, not logs.
4867 CloseGuard.setEnabled(false);
4868
Kenny Roote29df162012-08-10 08:28:37 -07004869 Security.addProvider(new AndroidKeyStoreProvider());
4870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004871 Process.setArgV0("<pre-initialized>");
4872
4873 Looper.prepareMainLooper();
4874
4875 ActivityThread thread = new ActivityThread();
4876 thread.attach(false);
4877
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004878 if (sMainThreadHandler == null) {
4879 sMainThreadHandler = thread.getHandler();
4880 }
4881
Romain Guy5e9120d2012-01-30 12:17:22 -08004882 AsyncTask.init();
4883
Dianne Hackborn287952c2010-09-22 22:34:31 -07004884 if (false) {
4885 Looper.myLooper().setMessageLogging(new
4886 LogPrinter(Log.DEBUG, "ActivityThread"));
4887 }
4888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004889 Looper.loop();
4890
Jeff Brown10e89712011-07-08 18:52:57 -07004891 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 }
4893}