blob: e667bad08077aeb9af67780e4488afd803d64a17 [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;
Christopher Tate346acb12012-10-15 19:20:25 -070032import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070034import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.ProviderInfo;
36import android.content.pm.ServiceInfo;
37import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070038import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.res.Configuration;
40import android.content.res.Resources;
41import android.database.sqlite.SQLiteDatabase;
42import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080043import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Bitmap;
45import android.graphics.Canvas;
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;
Geremy Condrab7faaf42012-09-19 18:07:42 -070055import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070056import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.os.Handler;
58import android.os.IBinder;
Adam Powellcfbe9be2013-11-06 14:58:58 -080059import android.os.IRemoteCallback;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Looper;
61import android.os.Message;
62import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070063import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Process;
65import android.os.RemoteException;
66import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070067import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070069import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070070import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070071import android.os.UserHandle;
Adam Powellcfbe9be2013-11-06 14:58:58 -080072import android.transition.Scene;
73import android.transition.TransitionManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070075import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.DisplayMetrics;
77import android.util.EventLog;
78import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070079import android.util.LogPrinter;
Adam Powellcfbe9be2013-11-06 14:58:58 -080080import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080081import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080082import android.util.Slog;
Adam Powell14874662013-07-18 19:42:41 -070083import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070085import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.View;
87import android.view.ViewDebug;
88import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070089import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.Window;
91import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070092import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080093import android.renderscript.RenderScript;
Kenny Root8b514752013-02-04 09:35:16 -080094import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
96import com.android.internal.os.BinderInternal;
97import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070098import com.android.internal.os.SamplingProfilerIntegration;
Dianne Hackborn8c841092013-06-24 13:46:13 -070099import com.android.internal.util.FastPrintWriter;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700100import com.android.internal.util.Objects;
Kenny Root12e75222013-04-23 22:34:24 -0700101import com.android.org.conscrypt.OpenSSLSocketImpl;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700102import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104import java.io.File;
105import java.io.FileDescriptor;
106import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700107import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import java.io.PrintWriter;
109import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700110import java.net.InetAddress;
Kenny Root8b514752013-02-04 09:35:16 -0800111import java.security.Security;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import java.util.List;
114import java.util.Locale;
115import java.util.Map;
116import java.util.TimeZone;
117import java.util.regex.Pattern;
118
Geremy Condrab7faaf42012-09-19 18:07:42 -0700119import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700120import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800121import libcore.io.IoUtils;
122
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800123import dalvik.system.CloseGuard;
Bob Leee5408332009-09-04 18:31:17 -0700124
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700125final class RemoteServiceException extends AndroidRuntimeException {
126 public RemoteServiceException(String msg) {
127 super(msg);
128 }
129}
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131/**
132 * This manages the execution of the main thread in an
133 * application process, scheduling and executing activities,
134 * broadcasts, and other operations on it as the activity
135 * manager requests.
136 *
137 * {@hide}
138 */
139public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700140 /** @hide */
141 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700142 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700143 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700144 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700145 /** @hide */
146 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700147 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700148 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700149 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800150 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700151 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700152 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
154 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
155 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
156 private static final int LOG_ON_PAUSE_CALLED = 30021;
157 private static final int LOG_ON_RESUME_CALLED = 30022;
158
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700159 static ContextImpl mSystemContext = null;
Bob Leee5408332009-09-04 18:31:17 -0700160
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700161 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700163 final ApplicationThread mAppThread = new ApplicationThread();
164 final Looper mLooper = Looper.myLooper();
165 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700166 final ArrayMap<IBinder, ActivityClientRecord> mActivities
167 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700168 // List of new activities (via ActivityRecord.nextIdle) that should
169 // be reported when next we idle.
170 ActivityClientRecord mNewActivities = null;
171 // Number of activities that are currently visible on-screen.
172 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700173 final ArrayMap<IBinder, Service> mServices
174 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700175 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700176 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700177 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700178 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700179 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700180 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700181 Application mInitialApplication;
182 final ArrayList<Application> mAllApplications
183 = new ArrayList<Application>();
184 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700185 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800186 /** Reference to singleton {@link ActivityThread} */
187 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700188 Instrumentation mInstrumentation;
189 String mInstrumentationAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700190 String mInstrumentationAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700191 String mInstrumentationAppPackage = null;
192 String mInstrumentedAppDir = null;
Brian Carlstromd893a892012-04-01 21:30:26 -0700193 String mInstrumentedAppLibraryDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 boolean mSystemThread = false;
195 boolean mJitEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700197 // These can be accessed by multiple threads; mPackages is the lock.
198 // XXX For now we keep around information about all packages we have
199 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800200 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700201 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800202 // which means this lock gets held while the activity and window managers
203 // holds their own lock. Thus you MUST NEVER call back into the activity manager
204 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700205 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
206 = new ArrayMap<String, WeakReference<LoadedApk>>();
207 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
208 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 final ArrayList<ActivityClientRecord> mRelaunchingActivities
210 = new ArrayList<ActivityClientRecord>();
211 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Craig Mautner88c05892013-06-28 09:47:45 -0700213 private final ResourcesManager mResourcesManager;
214
Jeff Sharkey6d515712012-09-20 16:06:08 -0700215 private static final class ProviderKey {
216 final String authority;
217 final int userId;
218
219 public ProviderKey(String authority, int userId) {
220 this.authority = authority;
221 this.userId = userId;
222 }
223
224 @Override
225 public boolean equals(Object o) {
226 if (o instanceof ProviderKey) {
227 final ProviderKey other = (ProviderKey) o;
228 return Objects.equal(authority, other.authority) && userId == other.userId;
229 }
230 return false;
231 }
232
233 @Override
234 public int hashCode() {
235 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
236 }
237 }
238
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700240 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
241 = new ArrayMap<ProviderKey, ProviderClientRecord>();
242 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
243 = new ArrayMap<IBinder, ProviderRefCount>();
244 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
245 = new ArrayMap<IBinder, ProviderClientRecord>();
246 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
247 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700249 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
250 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600251
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700252 final GcIdler mGcIdler = new GcIdler();
253 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700255 static Handler sMainThreadHandler; // set once in main()
256
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800257 Bundle mCoreSettings = null;
258
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400259 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700261 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 Intent intent;
263 Bundle state;
264 Activity activity;
265 Window window;
266 Activity parent;
267 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700268 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 boolean paused;
270 boolean stopped;
271 boolean hideForNow;
272 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700273 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700274 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700276 String profileFile;
277 ParcelFileDescriptor profileFd;
278 boolean autoStopProfiler;
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400281 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700282 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
284 List<ResultInfo> pendingResults;
285 List<Intent> pendingIntents;
286
287 boolean startsNotResumed;
288 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800289 int pendingConfigChanges;
290 boolean onlyLocalRequest;
Adam Powellcfbe9be2013-11-06 14:58:58 -0800291 Bundle activityOptions;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800292
293 View mPendingRemoveWindow;
294 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700296 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 parent = null;
298 embeddedID = null;
299 paused = false;
300 stopped = false;
301 hideForNow = false;
302 nextIdle = null;
303 }
304
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800305 public boolean isPreHoneycomb() {
306 if (activity != null) {
307 return activity.getApplicationInfo().targetSdkVersion
308 < android.os.Build.VERSION_CODES.HONEYCOMB;
309 }
310 return false;
311 }
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700314 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 return "ActivityRecord{"
316 + Integer.toHexString(System.identityHashCode(this))
317 + " token=" + token + " " + (componentName == null
318 ? "no component name" : componentName.toShortString())
319 + "}";
320 }
321 }
322
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700323 final class ProviderClientRecord {
324 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 final IContentProvider mProvider;
326 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700327 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700329 ProviderClientRecord(String[] names, IContentProvider provider,
330 ContentProvider localProvider,
331 IActivityManager.ContentProviderHolder holder) {
332 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 mProvider = provider;
334 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700335 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
337 }
338
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400339 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 List<Intent> intents;
341 IBinder token;
342 public String toString() {
343 return "NewIntentData{intents=" + intents + " token=" + token + "}";
344 }
345 }
346
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400347 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700348 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700349 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
350 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
351 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700352 this.intent = intent;
353 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 Intent intent;
356 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400357 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 public String toString() {
359 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700360 info.packageName + " resultCode=" + getResultCode()
361 + " resultData=" + getResultData() + " resultExtras="
362 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
364 }
365
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400366 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700367 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400368 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700369 int backupMode;
370 public String toString() {
371 return "CreateBackupAgentData{appInfo=" + appInfo
372 + " backupAgent=" + appInfo.backupAgentName
373 + " mode=" + backupMode + "}";
374 }
375 }
Bob Leee5408332009-09-04 18:31:17 -0700376
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 IBinder token;
379 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400380 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 Intent intent;
382 public String toString() {
383 return "CreateServiceData{token=" + token + " className="
384 + info.name + " packageName=" + info.packageName
385 + " intent=" + intent + "}";
386 }
387 }
388
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400389 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 IBinder token;
391 Intent intent;
392 boolean rebind;
393 public String toString() {
394 return "BindServiceData{token=" + token + " intent=" + intent + "}";
395 }
396 }
397
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400398 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700400 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700402 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 Intent args;
404 public String toString() {
405 return "ServiceArgsData{token=" + token + " startId=" + startId
406 + " args=" + args + "}";
407 }
408 }
409
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400410 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700411 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 String processName;
413 ApplicationInfo appInfo;
414 List<ProviderInfo> providers;
415 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 Bundle instrumentationArgs;
417 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800418 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800420 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700421 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700422 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400424 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700425
426 /** Initial values for {@link Profiler}. */
427 String initProfileFile;
428 ParcelFileDescriptor initProfileFd;
429 boolean initAutoStopProfiler;
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 public String toString() {
432 return "AppBindData{appInfo=" + appInfo + "}";
433 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700434 }
435
436 static final class Profiler {
437 String profileFile;
438 ParcelFileDescriptor profileFd;
439 boolean autoStopProfiler;
440 boolean profiling;
441 boolean handlingProfiling;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700442 public void setProfiler(String file, ParcelFileDescriptor fd) {
443 if (profiling) {
444 if (fd != null) {
445 try {
446 fd.close();
447 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700448 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700449 }
450 }
451 return;
452 }
453 if (profileFd != null) {
454 try {
455 profileFd.close();
456 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700457 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700458 }
459 }
460 profileFile = file;
461 profileFd = fd;
462 }
463 public void startProfiling() {
464 if (profileFd == null || profiling) {
465 return;
466 }
467 try {
468 Debug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
469 8 * 1024 * 1024, 0);
470 profiling = true;
471 } catch (RuntimeException e) {
472 Slog.w(TAG, "Profiling failed on path " + profileFile);
473 try {
474 profileFd.close();
475 profileFd = null;
476 } catch (IOException e2) {
477 Slog.w(TAG, "Failure closing profile fd", e2);
478 }
479 }
480 }
481 public void stopProfiling() {
482 if (profiling) {
483 profiling = false;
484 Debug.stopMethodTracing();
485 if (profileFd != null) {
486 try {
487 profileFd.close();
488 } catch (IOException e) {
489 }
490 }
491 profileFd = null;
492 profileFile = null;
493 }
494 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400497 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700498 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700499 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800500 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 }
503
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400504 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 IBinder token;
506 List<ResultInfo> results;
507 public String toString() {
508 return "ResultData{token=" + token + " results" + results + "}";
509 }
510 }
511
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400512 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800513 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 String what;
515 String who;
516 }
517
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400518 static final class ProfilerControlData {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700519 String path;
520 ParcelFileDescriptor fd;
521 }
522
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400523 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700524 String path;
525 ParcelFileDescriptor fd;
526 }
527
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400528 static final class UpdateCompatibilityData {
529 String pkg;
530 CompatibilityInfo info;
531 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800532
Adam Skorydfc7fd72013-08-05 19:23:41 -0700533 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800534 IBinder activityToken;
535 IBinder requestToken;
536 int requestType;
537 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700538
Romain Guy65b345f2011-07-27 18:51:50 -0700539 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700540
Romain Guy65b345f2011-07-27 18:51:50 -0700541 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700542 private static final String ONE_COUNT_COLUMN = "%21s %8d";
543 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700544 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700545
Dianne Hackborna413dc02013-07-12 12:02:55 -0700546 private int mLastProcessState = -1;
547
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700548 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700549 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700550 if (mPendingConfiguration == null ||
551 mPendingConfiguration.isOtherSeqNewer(config)) {
552 mPendingConfiguration = config;
553 }
554 }
555 }
556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 public final void schedulePauseActivity(IBinder token, boolean finished,
558 boolean userLeaving, int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700559 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
561 token,
562 (userLeaving ? 1 : 0),
563 configChanges);
564 }
565
566 public final void scheduleStopActivity(IBinder token, boolean showWindow,
567 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700568 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
570 token, 0, configChanges);
571 }
572
573 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700574 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
576 token);
577 }
578
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800579 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700580 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800581 }
582
Dianne Hackborna413dc02013-07-12 12:02:55 -0700583 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800584 boolean isForward, Bundle resumeArgs) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700585 updateProcessState(processState, false);
Adam Powellcfbe9be2013-11-06 14:58:58 -0800586 sendMessage(H.RESUME_ACTIVITY, new Pair<IBinder, Bundle>(token, resumeArgs),
587 isForward ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
589
590 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
591 ResultData res = new ResultData();
592 res.token = token;
593 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700594 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
597 // we use token to identify this activity without having to send the
598 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700599 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700600 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700601 int procState, Bundle state, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700602 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800603 String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler,
604 Bundle resumeArgs) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700605
606 updateProcessState(procState, false);
607
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700608 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700611 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 r.intent = intent;
613 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400614 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 r.state = state;
616
617 r.pendingResults = pendingResults;
618 r.pendingIntents = pendingNewIntents;
619
620 r.startsNotResumed = notResumed;
621 r.isForward = isForward;
622
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700623 r.profileFile = profileName;
624 r.profileFd = profileFd;
625 r.autoStopProfiler = autoStopProfiler;
Adam Powellcfbe9be2013-11-06 14:58:58 -0800626 r.activityOptions = resumeArgs;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700627
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700628 updatePendingConfiguration(curConfig);
629
Jeff Brown9ef09972013-10-15 20:49:59 -0700630 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
633 public final void scheduleRelaunchActivity(IBinder token,
634 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800635 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800636 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
637 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 }
639
640 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
641 NewIntentData data = new NewIntentData();
642 data.intents = intents;
643 data.token = token;
644
Jeff Brown9ef09972013-10-15 20:49:59 -0700645 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 }
647
648 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
649 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700650 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651 configChanges);
652 }
653
654 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400655 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700656 boolean sync, int sendingUser, int processState) {
657 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700658 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700659 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400661 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700662 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400665 public final void scheduleCreateBackupAgent(ApplicationInfo app,
666 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700667 CreateBackupAgentData d = new CreateBackupAgentData();
668 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400669 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700670 d.backupMode = backupMode;
671
Jeff Brown9ef09972013-10-15 20:49:59 -0700672 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700673 }
674
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400675 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
676 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700677 CreateBackupAgentData d = new CreateBackupAgentData();
678 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400679 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700680
Jeff Brown9ef09972013-10-15 20:49:59 -0700681 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700682 }
683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700685 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
686 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 CreateServiceData s = new CreateServiceData();
688 s.token = token;
689 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400690 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691
Jeff Brown9ef09972013-10-15 20:49:59 -0700692 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694
695 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700696 boolean rebind, int processState) {
697 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 BindServiceData s = new BindServiceData();
699 s.token = token;
700 s.intent = intent;
701 s.rebind = rebind;
702
Amith Yamasani742a6712011-05-04 14:49:28 -0700703 if (DEBUG_SERVICE)
704 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
705 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700706 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
709 public final void scheduleUnbindService(IBinder token, Intent intent) {
710 BindServiceData s = new BindServiceData();
711 s.token = token;
712 s.intent = intent;
713
Jeff Brown9ef09972013-10-15 20:49:59 -0700714 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700717 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700718 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 ServiceArgsData s = new ServiceArgsData();
720 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700721 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700723 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 s.args = args;
725
Jeff Brown9ef09972013-10-15 20:49:59 -0700726 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728
729 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700730 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 public final void bindApplication(String processName,
734 ApplicationInfo appInfo, List<ProviderInfo> providers,
735 ComponentName instrumentationName, String profileFile,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700736 ParcelFileDescriptor profileFd, boolean autoStopProfiler,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 Bundle instrumentationArgs, IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800738 IUiAutomationConnection instrumentationUiConnection, int debugMode,
739 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
740 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
741 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
743 if (services != null) {
744 // Setup the service cache in the ServiceManager
745 ServiceManager.initServiceCache(services);
746 }
747
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800748 setCoreSettings(coreSettings);
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 AppBindData data = new AppBindData();
751 data.processName = processName;
752 data.appInfo = appInfo;
753 data.providers = providers;
754 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 data.instrumentationArgs = instrumentationArgs;
756 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800757 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800759 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700760 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700761 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400763 data.compatInfo = compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700764 data.initProfileFile = profileFile;
765 data.initProfileFd = profileFd;
766 data.initAutoStopProfiler = false;
Jeff Brown9ef09972013-10-15 20:49:59 -0700767 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700771 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
Christopher Tate5e1ab332009-09-01 20:32:49 -0700774 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700775 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700776 }
777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 public void requestThumbnail(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700779 sendMessage(H.REQUEST_THUMBNAIL, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 }
781
782 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700783 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700784 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
787 public void updateTimeZone() {
788 TimeZone.setDefault(null);
789 }
790
Robert Greenwalt03595d02010-11-02 14:08:23 -0700791 public void clearDnsCache() {
792 // a non-standard API to get this to libcore
793 InetAddress.clearDnsCache();
794 }
795
Jason Monk602b2322013-07-03 17:04:33 -0400796 public void setHttpProxy(String host, String port, String exclList, String pacFileUrl) {
797 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700798 }
799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 public void processInBackground() {
801 mH.removeMessages(H.GC_WHEN_IDLE);
802 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
803 }
804
805 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700806 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700807 try {
808 data.fd = ParcelFileDescriptor.dup(fd);
809 data.token = servicetoken;
810 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700811 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700812 } catch (IOException e) {
813 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814 }
815 }
816
817 // This function exists to make sure all receiver dispatching is
818 // correctly ordered, since these are one-way calls and the binder driver
819 // applies transaction ordering per object for such calls.
820 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700821 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700822 boolean sticky, int sendingUser, int processState) throws RemoteException {
823 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700824 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
825 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
Bob Leee5408332009-09-04 18:31:17 -0700827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700829 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831
832 public void scheduleActivityConfigurationChanged(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700833 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 }
835
Romain Guy7eabe552011-07-21 14:56:34 -0700836 public void profilerControl(boolean start, String path, ParcelFileDescriptor fd,
837 int profileType) {
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700838 ProfilerControlData pcd = new ProfilerControlData();
839 pcd.path = path;
840 pcd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700841 sendMessage(H.PROFILER_CONTROL, pcd, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800842 }
843
Andy McFadden824c5102010-07-09 16:26:57 -0700844 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
845 DumpHeapData dhd = new DumpHeapData();
846 dhd.path = path;
847 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700848 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700849 }
850
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700851 public void setSchedulingGroup(int group) {
852 // Note: do this immediately, since going into the foreground
853 // should happen regardless of what pending work we have to do
854 // and the activity manager will wait for us to report back that
855 // we are done before sending us to the background.
856 try {
857 Process.setProcessGroup(Process.myPid(), group);
858 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800859 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700860 }
861 }
Bob Leee5408332009-09-04 18:31:17 -0700862
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700863 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700864 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700865 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700866
867 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700868 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700869 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700870
Dianne Hackborn30d71892010-12-11 10:37:55 -0800871 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
872 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700873 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700874 try {
875 data.fd = ParcelFileDescriptor.dup(fd);
876 data.token = activitytoken;
877 data.prefix = prefix;
878 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700879 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700880 } catch (IOException e) {
881 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700882 }
883 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700884
Marco Nelissen18cb2872011-11-15 11:19:53 -0800885 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
886 String[] args) {
887 DumpComponentInfo data = new DumpComponentInfo();
888 try {
889 data.fd = ParcelFileDescriptor.dup(fd);
890 data.token = providertoken;
891 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700892 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800893 } catch (IOException e) {
894 Slog.w(TAG, "dumpProvider failed", e);
895 }
896 }
897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700899 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700900 boolean dumpFullInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700901 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700902 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700903 try {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700904 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700905 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700906 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700907 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700908 }
909
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700910 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700911 boolean dumpFullInfo, boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 long nativeMax = Debug.getNativeHeapSize() / 1024;
913 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
914 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 Runtime runtime = Runtime.getRuntime();
917
918 long dalvikMax = runtime.totalMemory() / 1024;
919 long dalvikFree = runtime.freeMemory() / 1024;
920 long dalvikAllocated = dalvikMax - dalvikFree;
921 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700922 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700923 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
924 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 int globalAssetCount = AssetManager.getGlobalAssetCount();
926 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
927 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
928 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
929 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700930 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800931 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700932
Dianne Hackborne77187d2013-10-25 16:32:41 -0700933 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, Process.myPid(),
934 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
935 nativeMax, nativeAllocated, nativeFree,
936 dalvikMax, dalvikAllocated, dalvikFree);
937
Dianne Hackbornb437e092011-08-05 17:50:29 -0700938 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 // NOTE: if you change anything significant below, also consider changing
940 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // Object counts
943 pw.print(viewInstanceCount); pw.print(',');
944 pw.print(viewRootInstanceCount); pw.print(',');
945 pw.print(appContextInstanceCount); pw.print(',');
946 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 pw.print(globalAssetCount); pw.print(',');
949 pw.print(globalAssetManagerCount); pw.print(',');
950 pw.print(binderLocalObjectCount); pw.print(',');
951 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 pw.print(binderDeathObjectCount); pw.print(',');
954 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800957 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800958 pw.print(stats.memoryUsed / 1024); pw.print(',');
959 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700960 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800961 for (int i = 0; i < stats.dbStats.size(); i++) {
962 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700963 pw.print(','); pw.print(dbStats.dbName);
964 pw.print(','); pw.print(dbStats.pageSize);
965 pw.print(','); pw.print(dbStats.dbSize);
966 pw.print(','); pw.print(dbStats.lookaside);
967 pw.print(','); pw.print(dbStats.cache);
968 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -0800969 }
Dianne Hackbornb437e092011-08-05 17:50:29 -0700970 pw.println();
Bob Leee5408332009-09-04 18:31:17 -0700971
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700972 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
Bob Leee5408332009-09-04 18:31:17 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 pw.println(" ");
976 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -0700977 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 viewRootInstanceCount);
979
980 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
981 "Activities:", activityInstanceCount);
982
983 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
984 "AssetManagers:", globalAssetManagerCount);
985
986 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
987 "Proxy Binders:", binderProxyObjectCount);
988 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
989
990 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -0700991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 // SQLite mem info
993 pw.println(" ");
994 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -0800995 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
996 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
997 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800998 pw.println(" ");
999 int N = stats.dbStats.size();
1000 if (N > 0) {
1001 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001002 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1003 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001004 for (int i = 0; i < N; i++) {
1005 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001006 printRow(pw, DB_INFO_FORMAT,
1007 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1008 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1009 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1010 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001011 }
1012 }
Bob Leee5408332009-09-04 18:31:17 -07001013
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001014 // Asset details.
1015 String assetAlloc = AssetManager.getAssetAllocations();
1016 if (assetAlloc != null) {
1017 pw.println(" ");
1018 pw.println(" Asset Allocations");
1019 pw.print(assetAlloc);
1020 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001021 }
1022
1023 @Override
1024 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1025 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001026 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028
Jeff Brown6754ba22011-12-14 20:20:01 -08001029 @Override
1030 public void dumpDbInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001031 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001032 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1033 SQLiteDebug.dump(printer, args);
1034 pw.flush();
1035 }
1036
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001037 @Override
1038 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001039 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001040 }
1041
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001042 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001043 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001044 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001045 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001046 cmd.activityToken = activityToken;
1047 cmd.requestToken = requestToken;
1048 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001049 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001050 }
1051
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001052 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001053 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001054 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001055
1056 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1057 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1058 ucd.pkg = pkg;
1059 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001060 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001061 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001062
1063 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001064 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001065 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001066
Craig Mautner5eda9b32013-07-02 11:58:16 -07001067 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001068 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001069 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001070
1071 public void setProcessState(int state) {
1072 updateProcessState(state, true);
1073 }
1074
1075 public void updateProcessState(int processState, boolean fromIpc) {
1076 synchronized (this) {
1077 if (mLastProcessState != processState) {
1078 mLastProcessState = processState;
1079
1080 // Update Dalvik state here based on ActivityManager.PROCESS_STATE_* constants.
1081 if (false) {
1082 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1083 + (fromIpc ? " (from ipc": ""));
1084 }
1085 }
1086 }
1087 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001088
1089 @Override
1090 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001091 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 }
1094
Romain Guy65b345f2011-07-27 18:51:50 -07001095 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 public static final int LAUNCH_ACTIVITY = 100;
1097 public static final int PAUSE_ACTIVITY = 101;
1098 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1099 public static final int STOP_ACTIVITY_SHOW = 103;
1100 public static final int STOP_ACTIVITY_HIDE = 104;
1101 public static final int SHOW_WINDOW = 105;
1102 public static final int HIDE_WINDOW = 106;
1103 public static final int RESUME_ACTIVITY = 107;
1104 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001105 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001106 public static final int BIND_APPLICATION = 110;
1107 public static final int EXIT_APPLICATION = 111;
1108 public static final int NEW_INTENT = 112;
1109 public static final int RECEIVER = 113;
1110 public static final int CREATE_SERVICE = 114;
1111 public static final int SERVICE_ARGS = 115;
1112 public static final int STOP_SERVICE = 116;
1113 public static final int REQUEST_THUMBNAIL = 117;
1114 public static final int CONFIGURATION_CHANGED = 118;
1115 public static final int CLEAN_UP_CONTEXT = 119;
1116 public static final int GC_WHEN_IDLE = 120;
1117 public static final int BIND_SERVICE = 121;
1118 public static final int UNBIND_SERVICE = 122;
1119 public static final int DUMP_SERVICE = 123;
1120 public static final int LOW_MEMORY = 124;
1121 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1122 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001123 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001124 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001125 public static final int DESTROY_BACKUP_AGENT = 129;
1126 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001127 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001128 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001129 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001130 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001131 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001132 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001133 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001134 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001135 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001136 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001137 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001138 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001139 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001140 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001141 public static final int INSTALL_PROVIDER = 145;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001143 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 switch (code) {
1145 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1146 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1147 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1148 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1149 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1150 case SHOW_WINDOW: return "SHOW_WINDOW";
1151 case HIDE_WINDOW: return "HIDE_WINDOW";
1152 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1153 case SEND_RESULT: return "SEND_RESULT";
1154 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1155 case BIND_APPLICATION: return "BIND_APPLICATION";
1156 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1157 case NEW_INTENT: return "NEW_INTENT";
1158 case RECEIVER: return "RECEIVER";
1159 case CREATE_SERVICE: return "CREATE_SERVICE";
1160 case SERVICE_ARGS: return "SERVICE_ARGS";
1161 case STOP_SERVICE: return "STOP_SERVICE";
1162 case REQUEST_THUMBNAIL: return "REQUEST_THUMBNAIL";
1163 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1164 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1165 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1166 case BIND_SERVICE: return "BIND_SERVICE";
1167 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1168 case DUMP_SERVICE: return "DUMP_SERVICE";
1169 case LOW_MEMORY: return "LOW_MEMORY";
1170 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1171 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001172 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001173 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1174 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001175 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001176 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001177 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001178 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001179 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001180 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001181 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001182 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001183 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001184 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001185 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001186 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001187 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001188 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001189 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001190 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 }
1192 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001193 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001196 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 switch (msg.what) {
1198 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001199 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001200 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201
1202 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001203 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001204 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001205 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 } break;
1207 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001208 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001209 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001210 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001211 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 } break;
1213 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001214 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 handlePauseActivity((IBinder)msg.obj, false, msg.arg1 != 0, msg.arg2);
Bob Leee5408332009-09-04 18:31:17 -07001216 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001217 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 break;
1219 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001220 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 handlePauseActivity((IBinder)msg.obj, true, msg.arg1 != 0, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001222 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 break;
1224 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001225 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001227 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 break;
1229 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001230 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001232 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 break;
1234 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001235 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001237 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 break;
1239 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001240 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001242 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 break;
1244 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001245 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001246 final Pair<IBinder, Bundle> resumeArgs = (Pair<IBinder, Bundle>) msg.obj;
1247 handleResumeActivity(resumeArgs.first, resumeArgs.second, true,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07001248 msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001249 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 break;
1251 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001252 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001254 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 break;
1256 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001257 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1259 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001260 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 break;
1262 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001263 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 AppBindData data = (AppBindData)msg.obj;
1265 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001266 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 break;
1268 case EXIT_APPLICATION:
1269 if (mInitialApplication != null) {
1270 mInitialApplication.onTerminate();
1271 }
1272 Looper.myLooper().quit();
1273 break;
1274 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001275 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001277 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 break;
1279 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001280 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001282 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001283 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 break;
1285 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001286 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 break;
1290 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001291 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001293 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 break;
1295 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001301 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 break;
1305 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001308 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 break;
1311 case REQUEST_THUMBNAIL:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001312 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "requestThumbnail");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 handleRequestThumbnail((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001314 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 break;
1316 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001317 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001318 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001319 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001320 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 break;
1322 case CLEAN_UP_CONTEXT:
1323 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1324 cci.context.performFinalCleanup(cci.who, cci.what);
1325 break;
1326 case GC_WHEN_IDLE:
1327 scheduleGcIdler();
1328 break;
1329 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001330 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331 break;
1332 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001333 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001334 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001335 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 break;
1337 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001338 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001342 case PROFILER_CONTROL:
Romain Guy7eabe552011-07-21 14:56:34 -07001343 handleProfilerControl(msg.arg1 != 0, (ProfilerControlData)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001344 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001345 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001347 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001348 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001349 break;
1350 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001351 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001352 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001353 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001354 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001355 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001356 Process.killProcess(Process.myPid());
1357 break;
1358 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001359 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001360 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001361 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001362 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001363 case ENABLE_JIT:
1364 ensureJitEnabled();
1365 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001366 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001367 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001368 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001369 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001370 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001371 case SCHEDULE_CRASH:
1372 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001373 case DUMP_HEAP:
1374 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1375 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001376 case DUMP_ACTIVITY:
1377 handleDumpActivity((DumpComponentInfo)msg.obj);
1378 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001379 case DUMP_PROVIDER:
1380 handleDumpProvider((DumpComponentInfo)msg.obj);
1381 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001382 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001384 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001386 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001387 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001388 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001389 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001390 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001391 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001392 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1393 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001394 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001395 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001396 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001397 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001398 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001399 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001400 case UNSTABLE_PROVIDER_DIED:
1401 handleUnstableProviderDied((IBinder)msg.obj, false);
1402 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001403 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1404 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001405 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001406 case TRANSLUCENT_CONVERSION_COMPLETE:
1407 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1408 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001409 case INSTALL_PROVIDER:
1410 handleInstallProvider((ProviderInfo) msg.obj);
1411 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001413 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 }
Bob Leee5408332009-09-04 18:31:17 -07001415
Brian Carlstromed7e0072011-03-24 13:27:57 -07001416 private void maybeSnapshot() {
1417 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001418 // convert the *private* ActivityThread.PackageInfo to *public* known
1419 // android.content.pm.PackageInfo
1420 String packageName = mBoundApplication.info.mPackageName;
1421 android.content.pm.PackageInfo packageInfo = null;
1422 try {
1423 Context context = getSystemContext();
1424 if(context == null) {
1425 Log.e(TAG, "cannot get a valid context");
1426 return;
1427 }
1428 PackageManager pm = context.getPackageManager();
1429 if(pm == null) {
1430 Log.e(TAG, "cannot get a valid PackageManager");
1431 return;
1432 }
1433 packageInfo = pm.getPackageInfo(
1434 packageName, PackageManager.GET_ACTIVITIES);
1435 } catch (NameNotFoundException e) {
1436 Log.e(TAG, "cannot get package info for " + packageName, e);
1437 }
1438 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001439 }
1440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 }
1442
Romain Guy65b345f2011-07-27 18:51:50 -07001443 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001444 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001446 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001447 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001448 if (mBoundApplication != null && mProfiler.profileFd != null
1449 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001450 stopProfiling = true;
1451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 if (a != null) {
1453 mNewActivities = null;
1454 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001455 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001457 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 TAG, "Reporting idle of " + a +
1459 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001460 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 if (a.activity != null && !a.activity.mFinished) {
1462 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001463 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001464 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001466 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 }
1468 }
1469 prev = a;
1470 a = a.nextIdle;
1471 prev.nextIdle = null;
1472 } while (a != null);
1473 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001474 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001475 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001476 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001477 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 return false;
1479 }
1480 }
1481
1482 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001483 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 public final boolean queueIdle() {
1485 doGcIfNeeded();
1486 return false;
1487 }
1488 }
1489
Romain Guy65b345f2011-07-27 18:51:50 -07001490 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001491 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493
Romain Guy65b345f2011-07-27 18:51:50 -07001494 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001495 ActivityThread am = currentActivityThread();
1496 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001497 ? am.mBoundApplication.appInfo.packageName : null;
1498 }
1499
1500 public static String currentProcessName() {
1501 ActivityThread am = currentActivityThread();
1502 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001503 ? am.mBoundApplication.processName : null;
1504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505
Romain Guy65b345f2011-07-27 18:51:50 -07001506 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001507 ActivityThread am = currentActivityThread();
1508 return am != null ? am.mInitialApplication : null;
1509 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001511 public static IPackageManager getPackageManager() {
1512 if (sPackageManager != null) {
1513 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1514 return sPackageManager;
1515 }
1516 IBinder b = ServiceManager.getService("package");
1517 //Slog.v("PackageManager", "default service binder = " + b);
1518 sPackageManager = IPackageManager.Stub.asInterface(b);
1519 //Slog.v("PackageManager", "default service = " + sPackageManager);
1520 return sPackageManager;
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
Romain Guy65b345f2011-07-27 18:51:50 -07001523 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001524 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1525 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001526 if (config == null) {
1527 return null;
1528 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001529 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001530 mMainThreadConfig.setTo(config);
1531 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001532 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001533 }
1534 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001538 * Creates the top level resources for the given package.
1539 */
Jeff Browna492c3a2012-08-23 19:48:44 -07001540 Resources getTopLevelResources(String resDir,
1541 int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001542 LoadedApk pkgInfo) {
Craig Mautner88c05892013-06-28 09:47:45 -07001543 return mResourcesManager.getTopLevelResources(resDir, displayId, overrideConfiguration,
Craig Mautner48d0d182013-06-11 07:53:06 -07001544 pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001545 }
1546
1547 final Handler getHandler() {
1548 return mH;
1549 }
1550
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001551 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1552 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001553 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1554 }
1555
1556 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1557 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001558 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001559 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1561 ref = mPackages.get(packageName);
1562 } else {
1563 ref = mResourcePackages.get(packageName);
1564 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001565 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001566 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001567 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1568 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (packageInfo != null && (packageInfo.mResources == null
1570 || packageInfo.mResources.getAssets().isUpToDate())) {
1571 if (packageInfo.isSecurityViolation()
1572 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1573 throw new SecurityException(
1574 "Requesting code from " + packageName
1575 + " to be run in process "
1576 + mBoundApplication.processName
1577 + "/" + mBoundApplication.appInfo.uid);
1578 }
1579 return packageInfo;
1580 }
1581 }
1582
1583 ApplicationInfo ai = null;
1584 try {
1585 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001586 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001588 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
1590
1591 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001592 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
1595 return null;
1596 }
1597
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001598 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1599 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1601 boolean securityViolation = includeCode && ai.uid != 0
1602 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001603 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001604 : true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1606 |Context.CONTEXT_IGNORE_SECURITY))
1607 == Context.CONTEXT_INCLUDE_CODE) {
1608 if (securityViolation) {
1609 String msg = "Requesting code from " + ai.packageName
1610 + " (with uid " + ai.uid + ")";
1611 if (mBoundApplication != null) {
1612 msg = msg + " to be run in process "
1613 + mBoundApplication.processName + " (with uid "
1614 + mBoundApplication.appInfo.uid + ")";
1615 }
1616 throw new SecurityException(msg);
1617 }
1618 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001619 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 }
1621
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001622 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1623 CompatibilityInfo compatInfo) {
1624 return getPackageInfo(ai, compatInfo, null, false, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 }
1626
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001627 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001628 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001629 WeakReference<LoadedApk> ref;
1630 if (includeCode) {
1631 ref = mPackages.get(packageName);
1632 } else {
1633 ref = mResourcePackages.get(packageName);
1634 }
1635 return ref != null ? ref.get() : null;
1636 }
1637 }
1638
Romain Guy65b345f2011-07-27 18:51:50 -07001639 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 ClassLoader baseLoader, boolean securityViolation, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001641 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001642 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001643 if (includeCode) {
1644 ref = mPackages.get(aInfo.packageName);
1645 } else {
1646 ref = mResourcePackages.get(aInfo.packageName);
1647 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001648 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 if (packageInfo == null || (packageInfo.mResources != null
1650 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001651 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 : "Loading resource-only package ") + aInfo.packageName
1653 + " (in " + (mBoundApplication != null
1654 ? mBoundApplication.processName : null)
1655 + ")");
1656 packageInfo =
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001657 new LoadedApk(this, aInfo, compatInfo, this, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 securityViolation, includeCode &&
1659 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0);
1660 if (includeCode) {
1661 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001662 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 } else {
1664 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001665 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 }
1667 }
1668 return packageInfo;
1669 }
1670 }
1671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001673 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 }
1675
1676 public ApplicationThread getApplicationThread()
1677 {
1678 return mAppThread;
1679 }
1680
1681 public Instrumentation getInstrumentation()
1682 {
1683 return mInstrumentation;
1684 }
1685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001687 return mProfiler != null && mProfiler.profileFile != null
1688 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690
1691 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001692 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 }
1694
1695 public Looper getLooper() {
1696 return mLooper;
1697 }
1698
1699 public Application getApplication() {
1700 return mInitialApplication;
1701 }
Bob Leee5408332009-09-04 18:31:17 -07001702
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001703 public String getProcessName() {
1704 return mBoundApplication.processName;
1705 }
Bob Leee5408332009-09-04 18:31:17 -07001706
Dianne Hackborn21556372010-02-04 16:34:40 -08001707 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 synchronized (this) {
1709 if (mSystemContext == null) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001710 ContextImpl context =
1711 ContextImpl.createSystemContext(this);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001712 LoadedApk info = new LoadedApk(this, "android", context, null,
1713 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 context.init(info, null, this);
Craig Mautner88c05892013-06-28 09:47:45 -07001715 context.getResources().updateConfiguration(mResourcesManager.getConfiguration(),
1716 mResourcesManager.getDisplayMetricsLocked(Display.DEFAULT_DISPLAY));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 mSystemContext = context;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001718 //Slog.i(TAG, "Created system resources " + context.getResources()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 // + ": " + context.getResources().getConfiguration());
1720 }
1721 }
1722 return mSystemContext;
1723 }
1724
Mike Cleron432b7132009-09-24 15:28:29 -07001725 public void installSystemApplicationInfo(ApplicationInfo info) {
1726 synchronized (this) {
Dianne Hackborn21556372010-02-04 16:34:40 -08001727 ContextImpl context = getSystemContext();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001728 context.init(new LoadedApk(this, "android", context, info,
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07001729 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO), null, this);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001730
1731 // give ourselves a default profiler
1732 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001733 }
1734 }
1735
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001736 void ensureJitEnabled() {
1737 if (!mJitEnabled) {
1738 mJitEnabled = true;
1739 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1740 }
1741 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 void scheduleGcIdler() {
1744 if (!mGcIdlerScheduled) {
1745 mGcIdlerScheduled = true;
1746 Looper.myQueue().addIdleHandler(mGcIdler);
1747 }
1748 mH.removeMessages(H.GC_WHEN_IDLE);
1749 }
1750
1751 void unscheduleGcIdler() {
1752 if (mGcIdlerScheduled) {
1753 mGcIdlerScheduled = false;
1754 Looper.myQueue().removeIdleHandler(mGcIdler);
1755 }
1756 mH.removeMessages(H.GC_WHEN_IDLE);
1757 }
1758
1759 void doGcIfNeeded() {
1760 mGcIdlerScheduled = false;
1761 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001762 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 // + "m now=" + now);
1764 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001765 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 BinderInternal.forceGc("bg");
1767 }
1768 }
1769
Dianne Hackborne77187d2013-10-25 16:32:41 -07001770 private static final String HEAP_FULL_COLUMN
1771 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1772 private static final String HEAP_COLUMN
1773 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1774
1775 // Formatting for checkin service - update version if row format changes
1776 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1777
1778 static void printRow(PrintWriter pw, String format, Object...objs) {
1779 pw.println(String.format(format, objs));
1780 }
1781
1782 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1783 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1784 long nativeMax, long nativeAllocated, long nativeFree,
1785 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1786
1787 // For checkin, we print one long comma-separated list of values
1788 if (checkin) {
1789 // NOTE: if you change anything significant below, also consider changing
1790 // ACTIVITY_THREAD_CHECKIN_VERSION.
1791
1792 // Header
1793 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1794 pw.print(pid); pw.print(',');
1795 pw.print(processName); pw.print(',');
1796
1797 // Heap info - max
1798 pw.print(nativeMax); pw.print(',');
1799 pw.print(dalvikMax); pw.print(',');
1800 pw.print("N/A,");
1801 pw.print(nativeMax + dalvikMax); pw.print(',');
1802
1803 // Heap info - allocated
1804 pw.print(nativeAllocated); pw.print(',');
1805 pw.print(dalvikAllocated); pw.print(',');
1806 pw.print("N/A,");
1807 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1808
1809 // Heap info - free
1810 pw.print(nativeFree); pw.print(',');
1811 pw.print(dalvikFree); pw.print(',');
1812 pw.print("N/A,");
1813 pw.print(nativeFree + dalvikFree); pw.print(',');
1814
1815 // Heap info - proportional set size
1816 pw.print(memInfo.nativePss); pw.print(',');
1817 pw.print(memInfo.dalvikPss); pw.print(',');
1818 pw.print(memInfo.otherPss); pw.print(',');
1819 pw.print(memInfo.getTotalPss()); pw.print(',');
1820
1821 // Heap info - swappable set size
1822 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1823 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1824 pw.print(memInfo.otherSwappablePss); pw.print(',');
1825 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1826
1827 // Heap info - shared dirty
1828 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1829 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1830 pw.print(memInfo.otherSharedDirty); pw.print(',');
1831 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1832
1833 // Heap info - shared clean
1834 pw.print(memInfo.nativeSharedClean); pw.print(',');
1835 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1836 pw.print(memInfo.otherSharedClean); pw.print(',');
1837 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1838
1839 // Heap info - private Dirty
1840 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1841 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1842 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1843 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1844
1845 // Heap info - private Clean
1846 pw.print(memInfo.nativePrivateClean); pw.print(',');
1847 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1848 pw.print(memInfo.otherPrivateClean); pw.print(',');
1849 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1850
1851 // Heap info - other areas
1852 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1853 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1854 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1855 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1856 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1857 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1858 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1859 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1860 }
1861 return;
1862 }
1863
1864 // otherwise, show human-readable format
1865 if (dumpFullInfo) {
1866 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1867 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
1868 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
1869 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1870 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1871 "------", "------", "------", "------", "------", "------");
1872 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1873 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1874 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1875 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1876 nativeMax, nativeAllocated, nativeFree);
1877 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1878 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1879 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1880 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1881 dalvikMax, dalvikAllocated, dalvikFree);
1882 } else {
1883 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1884 "Private", "Swapped", "Heap", "Heap", "Heap");
1885 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1886 "Clean", "Dirty", "Size", "Alloc", "Free");
1887 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1888 "------", "------", "------", "------", "------");
1889 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1890 memInfo.nativePrivateDirty,
1891 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1892 nativeMax, nativeAllocated, nativeFree);
1893 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1894 memInfo.dalvikPrivateDirty,
1895 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1896 dalvikMax, dalvikAllocated, dalvikFree);
1897 }
1898
1899 int otherPss = memInfo.otherPss;
1900 int otherSwappablePss = memInfo.otherSwappablePss;
1901 int otherSharedDirty = memInfo.otherSharedDirty;
1902 int otherPrivateDirty = memInfo.otherPrivateDirty;
1903 int otherSharedClean = memInfo.otherSharedClean;
1904 int otherPrivateClean = memInfo.otherPrivateClean;
1905 int otherSwappedOut = memInfo.otherSwappedOut;
1906
1907 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1908 final int myPss = memInfo.getOtherPss(i);
1909 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1910 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1911 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1912 final int mySharedClean = memInfo.getOtherSharedClean(i);
1913 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1914 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1915 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1916 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
1917 if (dumpFullInfo) {
1918 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1919 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1920 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
1921 } else {
1922 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1923 myPss, myPrivateDirty,
1924 myPrivateClean, mySwappedOut, "", "", "");
1925 }
1926 otherPss -= myPss;
1927 otherSwappablePss -= mySwappablePss;
1928 otherSharedDirty -= mySharedDirty;
1929 otherPrivateDirty -= myPrivateDirty;
1930 otherSharedClean -= mySharedClean;
1931 otherPrivateClean -= myPrivateClean;
1932 otherSwappedOut -= mySwappedOut;
1933 }
1934 }
1935
1936 if (dumpFullInfo) {
1937 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
1938 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
1939 otherSwappedOut, "", "", "");
1940 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
1941 memInfo.getTotalSwappablePss(),
1942 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
1943 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
1944 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
1945 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
1946 } else {
1947 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
1948 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
1949 "", "", "");
1950 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
1951 memInfo.getTotalPrivateDirty(),
1952 memInfo.getTotalPrivateClean(),
1953 memInfo.getTotalSwappedOut(),
1954 nativeMax+dalvikMax,
1955 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
1956 }
1957
1958 if (dumpDalvik) {
1959 pw.println(" ");
1960 pw.println(" Dalvik Details");
1961
1962 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
1963 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
1964 final int myPss = memInfo.getOtherPss(i);
1965 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1966 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1967 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1968 final int mySharedClean = memInfo.getOtherSharedClean(i);
1969 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1970 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1971 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1972 || mySharedClean != 0 || myPrivateClean != 0) {
1973 if (dumpFullInfo) {
1974 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1975 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1976 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
1977 } else {
1978 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1979 myPss, myPrivateDirty,
1980 myPrivateClean, mySwappedOut, "", "", "");
1981 }
1982 }
1983 }
1984 }
1985 }
1986
Jeff Hamilton52d32032011-01-08 15:31:26 -06001987 public void registerOnActivityPausedListener(Activity activity,
1988 OnActivityPausedListener listener) {
1989 synchronized (mOnPauseListeners) {
1990 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
1991 if (list == null) {
1992 list = new ArrayList<OnActivityPausedListener>();
1993 mOnPauseListeners.put(activity, list);
1994 }
1995 list.add(listener);
1996 }
1997 }
1998
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08001999 public void unregisterOnActivityPausedListener(Activity activity,
2000 OnActivityPausedListener listener) {
2001 synchronized (mOnPauseListeners) {
2002 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2003 if (list != null) {
2004 list.remove(listener);
2005 }
2006 }
2007 }
2008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 public final ActivityInfo resolveActivityInfo(Intent intent) {
2010 ActivityInfo aInfo = intent.resolveActivityInfo(
2011 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2012 if (aInfo == null) {
2013 // Throw an exception.
2014 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002015 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 }
2017 return aInfo;
2018 }
Bob Leee5408332009-09-04 18:31:17 -07002019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002022 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002023 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002025 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 r.intent = intent;
2027 r.state = state;
2028 r.parent = parent;
2029 r.embeddedID = id;
2030 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002031 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 if (localLOGV) {
2033 ComponentName compname = intent.getComponent();
2034 String name;
2035 if (compname != null) {
2036 name = compname.toShortString();
2037 } else {
2038 name = "(Intent " + intent + ").getComponent() returned null";
2039 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002040 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 + ", comp=" + name
2042 + ", token=" + token);
2043 }
Adam Powellcfbe9be2013-11-06 14:58:58 -08002044 return performLaunchActivity(r, null, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002045 }
2046
2047 public final Activity getActivity(IBinder token) {
2048 return mActivities.get(token).activity;
2049 }
2050
2051 public final void sendActivityResult(
2052 IBinder token, String id, int requestCode,
2053 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002054 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002055 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2057 list.add(new ResultInfo(id, requestCode, resultCode, data));
2058 mAppThread.scheduleSendResult(token, list);
2059 }
2060
Jeff Brown9ef09972013-10-15 20:49:59 -07002061 private void sendMessage(int what, Object obj) {
2062 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 }
2064
Jeff Brown9ef09972013-10-15 20:49:59 -07002065 private void sendMessage(int what, Object obj, int arg1) {
2066 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 }
2068
Jeff Brown9ef09972013-10-15 20:49:59 -07002069 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2070 sendMessage(what, obj, arg1, arg2, false);
2071 }
2072
2073 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2074 if (DEBUG_MESSAGES) Slog.v(
2075 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2076 + ": " + arg1 + " / " + obj);
2077 Message msg = Message.obtain();
2078 msg.what = what;
2079 msg.obj = obj;
2080 msg.arg1 = arg1;
2081 msg.arg2 = arg2;
2082 if (async) {
2083 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002085 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087
Dianne Hackborn21556372010-02-04 16:34:40 -08002088 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 String what) {
2090 ContextCleanupInfo cci = new ContextCleanupInfo();
2091 cci.context = context;
2092 cci.who = who;
2093 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002094 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002095 }
2096
Adam Powellcfbe9be2013-11-06 14:58:58 -08002097 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent,
2098 Bundle options) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2100
2101 ActivityInfo aInfo = r.activityInfo;
2102 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002103 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 Context.CONTEXT_INCLUDE_CODE);
2105 }
Bob Leee5408332009-09-04 18:31:17 -07002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 ComponentName component = r.intent.getComponent();
2108 if (component == null) {
2109 component = r.intent.resolveActivity(
2110 mInitialApplication.getPackageManager());
2111 r.intent.setComponent(component);
2112 }
2113
2114 if (r.activityInfo.targetActivity != null) {
2115 component = new ComponentName(r.activityInfo.packageName,
2116 r.activityInfo.targetActivity);
2117 }
2118
2119 Activity activity = null;
2120 try {
2121 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2122 activity = mInstrumentation.newActivity(
2123 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002124 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 r.intent.setExtrasClassLoader(cl);
2126 if (r.state != null) {
2127 r.state.setClassLoader(cl);
2128 }
2129 } catch (Exception e) {
2130 if (!mInstrumentation.onException(activity, e)) {
2131 throw new RuntimeException(
2132 "Unable to instantiate activity " + component
2133 + ": " + e.toString(), e);
2134 }
2135 }
2136
2137 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002138 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002139
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002140 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2141 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 TAG, r + ": app=" + app
2143 + ", appName=" + app.getPackageName()
2144 + ", pkg=" + r.packageInfo.getPackageName()
2145 + ", comp=" + r.intent.getComponent().toShortString()
2146 + ", dir=" + r.packageInfo.getAppDir());
2147
2148 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002149 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002151 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002152 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002153 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002154 activity.attach(appContext, this, getInstrumentation(), r.token,
2155 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002156 r.embeddedID, r.lastNonConfigurationInstances, config, options);
Bob Leee5408332009-09-04 18:31:17 -07002157
Christopher Tateb70f3df2009-04-07 16:07:59 -07002158 if (customIntent != null) {
2159 activity.mIntent = customIntent;
2160 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002161 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 activity.mStartedActivity = false;
2163 int theme = r.activityInfo.getThemeResource();
2164 if (theme != 0) {
2165 activity.setTheme(theme);
2166 }
2167
2168 activity.mCalled = false;
2169 mInstrumentation.callActivityOnCreate(activity, r.state);
2170 if (!activity.mCalled) {
2171 throw new SuperNotCalledException(
2172 "Activity " + r.intent.getComponent().toShortString() +
2173 " did not call through to super.onCreate()");
2174 }
2175 r.activity = activity;
2176 r.stopped = true;
2177 if (!r.activity.mFinished) {
2178 activity.performStart();
2179 r.stopped = false;
2180 }
2181 if (!r.activity.mFinished) {
2182 if (r.state != null) {
2183 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2184 }
2185 }
2186 if (!r.activity.mFinished) {
2187 activity.mCalled = false;
2188 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2189 if (!activity.mCalled) {
2190 throw new SuperNotCalledException(
2191 "Activity " + r.intent.getComponent().toShortString() +
2192 " did not call through to super.onPostCreate()");
2193 }
2194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002195 }
2196 r.paused = true;
2197
2198 mActivities.put(r.token, r);
2199
2200 } catch (SuperNotCalledException e) {
2201 throw e;
2202
2203 } catch (Exception e) {
2204 if (!mInstrumentation.onException(activity, e)) {
2205 throw new RuntimeException(
2206 "Unable to start activity " + component
2207 + ": " + e.toString(), e);
2208 }
2209 }
2210
2211 return activity;
2212 }
2213
Jeff Brownefd43bd2012-09-21 17:02:35 -07002214 private Context createBaseContextForActivity(ActivityClientRecord r,
2215 final Activity activity) {
2216 ContextImpl appContext = new ContextImpl();
2217 appContext.init(r.packageInfo, r.token, this);
2218 appContext.setOuterContext(activity);
2219
2220 // For debugging purposes, if the activity's package name contains the value of
2221 // the "debug.use-second-display" system property as a substring, then show
2222 // its content on a secondary display if there is one.
2223 Context baseContext = appContext;
2224 String pkgName = SystemProperties.get("debug.second-display.pkg");
2225 if (pkgName != null && !pkgName.isEmpty()
2226 && r.packageInfo.mPackageName.contains(pkgName)) {
2227 DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2228 for (int displayId : dm.getDisplayIds()) {
2229 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002230 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002231 baseContext = appContext.createDisplayContext(display);
2232 break;
2233 }
2234 }
2235 }
2236 return baseContext;
2237 }
2238
Romain Guy65b345f2011-07-27 18:51:50 -07002239 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 // If we are getting ready to gc after going to the background, well
2241 // we are back active so skip it.
2242 unscheduleGcIdler();
2243
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002244 if (r.profileFd != null) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002245 mProfiler.setProfiler(r.profileFile, r.profileFd);
2246 mProfiler.startProfiling();
2247 mProfiler.autoStopProfiler = r.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002248 }
2249
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002250 // Make sure we are running with the most recent config.
2251 handleConfigurationChanged(null, null);
2252
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002253 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002255
2256 Activity a = performLaunchActivity(r, customIntent, r.activityOptions);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257
2258 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002259 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002260 Bundle oldState = r.state;
Adam Powellcfbe9be2013-11-06 14:58:58 -08002261 handleResumeActivity(r.token, r.activityOptions, false, r.isForward,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002262 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002263
2264 if (!r.activity.mFinished && r.startsNotResumed) {
2265 // The activity manager actually wants this one to start out
2266 // paused, because it needs to be visible but isn't in the
2267 // foreground. We accomplish this by going through the
2268 // normal startup (because activities expect to go through
2269 // onResume() the first time they run, before their window
2270 // is displayed), and then pausing it. However, in this case
2271 // we do -not- need to do the full pause cycle (of freezing
2272 // and such) because the activity manager assumes it can just
2273 // retain the current state it has.
2274 try {
2275 r.activity.mCalled = false;
2276 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002277 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002278 // we need to be created again. But we only do this
2279 // for pre-Honeycomb apps, which always save their state
2280 // when pausing, so we can not have them save their state
2281 // when restarting from a paused state. For HC and later,
2282 // we want to (and can) let the state be saved as the normal
2283 // part of stopping the activity.
2284 if (r.isPreHoneycomb()) {
2285 r.state = oldState;
2286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 if (!r.activity.mCalled) {
2288 throw new SuperNotCalledException(
2289 "Activity " + r.intent.getComponent().toShortString() +
2290 " did not call through to super.onPause()");
2291 }
2292
2293 } catch (SuperNotCalledException e) {
2294 throw e;
2295
2296 } catch (Exception e) {
2297 if (!mInstrumentation.onException(r.activity, e)) {
2298 throw new RuntimeException(
2299 "Unable to pause activity "
2300 + r.intent.getComponent().toShortString()
2301 + ": " + e.toString(), e);
2302 }
2303 }
2304 r.paused = true;
2305 }
2306 } else {
2307 // If there was an error, for any reason, tell the activity
2308 // manager to stop us.
2309 try {
2310 ActivityManagerNative.getDefault()
2311 .finishActivity(r.token, Activity.RESULT_CANCELED, null);
2312 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002313 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 }
2315 }
2316 }
2317
Romain Guy65b345f2011-07-27 18:51:50 -07002318 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 List<Intent> intents) {
2320 final int N = intents.size();
2321 for (int i=0; i<N; i++) {
2322 Intent intent = intents.get(i);
2323 intent.setExtrasClassLoader(r.activity.getClassLoader());
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002324 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2326 }
2327 }
2328
2329 public final void performNewIntents(IBinder token,
2330 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002331 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 if (r != null) {
2333 final boolean resumed = !r.paused;
2334 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002335 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 mInstrumentation.callActivityOnPause(r.activity);
2337 }
2338 deliverNewIntents(r, intents);
2339 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002340 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002341 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 }
2343 }
2344 }
Bob Leee5408332009-09-04 18:31:17 -07002345
Romain Guy65b345f2011-07-27 18:51:50 -07002346 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 performNewIntents(data.token, data.intents);
2348 }
2349
Adam Skorydfc7fd72013-08-05 19:23:41 -07002350 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002351 Bundle data = new Bundle();
2352 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2353 if (r != null) {
2354 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2355 r.activity.onProvideAssistData(data);
2356 }
2357 if (data.isEmpty()) {
2358 data = null;
2359 }
2360 IActivityManager mgr = ActivityManagerNative.getDefault();
2361 try {
Adam Skory7140a252013-09-11 12:04:58 +01002362 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002363 } catch (RemoteException e) {
2364 }
2365 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002366
2367 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2368 ActivityClientRecord r = mActivities.get(token);
2369 if (r != null) {
2370 r.activity.onTranslucentConversionComplete(drawComplete);
2371 }
2372 }
2373
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002374 public void handleInstallProvider(ProviderInfo info) {
2375 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2376 }
2377
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002378 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2379
2380 /**
2381 * Return the Intent that's currently being handled by a
2382 * BroadcastReceiver on this thread, or null if none.
2383 * @hide
2384 */
2385 public static Intent getIntentBeingBroadcast() {
2386 return sCurrentBroadcastIntent.get();
2387 }
2388
Romain Guy65b345f2011-07-27 18:51:50 -07002389 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390 // If we are getting ready to gc after going to the background, well
2391 // we are back active so skip it.
2392 unscheduleGcIdler();
2393
2394 String component = data.intent.getComponent().getClassName();
2395
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002396 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002397 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398
2399 IActivityManager mgr = ActivityManagerNative.getDefault();
2400
Romain Guy65b345f2011-07-27 18:51:50 -07002401 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 try {
2403 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2404 data.intent.setExtrasClassLoader(cl);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002405 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2407 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002408 if (DEBUG_BROADCAST) Slog.i(TAG,
2409 "Finishing failed broadcast to " + data.intent.getComponent());
2410 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 throw new RuntimeException(
2412 "Unable to instantiate receiver " + component
2413 + ": " + e.toString(), e);
2414 }
2415
2416 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002417 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002418
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002419 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 TAG, "Performing receive of " + data.intent
2421 + ": app=" + app
2422 + ", appName=" + app.getPackageName()
2423 + ", pkg=" + packageInfo.getPackageName()
2424 + ", comp=" + data.intent.getComponent().toShortString()
2425 + ", dir=" + packageInfo.getAppDir());
2426
Dianne Hackborn21556372010-02-04 16:34:40 -08002427 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002428 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002429 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 receiver.onReceive(context.getReceiverRestrictedContext(),
2431 data.intent);
2432 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002433 if (DEBUG_BROADCAST) Slog.i(TAG,
2434 "Finishing failed broadcast to " + data.intent.getComponent());
2435 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 if (!mInstrumentation.onException(receiver, e)) {
2437 throw new RuntimeException(
2438 "Unable to start receiver " + component
2439 + ": " + e.toString(), e);
2440 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002441 } finally {
2442 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
Dianne Hackborne829fef2010-10-26 17:44:01 -07002445 if (receiver.getPendingResult() != null) {
2446 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 }
2449
Christopher Tate181fafa2009-05-14 11:12:14 -07002450 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002451 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002452 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002453
Christopher Tate346acb12012-10-15 19:20:25 -07002454 // Sanity check the requested target package's uid against ours
2455 try {
2456 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2457 data.appInfo.packageName, 0, UserHandle.myUserId());
2458 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2459 Slog.w(TAG, "Asked to instantiate non-matching package "
2460 + data.appInfo.packageName);
2461 return;
2462 }
2463 } catch (RemoteException e) {
2464 Slog.e(TAG, "Can't reach package manager", e);
2465 return;
2466 }
2467
Christopher Tate181fafa2009-05-14 11:12:14 -07002468 // no longer idle; we have backup work to do
2469 unscheduleGcIdler();
2470
2471 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002472 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002473 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002474 if (packageName == null) {
2475 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2476 return;
2477 }
2478
Christopher Tate181fafa2009-05-14 11:12:14 -07002479 if (mBackupAgents.get(packageName) != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002480 Slog.d(TAG, "BackupAgent " + " for " + packageName
Christopher Tate181fafa2009-05-14 11:12:14 -07002481 + " already exists");
2482 return;
2483 }
Bob Leee5408332009-09-04 18:31:17 -07002484
Christopher Tate181fafa2009-05-14 11:12:14 -07002485 BackupAgent agent = null;
2486 String classname = data.appInfo.backupAgentName;
Christopher Tate4a627c72011-04-01 14:43:32 -07002487
Christopher Tate79ec80d2011-06-24 14:58:49 -07002488 // full backup operation but no app-supplied agent? use the default implementation
2489 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2490 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002491 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002492 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002493
Christopher Tate181fafa2009-05-14 11:12:14 -07002494 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002495 IBinder binder = null;
2496 try {
Christopher Tate4a627c72011-04-01 14:43:32 -07002497 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2498
Christopher Tated1475e02009-07-09 15:36:17 -07002499 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Christopher Tate4a627c72011-04-01 14:43:32 -07002500 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tated1475e02009-07-09 15:36:17 -07002501
2502 // set up the agent's context
Dianne Hackborn21556372010-02-04 16:34:40 -08002503 ContextImpl context = new ContextImpl();
Christopher Tated1475e02009-07-09 15:36:17 -07002504 context.init(packageInfo, null, this);
2505 context.setOuterContext(agent);
2506 agent.attach(context);
2507
2508 agent.onCreate();
2509 binder = agent.onBind();
2510 mBackupAgents.put(packageName, agent);
2511 } catch (Exception e) {
2512 // If this is during restore, fail silently; otherwise go
2513 // ahead and let the user see the crash.
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002514 Slog.e(TAG, "Agent threw during creation: " + e);
Christopher Tate75a99702011-05-18 16:28:19 -07002515 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2516 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
Christopher Tated1475e02009-07-09 15:36:17 -07002517 throw e;
2518 }
2519 // falling through with 'binder' still null
2520 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002521
2522 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002523 try {
2524 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2525 } catch (RemoteException e) {
2526 // nothing to do.
2527 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002528 } catch (Exception e) {
2529 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002530 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002531 }
2532 }
2533
2534 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002535 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002536 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002537
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002538 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002539 String packageName = packageInfo.mPackageName;
2540 BackupAgent agent = mBackupAgents.get(packageName);
2541 if (agent != null) {
2542 try {
2543 agent.onDestroy();
2544 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002545 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002546 e.printStackTrace();
2547 }
2548 mBackupAgents.remove(packageName);
2549 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002550 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002551 }
2552 }
2553
Romain Guy65b345f2011-07-27 18:51:50 -07002554 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 // If we are getting ready to gc after going to the background, well
2556 // we are back active so skip it.
2557 unscheduleGcIdler();
2558
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002559 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002560 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 Service service = null;
2562 try {
2563 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2564 service = (Service) cl.loadClass(data.info.name).newInstance();
2565 } catch (Exception e) {
2566 if (!mInstrumentation.onException(service, e)) {
2567 throw new RuntimeException(
2568 "Unable to instantiate service " + data.info.name
2569 + ": " + e.toString(), e);
2570 }
2571 }
2572
2573 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002574 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575
Dianne Hackborn21556372010-02-04 16:34:40 -08002576 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 context.init(packageInfo, null, this);
2578
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002579 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 context.setOuterContext(service);
2581 service.attach(context, this, data.info.name, data.token, app,
2582 ActivityManagerNative.getDefault());
2583 service.onCreate();
2584 mServices.put(data.token, service);
2585 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002586 ActivityManagerNative.getDefault().serviceDoneExecuting(
2587 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 } catch (RemoteException e) {
2589 // nothing to do.
2590 }
2591 } catch (Exception e) {
2592 if (!mInstrumentation.onException(service, e)) {
2593 throw new RuntimeException(
2594 "Unable to create service " + data.info.name
2595 + ": " + e.toString(), e);
2596 }
2597 }
2598 }
2599
Romain Guy65b345f2011-07-27 18:51:50 -07002600 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002602 if (DEBUG_SERVICE)
2603 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 if (s != null) {
2605 try {
2606 data.intent.setExtrasClassLoader(s.getClassLoader());
2607 try {
2608 if (!data.rebind) {
2609 IBinder binder = s.onBind(data.intent);
2610 ActivityManagerNative.getDefault().publishService(
2611 data.token, data.intent, binder);
2612 } else {
2613 s.onRebind(data.intent);
2614 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002615 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002617 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 } catch (RemoteException ex) {
2619 }
2620 } catch (Exception e) {
2621 if (!mInstrumentation.onException(s, e)) {
2622 throw new RuntimeException(
2623 "Unable to bind to service " + s
2624 + " with " + data.intent + ": " + e.toString(), e);
2625 }
2626 }
2627 }
2628 }
2629
Romain Guy65b345f2011-07-27 18:51:50 -07002630 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 Service s = mServices.get(data.token);
2632 if (s != null) {
2633 try {
2634 data.intent.setExtrasClassLoader(s.getClassLoader());
2635 boolean doRebind = s.onUnbind(data.intent);
2636 try {
2637 if (doRebind) {
2638 ActivityManagerNative.getDefault().unbindFinished(
2639 data.token, data.intent, doRebind);
2640 } else {
2641 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002642 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 }
2644 } catch (RemoteException ex) {
2645 }
2646 } catch (Exception e) {
2647 if (!mInstrumentation.onException(s, e)) {
2648 throw new RuntimeException(
2649 "Unable to unbind to service " + s
2650 + " with " + data.intent + ": " + e.toString(), e);
2651 }
2652 }
2653 }
2654 }
2655
Dianne Hackborn625ac272010-09-17 18:29:22 -07002656 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002657 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2658 try {
2659 Service s = mServices.get(info.token);
2660 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002661 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2662 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002663 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2664 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002666 } finally {
2667 IoUtils.closeQuietly(info.fd);
2668 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 }
2670 }
2671
Dianne Hackborn625ac272010-09-17 18:29:22 -07002672 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002673 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2674 try {
2675 ActivityClientRecord r = mActivities.get(info.token);
2676 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002677 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2678 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002679 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2680 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002681 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002682 } finally {
2683 IoUtils.closeQuietly(info.fd);
2684 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002685 }
2686 }
2687
Marco Nelissen18cb2872011-11-15 11:19:53 -08002688 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002689 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2690 try {
2691 ProviderClientRecord r = mLocalProviders.get(info.token);
2692 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002693 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2694 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002695 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2696 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002697 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002698 } finally {
2699 IoUtils.closeQuietly(info.fd);
2700 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002701 }
2702 }
2703
Romain Guy65b345f2011-07-27 18:51:50 -07002704 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 Service s = mServices.get(data.token);
2706 if (s != null) {
2707 try {
2708 if (data.args != null) {
2709 data.args.setExtrasClassLoader(s.getClassLoader());
2710 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002711 int res;
2712 if (!data.taskRemoved) {
2713 res = s.onStartCommand(data.args, data.flags, data.startId);
2714 } else {
2715 s.onTaskRemoved(data.args);
2716 res = Service.START_TASK_REMOVED_COMPLETE;
2717 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002718
2719 QueuedWork.waitToFinish();
2720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002722 ActivityManagerNative.getDefault().serviceDoneExecuting(
2723 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724 } catch (RemoteException e) {
2725 // nothing to do.
2726 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002727 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 } catch (Exception e) {
2729 if (!mInstrumentation.onException(s, e)) {
2730 throw new RuntimeException(
2731 "Unable to start service " + s
2732 + " with " + data.args + ": " + e.toString(), e);
2733 }
2734 }
2735 }
2736 }
2737
Romain Guy65b345f2011-07-27 18:51:50 -07002738 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 Service s = mServices.remove(token);
2740 if (s != null) {
2741 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002742 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 s.onDestroy();
2744 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002745 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002747 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002749
2750 QueuedWork.waitToFinish();
2751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002753 ActivityManagerNative.getDefault().serviceDoneExecuting(
2754 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002755 } catch (RemoteException e) {
2756 // nothing to do.
2757 }
2758 } catch (Exception e) {
2759 if (!mInstrumentation.onException(s, e)) {
2760 throw new RuntimeException(
2761 "Unable to stop service " + s
2762 + ": " + e.toString(), e);
2763 }
2764 }
2765 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002766 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 }
2768
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002769 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002771 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002772 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 + " finished=" + r.activity.mFinished);
2774 if (r != null && !r.activity.mFinished) {
2775 if (clearHide) {
2776 r.hideForNow = false;
2777 r.activity.mStartedActivity = false;
2778 }
2779 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002780 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 if (r.pendingIntents != null) {
2782 deliverNewIntents(r, r.pendingIntents);
2783 r.pendingIntents = null;
2784 }
2785 if (r.pendingResults != null) {
2786 deliverResults(r, r.pendingResults);
2787 r.pendingResults = null;
2788 }
2789 r.activity.performResume();
2790
Bob Leee5408332009-09-04 18:31:17 -07002791 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002792 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 r.paused = false;
2795 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796 r.state = null;
2797 } catch (Exception e) {
2798 if (!mInstrumentation.onException(r.activity, e)) {
2799 throw new RuntimeException(
2800 "Unable to resume activity "
2801 + r.intent.getComponent().toShortString()
2802 + ": " + e.toString(), e);
2803 }
2804 }
2805 }
2806 return r;
2807 }
2808
Romain Guya998dff2012-03-23 18:58:36 -07002809 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002810 if (r.mPendingRemoveWindow != null) {
2811 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2812 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2813 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002814 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002815 r.activity.getClass().getName(), "Activity");
2816 }
2817 }
2818 r.mPendingRemoveWindow = null;
2819 r.mPendingRemoveWindowManager = null;
2820 }
2821
Adam Powellcfbe9be2013-11-06 14:58:58 -08002822 final void handleResumeActivity(IBinder token, Bundle resumeArgs,
2823 boolean clearHide, boolean isForward, boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002824 // If we are getting ready to gc after going to the background, well
2825 // we are back active so skip it.
2826 unscheduleGcIdler();
2827
Adam Powellcfbe9be2013-11-06 14:58:58 -08002828 // TODO Push resumeArgs into the activity for consideration
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002829 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830
2831 if (r != null) {
2832 final Activity a = r.activity;
2833
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002834 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 TAG, "Resume " + r + " started activity: " +
2836 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2837 + ", finished: " + a.mFinished);
2838
2839 final int forwardBit = isForward ?
2840 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 // If the window hasn't yet been added to the window manager,
2843 // and this guy didn't finish itself or start another activity,
2844 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002845 boolean willBeVisible = !a.mStartedActivity;
2846 if (!willBeVisible) {
2847 try {
2848 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
2849 a.getActivityToken());
2850 } catch (RemoteException e) {
2851 }
2852 }
2853 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 r.window = r.activity.getWindow();
2855 View decor = r.window.getDecorView();
2856 decor.setVisibility(View.INVISIBLE);
2857 ViewManager wm = a.getWindowManager();
2858 WindowManager.LayoutParams l = r.window.getAttributes();
2859 a.mDecor = decor;
2860 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
2861 l.softInputMode |= forwardBit;
2862 if (a.mVisibleFromClient) {
2863 a.mWindowAdded = true;
2864 wm.addView(decor, l);
2865 }
2866
2867 // If the window has already been added, but during resume
2868 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002869 // window visible.
2870 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002871 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 TAG, "Launch " + r + " mStartedActivity set");
2873 r.hideForNow = true;
2874 }
2875
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002876 // Get rid of anything left hanging around.
2877 cleanUpPendingRemoveWindows(r);
2878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 // The window is now visible if it has been added, we are not
2880 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002881 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002882 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002884 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002885 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002886 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07002887 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 r.newConfig = null;
2889 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002890 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891 + isForward);
2892 WindowManager.LayoutParams l = r.window.getAttributes();
2893 if ((l.softInputMode
2894 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
2895 != forwardBit) {
2896 l.softInputMode = (l.softInputMode
2897 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
2898 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07002899 if (r.activity.mVisibleFromClient) {
2900 ViewManager wm = a.getWindowManager();
2901 View decor = r.window.getDecorView();
2902 wm.updateViewLayout(decor, l);
2903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 }
2905 r.activity.mVisibleFromServer = true;
2906 mNumVisibleActivities++;
2907 if (r.activity.mVisibleFromClient) {
2908 r.activity.makeVisible();
2909 }
2910 }
2911
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002912 if (!r.onlyLocalRequest) {
2913 r.nextIdle = mNewActivities;
2914 mNewActivities = r;
2915 if (localLOGV) Slog.v(
2916 TAG, "Scheduling idle handler for " + r);
2917 Looper.myQueue().addIdleHandler(new Idler());
2918 }
2919 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002921 // Tell the activity manager we have resumed.
2922 if (reallyResume) {
2923 try {
2924 ActivityManagerNative.getDefault().activityResumed(token);
2925 } catch (RemoteException ex) {
2926 }
2927 }
2928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 } else {
2930 // If an exception was thrown when trying to resume, then
2931 // just end this activity.
2932 try {
2933 ActivityManagerNative.getDefault()
2934 .finishActivity(token, Activity.RESULT_CANCELED, null);
2935 } catch (RemoteException ex) {
2936 }
2937 }
2938 }
2939
2940 private int mThumbnailWidth = -1;
2941 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002942 private Bitmap mAvailThumbnailBitmap = null;
2943 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944
Romain Guy65b345f2011-07-27 18:51:50 -07002945 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002946 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002948 if (thumbnail == null) {
2949 int w = mThumbnailWidth;
2950 int h;
2951 if (w < 0) {
2952 Resources res = r.activity.getResources();
2953 mThumbnailHeight = h =
2954 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002956 mThumbnailWidth = w =
2957 res.getDimensionPixelSize(com.android.internal.R.dimen.thumbnail_width);
2958 } else {
2959 h = mThumbnailHeight;
2960 }
2961
2962 // On platforms where we don't want thumbnails, set dims to (0,0)
2963 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07002964 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
2965 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08002966 thumbnail.eraseColor(0);
2967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08002970 if (thumbnail != null) {
2971 Canvas cv = mThumbnailCanvas;
2972 if (cv == null) {
2973 mThumbnailCanvas = cv = new Canvas();
2974 }
2975
2976 cv.setBitmap(thumbnail);
2977 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
2978 mAvailThumbnailBitmap = thumbnail;
2979 thumbnail = null;
2980 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07002981 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07002983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 } catch (Exception e) {
2985 if (!mInstrumentation.onException(r.activity, e)) {
2986 throw new RuntimeException(
2987 "Unable to create thumbnail of "
2988 + r.intent.getComponent().toShortString()
2989 + ": " + e.toString(), e);
2990 }
2991 thumbnail = null;
2992 }
2993
2994 return thumbnail;
2995 }
2996
Romain Guy65b345f2011-07-27 18:51:50 -07002997 private void handlePauseActivity(IBinder token, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 boolean userLeaving, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002999 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003001 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003002 if (userLeaving) {
3003 performUserLeavingActivity(r);
3004 }
Bob Leee5408332009-09-04 18:31:17 -07003005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003007 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003009 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003010 if (r.isPreHoneycomb()) {
3011 QueuedWork.waitToFinish();
3012 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003013
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003014 // Tell the activity manager we have paused.
3015 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003016 ActivityManagerNative.getDefault().activityPaused(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 } catch (RemoteException ex) {
3018 }
3019 }
3020 }
3021
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003022 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 mInstrumentation.callActivityOnUserLeaving(r.activity);
3024 }
3025
3026 final Bundle performPauseActivity(IBinder token, boolean finished,
3027 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003028 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 return r != null ? performPauseActivity(r, finished, saveState) : null;
3030 }
3031
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003032 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 boolean saveState) {
3034 if (r.paused) {
3035 if (r.activity.mFinished) {
3036 // If we are finishing, we won't call onResume() in certain cases.
3037 // So here we likewise don't want to call onPause() if the activity
3038 // isn't resumed.
3039 return null;
3040 }
3041 RuntimeException e = new RuntimeException(
3042 "Performing pause of activity that is not resumed: "
3043 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003044 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003045 }
3046 Bundle state = null;
3047 if (finished) {
3048 r.activity.mFinished = true;
3049 }
3050 try {
3051 // Next have the activity save its current state and managed dialogs...
3052 if (!r.activity.mFinished && saveState) {
3053 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003054 state.setAllowFds(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3056 r.state = state;
3057 }
3058 // Now we are idle.
3059 r.activity.mCalled = false;
3060 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003061 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3062 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 if (!r.activity.mCalled) {
3064 throw new SuperNotCalledException(
3065 "Activity " + r.intent.getComponent().toShortString() +
3066 " did not call through to super.onPause()");
3067 }
3068
3069 } catch (SuperNotCalledException e) {
3070 throw e;
3071
3072 } catch (Exception e) {
3073 if (!mInstrumentation.onException(r.activity, e)) {
3074 throw new RuntimeException(
3075 "Unable to pause activity "
3076 + r.intent.getComponent().toShortString()
3077 + ": " + e.toString(), e);
3078 }
3079 }
3080 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003081
3082 // Notify any outstanding on paused listeners
3083 ArrayList<OnActivityPausedListener> listeners;
3084 synchronized (mOnPauseListeners) {
3085 listeners = mOnPauseListeners.remove(r.activity);
3086 }
3087 int size = (listeners != null ? listeners.size() : 0);
3088 for (int i = 0; i < size; i++) {
3089 listeners.get(i).onPaused(r.activity);
3090 }
3091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 return state;
3093 }
3094
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003095 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003096 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003097 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003098 }
3099
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003100 private static class StopInfo implements Runnable {
3101 ActivityClientRecord activity;
3102 Bundle state;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 Bitmap thumbnail;
3104 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003105
3106 @Override public void run() {
3107 // Tell activity manager we have been stopped.
3108 try {
3109 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3110 ActivityManagerNative.getDefault().activityStopped(
3111 activity.token, state, thumbnail, description);
3112 } catch (RemoteException ex) {
3113 }
3114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 }
3116
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003117 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003118 public final IActivityManager.ContentProviderHolder holder;
3119 public final ProviderClientRecord client;
3120 public int stableCount;
3121 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003122
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003123 // When this is set, the stable and unstable ref counts are 0 and
3124 // we have a pending operation scheduled to remove the ref count
3125 // from the activity manager. On the activity manager we are still
3126 // holding an unstable ref, though it is not reflected in the counts
3127 // here.
3128 public boolean removePending;
3129
3130 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3131 ProviderClientRecord inClient, int sCount, int uCount) {
3132 holder = inHolder;
3133 client = inClient;
3134 stableCount = sCount;
3135 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 }
3137 }
3138
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003139 /**
3140 * Core implementation of stopping an activity. Note this is a little
3141 * tricky because the server's meaning of stop is slightly different
3142 * than our client -- for the server, stop means to save state and give
3143 * it the result when it is done, but the window may still be visible.
3144 * For the client, we want to call onStop()/onStart() to indicate when
3145 * the activity's UI visibillity changes.
3146 */
Romain Guy65b345f2011-07-27 18:51:50 -07003147 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003148 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003149 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003150 Bundle state = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 if (r != null) {
3152 if (!keepShown && r.stopped) {
3153 if (r.activity.mFinished) {
3154 // If we are finishing, we won't call onResume() in certain
3155 // cases. So here we likewise don't want to call onStop()
3156 // if the activity isn't resumed.
3157 return;
3158 }
3159 RuntimeException e = new RuntimeException(
3160 "Performing stop of activity that is not resumed: "
3161 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003162 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 }
3164
3165 if (info != null) {
3166 try {
3167 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003168 // For now, don't create the thumbnail here; we are
3169 // doing that by doing a screen snapshot.
3170 info.thumbnail = null; //createThumbnailBitmap(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 info.description = r.activity.onCreateDescription();
3172 } catch (Exception e) {
3173 if (!mInstrumentation.onException(r.activity, e)) {
3174 throw new RuntimeException(
3175 "Unable to save state of activity "
3176 + r.intent.getComponent().toShortString()
3177 + ": " + e.toString(), e);
3178 }
3179 }
3180 }
3181
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003182 // Next have the activity save its current state and managed dialogs...
3183 if (!r.activity.mFinished && saveState) {
3184 if (r.state == null) {
3185 state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003186 state.setAllowFds(false);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003187 mInstrumentation.callActivityOnSaveInstanceState(r.activity, state);
3188 r.state = state;
3189 } else {
3190 state = r.state;
3191 }
3192 }
3193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 if (!keepShown) {
3195 try {
3196 // Now we are idle.
3197 r.activity.performStop();
3198 } catch (Exception e) {
3199 if (!mInstrumentation.onException(r.activity, e)) {
3200 throw new RuntimeException(
3201 "Unable to stop activity "
3202 + r.intent.getComponent().toShortString()
3203 + ": " + e.toString(), e);
3204 }
3205 }
3206 r.stopped = true;
3207 }
3208
3209 r.paused = true;
3210 }
3211 }
3212
Romain Guy65b345f2011-07-27 18:51:50 -07003213 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 View v = r.activity.mDecor;
3215 if (v != null) {
3216 if (show) {
3217 if (!r.activity.mVisibleFromServer) {
3218 r.activity.mVisibleFromServer = true;
3219 mNumVisibleActivities++;
3220 if (r.activity.mVisibleFromClient) {
3221 r.activity.makeVisible();
3222 }
3223 }
3224 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003225 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003226 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003228 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 r.newConfig = null;
3230 }
3231 } else {
3232 if (r.activity.mVisibleFromServer) {
3233 r.activity.mVisibleFromServer = false;
3234 mNumVisibleActivities--;
3235 v.setVisibility(View.INVISIBLE);
3236 }
3237 }
3238 }
3239 }
3240
Romain Guy65b345f2011-07-27 18:51:50 -07003241 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003242 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 r.activity.mConfigChangeFlags |= configChanges;
3244
3245 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003246 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003248 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 TAG, "Finishing stop of " + r + ": show=" + show
3250 + " win=" + r.window);
3251
3252 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003253
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003254 // Make sure any pending writes are now committed.
3255 if (!r.isPreHoneycomb()) {
3256 QueuedWork.waitToFinish();
3257 }
3258
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003259 // Schedule the call to tell the activity manager we have
3260 // stopped. We don't do this immediately, because we want to
3261 // have a chance for any other pending work (in particular memory
3262 // trim requests) to complete before you tell the activity
3263 // manager to proceed and allow us to go fully into the background.
3264 info.activity = r;
3265 info.state = r.state;
3266 mH.post(info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268
3269 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003270 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 if (r.stopped) {
3272 r.activity.performRestart();
3273 r.stopped = false;
3274 }
3275 }
3276
Romain Guy65b345f2011-07-27 18:51:50 -07003277 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003278 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003279
3280 if (r == null) {
3281 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3282 return;
3283 }
3284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003286 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 } else if (show && r.stopped) {
3288 // If we are getting ready to gc after going to the background, well
3289 // we are back active so skip it.
3290 unscheduleGcIdler();
3291
3292 r.activity.performRestart();
3293 r.stopped = false;
3294 }
3295 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003296 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003297 TAG, "Handle window " + r + " visibility: " + show);
3298 updateVisibility(r, show);
3299 }
3300 }
3301
Romain Guy65b345f2011-07-27 18:51:50 -07003302 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003303 ActivityClientRecord r = mActivities.get(token);
3304
3305 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003306 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003307 return;
3308 }
3309
3310 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003311 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003312 try {
3313 // Now we are idle.
3314 r.activity.performStop();
3315 } catch (Exception e) {
3316 if (!mInstrumentation.onException(r.activity, e)) {
3317 throw new RuntimeException(
3318 "Unable to stop activity "
3319 + r.intent.getComponent().toShortString()
3320 + ": " + e.toString(), e);
3321 }
3322 }
3323 r.stopped = true;
3324 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003325
3326 // Make sure any pending writes are now committed.
3327 if (!r.isPreHoneycomb()) {
3328 QueuedWork.waitToFinish();
3329 }
3330
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003331 // Tell activity manager we slept.
3332 try {
3333 ActivityManagerNative.getDefault().activitySlept(r.token);
3334 } catch (RemoteException ex) {
3335 }
3336 } else {
3337 if (r.stopped && r.activity.mVisibleFromServer) {
3338 r.activity.performRestart();
3339 r.stopped = false;
3340 }
3341 }
3342 }
3343
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003344 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003345 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003346 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003347 }
3348 }
3349
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003350 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3351 LoadedApk apk = peekPackageInfo(data.pkg, false);
3352 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003353 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003354 }
3355 apk = peekPackageInfo(data.pkg, true);
3356 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003357 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003358 }
3359 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003360 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003361 }
3362
Romain Guy65b345f2011-07-27 18:51:50 -07003363 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 final int N = results.size();
3365 for (int i=0; i<N; i++) {
3366 ResultInfo ri = results.get(i);
3367 try {
3368 if (ri.mData != null) {
3369 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
3370 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003371 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003372 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 r.activity.dispatchActivityResult(ri.mResultWho,
3374 ri.mRequestCode, ri.mResultCode, ri.mData);
3375 } catch (Exception e) {
3376 if (!mInstrumentation.onException(r.activity, e)) {
3377 throw new RuntimeException(
3378 "Failure delivering result " + ri + " to activity "
3379 + r.intent.getComponent().toShortString()
3380 + ": " + e.toString(), e);
3381 }
3382 }
3383 }
3384 }
3385
Romain Guy65b345f2011-07-27 18:51:50 -07003386 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003387 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003388 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 if (r != null) {
3390 final boolean resumed = !r.paused;
3391 if (!r.activity.mFinished && r.activity.mDecor != null
3392 && r.hideForNow && resumed) {
3393 // We had hidden the activity because it started another
3394 // one... we have gotten a result back and we are not
3395 // paused, so make sure our window is visible.
3396 updateVisibility(r, true);
3397 }
3398 if (resumed) {
3399 try {
3400 // Now we are idle.
3401 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003402 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 mInstrumentation.callActivityOnPause(r.activity);
3404 if (!r.activity.mCalled) {
3405 throw new SuperNotCalledException(
3406 "Activity " + r.intent.getComponent().toShortString()
3407 + " did not call through to super.onPause()");
3408 }
3409 } catch (SuperNotCalledException e) {
3410 throw e;
3411 } catch (Exception e) {
3412 if (!mInstrumentation.onException(r.activity, e)) {
3413 throw new RuntimeException(
3414 "Unable to pause activity "
3415 + r.intent.getComponent().toShortString()
3416 + ": " + e.toString(), e);
3417 }
3418 }
3419 }
3420 deliverResults(r, res.results);
3421 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003422 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003423 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 }
3425 }
3426 }
3427
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003428 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 return performDestroyActivity(token, finishing, 0, false);
3430 }
3431
Romain Guy65b345f2011-07-27 18:51:50 -07003432 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003434 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003435 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003436 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003438 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003439 r.activity.mConfigChangeFlags |= configChanges;
3440 if (finishing) {
3441 r.activity.mFinished = true;
3442 }
3443 if (!r.paused) {
3444 try {
3445 r.activity.mCalled = false;
3446 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003447 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 r.activity.getComponentName().getClassName());
3449 if (!r.activity.mCalled) {
3450 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003451 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 + " did not call through to super.onPause()");
3453 }
3454 } catch (SuperNotCalledException e) {
3455 throw e;
3456 } catch (Exception e) {
3457 if (!mInstrumentation.onException(r.activity, e)) {
3458 throw new RuntimeException(
3459 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003460 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 + ": " + e.toString(), e);
3462 }
3463 }
3464 r.paused = true;
3465 }
3466 if (!r.stopped) {
3467 try {
3468 r.activity.performStop();
3469 } catch (SuperNotCalledException e) {
3470 throw e;
3471 } catch (Exception e) {
3472 if (!mInstrumentation.onException(r.activity, e)) {
3473 throw new RuntimeException(
3474 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003475 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 + ": " + e.toString(), e);
3477 }
3478 }
3479 r.stopped = true;
3480 }
3481 if (getNonConfigInstance) {
3482 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003483 r.lastNonConfigurationInstances
3484 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 } catch (Exception e) {
3486 if (!mInstrumentation.onException(r.activity, e)) {
3487 throw new RuntimeException(
3488 "Unable to retain activity "
3489 + r.intent.getComponent().toShortString()
3490 + ": " + e.toString(), e);
3491 }
3492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494 try {
3495 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003496 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003497 if (!r.activity.mCalled) {
3498 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003499 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 " did not call through to super.onDestroy()");
3501 }
3502 if (r.window != null) {
3503 r.window.closeAllPanels();
3504 }
3505 } catch (SuperNotCalledException e) {
3506 throw e;
3507 } catch (Exception e) {
3508 if (!mInstrumentation.onException(r.activity, e)) {
3509 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003510 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3511 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 }
3513 }
3514 }
3515 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003516 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 return r;
3518 }
3519
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003520 private static String safeToComponentShortString(Intent intent) {
3521 ComponentName component = intent.getComponent();
3522 return component == null ? "[Unknown]" : component.toShortString();
3523 }
3524
Romain Guy65b345f2011-07-27 18:51:50 -07003525 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003527 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 configChanges, getNonConfigInstance);
3529 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003530 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 WindowManager wm = r.activity.getWindowManager();
3532 View v = r.activity.mDecor;
3533 if (v != null) {
3534 if (r.activity.mVisibleFromServer) {
3535 mNumVisibleActivities--;
3536 }
3537 IBinder wtoken = v.getWindowToken();
3538 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003539 if (r.onlyLocalRequest) {
3540 // Hold off on removing this until the new activity's
3541 // window is being added.
3542 r.mPendingRemoveWindow = v;
3543 r.mPendingRemoveWindowManager = wm;
3544 } else {
3545 wm.removeViewImmediate(v);
3546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003548 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003549 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 r.activity.getClass().getName(), "Activity");
3551 }
3552 r.activity.mDecor = null;
3553 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003554 if (r.mPendingRemoveWindow == null) {
3555 // If we are delaying the removal of the activity window, then
3556 // we can't clean up all windows here. Note that we can't do
3557 // so later either, which means any windows that aren't closed
3558 // by the app will leak. Well we try to warning them a lot
3559 // about leaking windows, because that is a bug, so if they are
3560 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003561 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003562 r.activity.getClass().getName(), "Activity");
3563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564
3565 // Mocked out contexts won't be participating in the normal
3566 // process lifecycle, but if we're running with a proper
3567 // ApplicationContext we need to have it tear down things
3568 // cleanly.
3569 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003570 if (c instanceof ContextImpl) {
3571 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 r.activity.getClass().getName(), "Activity");
3573 }
3574 }
3575 if (finishing) {
3576 try {
3577 ActivityManagerNative.getDefault().activityDestroyed(token);
3578 } catch (RemoteException ex) {
3579 // If the system process has died, it's game over for everyone.
3580 }
3581 }
3582 }
3583
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003584 public final void requestRelaunchActivity(IBinder token,
3585 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3586 int configChanges, boolean notResumed, Configuration config,
3587 boolean fromServer) {
3588 ActivityClientRecord target = null;
3589
Craig Mautner88c05892013-06-28 09:47:45 -07003590 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003591 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3592 ActivityClientRecord r = mRelaunchingActivities.get(i);
3593 if (r.token == token) {
3594 target = r;
3595 if (pendingResults != null) {
3596 if (r.pendingResults != null) {
3597 r.pendingResults.addAll(pendingResults);
3598 } else {
3599 r.pendingResults = pendingResults;
3600 }
3601 }
3602 if (pendingNewIntents != null) {
3603 if (r.pendingIntents != null) {
3604 r.pendingIntents.addAll(pendingNewIntents);
3605 } else {
3606 r.pendingIntents = pendingNewIntents;
3607 }
3608 }
3609 break;
3610 }
3611 }
3612
3613 if (target == null) {
3614 target = new ActivityClientRecord();
3615 target.token = token;
3616 target.pendingResults = pendingResults;
3617 target.pendingIntents = pendingNewIntents;
3618 if (!fromServer) {
3619 ActivityClientRecord existing = mActivities.get(token);
3620 if (existing != null) {
3621 target.startsNotResumed = existing.paused;
3622 }
3623 target.onlyLocalRequest = true;
3624 }
3625 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003626 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003627 }
3628
3629 if (fromServer) {
3630 target.startsNotResumed = notResumed;
3631 target.onlyLocalRequest = false;
3632 }
3633 if (config != null) {
3634 target.createdConfig = config;
3635 }
3636 target.pendingConfigChanges |= configChanges;
3637 }
3638 }
3639
Romain Guy65b345f2011-07-27 18:51:50 -07003640 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 // If we are getting ready to gc after going to the background, well
3642 // we are back active so skip it.
3643 unscheduleGcIdler();
3644
3645 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003646 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 // First: make sure we have the most recent configuration and most
3649 // recent version of the activity, or skip it if some previous call
3650 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003651 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 int N = mRelaunchingActivities.size();
3653 IBinder token = tmp.token;
3654 tmp = null;
3655 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003656 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003657 if (r.token == token) {
3658 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003659 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 mRelaunchingActivities.remove(i);
3661 i--;
3662 N--;
3663 }
3664 }
Bob Leee5408332009-09-04 18:31:17 -07003665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003667 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 return;
3669 }
Bob Leee5408332009-09-04 18:31:17 -07003670
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003671 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3672 + tmp.token + " with configChanges=0x"
3673 + Integer.toHexString(configChanges));
3674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 if (mPendingConfiguration != null) {
3676 changedConfig = mPendingConfiguration;
3677 mPendingConfiguration = null;
3678 }
3679 }
Bob Leee5408332009-09-04 18:31:17 -07003680
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003681 if (tmp.createdConfig != null) {
3682 // If the activity manager is passing us its current config,
3683 // assume that is really what we want regardless of what we
3684 // may have pending.
3685 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003686 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3687 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3688 if (changedConfig == null
3689 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3690 changedConfig = tmp.createdConfig;
3691 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003692 }
3693 }
3694
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003695 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003696 + tmp.token + ": changedConfig=" + changedConfig);
3697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 // If there was a pending configuration change, execute it first.
3699 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003700 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003701 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003702 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 }
Bob Leee5408332009-09-04 18:31:17 -07003704
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003705 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003706 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003707 if (r == null) {
3708 return;
3709 }
Bob Leee5408332009-09-04 18:31:17 -07003710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003712 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003713 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003714
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003715 r.activity.mChangingConfigurations = true;
3716
Dianne Hackborne2b04802010-12-09 09:24:55 -08003717 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003719 performPauseActivity(r.token, false, r.isPreHoneycomb());
3720 }
3721 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
3722 r.state = new Bundle();
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -04003723 r.state.setAllowFds(false);
Dianne Hackborne2b04802010-12-09 09:24:55 -08003724 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 }
Bob Leee5408332009-09-04 18:31:17 -07003726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003727 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 r.activity = null;
3730 r.window = null;
3731 r.hideForNow = false;
3732 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003733 // Merge any pending results and pending intents; don't just replace them
3734 if (tmp.pendingResults != null) {
3735 if (r.pendingResults == null) {
3736 r.pendingResults = tmp.pendingResults;
3737 } else {
3738 r.pendingResults.addAll(tmp.pendingResults);
3739 }
3740 }
3741 if (tmp.pendingIntents != null) {
3742 if (r.pendingIntents == null) {
3743 r.pendingIntents = tmp.pendingIntents;
3744 } else {
3745 r.pendingIntents.addAll(tmp.pendingIntents);
3746 }
3747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 r.startsNotResumed = tmp.startsNotResumed;
Adam Powellcfbe9be2013-11-06 14:58:58 -08003749 r.activityOptions = null;
Bob Leee5408332009-09-04 18:31:17 -07003750
Christopher Tateb70f3df2009-04-07 16:07:59 -07003751 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
3753
Romain Guy65b345f2011-07-27 18:51:50 -07003754 private void handleRequestThumbnail(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003755 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 Bitmap thumbnail = createThumbnailBitmap(r);
3757 CharSequence description = null;
3758 try {
3759 description = r.activity.onCreateDescription();
3760 } catch (Exception e) {
3761 if (!mInstrumentation.onException(r.activity, e)) {
3762 throw new RuntimeException(
3763 "Unable to create description of activity "
3764 + r.intent.getComponent().toShortString()
3765 + ": " + e.toString(), e);
3766 }
3767 }
3768 //System.out.println("Reporting top thumbnail " + thumbnail);
3769 try {
3770 ActivityManagerNative.getDefault().reportThumbnail(
3771 token, thumbnail, description);
3772 } catch (RemoteException ex) {
3773 }
3774 }
3775
Dianne Hackborn73c14162012-09-19 15:45:06 -07003776 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003778 ArrayList<ComponentCallbacks2> callbacks
3779 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003780
Craig Mautner88c05892013-06-28 09:47:45 -07003781 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003782 final int NAPP = mAllApplications.size();
3783 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003784 callbacks.add(mAllApplications.get(i));
3785 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003786 final int NACT = mActivities.size();
3787 for (int i=0; i<NACT; i++) {
3788 ActivityClientRecord ar = mActivities.valueAt(i);
3789 Activity a = ar.activity;
3790 if (a != null) {
3791 Configuration thisConfig = applyConfigCompatMainThread(
3792 mCurDefaultDisplayDpi, newConfig,
3793 ar.packageInfo.getCompatibilityInfo());
3794 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3795 // If the activity is currently resumed, its configuration
3796 // needs to change right now.
3797 callbacks.add(a);
3798 } else if (thisConfig != null) {
3799 // Otherwise, we will tell it about the change
3800 // the next time it is resumed or shown. Note that
3801 // the activity manager may, before then, decide the
3802 // activity needs to be destroyed to handle its new
3803 // configuration.
3804 if (DEBUG_CONFIGURATION) {
3805 Slog.v(TAG, "Setting activity "
3806 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003807 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003808 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 }
3810 }
3811 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003812 final int NSVC = mServices.size();
3813 for (int i=0; i<NSVC; i++) {
3814 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
3816 }
3817 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003818 final int NPRV = mLocalProviders.size();
3819 for (int i=0; i<NPRV; i++) {
3820 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 }
3822 }
Bob Leee5408332009-09-04 18:31:17 -07003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 return callbacks;
3825 }
Bob Leee5408332009-09-04 18:31:17 -07003826
Romain Guya998dff2012-03-23 18:58:36 -07003827 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003828 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003829 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 // we check the runtime type and act accordingly.
3831 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3832 if (activity != null) {
3833 activity.mCalled = false;
3834 }
Bob Leee5408332009-09-04 18:31:17 -07003835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 boolean shouldChangeConfig = false;
3837 if ((activity == null) || (activity.mCurrentConfig == null)) {
3838 shouldChangeConfig = true;
3839 } else {
Bob Leee5408332009-09-04 18:31:17 -07003840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841 // If the new config is the same as the config this Activity
3842 // is already running with then don't bother calling
3843 // onConfigurationChanged
3844 int diff = activity.mCurrentConfig.diff(config);
3845 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 // If this activity doesn't handle any of the config changes
3847 // then don't bother calling onConfigurationChanged as we're
3848 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07003849 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 shouldChangeConfig = true;
3851 }
3852 }
3853 }
Bob Leee5408332009-09-04 18:31:17 -07003854
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003855 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003856 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 if (shouldChangeConfig) {
3858 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07003859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 if (activity != null) {
3861 if (!activity.mCalled) {
3862 throw new SuperNotCalledException(
3863 "Activity " + activity.getLocalClassName() +
3864 " did not call through to super.onConfigurationChanged()");
3865 }
3866 activity.mConfigChangeFlags = 0;
3867 activity.mCurrentConfig = new Configuration(config);
3868 }
3869 }
3870 }
3871
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003872 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07003873 synchronized (mResourcesManager) {
3874 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07003875 }
3876 }
3877
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003878 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003879 Configuration config = mConfiguration;
3880 if (mCompatConfiguration == null) {
3881 mCompatConfiguration = new Configuration();
3882 }
3883 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07003884 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07003885 config = mCompatConfiguration;
3886 }
3887 return config;
3888 }
3889
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003890 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003891
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003892 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003893
Craig Mautner88c05892013-06-28 09:47:45 -07003894 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003895 if (mPendingConfiguration != null) {
3896 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
3897 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003898 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003899 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003900 }
3901 mPendingConfiguration = null;
3902 }
3903
3904 if (config == null) {
3905 return;
3906 }
3907
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003908 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003909 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07003910
3911 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
3912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 if (mConfiguration == null) {
3914 mConfiguration = new Configuration();
3915 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003916 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003917 return;
3918 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003919 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003920 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003921 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07003923
3924 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
3925
Romain Guy65b345f2011-07-27 18:51:50 -07003926 // Cleanup hardware accelerated stuff
Jeff Brown98365d72012-08-19 20:30:52 -07003927 WindowManagerGlobal.getInstance().trimLocalMemory();
Bob Leee5408332009-09-04 18:31:17 -07003928
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003929 freeTextLayoutCachesIfNeeded(configDiff);
3930
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003931 if (callbacks != null) {
3932 final int N = callbacks.size();
3933 for (int i=0; i<N; i++) {
3934 performConfigurationChanged(callbacks.get(i), config);
3935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937 }
3938
Romain Guy46bfc482013-08-16 18:38:29 -07003939 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003940 if (configDiff != 0) {
3941 // Ask text layout engine to free its caches if there is a locale change
3942 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
3943 if (hasLocaleConfigChange) {
3944 Canvas.freeTextLayoutCaches();
3945 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
3946 }
3947 }
3948 }
3949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003950 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003951 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003952 if (r == null || r.activity == null) {
3953 return;
3954 }
Bob Leee5408332009-09-04 18:31:17 -07003955
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003956 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003957 + r.activityInfo.name);
3958
Dianne Hackborn5fd21692011-06-07 14:09:47 -07003959 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003960
3961 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 }
3963
Romain Guy7eabe552011-07-21 14:56:34 -07003964 final void handleProfilerControl(boolean start, ProfilerControlData pcd, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003965 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003966 try {
Romain Guy7eabe552011-07-21 14:56:34 -07003967 switch (profileType) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003968 default:
3969 mProfiler.setProfiler(pcd.path, pcd.fd);
3970 mProfiler.autoStopProfiler = false;
3971 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003972 break;
3973 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003974 } catch (RuntimeException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003975 Slog.w(TAG, "Profiling failed on path " + pcd.path
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003976 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003977 } finally {
3978 try {
3979 pcd.fd.close();
3980 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003981 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07003982 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003983 }
3984 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07003985 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07003986 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07003987 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07003988 break;
Romain Guy7eabe552011-07-21 14:56:34 -07003989 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08003990 }
3991 }
Bob Leee5408332009-09-04 18:31:17 -07003992
Romain Guya998dff2012-03-23 18:58:36 -07003993 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07003994 if (managed) {
3995 try {
3996 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
3997 } catch (IOException e) {
3998 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
3999 + " -- can the process access this path?");
4000 } finally {
4001 try {
4002 dhd.fd.close();
4003 } catch (IOException e) {
4004 Slog.w(TAG, "Failure closing profile fd", e);
4005 }
4006 }
4007 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004008 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004009 }
4010 }
4011
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004012 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4013 boolean hasPkgInfo = false;
4014 if (packages != null) {
4015 for (int i=packages.length-1; i>=0; i--) {
4016 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4017 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004018 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004019 ref = mPackages.get(packages[i]);
4020 if (ref != null && ref.get() != null) {
4021 hasPkgInfo = true;
4022 } else {
4023 ref = mResourcePackages.get(packages[i]);
4024 if (ref != null && ref.get() != null) {
4025 hasPkgInfo = true;
4026 }
4027 }
4028 }
4029 mPackages.remove(packages[i]);
4030 mResourcePackages.remove(packages[i]);
4031 }
4032 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004033 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004034 hasPkgInfo);
4035 }
4036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004037 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004038 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004040 final int N = callbacks.size();
4041 for (int i=0; i<N; i++) {
4042 callbacks.get(i).onLowMemory();
4043 }
4044
Chris Tatece229052009-03-25 16:44:52 -07004045 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4046 if (Process.myUid() != Process.SYSTEM_UID) {
4047 int sqliteReleased = SQLiteDatabase.releaseMemory();
4048 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4049 }
Bob Leee5408332009-09-04 18:31:17 -07004050
Mike Reedcaf0df12009-04-27 14:32:05 -04004051 // Ask graphics to free up as much as possible (font/image caches)
4052 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004053
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004054 // Ask text layout engine to free also as much as possible
4055 Canvas.freeTextLayoutCaches();
4056
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004057 BinderInternal.forceGc("mem");
4058 }
4059
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004060 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004061 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004062
Jeff Brown98365d72012-08-19 20:30:52 -07004063 final WindowManagerGlobal windowManager = WindowManagerGlobal.getInstance();
Romain Guy19f86e82012-04-23 15:19:07 -07004064 windowManager.startTrimMemory(level);
4065
Dianne Hackborn73c14162012-09-19 15:45:06 -07004066 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004067
4068 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004069 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004070 callbacks.get(i).onTrimMemory(level);
4071 }
Romain Guy19f86e82012-04-23 15:19:07 -07004072
Jeff Brown98365d72012-08-19 20:30:52 -07004073 windowManager.endTrimMemory();
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004074 }
4075
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004076 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004077 if (Process.isIsolated()) {
4078 // Isolated processes aren't going to do UI.
4079 return;
4080 }
Romain Guya9582652011-11-10 14:20:10 -08004081 try {
4082 int uid = Process.myUid();
4083 String[] packages = getPackageManager().getPackagesForUid(uid);
4084
4085 // If there are several packages in this application we won't
4086 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004087 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004088 HardwareRenderer.setupDiskCache(cacheDir);
4089 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004090 }
4091 } catch (RemoteException e) {
4092 // Ignore
4093 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004094 }
4095
4096 private void updateDefaultDensity() {
4097 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4098 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4099 && !mDensityCompatMode) {
4100 Slog.i(TAG, "Switching default density from "
4101 + DisplayMetrics.DENSITY_DEVICE + " to "
4102 + mCurDefaultDisplayDpi);
4103 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4104 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4105 }
4106 }
4107
Romain Guy65b345f2011-07-27 18:51:50 -07004108 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 mBoundApplication = data;
4110 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004111 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004113 mProfiler = new Profiler();
4114 mProfiler.profileFile = data.initProfileFile;
4115 mProfiler.profileFd = data.initProfileFd;
4116 mProfiler.autoStopProfiler = data.initAutoStopProfiler;
4117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004118 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004119 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004120 android.ddm.DdmHandleAppName.setAppName(data.processName,
4121 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004123 if (data.persistent) {
4124 // Persistent processes on low-memory devices do not get to
4125 // use hardware accelerated drawing, since this can add too much
4126 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004127 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004128 HardwareRenderer.disable(false);
4129 }
4130 }
Romain Guya9582652011-11-10 14:20:10 -08004131
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004132 if (mProfiler.profileFd != null) {
4133 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004134 }
4135
Joe Onoratod630f102011-03-17 18:42:26 -07004136 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4137 // implementation to use the pool executor. Normally, we use the
4138 // serialized executor as the default. This has to happen in the
4139 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004140 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004141 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4142 }
4143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 /*
4145 * Before spawning a new process, reset the time zone to be the system time zone.
4146 * This needs to be done because the system time zone could have changed after the
4147 * the spawning of this process. Without doing this this process would have the incorrect
4148 * system time zone.
4149 */
4150 TimeZone.setDefault(null);
4151
4152 /*
4153 * Initialize the default locale in this process for the reasons we set the time zone.
4154 */
4155 Locale.setDefault(data.config.locale);
4156
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004157 /*
4158 * Update the system configuration since its preloaded and might not
4159 * reflect configuration changes. The configuration object passed
4160 * in AppBindData can be safely assumed to be up to date
4161 */
Craig Mautner88c05892013-06-28 09:47:45 -07004162 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004163 mCurDefaultDisplayDpi = data.config.densityDpi;
4164 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004165
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004166 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004167
Dianne Hackborndde331c2012-08-03 14:01:57 -07004168 /**
4169 * Switch this process to density compatibility mode if needed.
4170 */
4171 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4172 == 0) {
4173 mDensityCompatMode = true;
4174 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4175 }
4176 updateDefaultDensity();
4177
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004178 final ContextImpl appContext = new ContextImpl();
4179 appContext.init(data.info, null, this);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004180 if (!Process.isIsolated()) {
4181 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004182
Amith Yamasani92d57052012-08-23 10:07:52 -07004183 if (cacheDir != null) {
4184 // Provide a usable directory for temporary files
4185 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4186
4187 setupGraphicsSupport(data.info, cacheDir);
4188 } else {
4189 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4190 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004191 }
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004192 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004193 * For system applications on userdebug/eng builds, log stack
4194 * traces of disk and network access to dropbox for analysis.
4195 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004196 if ((data.appInfo.flags &
4197 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004198 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4199 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004200 }
4201
4202 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004203 * For apps targetting SDK Honeycomb or later, we don't allow
4204 * network usage on the main event loop / UI thread.
4205 *
4206 * Note to those grepping: this is what ultimately throws
4207 * NetworkOnMainThreadException ...
4208 */
4209 if (data.appInfo.targetSdkVersion > 9) {
4210 StrictMode.enableDeathOnNetwork();
4211 }
4212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4214 // XXX should have option to change the port.
4215 Debug.changeDebugPort(8100);
4216 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004217 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 + " is waiting for the debugger on port 8100...");
4219
4220 IActivityManager mgr = ActivityManagerNative.getDefault();
4221 try {
4222 mgr.showWaitingForDebugger(mAppThread, true);
4223 } catch (RemoteException ex) {
4224 }
4225
4226 Debug.waitForDebugger();
4227
4228 try {
4229 mgr.showWaitingForDebugger(mAppThread, false);
4230 } catch (RemoteException ex) {
4231 }
4232
4233 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004234 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 + " can be debugged on port 8100...");
4236 }
4237 }
4238
Siva Velusamy92a8b222012-03-09 16:24:04 -08004239 // Enable OpenGL tracing if required
4240 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004241 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004242 }
4243
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004244 // Allow application-generated systrace messages if we're debuggable.
4245 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4246 Trace.setAppTracingAllowed(appTracingAllowed);
4247
Robert Greenwalt434203a2010-10-11 16:00:27 -07004248 /**
4249 * Initialize the default http proxy in this process for the reasons we set the time zone.
4250 */
4251 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004252 if (b != null) {
4253 // In pre-boot mode (doing initial launch to collect password), not
4254 // all system is up. This includes the connectivity service, so don't
4255 // crash if we can't get it.
4256 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4257 try {
4258 ProxyProperties proxyProperties = service.getProxy();
4259 Proxy.setHttpProxySystemProperty(proxyProperties);
4260 } catch (RemoteException e) {}
4261 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 InstrumentationInfo ii = null;
4265 try {
4266 ii = appContext.getPackageManager().
4267 getInstrumentationInfo(data.instrumentationName, 0);
4268 } catch (PackageManager.NameNotFoundException e) {
4269 }
4270 if (ii == null) {
4271 throw new RuntimeException(
4272 "Unable to find instrumentation info for: "
4273 + data.instrumentationName);
4274 }
4275
4276 mInstrumentationAppDir = ii.sourceDir;
Brian Carlstromd893a892012-04-01 21:30:26 -07004277 mInstrumentationAppLibraryDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 mInstrumentationAppPackage = ii.packageName;
4279 mInstrumentedAppDir = data.info.getAppDir();
Brian Carlstromd893a892012-04-01 21:30:26 -07004280 mInstrumentedAppLibraryDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281
4282 ApplicationInfo instrApp = new ApplicationInfo();
4283 instrApp.packageName = ii.packageName;
4284 instrApp.sourceDir = ii.sourceDir;
4285 instrApp.publicSourceDir = ii.publicSourceDir;
4286 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004287 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004288 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 appContext.getClassLoader(), false, true);
Dianne Hackborn21556372010-02-04 16:34:40 -08004290 ContextImpl instrContext = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 instrContext.init(pi, null, this);
4292
4293 try {
4294 java.lang.ClassLoader cl = instrContext.getClassLoader();
4295 mInstrumentation = (Instrumentation)
4296 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4297 } catch (Exception e) {
4298 throw new RuntimeException(
4299 "Unable to instantiate instrumentation "
4300 + data.instrumentationName + ": " + e.toString(), e);
4301 }
4302
4303 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004304 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4305 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004306
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004307 if (mProfiler.profileFile != null && !ii.handleProfiling
4308 && mProfiler.profileFd == null) {
4309 mProfiler.handlingProfiling = true;
4310 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 file.getParentFile().mkdirs();
4312 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4313 }
4314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 } else {
4316 mInstrumentation = new Instrumentation();
4317 }
4318
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004319 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004320 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004321 }
4322
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004323 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004324 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004325 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004326 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004328 // If the app is being launched for full backup or restore, bring it up in
4329 // a restricted environment with the base application class.
4330 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4331 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004332
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004333 // don't bring up providers in restricted mode; they may depend on the
4334 // app's custom Application class
4335 if (!data.restrictedBackupMode) {
4336 List<ProviderInfo> providers = data.providers;
4337 if (providers != null) {
4338 installContentProviders(app, providers);
4339 // For process that contains content providers, we want to
4340 // ensure that the JIT is enabled "at some point".
4341 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4342 }
4343 }
4344
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004345 // Do this after providers, since instrumentation tests generally start their
4346 // test thread at this point, and we don't want that racing.
4347 try {
4348 mInstrumentation.onCreate(data.instrumentationArgs);
4349 }
4350 catch (Exception e) {
4351 throw new RuntimeException(
4352 "Exception thrown in onCreate() of "
4353 + data.instrumentationName + ": " + e.toString(), e);
4354 }
4355
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004356 try {
4357 mInstrumentation.callApplicationOnCreate(app);
4358 } catch (Exception e) {
4359 if (!mInstrumentation.onException(app, e)) {
4360 throw new RuntimeException(
4361 "Unable to create application " + app.getClass().getName()
4362 + ": " + e.toString(), e);
4363 }
4364 }
4365 } finally {
4366 StrictMode.setThreadPolicy(savedPolicy);
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368 }
4369
4370 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4371 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004372 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4373 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 Debug.stopMethodTracing();
4375 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004376 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 // + ", app thr: " + mAppThread);
4378 try {
4379 am.finishInstrumentation(mAppThread, resultCode, results);
4380 } catch (RemoteException ex) {
4381 }
4382 }
4383
Romain Guy65b345f2011-07-27 18:51:50 -07004384 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004385 Context context, List<ProviderInfo> providers) {
4386 final ArrayList<IActivityManager.ContentProviderHolder> results =
4387 new ArrayList<IActivityManager.ContentProviderHolder>();
4388
Romain Guya998dff2012-03-23 18:58:36 -07004389 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004390 if (DEBUG_PROVIDER) {
4391 StringBuilder buf = new StringBuilder(128);
4392 buf.append("Pub ");
4393 buf.append(cpi.authority);
4394 buf.append(": ");
4395 buf.append(cpi.name);
4396 Log.i(TAG, buf.toString());
4397 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004398 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4399 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4400 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004401 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 }
4404 }
4405
4406 try {
4407 ActivityManagerNative.getDefault().publishContentProviders(
4408 getApplicationThread(), results);
4409 } catch (RemoteException ex) {
4410 }
4411 }
4412
Jeff Sharkey6d515712012-09-20 16:06:08 -07004413 public final IContentProvider acquireProvider(
4414 Context c, String auth, int userId, boolean stable) {
4415 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004416 if (provider != null) {
4417 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004418 }
4419
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004420 // There is a possible race here. Another thread may try to acquire
4421 // the same provider at the same time. When this happens, we want to ensure
4422 // that the first one wins.
4423 // Note that we cannot hold the lock while acquiring and installing the
4424 // provider since it might take a long time to run and it could also potentially
4425 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 IActivityManager.ContentProviderHolder holder = null;
4427 try {
4428 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004429 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004430 } catch (RemoteException ex) {
4431 }
4432 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004433 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 return null;
4435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004437 // Install provider will increment the reference count for us, and break
4438 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004439 holder = installProvider(c, holder, holder.info,
4440 true /*noisy*/, holder.noReleaseNeeded, stable);
4441 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004442 }
4443
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004444 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4445 if (stable) {
4446 prc.stableCount += 1;
4447 if (prc.stableCount == 1) {
4448 // We are acquiring a new stable reference on the provider.
4449 int unstableDelta;
4450 if (prc.removePending) {
4451 // We have a pending remove operation, which is holding the
4452 // last unstable reference. At this point we are converting
4453 // that unstable reference to our new stable reference.
4454 unstableDelta = -1;
4455 // Cancel the removal of the provider.
4456 if (DEBUG_PROVIDER) {
4457 Slog.v(TAG, "incProviderRef: stable "
4458 + "snatched provider from the jaws of death");
4459 }
4460 prc.removePending = false;
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004461 // There is a race! It fails to remove the message, which
4462 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004463 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4464 } else {
4465 unstableDelta = 0;
4466 }
4467 try {
4468 if (DEBUG_PROVIDER) {
4469 Slog.v(TAG, "incProviderRef Now stable - "
4470 + prc.holder.info.name + ": unstableDelta="
4471 + unstableDelta);
4472 }
4473 ActivityManagerNative.getDefault().refContentProvider(
4474 prc.holder.connection, 1, unstableDelta);
4475 } catch (RemoteException e) {
4476 //do nothing content provider object is dead any way
4477 }
4478 }
4479 } else {
4480 prc.unstableCount += 1;
4481 if (prc.unstableCount == 1) {
4482 // We are acquiring a new unstable reference on the provider.
4483 if (prc.removePending) {
4484 // Oh look, we actually have a remove pending for the
4485 // provider, which is still holding the last unstable
4486 // reference. We just need to cancel that to take new
4487 // ownership of the reference.
4488 if (DEBUG_PROVIDER) {
4489 Slog.v(TAG, "incProviderRef: unstable "
4490 + "snatched provider from the jaws of death");
4491 }
4492 prc.removePending = false;
4493 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4494 } else {
4495 // First unstable ref, increment our count in the
4496 // activity manager.
4497 try {
4498 if (DEBUG_PROVIDER) {
4499 Slog.v(TAG, "incProviderRef: Now unstable - "
4500 + prc.holder.info.name);
4501 }
4502 ActivityManagerNative.getDefault().refContentProvider(
4503 prc.holder.connection, 0, 1);
4504 } catch (RemoteException e) {
4505 //do nothing content provider object is dead any way
4506 }
4507 }
4508 }
4509 }
4510 }
4511
Jeff Sharkey6d515712012-09-20 16:06:08 -07004512 public final IContentProvider acquireExistingProvider(
4513 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004514 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004515 final ProviderKey key = new ProviderKey(auth, userId);
4516 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004517 if (pr == null) {
4518 return null;
4519 }
4520
4521 IContentProvider provider = pr.mProvider;
4522 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004523 if (!jBinder.isBinderAlive()) {
4524 // The hosting process of the provider has died; we can't
4525 // use this one.
4526 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4527 + ": existing object's process dead");
4528 handleUnstableProviderDiedLocked(jBinder, true);
4529 return null;
4530 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004531
4532 // Only increment the ref count if we have one. If we don't then the
4533 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004534 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004535 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004536 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004537 }
4538 return provider;
4539 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004540 }
4541
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004542 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4543 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 return false;
4545 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004548 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004550 if (prc == null) {
4551 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004553 }
4554
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004555 boolean lastRef = false;
4556 if (stable) {
4557 if (prc.stableCount == 0) {
4558 if (DEBUG_PROVIDER) Slog.v(TAG,
4559 "releaseProvider: stable ref count already 0, how?");
4560 return false;
4561 }
4562 prc.stableCount -= 1;
4563 if (prc.stableCount == 0) {
4564 // What we do at this point depends on whether there are
4565 // any unstable refs left: if there are, we just tell the
4566 // activity manager to decrement its stable count; if there
4567 // aren't, we need to enqueue this provider to be removed,
4568 // and convert to holding a single unstable ref while
4569 // doing so.
4570 lastRef = prc.unstableCount == 0;
4571 try {
4572 if (DEBUG_PROVIDER) {
4573 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4574 + lastRef + " - " + prc.holder.info.name);
4575 }
4576 ActivityManagerNative.getDefault().refContentProvider(
4577 prc.holder.connection, -1, lastRef ? 1 : 0);
4578 } catch (RemoteException e) {
4579 //do nothing content provider object is dead any way
4580 }
4581 }
4582 } else {
4583 if (prc.unstableCount == 0) {
4584 if (DEBUG_PROVIDER) Slog.v(TAG,
4585 "releaseProvider: unstable ref count already 0, how?");
4586 return false;
4587 }
4588 prc.unstableCount -= 1;
4589 if (prc.unstableCount == 0) {
4590 // If this is the last reference, we need to enqueue
4591 // this provider to be removed instead of telling the
4592 // activity manager to remove it at this point.
4593 lastRef = prc.stableCount == 0;
4594 if (!lastRef) {
4595 try {
4596 if (DEBUG_PROVIDER) {
4597 Slog.v(TAG, "releaseProvider: No longer unstable - "
4598 + prc.holder.info.name);
4599 }
4600 ActivityManagerNative.getDefault().refContentProvider(
4601 prc.holder.connection, 0, -1);
4602 } catch (RemoteException e) {
4603 //do nothing content provider object is dead any way
4604 }
4605 }
4606 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004607 }
4608
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004609 if (lastRef) {
4610 if (!prc.removePending) {
4611 // Schedule the actual remove asynchronously, since we don't know the context
4612 // this will be called in.
4613 // TODO: it would be nice to post a delayed message, so
4614 // if we come back and need the same provider quickly
4615 // we will still have it available.
4616 if (DEBUG_PROVIDER) {
4617 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4618 + prc.holder.info.name);
4619 }
4620 prc.removePending = true;
4621 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4622 mH.sendMessage(msg);
4623 } else {
4624 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4625 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004626 }
4627 return true;
4628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004629 }
4630
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004631 final void completeRemoveProvider(ProviderRefCount prc) {
4632 synchronized (mProviderMap) {
4633 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004634 // There was a race! Some other client managed to acquire
4635 // the provider before the removal was completed.
4636 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004637 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004638 + "provider still in use");
4639 return;
4640 }
4641
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004642 // More complicated race!! Some client managed to acquire the
4643 // provider and release it before the removal was completed.
4644 // Continue the removal, and abort the next remove message.
4645 prc.removePending = false;
4646
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004647 final IBinder jBinder = prc.holder.provider.asBinder();
4648 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4649 if (existingPrc == prc) {
4650 mProviderRefCountMap.remove(jBinder);
4651 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004652
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004653 for (int i=mProviderMap.size()-1; i>=0; i--) {
4654 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004655 IBinder myBinder = pr.mProvider.asBinder();
4656 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004657 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004658 }
4659 }
4660 }
4661
4662 try {
4663 if (DEBUG_PROVIDER) {
4664 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4665 + "removeContentProvider(" + prc.holder.info.name + ")");
4666 }
4667 ActivityManagerNative.getDefault().removeContentProvider(
4668 prc.holder.connection, false);
4669 } catch (RemoteException e) {
4670 //do nothing content provider object is dead any way
4671 }
4672 }
4673
4674 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004675 synchronized (mProviderMap) {
4676 handleUnstableProviderDiedLocked(provider, fromClient);
4677 }
4678 }
4679
4680 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4681 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4682 if (prc != null) {
4683 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4684 + provider + " " + prc.holder.info.name);
4685 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004686 for (int i=mProviderMap.size()-1; i>=0; i--) {
4687 ProviderClientRecord pr = mProviderMap.valueAt(i);
4688 if (pr != null && pr.mProvider.asBinder() == provider) {
4689 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4690 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004691 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004692 }
You Kimbc74de62013-10-01 00:13:26 +09004693
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004694 if (fromClient) {
4695 // We found out about this due to execution in our client
4696 // code. Tell the activity manager about it now, to ensure
4697 // that the next time we go to do anything with the provider
4698 // it knows it is dead (so we don't race with its death
4699 // notification).
4700 try {
4701 ActivityManagerNative.getDefault().unstableProviderDied(
4702 prc.holder.connection);
4703 } catch (RemoteException e) {
4704 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004705 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004706 }
4707 }
4708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004709
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004710 final void appNotRespondingViaProvider(IBinder provider) {
4711 synchronized (mProviderMap) {
4712 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4713 if (prc != null) {
4714 try {
4715 ActivityManagerNative.getDefault()
4716 .appNotRespondingViaProvider(prc.holder.connection);
4717 } catch (RemoteException e) {
4718 }
4719 }
4720 }
4721 }
4722
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004723 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004724 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4725 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4726 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4727
4728 final ProviderClientRecord pcr = new ProviderClientRecord(
4729 auths, provider, localProvider, holder);
4730 for (String auth : auths) {
4731 final ProviderKey key = new ProviderKey(auth, userId);
4732 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004733 if (existing != null) {
4734 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004735 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004736 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004737 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 }
4739 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004740 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 }
4742
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004743 /**
4744 * Installs the provider.
4745 *
4746 * Providers that are local to the process or that come from the system server
4747 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4748 * Other remote providers are reference counted. The initial reference count
4749 * for all reference counted providers is one. Providers that are not reference
4750 * counted do not have a reference count (at all).
4751 *
4752 * This method detects when a provider has already been installed. When this happens,
4753 * it increments the reference count of the existing provider (if appropriate)
4754 * and returns the existing provider. This can happen due to concurrent
4755 * attempts to acquire the same provider.
4756 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004757 private IActivityManager.ContentProviderHolder installProvider(Context context,
4758 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4759 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004761 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004762 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004763 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004764 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 + info.name);
4766 }
4767 Context c = null;
4768 ApplicationInfo ai = info.applicationInfo;
4769 if (context.getPackageName().equals(ai.packageName)) {
4770 c = context;
4771 } else if (mInitialApplication != null &&
4772 mInitialApplication.getPackageName().equals(ai.packageName)) {
4773 c = mInitialApplication;
4774 } else {
4775 try {
4776 c = context.createPackageContext(ai.packageName,
4777 Context.CONTEXT_INCLUDE_CODE);
4778 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004779 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 }
4781 }
4782 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004783 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 ai.packageName +
4785 " while loading content provider " +
4786 info.name);
4787 return null;
4788 }
4789 try {
4790 final java.lang.ClassLoader cl = c.getClassLoader();
4791 localProvider = (ContentProvider)cl.
4792 loadClass(info.name).newInstance();
4793 provider = localProvider.getIContentProvider();
4794 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004795 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 info.name + " from sourceDir " +
4797 info.applicationInfo.sourceDir);
4798 return null;
4799 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004800 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004801 TAG, "Instantiating local provider " + info.name);
4802 // XXX Need to create the correct context for this provider.
4803 localProvider.attachInfo(c, info);
4804 } catch (java.lang.Exception e) {
4805 if (!mInstrumentation.onException(null, e)) {
4806 throw new RuntimeException(
4807 "Unable to get provider " + info.name
4808 + ": " + e.toString(), e);
4809 }
4810 return null;
4811 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004812 } else {
4813 provider = holder.provider;
4814 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 + info.name);
4816 }
4817
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004818 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004819
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004820 synchronized (mProviderMap) {
4821 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4822 + " / " + info.name);
4823 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004824 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004825 ComponentName cname = new ComponentName(info.packageName, info.name);
4826 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004827 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004828 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004829 Slog.v(TAG, "installProvider: lost the race, "
4830 + "using existing local provider");
4831 }
4832 provider = pr.mProvider;
4833 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004834 holder = new IActivityManager.ContentProviderHolder(info);
4835 holder.provider = provider;
4836 holder.noReleaseNeeded = true;
4837 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004838 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004839 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004840 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004841 retHolder = pr.mHolder;
4842 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004843 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
4844 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004845 if (DEBUG_PROVIDER) {
4846 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004847 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004848 // We need to transfer our new reference to the existing
4849 // ref count, releasing the old one... but only if
4850 // release is needed (that is, it is not running in the
4851 // system process).
4852 if (!noReleaseNeeded) {
4853 incProviderRefLocked(prc, stable);
4854 try {
4855 ActivityManagerNative.getDefault().removeContentProvider(
4856 holder.connection, stable);
4857 } catch (RemoteException e) {
4858 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004859 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004860 }
4861 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004862 ProviderClientRecord client = installProviderAuthoritiesLocked(
4863 provider, localProvider, holder);
4864 if (noReleaseNeeded) {
4865 prc = new ProviderRefCount(holder, client, 1000, 1000);
4866 } else {
4867 prc = stable
4868 ? new ProviderRefCount(holder, client, 1, 0)
4869 : new ProviderRefCount(holder, client, 0, 1);
4870 }
4871 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004872 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004873 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 }
4875 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004876
4877 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004878 }
4879
Romain Guy65b345f2011-07-27 18:51:50 -07004880 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08004881 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07004884 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07004885 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08004886 public void run() {
4887 ensureJitEnabled();
4888 }
4889 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004890 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
4891 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 RuntimeInit.setApplicationObject(mAppThread.asBinder());
4893 IActivityManager mgr = ActivityManagerNative.getDefault();
4894 try {
4895 mgr.attachApplication(mAppThread);
4896 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07004897 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 }
4899 } else {
4900 // Don't set application object here -- if the system crashes,
4901 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004902 android.ddm.DdmHandleAppName.setAppName("system_process",
4903 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 try {
4905 mInstrumentation = new Instrumentation();
Dianne Hackborn21556372010-02-04 16:34:40 -08004906 ContextImpl context = new ContextImpl();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907 context.init(getSystemContext().mPackageInfo, null, this);
4908 Application app = Instrumentation.newApplication(Application.class, context);
4909 mAllApplications.add(app);
4910 mInitialApplication = app;
4911 app.onCreate();
4912 } catch (Exception e) {
4913 throw new RuntimeException(
4914 "Unable to instantiate Application():" + e.toString(), e);
4915 }
4916 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07004917
4918 // add dropbox logging to libcore
4919 DropBox.setReporter(new DropBoxReporter());
4920
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004921 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07004922 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004923 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07004924 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004925 // We need to apply this change to the resources
4926 // immediately, because upon returning the view
4927 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07004928 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07004929 // This actually changed the resources! Tell
4930 // everyone about it.
4931 if (mPendingConfiguration == null ||
4932 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
4933 mPendingConfiguration = newConfig;
4934
Jeff Brown9ef09972013-10-15 20:49:59 -07004935 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07004936 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004937 }
4938 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004939 }
Craig Mautner88c05892013-06-28 09:47:45 -07004940 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004941 public void onLowMemory() {
4942 }
Craig Mautner88c05892013-06-28 09:47:45 -07004943 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004944 public void onTrimMemory(int level) {
4945 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004946 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 }
4948
Romain Guy5e9120d2012-01-30 12:17:22 -08004949 public static ActivityThread systemMain() {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004950 HardwareRenderer.disable(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004951 ActivityThread thread = new ActivityThread();
4952 thread.attach(true);
4953 return thread;
4954 }
4955
Jeff Brown10e89712011-07-08 18:52:57 -07004956 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07004958 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 }
4960 }
4961
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004962 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07004963 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004964 if (mCoreSettings != null) {
4965 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004966 }
Craig Mautner88c05892013-06-28 09:47:45 -07004967 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004968 }
4969 }
4970
Geremy Condra69689a72012-09-11 16:57:17 -07004971 private static class EventLoggingReporter implements EventLogger.Reporter {
4972 @Override
4973 public void report (int code, Object... list) {
4974 EventLog.writeEvent(code, list);
4975 }
4976 }
4977
Geremy Condrab7faaf42012-09-19 18:07:42 -07004978 private class DropBoxReporter implements DropBox.Reporter {
4979
4980 private DropBoxManager dropBox;
4981
4982 public DropBoxReporter() {
4983 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
4984 }
4985
4986 @Override
4987 public void addData(String tag, byte[] data, int flags) {
4988 dropBox.addData(tag, data, flags);
4989 }
4990
4991 @Override
4992 public void addText(String tag, String data) {
4993 dropBox.addText(tag, data);
4994 }
4995 }
4996
Romain Guy65b345f2011-07-27 18:51:50 -07004997 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07004998 SamplingProfilerIntegration.start();
4999
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005000 // CloseGuard defaults to true and can be quite spammy. We
5001 // disable it here, but selectively enable it later (via
5002 // StrictMode) on debug builds, but using DropBox, not logs.
5003 CloseGuard.setEnabled(false);
5004
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005005 Environment.initForCurrentUser();
5006
Geremy Condra69689a72012-09-11 16:57:17 -07005007 // Set the reporter for event logging in libcore
5008 EventLogger.setReporter(new EventLoggingReporter());
5009
Kenny Root8b514752013-02-04 09:35:16 -08005010 Security.addProvider(new AndroidKeyStoreProvider());
5011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 Process.setArgV0("<pre-initialized>");
5013
5014 Looper.prepareMainLooper();
5015
5016 ActivityThread thread = new ActivityThread();
5017 thread.attach(false);
5018
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005019 if (sMainThreadHandler == null) {
5020 sMainThreadHandler = thread.getHandler();
5021 }
5022
Romain Guy5e9120d2012-01-30 12:17:22 -08005023 AsyncTask.init();
5024
Dianne Hackborn287952c2010-09-22 22:34:31 -07005025 if (false) {
5026 Looper.myLooper().setMessageLogging(new
5027 LogPrinter(Log.DEBUG, "ActivityThread"));
5028 }
5029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005030 Looper.loop();
5031
Jeff Brown10e89712011-07-08 18:52:57 -07005032 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033 }
5034}