blob: 02b94de925091183df8a66a421a127c2a19f09e2 [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
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070019import android.app.assist.AssistContent;
20import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070023import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ComponentName;
25import android.content.ContentProvider;
26import android.content.Context;
27import android.content.IContentProvider;
28import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.IPackageManager;
33import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070034import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ProviderInfo;
38import android.content.pm.ServiceInfo;
39import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070040import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.res.Configuration;
42import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070043import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.database.sqlite.SQLiteDatabase;
45import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080046import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.graphics.Bitmap;
48import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070049import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050050import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070051import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050052import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070053import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040054import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040055import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070056import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070057import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070058import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Bundle;
60import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070061import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.Looper;
66import android.os.Message;
67import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080068import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070069import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Process;
72import android.os.RemoteException;
73import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070074import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070076import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070077import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070078import android.os.UserHandle;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000079import android.provider.Settings;
Alex Klyubinf9034cc2015-02-12 11:43:09 -080080import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080081import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070083import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.util.DisplayMetrics;
85import android.util.EventLog;
86import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070087import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070088import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080089import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080090import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -070091import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -070092import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -080094import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.View;
96import android.view.ViewDebug;
97import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070098import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.Window;
100import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700101import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700102import android.renderscript.RenderScriptCacheDir;
Alex Klyubindcdaf872015-05-13 15:57:09 -0700103import android.security.keystore.AndroidKeyStoreProvider;
Calin Juravle69052392015-12-17 17:06:49 +0200104import android.system.Os;
105import android.system.OsConstants;
106import android.system.ErrnoException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700108import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700109import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800110import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111import com.android.internal.os.BinderInternal;
112import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700113import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700114import com.android.internal.os.SomeArgs;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700115import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700116import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100117import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700118import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120import java.io.File;
121import java.io.FileDescriptor;
122import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700123import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import java.io.PrintWriter;
125import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700126import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000127import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000129import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800132import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Geremy Condrab7faaf42012-09-19 18:07:42 -0700135import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700136import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800137import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400138import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800139import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700140import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700141import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700142import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700143
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700144final class RemoteServiceException extends AndroidRuntimeException {
145 public RemoteServiceException(String msg) {
146 super(msg);
147 }
148}
149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150/**
151 * This manages the execution of the main thread in an
152 * application process, scheduling and executing activities,
153 * broadcasts, and other operations on it as the activity
154 * manager requests.
155 *
156 * {@hide}
157 */
158public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700159 /** @hide */
160 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700161 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700162 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700163 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700164 /** @hide */
165 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700166 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700167 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700168 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800169 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700170 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700171 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700172 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800175 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
176 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
Dianne Hackborn455625e2015-01-21 09:55:13 -0800178 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
179 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
180 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
181 public static final int SERVICE_DONE_EXECUTING_START = 1;
182 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
183 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
184
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700185 // Details for pausing activity.
186 private static final int USER_LEAVING = 1;
187 private static final int DONT_REPORT = 2;
188
Filip Gruszczynskica664812015-12-04 12:43:36 -0800189 // Whether to invoke an activity callback after delivering new configuration.
190 private static final boolean REPORT_TO_ACTIVITY = true;
191
Jeff Browndefd4a62014-03-10 21:24:37 -0700192 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700193
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700194 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 final ApplicationThread mAppThread = new ApplicationThread();
197 final Looper mLooper = Looper.myLooper();
198 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700199 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 // List of new activities (via ActivityRecord.nextIdle) that should
201 // be reported when next we idle.
202 ActivityClientRecord mNewActivities = null;
203 // Number of activities that are currently visible on-screen.
204 int mNumVisibleActivities = 0;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700205 WeakReference<AssistStructure> mLastAssistStructure;
206 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700207 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700208 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700209 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700210 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700211 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700212 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700213 Application mInitialApplication;
214 final ArrayList<Application> mAllApplications
215 = new ArrayList<Application>();
216 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700217 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800218 /** Reference to singleton {@link ActivityThread} */
219 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700220 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700221 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700222 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700223 String[] mInstrumentationSplitAppDirs = null;
224 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700225 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700226 String[] mInstrumentedSplitAppDirs = null;
227 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700228 boolean mSystemThread = false;
229 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700230 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700232 // These can be accessed by multiple threads; mPackages is the lock.
233 // XXX For now we keep around information about all packages we have
234 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800235 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700236 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800237 // which means this lock gets held while the activity and window managers
238 // holds their own lock. Thus you MUST NEVER call back into the activity manager
239 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700240 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700241 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
242 = new ArrayMap<String, WeakReference<LoadedApk>>();
243 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
244 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700245 final ArrayList<ActivityClientRecord> mRelaunchingActivities
246 = new ArrayList<ActivityClientRecord>();
247 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700248 // Because we merge activity relaunch operations we can't depend on the ordering provided by
249 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
250 // us to drop certain events, if we know that they happened before relaunch we already executed.
251 // This represents the order of receiving the request from AM.
252 @GuardedBy("mResourcesManager")
253 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Craig Mautner88c05892013-06-28 09:47:45 -0700255 private final ResourcesManager mResourcesManager;
256
Jeff Sharkey6d515712012-09-20 16:06:08 -0700257 private static final class ProviderKey {
258 final String authority;
259 final int userId;
260
261 public ProviderKey(String authority, int userId) {
262 this.authority = authority;
263 this.userId = userId;
264 }
265
266 @Override
267 public boolean equals(Object o) {
268 if (o instanceof ProviderKey) {
269 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800270 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700271 }
272 return false;
273 }
274
275 @Override
276 public int hashCode() {
277 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
278 }
279 }
280
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700281 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700282 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
283 = new ArrayMap<ProviderKey, ProviderClientRecord>();
284 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
285 = new ArrayMap<IBinder, ProviderRefCount>();
286 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
287 = new ArrayMap<IBinder, ProviderClientRecord>();
288 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
289 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700291 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700292 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600293
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700294 final GcIdler mGcIdler = new GcIdler();
295 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700297 static Handler sMainThreadHandler; // set once in main()
298
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800299 Bundle mCoreSettings = null;
300
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400301 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700303 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800305 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700306 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700308 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 Activity activity;
310 Window window;
311 Activity parent;
312 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700313 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 boolean paused;
315 boolean stopped;
316 boolean hideForNow;
317 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700318 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800319 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800320 // Used for consolidating configs before sending on to Activity.
321 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700322 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
Jeff Hao1b012d32014-08-20 10:35:34 -0700324 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400327 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700328 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
330 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800331 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 boolean startsNotResumed;
334 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800335 int pendingConfigChanges;
336 boolean onlyLocalRequest;
337
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700338 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800339 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700340 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700342 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
343 // can be compared with other lifecycle operations.
344 int relaunchSeq = 0;
345
346 // Can only be accessed from the UI thread. This represents the latest processed message
347 // that is related to lifecycle events/
348 int lastProcessedSeq = 0;
349
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700350 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 parent = null;
352 embeddedID = null;
353 paused = false;
354 stopped = false;
355 hideForNow = false;
356 nextIdle = null;
357 }
358
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800359 public boolean isPreHoneycomb() {
360 if (activity != null) {
361 return activity.getApplicationInfo().targetSdkVersion
362 < android.os.Build.VERSION_CODES.HONEYCOMB;
363 }
364 return false;
365 }
366
Craig Mautnera0026042014-04-23 11:45:37 -0700367 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700368 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700369 }
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700372 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 return "ActivityRecord{"
374 + Integer.toHexString(System.identityHashCode(this))
375 + " token=" + token + " " + (componentName == null
376 ? "no component name" : componentName.toShortString())
377 + "}";
378 }
379 }
380
Wale Ogunwale1d646122015-04-24 14:45:14 -0700381 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700382 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 final IContentProvider mProvider;
384 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700385 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700387 ProviderClientRecord(String[] names, IContentProvider provider,
388 ContentProvider localProvider,
389 IActivityManager.ContentProviderHolder holder) {
390 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 mProvider = provider;
392 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700393 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 }
395 }
396
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400397 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800398 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 IBinder token;
400 public String toString() {
401 return "NewIntentData{intents=" + intents + " token=" + token + "}";
402 }
403 }
404
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400405 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700406 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700407 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
408 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800409 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700410 this.intent = intent;
411 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 Intent intent;
414 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400415 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 public String toString() {
417 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700418 info.packageName + " resultCode=" + getResultCode()
419 + " resultData=" + getResultData() + " resultExtras="
420 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 }
422 }
423
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400424 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700425 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400426 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700427 int backupMode;
428 public String toString() {
429 return "CreateBackupAgentData{appInfo=" + appInfo
430 + " backupAgent=" + appInfo.backupAgentName
431 + " mode=" + backupMode + "}";
432 }
433 }
Bob Leee5408332009-09-04 18:31:17 -0700434
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 IBinder token;
437 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400438 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 Intent intent;
440 public String toString() {
441 return "CreateServiceData{token=" + token + " className="
442 + info.name + " packageName=" + info.packageName
443 + " intent=" + intent + "}";
444 }
445 }
446
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400447 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 IBinder token;
449 Intent intent;
450 boolean rebind;
451 public String toString() {
452 return "BindServiceData{token=" + token + " intent=" + intent + "}";
453 }
454 }
455
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700458 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700460 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 Intent args;
462 public String toString() {
463 return "ServiceArgsData{token=" + token + " startId=" + startId
464 + " args=" + args + "}";
465 }
466 }
467
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400468 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700469 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 String processName;
471 ApplicationInfo appInfo;
472 List<ProviderInfo> providers;
473 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 Bundle instrumentationArgs;
475 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800476 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400478 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700479 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700480 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700481 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400483 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700484
485 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700486 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 public String toString() {
489 return "AppBindData{appInfo=" + appInfo + "}";
490 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700491 }
492
493 static final class Profiler {
494 String profileFile;
495 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700496 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700497 boolean autoStopProfiler;
498 boolean profiling;
499 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700500 public void setProfiler(ProfilerInfo profilerInfo) {
501 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700502 if (profiling) {
503 if (fd != null) {
504 try {
505 fd.close();
506 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700507 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700508 }
509 }
510 return;
511 }
512 if (profileFd != null) {
513 try {
514 profileFd.close();
515 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700516 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700517 }
518 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700519 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700520 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700521 samplingInterval = profilerInfo.samplingInterval;
522 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700523 }
524 public void startProfiling() {
525 if (profileFd == null || profiling) {
526 return;
527 }
528 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700529 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
530 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700531 profiling = true;
532 } catch (RuntimeException e) {
533 Slog.w(TAG, "Profiling failed on path " + profileFile);
534 try {
535 profileFd.close();
536 profileFd = null;
537 } catch (IOException e2) {
538 Slog.w(TAG, "Failure closing profile fd", e2);
539 }
540 }
541 }
542 public void stopProfiling() {
543 if (profiling) {
544 profiling = false;
545 Debug.stopMethodTracing();
546 if (profileFd != null) {
547 try {
548 profileFd.close();
549 } catch (IOException e) {
550 }
551 }
552 profileFd = null;
553 profileFile = null;
554 }
555 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 }
557
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400558 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700559 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700560 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800561 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400565 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 IBinder token;
567 List<ResultInfo> results;
568 public String toString() {
569 return "ResultData{token=" + token + " results" + results + "}";
570 }
571 }
572
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400573 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800574 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 String what;
576 String who;
577 }
578
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400579 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700580 String path;
581 ParcelFileDescriptor fd;
582 }
583
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400584 static final class UpdateCompatibilityData {
585 String pkg;
586 CompatibilityInfo info;
587 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800588
Adam Skorydfc7fd72013-08-05 19:23:41 -0700589 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800590 IBinder activityToken;
591 IBinder requestToken;
592 int requestType;
593 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700594
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800595 static final class ActivityConfigChangeData {
596 final IBinder activityToken;
597 final Configuration overrideConfig;
598 public ActivityConfigChangeData(IBinder token, Configuration config) {
599 activityToken = token;
600 overrideConfig = config;
601 }
602 }
603
Romain Guy65b345f2011-07-27 18:51:50 -0700604 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700605
Romain Guy65b345f2011-07-27 18:51:50 -0700606 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700607 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700608
Dianne Hackborna413dc02013-07-12 12:02:55 -0700609 private int mLastProcessState = -1;
610
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700611 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700612 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700613 if (mPendingConfiguration == null ||
614 mPendingConfiguration.isOtherSeqNewer(config)) {
615 mPendingConfiguration = config;
616 }
617 }
618 }
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700621 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700622 int seq = getLifecycleSeq();
623 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
624 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700625 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
627 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700628 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
629 configChanges,
630 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 }
632
633 public final void scheduleStopActivity(IBinder token, boolean showWindow,
634 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700635 int seq = getLifecycleSeq();
636 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
637 + " operation received seq: " + seq);
638 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700640 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 }
642
643 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700644 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
646 token);
647 }
648
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800649 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700650 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800651 }
652
Dianne Hackborna413dc02013-07-12 12:02:55 -0700653 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800654 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700655 int seq = getLifecycleSeq();
656 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
657 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700658 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700659 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 }
661
662 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
663 ResultData res = new ResultData();
664 res.token = token;
665 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700666 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668
669 // we use token to identify this activity without having to send the
670 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800671 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700672 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800673 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
674 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
675 int procState, Bundle state, PersistableBundle persistentState,
676 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
677 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700678
679 updateProcessState(procState, false);
680
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700681 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682
683 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700684 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800686 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700687 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400689 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700691 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692
693 r.pendingResults = pendingResults;
694 r.pendingIntents = pendingNewIntents;
695
696 r.startsNotResumed = notResumed;
697 r.isForward = isForward;
698
Jeff Hao1b012d32014-08-20 10:35:34 -0700699 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700700
Wale Ogunwale60454db2015-01-23 16:05:07 -0800701 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700702 updatePendingConfiguration(curConfig);
703
Jeff Brown9ef09972013-10-15 20:49:59 -0700704 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 }
706
Wale Ogunwale60454db2015-01-23 16:05:07 -0800707 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800709 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800710 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700711 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800712 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700713 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714 }
715
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800716 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 NewIntentData data = new NewIntentData();
718 data.intents = intents;
719 data.token = token;
720
Jeff Brown9ef09972013-10-15 20:49:59 -0700721 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723
724 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
725 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700726 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 configChanges);
728 }
729
730 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400731 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700732 boolean sync, int sendingUser, int processState) {
733 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700734 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700735 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400737 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700738 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400741 public final void scheduleCreateBackupAgent(ApplicationInfo app,
742 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700743 CreateBackupAgentData d = new CreateBackupAgentData();
744 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400745 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700746 d.backupMode = backupMode;
747
Jeff Brown9ef09972013-10-15 20:49:59 -0700748 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700749 }
750
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400751 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
752 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700753 CreateBackupAgentData d = new CreateBackupAgentData();
754 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400755 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700756
Jeff Brown9ef09972013-10-15 20:49:59 -0700757 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700758 }
759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700761 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
762 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 CreateServiceData s = new CreateServiceData();
764 s.token = token;
765 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400766 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767
Jeff Brown9ef09972013-10-15 20:49:59 -0700768 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770
771 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700772 boolean rebind, int processState) {
773 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 BindServiceData s = new BindServiceData();
775 s.token = token;
776 s.intent = intent;
777 s.rebind = rebind;
778
Amith Yamasani742a6712011-05-04 14:49:28 -0700779 if (DEBUG_SERVICE)
780 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
781 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700782 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
785 public final void scheduleUnbindService(IBinder token, Intent intent) {
786 BindServiceData s = new BindServiceData();
787 s.token = token;
788 s.intent = intent;
789
Jeff Brown9ef09972013-10-15 20:49:59 -0700790 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 }
792
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700793 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700794 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 ServiceArgsData s = new ServiceArgsData();
796 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700797 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700799 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 s.args = args;
801
Jeff Brown9ef09972013-10-15 20:49:59 -0700802 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
805 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700806 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 }
808
Jeff Hao1b012d32014-08-20 10:35:34 -0700809 public final void bindApplication(String processName, ApplicationInfo appInfo,
810 List<ProviderInfo> providers, ComponentName instrumentationName,
811 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
812 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800813 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700814 boolean enableBinderTracking, boolean trackAllocation,
815 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
816 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817
818 if (services != null) {
819 // Setup the service cache in the ServiceManager
820 ServiceManager.initServiceCache(services);
821 }
822
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800823 setCoreSettings(coreSettings);
824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 AppBindData data = new AppBindData();
826 data.processName = processName;
827 data.appInfo = appInfo;
828 data.providers = providers;
829 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 data.instrumentationArgs = instrumentationArgs;
831 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800832 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400834 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700835 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700836 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700837 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400839 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700840 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700841 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 }
843
844 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700845 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 }
847
Christopher Tate5e1ab332009-09-01 20:32:49 -0700848 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700849 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700850 }
851
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700853 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700854 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 }
856
857 public void updateTimeZone() {
858 TimeZone.setDefault(null);
859 }
860
Robert Greenwalt03595d02010-11-02 14:08:23 -0700861 public void clearDnsCache() {
862 // a non-standard API to get this to libcore
863 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400864 // Allow libcore to perform the necessary actions as it sees fit upon a network
865 // configuration change.
866 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700867 }
868
Jason Monk83520b92014-05-09 15:16:06 -0400869 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400870 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
871 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500872 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400873 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500874 } else {
875 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
876 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700877 }
878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 public void processInBackground() {
880 mH.removeMessages(H.GC_WHEN_IDLE);
881 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
882 }
883
884 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700885 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700886 try {
887 data.fd = ParcelFileDescriptor.dup(fd);
888 data.token = servicetoken;
889 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700890 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700891 } catch (IOException e) {
892 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894 }
895
896 // This function exists to make sure all receiver dispatching is
897 // correctly ordered, since these are one-way calls and the binder driver
898 // applies transaction ordering per object for such calls.
899 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700900 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700901 boolean sticky, int sendingUser, int processState) throws RemoteException {
902 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700903 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
904 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
Bob Leee5408332009-09-04 18:31:17 -0700906
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800907 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700909 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
911
Wale Ogunwale60454db2015-01-23 16:05:07 -0800912 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800913 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800914 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800915 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800916 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 }
918
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800919 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700920 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
921 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800922 }
923
Andy McFadden824c5102010-07-09 16:26:57 -0700924 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
925 DumpHeapData dhd = new DumpHeapData();
926 dhd.path = path;
927 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700928 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700929 }
930
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700931 public void setSchedulingGroup(int group) {
932 // Note: do this immediately, since going into the foreground
933 // should happen regardless of what pending work we have to do
934 // and the activity manager will wait for us to report back that
935 // we are done before sending us to the background.
936 try {
937 Process.setProcessGroup(Process.myPid(), group);
938 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800939 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700940 }
941 }
Bob Leee5408332009-09-04 18:31:17 -0700942
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700943 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700944 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700945 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700946
947 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700948 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700949 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700950
Dianne Hackborn30d71892010-12-11 10:37:55 -0800951 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
952 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700953 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700954 try {
955 data.fd = ParcelFileDescriptor.dup(fd);
956 data.token = activitytoken;
957 data.prefix = prefix;
958 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700959 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700960 } catch (IOException e) {
961 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700962 }
963 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700964
Marco Nelissen18cb2872011-11-15 11:19:53 -0800965 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
966 String[] args) {
967 DumpComponentInfo data = new DumpComponentInfo();
968 try {
969 data.fd = ParcelFileDescriptor.dup(fd);
970 data.token = providertoken;
971 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700972 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800973 } catch (IOException e) {
974 Slog.w(TAG, "dumpProvider failed", e);
975 }
976 }
977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700979 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700980 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700981 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700982 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700983 try {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700984 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700985 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700986 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700987 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700988 }
989
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700990 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700991 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 long nativeMax = Debug.getNativeHeapSize() / 1024;
993 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
994 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -0700997 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 long dalvikMax = runtime.totalMemory() / 1024;
999 long dalvikFree = runtime.freeMemory() / 1024;
1000 long dalvikAllocated = dalvikMax - dalvikFree;
1001 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001002 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001003 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1004 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001005 int globalAssetCount = AssetManager.getGlobalAssetCount();
1006 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1007 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1008 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1009 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001010 long parcelSize = Parcel.getGlobalAllocSize();
1011 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001012 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001013 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001014
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001015 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1016 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001017 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1018 nativeMax, nativeAllocated, nativeFree,
1019 dalvikMax, dalvikAllocated, dalvikFree);
1020
Dianne Hackbornb437e092011-08-05 17:50:29 -07001021 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 // NOTE: if you change anything significant below, also consider changing
1023 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 // Object counts
1026 pw.print(viewInstanceCount); pw.print(',');
1027 pw.print(viewRootInstanceCount); pw.print(',');
1028 pw.print(appContextInstanceCount); pw.print(',');
1029 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 pw.print(globalAssetCount); pw.print(',');
1032 pw.print(globalAssetManagerCount); pw.print(',');
1033 pw.print(binderLocalObjectCount); pw.print(',');
1034 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 pw.print(binderDeathObjectCount); pw.print(',');
1037 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001040 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001041 pw.print(stats.memoryUsed / 1024); pw.print(',');
1042 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001043 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001044 for (int i = 0; i < stats.dbStats.size(); i++) {
1045 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001046 pw.print(','); pw.print(dbStats.dbName);
1047 pw.print(','); pw.print(dbStats.pageSize);
1048 pw.print(','); pw.print(dbStats.dbSize);
1049 pw.print(','); pw.print(dbStats.lookaside);
1050 pw.print(','); pw.print(dbStats.cache);
1051 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001052 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001053 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001054
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001055 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
Bob Leee5408332009-09-04 18:31:17 -07001057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 pw.println(" ");
1059 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001060 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 viewRootInstanceCount);
1062
1063 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1064 "Activities:", activityInstanceCount);
1065
1066 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1067 "AssetManagers:", globalAssetManagerCount);
1068
1069 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1070 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001071 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1072 "Parcel count:", parcelCount);
1073 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1074 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 // SQLite mem info
1077 pw.println(" ");
1078 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001079 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1080 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1081 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001082 pw.println(" ");
1083 int N = stats.dbStats.size();
1084 if (N > 0) {
1085 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001086 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1087 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001088 for (int i = 0; i < N; i++) {
1089 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001090 printRow(pw, DB_INFO_FORMAT,
1091 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1092 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1093 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1094 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001095 }
1096 }
Bob Leee5408332009-09-04 18:31:17 -07001097
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001098 // Asset details.
1099 String assetAlloc = AssetManager.getAssetAllocations();
1100 if (assetAlloc != null) {
1101 pw.println(" ");
1102 pw.println(" Asset Allocations");
1103 pw.print(assetAlloc);
1104 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001105 }
1106
1107 @Override
1108 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1109 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001110 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001113 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001114 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001115 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1116 SQLiteDebug.dump(printer, args);
1117 pw.flush();
1118 }
1119
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001120 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001121 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1122 if (mSystemThread) {
1123 // Ensure this invocation is asynchronous to prevent
1124 // writer waiting due to buffer cannot be consumed.
1125 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1126 @Override
1127 public void run() {
1128 dumpDatabaseInfo(fd, args);
1129 }
1130 });
1131 } else {
1132 dumpDatabaseInfo(fd, args);
1133 }
1134 }
1135
1136 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001137 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001138 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001139 }
1140
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001141 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001142 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001143 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001144 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001145 cmd.activityToken = activityToken;
1146 cmd.requestToken = requestToken;
1147 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001148 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001149 }
1150
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001151 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001152 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001153 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001154
1155 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1156 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1157 ucd.pkg = pkg;
1158 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001159 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001160 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001161
1162 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001163 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001164 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001165
Craig Mautner5eda9b32013-07-02 11:58:16 -07001166 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001167 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001168 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001169
Craig Mautnereb8abf72014-07-02 15:04:09 -07001170 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1171 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1172 new Pair<IBinder, ActivityOptions>(token, options));
1173 }
1174
Dianne Hackborna413dc02013-07-12 12:02:55 -07001175 public void setProcessState(int state) {
1176 updateProcessState(state, true);
1177 }
1178
1179 public void updateProcessState(int processState, boolean fromIpc) {
1180 synchronized (this) {
1181 if (mLastProcessState != processState) {
1182 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001183 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1184 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1185 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1186 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1187 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1188 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1189 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1190 }
1191 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001192 if (false) {
1193 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1194 + (fromIpc ? " (from ipc": ""));
1195 }
1196 }
1197 }
1198 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001199
1200 @Override
1201 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001202 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001203 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001204
1205 @Override
1206 public final void updateTimePrefs(boolean is24Hour) {
1207 DateFormat.set24HourTimePref(is24Hour);
1208 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001209
1210 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001211 public void scheduleCancelVisibleBehind(IBinder token) {
1212 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001213 }
1214
1215 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001216 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1217 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001218 }
Craig Mautner8746a472014-07-24 15:12:54 -07001219
Jeff Sharkey605eb792014-11-04 13:34:06 -08001220 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001221 public void scheduleEnterAnimationComplete(IBinder token) {
1222 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1223 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001224
1225 @Override
1226 public void notifyCleartextNetwork(byte[] firstPacket) {
1227 if (StrictMode.vmCleartextNetworkEnabled()) {
1228 StrictMode.onCleartextNetworkDetected(firstPacket);
1229 }
1230 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001231
1232 @Override
1233 public void startBinderTracking() {
1234 sendMessage(H.START_BINDER_TRACKING, null);
1235 }
1236
1237 @Override
1238 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1239 try {
1240 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1241 } catch (IOException e) {
1242 }
1243 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001244
1245 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001246 public void scheduleMultiWindowChanged(IBinder token, boolean inMultiWindow)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001247 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001248 sendMessage(H.MULTI_WINDOW_CHANGED, token, inMultiWindow ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001249 }
1250
1251 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001252 public void schedulePictureInPictureChanged(IBinder token, boolean inPip)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001253 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001254 sendMessage(H.PICTURE_IN_PICTURE_CHANGED, token, inPip ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001255 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001256
1257 @Override
1258 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1259 IVoiceInteractor voiceInteractor) throws RemoteException {
1260 SomeArgs args = SomeArgs.obtain();
1261 args.arg1 = token;
1262 args.arg2 = voiceInteractor;
1263 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
1266
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001267 private int getLifecycleSeq() {
1268 synchronized (mResourcesManager) {
1269 return mLifecycleSeq++;
1270 }
1271 }
1272
Romain Guy65b345f2011-07-27 18:51:50 -07001273 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 public static final int LAUNCH_ACTIVITY = 100;
1275 public static final int PAUSE_ACTIVITY = 101;
1276 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1277 public static final int STOP_ACTIVITY_SHOW = 103;
1278 public static final int STOP_ACTIVITY_HIDE = 104;
1279 public static final int SHOW_WINDOW = 105;
1280 public static final int HIDE_WINDOW = 106;
1281 public static final int RESUME_ACTIVITY = 107;
1282 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001283 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public static final int BIND_APPLICATION = 110;
1285 public static final int EXIT_APPLICATION = 111;
1286 public static final int NEW_INTENT = 112;
1287 public static final int RECEIVER = 113;
1288 public static final int CREATE_SERVICE = 114;
1289 public static final int SERVICE_ARGS = 115;
1290 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 public static final int CONFIGURATION_CHANGED = 118;
1293 public static final int CLEAN_UP_CONTEXT = 119;
1294 public static final int GC_WHEN_IDLE = 120;
1295 public static final int BIND_SERVICE = 121;
1296 public static final int UNBIND_SERVICE = 122;
1297 public static final int DUMP_SERVICE = 123;
1298 public static final int LOW_MEMORY = 124;
1299 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1300 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001301 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001302 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001303 public static final int DESTROY_BACKUP_AGENT = 129;
1304 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001305 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001306 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001307 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001308 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001309 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001310 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001311 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001312 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001313 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001314 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001315 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001316 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001317 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001318 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001319 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001320 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001321 public static final int CANCEL_VISIBLE_BEHIND = 147;
1322 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001323 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001324 public static final int START_BINDER_TRACKING = 150;
1325 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001326 public static final int MULTI_WINDOW_CHANGED = 152;
1327 public static final int PICTURE_IN_PICTURE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001328 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001331 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 switch (code) {
1333 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1334 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1335 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1336 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1337 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1338 case SHOW_WINDOW: return "SHOW_WINDOW";
1339 case HIDE_WINDOW: return "HIDE_WINDOW";
1340 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1341 case SEND_RESULT: return "SEND_RESULT";
1342 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1343 case BIND_APPLICATION: return "BIND_APPLICATION";
1344 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1345 case NEW_INTENT: return "NEW_INTENT";
1346 case RECEIVER: return "RECEIVER";
1347 case CREATE_SERVICE: return "CREATE_SERVICE";
1348 case SERVICE_ARGS: return "SERVICE_ARGS";
1349 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1351 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1352 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1353 case BIND_SERVICE: return "BIND_SERVICE";
1354 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1355 case DUMP_SERVICE: return "DUMP_SERVICE";
1356 case LOW_MEMORY: return "LOW_MEMORY";
1357 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1358 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001359 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001360 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1361 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001362 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001363 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001364 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001365 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001366 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001367 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001368 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001369 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001370 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001371 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001372 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001373 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001374 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001375 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001376 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001377 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001378 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001379 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1380 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001381 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001382 case MULTI_WINDOW_CHANGED: return "MULTI_WINDOW_CHANGED";
1383 case PICTURE_IN_PICTURE_CHANGED: return "PICTURE_IN_PICTURE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001384 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 }
1386 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001387 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001388 }
1389 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001390 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 switch (msg.what) {
1392 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001393 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001394 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395
1396 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001397 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001398 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001399 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 } break;
1401 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001402 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001403 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001404 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001405 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001407 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001408 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001409 SomeArgs args = (SomeArgs) msg.obj;
1410 handlePauseActivity((IBinder) args.arg1, false,
1411 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1412 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001413 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001414 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001415 } break;
1416 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001417 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001418 SomeArgs args = (SomeArgs) msg.obj;
1419 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1420 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001421 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001422 } break;
1423 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001424 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001425 SomeArgs args = (SomeArgs) msg.obj;
1426 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001427 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001428 } break;
1429 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001431 SomeArgs args = (SomeArgs) msg.obj;
1432 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001433 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001434 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001436 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001438 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 break;
1440 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001441 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 break;
1445 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001446 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001447 SomeArgs args = (SomeArgs) msg.obj;
1448 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
1449 args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001450 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 break;
1452 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001453 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001455 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 break;
1457 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001458 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1460 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001461 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 break;
1463 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001464 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 AppBindData data = (AppBindData)msg.obj;
1466 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001467 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 break;
1469 case EXIT_APPLICATION:
1470 if (mInitialApplication != null) {
1471 mInitialApplication.onTerminate();
1472 }
1473 Looper.myLooper().quit();
1474 break;
1475 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001476 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001478 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 break;
1480 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001481 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001483 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001484 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 break;
1486 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001487 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001489 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 break;
1491 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001492 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001494 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 break;
1496 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001497 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001499 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 break;
1501 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001502 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001504 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 break;
1506 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001507 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001509 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001510 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001513 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001514 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001515 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001516 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 break;
1518 case CLEAN_UP_CONTEXT:
1519 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1520 cci.context.performFinalCleanup(cci.who, cci.what);
1521 break;
1522 case GC_WHEN_IDLE:
1523 scheduleGcIdler();
1524 break;
1525 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001526 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 break;
1528 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001529 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001531 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532 break;
1533 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001534 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001535 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1536 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001537 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001539 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001540 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001541 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001542 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001543 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001544 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001545 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001546 break;
1547 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001548 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001549 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001550 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001551 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001552 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001553 Process.killProcess(Process.myPid());
1554 break;
1555 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001556 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001557 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001558 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001559 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001560 case ENABLE_JIT:
1561 ensureJitEnabled();
1562 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001563 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001564 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001565 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001566 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001567 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001568 case SCHEDULE_CRASH:
1569 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001570 case DUMP_HEAP:
1571 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1572 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001573 case DUMP_ACTIVITY:
1574 handleDumpActivity((DumpComponentInfo)msg.obj);
1575 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001576 case DUMP_PROVIDER:
1577 handleDumpProvider((DumpComponentInfo)msg.obj);
1578 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001579 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001580 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001581 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001582 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001583 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001584 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001585 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001586 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001587 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001588 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001589 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1590 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001591 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001592 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001593 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001594 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001595 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001596 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001597 case UNSTABLE_PROVIDER_DIED:
1598 handleUnstableProviderDied((IBinder)msg.obj, false);
1599 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001600 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1601 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001602 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001603 case TRANSLUCENT_CONVERSION_COMPLETE:
1604 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1605 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001606 case INSTALL_PROVIDER:
1607 handleInstallProvider((ProviderInfo) msg.obj);
1608 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001609 case ON_NEW_ACTIVITY_OPTIONS:
1610 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1611 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001612 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001613 case CANCEL_VISIBLE_BEHIND:
1614 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001615 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001616 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1617 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001618 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001619 case ENTER_ANIMATION_COMPLETE:
1620 handleEnterAnimationComplete((IBinder) msg.obj);
1621 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001622 case START_BINDER_TRACKING:
1623 handleStartBinderTracking();
1624 break;
1625 case STOP_BINDER_TRACKING_AND_DUMP:
1626 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1627 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001628 case MULTI_WINDOW_CHANGED:
1629 handleMultiWindowChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001630 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001631 case PICTURE_IN_PICTURE_CHANGED:
1632 handlePictureInPictureChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001633 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001634 case LOCAL_VOICE_INTERACTION_STARTED:
1635 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1636 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1637 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001639 Object obj = msg.obj;
1640 if (obj instanceof SomeArgs) {
1641 ((SomeArgs) obj).recycle();
1642 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001643 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
Bob Leee5408332009-09-04 18:31:17 -07001645
Brian Carlstromed7e0072011-03-24 13:27:57 -07001646 private void maybeSnapshot() {
1647 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001648 // convert the *private* ActivityThread.PackageInfo to *public* known
1649 // android.content.pm.PackageInfo
1650 String packageName = mBoundApplication.info.mPackageName;
1651 android.content.pm.PackageInfo packageInfo = null;
1652 try {
1653 Context context = getSystemContext();
1654 if(context == null) {
1655 Log.e(TAG, "cannot get a valid context");
1656 return;
1657 }
1658 PackageManager pm = context.getPackageManager();
1659 if(pm == null) {
1660 Log.e(TAG, "cannot get a valid PackageManager");
1661 return;
1662 }
1663 packageInfo = pm.getPackageInfo(
1664 packageName, PackageManager.GET_ACTIVITIES);
1665 } catch (NameNotFoundException e) {
1666 Log.e(TAG, "cannot get package info for " + packageName, e);
1667 }
1668 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001669 }
1670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
1672
Romain Guy65b345f2011-07-27 18:51:50 -07001673 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001674 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001676 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001677 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001678 if (mBoundApplication != null && mProfiler.profileFd != null
1679 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001680 stopProfiling = true;
1681 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 if (a != null) {
1683 mNewActivities = null;
1684 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001685 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001687 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 TAG, "Reporting idle of " + a +
1689 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001690 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 if (a.activity != null && !a.activity.mFinished) {
1692 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001693 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001694 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001696 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 }
1698 }
1699 prev = a;
1700 a = a.nextIdle;
1701 prev.nextIdle = null;
1702 } while (a != null);
1703 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001704 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001705 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001706 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001707 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 return false;
1709 }
1710 }
1711
1712 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001713 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001714 public final boolean queueIdle() {
1715 doGcIfNeeded();
1716 return false;
1717 }
1718 }
1719
Romain Guy65b345f2011-07-27 18:51:50 -07001720 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001721 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001724 public static boolean isSystem() {
1725 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1726 }
1727
Svetoslavfbf0eca2015-05-01 16:52:41 -07001728 public static String currentOpPackageName() {
1729 ActivityThread am = currentActivityThread();
1730 return (am != null && am.getApplication() != null)
1731 ? am.getApplication().getOpPackageName() : null;
1732 }
1733
Romain Guy65b345f2011-07-27 18:51:50 -07001734 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001735 ActivityThread am = currentActivityThread();
1736 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001737 ? am.mBoundApplication.appInfo.packageName : null;
1738 }
1739
1740 public static String currentProcessName() {
1741 ActivityThread am = currentActivityThread();
1742 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001743 ? am.mBoundApplication.processName : null;
1744 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745
Romain Guy65b345f2011-07-27 18:51:50 -07001746 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001747 ActivityThread am = currentActivityThread();
1748 return am != null ? am.mInitialApplication : null;
1749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001751 public static IPackageManager getPackageManager() {
1752 if (sPackageManager != null) {
1753 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1754 return sPackageManager;
1755 }
1756 IBinder b = ServiceManager.getService("package");
1757 //Slog.v("PackageManager", "default service binder = " + b);
1758 sPackageManager = IPackageManager.Stub.asInterface(b);
1759 //Slog.v("PackageManager", "default service = " + sPackageManager);
1760 return sPackageManager;
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762
Romain Guy65b345f2011-07-27 18:51:50 -07001763 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001764 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1765 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001766 if (config == null) {
1767 return null;
1768 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001769 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001770 mMainThreadConfig.setTo(config);
1771 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001772 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001773 }
1774 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001777 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001778 * Creates the top level resources for the given package.
1779 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001780 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1781 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001782 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001783 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
Alan Viverette02fc5fe2015-08-27 13:16:09 -04001784 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(),
1785 pkgInfo.getClassLoader());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001786 }
1787
1788 final Handler getHandler() {
1789 return mH;
1790 }
1791
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001792 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1793 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001794 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1795 }
1796
1797 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1798 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001799 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001800 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001801 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001802 if (differentUser) {
1803 // Caching not supported across users
1804 ref = null;
1805 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 ref = mPackages.get(packageName);
1807 } else {
1808 ref = mResourcePackages.get(packageName);
1809 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001810
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001811 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001812 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001813 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1814 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 if (packageInfo != null && (packageInfo.mResources == null
1816 || packageInfo.mResources.getAssets().isUpToDate())) {
1817 if (packageInfo.isSecurityViolation()
1818 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1819 throw new SecurityException(
1820 "Requesting code from " + packageName
1821 + " to be run in process "
1822 + mBoundApplication.processName
1823 + "/" + mBoundApplication.appInfo.uid);
1824 }
1825 return packageInfo;
1826 }
1827 }
1828
1829 ApplicationInfo ai = null;
1830 try {
1831 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001832 PackageManager.GET_SHARED_LIBRARY_FILES
1833 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1834 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001836 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838
1839 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001840 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842
1843 return null;
1844 }
1845
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001846 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1847 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1849 boolean securityViolation = includeCode && ai.uid != 0
1850 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001851 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001852 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001853 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1855 |Context.CONTEXT_IGNORE_SECURITY))
1856 == Context.CONTEXT_INCLUDE_CODE) {
1857 if (securityViolation) {
1858 String msg = "Requesting code from " + ai.packageName
1859 + " (with uid " + ai.uid + ")";
1860 if (mBoundApplication != null) {
1861 msg = msg + " to be run in process "
1862 + mBoundApplication.processName + " (with uid "
1863 + mBoundApplication.appInfo.uid + ")";
1864 }
1865 throw new SecurityException(msg);
1866 }
1867 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001868 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1869 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001872 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1873 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001874 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001877 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001878 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001879 WeakReference<LoadedApk> ref;
1880 if (includeCode) {
1881 ref = mPackages.get(packageName);
1882 } else {
1883 ref = mResourcePackages.get(packageName);
1884 }
1885 return ref != null ? ref.get() : null;
1886 }
1887 }
1888
Romain Guy65b345f2011-07-27 18:51:50 -07001889 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001890 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1891 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001892 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001893 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001894 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001895 if (differentUser) {
1896 // Caching not supported across users
1897 ref = null;
1898 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899 ref = mPackages.get(aInfo.packageName);
1900 } else {
1901 ref = mResourcePackages.get(aInfo.packageName);
1902 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001903
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001904 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 if (packageInfo == null || (packageInfo.mResources != null
1906 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001907 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 : "Loading resource-only package ") + aInfo.packageName
1909 + " (in " + (mBoundApplication != null
1910 ? mBoundApplication.processName : null)
1911 + ")");
1912 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001913 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001915 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001916
1917 if (mSystemThread && "android".equals(aInfo.packageName)) {
1918 packageInfo.installSystemApplicationInfo(aInfo,
1919 getSystemContext().mPackageInfo.getClassLoader());
1920 }
1921
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001922 if (differentUser) {
1923 // Caching not supported across users
1924 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001926 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 } else {
1928 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001929 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 }
1931 }
1932 return packageInfo;
1933 }
1934 }
1935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001937 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
1940 public ApplicationThread getApplicationThread()
1941 {
1942 return mAppThread;
1943 }
1944
1945 public Instrumentation getInstrumentation()
1946 {
1947 return mInstrumentation;
1948 }
1949
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001951 return mProfiler != null && mProfiler.profileFile != null
1952 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954
1955 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001956 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
1959 public Looper getLooper() {
1960 return mLooper;
1961 }
1962
1963 public Application getApplication() {
1964 return mInitialApplication;
1965 }
Bob Leee5408332009-09-04 18:31:17 -07001966
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001967 public String getProcessName() {
1968 return mBoundApplication.processName;
1969 }
Bob Leee5408332009-09-04 18:31:17 -07001970
Dianne Hackborn21556372010-02-04 16:34:40 -08001971 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 synchronized (this) {
1973 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001974 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001976 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979
Narayan Kamath29564cd2014-08-07 10:57:40 +01001980 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001981 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001982 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001983
1984 // give ourselves a default profiler
1985 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001986 }
1987 }
1988
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001989 void ensureJitEnabled() {
1990 if (!mJitEnabled) {
1991 mJitEnabled = true;
1992 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1993 }
1994 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 void scheduleGcIdler() {
1997 if (!mGcIdlerScheduled) {
1998 mGcIdlerScheduled = true;
1999 Looper.myQueue().addIdleHandler(mGcIdler);
2000 }
2001 mH.removeMessages(H.GC_WHEN_IDLE);
2002 }
2003
2004 void unscheduleGcIdler() {
2005 if (mGcIdlerScheduled) {
2006 mGcIdlerScheduled = false;
2007 Looper.myQueue().removeIdleHandler(mGcIdler);
2008 }
2009 mH.removeMessages(H.GC_WHEN_IDLE);
2010 }
2011
2012 void doGcIfNeeded() {
2013 mGcIdlerScheduled = false;
2014 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002015 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 // + "m now=" + now);
2017 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002018 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 BinderInternal.forceGc("bg");
2020 }
2021 }
2022
Dianne Hackborne77187d2013-10-25 16:32:41 -07002023 private static final String HEAP_FULL_COLUMN
2024 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2025 private static final String HEAP_COLUMN
2026 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002027 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2028 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2029 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002030
2031 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002032 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002033
2034 static void printRow(PrintWriter pw, String format, Object...objs) {
2035 pw.println(String.format(format, objs));
2036 }
2037
2038 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002039 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2040 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002041 long nativeMax, long nativeAllocated, long nativeFree,
2042 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2043
2044 // For checkin, we print one long comma-separated list of values
2045 if (checkin) {
2046 // NOTE: if you change anything significant below, also consider changing
2047 // ACTIVITY_THREAD_CHECKIN_VERSION.
2048
2049 // Header
2050 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2051 pw.print(pid); pw.print(',');
2052 pw.print(processName); pw.print(',');
2053
2054 // Heap info - max
2055 pw.print(nativeMax); pw.print(',');
2056 pw.print(dalvikMax); pw.print(',');
2057 pw.print("N/A,");
2058 pw.print(nativeMax + dalvikMax); pw.print(',');
2059
2060 // Heap info - allocated
2061 pw.print(nativeAllocated); pw.print(',');
2062 pw.print(dalvikAllocated); pw.print(',');
2063 pw.print("N/A,");
2064 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2065
2066 // Heap info - free
2067 pw.print(nativeFree); pw.print(',');
2068 pw.print(dalvikFree); pw.print(',');
2069 pw.print("N/A,");
2070 pw.print(nativeFree + dalvikFree); pw.print(',');
2071
2072 // Heap info - proportional set size
2073 pw.print(memInfo.nativePss); pw.print(',');
2074 pw.print(memInfo.dalvikPss); pw.print(',');
2075 pw.print(memInfo.otherPss); pw.print(',');
2076 pw.print(memInfo.getTotalPss()); pw.print(',');
2077
2078 // Heap info - swappable set size
2079 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2080 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2081 pw.print(memInfo.otherSwappablePss); pw.print(',');
2082 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2083
2084 // Heap info - shared dirty
2085 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2086 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2087 pw.print(memInfo.otherSharedDirty); pw.print(',');
2088 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2089
2090 // Heap info - shared clean
2091 pw.print(memInfo.nativeSharedClean); pw.print(',');
2092 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2093 pw.print(memInfo.otherSharedClean); pw.print(',');
2094 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2095
2096 // Heap info - private Dirty
2097 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2098 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2099 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2100 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2101
2102 // Heap info - private Clean
2103 pw.print(memInfo.nativePrivateClean); pw.print(',');
2104 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2105 pw.print(memInfo.otherPrivateClean); pw.print(',');
2106 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2107
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002108 // Heap info - swapped out
2109 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2110 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2111 pw.print(memInfo.otherSwappedOut); pw.print(',');
2112 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2113
2114 // Heap info - swapped out pss
2115 if (memInfo.hasSwappedOutPss) {
2116 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2117 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2118 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2119 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2120 } else {
2121 pw.print("N/A,");
2122 pw.print("N/A,");
2123 pw.print("N/A,");
2124 pw.print("N/A,");
2125 }
2126
Dianne Hackborne77187d2013-10-25 16:32:41 -07002127 // Heap info - other areas
2128 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2129 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2130 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2131 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2132 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2133 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2134 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2135 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002136 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2137 if (memInfo.hasSwappedOutPss) {
2138 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2139 } else {
2140 pw.print("N/A,");
2141 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002142 }
2143 return;
2144 }
2145
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002146 if (!dumpSummaryOnly) {
2147 if (dumpFullInfo) {
2148 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002149 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2150 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002151 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002152 "Clean", "Clean", "Dirty",
2153 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002154 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2155 "------", "------", "------", "------", "------", "------");
2156 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2157 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2158 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002159 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2160 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002161 nativeMax, nativeAllocated, nativeFree);
2162 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2163 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2164 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002165 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2166 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002167 dalvikMax, dalvikAllocated, dalvikFree);
2168 } else {
2169 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002170 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2171 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002172 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2173 "Clean", "Dirty", "Size", "Alloc", "Free");
2174 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2175 "------", "------", "------", "------", "------");
2176 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2177 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002178 memInfo.nativePrivateClean,
2179 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2180 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002181 nativeMax, nativeAllocated, nativeFree);
2182 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2183 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002184 memInfo.dalvikPrivateClean,
2185 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2186 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002187 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002188 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002189
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002190 int otherPss = memInfo.otherPss;
2191 int otherSwappablePss = memInfo.otherSwappablePss;
2192 int otherSharedDirty = memInfo.otherSharedDirty;
2193 int otherPrivateDirty = memInfo.otherPrivateDirty;
2194 int otherSharedClean = memInfo.otherSharedClean;
2195 int otherPrivateClean = memInfo.otherPrivateClean;
2196 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002197 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002198
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002199 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002200 final int myPss = memInfo.getOtherPss(i);
2201 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2202 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2203 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2204 final int mySharedClean = memInfo.getOtherSharedClean(i);
2205 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2206 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002207 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002208 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002209 || mySharedClean != 0 || myPrivateClean != 0
2210 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002211 if (dumpFullInfo) {
2212 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2213 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002214 mySharedClean, myPrivateClean,
2215 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2216 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002217 } else {
2218 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2219 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002220 myPrivateClean,
2221 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2222 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002223 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002224 otherPss -= myPss;
2225 otherSwappablePss -= mySwappablePss;
2226 otherSharedDirty -= mySharedDirty;
2227 otherPrivateDirty -= myPrivateDirty;
2228 otherSharedClean -= mySharedClean;
2229 otherPrivateClean -= myPrivateClean;
2230 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002231 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002232 }
2233 }
2234
2235 if (dumpFullInfo) {
2236 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2237 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002238 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2239 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002240 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2241 memInfo.getTotalSwappablePss(),
2242 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2243 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002244 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOut() :
2245 memInfo.getTotalSwappedOutPss(),
2246 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2247 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002248 } else {
2249 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002250 otherPrivateDirty, otherPrivateClean,
2251 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002252 "", "", "");
2253 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2254 memInfo.getTotalPrivateDirty(),
2255 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002256 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002257 memInfo.getTotalSwappedOut(),
2258 nativeMax+dalvikMax,
2259 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2260 }
2261
2262 if (dumpDalvik) {
2263 pw.println(" ");
2264 pw.println(" Dalvik Details");
2265
2266 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2267 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2268 final int myPss = memInfo.getOtherPss(i);
2269 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2270 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2271 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2272 final int mySharedClean = memInfo.getOtherSharedClean(i);
2273 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2274 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002275 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002276 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002277 || mySharedClean != 0 || myPrivateClean != 0
2278 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002279 if (dumpFullInfo) {
2280 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2281 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002282 mySharedClean, myPrivateClean,
2283 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2284 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002285 } else {
2286 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2287 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002288 myPrivateClean,
2289 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2290 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002291 }
2292 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002293 }
2294 }
2295 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002296
2297 pw.println(" ");
2298 pw.println(" App Summary");
2299 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2300 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2301 printRow(pw, ONE_COUNT_COLUMN,
2302 "Java Heap:", memInfo.getSummaryJavaHeap());
2303 printRow(pw, ONE_COUNT_COLUMN,
2304 "Native Heap:", memInfo.getSummaryNativeHeap());
2305 printRow(pw, ONE_COUNT_COLUMN,
2306 "Code:", memInfo.getSummaryCode());
2307 printRow(pw, ONE_COUNT_COLUMN,
2308 "Stack:", memInfo.getSummaryStack());
2309 printRow(pw, ONE_COUNT_COLUMN,
2310 "Graphics:", memInfo.getSummaryGraphics());
2311 printRow(pw, ONE_COUNT_COLUMN,
2312 "Private Other:", memInfo.getSummaryPrivateOther());
2313 printRow(pw, ONE_COUNT_COLUMN,
2314 "System:", memInfo.getSummarySystem());
2315 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002316 if (memInfo.hasSwappedOutPss) {
2317 printRow(pw, TWO_COUNT_COLUMNS,
2318 "TOTAL:", memInfo.getSummaryTotalPss(),
2319 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2320 } else {
2321 printRow(pw, TWO_COUNT_COLUMNS,
2322 "TOTAL:", memInfo.getSummaryTotalPss(),
2323 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2324 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002325 }
2326
Jeff Hamilton52d32032011-01-08 15:31:26 -06002327 public void registerOnActivityPausedListener(Activity activity,
2328 OnActivityPausedListener listener) {
2329 synchronized (mOnPauseListeners) {
2330 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2331 if (list == null) {
2332 list = new ArrayList<OnActivityPausedListener>();
2333 mOnPauseListeners.put(activity, list);
2334 }
2335 list.add(listener);
2336 }
2337 }
2338
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002339 public void unregisterOnActivityPausedListener(Activity activity,
2340 OnActivityPausedListener listener) {
2341 synchronized (mOnPauseListeners) {
2342 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2343 if (list != null) {
2344 list.remove(listener);
2345 }
2346 }
2347 }
2348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 public final ActivityInfo resolveActivityInfo(Intent intent) {
2350 ActivityInfo aInfo = intent.resolveActivityInfo(
2351 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2352 if (aInfo == null) {
2353 // Throw an exception.
2354 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002355 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357 return aInfo;
2358 }
Bob Leee5408332009-09-04 18:31:17 -07002359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002362 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002363 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002365 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 r.intent = intent;
2367 r.state = state;
2368 r.parent = parent;
2369 r.embeddedID = id;
2370 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002371 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002372 if (localLOGV) {
2373 ComponentName compname = intent.getComponent();
2374 String name;
2375 if (compname != null) {
2376 name = compname.toShortString();
2377 } else {
2378 name = "(Intent " + intent + ").getComponent() returned null";
2379 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002380 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 + ", comp=" + name
2382 + ", token=" + token);
2383 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002384 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 }
2386
2387 public final Activity getActivity(IBinder token) {
2388 return mActivities.get(token).activity;
2389 }
2390
2391 public final void sendActivityResult(
2392 IBinder token, String id, int requestCode,
2393 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002394 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002395 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2397 list.add(new ResultInfo(id, requestCode, resultCode, data));
2398 mAppThread.scheduleSendResult(token, list);
2399 }
2400
Jeff Brown9ef09972013-10-15 20:49:59 -07002401 private void sendMessage(int what, Object obj) {
2402 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
2404
Jeff Brown9ef09972013-10-15 20:49:59 -07002405 private void sendMessage(int what, Object obj, int arg1) {
2406 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 }
2408
Jeff Brown9ef09972013-10-15 20:49:59 -07002409 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2410 sendMessage(what, obj, arg1, arg2, false);
2411 }
2412
2413 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2414 if (DEBUG_MESSAGES) Slog.v(
2415 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2416 + ": " + arg1 + " / " + obj);
2417 Message msg = Message.obtain();
2418 msg.what = what;
2419 msg.obj = obj;
2420 msg.arg1 = arg1;
2421 msg.arg2 = arg2;
2422 if (async) {
2423 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002425 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 }
2427
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002428 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2429 if (DEBUG_MESSAGES) Slog.v(
2430 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2431 "seq= " + seq);
2432 Message msg = Message.obtain();
2433 msg.what = what;
2434 SomeArgs args = SomeArgs.obtain();
2435 args.arg1 = obj;
2436 args.argi1 = arg1;
2437 args.argi2 = arg2;
2438 args.argi3 = seq;
2439 msg.obj = args;
2440 mH.sendMessage(msg);
2441 }
2442
Dianne Hackborn21556372010-02-04 16:34:40 -08002443 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 String what) {
2445 ContextCleanupInfo cci = new ContextCleanupInfo();
2446 cci.context = context;
2447 cci.who = who;
2448 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002449 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 }
2451
Craig Mautner233ceee2014-05-09 17:05:11 -07002452 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2454
2455 ActivityInfo aInfo = r.activityInfo;
2456 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002457 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 Context.CONTEXT_INCLUDE_CODE);
2459 }
Bob Leee5408332009-09-04 18:31:17 -07002460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 ComponentName component = r.intent.getComponent();
2462 if (component == null) {
2463 component = r.intent.resolveActivity(
2464 mInitialApplication.getPackageManager());
2465 r.intent.setComponent(component);
2466 }
2467
2468 if (r.activityInfo.targetActivity != null) {
2469 component = new ComponentName(r.activityInfo.packageName,
2470 r.activityInfo.targetActivity);
2471 }
2472
2473 Activity activity = null;
2474 try {
2475 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2476 activity = mInstrumentation.newActivity(
2477 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002478 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002480 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 if (r.state != null) {
2482 r.state.setClassLoader(cl);
2483 }
2484 } catch (Exception e) {
2485 if (!mInstrumentation.onException(activity, e)) {
2486 throw new RuntimeException(
2487 "Unable to instantiate activity " + component
2488 + ": " + e.toString(), e);
2489 }
2490 }
2491
2492 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002493 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002494
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002495 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2496 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 TAG, r + ": app=" + app
2498 + ", appName=" + app.getPackageName()
2499 + ", pkg=" + r.packageInfo.getPackageName()
2500 + ", comp=" + r.intent.getComponent().toShortString()
2501 + ", dir=" + r.packageInfo.getAppDir());
2502
2503 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002504 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002505 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002506 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002507 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002508 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002509 Window window = null;
2510 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2511 window = r.mPendingRemoveWindow;
2512 r.mPendingRemoveWindow = null;
2513 r.mPendingRemoveWindowManager = null;
2514 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002515 activity.attach(appContext, this, getInstrumentation(), r.token,
2516 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002517 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002518 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002519
Christopher Tateb70f3df2009-04-07 16:07:59 -07002520 if (customIntent != null) {
2521 activity.mIntent = customIntent;
2522 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002523 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 activity.mStartedActivity = false;
2525 int theme = r.activityInfo.getThemeResource();
2526 if (theme != 0) {
2527 activity.setTheme(theme);
2528 }
2529
2530 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002531 if (r.isPersistable()) {
2532 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2533 } else {
2534 mInstrumentation.callActivityOnCreate(activity, r.state);
2535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 if (!activity.mCalled) {
2537 throw new SuperNotCalledException(
2538 "Activity " + r.intent.getComponent().toShortString() +
2539 " did not call through to super.onCreate()");
2540 }
2541 r.activity = activity;
2542 r.stopped = true;
2543 if (!r.activity.mFinished) {
2544 activity.performStart();
2545 r.stopped = false;
2546 }
2547 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002548 if (r.isPersistable()) {
2549 if (r.state != null || r.persistentState != null) {
2550 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2551 r.persistentState);
2552 }
2553 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2555 }
2556 }
2557 if (!r.activity.mFinished) {
2558 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002559 if (r.isPersistable()) {
2560 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2561 r.persistentState);
2562 } else {
2563 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2564 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 if (!activity.mCalled) {
2566 throw new SuperNotCalledException(
2567 "Activity " + r.intent.getComponent().toShortString() +
2568 " did not call through to super.onPostCreate()");
2569 }
2570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 }
2572 r.paused = true;
2573
2574 mActivities.put(r.token, r);
2575
2576 } catch (SuperNotCalledException e) {
2577 throw e;
2578
2579 } catch (Exception e) {
2580 if (!mInstrumentation.onException(activity, e)) {
2581 throw new RuntimeException(
2582 "Unable to start activity " + component
2583 + ": " + e.toString(), e);
2584 }
2585 }
2586
2587 return activity;
2588 }
2589
Wale Ogunwale7c726682015-02-06 17:34:28 -08002590 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2591 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002592 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002593 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002594 } catch (RemoteException e) {
2595 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002596
Wale Ogunwale7c726682015-02-06 17:34:28 -08002597 ContextImpl appContext = ContextImpl.createActivityContext(
2598 this, r.packageInfo, displayId, r.overrideConfig);
2599 appContext.setOuterContext(activity);
2600 Context baseContext = appContext;
2601
2602 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002603 // For debugging purposes, if the activity's package name contains the value of
2604 // the "debug.use-second-display" system property as a substring, then show
2605 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002606 String pkgName = SystemProperties.get("debug.second-display.pkg");
2607 if (pkgName != null && !pkgName.isEmpty()
2608 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002609 for (int id : dm.getDisplayIds()) {
2610 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002611 Display display =
2612 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002613 baseContext = appContext.createDisplayContext(display);
2614 break;
2615 }
2616 }
2617 }
2618 return baseContext;
2619 }
2620
Romain Guy65b345f2011-07-27 18:51:50 -07002621 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 // If we are getting ready to gc after going to the background, well
2623 // we are back active so skip it.
2624 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002625 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626
Jeff Hao1b012d32014-08-20 10:35:34 -07002627 if (r.profilerInfo != null) {
2628 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002629 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002630 }
2631
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002632 // Make sure we are running with the most recent config.
2633 handleConfigurationChanged(null, null);
2634
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002635 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002637
Chet Haase0d1c27a2014-11-03 18:35:16 +00002638 // Initialize before creating the activity
2639 WindowManagerGlobal.initialize();
2640
Craig Mautner233ceee2014-05-09 17:05:11 -07002641 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642
2643 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002644 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002645 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002646 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002647 handleResumeActivity(r.token, false, r.isForward,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002648 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649
2650 if (!r.activity.mFinished && r.startsNotResumed) {
2651 // The activity manager actually wants this one to start out
2652 // paused, because it needs to be visible but isn't in the
2653 // foreground. We accomplish this by going through the
2654 // normal startup (because activities expect to go through
2655 // onResume() the first time they run, before their window
2656 // is displayed), and then pausing it. However, in this case
2657 // we do -not- need to do the full pause cycle (of freezing
2658 // and such) because the activity manager assumes it can just
2659 // retain the current state it has.
2660 try {
2661 r.activity.mCalled = false;
2662 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002663 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002664 // we need to be created again. But we only do this
2665 // for pre-Honeycomb apps, which always save their state
2666 // when pausing, so we can not have them save their state
2667 // when restarting from a paused state. For HC and later,
2668 // we want to (and can) let the state be saved as the normal
2669 // part of stopping the activity.
2670 if (r.isPreHoneycomb()) {
2671 r.state = oldState;
2672 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 if (!r.activity.mCalled) {
2674 throw new SuperNotCalledException(
2675 "Activity " + r.intent.getComponent().toShortString() +
2676 " did not call through to super.onPause()");
2677 }
2678
2679 } catch (SuperNotCalledException e) {
2680 throw e;
2681
2682 } catch (Exception e) {
2683 if (!mInstrumentation.onException(r.activity, e)) {
2684 throw new RuntimeException(
2685 "Unable to pause activity "
2686 + r.intent.getComponent().toShortString()
2687 + ": " + e.toString(), e);
2688 }
2689 }
2690 r.paused = true;
2691 }
2692 } else {
2693 // If there was an error, for any reason, tell the activity
2694 // manager to stop us.
2695 try {
2696 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002697 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2698 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002700 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002701 }
2702 }
2703 }
2704
Filip Gruszczynski23493322015-07-29 17:02:59 -07002705 private void reportSizeConfigurations(ActivityClientRecord r) {
2706 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2707 if (configurations == null) {
2708 return;
2709 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002710 SparseIntArray horizontal = new SparseIntArray();
2711 SparseIntArray vertical = new SparseIntArray();
2712 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002713 for (int i = configurations.length - 1; i >= 0; i--) {
2714 Configuration config = configurations[i];
2715 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002716 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002717 }
2718 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002719 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002720 }
2721 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002722 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002723 }
2724 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002725 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002726 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2727 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002728 } catch (RemoteException ex) {
2729 }
2730
2731 }
2732
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002733 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 final int N = intents.size();
2735 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002736 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002738 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002739 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2741 }
2742 }
2743
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002744 public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002745 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 if (r != null) {
2747 final boolean resumed = !r.paused;
2748 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002749 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 mInstrumentation.callActivityOnPause(r.activity);
2751 }
2752 deliverNewIntents(r, intents);
2753 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002754 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002755 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757 }
2758 }
Bob Leee5408332009-09-04 18:31:17 -07002759
Romain Guy65b345f2011-07-27 18:51:50 -07002760 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 performNewIntents(data.token, data.intents);
2762 }
2763
Adam Skorydfc7fd72013-08-05 19:23:41 -07002764 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackborn782d4982015-07-08 17:36:37 -07002765 if (mLastAssistStructure != null) {
2766 AssistStructure structure = mLastAssistStructure.get();
2767 if (structure != null) {
2768 structure.clearSendChannel();
2769 }
2770 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002771 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002772 AssistStructure structure = null;
2773 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002774 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002775 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002776 if (r != null) {
2777 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2778 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002779 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002780 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002781 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002782 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002783 if (activityIntent != null && (r.window == null ||
2784 (r.window.getAttributes().flags
2785 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002786 Intent intent = new Intent(activityIntent);
2787 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2788 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2789 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002790 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002791 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002792 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002793 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002794 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002795 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002796 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002797 if (structure == null) {
2798 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002799 }
Dianne Hackborn782d4982015-07-08 17:36:37 -07002800 mLastAssistStructure = new WeakReference<>(structure);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002801 IActivityManager mgr = ActivityManagerNative.getDefault();
2802 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002803 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002804 } catch (RemoteException e) {
2805 }
2806 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002807
2808 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2809 ActivityClientRecord r = mActivities.get(token);
2810 if (r != null) {
2811 r.activity.onTranslucentConversionComplete(drawComplete);
2812 }
2813 }
2814
Craig Mautnereb8abf72014-07-02 15:04:09 -07002815 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2816 ActivityClientRecord r = mActivities.get(token);
2817 if (r != null) {
2818 r.activity.onNewActivityOptions(options);
2819 }
2820 }
2821
Jose Lima4b6c6692014-08-12 17:41:12 -07002822 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002823 ActivityClientRecord r = mActivities.get(token);
2824 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002825 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002826 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002827 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002828 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002829 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002830 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2831 if (!activity.mCalled) {
2832 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002833 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002834 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002835 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002836 }
2837 }
2838 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002839 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002840 } catch (RemoteException e) {
2841 }
2842 }
2843
Jose Lima4b6c6692014-08-12 17:41:12 -07002844 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002845 ActivityClientRecord r = mActivities.get(token);
2846 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002847 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002848 }
2849 }
2850
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002851 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002852 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2853 try {
2854 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2855 } finally {
2856 StrictMode.setThreadPolicy(oldPolicy);
2857 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002858 }
2859
Craig Mautner8746a472014-07-24 15:12:54 -07002860 private void handleEnterAnimationComplete(IBinder token) {
2861 ActivityClientRecord r = mActivities.get(token);
2862 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002863 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002864 }
2865 }
2866
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002867 private void handleStartBinderTracking() {
2868 Binder.enableTracing();
2869 }
2870
2871 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2872 try {
2873 Binder.disableTracing();
2874 Binder.getTransactionTracker().writeTracesToFile(fd);
2875 } finally {
2876 IoUtils.closeQuietly(fd);
2877 Binder.getTransactionTracker().clearTraces();
2878 }
2879 }
2880
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002881 private void handleMultiWindowChanged(IBinder token, boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002882 final ActivityClientRecord r = mActivities.get(token);
2883 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002884 r.activity.onMultiWindowChanged(inMultiWindow);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002885 }
2886 }
2887
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002888 private void handlePictureInPictureChanged(IBinder token, boolean inPip) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002889 final ActivityClientRecord r = mActivities.get(token);
2890 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002891 r.activity.onPictureInPictureChanged(inPip);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002892 }
2893 }
2894
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002895 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2896 final ActivityClientRecord r = mActivities.get(token);
2897 if (r != null) {
2898 r.voiceInteractor = interactor;
2899 r.activity.setVoiceInteractor(interactor);
2900 if (interactor == null) {
2901 r.activity.onLocalVoiceInteractionStopped();
2902 } else {
2903 r.activity.onLocalVoiceInteractionStarted();
2904 }
2905 }
2906 }
2907
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002908 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2909
2910 /**
2911 * Return the Intent that's currently being handled by a
2912 * BroadcastReceiver on this thread, or null if none.
2913 * @hide
2914 */
2915 public static Intent getIntentBeingBroadcast() {
2916 return sCurrentBroadcastIntent.get();
2917 }
2918
Romain Guy65b345f2011-07-27 18:51:50 -07002919 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 // If we are getting ready to gc after going to the background, well
2921 // we are back active so skip it.
2922 unscheduleGcIdler();
2923
2924 String component = data.intent.getComponent().getClassName();
2925
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002926 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002927 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002928
2929 IActivityManager mgr = ActivityManagerNative.getDefault();
2930
Romain Guy65b345f2011-07-27 18:51:50 -07002931 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 try {
2933 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2934 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002935 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002936 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2938 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002939 if (DEBUG_BROADCAST) Slog.i(TAG,
2940 "Finishing failed broadcast to " + data.intent.getComponent());
2941 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 throw new RuntimeException(
2943 "Unable to instantiate receiver " + component
2944 + ": " + e.toString(), e);
2945 }
2946
2947 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002948 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002949
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002950 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 TAG, "Performing receive of " + data.intent
2952 + ": app=" + app
2953 + ", appName=" + app.getPackageName()
2954 + ", pkg=" + packageInfo.getPackageName()
2955 + ", comp=" + data.intent.getComponent().toShortString()
2956 + ", dir=" + packageInfo.getAppDir());
2957
Dianne Hackborn21556372010-02-04 16:34:40 -08002958 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002959 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002960 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 receiver.onReceive(context.getReceiverRestrictedContext(),
2962 data.intent);
2963 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002964 if (DEBUG_BROADCAST) Slog.i(TAG,
2965 "Finishing failed broadcast to " + data.intent.getComponent());
2966 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 if (!mInstrumentation.onException(receiver, e)) {
2968 throw new RuntimeException(
2969 "Unable to start receiver " + component
2970 + ": " + e.toString(), e);
2971 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002972 } finally {
2973 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 }
2975
Dianne Hackborne829fef2010-10-26 17:44:01 -07002976 if (receiver.getPendingResult() != null) {
2977 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 }
2979 }
2980
Christopher Tate181fafa2009-05-14 11:12:14 -07002981 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002982 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002983 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002984
Christopher Tate346acb12012-10-15 19:20:25 -07002985 // Sanity check the requested target package's uid against ours
2986 try {
2987 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2988 data.appInfo.packageName, 0, UserHandle.myUserId());
2989 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2990 Slog.w(TAG, "Asked to instantiate non-matching package "
2991 + data.appInfo.packageName);
2992 return;
2993 }
2994 } catch (RemoteException e) {
2995 Slog.e(TAG, "Can't reach package manager", e);
2996 return;
2997 }
2998
Christopher Tate181fafa2009-05-14 11:12:14 -07002999 // no longer idle; we have backup work to do
3000 unscheduleGcIdler();
3001
3002 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003003 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003004 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003005 if (packageName == null) {
3006 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3007 return;
3008 }
3009
Christopher Tate181fafa2009-05-14 11:12:14 -07003010 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003011 // full backup operation but no app-supplied agent? use the default implementation
3012 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3013 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003014 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003015 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003016
Christopher Tate181fafa2009-05-14 11:12:14 -07003017 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003018 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003019 BackupAgent agent = mBackupAgents.get(packageName);
3020 if (agent != null) {
3021 // reusing the existing instance
3022 if (DEBUG_BACKUP) {
3023 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003024 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003025 binder = agent.onBind();
3026 } else {
3027 try {
3028 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3029
3030 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3031 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3032
3033 // set up the agent's context
3034 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3035 context.setOuterContext(agent);
3036 agent.attach(context);
3037
3038 agent.onCreate();
3039 binder = agent.onBind();
3040 mBackupAgents.put(packageName, agent);
3041 } catch (Exception e) {
3042 // If this is during restore, fail silently; otherwise go
3043 // ahead and let the user see the crash.
3044 Slog.e(TAG, "Agent threw during creation: " + e);
3045 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3046 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3047 throw e;
3048 }
3049 // falling through with 'binder' still null
3050 }
Christopher Tated1475e02009-07-09 15:36:17 -07003051 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003052
3053 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003054 try {
3055 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3056 } catch (RemoteException e) {
3057 // nothing to do.
3058 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003059 } catch (Exception e) {
3060 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003061 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003062 }
3063 }
3064
3065 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003066 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003067 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003068
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003069 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003070 String packageName = packageInfo.mPackageName;
3071 BackupAgent agent = mBackupAgents.get(packageName);
3072 if (agent != null) {
3073 try {
3074 agent.onDestroy();
3075 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003076 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003077 e.printStackTrace();
3078 }
3079 mBackupAgents.remove(packageName);
3080 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003081 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003082 }
3083 }
3084
Romain Guy65b345f2011-07-27 18:51:50 -07003085 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 // If we are getting ready to gc after going to the background, well
3087 // we are back active so skip it.
3088 unscheduleGcIdler();
3089
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003090 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003091 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 Service service = null;
3093 try {
3094 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3095 service = (Service) cl.loadClass(data.info.name).newInstance();
3096 } catch (Exception e) {
3097 if (!mInstrumentation.onException(service, e)) {
3098 throw new RuntimeException(
3099 "Unable to instantiate service " + data.info.name
3100 + ": " + e.toString(), e);
3101 }
3102 }
3103
3104 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003105 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003106
Jeff Browndefd4a62014-03-10 21:24:37 -07003107 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3108 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003110 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 service.attach(context, this, data.info.name, data.token, app,
3112 ActivityManagerNative.getDefault());
3113 service.onCreate();
3114 mServices.put(data.token, service);
3115 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003116 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003117 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118 } catch (RemoteException e) {
3119 // nothing to do.
3120 }
3121 } catch (Exception e) {
3122 if (!mInstrumentation.onException(service, e)) {
3123 throw new RuntimeException(
3124 "Unable to create service " + data.info.name
3125 + ": " + e.toString(), e);
3126 }
3127 }
3128 }
3129
Romain Guy65b345f2011-07-27 18:51:50 -07003130 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003132 if (DEBUG_SERVICE)
3133 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 if (s != null) {
3135 try {
3136 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003137 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 try {
3139 if (!data.rebind) {
3140 IBinder binder = s.onBind(data.intent);
3141 ActivityManagerNative.getDefault().publishService(
3142 data.token, data.intent, binder);
3143 } else {
3144 s.onRebind(data.intent);
3145 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003146 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003147 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003148 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 } catch (RemoteException ex) {
3150 }
3151 } catch (Exception e) {
3152 if (!mInstrumentation.onException(s, e)) {
3153 throw new RuntimeException(
3154 "Unable to bind to service " + s
3155 + " with " + data.intent + ": " + e.toString(), e);
3156 }
3157 }
3158 }
3159 }
3160
Romain Guy65b345f2011-07-27 18:51:50 -07003161 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 Service s = mServices.get(data.token);
3163 if (s != null) {
3164 try {
3165 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003166 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 boolean doRebind = s.onUnbind(data.intent);
3168 try {
3169 if (doRebind) {
3170 ActivityManagerNative.getDefault().unbindFinished(
3171 data.token, data.intent, doRebind);
3172 } else {
3173 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003174 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 }
3176 } catch (RemoteException ex) {
3177 }
3178 } catch (Exception e) {
3179 if (!mInstrumentation.onException(s, e)) {
3180 throw new RuntimeException(
3181 "Unable to unbind to service " + s
3182 + " with " + data.intent + ": " + e.toString(), e);
3183 }
3184 }
3185 }
3186 }
3187
Dianne Hackborn625ac272010-09-17 18:29:22 -07003188 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003189 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3190 try {
3191 Service s = mServices.get(info.token);
3192 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003193 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3194 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003195 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3196 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003198 } finally {
3199 IoUtils.closeQuietly(info.fd);
3200 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 }
3202 }
3203
Dianne Hackborn625ac272010-09-17 18:29:22 -07003204 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003205 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3206 try {
3207 ActivityClientRecord r = mActivities.get(info.token);
3208 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003209 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3210 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003211 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3212 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003213 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003214 } finally {
3215 IoUtils.closeQuietly(info.fd);
3216 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003217 }
3218 }
3219
Marco Nelissen18cb2872011-11-15 11:19:53 -08003220 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003221 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3222 try {
3223 ProviderClientRecord r = mLocalProviders.get(info.token);
3224 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003225 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3226 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003227 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3228 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003229 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003230 } finally {
3231 IoUtils.closeQuietly(info.fd);
3232 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003233 }
3234 }
3235
Romain Guy65b345f2011-07-27 18:51:50 -07003236 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 Service s = mServices.get(data.token);
3238 if (s != null) {
3239 try {
3240 if (data.args != null) {
3241 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003242 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003244 int res;
3245 if (!data.taskRemoved) {
3246 res = s.onStartCommand(data.args, data.flags, data.startId);
3247 } else {
3248 s.onTaskRemoved(data.args);
3249 res = Service.START_TASK_REMOVED_COMPLETE;
3250 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003251
3252 QueuedWork.waitToFinish();
3253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003255 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003256 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 } catch (RemoteException e) {
3258 // nothing to do.
3259 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003260 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 } catch (Exception e) {
3262 if (!mInstrumentation.onException(s, e)) {
3263 throw new RuntimeException(
3264 "Unable to start service " + s
3265 + " with " + data.args + ": " + e.toString(), e);
3266 }
3267 }
3268 }
3269 }
3270
Romain Guy65b345f2011-07-27 18:51:50 -07003271 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 Service s = mServices.remove(token);
3273 if (s != null) {
3274 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003275 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 s.onDestroy();
3277 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003278 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003280 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003282
3283 QueuedWork.waitToFinish();
3284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003285 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003286 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003287 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003288 } catch (RemoteException e) {
3289 // nothing to do.
Craig Mautner66c4a822015-01-16 12:48:16 -08003290 Slog.i(TAG, "handleStopService: unable to execute serviceDoneExecuting for "
3291 + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 }
3293 } catch (Exception e) {
3294 if (!mInstrumentation.onException(s, e)) {
3295 throw new RuntimeException(
3296 "Unable to stop service " + s
3297 + ": " + e.toString(), e);
3298 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003299 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003300 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003301 } else {
3302 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003304 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 }
3306
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003307 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003309 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003310 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 + " finished=" + r.activity.mFinished);
3312 if (r != null && !r.activity.mFinished) {
3313 if (clearHide) {
3314 r.hideForNow = false;
3315 r.activity.mStartedActivity = false;
3316 }
3317 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003318 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003319 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 if (r.pendingIntents != null) {
3321 deliverNewIntents(r, r.pendingIntents);
3322 r.pendingIntents = null;
3323 }
3324 if (r.pendingResults != null) {
3325 deliverResults(r, r.pendingResults);
3326 r.pendingResults = null;
3327 }
3328 r.activity.performResume();
3329
Craig Mautnere3119b72015-01-20 15:02:36 -08003330 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003331 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07003332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 r.paused = false;
3334 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003336 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 } catch (Exception e) {
3338 if (!mInstrumentation.onException(r.activity, e)) {
3339 throw new RuntimeException(
3340 "Unable to resume activity "
3341 + r.intent.getComponent().toShortString()
3342 + ": " + e.toString(), e);
3343 }
3344 }
3345 }
3346 return r;
3347 }
3348
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003349 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3350 if (r.mPreserveWindow && !force) {
3351 return;
3352 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003353 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003354 r.mPendingRemoveWindowManager.removeViewImmediate(
3355 r.mPendingRemoveWindow.getDecorView());
3356 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003357 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003358 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003359 r.activity.getClass().getName(), "Activity");
3360 }
3361 }
3362 r.mPendingRemoveWindow = null;
3363 r.mPendingRemoveWindowManager = null;
3364 }
3365
Craig Mautner233ceee2014-05-09 17:05:11 -07003366 final void handleResumeActivity(IBinder token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003367 boolean clearHide, boolean isForward, boolean reallyResume, int seq) {
3368 ActivityClientRecord r = mActivities.get(token);
3369 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3370 return;
3371 }
3372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 // If we are getting ready to gc after going to the background, well
3374 // we are back active so skip it.
3375 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003376 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377
Adam Powellcfbe9be2013-11-06 14:58:58 -08003378 // TODO Push resumeArgs into the activity for consideration
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003379 r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380
3381 if (r != null) {
3382 final Activity a = r.activity;
3383
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003384 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 TAG, "Resume " + r + " started activity: " +
3386 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3387 + ", finished: " + a.mFinished);
3388
3389 final int forwardBit = isForward ?
3390 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 // If the window hasn't yet been added to the window manager,
3393 // and this guy didn't finish itself or start another activity,
3394 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003395 boolean willBeVisible = !a.mStartedActivity;
3396 if (!willBeVisible) {
3397 try {
3398 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3399 a.getActivityToken());
3400 } catch (RemoteException e) {
3401 }
3402 }
3403 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 r.window = r.activity.getWindow();
3405 View decor = r.window.getDecorView();
3406 decor.setVisibility(View.INVISIBLE);
3407 ViewManager wm = a.getWindowManager();
3408 WindowManager.LayoutParams l = r.window.getAttributes();
3409 a.mDecor = decor;
3410 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3411 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003412 if (r.mPreserveWindow) {
3413 a.mWindowAdded = true;
3414 r.mPreserveWindow = false;
3415 }
3416 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 a.mWindowAdded = true;
3418 wm.addView(decor, l);
3419 }
3420
3421 // If the window has already been added, but during resume
3422 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003423 // window visible.
3424 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003425 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 TAG, "Launch " + r + " mStartedActivity set");
3427 r.hideForNow = true;
3428 }
3429
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003430 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003431 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 // The window is now visible if it has been added, we are not
3434 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003435 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003436 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003438 r.tmpConfig.setTo(r.newConfig);
3439 if (r.overrideConfig != null) {
3440 r.tmpConfig.updateFrom(r.overrideConfig);
3441 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003442 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003443 + r.activityInfo.name + " with newConfig " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003444 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003445 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 r.newConfig = null;
3447 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003448 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 + isForward);
3450 WindowManager.LayoutParams l = r.window.getAttributes();
3451 if ((l.softInputMode
3452 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3453 != forwardBit) {
3454 l.softInputMode = (l.softInputMode
3455 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3456 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003457 if (r.activity.mVisibleFromClient) {
3458 ViewManager wm = a.getWindowManager();
3459 View decor = r.window.getDecorView();
3460 wm.updateViewLayout(decor, l);
3461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 }
3463 r.activity.mVisibleFromServer = true;
3464 mNumVisibleActivities++;
3465 if (r.activity.mVisibleFromClient) {
3466 r.activity.makeVisible();
3467 }
3468 }
3469
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003470 if (!r.onlyLocalRequest) {
3471 r.nextIdle = mNewActivities;
3472 mNewActivities = r;
3473 if (localLOGV) Slog.v(
3474 TAG, "Scheduling idle handler for " + r);
3475 Looper.myQueue().addIdleHandler(new Idler());
3476 }
3477 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003479 // Tell the activity manager we have resumed.
3480 if (reallyResume) {
3481 try {
3482 ActivityManagerNative.getDefault().activityResumed(token);
3483 } catch (RemoteException ex) {
3484 }
3485 }
3486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 } else {
3488 // If an exception was thrown when trying to resume, then
3489 // just end this activity.
3490 try {
3491 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003492 .finishActivity(token, Activity.RESULT_CANCELED, null,
3493 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 } catch (RemoteException ex) {
3495 }
3496 }
3497 }
3498
3499 private int mThumbnailWidth = -1;
3500 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003501 private Bitmap mAvailThumbnailBitmap = null;
3502 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003503
Romain Guy65b345f2011-07-27 18:51:50 -07003504 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003505 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003507 if (thumbnail == null) {
3508 int w = mThumbnailWidth;
3509 int h;
3510 if (w < 0) {
3511 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003512 int wId = com.android.internal.R.dimen.thumbnail_width;
3513 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003514 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3515 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003516 } else {
3517 h = mThumbnailHeight;
3518 }
3519
3520 // On platforms where we don't want thumbnails, set dims to (0,0)
3521 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003522 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3523 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003524 thumbnail.eraseColor(0);
3525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 }
3527
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003528 if (thumbnail != null) {
3529 Canvas cv = mThumbnailCanvas;
3530 if (cv == null) {
3531 mThumbnailCanvas = cv = new Canvas();
3532 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003533
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003534 cv.setBitmap(thumbnail);
3535 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3536 mAvailThumbnailBitmap = thumbnail;
3537 thumbnail = null;
3538 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003539 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 } catch (Exception e) {
3543 if (!mInstrumentation.onException(r.activity, e)) {
3544 throw new RuntimeException(
3545 "Unable to create thumbnail of "
3546 + r.intent.getComponent().toShortString()
3547 + ": " + e.toString(), e);
3548 }
3549 thumbnail = null;
3550 }
3551
3552 return thumbnail;
3553 }
3554
Romain Guy65b345f2011-07-27 18:51:50 -07003555 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003556 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003557 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003558 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3559 return;
3560 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003562 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003563 if (userLeaving) {
3564 performUserLeavingActivity(r);
3565 }
Bob Leee5408332009-09-04 18:31:17 -07003566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003567 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003568 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003570 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003571 if (r.isPreHoneycomb()) {
3572 QueuedWork.waitToFinish();
3573 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003576 if (!dontReport) {
3577 try {
3578 ActivityManagerNative.getDefault().activityPaused(token);
3579 } catch (RemoteException ex) {
3580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003582 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 }
3584 }
3585
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003586 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003587 mInstrumentation.callActivityOnUserLeaving(r.activity);
3588 }
3589
3590 final Bundle performPauseActivity(IBinder token, boolean finished,
3591 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003592 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 return r != null ? performPauseActivity(r, finished, saveState) : null;
3594 }
3595
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003596 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 boolean saveState) {
3598 if (r.paused) {
3599 if (r.activity.mFinished) {
3600 // If we are finishing, we won't call onResume() in certain cases.
3601 // So here we likewise don't want to call onPause() if the activity
3602 // isn't resumed.
3603 return null;
3604 }
3605 RuntimeException e = new RuntimeException(
3606 "Performing pause of activity that is not resumed: "
3607 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003608 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 if (finished) {
3611 r.activity.mFinished = true;
3612 }
3613 try {
3614 // Next have the activity save its current state and managed dialogs...
3615 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003616 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 }
3618 // Now we are idle.
3619 r.activity.mCalled = false;
3620 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08003621 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003622 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 if (!r.activity.mCalled) {
3624 throw new SuperNotCalledException(
3625 "Activity " + r.intent.getComponent().toShortString() +
3626 " did not call through to super.onPause()");
3627 }
3628
3629 } catch (SuperNotCalledException e) {
3630 throw e;
3631
3632 } catch (Exception e) {
3633 if (!mInstrumentation.onException(r.activity, e)) {
3634 throw new RuntimeException(
3635 "Unable to pause activity "
3636 + r.intent.getComponent().toShortString()
3637 + ": " + e.toString(), e);
3638 }
3639 }
3640 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003641
3642 // Notify any outstanding on paused listeners
3643 ArrayList<OnActivityPausedListener> listeners;
3644 synchronized (mOnPauseListeners) {
3645 listeners = mOnPauseListeners.remove(r.activity);
3646 }
3647 int size = (listeners != null ? listeners.size() : 0);
3648 for (int i = 0; i < size; i++) {
3649 listeners.get(i).onPaused(r.activity);
3650 }
3651
Craig Mautnera0026042014-04-23 11:45:37 -07003652 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 }
3654
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003655 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003656 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003657 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 }
3659
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003660 private static class StopInfo implements Runnable {
3661 ActivityClientRecord activity;
3662 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003663 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003664 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003665
3666 @Override public void run() {
3667 // Tell activity manager we have been stopped.
3668 try {
3669 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3670 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003671 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003672 } catch (RemoteException ex) {
3673 }
3674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 }
3676
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003677 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003678 public final IActivityManager.ContentProviderHolder holder;
3679 public final ProviderClientRecord client;
3680 public int stableCount;
3681 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003682
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003683 // When this is set, the stable and unstable ref counts are 0 and
3684 // we have a pending operation scheduled to remove the ref count
3685 // from the activity manager. On the activity manager we are still
3686 // holding an unstable ref, though it is not reflected in the counts
3687 // here.
3688 public boolean removePending;
3689
3690 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3691 ProviderClientRecord inClient, int sCount, int uCount) {
3692 holder = inHolder;
3693 client = inClient;
3694 stableCount = sCount;
3695 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 }
3697 }
3698
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003699 /**
3700 * Core implementation of stopping an activity. Note this is a little
3701 * tricky because the server's meaning of stop is slightly different
3702 * than our client -- for the server, stop means to save state and give
3703 * it the result when it is done, but the window may still be visible.
3704 * For the client, we want to call onStop()/onStart() to indicate when
3705 * the activity's UI visibillity changes.
3706 */
Romain Guy65b345f2011-07-27 18:51:50 -07003707 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003708 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003709 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 if (r != null) {
3711 if (!keepShown && r.stopped) {
3712 if (r.activity.mFinished) {
3713 // If we are finishing, we won't call onResume() in certain
3714 // cases. So here we likewise don't want to call onStop()
3715 // if the activity isn't resumed.
3716 return;
3717 }
3718 RuntimeException e = new RuntimeException(
3719 "Performing stop of activity that is not resumed: "
3720 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003721 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723
3724 if (info != null) {
3725 try {
3726 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003727 // For now, don't create the thumbnail here; we are
3728 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 info.description = r.activity.onCreateDescription();
3730 } catch (Exception e) {
3731 if (!mInstrumentation.onException(r.activity, e)) {
3732 throw new RuntimeException(
3733 "Unable to save state of activity "
3734 + r.intent.getComponent().toShortString()
3735 + ": " + e.toString(), e);
3736 }
3737 }
3738 }
3739
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003740 // Next have the activity save its current state and managed dialogs...
3741 if (!r.activity.mFinished && saveState) {
3742 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003743 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003744 }
3745 }
3746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747 if (!keepShown) {
3748 try {
3749 // Now we are idle.
3750 r.activity.performStop();
3751 } catch (Exception e) {
3752 if (!mInstrumentation.onException(r.activity, e)) {
3753 throw new RuntimeException(
3754 "Unable to stop activity "
3755 + r.intent.getComponent().toShortString()
3756 + ": " + e.toString(), e);
3757 }
3758 }
3759 r.stopped = true;
3760 }
3761
3762 r.paused = true;
3763 }
3764 }
3765
Romain Guy65b345f2011-07-27 18:51:50 -07003766 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003767 View v = r.activity.mDecor;
3768 if (v != null) {
3769 if (show) {
3770 if (!r.activity.mVisibleFromServer) {
3771 r.activity.mVisibleFromServer = true;
3772 mNumVisibleActivities++;
3773 if (r.activity.mVisibleFromClient) {
3774 r.activity.makeVisible();
3775 }
3776 }
3777 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003778 r.tmpConfig.setTo(r.newConfig);
3779 if (r.overrideConfig != null) {
3780 r.tmpConfig.updateFrom(r.overrideConfig);
3781 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003782 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003783 + r.activityInfo.name + " with new config " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003784 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003785 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 r.newConfig = null;
3787 }
3788 } else {
3789 if (r.activity.mVisibleFromServer) {
3790 r.activity.mVisibleFromServer = false;
3791 mNumVisibleActivities--;
3792 v.setVisibility(View.INVISIBLE);
3793 }
3794 }
3795 }
3796 }
3797
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003798 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003799 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003800 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3801 return;
3802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003803 r.activity.mConfigChangeFlags |= configChanges;
3804
3805 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003806 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003808 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003809 TAG, "Finishing stop of " + r + ": show=" + show
3810 + " win=" + r.window);
3811
3812 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003813
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003814 // Make sure any pending writes are now committed.
3815 if (!r.isPreHoneycomb()) {
3816 QueuedWork.waitToFinish();
3817 }
3818
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003819 // Schedule the call to tell the activity manager we have
3820 // stopped. We don't do this immediately, because we want to
3821 // have a chance for any other pending work (in particular memory
3822 // trim requests) to complete before you tell the activity
3823 // manager to proceed and allow us to go fully into the background.
3824 info.activity = r;
3825 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003826 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003827 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003828 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 }
3830
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003831 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3832 String action) {
3833 if (r == null) {
3834 return true;
3835 }
3836 if (seq < r.lastProcessedSeq) {
3837 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3838 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3839 return false;
3840 }
3841 r.lastProcessedSeq = seq;
3842 return true;
3843 }
3844
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003846 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 if (r.stopped) {
3848 r.activity.performRestart();
3849 r.stopped = false;
3850 }
3851 }
3852
Romain Guy65b345f2011-07-27 18:51:50 -07003853 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003854 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003855
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003856 if (r == null) {
3857 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3858 return;
3859 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003862 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 } else if (show && r.stopped) {
3864 // If we are getting ready to gc after going to the background, well
3865 // we are back active so skip it.
3866 unscheduleGcIdler();
3867
3868 r.activity.performRestart();
3869 r.stopped = false;
3870 }
3871 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003872 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 TAG, "Handle window " + r + " visibility: " + show);
3874 updateVisibility(r, show);
3875 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003876 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 }
3878
Romain Guy65b345f2011-07-27 18:51:50 -07003879 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003880 ActivityClientRecord r = mActivities.get(token);
3881
3882 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003883 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003884 return;
3885 }
3886
3887 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003888 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003889 try {
3890 // Now we are idle.
3891 r.activity.performStop();
3892 } catch (Exception e) {
3893 if (!mInstrumentation.onException(r.activity, e)) {
3894 throw new RuntimeException(
3895 "Unable to stop activity "
3896 + r.intent.getComponent().toShortString()
3897 + ": " + e.toString(), e);
3898 }
3899 }
3900 r.stopped = true;
3901 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003902
3903 // Make sure any pending writes are now committed.
3904 if (!r.isPreHoneycomb()) {
3905 QueuedWork.waitToFinish();
3906 }
3907
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003908 // Tell activity manager we slept.
3909 try {
3910 ActivityManagerNative.getDefault().activitySlept(r.token);
3911 } catch (RemoteException ex) {
3912 }
3913 } else {
3914 if (r.stopped && r.activity.mVisibleFromServer) {
3915 r.activity.performRestart();
3916 r.stopped = false;
3917 }
3918 }
3919 }
3920
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003921 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003922 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003923 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003924 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003925 onCoreSettingsChange();
3926 }
3927
3928 private void onCoreSettingsChange() {
3929 boolean debugViewAttributes =
3930 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3931 if (debugViewAttributes != View.mDebugViewAttributes) {
3932 View.mDebugViewAttributes = debugViewAttributes;
3933
3934 // request all activities to relaunch for the changes to take place
3935 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003936 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
3937 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07003938 }
3939 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003940 }
3941
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003942 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3943 LoadedApk apk = peekPackageInfo(data.pkg, false);
3944 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003945 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003946 }
3947 apk = peekPackageInfo(data.pkg, true);
3948 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003949 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003950 }
3951 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003952 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003953 }
3954
Romain Guy65b345f2011-07-27 18:51:50 -07003955 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956 final int N = results.size();
3957 for (int i=0; i<N; i++) {
3958 ResultInfo ri = results.get(i);
3959 try {
3960 if (ri.mData != null) {
3961 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003962 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003963 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003964 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003965 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 r.activity.dispatchActivityResult(ri.mResultWho,
3967 ri.mRequestCode, ri.mResultCode, ri.mData);
3968 } catch (Exception e) {
3969 if (!mInstrumentation.onException(r.activity, e)) {
3970 throw new RuntimeException(
3971 "Failure delivering result " + ri + " to activity "
3972 + r.intent.getComponent().toShortString()
3973 + ": " + e.toString(), e);
3974 }
3975 }
3976 }
3977 }
3978
Romain Guy65b345f2011-07-27 18:51:50 -07003979 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003980 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003981 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 if (r != null) {
3983 final boolean resumed = !r.paused;
3984 if (!r.activity.mFinished && r.activity.mDecor != null
3985 && r.hideForNow && resumed) {
3986 // We had hidden the activity because it started another
3987 // one... we have gotten a result back and we are not
3988 // paused, so make sure our window is visible.
3989 updateVisibility(r, true);
3990 }
3991 if (resumed) {
3992 try {
3993 // Now we are idle.
3994 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003995 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 mInstrumentation.callActivityOnPause(r.activity);
3997 if (!r.activity.mCalled) {
3998 throw new SuperNotCalledException(
3999 "Activity " + r.intent.getComponent().toShortString()
4000 + " did not call through to super.onPause()");
4001 }
4002 } catch (SuperNotCalledException e) {
4003 throw e;
4004 } catch (Exception e) {
4005 if (!mInstrumentation.onException(r.activity, e)) {
4006 throw new RuntimeException(
4007 "Unable to pause activity "
4008 + r.intent.getComponent().toShortString()
4009 + ": " + e.toString(), e);
4010 }
4011 }
4012 }
4013 deliverResults(r, res.results);
4014 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004015 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004016 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004017 }
4018 }
4019 }
4020
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004021 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 return performDestroyActivity(token, finishing, 0, false);
4023 }
4024
Romain Guy65b345f2011-07-27 18:51:50 -07004025 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004026 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004027 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004028 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004029 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004031 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 r.activity.mConfigChangeFlags |= configChanges;
4033 if (finishing) {
4034 r.activity.mFinished = true;
4035 }
4036 if (!r.paused) {
4037 try {
4038 r.activity.mCalled = false;
4039 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08004040 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 r.activity.getComponentName().getClassName());
4042 if (!r.activity.mCalled) {
4043 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004044 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 + " did not call through to super.onPause()");
4046 }
4047 } catch (SuperNotCalledException e) {
4048 throw e;
4049 } catch (Exception e) {
4050 if (!mInstrumentation.onException(r.activity, e)) {
4051 throw new RuntimeException(
4052 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004053 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 + ": " + e.toString(), e);
4055 }
4056 }
4057 r.paused = true;
4058 }
4059 if (!r.stopped) {
4060 try {
4061 r.activity.performStop();
4062 } catch (SuperNotCalledException e) {
4063 throw e;
4064 } catch (Exception e) {
4065 if (!mInstrumentation.onException(r.activity, e)) {
4066 throw new RuntimeException(
4067 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004068 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 + ": " + e.toString(), e);
4070 }
4071 }
4072 r.stopped = true;
4073 }
4074 if (getNonConfigInstance) {
4075 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004076 r.lastNonConfigurationInstances
4077 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 } catch (Exception e) {
4079 if (!mInstrumentation.onException(r.activity, e)) {
4080 throw new RuntimeException(
4081 "Unable to retain activity "
4082 + r.intent.getComponent().toShortString()
4083 + ": " + e.toString(), e);
4084 }
4085 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 }
4087 try {
4088 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004089 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 if (!r.activity.mCalled) {
4091 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004092 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 " did not call through to super.onDestroy()");
4094 }
4095 if (r.window != null) {
4096 r.window.closeAllPanels();
4097 }
4098 } catch (SuperNotCalledException e) {
4099 throw e;
4100 } catch (Exception e) {
4101 if (!mInstrumentation.onException(r.activity, e)) {
4102 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004103 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4104 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
4106 }
4107 }
4108 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004109 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 return r;
4111 }
4112
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004113 private static String safeToComponentShortString(Intent intent) {
4114 ComponentName component = intent.getComponent();
4115 return component == null ? "[Unknown]" : component.toShortString();
4116 }
4117
Romain Guy65b345f2011-07-27 18:51:50 -07004118 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004120 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 configChanges, getNonConfigInstance);
4122 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004123 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004124 WindowManager wm = r.activity.getWindowManager();
4125 View v = r.activity.mDecor;
4126 if (v != null) {
4127 if (r.activity.mVisibleFromServer) {
4128 mNumVisibleActivities--;
4129 }
4130 IBinder wtoken = v.getWindowToken();
4131 if (r.activity.mWindowAdded) {
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004132 if (r.onlyLocalRequest || r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004133 // Hold off on removing this until the new activity's
4134 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004135 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004136 r.mPendingRemoveWindowManager = wm;
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004137 if (r.mPreserveWindow) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004138 // We can only keep the part of the view hierarchy that we control,
4139 // everything else must be removed, because it might not be able to
4140 // behave properly when activity is relaunching.
4141 r.window.clearContentView();
4142 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004143 } else {
4144 wm.removeViewImmediate(v);
4145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004147 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004148 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 r.activity.getClass().getName(), "Activity");
4150 }
4151 r.activity.mDecor = null;
4152 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004153 if (r.mPendingRemoveWindow == null) {
4154 // If we are delaying the removal of the activity window, then
4155 // we can't clean up all windows here. Note that we can't do
4156 // so later either, which means any windows that aren't closed
4157 // by the app will leak. Well we try to warning them a lot
4158 // about leaking windows, because that is a bug, so if they are
4159 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004160 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004161 r.activity.getClass().getName(), "Activity");
4162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163
4164 // Mocked out contexts won't be participating in the normal
4165 // process lifecycle, but if we're running with a proper
4166 // ApplicationContext we need to have it tear down things
4167 // cleanly.
4168 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004169 if (c instanceof ContextImpl) {
4170 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 r.activity.getClass().getName(), "Activity");
4172 }
4173 }
4174 if (finishing) {
4175 try {
4176 ActivityManagerNative.getDefault().activityDestroyed(token);
4177 } catch (RemoteException ex) {
4178 // If the system process has died, it's game over for everyone.
4179 }
4180 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004181 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 }
4183
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004184 /**
4185 * @param preserveWindow Whether the activity should try to reuse the window it created,
4186 * including the decor view after the relaunch.
4187 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004188 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004189 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004190 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004191 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004192 ActivityClientRecord target = null;
4193
Craig Mautner88c05892013-06-28 09:47:45 -07004194 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004195 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4196 ActivityClientRecord r = mRelaunchingActivities.get(i);
4197 if (r.token == token) {
4198 target = r;
4199 if (pendingResults != null) {
4200 if (r.pendingResults != null) {
4201 r.pendingResults.addAll(pendingResults);
4202 } else {
4203 r.pendingResults = pendingResults;
4204 }
4205 }
4206 if (pendingNewIntents != null) {
4207 if (r.pendingIntents != null) {
4208 r.pendingIntents.addAll(pendingNewIntents);
4209 } else {
4210 r.pendingIntents = pendingNewIntents;
4211 }
4212 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004213
4214 // For each relaunch request, activity manager expects an answer
4215 if (!r.onlyLocalRequest && fromServer) {
4216 try {
4217 ActivityManagerNative.getDefault().activityRelaunched(token);
4218 } catch (RemoteException e) {
4219 e.printStackTrace();
4220 }
4221 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004222 break;
4223 }
4224 }
4225
4226 if (target == null) {
4227 target = new ActivityClientRecord();
4228 target.token = token;
4229 target.pendingResults = pendingResults;
4230 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004231 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004232 if (!fromServer) {
Wale Ogunwale32ebbd82016-01-26 08:47:20 -08004233 ActivityClientRecord existing = mActivities.get(token);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004234 if (existing != null) {
4235 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004236 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004237 }
4238 target.onlyLocalRequest = true;
4239 }
4240 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004241 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004242 }
4243
4244 if (fromServer) {
4245 target.startsNotResumed = notResumed;
4246 target.onlyLocalRequest = false;
4247 }
4248 if (config != null) {
4249 target.createdConfig = config;
4250 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004251 if (overrideConfig != null) {
4252 target.overrideConfig = overrideConfig;
4253 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004254 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004255 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004256 }
Wale Ogunwale32ebbd82016-01-26 08:47:20 -08004257 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this
4258 + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004259 }
4260
Romain Guy65b345f2011-07-27 18:51:50 -07004261 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004262 // If we are getting ready to gc after going to the background, well
4263 // we are back active so skip it.
4264 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004265 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266
4267 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004268 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 // First: make sure we have the most recent configuration and most
4271 // recent version of the activity, or skip it if some previous call
4272 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004273 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 int N = mRelaunchingActivities.size();
4275 IBinder token = tmp.token;
4276 tmp = null;
4277 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004278 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279 if (r.token == token) {
4280 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004281 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004282 mRelaunchingActivities.remove(i);
4283 i--;
4284 N--;
4285 }
4286 }
Bob Leee5408332009-09-04 18:31:17 -07004287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004288 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004289 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 return;
4291 }
Bob Leee5408332009-09-04 18:31:17 -07004292
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004293 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4294 + tmp.token + " with configChanges=0x"
4295 + Integer.toHexString(configChanges));
4296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 if (mPendingConfiguration != null) {
4298 changedConfig = mPendingConfiguration;
4299 mPendingConfiguration = null;
4300 }
4301 }
Bob Leee5408332009-09-04 18:31:17 -07004302
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004303 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4304 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4305 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4306 } else {
4307 tmp.lastProcessedSeq = tmp.relaunchSeq;
4308 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004309 if (tmp.createdConfig != null) {
4310 // If the activity manager is passing us its current config,
4311 // assume that is really what we want regardless of what we
4312 // may have pending.
4313 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004314 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4315 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4316 if (changedConfig == null
4317 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4318 changedConfig = tmp.createdConfig;
4319 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004320 }
4321 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004322
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004323 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004324 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 // If there was a pending configuration change, execute it first.
4327 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004328 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004329 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004330 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 }
Bob Leee5408332009-09-04 18:31:17 -07004332
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004333 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004334 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004336 if (!tmp.onlyLocalRequest) {
4337 try {
4338 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4339 } catch (RemoteException e) {
4340 // If the system process has died, it's game over for everyone.
4341 }
4342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 return;
4344 }
Bob Leee5408332009-09-04 18:31:17 -07004345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004347 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004348 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004349 r.lastProcessedSeq = tmp.lastProcessedSeq;
4350 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004351 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004352
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004353 r.activity.mChangingConfigurations = true;
4354
Robert Carr23fa16b2016-01-13 13:19:58 -08004355 // If we are preserving the main window across relaunches we would also like to preserve
4356 // the children. However the client side view system does not support preserving
4357 // the child views so we notify the window manager to expect these windows to
4358 // be replaced and defer requests to destroy or hide them. This way we can achieve
4359 // visual continuity. It's important that we do this here prior to pause and destroy
4360 // as that is when we may hide or remove the child views.
4361 try {
4362 if (r.mPreserveWindow) {
4363 WindowManagerGlobal.getWindowSession().prepareToReplaceChildren(r.token);
4364 }
4365 } catch (RemoteException e) {
4366 // If the system process has died, it's game over for everyone.
4367 }
4368
4369
Dianne Hackborne2b04802010-12-09 09:24:55 -08004370 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08004372 performPauseActivity(r.token, false, r.isPreHoneycomb());
4373 }
4374 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004375 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
Bob Leee5408332009-09-04 18:31:17 -07004377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 r.activity = null;
4381 r.window = null;
4382 r.hideForNow = false;
4383 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004384 // Merge any pending results and pending intents; don't just replace them
4385 if (tmp.pendingResults != null) {
4386 if (r.pendingResults == null) {
4387 r.pendingResults = tmp.pendingResults;
4388 } else {
4389 r.pendingResults.addAll(tmp.pendingResults);
4390 }
4391 }
4392 if (tmp.pendingIntents != null) {
4393 if (r.pendingIntents == null) {
4394 r.pendingIntents = tmp.pendingIntents;
4395 } else {
4396 r.pendingIntents.addAll(tmp.pendingIntents);
4397 }
4398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004400 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004401
Christopher Tateb70f3df2009-04-07 16:07:59 -07004402 handleLaunchActivity(r, currentIntent);
Jorim Jaggife89d122015-12-22 16:28:44 +01004403
4404 if (!tmp.onlyLocalRequest) {
4405 try {
4406 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004407 if (r.window != null) {
4408 r.window.reportActivityRelaunched();
4409 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004410 } catch (RemoteException e) {
4411 // If the system process has died, it's game over for everyone.
4412 }
4413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 }
4415
Craig Mautnera0026042014-04-23 11:45:37 -07004416 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4417 r.state = new Bundle();
4418 r.state.setAllowFds(false);
4419 if (r.isPersistable()) {
4420 r.persistentState = new PersistableBundle();
4421 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4422 r.persistentState);
4423 } else {
4424 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4425 }
4426 }
4427
Dianne Hackborn73c14162012-09-19 15:45:06 -07004428 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004430 ArrayList<ComponentCallbacks2> callbacks
4431 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004432
Craig Mautner88c05892013-06-28 09:47:45 -07004433 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004434 final int NAPP = mAllApplications.size();
4435 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004436 callbacks.add(mAllApplications.get(i));
4437 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004438 final int NACT = mActivities.size();
4439 for (int i=0; i<NACT; i++) {
4440 ActivityClientRecord ar = mActivities.valueAt(i);
4441 Activity a = ar.activity;
4442 if (a != null) {
4443 Configuration thisConfig = applyConfigCompatMainThread(
4444 mCurDefaultDisplayDpi, newConfig,
4445 ar.packageInfo.getCompatibilityInfo());
4446 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4447 // If the activity is currently resumed, its configuration
4448 // needs to change right now.
4449 callbacks.add(a);
4450 } else if (thisConfig != null) {
4451 // Otherwise, we will tell it about the change
4452 // the next time it is resumed or shown. Note that
4453 // the activity manager may, before then, decide the
4454 // activity needs to be destroyed to handle its new
4455 // configuration.
4456 if (DEBUG_CONFIGURATION) {
4457 Slog.v(TAG, "Setting activity "
4458 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004459 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004460 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 }
4462 }
4463 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004464 final int NSVC = mServices.size();
4465 for (int i=0; i<NSVC; i++) {
4466 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 }
4468 }
4469 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004470 final int NPRV = mLocalProviders.size();
4471 for (int i=0; i<NPRV; i++) {
4472 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004473 }
4474 }
Bob Leee5408332009-09-04 18:31:17 -07004475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 return callbacks;
4477 }
Bob Leee5408332009-09-04 18:31:17 -07004478
Filip Gruszczynskica664812015-12-04 12:43:36 -08004479 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config,
4480 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004481 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004482 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004483 // we check the runtime type and act accordingly.
4484 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4485 if (activity != null) {
4486 activity.mCalled = false;
4487 }
Bob Leee5408332009-09-04 18:31:17 -07004488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 boolean shouldChangeConfig = false;
4490 if ((activity == null) || (activity.mCurrentConfig == null)) {
4491 shouldChangeConfig = true;
4492 } else {
Bob Leee5408332009-09-04 18:31:17 -07004493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 // If the new config is the same as the config this Activity
4495 // is already running with then don't bother calling
4496 // onConfigurationChanged
4497 int diff = activity.mCurrentConfig.diff(config);
4498 if (diff != 0) {
Wale Ogunwale14e811f2015-12-14 15:04:22 -08004499 // If this activity doesn't handle any of the config changes then don't bother
4500 // calling onConfigurationChanged as we're going to destroy it.
4501 // Except in the case where the configuration changed on the activity manager side,
4502 // but wasn't big enough to cause a resource change so the activity wasn't destroyed.
4503 // In this case we still want to change the configuration of the activity but not
4504 // report it to the app.
4505 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4506 || !reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 shouldChangeConfig = true;
4508 }
4509 }
4510 }
Bob Leee5408332009-09-04 18:31:17 -07004511
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004512 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004513 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 if (shouldChangeConfig) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004515 if (reportToActivity) {
4516 cb.onConfigurationChanged(config);
4517 }
Bob Leee5408332009-09-04 18:31:17 -07004518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004519 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004520 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 throw new SuperNotCalledException(
4522 "Activity " + activity.getLocalClassName() +
4523 " did not call through to super.onConfigurationChanged()");
4524 }
4525 activity.mConfigChangeFlags = 0;
4526 activity.mCurrentConfig = new Configuration(config);
4527 }
4528 }
4529 }
4530
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004531 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004532 synchronized (mResourcesManager) {
4533 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004534 }
4535 }
4536
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004537 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004538 Configuration config = mConfiguration;
4539 if (mCompatConfiguration == null) {
4540 mCompatConfiguration = new Configuration();
4541 }
4542 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004543 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004544 config = mCompatConfiguration;
4545 }
4546 return config;
4547 }
4548
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004549 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004550
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004551 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004552
Craig Mautner88c05892013-06-28 09:47:45 -07004553 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004554 if (mPendingConfiguration != null) {
4555 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4556 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004557 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004558 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004559 }
4560 mPendingConfiguration = null;
4561 }
4562
4563 if (config == null) {
4564 return;
4565 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004566
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004567 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004568 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004569
4570 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 if (mConfiguration == null) {
4573 mConfiguration = new Configuration();
4574 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004575 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004576 return;
4577 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004578
4579 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004580 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004581
4582 final Theme systemTheme = getSystemContext().getTheme();
4583 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4584 systemTheme.rebase();
4585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004587
4588 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4589
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004590 freeTextLayoutCachesIfNeeded(configDiff);
4591
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004592 if (callbacks != null) {
4593 final int N = callbacks.size();
4594 for (int i=0; i<N; i++) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004595 performConfigurationChanged(callbacks.get(i), config, REPORT_TO_ACTIVITY);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 }
4598 }
4599
Romain Guy46bfc482013-08-16 18:38:29 -07004600 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004601 if (configDiff != 0) {
4602 // Ask text layout engine to free its caches if there is a locale change
4603 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4604 if (hasLocaleConfigChange) {
4605 Canvas.freeTextLayoutCaches();
4606 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4607 }
4608 }
4609 }
4610
Filip Gruszczynskica664812015-12-04 12:43:36 -08004611 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4612 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004613 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 if (r == null || r.activity == null) {
4615 return;
4616 }
Bob Leee5408332009-09-04 18:31:17 -07004617
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004618 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004619 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004620
4621 r.tmpConfig.setTo(mCompatConfiguration);
4622 if (data.overrideConfig != null) {
4623 r.overrideConfig = data.overrideConfig;
4624 r.tmpConfig.updateFrom(data.overrideConfig);
4625 }
Filip Gruszczynskica664812015-12-04 12:43:36 -08004626 performConfigurationChanged(r.activity, r.tmpConfig, reportToActivity);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004627
4628 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004629
4630 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 }
4632
Jeff Hao1b012d32014-08-20 10:35:34 -07004633 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004634 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004635 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004636 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004637 default:
4638 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004639 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004640 break;
4641 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004642 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004643 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004644 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004645 } finally {
4646 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004647 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004648 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004649 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004650 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004651 }
4652 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004653 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004654 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004655 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004656 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004657 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004658 }
4659 }
Bob Leee5408332009-09-04 18:31:17 -07004660
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004661 /**
4662 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4663 * so that profiler data won't be lost.
4664 *
4665 * @hide
4666 */
4667 public void stopProfiling() {
4668 mProfiler.stopProfiling();
4669 }
4670
Romain Guya998dff2012-03-23 18:58:36 -07004671 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004672 if (managed) {
4673 try {
4674 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4675 } catch (IOException e) {
4676 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4677 + " -- can the process access this path?");
4678 } finally {
4679 try {
4680 dhd.fd.close();
4681 } catch (IOException e) {
4682 Slog.w(TAG, "Failure closing profile fd", e);
4683 }
4684 }
4685 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004686 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004687 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004688 try {
4689 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4690 } catch (RemoteException e) {
4691 }
Andy McFadden824c5102010-07-09 16:26:57 -07004692 }
4693
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004694 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4695 boolean hasPkgInfo = false;
4696 if (packages != null) {
mark_chen89764e32014-12-12 15:38:48 +08004697 synchronized (mResourcesManager) {
4698 for (int i=packages.length-1; i>=0; i--) {
4699 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4700 if (!hasPkgInfo) {
4701 WeakReference<LoadedApk> ref;
4702 ref = mPackages.get(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004703 if (ref != null && ref.get() != null) {
4704 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004705 } else {
4706 ref = mResourcePackages.get(packages[i]);
4707 if (ref != null && ref.get() != null) {
4708 hasPkgInfo = true;
4709 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004710 }
4711 }
mark_chen89764e32014-12-12 15:38:48 +08004712 mPackages.remove(packages[i]);
4713 mResourcePackages.remove(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004714 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004715 }
4716 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004717 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004718 hasPkgInfo);
4719 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004722 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004724 final int N = callbacks.size();
4725 for (int i=0; i<N; i++) {
4726 callbacks.get(i).onLowMemory();
4727 }
4728
Chris Tatece229052009-03-25 16:44:52 -07004729 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4730 if (Process.myUid() != Process.SYSTEM_UID) {
4731 int sqliteReleased = SQLiteDatabase.releaseMemory();
4732 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4733 }
Bob Leee5408332009-09-04 18:31:17 -07004734
Mike Reedcaf0df12009-04-27 14:32:05 -04004735 // Ask graphics to free up as much as possible (font/image caches)
4736 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004737
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004738 // Ask text layout engine to free also as much as possible
4739 Canvas.freeTextLayoutCaches();
4740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 BinderInternal.forceGc("mem");
4742 }
4743
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004744 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004745 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004746
Dianne Hackborn73c14162012-09-19 15:45:06 -07004747 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004748
4749 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004750 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004751 callbacks.get(i).onTrimMemory(level);
4752 }
Romain Guy19f86e82012-04-23 15:19:07 -07004753
John Reckf47a5942014-06-30 16:20:04 -07004754 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004755 }
4756
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004757 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004758 if (Process.isIsolated()) {
4759 // Isolated processes aren't going to do UI.
4760 return;
4761 }
Romain Guya9582652011-11-10 14:20:10 -08004762 try {
4763 int uid = Process.myUid();
4764 String[] packages = getPackageManager().getPackagesForUid(uid);
4765
4766 // If there are several packages in this application we won't
Tim Murraye1e6c662015-04-07 13:24:14 -07004767 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004768 if (packages != null && packages.length == 1) {
John Reck51aaf902015-12-02 15:08:07 -08004769 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004770 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004771 }
4772 } catch (RemoteException e) {
4773 // Ignore
4774 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004775 }
4776
Calin Juravled96d2dd2016-02-02 23:28:39 +00004777 // Keep in sync with installd (frameworks/native/cmds/installd/commands.cpp).
4778 private static File getPrimaryProfileFile(String packageName) {
4779 return new File("/data/misc/profiles/cur/" + UserHandle.myUserId() +
4780 "/" + packageName + "/primary.prof");
4781 }
4782
Calin Juravle69052392015-12-17 17:06:49 +02004783 private static void setupJitProfileSupport(LoadedApk loadedApk, File cacheDir) {
Calin Juravledb4a79a2015-12-23 18:55:08 +02004784 if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
4785 return;
4786 }
Calin Juravle69052392015-12-17 17:06:49 +02004787 final ApplicationInfo appInfo = loadedApk.getApplicationInfo();
Calin Juravle69052392015-12-17 17:06:49 +02004788 final List<String> codePaths = new ArrayList<>();
4789 if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
4790 codePaths.add(appInfo.sourceDir);
4791 }
4792 if (appInfo.splitSourceDirs != null) {
4793 Collections.addAll(codePaths, appInfo.splitSourceDirs);
4794 }
4795
4796 if (codePaths.isEmpty()) {
4797 // If there are no code paths there's no need to setup a profile file and register with
4798 // the runtime,
4799 return;
4800 }
4801
Calin Juravled96d2dd2016-02-02 23:28:39 +00004802 final File profileFile = getPrimaryProfileFile(loadedApk.mPackageName);
Calin Juravle69052392015-12-17 17:06:49 +02004803 if (!profileFile.exists()) {
4804 FileDescriptor fd = null;
4805 try {
4806 final int permissions = 0600; // read-write for user.
4807 fd = Os.open(profileFile.getAbsolutePath(), OsConstants.O_CREAT, permissions);
4808 Os.fchmod(fd, permissions);
4809 Os.fchown(fd, appInfo.uid, appInfo.uid);
4810 } catch (ErrnoException e) {
Calin Juravled96d2dd2016-02-02 23:28:39 +00004811 Log.v(TAG, "Unable to create jit profile file " + profileFile, e);
Calin Juravle69052392015-12-17 17:06:49 +02004812 try {
4813 Os.unlink(profileFile.getAbsolutePath());
4814 } catch (ErrnoException unlinkErr) {
4815 Log.v(TAG, "Unable to unlink jit profile file " + profileFile, unlinkErr);
4816 }
4817 return;
4818 } finally {
4819 IoUtils.closeQuietly(fd);
4820 }
4821 }
4822
4823 VMRuntime.registerAppInfo(profileFile.getAbsolutePath(), appInfo.dataDir,
4824 codePaths.toArray(new String[codePaths.size()]));
4825 }
4826
Dianne Hackborndde331c2012-08-03 14:01:57 -07004827 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05004828 final int densityDpi = mCurDefaultDisplayDpi;
4829 if (!mDensityCompatMode
4830 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
4831 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
4832 DisplayMetrics.DENSITY_DEVICE = densityDpi;
4833 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07004834 }
4835 }
4836
Romain Guy65b345f2011-07-27 18:51:50 -07004837 private void handleBindApplication(AppBindData data) {
Man Caocfa78b22015-06-11 20:14:34 -07004838 if (data.trackAllocation) {
4839 DdmVmInternal.enableRecentAllocations(true);
4840 }
4841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004842 mBoundApplication = data;
4843 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004844 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004845
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004846 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004847 if (data.initProfilerInfo != null) {
4848 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4849 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4850 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4851 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4852 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004854 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004855 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004856 android.ddm.DdmHandleAppName.setAppName(data.processName,
4857 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004858
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004859 if (data.persistent) {
4860 // Persistent processes on low-memory devices do not get to
4861 // use hardware accelerated drawing, since this can add too much
4862 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004863 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08004864 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004865 }
4866 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004867
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004868 if (mProfiler.profileFd != null) {
4869 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004870 }
4871
Joe Onoratod630f102011-03-17 18:42:26 -07004872 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4873 // implementation to use the pool executor. Normally, we use the
4874 // serialized executor as the default. This has to happen in the
4875 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004876 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004877 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4878 }
4879
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004880 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882 /*
4883 * Before spawning a new process, reset the time zone to be the system time zone.
4884 * This needs to be done because the system time zone could have changed after the
4885 * the spawning of this process. Without doing this this process would have the incorrect
4886 * system time zone.
4887 */
4888 TimeZone.setDefault(null);
4889
4890 /*
Seigo Nonakacada57a2016-01-22 17:22:11 +09004891 * Initialize the default locales in this process for the reasons we set the time zone.
Roozbeh Pournader834641b2016-01-23 22:34:57 -08004892 *
4893 * We do this through ResourcesManager, since we need to do locale negotiation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 */
Roozbeh Pournader834641b2016-01-23 22:34:57 -08004895 mResourcesManager.setDefaultLocalesLocked(data.config.getLocales());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004897 /*
4898 * Update the system configuration since its preloaded and might not
4899 * reflect configuration changes. The configuration object passed
4900 * in AppBindData can be safely assumed to be up to date
4901 */
Craig Mautner88c05892013-06-28 09:47:45 -07004902 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004903 mCurDefaultDisplayDpi = data.config.densityDpi;
4904 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004905
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004906 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907
Dianne Hackborndde331c2012-08-03 14:01:57 -07004908 /**
4909 * Switch this process to density compatibility mode if needed.
4910 */
4911 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4912 == 0) {
4913 mDensityCompatMode = true;
4914 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4915 }
4916 updateDefaultDensity();
4917
Narayan Kamathccb2a0862013-12-19 14:49:36 +00004918 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4919 DateFormat.set24HourTimePref(is24Hr);
4920
Jon Miranda836c0a82014-08-11 12:32:26 -07004921 View.mDebugViewAttributes =
4922 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4923
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004924 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004925 * For system applications on userdebug/eng builds, log stack
4926 * traces of disk and network access to dropbox for analysis.
4927 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004928 if ((data.appInfo.flags &
4929 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004930 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4931 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004932 }
4933
4934 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07004935 * For apps targetting Honeycomb or later, we don't allow network usage
4936 * on the main event loop / UI thread. This is what ultimately throws
4937 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004938 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07004939 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004940 StrictMode.enableDeathOnNetwork();
4941 }
4942
Jeff Sharkey344744b2016-01-28 19:03:30 -07004943 /**
4944 * For apps targetting N or later, we don't allow file:// Uri exposure.
4945 * This is what ultimately throws {@link FileUriExposedException}.
4946 */
4947 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00004948 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07004949 }
4950
Alex Klyubinf9034cc2015-02-12 11:43:09 -08004951 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
4952 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
4953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4955 // XXX should have option to change the port.
4956 Debug.changeDebugPort(8100);
4957 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004958 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 + " is waiting for the debugger on port 8100...");
4960
4961 IActivityManager mgr = ActivityManagerNative.getDefault();
4962 try {
4963 mgr.showWaitingForDebugger(mAppThread, true);
4964 } catch (RemoteException ex) {
4965 }
4966
4967 Debug.waitForDebugger();
4968
4969 try {
4970 mgr.showWaitingForDebugger(mAppThread, false);
4971 } catch (RemoteException ex) {
4972 }
4973
4974 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004975 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 + " can be debugged on port 8100...");
4977 }
4978 }
4979
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004980 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04004981 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4982 Trace.setAppTracingAllowed(isAppDebuggable);
4983 if (isAppDebuggable && data.enableBinderTracking) {
4984 Binder.enableTracing();
4985 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004986
Robert Greenwalt434203a2010-10-11 16:00:27 -07004987 /**
4988 * Initialize the default http proxy in this process for the reasons we set the time zone.
4989 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04004990 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004991 if (b != null) {
4992 // In pre-boot mode (doing initial launch to collect password), not
4993 // all system is up. This includes the connectivity service, so don't
4994 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04004995 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004996 try {
Paul Jensencee9b512015-05-06 07:32:40 -04004997 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04004998 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004999 } catch (RemoteException e) {}
5000 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07005001
Alan Viverette2107d692015-09-03 14:55:27 -04005002 // Instrumentation info affects the class loader, so load it before
5003 // setting up the app context.
5004 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005007 ii = new ApplicationPackageManager(null, getPackageManager())
5008 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005011 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005012 }
5013
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005014 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005016 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
5017 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005019 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5020 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005021 } else {
5022 ii = null;
5023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005024
Alan Viverette2107d692015-09-03 14:55:27 -04005025 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005026 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005027 // This cache location probably points at credential-encrypted
5028 // storage which may not be accessible yet; assign it anyway instead
5029 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005030 final File cacheDir = appContext.getCacheDir();
5031 if (cacheDir != null) {
5032 // Provide a usable directory for temporary files
5033 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5034 } else {
5035 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5036 + "due to missing cache directory");
5037 }
Alan Viverette346296b2015-09-01 13:08:05 -04005038
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005039 // Setup a location to store generated/compiled graphics code and
5040 // JIT profiling data. Note that this data is stored in a
5041 // device-encrypted storage area, so these caches must never contain
5042 // user sensitive user data.
5043 final Context deviceContext = appContext.createDeviceEncryptedStorageContext();
5044 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005045 if (codeCacheDir != null) {
5046 setupGraphicsSupport(data.info, codeCacheDir);
Calin Juravlec74d3382016-01-07 12:15:36 +00005047 setupJitProfileSupport(data.info, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005048 } else {
Calin Juravlec74d3382016-01-07 12:15:36 +00005049 Log.e(TAG, "Unable to setupGraphicsSupport and setupJitProfileSupport " +
5050 "due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005051 }
Alan Viverette2107d692015-09-03 14:55:27 -04005052
Michael Lentine2ba303f2016-02-01 20:44:34 -06005053 // Add the lib dir path to hardware renderer so that vulkan layers
5054 // can be searched for within that directory.
5055 ThreadedRenderer.setLibDir(data.info.getLibDir());
5056 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005057
Chad Brubaker78d47122015-11-17 22:26:58 -08005058 // Install the Network Security Config Provider. This must happen before the application
5059 // code is loaded to prevent issues with instances of TLS objects being created before
5060 // the provider is installed.
5061 NetworkSecurityConfigProvider.install(appContext);
5062
Alan Viverette2107d692015-09-03 14:55:27 -04005063 // Continue loading instrumentation.
5064 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005065 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005066 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005067 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005068 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005069 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005070 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071
5072 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005073 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 mInstrumentation = (Instrumentation)
5075 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5076 } catch (Exception e) {
5077 throw new RuntimeException(
5078 "Unable to instantiate instrumentation "
5079 + data.instrumentationName + ": " + e.toString(), e);
5080 }
5081
Alan Viverettebe64eae2015-09-03 14:56:04 -04005082 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5083 mInstrumentation.init(this, instrContext, appContext, component,
5084 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005086 if (mProfiler.profileFile != null && !ii.handleProfiling
5087 && mProfiler.profileFd == null) {
5088 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005089 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 file.getParentFile().mkdirs();
5091 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005093 } else {
5094 mInstrumentation = new Instrumentation();
5095 }
5096
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005097 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005098 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005099 } else {
5100 // Small heap, clamp to the current growth limit and let the heap release
5101 // pages after the growth limit to the non growth limit capacity. b/18387825
5102 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005103 }
5104
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005105 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005106 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005107 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005108 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005110 // If the app is being launched for full backup or restore, bring it up in
5111 // a restricted environment with the base application class.
5112 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5113 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005114
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005115 // don't bring up providers in restricted mode; they may depend on the
5116 // app's custom Application class
5117 if (!data.restrictedBackupMode) {
5118 List<ProviderInfo> providers = data.providers;
5119 if (providers != null) {
5120 installContentProviders(app, providers);
5121 // For process that contains content providers, we want to
5122 // ensure that the JIT is enabled "at some point".
5123 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5124 }
5125 }
5126
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005127 // Do this after providers, since instrumentation tests generally start their
5128 // test thread at this point, and we don't want that racing.
5129 try {
5130 mInstrumentation.onCreate(data.instrumentationArgs);
5131 }
5132 catch (Exception e) {
5133 throw new RuntimeException(
5134 "Exception thrown in onCreate() of "
5135 + data.instrumentationName + ": " + e.toString(), e);
5136 }
5137
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005138 try {
5139 mInstrumentation.callApplicationOnCreate(app);
5140 } catch (Exception e) {
5141 if (!mInstrumentation.onException(app, e)) {
5142 throw new RuntimeException(
5143 "Unable to create application " + app.getClass().getName()
5144 + ": " + e.toString(), e);
5145 }
5146 }
5147 } finally {
5148 StrictMode.setThreadPolicy(savedPolicy);
5149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 }
5151
5152 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5153 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005154 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5155 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 Debug.stopMethodTracing();
5157 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005158 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 // + ", app thr: " + mAppThread);
5160 try {
5161 am.finishInstrumentation(mAppThread, resultCode, results);
5162 } catch (RemoteException ex) {
5163 }
5164 }
5165
Romain Guy65b345f2011-07-27 18:51:50 -07005166 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 Context context, List<ProviderInfo> providers) {
5168 final ArrayList<IActivityManager.ContentProviderHolder> results =
5169 new ArrayList<IActivityManager.ContentProviderHolder>();
5170
Romain Guya998dff2012-03-23 18:58:36 -07005171 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005172 if (DEBUG_PROVIDER) {
5173 StringBuilder buf = new StringBuilder(128);
5174 buf.append("Pub ");
5175 buf.append(cpi.authority);
5176 buf.append(": ");
5177 buf.append(cpi.name);
5178 Log.i(TAG, buf.toString());
5179 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005180 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5181 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5182 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005183 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005184 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 }
5186 }
5187
5188 try {
5189 ActivityManagerNative.getDefault().publishContentProviders(
5190 getApplicationThread(), results);
5191 } catch (RemoteException ex) {
5192 }
5193 }
5194
Jeff Sharkey6d515712012-09-20 16:06:08 -07005195 public final IContentProvider acquireProvider(
5196 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005197 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005198 if (provider != null) {
5199 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005200 }
5201
Wale Ogunwale1d646122015-04-24 14:45:14 -07005202 // There is a possible race here. Another thread may try to acquire
5203 // the same provider at the same time. When this happens, we want to ensure
5204 // that the first one wins.
5205 // Note that we cannot hold the lock while acquiring and installing the
5206 // provider since it might take a long time to run and it could also potentially
5207 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005209 try {
5210 holder = ActivityManagerNative.getDefault().getContentProvider(
5211 getApplicationThread(), auth, userId, stable);
5212 } catch (RemoteException ex) {
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005215 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 return null;
5217 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005219 // Install provider will increment the reference count for us, and break
5220 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005221 holder = installProvider(c, holder, holder.info,
5222 true /*noisy*/, holder.noReleaseNeeded, stable);
5223 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 }
5225
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005226 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5227 if (stable) {
5228 prc.stableCount += 1;
5229 if (prc.stableCount == 1) {
5230 // We are acquiring a new stable reference on the provider.
5231 int unstableDelta;
5232 if (prc.removePending) {
5233 // We have a pending remove operation, which is holding the
5234 // last unstable reference. At this point we are converting
5235 // that unstable reference to our new stable reference.
5236 unstableDelta = -1;
5237 // Cancel the removal of the provider.
5238 if (DEBUG_PROVIDER) {
5239 Slog.v(TAG, "incProviderRef: stable "
5240 + "snatched provider from the jaws of death");
5241 }
5242 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005243 // There is a race! It fails to remove the message, which
5244 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005245 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5246 } else {
5247 unstableDelta = 0;
5248 }
5249 try {
5250 if (DEBUG_PROVIDER) {
5251 Slog.v(TAG, "incProviderRef Now stable - "
5252 + prc.holder.info.name + ": unstableDelta="
5253 + unstableDelta);
5254 }
5255 ActivityManagerNative.getDefault().refContentProvider(
5256 prc.holder.connection, 1, unstableDelta);
5257 } catch (RemoteException e) {
5258 //do nothing content provider object is dead any way
5259 }
5260 }
5261 } else {
5262 prc.unstableCount += 1;
5263 if (prc.unstableCount == 1) {
5264 // We are acquiring a new unstable reference on the provider.
5265 if (prc.removePending) {
5266 // Oh look, we actually have a remove pending for the
5267 // provider, which is still holding the last unstable
5268 // reference. We just need to cancel that to take new
5269 // ownership of the reference.
5270 if (DEBUG_PROVIDER) {
5271 Slog.v(TAG, "incProviderRef: unstable "
5272 + "snatched provider from the jaws of death");
5273 }
5274 prc.removePending = false;
5275 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5276 } else {
5277 // First unstable ref, increment our count in the
5278 // activity manager.
5279 try {
5280 if (DEBUG_PROVIDER) {
5281 Slog.v(TAG, "incProviderRef: Now unstable - "
5282 + prc.holder.info.name);
5283 }
5284 ActivityManagerNative.getDefault().refContentProvider(
5285 prc.holder.connection, 0, 1);
5286 } catch (RemoteException e) {
5287 //do nothing content provider object is dead any way
5288 }
5289 }
5290 }
5291 }
5292 }
5293
Jeff Sharkey6d515712012-09-20 16:06:08 -07005294 public final IContentProvider acquireExistingProvider(
5295 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005296 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005297 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005298 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005299 if (pr == null) {
5300 return null;
5301 }
5302
5303 IContentProvider provider = pr.mProvider;
5304 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005305 if (!jBinder.isBinderAlive()) {
5306 // The hosting process of the provider has died; we can't
5307 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005308 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005309 + ": existing object's process dead");
5310 handleUnstableProviderDiedLocked(jBinder, true);
5311 return null;
5312 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005313
5314 // Only increment the ref count if we have one. If we don't then the
5315 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005316 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005317 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005318 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005319 }
5320 return provider;
5321 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005322 }
5323
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005324 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5325 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005326 return false;
5327 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005330 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005331 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005332 if (prc == null) {
5333 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005335 }
5336
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005337 boolean lastRef = false;
5338 if (stable) {
5339 if (prc.stableCount == 0) {
5340 if (DEBUG_PROVIDER) Slog.v(TAG,
5341 "releaseProvider: stable ref count already 0, how?");
5342 return false;
5343 }
5344 prc.stableCount -= 1;
5345 if (prc.stableCount == 0) {
5346 // What we do at this point depends on whether there are
5347 // any unstable refs left: if there are, we just tell the
5348 // activity manager to decrement its stable count; if there
5349 // aren't, we need to enqueue this provider to be removed,
5350 // and convert to holding a single unstable ref while
5351 // doing so.
5352 lastRef = prc.unstableCount == 0;
5353 try {
5354 if (DEBUG_PROVIDER) {
5355 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5356 + lastRef + " - " + prc.holder.info.name);
5357 }
5358 ActivityManagerNative.getDefault().refContentProvider(
5359 prc.holder.connection, -1, lastRef ? 1 : 0);
5360 } catch (RemoteException e) {
5361 //do nothing content provider object is dead any way
5362 }
5363 }
5364 } else {
5365 if (prc.unstableCount == 0) {
5366 if (DEBUG_PROVIDER) Slog.v(TAG,
5367 "releaseProvider: unstable ref count already 0, how?");
5368 return false;
5369 }
5370 prc.unstableCount -= 1;
5371 if (prc.unstableCount == 0) {
5372 // If this is the last reference, we need to enqueue
5373 // this provider to be removed instead of telling the
5374 // activity manager to remove it at this point.
5375 lastRef = prc.stableCount == 0;
5376 if (!lastRef) {
5377 try {
5378 if (DEBUG_PROVIDER) {
5379 Slog.v(TAG, "releaseProvider: No longer unstable - "
5380 + prc.holder.info.name);
5381 }
5382 ActivityManagerNative.getDefault().refContentProvider(
5383 prc.holder.connection, 0, -1);
5384 } catch (RemoteException e) {
5385 //do nothing content provider object is dead any way
5386 }
5387 }
5388 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005389 }
5390
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005391 if (lastRef) {
5392 if (!prc.removePending) {
5393 // Schedule the actual remove asynchronously, since we don't know the context
5394 // this will be called in.
5395 // TODO: it would be nice to post a delayed message, so
5396 // if we come back and need the same provider quickly
5397 // we will still have it available.
5398 if (DEBUG_PROVIDER) {
5399 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5400 + prc.holder.info.name);
5401 }
5402 prc.removePending = true;
5403 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5404 mH.sendMessage(msg);
5405 } else {
5406 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5407 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005408 }
5409 return true;
5410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005411 }
5412
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005413 final void completeRemoveProvider(ProviderRefCount prc) {
5414 synchronized (mProviderMap) {
5415 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005416 // There was a race! Some other client managed to acquire
5417 // the provider before the removal was completed.
5418 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005419 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005420 + "provider still in use");
5421 return;
5422 }
5423
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005424 // More complicated race!! Some client managed to acquire the
5425 // provider and release it before the removal was completed.
5426 // Continue the removal, and abort the next remove message.
5427 prc.removePending = false;
5428
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005429 final IBinder jBinder = prc.holder.provider.asBinder();
5430 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5431 if (existingPrc == prc) {
5432 mProviderRefCountMap.remove(jBinder);
5433 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005434
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005435 for (int i=mProviderMap.size()-1; i>=0; i--) {
5436 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005437 IBinder myBinder = pr.mProvider.asBinder();
5438 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005439 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005440 }
5441 }
5442 }
5443
5444 try {
5445 if (DEBUG_PROVIDER) {
5446 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5447 + "removeContentProvider(" + prc.holder.info.name + ")");
5448 }
5449 ActivityManagerNative.getDefault().removeContentProvider(
5450 prc.holder.connection, false);
5451 } catch (RemoteException e) {
5452 //do nothing content provider object is dead any way
5453 }
5454 }
5455
5456 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005457 synchronized (mProviderMap) {
5458 handleUnstableProviderDiedLocked(provider, fromClient);
5459 }
5460 }
5461
5462 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5463 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5464 if (prc != null) {
5465 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5466 + provider + " " + prc.holder.info.name);
5467 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005468 for (int i=mProviderMap.size()-1; i>=0; i--) {
5469 ProviderClientRecord pr = mProviderMap.valueAt(i);
5470 if (pr != null && pr.mProvider.asBinder() == provider) {
5471 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5472 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005473 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005474 }
You Kimbc74de62013-10-01 00:13:26 +09005475
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005476 if (fromClient) {
5477 // We found out about this due to execution in our client
5478 // code. Tell the activity manager about it now, to ensure
5479 // that the next time we go to do anything with the provider
5480 // it knows it is dead (so we don't race with its death
5481 // notification).
5482 try {
5483 ActivityManagerNative.getDefault().unstableProviderDied(
5484 prc.holder.connection);
5485 } catch (RemoteException e) {
5486 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005487 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005488 }
5489 }
5490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005492 final void appNotRespondingViaProvider(IBinder provider) {
5493 synchronized (mProviderMap) {
5494 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5495 if (prc != null) {
5496 try {
5497 ActivityManagerNative.getDefault()
5498 .appNotRespondingViaProvider(prc.holder.connection);
5499 } catch (RemoteException e) {
5500 }
5501 }
5502 }
5503 }
5504
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005505 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005506 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005507 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005508 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5509
5510 final ProviderClientRecord pcr = new ProviderClientRecord(
5511 auths, provider, localProvider, holder);
5512 for (String auth : auths) {
5513 final ProviderKey key = new ProviderKey(auth, userId);
5514 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005515 if (existing != null) {
5516 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005517 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005518 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005519 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 }
5521 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005522 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 }
5524
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005525 /**
5526 * Installs the provider.
5527 *
5528 * Providers that are local to the process or that come from the system server
5529 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5530 * Other remote providers are reference counted. The initial reference count
5531 * for all reference counted providers is one. Providers that are not reference
5532 * counted do not have a reference count (at all).
5533 *
5534 * This method detects when a provider has already been installed. When this happens,
5535 * it increments the reference count of the existing provider (if appropriate)
5536 * and returns the existing provider. This can happen due to concurrent
5537 * attempts to acquire the same provider.
5538 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005539 private IActivityManager.ContentProviderHolder installProvider(Context context,
5540 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5541 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005543 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005544 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005545 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005546 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 + info.name);
5548 }
5549 Context c = null;
5550 ApplicationInfo ai = info.applicationInfo;
5551 if (context.getPackageName().equals(ai.packageName)) {
5552 c = context;
5553 } else if (mInitialApplication != null &&
5554 mInitialApplication.getPackageName().equals(ai.packageName)) {
5555 c = mInitialApplication;
5556 } else {
5557 try {
5558 c = context.createPackageContext(ai.packageName,
5559 Context.CONTEXT_INCLUDE_CODE);
5560 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005561 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 }
5563 }
5564 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005565 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 ai.packageName +
5567 " while loading content provider " +
5568 info.name);
5569 return null;
5570 }
5571 try {
5572 final java.lang.ClassLoader cl = c.getClassLoader();
5573 localProvider = (ContentProvider)cl.
5574 loadClass(info.name).newInstance();
5575 provider = localProvider.getIContentProvider();
5576 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005577 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005578 info.name + " from sourceDir " +
5579 info.applicationInfo.sourceDir);
5580 return null;
5581 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005582 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 TAG, "Instantiating local provider " + info.name);
5584 // XXX Need to create the correct context for this provider.
5585 localProvider.attachInfo(c, info);
5586 } catch (java.lang.Exception e) {
5587 if (!mInstrumentation.onException(null, e)) {
5588 throw new RuntimeException(
5589 "Unable to get provider " + info.name
5590 + ": " + e.toString(), e);
5591 }
5592 return null;
5593 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005594 } else {
5595 provider = holder.provider;
5596 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 + info.name);
5598 }
5599
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005600 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005601
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005602 synchronized (mProviderMap) {
5603 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5604 + " / " + info.name);
5605 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005607 ComponentName cname = new ComponentName(info.packageName, info.name);
5608 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005609 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005610 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005611 Slog.v(TAG, "installProvider: lost the race, "
5612 + "using existing local provider");
5613 }
5614 provider = pr.mProvider;
5615 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005616 holder = new IActivityManager.ContentProviderHolder(info);
5617 holder.provider = provider;
5618 holder.noReleaseNeeded = true;
5619 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005620 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005621 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005622 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005623 retHolder = pr.mHolder;
5624 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005625 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5626 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005627 if (DEBUG_PROVIDER) {
5628 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005629 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005630 // We need to transfer our new reference to the existing
5631 // ref count, releasing the old one... but only if
5632 // release is needed (that is, it is not running in the
5633 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005634 if (!noReleaseNeeded) {
5635 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005636 try {
5637 ActivityManagerNative.getDefault().removeContentProvider(
5638 holder.connection, stable);
5639 } catch (RemoteException e) {
5640 //do nothing content provider object is dead any way
5641 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005642 }
5643 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005644 ProviderClientRecord client = installProviderAuthoritiesLocked(
5645 provider, localProvider, holder);
5646 if (noReleaseNeeded) {
5647 prc = new ProviderRefCount(holder, client, 1000, 1000);
5648 } else {
5649 prc = stable
5650 ? new ProviderRefCount(holder, client, 1, 0)
5651 : new ProviderRefCount(holder, client, 0, 1);
5652 }
5653 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005654 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005655 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 }
5657 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005658
5659 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 }
5661
Romain Guy65b345f2011-07-27 18:51:50 -07005662 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005663 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005665 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005666 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005667 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005668 public void run() {
5669 ensureJitEnabled();
5670 }
5671 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005672 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5673 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005674 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005675 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 try {
5677 mgr.attachApplication(mAppThread);
5678 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005679 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005681 // Watch for getting close to heap limit.
5682 BinderInternal.addGcWatcher(new Runnable() {
5683 @Override public void run() {
5684 if (!mSomeActivitiesChanged) {
5685 return;
5686 }
5687 Runtime runtime = Runtime.getRuntime();
5688 long dalvikMax = runtime.maxMemory();
5689 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5690 if (dalvikUsed > ((3*dalvikMax)/4)) {
5691 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5692 + " total=" + (runtime.totalMemory()/1024)
5693 + " used=" + (dalvikUsed/1024));
5694 mSomeActivitiesChanged = false;
5695 try {
5696 mgr.releaseSomeActivities(mAppThread);
5697 } catch (RemoteException e) {
5698 }
5699 }
5700 }
5701 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 } else {
5703 // Don't set application object here -- if the system crashes,
5704 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005705 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005706 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005707 try {
5708 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005709 ContextImpl context = ContextImpl.createAppContext(
5710 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005711 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5712 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005713 } catch (Exception e) {
5714 throw new RuntimeException(
5715 "Unable to instantiate Application():" + e.toString(), e);
5716 }
5717 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005718
5719 // add dropbox logging to libcore
5720 DropBox.setReporter(new DropBoxReporter());
5721
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005722 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005723 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005724 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005725 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005726 // We need to apply this change to the resources
5727 // immediately, because upon returning the view
5728 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005729 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005730 // This actually changed the resources! Tell
5731 // everyone about it.
5732 if (mPendingConfiguration == null ||
5733 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5734 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005735
Jeff Brown9ef09972013-10-15 20:49:59 -07005736 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005737 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005738 }
5739 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005740 }
Craig Mautner88c05892013-06-28 09:47:45 -07005741 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005742 public void onLowMemory() {
5743 }
Craig Mautner88c05892013-06-28 09:47:45 -07005744 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005745 public void onTrimMemory(int level) {
5746 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005747 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 }
5749
Romain Guy5e9120d2012-01-30 12:17:22 -08005750 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005751 // The system process on low-memory devices do not get to use hardware
5752 // accelerated drawing, since this can add too much overhead to the
5753 // process.
5754 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005755 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005756 } else {
John Reck51aaf902015-12-02 15:08:07 -08005757 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005759 ActivityThread thread = new ActivityThread();
5760 thread.attach(true);
5761 return thread;
5762 }
5763
Jeff Brown10e89712011-07-08 18:52:57 -07005764 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005765 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005766 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 }
5768 }
5769
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005770 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005771 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005772 if (mCoreSettings != null) {
5773 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005774 }
Craig Mautner88c05892013-06-28 09:47:45 -07005775 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005776 }
5777 }
5778
Geremy Condra69689a72012-09-11 16:57:17 -07005779 private static class EventLoggingReporter implements EventLogger.Reporter {
5780 @Override
5781 public void report (int code, Object... list) {
5782 EventLog.writeEvent(code, list);
5783 }
5784 }
5785
Geremy Condrab7faaf42012-09-19 18:07:42 -07005786 private class DropBoxReporter implements DropBox.Reporter {
5787
5788 private DropBoxManager dropBox;
5789
Narayan Kamath7f062242015-04-08 13:24:13 +01005790 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005791
5792 @Override
5793 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005794 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005795 dropBox.addData(tag, data, flags);
5796 }
5797
5798 @Override
5799 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005800 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005801 dropBox.addText(tag, data);
5802 }
Narayan Kamath7f062242015-04-08 13:24:13 +01005803
5804 private synchronized void ensureInitialized() {
5805 if (dropBox == null) {
5806 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5807 }
5808 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005809 }
5810
Romain Guy65b345f2011-07-27 18:51:50 -07005811 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005812 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07005813 SamplingProfilerIntegration.start();
5814
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005815 // CloseGuard defaults to true and can be quite spammy. We
5816 // disable it here, but selectively enable it later (via
5817 // StrictMode) on debug builds, but using DropBox, not logs.
5818 CloseGuard.setEnabled(false);
5819
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005820 Environment.initForCurrentUser();
5821
Geremy Condra69689a72012-09-11 16:57:17 -07005822 // Set the reporter for event logging in libcore
5823 EventLogger.setReporter(new EventLoggingReporter());
5824
Alex Klyubin48125632015-04-29 13:16:30 -07005825 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08005826
Robin Lee3d076af2014-04-25 14:57:49 +01005827 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5828 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5829 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005831 Process.setArgV0("<pre-initialized>");
5832
5833 Looper.prepareMainLooper();
5834
5835 ActivityThread thread = new ActivityThread();
5836 thread.attach(false);
5837
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005838 if (sMainThreadHandler == null) {
5839 sMainThreadHandler = thread.getHandler();
5840 }
5841
Dianne Hackborn287952c2010-09-22 22:34:31 -07005842 if (false) {
5843 Looper.myLooper().setMessageLogging(new
5844 LogPrinter(Log.DEBUG, "ActivityThread"));
5845 }
5846
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005847 // End of event ActivityThreadMain.
5848 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 Looper.loop();
5850
Jeff Brown10e89712011-07-08 18:52:57 -07005851 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 }
5853}