blob: 1b788c2a17596d3659f46a8e5aae0d363360fdbe [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,
323 boolean ordered, boolean sticky, IBinder token) {
324 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky, token);
325 this.intent = intent;
326 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 Intent intent;
329 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 public String toString() {
332 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700333 info.packageName + " resultCode=" + getResultCode()
334 + " resultData=" + getResultData() + " resultExtras="
335 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
337 }
338
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400339 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700340 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400341 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700342 int backupMode;
343 public String toString() {
344 return "CreateBackupAgentData{appInfo=" + appInfo
345 + " backupAgent=" + appInfo.backupAgentName
346 + " mode=" + backupMode + "}";
347 }
348 }
Bob Leee5408332009-09-04 18:31:17 -0700349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 IBinder token;
352 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400353 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 Intent intent;
355 public String toString() {
356 return "CreateServiceData{token=" + token + " className="
357 + info.name + " packageName=" + info.packageName
358 + " intent=" + intent + "}";
359 }
360 }
361
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400362 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 IBinder token;
364 Intent intent;
365 boolean rebind;
366 public String toString() {
367 return "BindServiceData{token=" + token + " intent=" + intent + "}";
368 }
369 }
370
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400371 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700373 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700375 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 Intent args;
377 public String toString() {
378 return "ServiceArgsData{token=" + token + " startId=" + startId
379 + " args=" + args + "}";
380 }
381 }
382
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400383 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700384 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 String processName;
386 ApplicationInfo appInfo;
387 List<ProviderInfo> providers;
388 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 Bundle instrumentationArgs;
390 IInstrumentationWatcher instrumentationWatcher;
391 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800392 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700393 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700394 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400396 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700397
398 /** Initial values for {@link Profiler}. */
399 String initProfileFile;
400 ParcelFileDescriptor initProfileFd;
401 boolean initAutoStopProfiler;
402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 public String toString() {
404 return "AppBindData{appInfo=" + appInfo + "}";
405 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700406 }
407
408 static final class Profiler {
409 String profileFile;
410 ParcelFileDescriptor profileFd;
411 boolean autoStopProfiler;
412 boolean profiling;
413 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700414 public void setProfiler(String file, ParcelFileDescriptor fd) {
415 if (profiling) {
416 if (fd != null) {
417 try {
418 fd.close();
419 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700420 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700421 }
422 }
423 return;
424 }
425 if (profileFd != null) {
426 try {
427 profileFd.close();
428 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700429 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700430 }
431 }
432 profileFile = file;
433 profileFd = fd;
434 }
435 public void startProfiling() {
436 if (profileFd == null || profiling) {
437 return;
438 }
439 try {
440 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
441 8 * 1024 * 1024, 0);
442 profiling = true;
443 } catch (RuntimeException e) {
444 Slog.w(TAG, "Profiling failed on path " + profileFile);
445 try {
446 profileFd.close();
447 profileFd = null;
448 } catch (IOException e2) {
449 Slog.w(TAG, "Failure closing profile fd", e2);
450 }
451 }
452 }
453 public void stopProfiling() {
454 if (profiling) {
455 profiling = false;
456 Debug.stopMethodTracing();
457 if (profileFd != null) {
458 try {
459 profileFd.close();
460 } catch (IOException e) {
461 }
462 }
463 profileFd = null;
464 profileFile = null;
465 }
466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 }
468
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400469 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700470 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700471 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800472 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
475
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400476 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 IBinder token;
478 List<ResultInfo> results;
479 public String toString() {
480 return "ResultData{token=" + token + " results" + results + "}";
481 }
482 }
483
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400484 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800485 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 String what;
487 String who;
488 }
489
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400490 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700491 String path;
492 ParcelFileDescriptor fd;
493 }
494
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400495 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700496 String path;
497 ParcelFileDescriptor fd;
498 }
499
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400500 static final class UpdateCompatibilityData {
501 String pkg;
502 CompatibilityInfo info;
503 }
Dianne Hackbornaa9d84c2011-05-09 19:00:59 -0700504
Romain Guy65b345f2011-07-27 18:51:50 -0700505 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700506
Romain Guy65b345f2011-07-27 18:51:50 -0700507 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700508 private static final String HEAP_COLUMN = "%13s %8s %8s %8s %8s %8s %8s";
509 private static final String ONE_COUNT_COLUMN = "%21s %8d";
510 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700511 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 // Formatting for checkin service - update version if row format changes
514 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 1;
Bob Leee5408332009-09-04 18:31:17 -0700515
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700516 private void updatePendingConfiguration(Configuration config) {
517 synchronized (mPackages) {
518 if (mPendingConfiguration == null ||
519 mPendingConfiguration.isOtherSeqNewer(config)) {
520 mPendingConfiguration = config;
521 }
522 }
523 }
524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 public final void schedulePauseActivity(IBinder token, boolean finished,
526 boolean userLeaving, int configChanges) {
527 queueOrSendMessage(
528 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
529 token,
530 (userLeaving ? 1 : 0),
531 configChanges);
532 }
533
534 public final void scheduleStopActivity(IBinder token, boolean showWindow,
535 int configChanges) {
536 queueOrSendMessage(
537 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
538 token, 0, configChanges);
539 }
540
541 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
542 queueOrSendMessage(
543 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
544 token);
545 }
546
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800547 public final void scheduleSleeping(IBinder token, boolean sleeping) {
548 queueOrSendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
549 }
550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 public final void scheduleResumeActivity(IBinder token, boolean isForward) {
552 queueOrSendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
553 }
554
555 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
556 ResultData res = new ResultData();
557 res.token = token;
558 res.results = results;
559 queueOrSendMessage(H.SEND_RESULT, res);
560 }
561
562 // we use token to identify this activity without having to send the
563 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700564 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700565 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
566 Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700567 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
568 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700569 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
571 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700572 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 r.intent = intent;
574 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400575 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 r.state = state;
577
578 r.pendingResults = pendingResults;
579 r.pendingIntents = pendingNewIntents;
580
581 r.startsNotResumed = notResumed;
582 r.isForward = isForward;
583
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700584 r.profileFile = profileName;
585 r.profileFd = profileFd;
586 r.autoStopProfiler = autoStopProfiler;
587
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700588 updatePendingConfiguration(curConfig);
589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
591 }
592
593 public final void scheduleRelaunchActivity(IBinder token,
594 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800595 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800596 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
597 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 }
599
600 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
601 NewIntentData data = new NewIntentData();
602 data.intents = intents;
603 data.token = token;
604
605 queueOrSendMessage(H.NEW_INTENT, data);
606 }
607
608 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
609 int configChanges) {
610 queueOrSendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
611 configChanges);
612 }
613
614 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400615 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
616 boolean sync) {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700617 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
618 sync, false, mAppThread.asBinder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400620 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 queueOrSendMessage(H.RECEIVER, r);
622 }
623
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400624 public final void scheduleCreateBackupAgent(ApplicationInfo app,
625 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700626 CreateBackupAgentData d = new CreateBackupAgentData();
627 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400628 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700629 d.backupMode = backupMode;
630
631 queueOrSendMessage(H.CREATE_BACKUP_AGENT, d);
632 }
633
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400634 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
635 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700636 CreateBackupAgentData d = new CreateBackupAgentData();
637 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400638 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700639
640 queueOrSendMessage(H.DESTROY_BACKUP_AGENT, d);
641 }
642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 public final void scheduleCreateService(IBinder token,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400644 ServiceInfo info, CompatibilityInfo compatInfo) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 CreateServiceData s = new CreateServiceData();
646 s.token = token;
647 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400648 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649
650 queueOrSendMessage(H.CREATE_SERVICE, s);
651 }
652
653 public final void scheduleBindService(IBinder token, Intent intent,
654 boolean rebind) {
655 BindServiceData s = new BindServiceData();
656 s.token = token;
657 s.intent = intent;
658 s.rebind = rebind;
659
Amith Yamasani742a6712011-05-04 14:49:28 -0700660 if (DEBUG_SERVICE)
661 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
662 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 queueOrSendMessage(H.BIND_SERVICE, s);
664 }
665
666 public final void scheduleUnbindService(IBinder token, Intent intent) {
667 BindServiceData s = new BindServiceData();
668 s.token = token;
669 s.intent = intent;
670
671 queueOrSendMessage(H.UNBIND_SERVICE, s);
672 }
673
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700674 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700675 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 ServiceArgsData s = new ServiceArgsData();
677 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700678 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700680 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 s.args = args;
682
683 queueOrSendMessage(H.SERVICE_ARGS, s);
684 }
685
686 public final void scheduleStopService(IBinder token) {
687 queueOrSendMessage(H.STOP_SERVICE, token);
688 }
689
690 public final void bindApplication(String processName,
691 ApplicationInfo appInfo, List<ProviderInfo> providers,
692 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700693 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Siva Velusamy92a8b222012-03-09 16:24:04 -0800695 int debugMode, boolean enableOpenGlTrace, boolean isRestrictedBackupMode,
696 boolean persistent, Configuration config, CompatibilityInfo compatInfo,
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700697 Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698
699 if (services != null) {
700 // Setup the service cache in the ServiceManager
701 ServiceManager.initServiceCache(services);
702 }
703
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800704 setCoreSettings(coreSettings);
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 AppBindData data = new AppBindData();
707 data.processName = processName;
708 data.appInfo = appInfo;
709 data.providers = providers;
710 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 data.instrumentationArgs = instrumentationArgs;
712 data.instrumentationWatcher = instrumentationWatcher;
713 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800714 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700715 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700716 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400718 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700719 data.initProfileFile = profileFile;
720 data.initProfileFd = profileFd;
721 data.initAutoStopProfiler = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 queueOrSendMessage(H.BIND_APPLICATION, data);
723 }
724
725 public final void scheduleExit() {
726 queueOrSendMessage(H.EXIT_APPLICATION, null);
727 }
728
Christopher Tate5e1ab332009-09-01 20:32:49 -0700729 public final void scheduleSuicide() {
730 queueOrSendMessage(H.SUICIDE, null);
731 }
732
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 public void requestThumbnail(IBinder token) {
734 queueOrSendMessage(H.REQUEST_THUMBNAIL, token);
735 }
736
737 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700738 updatePendingConfiguration(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 queueOrSendMessage(H.CONFIGURATION_CHANGED, config);
740 }
741
742 public void updateTimeZone() {
743 TimeZone.setDefault(null);
744 }
745
Robert Greenwalt03595d02010-11-02 14:08:23 -0700746 public void clearDnsCache() {
747 // a non-standard API to get this to libcore
748 InetAddress.clearDnsCache();
749 }
750
Robert Greenwalt434203a2010-10-11 16:00:27 -0700751 public void setHttpProxy(String host, String port, String exclList) {
752 Proxy.setHttpProxySystemProperty(host, port, exclList);
753 }
754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 public void processInBackground() {
756 mH.removeMessages(H.GC_WHEN_IDLE);
757 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
758 }
759
760 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700761 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700762 try {
763 data.fd = ParcelFileDescriptor.dup(fd);
764 data.token = servicetoken;
765 data.args = args;
766 queueOrSendMessage(H.DUMP_SERVICE, data);
767 } catch (IOException e) {
768 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770 }
771
772 // This function exists to make sure all receiver dispatching is
773 // correctly ordered, since these are one-way calls and the binder driver
774 // applies transaction ordering per object for such calls.
775 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700776 int resultCode, String dataStr, Bundle extras, boolean ordered,
777 boolean sticky) throws RemoteException {
778 receiver.performReceive(intent, resultCode, dataStr, extras, ordered, sticky);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
Bob Leee5408332009-09-04 18:31:17 -0700780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 public void scheduleLowMemory() {
782 queueOrSendMessage(H.LOW_MEMORY, null);
783 }
784
785 public void scheduleActivityConfigurationChanged(IBinder token) {
786 queueOrSendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
787 }
788
Romain Guy7eabe552011-07-21 14:56:34 -0700789 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
790 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700791 ProfilerControlData pcd = new ProfilerControlData();
792 pcd.path = path;
793 pcd.fd = fd;
Romain Guy7eabe552011-07-21 14:56:34 -0700794 queueOrSendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800795 }
796
Andy McFadden824c5102010-07-09 16:26:57 -0700797 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
798 DumpHeapData dhd = new DumpHeapData();
799 dhd.path = path;
800 dhd.fd = fd;
801 queueOrSendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0);
802 }
803
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700804 public void setSchedulingGroup(int group) {
805 // Note: do this immediately, since going into the foreground
806 // should happen regardless of what pending work we have to do
807 // and the activity manager will wait for us to report back that
808 // we are done before sending us to the background.
809 try {
810 Process.setProcessGroup(Process.myPid(), group);
811 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800812 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700813 }
814 }
Bob Leee5408332009-09-04 18:31:17 -0700815
Dianne Hackborn3025ef32009-08-31 21:31:47 -0700816 public void getMemoryInfo(Debug.MemoryInfo outInfo) {
817 Debug.getMemoryInfo(outInfo);
818 }
Bob Leee5408332009-09-04 18:31:17 -0700819
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700820 public void dispatchPackageBroadcast(int cmd, String[] packages) {
821 queueOrSendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
822 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700823
824 public void scheduleCrash(String msg) {
825 queueOrSendMessage(H.SCHEDULE_CRASH, msg);
826 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700827
Dianne Hackborn30d71892010-12-11 10:37:55 -0800828 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
829 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700830 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700831 try {
832 data.fd = ParcelFileDescriptor.dup(fd);
833 data.token = activitytoken;
834 data.prefix = prefix;
835 data.args = args;
836 queueOrSendMessage(H.DUMP_ACTIVITY, data);
837 } catch (IOException e) {
838 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700839 }
840 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700841
Marco Nelissen18cb2872011-11-15 11:19:53 -0800842 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
843 String[] args) {
844 DumpComponentInfo data = new DumpComponentInfo();
845 try {
846 data.fd = ParcelFileDescriptor.dup(fd);
847 data.token = providertoken;
848 data.args = args;
849 queueOrSendMessage(H.DUMP_PROVIDER, data);
850 } catch (IOException e) {
851 Slog.w(TAG, "dumpProvider failed", e);
852 }
853 }
854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 @Override
Dianne Hackbornb437e092011-08-05 17:50:29 -0700856 public Debug.MemoryInfo dumpMemInfo(FileDescriptor fd, boolean checkin,
857 boolean all, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700858 FileOutputStream fout = new FileOutputStream(fd);
859 PrintWriter pw = new PrintWriter(fout);
860 try {
Romain Guya998dff2012-03-23 18:58:36 -0700861 return dumpMemInfo(pw, checkin, all);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700862 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700863 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700864 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700865 }
866
Romain Guya998dff2012-03-23 18:58:36 -0700867 private Debug.MemoryInfo dumpMemInfo(PrintWriter pw, boolean checkin, boolean all) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 long nativeMax = Debug.getNativeHeapSize() / 1024;
869 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
870 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
871
872 Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
873 Debug.getMemoryInfo(memInfo);
874
Dianne Hackbornb437e092011-08-05 17:50:29 -0700875 if (!all) {
876 return memInfo;
877 }
878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 Runtime runtime = Runtime.getRuntime();
880
881 long dalvikMax = runtime.totalMemory() / 1024;
882 long dalvikFree = runtime.freeMemory() / 1024;
883 long dalvikAllocated = dalvikMax - dalvikFree;
884 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700885 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700886 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
887 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 int globalAssetCount = AssetManager.getGlobalAssetCount();
889 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
890 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
891 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
892 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700893 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800894 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 // For checkin, we print one long comma-separated list of values
Dianne Hackbornb437e092011-08-05 17:50:29 -0700897 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 // NOTE: if you change anything significant below, also consider changing
899 // ACTIVITY_THREAD_CHECKIN_VERSION.
Bob Leee5408332009-09-04 18:31:17 -0700900 String processName = (mBoundApplication != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 ? mBoundApplication.processName : "unknown";
Bob Leee5408332009-09-04 18:31:17 -0700902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 // Header
904 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
905 pw.print(Process.myPid()); pw.print(',');
906 pw.print(processName); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 // Heap info - max
909 pw.print(nativeMax); pw.print(',');
910 pw.print(dalvikMax); pw.print(',');
911 pw.print("N/A,");
912 pw.print(nativeMax + dalvikMax); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 // Heap info - allocated
915 pw.print(nativeAllocated); pw.print(',');
916 pw.print(dalvikAllocated); pw.print(',');
917 pw.print("N/A,");
918 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 // Heap info - free
921 pw.print(nativeFree); pw.print(',');
922 pw.print(dalvikFree); pw.print(',');
923 pw.print("N/A,");
924 pw.print(nativeFree + dalvikFree); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 // Heap info - proportional set size
927 pw.print(memInfo.nativePss); pw.print(',');
928 pw.print(memInfo.dalvikPss); pw.print(',');
929 pw.print(memInfo.otherPss); pw.print(',');
930 pw.print(memInfo.nativePss + memInfo.dalvikPss + memInfo.otherPss); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700931
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 // Heap info - shared
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700933 pw.print(memInfo.nativeSharedDirty); pw.print(',');
934 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
935 pw.print(memInfo.otherSharedDirty); pw.print(',');
936 pw.print(memInfo.nativeSharedDirty + memInfo.dalvikSharedDirty
937 + memInfo.otherSharedDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700938
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 // Heap info - private
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700940 pw.print(memInfo.nativePrivateDirty); pw.print(',');
941 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
942 pw.print(memInfo.otherPrivateDirty); pw.print(',');
943 pw.print(memInfo.nativePrivateDirty + memInfo.dalvikPrivateDirty
944 + memInfo.otherPrivateDirty); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 // Object counts
947 pw.print(viewInstanceCount); pw.print(',');
948 pw.print(viewRootInstanceCount); pw.print(',');
949 pw.print(appContextInstanceCount); pw.print(',');
950 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 pw.print(globalAssetCount); pw.print(',');
953 pw.print(globalAssetManagerCount); pw.print(',');
954 pw.print(binderLocalObjectCount); pw.print(',');
955 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 pw.print(binderDeathObjectCount); pw.print(',');
958 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800961 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800962 pw.print(stats.memoryUsed / 1024); pw.print(',');
963 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700964 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800965 for (int i = 0; i < stats.dbStats.size(); i++) {
966 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700967 pw.print(','); pw.print(dbStats.dbName);
968 pw.print(','); pw.print(dbStats.pageSize);
969 pw.print(','); pw.print(dbStats.dbSize);
970 pw.print(','); pw.print(dbStats.lookaside);
971 pw.print(','); pw.print(dbStats.cache);
972 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800973 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700974 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700975
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700976 return memInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 }
Bob Leee5408332009-09-04 18:31:17 -0700978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 // otherwise, show human-readable format
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700980 printRow(pw, HEAP_COLUMN, "", "", "Shared", "Private", "Heap", "Heap", "Heap");
981 printRow(pw, HEAP_COLUMN, "", "Pss", "Dirty", "Dirty", "Size", "Alloc", "Free");
982 printRow(pw, HEAP_COLUMN, "", "------", "------", "------", "------", "------",
983 "------");
984 printRow(pw, HEAP_COLUMN, "Native", memInfo.nativePss, memInfo.nativeSharedDirty,
985 memInfo.nativePrivateDirty, nativeMax, nativeAllocated, nativeFree);
986 printRow(pw, HEAP_COLUMN, "Dalvik", memInfo.dalvikPss, memInfo.dalvikSharedDirty,
987 memInfo.dalvikPrivateDirty, dalvikMax, dalvikAllocated, dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700989 int otherPss = memInfo.otherPss;
990 int otherSharedDirty = memInfo.otherSharedDirty;
991 int otherPrivateDirty = memInfo.otherPrivateDirty;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700993 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Romain Guy65b345f2011-07-27 18:51:50 -0700994 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700995 memInfo.getOtherPss(i), memInfo.getOtherSharedDirty(i),
996 memInfo.getOtherPrivateDirty(i), "", "", "");
997 otherPss -= memInfo.getOtherPss(i);
998 otherSharedDirty -= memInfo.getOtherSharedDirty(i);
999 otherPrivateDirty -= memInfo.getOtherPrivateDirty(i);
1000 }
1001
1002 printRow(pw, HEAP_COLUMN, "Unknown", otherPss, otherSharedDirty,
1003 otherPrivateDirty, "", "", "");
1004 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1005 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1006 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
1007 nativeFree+dalvikFree);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008
1009 pw.println(" ");
1010 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001011 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 viewRootInstanceCount);
1013
1014 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1015 "Activities:", activityInstanceCount);
1016
1017 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1018 "AssetManagers:", globalAssetManagerCount);
1019
1020 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1021 "Proxy Binders:", binderProxyObjectCount);
1022 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1023
1024 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 // SQLite mem info
1027 pw.println(" ");
1028 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001029 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1030 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1031 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001032 pw.println(" ");
1033 int N = stats.dbStats.size();
1034 if (N > 0) {
1035 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001036 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1037 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001038 for (int i = 0; i < N; i++) {
1039 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001040 printRow(pw, DB_INFO_FORMAT,
1041 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1042 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1043 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1044 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001045 }
1046 }
Bob Leee5408332009-09-04 18:31:17 -07001047
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001048 // Asset details.
1049 String assetAlloc = AssetManager.getAssetAllocations();
1050 if (assetAlloc != null) {
1051 pw.println(" ");
1052 pw.println(" Asset Allocations");
1053 pw.print(assetAlloc);
1054 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001055
1056 return memInfo;
1057 }
1058
1059 @Override
1060 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1061 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001062 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064
Jeff Brown6754ba22011-12-14 20:20:01 -08001065 @Override
1066 public void dumpDbInfo(FileDescriptor fd, String[] args) {
1067 PrintWriter pw = new PrintWriter(new FileOutputStream(fd));
1068 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1069 SQLiteDebug.dump(printer, args);
1070 pw.flush();
1071 }
1072
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001073 @Override
1074 public void unstableProviderDied(IBinder provider) {
1075 queueOrSendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
1076 }
1077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 private void printRow(PrintWriter pw, String format, Object...objs) {
1079 pw.println(String.format(format, objs));
1080 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001081
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001082 public void setCoreSettings(Bundle coreSettings) {
1083 queueOrSendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001084 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001085
1086 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1087 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1088 ucd.pkg = pkg;
1089 ucd.info = info;
1090 queueOrSendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
1091 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001092
1093 public void scheduleTrimMemory(int level) {
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001094 queueOrSendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001095 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 }
1098
Romain Guy65b345f2011-07-27 18:51:50 -07001099 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 public static final int LAUNCH_ACTIVITY = 100;
1101 public static final int PAUSE_ACTIVITY = 101;
1102 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1103 public static final int STOP_ACTIVITY_SHOW = 103;
1104 public static final int STOP_ACTIVITY_HIDE = 104;
1105 public static final int SHOW_WINDOW = 105;
1106 public static final int HIDE_WINDOW = 106;
1107 public static final int RESUME_ACTIVITY = 107;
1108 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001109 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 public static final int BIND_APPLICATION = 110;
1111 public static final int EXIT_APPLICATION = 111;
1112 public static final int NEW_INTENT = 112;
1113 public static final int RECEIVER = 113;
1114 public static final int CREATE_SERVICE = 114;
1115 public static final int SERVICE_ARGS = 115;
1116 public static final int STOP_SERVICE = 116;
1117 public static final int REQUEST_THUMBNAIL = 117;
1118 public static final int CONFIGURATION_CHANGED = 118;
1119 public static final int CLEAN_UP_CONTEXT = 119;
1120 public static final int GC_WHEN_IDLE = 120;
1121 public static final int BIND_SERVICE = 121;
1122 public static final int UNBIND_SERVICE = 122;
1123 public static final int DUMP_SERVICE = 123;
1124 public static final int LOW_MEMORY = 124;
1125 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1126 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001127 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001128 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001129 public static final int DESTROY_BACKUP_AGENT = 129;
1130 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001131 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001132 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001133 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001134 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001135 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001136 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001137 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001138 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001139 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001140 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001141 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001142 public static final int UNSTABLE_PROVIDER_DIED = 142;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001144 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 switch (code) {
1146 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1147 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1148 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1149 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1150 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1151 case SHOW_WINDOW: return "SHOW_WINDOW";
1152 case HIDE_WINDOW: return "HIDE_WINDOW";
1153 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1154 case SEND_RESULT: return "SEND_RESULT";
1155 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1156 case BIND_APPLICATION: return "BIND_APPLICATION";
1157 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1158 case NEW_INTENT: return "NEW_INTENT";
1159 case RECEIVER: return "RECEIVER";
1160 case CREATE_SERVICE: return "CREATE_SERVICE";
1161 case SERVICE_ARGS: return "SERVICE_ARGS";
1162 case STOP_SERVICE: return "STOP_SERVICE";
1163 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1164 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1165 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1166 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1167 case BIND_SERVICE: return "BIND_SERVICE";
1168 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1169 case DUMP_SERVICE: return "DUMP_SERVICE";
1170 case LOW_MEMORY: return "LOW_MEMORY";
1171 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1172 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001173 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001174 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1175 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001176 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001177 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001178 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001179 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001180 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001181 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001182 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001183 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001184 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001185 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001186 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001187 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001188 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 }
1190 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001191 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001194 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 switch (msg.what) {
1196 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001197 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001198 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199
1200 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001201 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001202 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001203 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } break;
1205 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001206 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001207 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001208 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001209 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 } break;
1211 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001212 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001214 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001215 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 break;
1217 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001218 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001220 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 break;
1222 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001223 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001225 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 break;
1227 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001228 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 break;
1232 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001233 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 break;
1237 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001238 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 break;
1242 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001243 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 handleResumeActivity((IBinder)msg.obj, true,
1245 msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001246 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 break;
1248 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001249 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001251 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 break;
1253 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001254 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1256 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001257 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 break;
1259 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001260 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 AppBindData data = (AppBindData)msg.obj;
1262 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001263 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 break;
1265 case EXIT_APPLICATION:
1266 if (mInitialApplication != null) {
1267 mInitialApplication.onTerminate();
1268 }
1269 Looper.myLooper().quit();
1270 break;
1271 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001272 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001274 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 break;
1276 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001279 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001280 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 break;
1282 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001283 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001285 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 break;
1287 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001290 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 break;
1292 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 break;
1297 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 break;
1302 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001305 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 break;
1308 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 break;
1313 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001314 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001315 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001316 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001317 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 break;
1319 case CLEAN_UP_CONTEXT:
1320 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1321 cci.context.performFinalCleanup(cci.who, cci.what);
1322 break;
1323 case GC_WHEN_IDLE:
1324 scheduleGcIdler();
1325 break;
1326 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001327 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 break;
1329 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001330 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001332 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 break;
1334 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001339 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001340 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001341 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001342 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001343 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001344 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001345 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001346 break;
1347 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001349 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001350 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001351 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001352 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001353 Process.killProcess(Process.myPid());
1354 break;
1355 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001356 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001357 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001358 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001359 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001360 case ENABLE_JIT:
1361 ensureJitEnabled();
1362 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001363 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001364 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001365 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001366 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001367 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001368 case SCHEDULE_CRASH:
1369 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001370 case DUMP_HEAP:
1371 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1372 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001373 case DUMP_ACTIVITY:
1374 handleDumpActivity((DumpComponentInfo)msg.obj);
1375 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001376 case DUMP_PROVIDER:
1377 handleDumpProvider((DumpComponentInfo)msg.obj);
1378 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001379 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001380 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001381 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001382 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001383 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001384 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001386 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001387 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001388 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001389 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1390 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001391 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001392 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001393 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001394 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001396 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001397 case UNSTABLE_PROVIDER_DIED:
1398 handleUnstableProviderDied((IBinder)msg.obj, false);
1399 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001401 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
Bob Leee5408332009-09-04 18:31:17 -07001403
Brian Carlstromed7e0072011-03-24 13:27:57 -07001404 private void maybeSnapshot() {
1405 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001406 // convert the *private* ActivityThread.PackageInfo to *public* known
1407 // android.content.pm.PackageInfo
1408 String packageName = mBoundApplication.info.mPackageName;
1409 android.content.pm.PackageInfo packageInfo = null;
1410 try {
1411 Context context = getSystemContext();
1412 if(context == null) {
1413 Log.e(TAG, "cannot get a valid context");
1414 return;
1415 }
1416 PackageManager pm = context.getPackageManager();
1417 if(pm == null) {
1418 Log.e(TAG, "cannot get a valid PackageManager");
1419 return;
1420 }
1421 packageInfo = pm.getPackageInfo(
1422 packageName, PackageManager.GET_ACTIVITIES);
1423 } catch (NameNotFoundException e) {
1424 Log.e(TAG, "cannot get package info for " + packageName, e);
1425 }
1426 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001427 }
1428 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430
Romain Guy65b345f2011-07-27 18:51:50 -07001431 private class Idler implements MessageQueue.IdleHandler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001433 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001434 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001435 if (mBoundApplication != null && mProfiler.profileFd != null
1436 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001437 stopProfiling = true;
1438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 if (a != null) {
1440 mNewActivities = null;
1441 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001442 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001444 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 TAG, "Reporting idle of " + a +
1446 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001447 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 if (a.activity != null && !a.activity.mFinished) {
1449 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001450 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001451 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001453 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455 }
1456 prev = a;
1457 a = a.nextIdle;
1458 prev.nextIdle = null;
1459 } while (a != null);
1460 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001461 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001462 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001463 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001464 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 return false;
1466 }
1467 }
1468
1469 final class GcIdler implements MessageQueue.IdleHandler {
1470 public final boolean queueIdle() {
1471 doGcIfNeeded();
1472 return false;
1473 }
1474 }
1475
Romain Guy65b345f2011-07-27 18:51:50 -07001476 private static class ResourcesKey {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001477 final private String mResDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001478 final private int mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001479 final private Configuration mOverrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001480 final private float mScale;
1481 final private int mHash;
Bob Leee5408332009-09-04 18:31:17 -07001482
Jeff Browna492c3a2012-08-23 19:48:44 -07001483 ResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale) {
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001484 mResDir = resDir;
Jeff Browna492c3a2012-08-23 19:48:44 -07001485 mDisplayId = displayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001486 if (overrideConfiguration != null) {
1487 if (Configuration.EMPTY.equals(overrideConfiguration)) {
1488 overrideConfiguration = null;
1489 }
1490 }
1491 mOverrideConfiguration = overrideConfiguration;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001492 mScale = scale;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001493 int hash = 17;
1494 hash = 31 * hash + mResDir.hashCode();
Jeff Browna492c3a2012-08-23 19:48:44 -07001495 hash = 31 * hash + mDisplayId;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001496 hash = 31 * hash + (mOverrideConfiguration != null
1497 ? mOverrideConfiguration.hashCode() : 0);
1498 hash = 31 * hash + Float.floatToIntBits(mScale);
1499 mHash = hash;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001500 }
Bob Leee5408332009-09-04 18:31:17 -07001501
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001502 @Override
1503 public int hashCode() {
1504 return mHash;
1505 }
1506
1507 @Override
1508 public boolean equals(Object obj) {
1509 if (!(obj instanceof ResourcesKey)) {
1510 return false;
1511 }
1512 ResourcesKey peer = (ResourcesKey) obj;
Dianne Hackborn756220b2012-08-14 16:45:30 -07001513 if (!mResDir.equals(peer.mResDir)) {
1514 return false;
1515 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001516 if (mDisplayId != peer.mDisplayId) {
1517 return false;
1518 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001519 if (mOverrideConfiguration != peer.mOverrideConfiguration) {
1520 if (mOverrideConfiguration == null || peer.mOverrideConfiguration == null) {
1521 return false;
1522 }
1523 if (!mOverrideConfiguration.equals(peer.mOverrideConfiguration)) {
1524 return false;
1525 }
1526 }
1527 if (mScale != peer.mScale) {
1528 return false;
1529 }
1530 return true;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -07001531 }
1532 }
1533
Romain Guy65b345f2011-07-27 18:51:50 -07001534 public static ActivityThread currentActivityThread() {
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07001535 return sThreadLocal.get();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537
Romain Guy65b345f2011-07-27 18:51:50 -07001538 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001539 ActivityThread am = currentActivityThread();
1540 return (am != null && am.mBoundApplication != null)
1541 ? am.mBoundApplication.processName : null;
1542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543
Romain Guy65b345f2011-07-27 18:51:50 -07001544 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001545 ActivityThread am = currentActivityThread();
1546 return am != null ? am.mInitialApplication : null;
1547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001549 public static IPackageManager getPackageManager() {
1550 if (sPackageManager != null) {
1551 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1552 return sPackageManager;
1553 }
1554 IBinder b = ServiceManager.getService("package");
1555 //Slog.v("PackageManager", "default service binder = " + b);
1556 sPackageManager = IPackageManager.Stub.asInterface(b);
1557 //Slog.v("PackageManager", "default service = " + sPackageManager);
1558 return sPackageManager;
1559 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560
Jeff Browna492c3a2012-08-23 19:48:44 -07001561 private void flushDisplayMetricsLocked() {
1562 mDefaultDisplayMetrics.clear();
1563 }
1564
1565 DisplayMetrics getDisplayMetricsLocked(int displayId, CompatibilityInfo ci) {
1566 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1567 DisplayMetrics dm = isDefaultDisplay ? mDefaultDisplayMetrics.get(ci) : null;
1568 if (dm != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001569 return dm;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001570 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001571 dm = new DisplayMetrics();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001572
Jeff Brownbd6e1502012-08-28 03:27:37 -07001573 DisplayManagerGlobal displayManager = DisplayManagerGlobal.getInstance();
Jeff Brown848c2dc2012-08-19 20:18:08 -07001574 if (displayManager == null) {
1575 // may be null early in system startup
Jeff Brown848c2dc2012-08-19 20:18:08 -07001576 dm.setToDefaults();
1577 return dm;
1578 }
1579
Jeff Browna492c3a2012-08-23 19:48:44 -07001580 if (isDefaultDisplay) {
1581 mDefaultDisplayMetrics.put(ci, dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001582 }
Jeff Brown848c2dc2012-08-19 20:18:08 -07001583
Jeff Brownd32460c2012-07-20 16:15:36 -07001584 CompatibilityInfoHolder cih = new CompatibilityInfoHolder();
1585 cih.set(ci);
Jeff Browna492c3a2012-08-23 19:48:44 -07001586 Display d = displayManager.getCompatibleDisplay(displayId, cih);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001587 d.getMetrics(dm);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001588 //Slog.i("foo", "New metrics: w=" + metrics.widthPixels + " h="
1589 // + metrics.heightPixels + " den=" + metrics.density
1590 // + " xdpi=" + metrics.xdpi + " ydpi=" + metrics.ydpi);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001591 return dm;
1592 }
1593
Romain Guy65b345f2011-07-27 18:51:50 -07001594 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001595 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1596 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001597 if (config == null) {
1598 return null;
1599 }
1600 if (compat != null && !compat.supportsScreen()) {
1601 mMainThreadConfig.setTo(config);
1602 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001603 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001604 }
1605 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001608 /**
1609 * Creates the top level Resources for applications with the given compatibility info.
1610 *
1611 * @param resDir the resource directory.
1612 * @param compInfo the compability info. It will use the default compatibility info when it's
1613 * null.
1614 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001615 Resources getTopLevelResources(String resDir,
1616 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001617 CompatibilityInfo compInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001618 ResourcesKey key = new ResourcesKey(resDir,
1619 displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001620 compInfo.applicationScale);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001621 Resources r;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001623 // Resources is app scale dependent.
1624 if (false) {
1625 Slog.w(TAG, "getTopLevelResources: " + resDir + " / "
1626 + compInfo.applicationScale);
1627 }
1628 WeakReference<Resources> wr = mActiveResources.get(key);
1629 r = wr != null ? wr.get() : null;
1630 //if (r != null) Slog.i(TAG, "isUpToDate " + resDir + ": " + r.getAssets().isUpToDate());
1631 if (r != null && r.getAssets().isUpToDate()) {
1632 if (false) {
1633 Slog.w(TAG, "Returning cached resources " + r + " " + resDir
1634 + ": appScale=" + r.getCompatibilityInfo().applicationScale);
1635 }
1636 return r;
1637 }
1638 }
1639
1640 //if (r != null) {
1641 // Slog.w(TAG, "Throwing away out-of-date resources!!!! "
1642 // + r + " " + resDir);
1643 //}
1644
1645 AssetManager assets = new AssetManager();
1646 if (assets.addAssetPath(resDir) == 0) {
1647 return null;
1648 }
1649
1650 //Slog.i(TAG, "Resource: key=" + key + ", display metrics=" + metrics);
Jeff Browna492c3a2012-08-23 19:48:44 -07001651 DisplayMetrics dm = getDisplayMetricsLocked(displayId, null);
Dianne Hackborn756220b2012-08-14 16:45:30 -07001652 Configuration config;
Jeff Browna492c3a2012-08-23 19:48:44 -07001653 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
1654 if (!isDefaultDisplay || key.mOverrideConfiguration != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07001655 config = new Configuration(getConfiguration());
Jeff Browna492c3a2012-08-23 19:48:44 -07001656 if (!isDefaultDisplay) {
1657 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, config);
1658 }
1659 if (key.mOverrideConfiguration != null) {
1660 config.updateFrom(key.mOverrideConfiguration);
1661 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001662 } else {
1663 config = getConfiguration();
1664 }
Jeff Browna492c3a2012-08-23 19:48:44 -07001665 r = new Resources(assets, dm, config, compInfo);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001666 if (false) {
1667 Slog.i(TAG, "Created app resources " + resDir + " " + r + ": "
1668 + r.getConfiguration() + " appScale="
1669 + r.getCompatibilityInfo().applicationScale);
1670 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07001671
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001672 synchronized (mPackages) {
1673 WeakReference<Resources> wr = mActiveResources.get(key);
1674 Resources existing = wr != null ? wr.get() : null;
1675 if (existing != null && existing.getAssets().isUpToDate()) {
1676 // Someone else already created the resources while we were
1677 // unlocked; go ahead and use theirs.
1678 r.getAssets().close();
1679 return existing;
1680 }
1681
1682 // XXX need to remove entries when weak references go away
1683 mActiveResources.put(key, new WeakReference<Resources>(r));
1684 return r;
1685 }
1686 }
1687
1688 /**
1689 * Creates the top level resources for the given package.
1690 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001691 Resources getTopLevelResources(String resDir,
1692 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001693 LoadedApk pkgInfo) {
Jeff Browna492c3a2012-08-23 19:48:44 -07001694 return getTopLevelResources(resDir, displayId, overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001695 pkgInfo.mCompatibilityInfo.get());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001696 }
1697
1698 final Handler getHandler() {
1699 return mH;
1700 }
1701
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001702 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1703 int flags) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001704 synchronized (mPackages) {
1705 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1707 ref = mPackages.get(packageName);
1708 } else {
1709 ref = mResourcePackages.get(packageName);
1710 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001711 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001712 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001713 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1714 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 if (packageInfo != null && (packageInfo.mResources == null
1716 || packageInfo.mResources.getAssets().isUpToDate())) {
1717 if (packageInfo.isSecurityViolation()
1718 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1719 throw new SecurityException(
1720 "Requesting code from " + packageName
1721 + " to be run in process "
1722 + mBoundApplication.processName
1723 + "/" + mBoundApplication.appInfo.uid);
1724 }
1725 return packageInfo;
1726 }
1727 }
1728
1729 ApplicationInfo ai = null;
1730 try {
1731 ai = getPackageManager().getApplicationInfo(packageName,
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001732 PackageManager.GET_SHARED_LIBRARY_FILES, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001734 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
1736
1737 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001738 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
1740
1741 return null;
1742 }
1743
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001744 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1745 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1747 boolean securityViolation = includeCode && ai.uid != 0
1748 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001749 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001750 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1752 |Context.CONTEXT_IGNORE_SECURITY))
1753 == Context.CONTEXT_INCLUDE_CODE) {
1754 if (securityViolation) {
1755 String msg = "Requesting code from " + ai.packageName
1756 + " (with uid " + ai.uid + ")";
1757 if (mBoundApplication != null) {
1758 msg = msg + " to be run in process "
1759 + mBoundApplication.processName + " (with uid "
1760 + mBoundApplication.appInfo.uid + ")";
1761 }
1762 throw new SecurityException(msg);
1763 }
1764 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001765 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 }
1767
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001768 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1769 CompatibilityInfo compatInfo) {
1770 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 }
1772
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001773 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
1774 synchronized (mPackages) {
1775 WeakReference<LoadedApk> ref;
1776 if (includeCode) {
1777 ref = mPackages.get(packageName);
1778 } else {
1779 ref = mResourcePackages.get(packageName);
1780 }
1781 return ref != null ? ref.get() : null;
1782 }
1783 }
1784
Romain Guy65b345f2011-07-27 18:51:50 -07001785 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
1787 synchronized (mPackages) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001788 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 if (includeCode) {
1790 ref = mPackages.get(aInfo.packageName);
1791 } else {
1792 ref = mResourcePackages.get(aInfo.packageName);
1793 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001794 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (packageInfo == null || (packageInfo.mResources != null
1796 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001797 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 : "Loading resource-only package ") + aInfo.packageName
1799 + " (in " + (mBoundApplication != null
1800 ? mBoundApplication.processName : null)
1801 + ")");
1802 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001803 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 securityViolation, includeCode &&
1805 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1806 if (includeCode) {
1807 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001808 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 } else {
1810 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001811 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 }
1814 return packageInfo;
1815 }
1816 }
1817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 ActivityThread() {
1819 }
1820
1821 public ApplicationThread getApplicationThread()
1822 {
1823 return mAppThread;
1824 }
1825
1826 public Instrumentation getInstrumentation()
1827 {
1828 return mInstrumentation;
1829 }
1830
1831 public Configuration getConfiguration() {
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001832 return mResConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 }
1834
1835 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001836 return mProfiler != null && mProfiler.profileFile != null
1837 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001841 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 }
1843
1844 public Looper getLooper() {
1845 return mLooper;
1846 }
1847
1848 public Application getApplication() {
1849 return mInitialApplication;
1850 }
Bob Leee5408332009-09-04 18:31:17 -07001851
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001852 public String getProcessName() {
1853 return mBoundApplication.processName;
1854 }
Bob Leee5408332009-09-04 18:31:17 -07001855
Dianne Hackborn21556372010-02-04 16:34:40 -08001856 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 synchronized (this) {
1858 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001859 ContextImpl context =
1860 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001861 LoadedApk info = new LoadedApk(this, "android", context, null,
1862 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 context.init(info, null, this);
1864 context.getResources().updateConfiguration(
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001865 getConfiguration(), getDisplayMetricsLocked(
Jeff Browna492c3a2012-08-23 19:48:44 -07001866 Display.DEFAULT_DISPLAY,
1867 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001869 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 // + ": " + context.getResources().getConfiguration());
1871 }
1872 }
1873 return mSystemContext;
1874 }
1875
Mike Cleron432b7132009-09-24 15:28:29 -07001876 public void installSystemApplicationInfo(ApplicationInfo info) {
1877 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001878 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001879 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001880 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001881
1882 // give ourselves a default profiler
1883 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001884 }
1885 }
1886
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001887 void ensureJitEnabled() {
1888 if (!mJitEnabled) {
1889 mJitEnabled = true;
1890 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1891 }
1892 }
1893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 void scheduleGcIdler() {
1895 if (!mGcIdlerScheduled) {
1896 mGcIdlerScheduled = true;
1897 Looper.myQueue().addIdleHandler(mGcIdler);
1898 }
1899 mH.removeMessages(H.GC_WHEN_IDLE);
1900 }
1901
1902 void unscheduleGcIdler() {
1903 if (mGcIdlerScheduled) {
1904 mGcIdlerScheduled = false;
1905 Looper.myQueue().removeIdleHandler(mGcIdler);
1906 }
1907 mH.removeMessages(H.GC_WHEN_IDLE);
1908 }
1909
1910 void doGcIfNeeded() {
1911 mGcIdlerScheduled = false;
1912 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001913 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 // + "m now=" + now);
1915 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001916 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 BinderInternal.forceGc("bg");
1918 }
1919 }
1920
Jeff Hamilton52d32032011-01-08 15:31:26 -06001921 public void registerOnActivityPausedListener(Activity activity,
1922 OnActivityPausedListener listener) {
1923 synchronized (mOnPauseListeners) {
1924 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1925 if (list == null) {
1926 list = new ArrayList<OnActivityPausedListener>();
1927 mOnPauseListeners.put(activity, list);
1928 }
1929 list.add(listener);
1930 }
1931 }
1932
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001933 public void unregisterOnActivityPausedListener(Activity activity,
1934 OnActivityPausedListener listener) {
1935 synchronized (mOnPauseListeners) {
1936 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1937 if (list != null) {
1938 list.remove(listener);
1939 }
1940 }
1941 }
1942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 public final ActivityInfo resolveActivityInfo(Intent intent) {
1944 ActivityInfo aInfo = intent.resolveActivityInfo(
1945 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
1946 if (aInfo == null) {
1947 // Throw an exception.
1948 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07001949 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 }
1951 return aInfo;
1952 }
Bob Leee5408332009-09-04 18:31:17 -07001953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001956 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001957 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07001959 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 r.intent = intent;
1961 r.state = state;
1962 r.parent = parent;
1963 r.embeddedID = id;
1964 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07001965 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 if (localLOGV) {
1967 ComponentName compname = intent.getComponent();
1968 String name;
1969 if (compname != null) {
1970 name = compname.toShortString();
1971 } else {
1972 name = "(Intent " + intent + ").getComponent() returned null";
1973 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001974 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 + ", comp=" + name
1976 + ", token=" + token);
1977 }
Christopher Tateb70f3df2009-04-07 16:07:59 -07001978 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 }
1980
1981 public final Activity getActivity(IBinder token) {
1982 return mActivities.get(token).activity;
1983 }
1984
1985 public final void sendActivityResult(
1986 IBinder token, String id, int requestCode,
1987 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001988 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07001989 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
1991 list.add(new ResultInfo(id, requestCode, resultCode, data));
1992 mAppThread.scheduleSendResult(token, list);
1993 }
1994
1995 // if the thread hasn't started yet, we don't have the handler, so just
1996 // save the messages until we're ready.
Romain Guy65b345f2011-07-27 18:51:50 -07001997 private void queueOrSendMessage(int what, Object obj) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 queueOrSendMessage(what, obj, 0, 0);
1999 }
2000
Romain Guy65b345f2011-07-27 18:51:50 -07002001 private void queueOrSendMessage(int what, Object obj, int arg1) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 queueOrSendMessage(what, obj, arg1, 0);
2003 }
2004
Romain Guy65b345f2011-07-27 18:51:50 -07002005 private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 synchronized (this) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07002007 if (DEBUG_MESSAGES) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2009 + ": " + arg1 + " / " + obj);
2010 Message msg = Message.obtain();
2011 msg.what = what;
2012 msg.obj = obj;
2013 msg.arg1 = arg1;
2014 msg.arg2 = arg2;
2015 mH.sendMessage(msg);
2016 }
2017 }
2018
Dianne Hackborn21556372010-02-04 16:34:40 -08002019 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 String what) {
2021 ContextCleanupInfo cci = new ContextCleanupInfo();
2022 cci.context = context;
2023 cci.who = who;
2024 cci.what = what;
2025 queueOrSendMessage(H.CLEAN_UP_CONTEXT, cci);
2026 }
2027
Romain Guy65b345f2011-07-27 18:51:50 -07002028 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2030
2031 ActivityInfo aInfo = r.activityInfo;
2032 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002033 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 Context.CONTEXT_INCLUDE_CODE);
2035 }
Bob Leee5408332009-09-04 18:31:17 -07002036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002037 ComponentName component = r.intent.getComponent();
2038 if (component == null) {
2039 component = r.intent.resolveActivity(
2040 mInitialApplication.getPackageManager());
2041 r.intent.setComponent(component);
2042 }
2043
2044 if (r.activityInfo.targetActivity != null) {
2045 component = new ComponentName(r.activityInfo.packageName,
2046 r.activityInfo.targetActivity);
2047 }
2048
2049 Activity activity = null;
2050 try {
2051 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2052 activity = mInstrumentation.newActivity(
2053 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002054 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002055 r.intent.setExtrasClassLoader(cl);
2056 if (r.state != null) {
2057 r.state.setClassLoader(cl);
2058 }
2059 } catch (Exception e) {
2060 if (!mInstrumentation.onException(activity, e)) {
2061 throw new RuntimeException(
2062 "Unable to instantiate activity " + component
2063 + ": " + e.toString(), e);
2064 }
2065 }
2066
2067 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002068 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002069
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002070 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2071 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 TAG, r + ": app=" + app
2073 + ", appName=" + app.getPackageName()
2074 + ", pkg=" + r.packageInfo.getPackageName()
2075 + ", comp=" + r.intent.getComponent().toShortString()
2076 + ", dir=" + r.packageInfo.getAppDir());
2077
2078 if (activity != null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08002079 ContextImpl appContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 appContext.init(r.packageInfo, r.token, this);
2081 appContext.setOuterContext(activity);
2082 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002083 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002084 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002085 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002086 activity.attach(appContext, this, getInstrumentation(), r.token,
2087 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002088 r.embeddedID, r.lastNonConfigurationInstances, config);
Bob Leee5408332009-09-04 18:31:17 -07002089
Christopher Tateb70f3df2009-04-07 16:07:59 -07002090 if (customIntent != null) {
2091 activity.mIntent = customIntent;
2092 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002093 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 activity.mStartedActivity = false;
2095 int theme = r.activityInfo.getThemeResource();
2096 if (theme != 0) {
2097 activity.setTheme(theme);
2098 }
2099
2100 activity.mCalled = false;
2101 mInstrumentation.callActivityOnCreate(activity, r.state);
2102 if (!activity.mCalled) {
2103 throw new SuperNotCalledException(
2104 "Activity " + r.intent.getComponent().toShortString() +
2105 " did not call through to super.onCreate()");
2106 }
2107 r.activity = activity;
2108 r.stopped = true;
2109 if (!r.activity.mFinished) {
2110 activity.performStart();
2111 r.stopped = false;
2112 }
2113 if (!r.activity.mFinished) {
2114 if (r.state != null) {
2115 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2116 }
2117 }
2118 if (!r.activity.mFinished) {
2119 activity.mCalled = false;
2120 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2121 if (!activity.mCalled) {
2122 throw new SuperNotCalledException(
2123 "Activity " + r.intent.getComponent().toShortString() +
2124 " did not call through to super.onPostCreate()");
2125 }
2126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 }
2128 r.paused = true;
2129
2130 mActivities.put(r.token, r);
2131
2132 } catch (SuperNotCalledException e) {
2133 throw e;
2134
2135 } catch (Exception e) {
2136 if (!mInstrumentation.onException(activity, e)) {
2137 throw new RuntimeException(
2138 "Unable to start activity " + component
2139 + ": " + e.toString(), e);
2140 }
2141 }
2142
2143 return activity;
2144 }
2145
Romain Guy65b345f2011-07-27 18:51:50 -07002146 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 // If we are getting ready to gc after going to the background, well
2148 // we are back active so skip it.
2149 unscheduleGcIdler();
2150
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002151 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002152 mProfiler.setProfiler(r.profileFile, r.profileFd);
2153 mProfiler.startProfiling();
2154 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002155 }
2156
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002157 // Make sure we are running with the most recent config.
2158 handleConfigurationChanged(null, null);
2159
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002160 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 TAG, "Handling launch of " + r);
Christopher Tateb70f3df2009-04-07 16:07:59 -07002162 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163
2164 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002165 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002166 Bundle oldState = r.state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 handleResumeActivity(r.token, false, r.isForward);
2168
2169 if (!r.activity.mFinished && r.startsNotResumed) {
2170 // The activity manager actually wants this one to start out
2171 // paused, because it needs to be visible but isn't in the
2172 // foreground. We accomplish this by going through the
2173 // normal startup (because activities expect to go through
2174 // onResume() the first time they run, before their window
2175 // is displayed), and then pausing it. However, in this case
2176 // we do -not- need to do the full pause cycle (of freezing
2177 // and such) because the activity manager assumes it can just
2178 // retain the current state it has.
2179 try {
2180 r.activity.mCalled = false;
2181 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002182 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002183 // we need to be created again. But we only do this
2184 // for pre-Honeycomb apps, which always save their state
2185 // when pausing, so we can not have them save their state
2186 // when restarting from a paused state. For HC and later,
2187 // we want to (and can) let the state be saved as the normal
2188 // part of stopping the activity.
2189 if (r.isPreHoneycomb()) {
2190 r.state = oldState;
2191 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 if (!r.activity.mCalled) {
2193 throw new SuperNotCalledException(
2194 "Activity " + r.intent.getComponent().toShortString() +
2195 " did not call through to super.onPause()");
2196 }
2197
2198 } catch (SuperNotCalledException e) {
2199 throw e;
2200
2201 } catch (Exception e) {
2202 if (!mInstrumentation.onException(r.activity, e)) {
2203 throw new RuntimeException(
2204 "Unable to pause activity "
2205 + r.intent.getComponent().toShortString()
2206 + ": " + e.toString(), e);
2207 }
2208 }
2209 r.paused = true;
2210 }
2211 } else {
2212 // If there was an error, for any reason, tell the activity
2213 // manager to stop us.
2214 try {
2215 ActivityManagerNative.getDefault()
2216 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2217 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002218 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002219 }
2220 }
2221 }
2222
Romain Guy65b345f2011-07-27 18:51:50 -07002223 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 List<Intent> intents) {
2225 final int N = intents.size();
2226 for (int i=0; i<N; i++) {
2227 Intent intent = intents.get(i);
2228 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002229 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2231 }
2232 }
2233
2234 public final void performNewIntents(IBinder token,
2235 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002236 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237 if (r != null) {
2238 final boolean resumed = !r.paused;
2239 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002240 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 mInstrumentation.callActivityOnPause(r.activity);
2242 }
2243 deliverNewIntents(r, intents);
2244 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002245 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002246 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 }
2248 }
2249 }
Bob Leee5408332009-09-04 18:31:17 -07002250
Romain Guy65b345f2011-07-27 18:51:50 -07002251 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 performNewIntents(data.token, data.intents);
2253 }
2254
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002255 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2256
2257 /**
2258 * Return the Intent that's currently being handled by a
2259 * BroadcastReceiver on this thread, or null if none.
2260 * @hide
2261 */
2262 public static Intent getIntentBeingBroadcast() {
2263 return sCurrentBroadcastIntent.get();
2264 }
2265
Romain Guy65b345f2011-07-27 18:51:50 -07002266 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267 // If we are getting ready to gc after going to the background, well
2268 // we are back active so skip it.
2269 unscheduleGcIdler();
2270
2271 String component = data.intent.getComponent().getClassName();
2272
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002273 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002274 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002275
2276 IActivityManager mgr = ActivityManagerNative.getDefault();
2277
Romain Guy65b345f2011-07-27 18:51:50 -07002278 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 try {
2280 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2281 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002282 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2284 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002285 if (DEBUG_BROADCAST) Slog.i(TAG,
2286 "Finishing failed broadcast to " + data.intent.getComponent());
2287 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 throw new RuntimeException(
2289 "Unable to instantiate receiver " + component
2290 + ": " + e.toString(), e);
2291 }
2292
2293 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002294 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002295
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002296 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 TAG, "Performing receive of " + data.intent
2298 + ": app=" + app
2299 + ", appName=" + app.getPackageName()
2300 + ", pkg=" + packageInfo.getPackageName()
2301 + ", comp=" + data.intent.getComponent().toShortString()
2302 + ", dir=" + packageInfo.getAppDir());
2303
Dianne Hackborn21556372010-02-04 16:34:40 -08002304 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002305 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002306 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 receiver.onReceive(context.getReceiverRestrictedContext(),
2308 data.intent);
2309 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002310 if (DEBUG_BROADCAST) Slog.i(TAG,
2311 "Finishing failed broadcast to " + data.intent.getComponent());
2312 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002313 if (!mInstrumentation.onException(receiver, e)) {
2314 throw new RuntimeException(
2315 "Unable to start receiver " + component
2316 + ": " + e.toString(), e);
2317 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002318 } finally {
2319 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002320 }
2321
Dianne Hackborne829fef2010-10-26 17:44:01 -07002322 if (receiver.getPendingResult() != null) {
2323 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 }
2325 }
2326
Christopher Tate181fafa2009-05-14 11:12:14 -07002327 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002328 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002329 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002330
2331 // no longer idle; we have backup work to do
2332 unscheduleGcIdler();
2333
2334 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002335 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002336 String packageName = packageInfo.mPackageName;
2337 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002338 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002339 + " already exists");
2340 return;
2341 }
Bob Leee5408332009-09-04 18:31:17 -07002342
Christopher Tate181fafa2009-05-14 11:12:14 -07002343 BackupAgent agent = null;
2344 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002345
Christopher Tate79ec80d2011-06-24 14:58:49 -07002346 // full backup operation but no app-supplied agent? use the default implementation
2347 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2348 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002349 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002350 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002351
Christopher Tate181fafa2009-05-14 11:12:14 -07002352 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002353 IBinder binder = null;
2354 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002355 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2356
Christopher Tated1475e02009-07-09 15:36:17 -07002357 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002358 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002359
2360 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002361 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002362 context.init(packageInfo, null, this);
2363 context.setOuterContext(agent);
2364 agent.attach(context);
2365
2366 agent.onCreate();
2367 binder = agent.onBind();
2368 mBackupAgents.put(packageName, agent);
2369 } catch (Exception e) {
2370 // If this is during restore, fail silently; otherwise go
2371 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002372 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002373 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2374 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002375 throw e;
2376 }
2377 // falling through with 'binder' still null
2378 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002379
2380 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002381 try {
2382 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2383 } catch (RemoteException e) {
2384 // nothing to do.
2385 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002386 } catch (Exception e) {
2387 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002388 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002389 }
2390 }
2391
2392 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002393 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002394 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002395
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002396 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002397 String packageName = packageInfo.mPackageName;
2398 BackupAgent agent = mBackupAgents.get(packageName);
2399 if (agent != null) {
2400 try {
2401 agent.onDestroy();
2402 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002403 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002404 e.printStackTrace();
2405 }
2406 mBackupAgents.remove(packageName);
2407 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002408 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002409 }
2410 }
2411
Romain Guy65b345f2011-07-27 18:51:50 -07002412 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 // If we are getting ready to gc after going to the background, well
2414 // we are back active so skip it.
2415 unscheduleGcIdler();
2416
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002417 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002418 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 Service service = null;
2420 try {
2421 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2422 service = (Service) cl.loadClass(data.info.name).newInstance();
2423 } catch (Exception e) {
2424 if (!mInstrumentation.onException(service, e)) {
2425 throw new RuntimeException(
2426 "Unable to instantiate service " + data.info.name
2427 + ": " + e.toString(), e);
2428 }
2429 }
2430
2431 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002432 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433
Dianne Hackborn21556372010-02-04 16:34:40 -08002434 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002435 context.init(packageInfo, null, this);
2436
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002437 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 context.setOuterContext(service);
2439 service.attach(context, this, data.info.name, data.token, app,
2440 ActivityManagerNative.getDefault());
2441 service.onCreate();
2442 mServices.put(data.token, service);
2443 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002444 ActivityManagerNative.getDefault().serviceDoneExecuting(
2445 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 } catch (RemoteException e) {
2447 // nothing to do.
2448 }
2449 } catch (Exception e) {
2450 if (!mInstrumentation.onException(service, e)) {
2451 throw new RuntimeException(
2452 "Unable to create service " + data.info.name
2453 + ": " + e.toString(), e);
2454 }
2455 }
2456 }
2457
Romain Guy65b345f2011-07-27 18:51:50 -07002458 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002460 if (DEBUG_SERVICE)
2461 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 if (s != null) {
2463 try {
2464 data.intent.setExtrasClassLoader(s.getClassLoader());
2465 try {
2466 if (!data.rebind) {
2467 IBinder binder = s.onBind(data.intent);
2468 ActivityManagerNative.getDefault().publishService(
2469 data.token, data.intent, binder);
2470 } else {
2471 s.onRebind(data.intent);
2472 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002473 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002475 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 } catch (RemoteException ex) {
2477 }
2478 } catch (Exception e) {
2479 if (!mInstrumentation.onException(s, e)) {
2480 throw new RuntimeException(
2481 "Unable to bind to service " + s
2482 + " with " + data.intent + ": " + e.toString(), e);
2483 }
2484 }
2485 }
2486 }
2487
Romain Guy65b345f2011-07-27 18:51:50 -07002488 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 Service s = mServices.get(data.token);
2490 if (s != null) {
2491 try {
2492 data.intent.setExtrasClassLoader(s.getClassLoader());
2493 boolean doRebind = s.onUnbind(data.intent);
2494 try {
2495 if (doRebind) {
2496 ActivityManagerNative.getDefault().unbindFinished(
2497 data.token, data.intent, doRebind);
2498 } else {
2499 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002500 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 }
2502 } catch (RemoteException ex) {
2503 }
2504 } catch (Exception e) {
2505 if (!mInstrumentation.onException(s, e)) {
2506 throw new RuntimeException(
2507 "Unable to unbind to service " + s
2508 + " with " + data.intent + ": " + e.toString(), e);
2509 }
2510 }
2511 }
2512 }
2513
Dianne Hackborn625ac272010-09-17 18:29:22 -07002514 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002515 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2516 try {
2517 Service s = mServices.get(info.token);
2518 if (s != null) {
2519 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2520 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2521 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002523 } finally {
2524 IoUtils.closeQuietly(info.fd);
2525 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 }
2527 }
2528
Dianne Hackborn625ac272010-09-17 18:29:22 -07002529 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002530 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2531 try {
2532 ActivityClientRecord r = mActivities.get(info.token);
2533 if (r != null && r.activity != null) {
2534 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2535 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2536 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002537 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002538 } finally {
2539 IoUtils.closeQuietly(info.fd);
2540 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002541 }
2542 }
2543
Marco Nelissen18cb2872011-11-15 11:19:53 -08002544 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002545 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2546 try {
2547 ProviderClientRecord r = mLocalProviders.get(info.token);
2548 if (r != null && r.mLocalProvider != null) {
2549 PrintWriter pw = new PrintWriter(new FileOutputStream(info.fd.getFileDescriptor()));
2550 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2551 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002552 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002553 } finally {
2554 IoUtils.closeQuietly(info.fd);
2555 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002556 }
2557 }
2558
Romain Guy65b345f2011-07-27 18:51:50 -07002559 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 Service s = mServices.get(data.token);
2561 if (s != null) {
2562 try {
2563 if (data.args != null) {
2564 data.args.setExtrasClassLoader(s.getClassLoader());
2565 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002566 int res;
2567 if (!data.taskRemoved) {
2568 res = s.onStartCommand(data.args, data.flags, data.startId);
2569 } else {
2570 s.onTaskRemoved(data.args);
2571 res = Service.START_TASK_REMOVED_COMPLETE;
2572 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002573
2574 QueuedWork.waitToFinish();
2575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002577 ActivityManagerNative.getDefault().serviceDoneExecuting(
2578 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 } catch (RemoteException e) {
2580 // nothing to do.
2581 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002582 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 } catch (Exception e) {
2584 if (!mInstrumentation.onException(s, e)) {
2585 throw new RuntimeException(
2586 "Unable to start service " + s
2587 + " with " + data.args + ": " + e.toString(), e);
2588 }
2589 }
2590 }
2591 }
2592
Romain Guy65b345f2011-07-27 18:51:50 -07002593 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 Service s = mServices.remove(token);
2595 if (s != null) {
2596 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002597 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 s.onDestroy();
2599 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002600 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002602 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002604
2605 QueuedWork.waitToFinish();
2606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002607 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002608 ActivityManagerNative.getDefault().serviceDoneExecuting(
2609 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 } catch (RemoteException e) {
2611 // nothing to do.
2612 }
2613 } catch (Exception e) {
2614 if (!mInstrumentation.onException(s, e)) {
2615 throw new RuntimeException(
2616 "Unable to stop service " + s
2617 + ": " + e.toString(), e);
2618 }
2619 }
2620 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002621 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 }
2623
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002624 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002626 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002627 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 + " finished=" + r.activity.mFinished);
2629 if (r != null && !r.activity.mFinished) {
2630 if (clearHide) {
2631 r.hideForNow = false;
2632 r.activity.mStartedActivity = false;
2633 }
2634 try {
2635 if (r.pendingIntents != null) {
2636 deliverNewIntents(r, r.pendingIntents);
2637 r.pendingIntents = null;
2638 }
2639 if (r.pendingResults != null) {
2640 deliverResults(r, r.pendingResults);
2641 r.pendingResults = null;
2642 }
2643 r.activity.performResume();
2644
Bob Leee5408332009-09-04 18:31:17 -07002645 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 r.paused = false;
2649 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 r.state = null;
2651 } catch (Exception e) {
2652 if (!mInstrumentation.onException(r.activity, e)) {
2653 throw new RuntimeException(
2654 "Unable to resume activity "
2655 + r.intent.getComponent().toShortString()
2656 + ": " + e.toString(), e);
2657 }
2658 }
2659 }
2660 return r;
2661 }
2662
Romain Guya998dff2012-03-23 18:58:36 -07002663 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002664 if (r.mPendingRemoveWindow != null) {
2665 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2666 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2667 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002668 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002669 r.activity.getClass().getName(), "Activity");
2670 }
2671 }
2672 r.mPendingRemoveWindow = null;
2673 r.mPendingRemoveWindowManager = null;
2674 }
2675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676 final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
2677 // If we are getting ready to gc after going to the background, well
2678 // we are back active so skip it.
2679 unscheduleGcIdler();
2680
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002681 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682
2683 if (r != null) {
2684 final Activity a = r.activity;
2685
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002686 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 TAG, "Resume " + r + " started activity: " +
2688 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2689 + ", finished: " + a.mFinished);
2690
2691 final int forwardBit = isForward ?
2692 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002693
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 // If the window hasn't yet been added to the window manager,
2695 // and this guy didn't finish itself or start another activity,
2696 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002697 boolean willBeVisible = !a.mStartedActivity;
2698 if (!willBeVisible) {
2699 try {
2700 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2701 a.getActivityToken());
2702 } catch (RemoteException e) {
2703 }
2704 }
2705 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 r.window = r.activity.getWindow();
2707 View decor = r.window.getDecorView();
2708 decor.setVisibility(View.INVISIBLE);
2709 ViewManager wm = a.getWindowManager();
2710 WindowManager.LayoutParams l = r.window.getAttributes();
2711 a.mDecor = decor;
2712 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2713 l.softInputMode |= forwardBit;
2714 if (a.mVisibleFromClient) {
2715 a.mWindowAdded = true;
2716 wm.addView(decor, l);
2717 }
2718
2719 // If the window has already been added, but during resume
2720 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002721 // window visible.
2722 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002723 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 TAG, "Launch " + r + " mStartedActivity set");
2725 r.hideForNow = true;
2726 }
2727
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002728 // Get rid of anything left hanging around.
2729 cleanUpPendingRemoveWindows(r);
2730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 // The window is now visible if it has been added, we are not
2732 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002733 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002734 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002736 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002737 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002738 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002739 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 r.newConfig = null;
2741 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002742 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 + isForward);
2744 WindowManager.LayoutParams l = r.window.getAttributes();
2745 if ((l.softInputMode
2746 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2747 != forwardBit) {
2748 l.softInputMode = (l.softInputMode
2749 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2750 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002751 if (r.activity.mVisibleFromClient) {
2752 ViewManager wm = a.getWindowManager();
2753 View decor = r.window.getDecorView();
2754 wm.updateViewLayout(decor, l);
2755 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757 r.activity.mVisibleFromServer = true;
2758 mNumVisibleActivities++;
2759 if (r.activity.mVisibleFromClient) {
2760 r.activity.makeVisible();
2761 }
2762 }
2763
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002764 if (!r.onlyLocalRequest) {
2765 r.nextIdle = mNewActivities;
2766 mNewActivities = r;
2767 if (localLOGV) Slog.v(
2768 TAG, "Scheduling idle handler for " + r);
2769 Looper.myQueue().addIdleHandler(new Idler());
2770 }
2771 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772
2773 } else {
2774 // If an exception was thrown when trying to resume, then
2775 // just end this activity.
2776 try {
2777 ActivityManagerNative.getDefault()
2778 .finishActivity(token, Activity.RESULT_CANCELED, null);
2779 } catch (RemoteException ex) {
2780 }
2781 }
2782 }
2783
2784 private int mThumbnailWidth = -1;
2785 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002786 private Bitmap mAvailThumbnailBitmap = null;
2787 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788
Romain Guy65b345f2011-07-27 18:51:50 -07002789 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002790 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002792 if (thumbnail == null) {
2793 int w = mThumbnailWidth;
2794 int h;
2795 if (w < 0) {
2796 Resources res = r.activity.getResources();
2797 mThumbnailHeight = h =
2798 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002800 mThumbnailWidth = w =
2801 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2802 } else {
2803 h = mThumbnailHeight;
2804 }
2805
2806 // On platforms where we don't want thumbnails, set dims to (0,0)
2807 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002808 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2809 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002810 thumbnail.eraseColor(0);
2811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 }
2813
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002814 if (thumbnail != null) {
2815 Canvas cv = mThumbnailCanvas;
2816 if (cv == null) {
2817 mThumbnailCanvas = cv = new Canvas();
2818 }
2819
2820 cv.setBitmap(thumbnail);
2821 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2822 mAvailThumbnailBitmap = thumbnail;
2823 thumbnail = null;
2824 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002825 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 } catch (Exception e) {
2829 if (!mInstrumentation.onException(r.activity, e)) {
2830 throw new RuntimeException(
2831 "Unable to create thumbnail of "
2832 + r.intent.getComponent().toShortString()
2833 + ": " + e.toString(), e);
2834 }
2835 thumbnail = null;
2836 }
2837
2838 return thumbnail;
2839 }
2840
Romain Guy65b345f2011-07-27 18:51:50 -07002841 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002843 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002845 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002846 if (userLeaving) {
2847 performUserLeavingActivity(r);
2848 }
Bob Leee5408332009-09-04 18:31:17 -07002849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002850 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002851 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002852
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002853 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08002854 if (r.isPreHoneycomb()) {
2855 QueuedWork.waitToFinish();
2856 }
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07002857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002858 // Tell the activity manager we have paused.
2859 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002860 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 } catch (RemoteException ex) {
2862 }
2863 }
2864 }
2865
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002866 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867 mInstrumentation.callActivityOnUserLeaving(r.activity);
2868 }
2869
2870 final Bundle performPauseActivity(IBinder token, boolean finished,
2871 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002872 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873 return r != null ? performPauseActivity(r, finished, saveState) : null;
2874 }
2875
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002876 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 boolean saveState) {
2878 if (r.paused) {
2879 if (r.activity.mFinished) {
2880 // If we are finishing, we won't call onResume() in certain cases.
2881 // So here we likewise don't want to call onPause() if the activity
2882 // isn't resumed.
2883 return null;
2884 }
2885 RuntimeException e = new RuntimeException(
2886 "Performing pause of activity that is not resumed: "
2887 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002888 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 }
2890 Bundle state = null;
2891 if (finished) {
2892 r.activity.mFinished = true;
2893 }
2894 try {
2895 // Next have the activity save its current state and managed dialogs...
2896 if (!r.activity.mFinished && saveState) {
2897 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04002898 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
2900 r.state = state;
2901 }
2902 // Now we are idle.
2903 r.activity.mCalled = false;
2904 mInstrumentation.callActivityOnPause(r.activity);
2905 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, r.activity.getComponentName().getClassName());
2906 if (!r.activity.mCalled) {
2907 throw new SuperNotCalledException(
2908 "Activity " + r.intent.getComponent().toShortString() +
2909 " did not call through to super.onPause()");
2910 }
2911
2912 } catch (SuperNotCalledException e) {
2913 throw e;
2914
2915 } catch (Exception e) {
2916 if (!mInstrumentation.onException(r.activity, e)) {
2917 throw new RuntimeException(
2918 "Unable to pause activity "
2919 + r.intent.getComponent().toShortString()
2920 + ": " + e.toString(), e);
2921 }
2922 }
2923 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06002924
2925 // Notify any outstanding on paused listeners
2926 ArrayList<OnActivityPausedListener> listeners;
2927 synchronized (mOnPauseListeners) {
2928 listeners = mOnPauseListeners.remove(r.activity);
2929 }
2930 int size = (listeners != null ? listeners.size() : 0);
2931 for (int i = 0; i < size; i++) {
2932 listeners.get(i).onPaused(r.activity);
2933 }
2934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002935 return state;
2936 }
2937
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002938 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002939 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002940 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
2942
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002943 private static class StopInfo implements Runnable {
2944 ActivityClientRecord activity;
2945 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 Bitmap thumbnail;
2947 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07002948
2949 @Override public void run() {
2950 // Tell activity manager we have been stopped.
2951 try {
2952 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
2953 ActivityManagerNative.getDefault().activityStopped(
2954 activity.token, state, thumbnail, description);
2955 } catch (RemoteException ex) {
2956 }
2957 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002960 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002961 public final IActivityManager.ContentProviderHolder holder;
2962 public final ProviderClientRecord client;
2963 public int stableCount;
2964 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08002965
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07002966 // When this is set, the stable and unstable ref counts are 0 and
2967 // we have a pending operation scheduled to remove the ref count
2968 // from the activity manager. On the activity manager we are still
2969 // holding an unstable ref, though it is not reflected in the counts
2970 // here.
2971 public boolean removePending;
2972
2973 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
2974 ProviderClientRecord inClient, int sCount, int uCount) {
2975 holder = inHolder;
2976 client = inClient;
2977 stableCount = sCount;
2978 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 }
2980 }
2981
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002982 /**
2983 * Core implementation of stopping an activity. Note this is a little
2984 * tricky because the server's meaning of stop is slightly different
2985 * than our client -- for the server, stop means to save state and give
2986 * it the result when it is done, but the window may still be visible.
2987 * For the client, we want to call onStop()/onStart() to indicate when
2988 * the activity's UI visibillity changes.
2989 */
Romain Guy65b345f2011-07-27 18:51:50 -07002990 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002991 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002992 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002993 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002994 if (r != null) {
2995 if (!keepShown && r.stopped) {
2996 if (r.activity.mFinished) {
2997 // If we are finishing, we won't call onResume() in certain
2998 // cases. So here we likewise don't want to call onStop()
2999 // if the activity isn't resumed.
3000 return;
3001 }
3002 RuntimeException e = new RuntimeException(
3003 "Performing stop of activity that is not resumed: "
3004 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003005 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 }
3007
3008 if (info != null) {
3009 try {
3010 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003011 // For now, don't create the thumbnail here; we are
3012 // doing that by doing a screen snapshot.
3013 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 info.description = r.activity.onCreateDescription();
3015 } catch (Exception e) {
3016 if (!mInstrumentation.onException(r.activity, e)) {
3017 throw new RuntimeException(
3018 "Unable to save state of activity "
3019 + r.intent.getComponent().toShortString()
3020 + ": " + e.toString(), e);
3021 }
3022 }
3023 }
3024
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003025 // Next have the activity save its current state and managed dialogs...
3026 if (!r.activity.mFinished && saveState) {
3027 if (r.state == null) {
3028 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003029 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003030 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3031 r.state = state;
3032 } else {
3033 state = r.state;
3034 }
3035 }
3036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003037 if (!keepShown) {
3038 try {
3039 // Now we are idle.
3040 r.activity.performStop();
3041 } catch (Exception e) {
3042 if (!mInstrumentation.onException(r.activity, e)) {
3043 throw new RuntimeException(
3044 "Unable to stop activity "
3045 + r.intent.getComponent().toShortString()
3046 + ": " + e.toString(), e);
3047 }
3048 }
3049 r.stopped = true;
3050 }
3051
3052 r.paused = true;
3053 }
3054 }
3055
Romain Guy65b345f2011-07-27 18:51:50 -07003056 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 View v = r.activity.mDecor;
3058 if (v != null) {
3059 if (show) {
3060 if (!r.activity.mVisibleFromServer) {
3061 r.activity.mVisibleFromServer = true;
3062 mNumVisibleActivities++;
3063 if (r.activity.mVisibleFromClient) {
3064 r.activity.makeVisible();
3065 }
3066 }
3067 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003068 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003069 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003071 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 r.newConfig = null;
3073 }
3074 } else {
3075 if (r.activity.mVisibleFromServer) {
3076 r.activity.mVisibleFromServer = false;
3077 mNumVisibleActivities--;
3078 v.setVisibility(View.INVISIBLE);
3079 }
3080 }
3081 }
3082 }
3083
Romain Guy65b345f2011-07-27 18:51:50 -07003084 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003085 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 r.activity.mConfigChangeFlags |= configChanges;
3087
3088 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003089 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003091 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 TAG, "Finishing stop of " + r + ": show=" + show
3093 + " win=" + r.window);
3094
3095 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003096
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003097 // Make sure any pending writes are now committed.
3098 if (!r.isPreHoneycomb()) {
3099 QueuedWork.waitToFinish();
3100 }
3101
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003102 // Schedule the call to tell the activity manager we have
3103 // stopped. We don't do this immediately, because we want to
3104 // have a chance for any other pending work (in particular memory
3105 // trim requests) to complete before you tell the activity
3106 // manager to proceed and allow us to go fully into the background.
3107 info.activity = r;
3108 info.state = r.state;
3109 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 }
3111
3112 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003113 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 if (r.stopped) {
3115 r.activity.performRestart();
3116 r.stopped = false;
3117 }
3118 }
3119
Romain Guy65b345f2011-07-27 18:51:50 -07003120 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003121 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003122
3123 if (r == null) {
3124 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3125 return;
3126 }
3127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003128 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003129 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003130 } else if (show && r.stopped) {
3131 // If we are getting ready to gc after going to the background, well
3132 // we are back active so skip it.
3133 unscheduleGcIdler();
3134
3135 r.activity.performRestart();
3136 r.stopped = false;
3137 }
3138 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003139 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 TAG, "Handle window " + r + " visibility: " + show);
3141 updateVisibility(r, show);
3142 }
3143 }
3144
Romain Guy65b345f2011-07-27 18:51:50 -07003145 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003146 ActivityClientRecord r = mActivities.get(token);
3147
3148 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003149 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003150 return;
3151 }
3152
3153 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003154 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003155 try {
3156 // Now we are idle.
3157 r.activity.performStop();
3158 } catch (Exception e) {
3159 if (!mInstrumentation.onException(r.activity, e)) {
3160 throw new RuntimeException(
3161 "Unable to stop activity "
3162 + r.intent.getComponent().toShortString()
3163 + ": " + e.toString(), e);
3164 }
3165 }
3166 r.stopped = true;
3167 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003168
3169 // Make sure any pending writes are now committed.
3170 if (!r.isPreHoneycomb()) {
3171 QueuedWork.waitToFinish();
3172 }
3173
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003174 // Tell activity manager we slept.
3175 try {
3176 ActivityManagerNative.getDefault().activitySlept(r.token);
3177 } catch (RemoteException ex) {
3178 }
3179 } else {
3180 if (r.stopped && r.activity.mVisibleFromServer) {
3181 r.activity.performRestart();
3182 r.stopped = false;
3183 }
3184 }
3185 }
3186
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003187 private void handleSetCoreSettings(Bundle coreSettings) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003188 synchronized (mPackages) {
3189 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003190 }
3191 }
3192
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003193 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3194 LoadedApk apk = peekPackageInfo(data.pkg, false);
3195 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003196 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003197 }
3198 apk = peekPackageInfo(data.pkg, true);
3199 if (apk != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003200 apk.mCompatibilityInfo.set(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003201 }
3202 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003203 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003204 }
3205
Romain Guy65b345f2011-07-27 18:51:50 -07003206 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 final int N = results.size();
3208 for (int i=0; i<N; i++) {
3209 ResultInfo ri = results.get(i);
3210 try {
3211 if (ri.mData != null) {
3212 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3213 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003214 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003215 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 r.activity.dispatchActivityResult(ri.mResultWho,
3217 ri.mRequestCode, ri.mResultCode, ri.mData);
3218 } catch (Exception e) {
3219 if (!mInstrumentation.onException(r.activity, e)) {
3220 throw new RuntimeException(
3221 "Failure delivering result " + ri + " to activity "
3222 + r.intent.getComponent().toShortString()
3223 + ": " + e.toString(), e);
3224 }
3225 }
3226 }
3227 }
3228
Romain Guy65b345f2011-07-27 18:51:50 -07003229 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003230 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003231 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 if (r != null) {
3233 final boolean resumed = !r.paused;
3234 if (!r.activity.mFinished && r.activity.mDecor != null
3235 && r.hideForNow && resumed) {
3236 // We had hidden the activity because it started another
3237 // one... we have gotten a result back and we are not
3238 // paused, so make sure our window is visible.
3239 updateVisibility(r, true);
3240 }
3241 if (resumed) {
3242 try {
3243 // Now we are idle.
3244 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003245 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003246 mInstrumentation.callActivityOnPause(r.activity);
3247 if (!r.activity.mCalled) {
3248 throw new SuperNotCalledException(
3249 "Activity " + r.intent.getComponent().toShortString()
3250 + " did not call through to super.onPause()");
3251 }
3252 } catch (SuperNotCalledException e) {
3253 throw e;
3254 } catch (Exception e) {
3255 if (!mInstrumentation.onException(r.activity, e)) {
3256 throw new RuntimeException(
3257 "Unable to pause activity "
3258 + r.intent.getComponent().toShortString()
3259 + ": " + e.toString(), e);
3260 }
3261 }
3262 }
3263 deliverResults(r, res.results);
3264 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003265 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003266 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268 }
3269 }
3270
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003271 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 return performDestroyActivity(token, finishing, 0, false);
3273 }
3274
Romain Guy65b345f2011-07-27 18:51:50 -07003275 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003277 ActivityClientRecord r = mActivities.get(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003278 Class activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003279 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003281 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 r.activity.mConfigChangeFlags |= configChanges;
3283 if (finishing) {
3284 r.activity.mFinished = true;
3285 }
3286 if (!r.paused) {
3287 try {
3288 r.activity.mCalled = false;
3289 mInstrumentation.callActivityOnPause(r.activity);
Bob Leee5408332009-09-04 18:31:17 -07003290 EventLog.writeEvent(LOG_ON_PAUSE_CALLED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 r.activity.getComponentName().getClassName());
3292 if (!r.activity.mCalled) {
3293 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003294 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003295 + " did not call through to super.onPause()");
3296 }
3297 } catch (SuperNotCalledException e) {
3298 throw e;
3299 } catch (Exception e) {
3300 if (!mInstrumentation.onException(r.activity, e)) {
3301 throw new RuntimeException(
3302 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003303 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 + ": " + e.toString(), e);
3305 }
3306 }
3307 r.paused = true;
3308 }
3309 if (!r.stopped) {
3310 try {
3311 r.activity.performStop();
3312 } catch (SuperNotCalledException e) {
3313 throw e;
3314 } catch (Exception e) {
3315 if (!mInstrumentation.onException(r.activity, e)) {
3316 throw new RuntimeException(
3317 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003318 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 + ": " + e.toString(), e);
3320 }
3321 }
3322 r.stopped = true;
3323 }
3324 if (getNonConfigInstance) {
3325 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003326 r.lastNonConfigurationInstances
3327 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 } catch (Exception e) {
3329 if (!mInstrumentation.onException(r.activity, e)) {
3330 throw new RuntimeException(
3331 "Unable to retain activity "
3332 + r.intent.getComponent().toShortString()
3333 + ": " + e.toString(), e);
3334 }
3335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
3337 try {
3338 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003339 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 if (!r.activity.mCalled) {
3341 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003342 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 " did not call through to super.onDestroy()");
3344 }
3345 if (r.window != null) {
3346 r.window.closeAllPanels();
3347 }
3348 } catch (SuperNotCalledException e) {
3349 throw e;
3350 } catch (Exception e) {
3351 if (!mInstrumentation.onException(r.activity, e)) {
3352 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003353 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3354 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
3356 }
3357 }
3358 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003359 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 return r;
3361 }
3362
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003363 private static String safeToComponentShortString(Intent intent) {
3364 ComponentName component = intent.getComponent();
3365 return component == null ? "[Unknown]" : component.toShortString();
3366 }
3367
Romain Guy65b345f2011-07-27 18:51:50 -07003368 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003370 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 configChanges, getNonConfigInstance);
3372 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003373 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 WindowManager wm = r.activity.getWindowManager();
3375 View v = r.activity.mDecor;
3376 if (v != null) {
3377 if (r.activity.mVisibleFromServer) {
3378 mNumVisibleActivities--;
3379 }
3380 IBinder wtoken = v.getWindowToken();
3381 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003382 if (r.onlyLocalRequest) {
3383 // Hold off on removing this until the new activity's
3384 // window is being added.
3385 r.mPendingRemoveWindow = v;
3386 r.mPendingRemoveWindowManager = wm;
3387 } else {
3388 wm.removeViewImmediate(v);
3389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003391 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003392 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 r.activity.getClass().getName(), "Activity");
3394 }
3395 r.activity.mDecor = null;
3396 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003397 if (r.mPendingRemoveWindow == null) {
3398 // If we are delaying the removal of the activity window, then
3399 // we can't clean up all windows here. Note that we can't do
3400 // so later either, which means any windows that aren't closed
3401 // by the app will leak. Well we try to warning them a lot
3402 // about leaking windows, because that is a bug, so if they are
3403 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003404 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003405 r.activity.getClass().getName(), "Activity");
3406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407
3408 // Mocked out contexts won't be participating in the normal
3409 // process lifecycle, but if we're running with a proper
3410 // ApplicationContext we need to have it tear down things
3411 // cleanly.
3412 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003413 if (c instanceof ContextImpl) {
3414 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 r.activity.getClass().getName(), "Activity");
3416 }
3417 }
3418 if (finishing) {
3419 try {
3420 ActivityManagerNative.getDefault().activityDestroyed(token);
3421 } catch (RemoteException ex) {
3422 // If the system process has died, it's game over for everyone.
3423 }
3424 }
3425 }
3426
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003427 public final void requestRelaunchActivity(IBinder token,
3428 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3429 int configChanges, boolean notResumed, Configuration config,
3430 boolean fromServer) {
3431 ActivityClientRecord target = null;
3432
3433 synchronized (mPackages) {
3434 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3435 ActivityClientRecord r = mRelaunchingActivities.get(i);
3436 if (r.token == token) {
3437 target = r;
3438 if (pendingResults != null) {
3439 if (r.pendingResults != null) {
3440 r.pendingResults.addAll(pendingResults);
3441 } else {
3442 r.pendingResults = pendingResults;
3443 }
3444 }
3445 if (pendingNewIntents != null) {
3446 if (r.pendingIntents != null) {
3447 r.pendingIntents.addAll(pendingNewIntents);
3448 } else {
3449 r.pendingIntents = pendingNewIntents;
3450 }
3451 }
3452 break;
3453 }
3454 }
3455
3456 if (target == null) {
3457 target = new ActivityClientRecord();
3458 target.token = token;
3459 target.pendingResults = pendingResults;
3460 target.pendingIntents = pendingNewIntents;
3461 if (!fromServer) {
3462 ActivityClientRecord existing = mActivities.get(token);
3463 if (existing != null) {
3464 target.startsNotResumed = existing.paused;
3465 }
3466 target.onlyLocalRequest = true;
3467 }
3468 mRelaunchingActivities.add(target);
3469 queueOrSendMessage(H.RELAUNCH_ACTIVITY, target);
3470 }
3471
3472 if (fromServer) {
3473 target.startsNotResumed = notResumed;
3474 target.onlyLocalRequest = false;
3475 }
3476 if (config != null) {
3477 target.createdConfig = config;
3478 }
3479 target.pendingConfigChanges |= configChanges;
3480 }
3481 }
3482
Romain Guy65b345f2011-07-27 18:51:50 -07003483 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 // If we are getting ready to gc after going to the background, well
3485 // we are back active so skip it.
3486 unscheduleGcIdler();
3487
3488 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003489 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 // First: make sure we have the most recent configuration and most
3492 // recent version of the activity, or skip it if some previous call
3493 // had taken a more recent version.
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003494 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 int N = mRelaunchingActivities.size();
3496 IBinder token = tmp.token;
3497 tmp = null;
3498 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003499 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 if (r.token == token) {
3501 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003502 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503 mRelaunchingActivities.remove(i);
3504 i--;
3505 N--;
3506 }
3507 }
Bob Leee5408332009-09-04 18:31:17 -07003508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003510 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 return;
3512 }
Bob Leee5408332009-09-04 18:31:17 -07003513
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003514 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3515 + tmp.token + " with configChanges=0x"
3516 + Integer.toHexString(configChanges));
3517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 if (mPendingConfiguration != null) {
3519 changedConfig = mPendingConfiguration;
3520 mPendingConfiguration = null;
3521 }
3522 }
Bob Leee5408332009-09-04 18:31:17 -07003523
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003524 if (tmp.createdConfig != null) {
3525 // If the activity manager is passing us its current config,
3526 // assume that is really what we want regardless of what we
3527 // may have pending.
3528 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003529 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3530 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3531 if (changedConfig == null
3532 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3533 changedConfig = tmp.createdConfig;
3534 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003535 }
3536 }
3537
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003538 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003539 + tmp.token + ": changedConfig=" + changedConfig);
3540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 // If there was a pending configuration change, execute it first.
3542 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003543 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003544 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003545 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 }
Bob Leee5408332009-09-04 18:31:17 -07003547
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003548 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003549 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 if (r == null) {
3551 return;
3552 }
Bob Leee5408332009-09-04 18:31:17 -07003553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003555 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003556 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003557
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003558 r.activity.mChangingConfigurations = true;
3559
Dianne Hackborne2b04802010-12-09 09:24:55 -08003560 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003562 performPauseActivity(r.token, false, r.isPreHoneycomb());
3563 }
3564 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3565 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003566 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003567 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
Bob Leee5408332009-09-04 18:31:17 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 r.activity = null;
3573 r.window = null;
3574 r.hideForNow = false;
3575 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003576 // Merge any pending results and pending intents; don't just replace them
3577 if (tmp.pendingResults != null) {
3578 if (r.pendingResults == null) {
3579 r.pendingResults = tmp.pendingResults;
3580 } else {
3581 r.pendingResults.addAll(tmp.pendingResults);
3582 }
3583 }
3584 if (tmp.pendingIntents != null) {
3585 if (r.pendingIntents == null) {
3586 r.pendingIntents = tmp.pendingIntents;
3587 } else {
3588 r.pendingIntents.addAll(tmp.pendingIntents);
3589 }
3590 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003592
Christopher Tateb70f3df2009-04-07 16:07:59 -07003593 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
3595
Romain Guy65b345f2011-07-27 18:51:50 -07003596 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003597 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 Bitmap thumbnail = createThumbnailBitmap(r);
3599 CharSequence description = null;
3600 try {
3601 description = r.activity.onCreateDescription();
3602 } catch (Exception e) {
3603 if (!mInstrumentation.onException(r.activity, e)) {
3604 throw new RuntimeException(
3605 "Unable to create description of activity "
3606 + r.intent.getComponent().toShortString()
3607 + ": " + e.toString(), e);
3608 }
3609 }
3610 //System.out.println("Reporting top thumbnail " + thumbnail);
3611 try {
3612 ActivityManagerNative.getDefault().reportThumbnail(
3613 token, thumbnail, description);
3614 } catch (RemoteException ex) {
3615 }
3616 }
3617
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003618 ArrayList<ComponentCallbacks2> collectComponentCallbacksLocked(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003620 ArrayList<ComponentCallbacks2> callbacks
3621 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 if (mActivities.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003624 for (ActivityClientRecord ar : mActivities.values()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 Activity a = ar.activity;
3626 if (a != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003627 Configuration thisConfig = applyConfigCompatMainThread(mCurDefaultDisplayDpi,
3628 newConfig, ar.packageInfo.mCompatibilityInfo.getIfNeeded());
Romain Guya998dff2012-03-23 18:58:36 -07003629 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 // If the activity is currently resumed, its configuration
3631 // needs to change right now.
3632 callbacks.add(a);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003633 } else if (thisConfig != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 // Otherwise, we will tell it about the change
3635 // the next time it is resumed or shown. Note that
3636 // the activity manager may, before then, decide the
3637 // activity needs to be destroyed to handle its new
3638 // configuration.
Romain Guya998dff2012-03-23 18:58:36 -07003639 if (DEBUG_CONFIGURATION) {
3640 Slog.v(TAG, "Setting activity "
3641 + ar.activityInfo.name + " newConfig=" + thisConfig);
3642 }
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003643 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 }
3645 }
3646 }
3647 }
3648 if (mServices.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003649 for (Service service : mServices.values()) {
3650 callbacks.add(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 }
3652 }
3653 synchronized (mProviderMap) {
3654 if (mLocalProviders.size() > 0) {
Romain Guya998dff2012-03-23 18:58:36 -07003655 for (ProviderClientRecord providerClientRecord : mLocalProviders.values()) {
3656 callbacks.add(providerClientRecord.mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 }
3658 }
3659 }
3660 final int N = mAllApplications.size();
3661 for (int i=0; i<N; i++) {
3662 callbacks.add(mAllApplications.get(i));
3663 }
Bob Leee5408332009-09-04 18:31:17 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 return callbacks;
3666 }
Bob Leee5408332009-09-04 18:31:17 -07003667
Romain Guya998dff2012-03-23 18:58:36 -07003668 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003670 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003671 // we check the runtime type and act accordingly.
3672 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3673 if (activity != null) {
3674 activity.mCalled = false;
3675 }
Bob Leee5408332009-09-04 18:31:17 -07003676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 boolean shouldChangeConfig = false;
3678 if ((activity == null) || (activity.mCurrentConfig == null)) {
3679 shouldChangeConfig = true;
3680 } else {
Bob Leee5408332009-09-04 18:31:17 -07003681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 // If the new config is the same as the config this Activity
3683 // is already running with then don't bother calling
3684 // onConfigurationChanged
3685 int diff = activity.mCurrentConfig.diff(config);
3686 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 // If this activity doesn't handle any of the config changes
3688 // then don't bother calling onConfigurationChanged as we're
3689 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003690 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 shouldChangeConfig = true;
3692 }
3693 }
3694 }
Bob Leee5408332009-09-04 18:31:17 -07003695
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003696 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003697 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 if (shouldChangeConfig) {
3699 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003701 if (activity != null) {
3702 if (!activity.mCalled) {
3703 throw new SuperNotCalledException(
3704 "Activity " + activity.getLocalClassName() +
3705 " did not call through to super.onConfigurationChanged()");
3706 }
3707 activity.mConfigChangeFlags = 0;
3708 activity.mCurrentConfig = new Configuration(config);
3709 }
3710 }
3711 }
3712
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003713 public final void applyConfigurationToResources(Configuration config) {
3714 synchronized (mPackages) {
3715 applyConfigurationToResourcesLocked(config, null);
3716 }
3717 }
3718
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003719 final boolean applyConfigurationToResourcesLocked(Configuration config,
3720 CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003721 if (mResConfiguration == null) {
3722 mResConfiguration = new Configuration();
3723 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003724 if (!mResConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003725 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Skipping new config: curSeq="
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003726 + mResConfiguration.seq + ", newSeq=" + config.seq);
Dianne Hackbornae078162010-03-18 11:29:37 -07003727 return false;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003728 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003729 int changes = mResConfiguration.updateFrom(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003730 flushDisplayMetricsLocked();
3731 DisplayMetrics defaultDisplayMetrics = getDisplayMetricsLocked(
3732 Display.DEFAULT_DISPLAY, null);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003733
3734 if (compat != null && (mResCompatibilityInfo == null ||
3735 !mResCompatibilityInfo.equals(compat))) {
3736 mResCompatibilityInfo = compat;
3737 changes |= ActivityInfo.CONFIG_SCREEN_LAYOUT
3738 | ActivityInfo.CONFIG_SCREEN_SIZE
3739 | ActivityInfo.CONFIG_SMALLEST_SCREEN_SIZE;
3740 }
Bob Leee5408332009-09-04 18:31:17 -07003741
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003742 // set it for java, this also affects newly created Resources
3743 if (config.locale != null) {
3744 Locale.setDefault(config.locale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 }
Bob Leee5408332009-09-04 18:31:17 -07003746
Jeff Browna492c3a2012-08-23 19:48:44 -07003747 Resources.updateSystemConfiguration(config, defaultDisplayMetrics, compat);
Bob Leee5408332009-09-04 18:31:17 -07003748
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003749 ApplicationPackageManager.configurationChanged();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003750 //Slog.i(TAG, "Configuration changed in " + currentPackageName());
Dianne Hackborn756220b2012-08-14 16:45:30 -07003751
3752 Configuration tmpConfig = null;
3753
3754 Iterator<Map.Entry<ResourcesKey, WeakReference<Resources>>> it =
3755 mActiveResources.entrySet().iterator();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003756 while (it.hasNext()) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003757 Map.Entry<ResourcesKey, WeakReference<Resources>> entry = it.next();
3758 Resources r = entry.getValue().get();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003759 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003760 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Changing resources "
Dianne Hackborn694f79b2010-03-17 19:44:59 -07003761 + r + " config to: " + config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003762 int displayId = entry.getKey().mDisplayId;
3763 boolean isDefaultDisplay = (displayId == Display.DEFAULT_DISPLAY);
3764 DisplayMetrics dm = defaultDisplayMetrics;
3765 Configuration overrideConfig = entry.getKey().mOverrideConfiguration;
3766 if (!isDefaultDisplay || overrideConfig != null) {
Dianne Hackborn756220b2012-08-14 16:45:30 -07003767 if (tmpConfig == null) {
3768 tmpConfig = new Configuration();
3769 }
3770 tmpConfig.setTo(config);
Jeff Browna492c3a2012-08-23 19:48:44 -07003771 if (!isDefaultDisplay) {
3772 dm = getDisplayMetricsLocked(displayId, null);
3773 applyNonDefaultDisplayMetricsToConfigurationLocked(dm, tmpConfig);
3774 }
3775 if (overrideConfig != null) {
3776 tmpConfig.updateFrom(overrideConfig);
3777 }
Dianne Hackborn756220b2012-08-14 16:45:30 -07003778 r.updateConfiguration(tmpConfig, dm, compat);
3779 } else {
3780 r.updateConfiguration(config, dm, compat);
3781 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003782 //Slog.i(TAG, "Updated app resources " + v.getKey()
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003783 // + " " + r + ": " + r.getConfiguration());
3784 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003785 //Slog.i(TAG, "Removing old resources " + v.getKey());
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003786 it.remove();
3787 }
3788 }
Dianne Hackbornae078162010-03-18 11:29:37 -07003789
3790 return changes != 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003791 }
Dianne Hackborn836e2622011-10-04 18:32:39 -07003792
Jeff Browna492c3a2012-08-23 19:48:44 -07003793 final void applyNonDefaultDisplayMetricsToConfigurationLocked(
3794 DisplayMetrics dm, Configuration config) {
3795 config.screenLayout = Configuration.SCREENLAYOUT_SIZE_XLARGE
3796 | Configuration.SCREENLAYOUT_LONG_NO;
3797 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
3798 config.orientation = (dm.widthPixels >= dm.heightPixels) ?
3799 Configuration.ORIENTATION_LANDSCAPE : Configuration.ORIENTATION_PORTRAIT;
3800 config.densityDpi = dm.densityDpi;
3801 config.screenWidthDp = (int)(dm.widthPixels / dm.density);
3802 config.screenHeightDp = (int)(dm.heightPixels / dm.density);
3803 config.smallestScreenWidthDp = config.screenWidthDp; // assume screen does not rotate
3804 config.compatScreenWidthDp = config.screenWidthDp;
3805 config.compatScreenHeightDp = config.screenHeightDp;
3806 config.compatSmallestScreenWidthDp = config.smallestScreenWidthDp;
3807 }
3808
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003809 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003810 Configuration config = mConfiguration;
3811 if (mCompatConfiguration == null) {
3812 mCompatConfiguration = new Configuration();
3813 }
3814 mCompatConfiguration.setTo(mConfiguration);
3815 if (mResCompatibilityInfo != null && !mResCompatibilityInfo.supportsScreen()) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003816 mResCompatibilityInfo.applyToConfiguration(displayDensity, mCompatConfiguration);
Dianne Hackborn836e2622011-10-04 18:32:39 -07003817 config = mCompatConfiguration;
3818 }
3819 return config;
3820 }
3821
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003822 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003823
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003824 ArrayList<ComponentCallbacks2> callbacks = null;
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003825 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003826
3827 synchronized (mPackages) {
3828 if (mPendingConfiguration != null) {
3829 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3830 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003831 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003832 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003833 }
3834 mPendingConfiguration = null;
3835 }
3836
3837 if (config == null) {
3838 return;
3839 }
3840
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003841 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003842 + config);
3843
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003844 applyConfigurationToResourcesLocked(config, compat);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 if (mConfiguration == null) {
3847 mConfiguration = new Configuration();
3848 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003849 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003850 return;
3851 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003852 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003854 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 callbacks = collectComponentCallbacksLocked(false, config);
3856 }
Romain Guy65b345f2011-07-27 18:51:50 -07003857
3858 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003859 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003860
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003861 freeTextLayoutCachesIfNeeded(configDiff);
3862
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003863 if (callbacks != null) {
3864 final int N = callbacks.size();
3865 for (int i=0; i<N; i++) {
3866 performConfigurationChanged(callbacks.get(i), config);
3867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 }
3869 }
3870
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003871 final void freeTextLayoutCachesIfNeeded(int configDiff) {
3872 if (configDiff != 0) {
3873 // Ask text layout engine to free its caches if there is a locale change
3874 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3875 if (hasLocaleConfigChange) {
3876 Canvas.freeTextLayoutCaches();
3877 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3878 }
3879 }
3880 }
3881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003883 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003884 if (r == null || r.activity == null) {
3885 return;
3886 }
Bob Leee5408332009-09-04 18:31:17 -07003887
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003888 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003889 + r.activityInfo.name);
3890
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003891 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003892
3893 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 }
3895
Romain Guy7eabe552011-07-21 14:56:34 -07003896 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003897 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003898 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003899 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003900 default:
3901 mProfiler.setProfiler(pcd.path, pcd.fd);
3902 mProfiler.autoStopProfiler = false;
3903 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003904 break;
3905 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003906 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003907 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003908 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003909 } finally {
3910 try {
3911 pcd.fd.close();
3912 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003913 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003914 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003915 }
3916 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003917 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003918 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003919 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003920 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003921 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003922 }
3923 }
Bob Leee5408332009-09-04 18:31:17 -07003924
Romain Guya998dff2012-03-23 18:58:36 -07003925 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003926 if (managed) {
3927 try {
3928 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3929 } catch (IOException e) {
3930 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3931 + " -- can the process access this path?");
3932 } finally {
3933 try {
3934 dhd.fd.close();
3935 } catch (IOException e) {
3936 Slog.w(TAG, "Failure closing profile fd", e);
3937 }
3938 }
3939 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07003940 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07003941 }
3942 }
3943
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003944 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
3945 boolean hasPkgInfo = false;
3946 if (packages != null) {
3947 for (int i=packages.length-1; i>=0; i--) {
3948 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
3949 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003950 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003951 ref = mPackages.get(packages[i]);
3952 if (ref != null && ref.get() != null) {
3953 hasPkgInfo = true;
3954 } else {
3955 ref = mResourcePackages.get(packages[i]);
3956 if (ref != null && ref.get() != null) {
3957 hasPkgInfo = true;
3958 }
3959 }
3960 }
3961 mPackages.remove(packages[i]);
3962 mResourcePackages.remove(packages[i]);
3963 }
3964 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08003965 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07003966 hasPkgInfo);
3967 }
3968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 final void handleLowMemory() {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003970 ArrayList<ComponentCallbacks2> callbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003972 synchronized (mPackages) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 callbacks = collectComponentCallbacksLocked(true, null);
3974 }
Bob Leee5408332009-09-04 18:31:17 -07003975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003976 final int N = callbacks.size();
3977 for (int i=0; i<N; i++) {
3978 callbacks.get(i).onLowMemory();
3979 }
3980
Chris Tatece229052009-03-25 16:44:52 -07003981 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
3982 if (Process.myUid() != Process.SYSTEM_UID) {
3983 int sqliteReleased = SQLiteDatabase.releaseMemory();
3984 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
3985 }
Bob Leee5408332009-09-04 18:31:17 -07003986
Mike Reedcaf0df12009-04-27 14:32:05 -04003987 // Ask graphics to free up as much as possible (font/image caches)
3988 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003990 // Ask text layout engine to free also as much as possible
3991 Canvas.freeTextLayoutCaches();
3992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 BinderInternal.forceGc("mem");
3994 }
3995
Dianne Hackbornce86ba82011-07-13 19:33:41 -07003996 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003997 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003998
Jeff Brown98365d72012-08-19 20:30:52 -07003999 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004000 windowManager.startTrimMemory(level);
4001
4002 ArrayList<ComponentCallbacks2> callbacks;
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004003 synchronized (mPackages) {
4004 callbacks = collectComponentCallbacksLocked(true, null);
4005 }
4006
4007 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004008 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004009 callbacks.get(i).onTrimMemory(level);
4010 }
Romain Guy19f86e82012-04-23 15:19:07 -07004011
Jeff Brown98365d72012-08-19 20:30:52 -07004012 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004013 }
4014
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004015 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004016 if (Process.isIsolated()) {
4017 // Isolated processes aren't going to do UI.
4018 return;
4019 }
Romain Guya9582652011-11-10 14:20:10 -08004020 try {
4021 int uid = Process.myUid();
4022 String[] packages = getPackageManager().getPackagesForUid(uid);
4023
4024 // If there are several packages in this application we won't
4025 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004026 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004027 HardwareRenderer.setupDiskCache(cacheDir);
4028 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004029 }
4030 } catch (RemoteException e) {
4031 // Ignore
4032 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004033 }
4034
4035 private void updateDefaultDensity() {
4036 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4037 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4038 && !mDensityCompatMode) {
4039 Slog.i(TAG, "Switching default density from "
4040 + DisplayMetrics.DENSITY_DEVICE + " to "
4041 + mCurDefaultDisplayDpi);
4042 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4043 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4044 }
4045 }
4046
Romain Guy65b345f2011-07-27 18:51:50 -07004047 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004048 mBoundApplication = data;
4049 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004050 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004052 mProfiler = new Profiler();
4053 mProfiler.profileFile = data.initProfileFile;
4054 mProfiler.profileFd = data.initProfileFd;
4055 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004058 Process.setArgV0(data.processName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 android.ddm.DdmHandleAppName.setAppName(data.processName);
4060
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004061 if (data.persistent) {
4062 // Persistent processes on low-memory devices do not get to
4063 // use hardware accelerated drawing, since this can add too much
4064 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004065 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004066 HardwareRenderer.disable(false);
4067 }
4068 }
Romain Guya9582652011-11-10 14:20:10 -08004069
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004070 if (mProfiler.profileFd != null) {
4071 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004072 }
4073
Joe Onoratod630f102011-03-17 18:42:26 -07004074 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4075 // implementation to use the pool executor. Normally, we use the
4076 // serialized executor as the default. This has to happen in the
4077 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004078 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004079 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4080 }
4081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 /*
4083 * Before spawning a new process, reset the time zone to be the system time zone.
4084 * This needs to be done because the system time zone could have changed after the
4085 * the spawning of this process. Without doing this this process would have the incorrect
4086 * system time zone.
4087 */
4088 TimeZone.setDefault(null);
4089
4090 /*
4091 * Initialize the default locale in this process for the reasons we set the time zone.
4092 */
4093 Locale.setDefault(data.config.locale);
4094
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004095 /*
4096 * Update the system configuration since its preloaded and might not
4097 * reflect configuration changes. The configuration object passed
4098 * in AppBindData can be safely assumed to be up to date
4099 */
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004100 applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004101 mCurDefaultDisplayDpi = data.config.densityDpi;
4102 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004103
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004104 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105
Dianne Hackborndde331c2012-08-03 14:01:57 -07004106 /**
4107 * Switch this process to density compatibility mode if needed.
4108 */
4109 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4110 == 0) {
4111 mDensityCompatMode = true;
4112 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4113 }
4114 updateDefaultDensity();
4115
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004116 final ContextImpl appContext = new ContextImpl();
4117 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004118 if (!Process.isIsolated()) {
4119 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004120
Amith Yamasani92d57052012-08-23 10:07:52 -07004121 if (cacheDir != null) {
4122 // Provide a usable directory for temporary files
4123 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4124
4125 setupGraphicsSupport(data.info, cacheDir);
4126 } else {
4127 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4128 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004129 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004130 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004131 * For system applications on userdebug/eng builds, log stack
4132 * traces of disk and network access to dropbox for analysis.
4133 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004134 if ((data.appInfo.flags &
4135 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004136 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4137 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004138 }
4139
4140 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004141 * For apps targetting SDK Honeycomb or later, we don't allow
4142 * network usage on the main event loop / UI thread.
4143 *
4144 * Note to those grepping: this is what ultimately throws
4145 * NetworkOnMainThreadException ...
4146 */
4147 if (data.appInfo.targetSdkVersion > 9) {
4148 StrictMode.enableDeathOnNetwork();
4149 }
4150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4152 // XXX should have option to change the port.
4153 Debug.changeDebugPort(8100);
4154 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004155 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004156 + " is waiting for the debugger on port 8100...");
4157
4158 IActivityManager mgr = ActivityManagerNative.getDefault();
4159 try {
4160 mgr.showWaitingForDebugger(mAppThread, true);
4161 } catch (RemoteException ex) {
4162 }
4163
4164 Debug.waitForDebugger();
4165
4166 try {
4167 mgr.showWaitingForDebugger(mAppThread, false);
4168 } catch (RemoteException ex) {
4169 }
4170
4171 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004172 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 + " can be debugged on port 8100...");
4174 }
4175 }
4176
Siva Velusamy92a8b222012-03-09 16:24:04 -08004177 // Enable OpenGL tracing if required
4178 if (data.enableOpenGlTrace) {
4179 GLUtils.enableTracing();
4180 }
4181
Robert Greenwalt434203a2010-10-11 16:00:27 -07004182 /**
4183 * Initialize the default http proxy in this process for the reasons we set the time zone.
4184 */
4185 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004186 if (b != null) {
4187 // In pre-boot mode (doing initial launch to collect password), not
4188 // all system is up. This includes the connectivity service, so don't
4189 // crash if we can't get it.
4190 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4191 try {
4192 ProxyProperties proxyProperties = service.getProxy();
4193 Proxy.setHttpProxySystemProperty(proxyProperties);
4194 } catch (RemoteException e) {}
4195 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 InstrumentationInfo ii = null;
4199 try {
4200 ii = appContext.getPackageManager().
4201 getInstrumentationInfo(data.instrumentationName, 0);
4202 } catch (PackageManager.NameNotFoundException e) {
4203 }
4204 if (ii == null) {
4205 throw new RuntimeException(
4206 "Unable to find instrumentation info for: "
4207 + data.instrumentationName);
4208 }
4209
4210 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004211 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 mInstrumentationAppPackage = ii.packageName;
4213 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004214 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215
4216 ApplicationInfo instrApp = new ApplicationInfo();
4217 instrApp.packageName = ii.packageName;
4218 instrApp.sourceDir = ii.sourceDir;
4219 instrApp.publicSourceDir = ii.publicSourceDir;
4220 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004221 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004222 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004224 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 instrContext.init(pi, null, this);
4226
4227 try {
4228 java.lang.ClassLoader cl = instrContext.getClassLoader();
4229 mInstrumentation = (Instrumentation)
4230 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4231 } catch (Exception e) {
4232 throw new RuntimeException(
4233 "Unable to instantiate instrumentation "
4234 + data.instrumentationName + ": " + e.toString(), e);
4235 }
4236
4237 mInstrumentation.init(this, instrContext, appContext,
4238 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher);
4239
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004240 if (mProfiler.profileFile != null && !ii.handleProfiling
4241 && mProfiler.profileFd == null) {
4242 mProfiler.handlingProfiling = true;
4243 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004244 file.getParentFile().mkdirs();
4245 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4246 }
4247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004248 } else {
4249 mInstrumentation = new Instrumentation();
4250 }
4251
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004252 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004253 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004254 }
4255
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004256 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004257 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004258 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004259 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004261 // If the app is being launched for full backup or restore, bring it up in
4262 // a restricted environment with the base application class.
4263 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4264 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004265
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004266 // don't bring up providers in restricted mode; they may depend on the
4267 // app's custom Application class
4268 if (!data.restrictedBackupMode) {
4269 List<ProviderInfo> providers = data.providers;
4270 if (providers != null) {
4271 installContentProviders(app, providers);
4272 // For process that contains content providers, we want to
4273 // ensure that the JIT is enabled "at some point".
4274 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4275 }
4276 }
4277
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004278 // Do this after providers, since instrumentation tests generally start their
4279 // test thread at this point, and we don't want that racing.
4280 try {
4281 mInstrumentation.onCreate(data.instrumentationArgs);
4282 }
4283 catch (Exception e) {
4284 throw new RuntimeException(
4285 "Exception thrown in onCreate() of "
4286 + data.instrumentationName + ": " + e.toString(), e);
4287 }
4288
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004289 try {
4290 mInstrumentation.callApplicationOnCreate(app);
4291 } catch (Exception e) {
4292 if (!mInstrumentation.onException(app, e)) {
4293 throw new RuntimeException(
4294 "Unable to create application " + app.getClass().getName()
4295 + ": " + e.toString(), e);
4296 }
4297 }
4298 } finally {
4299 StrictMode.setThreadPolicy(savedPolicy);
4300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302
4303 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4304 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004305 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4306 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 Debug.stopMethodTracing();
4308 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004309 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004310 // + ", app thr: " + mAppThread);
4311 try {
4312 am.finishInstrumentation(mAppThread, resultCode, results);
4313 } catch (RemoteException ex) {
4314 }
4315 }
4316
Romain Guy65b345f2011-07-27 18:51:50 -07004317 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 Context context, List<ProviderInfo> providers) {
4319 final ArrayList<IActivityManager.ContentProviderHolder> results =
4320 new ArrayList<IActivityManager.ContentProviderHolder>();
4321
Romain Guya998dff2012-03-23 18:58:36 -07004322 for (ProviderInfo cpi : providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004323 StringBuilder buf = new StringBuilder(128);
Dianne Hackborncef65ee2010-09-30 18:27:22 -07004324 buf.append("Pub ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 buf.append(cpi.authority);
4326 buf.append(": ");
4327 buf.append(cpi.name);
4328 Log.i(TAG, buf.toString());
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004329 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4330 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4331 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004332 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 }
4335 }
4336
4337 try {
4338 ActivityManagerNative.getDefault().publishContentProviders(
4339 getApplicationThread(), results);
4340 } catch (RemoteException ex) {
4341 }
4342 }
4343
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004344 public final IContentProvider acquireProvider(Context c, String name, boolean stable) {
4345 IContentProvider provider = acquireExistingProvider(c, name, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004346 if (provider != null) {
4347 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 }
4349
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004350 // There is a possible race here. Another thread may try to acquire
4351 // the same provider at the same time. When this happens, we want to ensure
4352 // that the first one wins.
4353 // Note that we cannot hold the lock while acquiring and installing the
4354 // provider since it might take a long time to run and it could also potentially
4355 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004356 IActivityManager.ContentProviderHolder holder = null;
4357 try {
4358 holder = ActivityManagerNative.getDefault().getContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004359 getApplicationThread(), name, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004360 } catch (RemoteException ex) {
4361 }
4362 if (holder == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004363 Slog.e(TAG, "Failed to find provider info for " + name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004364 return null;
4365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004367 // Install provider will increment the reference count for us, and break
4368 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004369 holder = installProvider(c, holder, holder.info,
4370 true /*noisy*/, holder.noReleaseNeeded, stable);
4371 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004372 }
4373
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004374 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4375 if (stable) {
4376 prc.stableCount += 1;
4377 if (prc.stableCount == 1) {
4378 // We are acquiring a new stable reference on the provider.
4379 int unstableDelta;
4380 if (prc.removePending) {
4381 // We have a pending remove operation, which is holding the
4382 // last unstable reference. At this point we are converting
4383 // that unstable reference to our new stable reference.
4384 unstableDelta = -1;
4385 // Cancel the removal of the provider.
4386 if (DEBUG_PROVIDER) {
4387 Slog.v(TAG, "incProviderRef: stable "
4388 + "snatched provider from the jaws of death");
4389 }
4390 prc.removePending = false;
4391 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4392 } else {
4393 unstableDelta = 0;
4394 }
4395 try {
4396 if (DEBUG_PROVIDER) {
4397 Slog.v(TAG, "incProviderRef Now stable - "
4398 + prc.holder.info.name + ": unstableDelta="
4399 + unstableDelta);
4400 }
4401 ActivityManagerNative.getDefault().refContentProvider(
4402 prc.holder.connection, 1, unstableDelta);
4403 } catch (RemoteException e) {
4404 //do nothing content provider object is dead any way
4405 }
4406 }
4407 } else {
4408 prc.unstableCount += 1;
4409 if (prc.unstableCount == 1) {
4410 // We are acquiring a new unstable reference on the provider.
4411 if (prc.removePending) {
4412 // Oh look, we actually have a remove pending for the
4413 // provider, which is still holding the last unstable
4414 // reference. We just need to cancel that to take new
4415 // ownership of the reference.
4416 if (DEBUG_PROVIDER) {
4417 Slog.v(TAG, "incProviderRef: unstable "
4418 + "snatched provider from the jaws of death");
4419 }
4420 prc.removePending = false;
4421 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4422 } else {
4423 // First unstable ref, increment our count in the
4424 // activity manager.
4425 try {
4426 if (DEBUG_PROVIDER) {
4427 Slog.v(TAG, "incProviderRef: Now unstable - "
4428 + prc.holder.info.name);
4429 }
4430 ActivityManagerNative.getDefault().refContentProvider(
4431 prc.holder.connection, 0, 1);
4432 } catch (RemoteException e) {
4433 //do nothing content provider object is dead any way
4434 }
4435 }
4436 }
4437 }
4438 }
4439
4440 public final IContentProvider acquireExistingProvider(Context c, String name,
4441 boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004442 synchronized (mProviderMap) {
4443 ProviderClientRecord pr = mProviderMap.get(name);
4444 if (pr == null) {
4445 return null;
4446 }
4447
4448 IContentProvider provider = pr.mProvider;
4449 IBinder jBinder = provider.asBinder();
4450
4451 // Only increment the ref count if we have one. If we don't then the
4452 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004453 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004454 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004455 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004456 }
4457 return provider;
4458 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004459 }
4460
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004461 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4462 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 return false;
4464 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004467 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004469 if (prc == null) {
4470 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004471 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004472 }
4473
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004474 boolean lastRef = false;
4475 if (stable) {
4476 if (prc.stableCount == 0) {
4477 if (DEBUG_PROVIDER) Slog.v(TAG,
4478 "releaseProvider: stable ref count already 0, how?");
4479 return false;
4480 }
4481 prc.stableCount -= 1;
4482 if (prc.stableCount == 0) {
4483 // What we do at this point depends on whether there are
4484 // any unstable refs left: if there are, we just tell the
4485 // activity manager to decrement its stable count; if there
4486 // aren't, we need to enqueue this provider to be removed,
4487 // and convert to holding a single unstable ref while
4488 // doing so.
4489 lastRef = prc.unstableCount == 0;
4490 try {
4491 if (DEBUG_PROVIDER) {
4492 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4493 + lastRef + " - " + prc.holder.info.name);
4494 }
4495 ActivityManagerNative.getDefault().refContentProvider(
4496 prc.holder.connection, -1, lastRef ? 1 : 0);
4497 } catch (RemoteException e) {
4498 //do nothing content provider object is dead any way
4499 }
4500 }
4501 } else {
4502 if (prc.unstableCount == 0) {
4503 if (DEBUG_PROVIDER) Slog.v(TAG,
4504 "releaseProvider: unstable ref count already 0, how?");
4505 return false;
4506 }
4507 prc.unstableCount -= 1;
4508 if (prc.unstableCount == 0) {
4509 // If this is the last reference, we need to enqueue
4510 // this provider to be removed instead of telling the
4511 // activity manager to remove it at this point.
4512 lastRef = prc.stableCount == 0;
4513 if (!lastRef) {
4514 try {
4515 if (DEBUG_PROVIDER) {
4516 Slog.v(TAG, "releaseProvider: No longer unstable - "
4517 + prc.holder.info.name);
4518 }
4519 ActivityManagerNative.getDefault().refContentProvider(
4520 prc.holder.connection, 0, -1);
4521 } catch (RemoteException e) {
4522 //do nothing content provider object is dead any way
4523 }
4524 }
4525 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004526 }
4527
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004528 if (lastRef) {
4529 if (!prc.removePending) {
4530 // Schedule the actual remove asynchronously, since we don't know the context
4531 // this will be called in.
4532 // TODO: it would be nice to post a delayed message, so
4533 // if we come back and need the same provider quickly
4534 // we will still have it available.
4535 if (DEBUG_PROVIDER) {
4536 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4537 + prc.holder.info.name);
4538 }
4539 prc.removePending = true;
4540 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4541 mH.sendMessage(msg);
4542 } else {
4543 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4544 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004545 }
4546 return true;
4547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 }
4549
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004550 final void completeRemoveProvider(ProviderRefCount prc) {
4551 synchronized (mProviderMap) {
4552 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004553 // There was a race! Some other client managed to acquire
4554 // the provider before the removal was completed.
4555 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004556 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004557 + "provider still in use");
4558 return;
4559 }
4560
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004561 final IBinder jBinder = prc.holder.provider.asBinder();
4562 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4563 if (existingPrc == prc) {
4564 mProviderRefCountMap.remove(jBinder);
4565 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004566
4567 Iterator<ProviderClientRecord> iter = mProviderMap.values().iterator();
4568 while (iter.hasNext()) {
4569 ProviderClientRecord pr = iter.next();
4570 IBinder myBinder = pr.mProvider.asBinder();
4571 if (myBinder == jBinder) {
4572 iter.remove();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004573 }
4574 }
4575 }
4576
4577 try {
4578 if (DEBUG_PROVIDER) {
4579 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4580 + "removeContentProvider(" + prc.holder.info.name + ")");
4581 }
4582 ActivityManagerNative.getDefault().removeContentProvider(
4583 prc.holder.connection, false);
4584 } catch (RemoteException e) {
4585 //do nothing content provider object is dead any way
4586 }
4587 }
4588
4589 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
4590 synchronized(mProviderMap) {
4591 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4592 if (prc != null) {
4593 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4594 + provider + " " + prc.holder.info.name);
4595 mProviderRefCountMap.remove(provider);
4596 if (prc.client != null && prc.client.mNames != null) {
4597 for (String name : prc.client.mNames) {
4598 ProviderClientRecord pr = mProviderMap.get(name);
4599 if (pr != null && pr.mProvider.asBinder() == provider) {
4600 Slog.i(TAG, "Removing dead content provider: " + name);
4601 mProviderMap.remove(name);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004602 }
4603 }
4604 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 if (fromClient) {
4606 // We found out about this due to execution in our client
4607 // code. Tell the activity manager about it now, to ensure
4608 // that the next time we go to do anything with the provider
4609 // it knows it is dead (so we don't race with its death
4610 // notification).
4611 try {
4612 ActivityManagerNative.getDefault().unstableProviderDied(
4613 prc.holder.connection);
4614 } catch (RemoteException e) {
4615 //do nothing content provider object is dead any way
4616 }
4617 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004618 }
4619 }
4620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004622 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
4623 ContentProvider localProvider,IActivityManager.ContentProviderHolder holder) {
4624 String names[] = PATTERN_SEMICOLON.split(holder.info.authority);
4625 ProviderClientRecord pcr = new ProviderClientRecord(names, provider,
4626 localProvider, holder);
4627 for (int i = 0; i < names.length; i++) {
4628 ProviderClientRecord existing = mProviderMap.get(names[i]);
4629 if (existing != null) {
4630 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
4631 + " already published as " + names[i]);
4632 } else {
4633 mProviderMap.put(names[i], pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004634 }
4635 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004636 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 }
4638
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004639 /**
4640 * Installs the provider.
4641 *
4642 * Providers that are local to the process or that come from the system server
4643 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4644 * Other remote providers are reference counted. The initial reference count
4645 * for all reference counted providers is one. Providers that are not reference
4646 * counted do not have a reference count (at all).
4647 *
4648 * This method detects when a provider has already been installed. When this happens,
4649 * it increments the reference count of the existing provider (if appropriate)
4650 * and returns the existing provider. This can happen due to concurrent
4651 * attempts to acquire the same provider.
4652 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004653 private IActivityManager.ContentProviderHolder installProvider(Context context,
4654 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4655 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004657 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004658 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004659 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004660 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 + info.name);
4662 }
4663 Context c = null;
4664 ApplicationInfo ai = info.applicationInfo;
4665 if (context.getPackageName().equals(ai.packageName)) {
4666 c = context;
4667 } else if (mInitialApplication != null &&
4668 mInitialApplication.getPackageName().equals(ai.packageName)) {
4669 c = mInitialApplication;
4670 } else {
4671 try {
4672 c = context.createPackageContext(ai.packageName,
4673 Context.CONTEXT_INCLUDE_CODE);
4674 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004675 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 }
4677 }
4678 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004679 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004680 ai.packageName +
4681 " while loading content provider " +
4682 info.name);
4683 return null;
4684 }
4685 try {
4686 final java.lang.ClassLoader cl = c.getClassLoader();
4687 localProvider = (ContentProvider)cl.
4688 loadClass(info.name).newInstance();
4689 provider = localProvider.getIContentProvider();
4690 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004691 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 info.name + " from sourceDir " +
4693 info.applicationInfo.sourceDir);
4694 return null;
4695 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004696 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004697 TAG, "Instantiating local provider " + info.name);
4698 // XXX Need to create the correct context for this provider.
4699 localProvider.attachInfo(c, info);
4700 } catch (java.lang.Exception e) {
4701 if (!mInstrumentation.onException(null, e)) {
4702 throw new RuntimeException(
4703 "Unable to get provider " + info.name
4704 + ": " + e.toString(), e);
4705 }
4706 return null;
4707 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004708 } else {
4709 provider = holder.provider;
4710 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 + info.name);
4712 }
4713
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004714 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004715
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004716 synchronized (mProviderMap) {
4717 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4718 + " / " + info.name);
4719 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004720 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004721 ComponentName cname = new ComponentName(info.packageName, info.name);
4722 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004723 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004724 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004725 Slog.v(TAG, "installProvider: lost the race, "
4726 + "using existing local provider");
4727 }
4728 provider = pr.mProvider;
4729 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004730 holder = new IActivityManager.ContentProviderHolder(info);
4731 holder.provider = provider;
4732 holder.noReleaseNeeded = true;
4733 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004734 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004735 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004736 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004737 retHolder = pr.mHolder;
4738 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004739 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4740 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004741 if (DEBUG_PROVIDER) {
4742 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004743 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004744 // We need to transfer our new reference to the existing
4745 // ref count, releasing the old one... but only if
4746 // release is needed (that is, it is not running in the
4747 // system process).
4748 if (!noReleaseNeeded) {
4749 incProviderRefLocked(prc, stable);
4750 try {
4751 ActivityManagerNative.getDefault().removeContentProvider(
4752 holder.connection, stable);
4753 } catch (RemoteException e) {
4754 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004755 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004756 }
4757 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004758 ProviderClientRecord client = installProviderAuthoritiesLocked(
4759 provider, localProvider, holder);
4760 if (noReleaseNeeded) {
4761 prc = new ProviderRefCount(holder, client, 1000, 1000);
4762 } else {
4763 prc = stable
4764 ? new ProviderRefCount(holder, client, 1, 0)
4765 : new ProviderRefCount(holder, client, 0, 1);
4766 }
4767 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004768 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004769 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 }
4771 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004772
4773 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004774 }
4775
Romain Guy65b345f2011-07-27 18:51:50 -07004776 private void attach(boolean system) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 sThreadLocal.set(this);
4778 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004780 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004781 public void run() {
4782 ensureJitEnabled();
4783 }
4784 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004785 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>");
4786 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4787 IActivityManager mgr = ActivityManagerNative.getDefault();
4788 try {
4789 mgr.attachApplication(mAppThread);
4790 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004791 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004792 }
4793 } else {
4794 // Don't set application object here -- if the system crashes,
4795 // we can't display an alert, we just want to die die die.
4796 android.ddm.DdmHandleAppName.setAppName("system_process");
4797 try {
4798 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004799 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800 context.init(getSystemContext().mPackageInfo, null, this);
4801 Application app = Instrumentation.newApplication(Application.class, context);
4802 mAllApplications.add(app);
4803 mInitialApplication = app;
4804 app.onCreate();
4805 } catch (Exception e) {
4806 throw new RuntimeException(
4807 "Unable to instantiate Application():" + e.toString(), e);
4808 }
4809 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004810
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004811 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004812 public void onConfigurationChanged(Configuration newConfig) {
4813 synchronized (mPackages) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004814 // We need to apply this change to the resources
4815 // immediately, because upon returning the view
4816 // hierarchy will be informed about it.
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004817 if (applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004818 // This actually changed the resources! Tell
4819 // everyone about it.
4820 if (mPendingConfiguration == null ||
4821 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4822 mPendingConfiguration = newConfig;
4823
4824 queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
4825 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004826 }
4827 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004828 }
4829 public void onLowMemory() {
4830 }
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004831 public void onTrimMemory(int level) {
4832 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004833 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 }
4835
Romain Guy5e9120d2012-01-30 12:17:22 -08004836 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004837 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004838 ActivityThread thread = new ActivityThread();
4839 thread.attach(true);
4840 return thread;
4841 }
4842
Jeff Brown10e89712011-07-08 18:52:57 -07004843 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004845 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004846 }
4847 }
4848
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004849 public int getIntCoreSetting(String key, int defaultValue) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004850 synchronized (mPackages) {
4851 if (mCoreSettings != null) {
4852 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004853 } else {
4854 return defaultValue;
4855 }
4856 }
4857 }
4858
Romain Guy65b345f2011-07-27 18:51:50 -07004859 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004860 SamplingProfilerIntegration.start();
4861
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08004862 // CloseGuard defaults to true and can be quite spammy. We
4863 // disable it here, but selectively enable it later (via
4864 // StrictMode) on debug builds, but using DropBox, not logs.
4865 CloseGuard.setEnabled(false);
4866
Kenny Roote29df162012-08-10 08:28:37 -07004867 Security.addProvider(new AndroidKeyStoreProvider());
4868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 Process.setArgV0("<pre-initialized>");
4870
4871 Looper.prepareMainLooper();
4872
4873 ActivityThread thread = new ActivityThread();
4874 thread.attach(false);
4875
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07004876 if (sMainThreadHandler == null) {
4877 sMainThreadHandler = thread.getHandler();
4878 }
4879
Romain Guy5e9120d2012-01-30 12:17:22 -08004880 AsyncTask.init();
4881
Dianne Hackborn287952c2010-09-22 22:34:31 -07004882 if (false) {
4883 Looper.myLooper().setMessageLogging(new
4884 LogPrinter(Log.DEBUG, "ActivityThread"));
4885 }
4886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887 Looper.loop();
4888
Jeff Brown10e89712011-07-08 18:52:57 -07004889 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 }
4891}