blob: 5ce13634a57836dfa426700943132e74032a3946 [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;
Romain Guya9582652011-11-10 14:20:10 -080056import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070057import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070058import android.os.Binder;
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;
131import java.util.Locale;
132import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800133import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135
Geremy Condrab7faaf42012-09-19 18:07:42 -0700136import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700137import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800138import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400139import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800140import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700141import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700142import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700143import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700144
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700145final class RemoteServiceException extends AndroidRuntimeException {
146 public RemoteServiceException(String msg) {
147 super(msg);
148 }
149}
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151/**
152 * This manages the execution of the main thread in an
153 * application process, scheduling and executing activities,
154 * broadcasts, and other operations on it as the activity
155 * manager requests.
156 *
157 * {@hide}
158 */
159public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700160 /** @hide */
161 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700162 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700163 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700164 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700165 /** @hide */
166 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700167 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700168 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700169 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800170 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700171 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700172 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700173 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800176 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
177 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178
Dianne Hackborn455625e2015-01-21 09:55:13 -0800179 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
180 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
181 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
182 public static final int SERVICE_DONE_EXECUTING_START = 1;
183 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
184 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
185
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700186 // Details for pausing activity.
187 private static final int USER_LEAVING = 1;
188 private static final int DONT_REPORT = 2;
189
Filip Gruszczynskica664812015-12-04 12:43:36 -0800190 // Whether to invoke an activity callback after delivering new configuration.
191 private static final boolean REPORT_TO_ACTIVITY = true;
192
Jeff Browndefd4a62014-03-10 21:24:37 -0700193 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700194
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700197 final ApplicationThread mAppThread = new ApplicationThread();
198 final Looper mLooper = Looper.myLooper();
199 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700200 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700201 // List of new activities (via ActivityRecord.nextIdle) that should
202 // be reported when next we idle.
203 ActivityClientRecord mNewActivities = null;
204 // Number of activities that are currently visible on-screen.
205 int mNumVisibleActivities = 0;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700206 WeakReference<AssistStructure> mLastAssistStructure;
207 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700208 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700209 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700210 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700211 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700212 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700213 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700214 Application mInitialApplication;
215 final ArrayList<Application> mAllApplications
216 = new ArrayList<Application>();
217 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700218 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800219 /** Reference to singleton {@link ActivityThread} */
220 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700221 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700222 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700224 String[] mInstrumentationSplitAppDirs = null;
225 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700226 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700227 String[] mInstrumentedSplitAppDirs = null;
228 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700229 boolean mSystemThread = false;
230 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700231 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700233 // These can be accessed by multiple threads; mPackages is the lock.
234 // XXX For now we keep around information about all packages we have
235 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800236 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700237 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800238 // which means this lock gets held while the activity and window managers
239 // holds their own lock. Thus you MUST NEVER call back into the activity manager
240 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700241 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700242 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
243 = new ArrayMap<String, WeakReference<LoadedApk>>();
244 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
245 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700246 final ArrayList<ActivityClientRecord> mRelaunchingActivities
247 = new ArrayList<ActivityClientRecord>();
248 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700249 // Because we merge activity relaunch operations we can't depend on the ordering provided by
250 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
251 // us to drop certain events, if we know that they happened before relaunch we already executed.
252 // This represents the order of receiving the request from AM.
253 @GuardedBy("mResourcesManager")
254 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Craig Mautner88c05892013-06-28 09:47:45 -0700256 private final ResourcesManager mResourcesManager;
257
Jeff Sharkey6d515712012-09-20 16:06:08 -0700258 private static final class ProviderKey {
259 final String authority;
260 final int userId;
261
262 public ProviderKey(String authority, int userId) {
263 this.authority = authority;
264 this.userId = userId;
265 }
266
267 @Override
268 public boolean equals(Object o) {
269 if (o instanceof ProviderKey) {
270 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800271 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700272 }
273 return false;
274 }
275
276 @Override
277 public int hashCode() {
278 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
279 }
280 }
281
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700282 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700283 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
284 = new ArrayMap<ProviderKey, ProviderClientRecord>();
285 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
286 = new ArrayMap<IBinder, ProviderRefCount>();
287 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
288 = new ArrayMap<IBinder, ProviderClientRecord>();
289 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
290 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700292 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700293 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600294
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700295 final GcIdler mGcIdler = new GcIdler();
296 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700298 static Handler sMainThreadHandler; // set once in main()
299
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800300 Bundle mCoreSettings = null;
301
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400302 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700304 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800306 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700307 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700309 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 Activity activity;
311 Window window;
312 Activity parent;
313 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700314 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 boolean paused;
316 boolean stopped;
317 boolean hideForNow;
318 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700319 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800320 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800321 // Used for consolidating configs before sending on to Activity.
322 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700323 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324
Jeff Hao1b012d32014-08-20 10:35:34 -0700325 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400328 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700329 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800332 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
334 boolean startsNotResumed;
335 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800336 int pendingConfigChanges;
337 boolean onlyLocalRequest;
338
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700339 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800340 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700341 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700343 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
344 // can be compared with other lifecycle operations.
345 int relaunchSeq = 0;
346
347 // Can only be accessed from the UI thread. This represents the latest processed message
348 // that is related to lifecycle events/
349 int lastProcessedSeq = 0;
350
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700351 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 parent = null;
353 embeddedID = null;
354 paused = false;
355 stopped = false;
356 hideForNow = false;
357 nextIdle = null;
358 }
359
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800360 public boolean isPreHoneycomb() {
361 if (activity != null) {
362 return activity.getApplicationInfo().targetSdkVersion
363 < android.os.Build.VERSION_CODES.HONEYCOMB;
364 }
365 return false;
366 }
367
Craig Mautnera0026042014-04-23 11:45:37 -0700368 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700369 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700370 }
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700373 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 return "ActivityRecord{"
375 + Integer.toHexString(System.identityHashCode(this))
376 + " token=" + token + " " + (componentName == null
377 ? "no component name" : componentName.toShortString())
378 + "}";
379 }
380 }
381
Wale Ogunwale1d646122015-04-24 14:45:14 -0700382 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700383 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 final IContentProvider mProvider;
385 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700386 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700388 ProviderClientRecord(String[] names, IContentProvider provider,
389 ContentProvider localProvider,
390 IActivityManager.ContentProviderHolder holder) {
391 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 mProvider = provider;
393 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700394 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396 }
397
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400398 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800399 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 IBinder token;
401 public String toString() {
402 return "NewIntentData{intents=" + intents + " token=" + token + "}";
403 }
404 }
405
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400406 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700407 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700408 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
409 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800410 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700411 this.intent = intent;
412 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 Intent intent;
415 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400416 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 public String toString() {
418 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700419 info.packageName + " resultCode=" + getResultCode()
420 + " resultData=" + getResultData() + " resultExtras="
421 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 }
423 }
424
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400425 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700426 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400427 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700428 int backupMode;
429 public String toString() {
430 return "CreateBackupAgentData{appInfo=" + appInfo
431 + " backupAgent=" + appInfo.backupAgentName
432 + " mode=" + backupMode + "}";
433 }
434 }
Bob Leee5408332009-09-04 18:31:17 -0700435
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400436 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 IBinder token;
438 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400439 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 Intent intent;
441 public String toString() {
442 return "CreateServiceData{token=" + token + " className="
443 + info.name + " packageName=" + info.packageName
444 + " intent=" + intent + "}";
445 }
446 }
447
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400448 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 IBinder token;
450 Intent intent;
451 boolean rebind;
452 public String toString() {
453 return "BindServiceData{token=" + token + " intent=" + intent + "}";
454 }
455 }
456
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400457 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700459 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700461 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 Intent args;
463 public String toString() {
464 return "ServiceArgsData{token=" + token + " startId=" + startId
465 + " args=" + args + "}";
466 }
467 }
468
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400469 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700470 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 String processName;
472 ApplicationInfo appInfo;
473 List<ProviderInfo> providers;
474 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 Bundle instrumentationArgs;
476 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800477 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400479 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700480 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700481 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700482 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400484 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700485
486 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700487 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 public String toString() {
490 return "AppBindData{appInfo=" + appInfo + "}";
491 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700492 }
493
494 static final class Profiler {
495 String profileFile;
496 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700497 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700498 boolean autoStopProfiler;
499 boolean profiling;
500 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700501 public void setProfiler(ProfilerInfo profilerInfo) {
502 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700503 if (profiling) {
504 if (fd != null) {
505 try {
506 fd.close();
507 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700508 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700509 }
510 }
511 return;
512 }
513 if (profileFd != null) {
514 try {
515 profileFd.close();
516 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700517 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700518 }
519 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700520 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700521 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700522 samplingInterval = profilerInfo.samplingInterval;
523 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700524 }
525 public void startProfiling() {
526 if (profileFd == null || profiling) {
527 return;
528 }
529 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700530 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
531 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700532 profiling = true;
533 } catch (RuntimeException e) {
534 Slog.w(TAG, "Profiling failed on path " + profileFile);
535 try {
536 profileFd.close();
537 profileFd = null;
538 } catch (IOException e2) {
539 Slog.w(TAG, "Failure closing profile fd", e2);
540 }
541 }
542 }
543 public void stopProfiling() {
544 if (profiling) {
545 profiling = false;
546 Debug.stopMethodTracing();
547 if (profileFd != null) {
548 try {
549 profileFd.close();
550 } catch (IOException e) {
551 }
552 }
553 profileFd = null;
554 profileFile = null;
555 }
556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400559 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700560 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700561 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800562 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 }
565
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400566 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 IBinder token;
568 List<ResultInfo> results;
569 public String toString() {
570 return "ResultData{token=" + token + " results" + results + "}";
571 }
572 }
573
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400574 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800575 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 String what;
577 String who;
578 }
579
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400580 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700581 String path;
582 ParcelFileDescriptor fd;
583 }
584
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400585 static final class UpdateCompatibilityData {
586 String pkg;
587 CompatibilityInfo info;
588 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800589
Adam Skorydfc7fd72013-08-05 19:23:41 -0700590 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800591 IBinder activityToken;
592 IBinder requestToken;
593 int requestType;
594 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700595
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800596 static final class ActivityConfigChangeData {
597 final IBinder activityToken;
598 final Configuration overrideConfig;
599 public ActivityConfigChangeData(IBinder token, Configuration config) {
600 activityToken = token;
601 overrideConfig = config;
602 }
603 }
604
Romain Guy65b345f2011-07-27 18:51:50 -0700605 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700606
Romain Guy65b345f2011-07-27 18:51:50 -0700607 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700608 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700609
Dianne Hackborna413dc02013-07-12 12:02:55 -0700610 private int mLastProcessState = -1;
611
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700612 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700613 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700614 if (mPendingConfiguration == null ||
615 mPendingConfiguration.isOtherSeqNewer(config)) {
616 mPendingConfiguration = config;
617 }
618 }
619 }
620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700622 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700623 int seq = getLifecycleSeq();
624 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
625 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700626 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
628 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700629 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
630 configChanges,
631 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 }
633
634 public final void scheduleStopActivity(IBinder token, boolean showWindow,
635 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700636 int seq = getLifecycleSeq();
637 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
638 + " operation received seq: " + seq);
639 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700641 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
644 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700645 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
647 token);
648 }
649
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800650 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700651 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 }
653
Dianne Hackborna413dc02013-07-12 12:02:55 -0700654 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800655 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700656 int seq = getLifecycleSeq();
657 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
658 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700659 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700660 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662
663 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
664 ResultData res = new ResultData();
665 res.token = token;
666 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700667 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 }
669
670 // we use token to identify this activity without having to send the
671 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800672 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700673 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800674 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
675 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
676 int procState, Bundle state, PersistableBundle persistentState,
677 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
678 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700679
680 updateProcessState(procState, false);
681
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700682 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683
684 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700685 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800687 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700688 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400690 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700692 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693
694 r.pendingResults = pendingResults;
695 r.pendingIntents = pendingNewIntents;
696
697 r.startsNotResumed = notResumed;
698 r.isForward = isForward;
699
Jeff Hao1b012d32014-08-20 10:35:34 -0700700 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700701
Wale Ogunwale60454db2015-01-23 16:05:07 -0800702 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700703 updatePendingConfiguration(curConfig);
704
Jeff Brown9ef09972013-10-15 20:49:59 -0700705 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707
Wale Ogunwale60454db2015-01-23 16:05:07 -0800708 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800710 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800711 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700712 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800713 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700714 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800717 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 NewIntentData data = new NewIntentData();
719 data.intents = intents;
720 data.token = token;
721
Jeff Brown9ef09972013-10-15 20:49:59 -0700722 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 }
724
725 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
726 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700727 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 configChanges);
729 }
730
731 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400732 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700733 boolean sync, int sendingUser, int processState) {
734 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700735 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700736 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400738 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700739 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 }
741
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400742 public final void scheduleCreateBackupAgent(ApplicationInfo app,
743 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700744 CreateBackupAgentData d = new CreateBackupAgentData();
745 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400746 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700747 d.backupMode = backupMode;
748
Jeff Brown9ef09972013-10-15 20:49:59 -0700749 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700750 }
751
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400752 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
753 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700754 CreateBackupAgentData d = new CreateBackupAgentData();
755 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400756 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700757
Jeff Brown9ef09972013-10-15 20:49:59 -0700758 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700759 }
760
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700762 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
763 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 CreateServiceData s = new CreateServiceData();
765 s.token = token;
766 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400767 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768
Jeff Brown9ef09972013-10-15 20:49:59 -0700769 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771
772 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700773 boolean rebind, int processState) {
774 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 BindServiceData s = new BindServiceData();
776 s.token = token;
777 s.intent = intent;
778 s.rebind = rebind;
779
Amith Yamasani742a6712011-05-04 14:49:28 -0700780 if (DEBUG_SERVICE)
781 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
782 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700783 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 }
785
786 public final void scheduleUnbindService(IBinder token, Intent intent) {
787 BindServiceData s = new BindServiceData();
788 s.token = token;
789 s.intent = intent;
790
Jeff Brown9ef09972013-10-15 20:49:59 -0700791 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700794 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700795 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 ServiceArgsData s = new ServiceArgsData();
797 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700798 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700800 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 s.args = args;
802
Jeff Brown9ef09972013-10-15 20:49:59 -0700803 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805
806 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700807 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
Jeff Hao1b012d32014-08-20 10:35:34 -0700810 public final void bindApplication(String processName, ApplicationInfo appInfo,
811 List<ProviderInfo> providers, ComponentName instrumentationName,
812 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
813 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800814 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700815 boolean enableBinderTracking, boolean trackAllocation,
816 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
817 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818
819 if (services != null) {
820 // Setup the service cache in the ServiceManager
821 ServiceManager.initServiceCache(services);
822 }
823
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800824 setCoreSettings(coreSettings);
825
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 AppBindData data = new AppBindData();
827 data.processName = processName;
828 data.appInfo = appInfo;
829 data.providers = providers;
830 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 data.instrumentationArgs = instrumentationArgs;
832 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800833 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400835 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700836 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700837 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700838 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400840 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700841 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700842 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 }
844
845 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700846 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
848
Christopher Tate5e1ab332009-09-01 20:32:49 -0700849 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700850 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700851 }
852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700854 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700855 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857
858 public void updateTimeZone() {
859 TimeZone.setDefault(null);
860 }
861
Robert Greenwalt03595d02010-11-02 14:08:23 -0700862 public void clearDnsCache() {
863 // a non-standard API to get this to libcore
864 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400865 // Allow libcore to perform the necessary actions as it sees fit upon a network
866 // configuration change.
867 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700868 }
869
Jason Monk83520b92014-05-09 15:16:06 -0400870 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400871 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
872 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500873 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400874 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500875 } else {
876 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
877 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700878 }
879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 public void processInBackground() {
881 mH.removeMessages(H.GC_WHEN_IDLE);
882 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
883 }
884
885 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700886 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700887 try {
888 data.fd = ParcelFileDescriptor.dup(fd);
889 data.token = servicetoken;
890 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700891 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700892 } catch (IOException e) {
893 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895 }
896
897 // This function exists to make sure all receiver dispatching is
898 // correctly ordered, since these are one-way calls and the binder driver
899 // applies transaction ordering per object for such calls.
900 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700901 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700902 boolean sticky, int sendingUser, int processState) throws RemoteException {
903 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700904 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
905 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
Bob Leee5408332009-09-04 18:31:17 -0700907
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800908 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700910 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 }
912
Wale Ogunwale60454db2015-01-23 16:05:07 -0800913 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800914 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800915 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800916 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800917 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 }
919
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800920 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700921 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
922 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800923 }
924
Andy McFadden824c5102010-07-09 16:26:57 -0700925 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
926 DumpHeapData dhd = new DumpHeapData();
927 dhd.path = path;
928 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700929 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700930 }
931
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700932 public void setSchedulingGroup(int group) {
933 // Note: do this immediately, since going into the foreground
934 // should happen regardless of what pending work we have to do
935 // and the activity manager will wait for us to report back that
936 // we are done before sending us to the background.
937 try {
938 Process.setProcessGroup(Process.myPid(), group);
939 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800940 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700941 }
942 }
Bob Leee5408332009-09-04 18:31:17 -0700943
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700944 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700945 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700946 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700947
948 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700949 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700950 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700951
Dianne Hackborn30d71892010-12-11 10:37:55 -0800952 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
953 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700954 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700955 try {
956 data.fd = ParcelFileDescriptor.dup(fd);
957 data.token = activitytoken;
958 data.prefix = prefix;
959 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700960 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700961 } catch (IOException e) {
962 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700963 }
964 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700965
Marco Nelissen18cb2872011-11-15 11:19:53 -0800966 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
967 String[] args) {
968 DumpComponentInfo data = new DumpComponentInfo();
969 try {
970 data.fd = ParcelFileDescriptor.dup(fd);
971 data.token = providertoken;
972 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700973 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800974 } catch (IOException e) {
975 Slog.w(TAG, "dumpProvider failed", e);
976 }
977 }
978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700980 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700981 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700982 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700983 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700984 try {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700985 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700987 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700988 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700989 }
990
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700991 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -0700992 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 long nativeMax = Debug.getNativeHeapSize() / 1024;
994 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
995 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -0700998 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 long dalvikMax = runtime.totalMemory() / 1024;
1000 long dalvikFree = runtime.freeMemory() / 1024;
1001 long dalvikAllocated = dalvikMax - dalvikFree;
1002 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001003 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001004 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1005 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 int globalAssetCount = AssetManager.getGlobalAssetCount();
1007 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1008 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1009 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1010 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001011 long parcelSize = Parcel.getGlobalAllocSize();
1012 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001013 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001014 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001015
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001016 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1017 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001018 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1019 nativeMax, nativeAllocated, nativeFree,
1020 dalvikMax, dalvikAllocated, dalvikFree);
1021
Dianne Hackbornb437e092011-08-05 17:50:29 -07001022 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 // NOTE: if you change anything significant below, also consider changing
1024 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 // Object counts
1027 pw.print(viewInstanceCount); pw.print(',');
1028 pw.print(viewRootInstanceCount); pw.print(',');
1029 pw.print(appContextInstanceCount); pw.print(',');
1030 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 pw.print(globalAssetCount); pw.print(',');
1033 pw.print(globalAssetManagerCount); pw.print(',');
1034 pw.print(binderLocalObjectCount); pw.print(',');
1035 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 pw.print(binderDeathObjectCount); pw.print(',');
1038 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001041 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001042 pw.print(stats.memoryUsed / 1024); pw.print(',');
1043 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001044 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001045 for (int i = 0; i < stats.dbStats.size(); i++) {
1046 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001047 pw.print(','); pw.print(dbStats.dbName);
1048 pw.print(','); pw.print(dbStats.pageSize);
1049 pw.print(','); pw.print(dbStats.dbSize);
1050 pw.print(','); pw.print(dbStats.lookaside);
1051 pw.print(','); pw.print(dbStats.cache);
1052 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001053 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001054 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001055
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001056 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 }
Bob Leee5408332009-09-04 18:31:17 -07001058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 pw.println(" ");
1060 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001061 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 viewRootInstanceCount);
1063
1064 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1065 "Activities:", activityInstanceCount);
1066
1067 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1068 "AssetManagers:", globalAssetManagerCount);
1069
1070 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1071 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001072 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1073 "Parcel count:", parcelCount);
1074 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1075 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 // SQLite mem info
1078 pw.println(" ");
1079 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001080 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1081 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1082 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001083 pw.println(" ");
1084 int N = stats.dbStats.size();
1085 if (N > 0) {
1086 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001087 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1088 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001089 for (int i = 0; i < N; i++) {
1090 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001091 printRow(pw, DB_INFO_FORMAT,
1092 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1093 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1094 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1095 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001096 }
1097 }
Bob Leee5408332009-09-04 18:31:17 -07001098
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001099 // Asset details.
1100 String assetAlloc = AssetManager.getAssetAllocations();
1101 if (assetAlloc != null) {
1102 pw.println(" ");
1103 pw.println(" Asset Allocations");
1104 pw.print(assetAlloc);
1105 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001106 }
1107
1108 @Override
1109 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1110 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001111 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001114 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001115 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001116 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1117 SQLiteDebug.dump(printer, args);
1118 pw.flush();
1119 }
1120
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001121 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001122 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1123 if (mSystemThread) {
1124 // Ensure this invocation is asynchronous to prevent
1125 // writer waiting due to buffer cannot be consumed.
1126 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1127 @Override
1128 public void run() {
1129 dumpDatabaseInfo(fd, args);
1130 }
1131 });
1132 } else {
1133 dumpDatabaseInfo(fd, args);
1134 }
1135 }
1136
1137 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001138 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001139 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001140 }
1141
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001142 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001143 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001144 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001145 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001146 cmd.activityToken = activityToken;
1147 cmd.requestToken = requestToken;
1148 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001149 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001150 }
1151
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001152 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001153 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001154 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001155
1156 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1157 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1158 ucd.pkg = pkg;
1159 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001160 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001161 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001162
1163 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001164 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001165 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001166
Craig Mautner5eda9b32013-07-02 11:58:16 -07001167 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001168 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001169 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001170
Craig Mautnereb8abf72014-07-02 15:04:09 -07001171 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1172 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1173 new Pair<IBinder, ActivityOptions>(token, options));
1174 }
1175
Dianne Hackborna413dc02013-07-12 12:02:55 -07001176 public void setProcessState(int state) {
1177 updateProcessState(state, true);
1178 }
1179
1180 public void updateProcessState(int processState, boolean fromIpc) {
1181 synchronized (this) {
1182 if (mLastProcessState != processState) {
1183 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001184 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1185 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1186 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1187 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1188 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1189 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1190 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1191 }
1192 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001193 if (false) {
1194 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1195 + (fromIpc ? " (from ipc": ""));
1196 }
1197 }
1198 }
1199 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001200
1201 @Override
1202 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001203 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001204 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001205
1206 @Override
1207 public final void updateTimePrefs(boolean is24Hour) {
1208 DateFormat.set24HourTimePref(is24Hour);
1209 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001210
1211 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001212 public void scheduleCancelVisibleBehind(IBinder token) {
1213 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001214 }
1215
1216 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001217 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1218 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001219 }
Craig Mautner8746a472014-07-24 15:12:54 -07001220
Jeff Sharkey605eb792014-11-04 13:34:06 -08001221 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001222 public void scheduleEnterAnimationComplete(IBinder token) {
1223 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1224 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001225
1226 @Override
1227 public void notifyCleartextNetwork(byte[] firstPacket) {
1228 if (StrictMode.vmCleartextNetworkEnabled()) {
1229 StrictMode.onCleartextNetworkDetected(firstPacket);
1230 }
1231 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001232
1233 @Override
1234 public void startBinderTracking() {
1235 sendMessage(H.START_BINDER_TRACKING, null);
1236 }
1237
1238 @Override
1239 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1240 try {
1241 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1242 } catch (IOException e) {
1243 }
1244 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001245
1246 @Override
1247 public void scheduleMultiWindowModeChanged(IBinder token, boolean multiWindowMode)
1248 throws RemoteException {
1249 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, token, multiWindowMode ? 1 : 0);
1250 }
1251
1252 @Override
1253 public void schedulePictureInPictureModeChanged(IBinder token, boolean pipMode)
1254 throws RemoteException {
1255 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, token, pipMode ? 1 : 0);
1256 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 }
1258
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001259 private int getLifecycleSeq() {
1260 synchronized (mResourcesManager) {
1261 return mLifecycleSeq++;
1262 }
1263 }
1264
Romain Guy65b345f2011-07-27 18:51:50 -07001265 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 public static final int LAUNCH_ACTIVITY = 100;
1267 public static final int PAUSE_ACTIVITY = 101;
1268 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1269 public static final int STOP_ACTIVITY_SHOW = 103;
1270 public static final int STOP_ACTIVITY_HIDE = 104;
1271 public static final int SHOW_WINDOW = 105;
1272 public static final int HIDE_WINDOW = 106;
1273 public static final int RESUME_ACTIVITY = 107;
1274 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001275 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 public static final int BIND_APPLICATION = 110;
1277 public static final int EXIT_APPLICATION = 111;
1278 public static final int NEW_INTENT = 112;
1279 public static final int RECEIVER = 113;
1280 public static final int CREATE_SERVICE = 114;
1281 public static final int SERVICE_ARGS = 115;
1282 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 public static final int CONFIGURATION_CHANGED = 118;
1285 public static final int CLEAN_UP_CONTEXT = 119;
1286 public static final int GC_WHEN_IDLE = 120;
1287 public static final int BIND_SERVICE = 121;
1288 public static final int UNBIND_SERVICE = 122;
1289 public static final int DUMP_SERVICE = 123;
1290 public static final int LOW_MEMORY = 124;
1291 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1292 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001293 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001294 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001295 public static final int DESTROY_BACKUP_AGENT = 129;
1296 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001297 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001298 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001299 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001300 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001301 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001302 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001303 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001304 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001305 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001306 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001307 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001308 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001309 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001310 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001311 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001312 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001313 public static final int CANCEL_VISIBLE_BEHIND = 147;
1314 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001315 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001316 public static final int START_BINDER_TRACKING = 150;
1317 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Wale Ogunwale5f986092015-12-04 15:35:38 -08001318 public static final int MULTI_WINDOW_MODE_CHANGED = 152;
1319 public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001322 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 switch (code) {
1324 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1325 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1326 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1327 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1328 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1329 case SHOW_WINDOW: return "SHOW_WINDOW";
1330 case HIDE_WINDOW: return "HIDE_WINDOW";
1331 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1332 case SEND_RESULT: return "SEND_RESULT";
1333 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1334 case BIND_APPLICATION: return "BIND_APPLICATION";
1335 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1336 case NEW_INTENT: return "NEW_INTENT";
1337 case RECEIVER: return "RECEIVER";
1338 case CREATE_SERVICE: return "CREATE_SERVICE";
1339 case SERVICE_ARGS: return "SERVICE_ARGS";
1340 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1342 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1343 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1344 case BIND_SERVICE: return "BIND_SERVICE";
1345 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1346 case DUMP_SERVICE: return "DUMP_SERVICE";
1347 case LOW_MEMORY: return "LOW_MEMORY";
1348 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1349 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001350 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001351 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1352 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001353 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001354 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001355 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001356 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001357 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001358 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001359 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001360 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001361 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001362 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001363 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001364 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001365 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001366 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001367 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001368 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001369 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001370 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1371 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001372 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Wale Ogunwale5f986092015-12-04 15:35:38 -08001373 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1374 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 }
1376 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001377 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 }
1379 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001380 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 switch (msg.what) {
1382 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001384 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385
1386 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001387 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001388 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 } break;
1391 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001392 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001393 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001394 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001397 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001398 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001399 SomeArgs args = (SomeArgs) msg.obj;
1400 handlePauseActivity((IBinder) args.arg1, false,
1401 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1402 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001403 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001404 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001405 } break;
1406 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001407 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001408 SomeArgs args = (SomeArgs) msg.obj;
1409 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1410 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001411 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001412 } break;
1413 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001414 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001415 SomeArgs args = (SomeArgs) msg.obj;
1416 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001417 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001418 } break;
1419 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001420 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001421 SomeArgs args = (SomeArgs) msg.obj;
1422 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001423 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001424 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001426 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001428 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 break;
1430 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001431 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001433 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 break;
1435 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001436 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001437 SomeArgs args = (SomeArgs) msg.obj;
1438 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
1439 args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001440 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 break;
1442 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001445 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 break;
1447 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001448 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1450 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001451 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 break;
1453 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001454 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 AppBindData data = (AppBindData)msg.obj;
1456 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001457 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 break;
1459 case EXIT_APPLICATION:
1460 if (mInitialApplication != null) {
1461 mInitialApplication.onTerminate();
1462 }
1463 Looper.myLooper().quit();
1464 break;
1465 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001466 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001468 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 break;
1470 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001471 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001473 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001474 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 break;
1476 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001477 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001479 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 break;
1481 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 handleBindService((BindServiceData)msg.obj);
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 UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001487 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 handleUnbindService((BindServiceData)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 SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001492 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 handleServiceArgs((ServiceArgsData)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 STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001497 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001499 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001500 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001503 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001504 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001505 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001506 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 break;
1508 case CLEAN_UP_CONTEXT:
1509 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1510 cci.context.performFinalCleanup(cci.who, cci.what);
1511 break;
1512 case GC_WHEN_IDLE:
1513 scheduleGcIdler();
1514 break;
1515 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001516 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 break;
1518 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001519 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001521 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 break;
1523 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001524 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001525 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1526 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001527 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001529 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001530 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001531 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001532 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001533 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001534 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001535 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001536 break;
1537 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001538 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001539 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001540 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001541 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001542 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001543 Process.killProcess(Process.myPid());
1544 break;
1545 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001546 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001547 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001548 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001549 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001550 case ENABLE_JIT:
1551 ensureJitEnabled();
1552 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001553 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001554 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001555 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001556 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001557 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001558 case SCHEDULE_CRASH:
1559 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001560 case DUMP_HEAP:
1561 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1562 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001563 case DUMP_ACTIVITY:
1564 handleDumpActivity((DumpComponentInfo)msg.obj);
1565 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001566 case DUMP_PROVIDER:
1567 handleDumpProvider((DumpComponentInfo)msg.obj);
1568 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001569 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001570 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001571 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001572 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001573 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001574 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001575 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001576 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001577 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001578 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001579 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1580 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001581 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001582 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001583 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001584 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001585 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001586 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001587 case UNSTABLE_PROVIDER_DIED:
1588 handleUnstableProviderDied((IBinder)msg.obj, false);
1589 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001590 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1591 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001592 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001593 case TRANSLUCENT_CONVERSION_COMPLETE:
1594 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1595 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001596 case INSTALL_PROVIDER:
1597 handleInstallProvider((ProviderInfo) msg.obj);
1598 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001599 case ON_NEW_ACTIVITY_OPTIONS:
1600 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1601 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001602 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001603 case CANCEL_VISIBLE_BEHIND:
1604 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001605 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001606 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1607 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001608 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001609 case ENTER_ANIMATION_COMPLETE:
1610 handleEnterAnimationComplete((IBinder) msg.obj);
1611 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001612 case START_BINDER_TRACKING:
1613 handleStartBinderTracking();
1614 break;
1615 case STOP_BINDER_TRACKING_AND_DUMP:
1616 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1617 break;
Wale Ogunwale5f986092015-12-04 15:35:38 -08001618 case MULTI_WINDOW_MODE_CHANGED:
1619 handleMultiWindowModeChanged((IBinder) msg.obj, msg.arg1 == 1);
1620 break;
1621 case PICTURE_IN_PICTURE_MODE_CHANGED:
1622 handlePictureInPictureModeChanged((IBinder) msg.obj, msg.arg1 == 1);
1623 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001625 Object obj = msg.obj;
1626 if (obj instanceof SomeArgs) {
1627 ((SomeArgs) obj).recycle();
1628 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001629 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Bob Leee5408332009-09-04 18:31:17 -07001631
Brian Carlstromed7e0072011-03-24 13:27:57 -07001632 private void maybeSnapshot() {
1633 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001634 // convert the *private* ActivityThread.PackageInfo to *public* known
1635 // android.content.pm.PackageInfo
1636 String packageName = mBoundApplication.info.mPackageName;
1637 android.content.pm.PackageInfo packageInfo = null;
1638 try {
1639 Context context = getSystemContext();
1640 if(context == null) {
1641 Log.e(TAG, "cannot get a valid context");
1642 return;
1643 }
1644 PackageManager pm = context.getPackageManager();
1645 if(pm == null) {
1646 Log.e(TAG, "cannot get a valid PackageManager");
1647 return;
1648 }
1649 packageInfo = pm.getPackageInfo(
1650 packageName, PackageManager.GET_ACTIVITIES);
1651 } catch (NameNotFoundException e) {
1652 Log.e(TAG, "cannot get package info for " + packageName, e);
1653 }
1654 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001655 }
1656 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
1658
Romain Guy65b345f2011-07-27 18:51:50 -07001659 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001660 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001662 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001663 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001664 if (mBoundApplication != null && mProfiler.profileFd != null
1665 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001666 stopProfiling = true;
1667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 if (a != null) {
1669 mNewActivities = null;
1670 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001671 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001673 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 TAG, "Reporting idle of " + a +
1675 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001676 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (a.activity != null && !a.activity.mFinished) {
1678 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001679 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001680 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001682 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
1684 }
1685 prev = a;
1686 a = a.nextIdle;
1687 prev.nextIdle = null;
1688 } while (a != null);
1689 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001690 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001691 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001692 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001693 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 return false;
1695 }
1696 }
1697
1698 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001699 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 public final boolean queueIdle() {
1701 doGcIfNeeded();
1702 return false;
1703 }
1704 }
1705
Romain Guy65b345f2011-07-27 18:51:50 -07001706 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001707 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001710 public static boolean isSystem() {
1711 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1712 }
1713
Svetoslavfbf0eca2015-05-01 16:52:41 -07001714 public static String currentOpPackageName() {
1715 ActivityThread am = currentActivityThread();
1716 return (am != null && am.getApplication() != null)
1717 ? am.getApplication().getOpPackageName() : null;
1718 }
1719
Romain Guy65b345f2011-07-27 18:51:50 -07001720 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001721 ActivityThread am = currentActivityThread();
1722 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001723 ? am.mBoundApplication.appInfo.packageName : null;
1724 }
1725
1726 public static String currentProcessName() {
1727 ActivityThread am = currentActivityThread();
1728 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001729 ? am.mBoundApplication.processName : null;
1730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731
Romain Guy65b345f2011-07-27 18:51:50 -07001732 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001733 ActivityThread am = currentActivityThread();
1734 return am != null ? am.mInitialApplication : null;
1735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001737 public static IPackageManager getPackageManager() {
1738 if (sPackageManager != null) {
1739 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1740 return sPackageManager;
1741 }
1742 IBinder b = ServiceManager.getService("package");
1743 //Slog.v("PackageManager", "default service binder = " + b);
1744 sPackageManager = IPackageManager.Stub.asInterface(b);
1745 //Slog.v("PackageManager", "default service = " + sPackageManager);
1746 return sPackageManager;
1747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748
Romain Guy65b345f2011-07-27 18:51:50 -07001749 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001750 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1751 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001752 if (config == null) {
1753 return null;
1754 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001755 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001756 mMainThreadConfig.setTo(config);
1757 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001758 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001759 }
1760 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001763 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001764 * Creates the top level resources for the given package.
1765 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001766 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1767 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001768 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001769 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
Alan Viverette02fc5fe2015-08-27 13:16:09 -04001770 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(),
1771 pkgInfo.getClassLoader());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001772 }
1773
1774 final Handler getHandler() {
1775 return mH;
1776 }
1777
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001778 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1779 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001780 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1781 }
1782
1783 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1784 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001785 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001786 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001787 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001788 if (differentUser) {
1789 // Caching not supported across users
1790 ref = null;
1791 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 ref = mPackages.get(packageName);
1793 } else {
1794 ref = mResourcePackages.get(packageName);
1795 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001796
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001797 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001798 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001799 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1800 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 if (packageInfo != null && (packageInfo.mResources == null
1802 || packageInfo.mResources.getAssets().isUpToDate())) {
1803 if (packageInfo.isSecurityViolation()
1804 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1805 throw new SecurityException(
1806 "Requesting code from " + packageName
1807 + " to be run in process "
1808 + mBoundApplication.processName
1809 + "/" + mBoundApplication.appInfo.uid);
1810 }
1811 return packageInfo;
1812 }
1813 }
1814
1815 ApplicationInfo ai = null;
1816 try {
1817 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001818 PackageManager.GET_SHARED_LIBRARY_FILES
1819 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1820 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001822 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 }
1824
1825 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001826 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
1828
1829 return null;
1830 }
1831
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001832 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1833 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1835 boolean securityViolation = includeCode && ai.uid != 0
1836 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001837 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001838 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001839 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1841 |Context.CONTEXT_IGNORE_SECURITY))
1842 == Context.CONTEXT_INCLUDE_CODE) {
1843 if (securityViolation) {
1844 String msg = "Requesting code from " + ai.packageName
1845 + " (with uid " + ai.uid + ")";
1846 if (mBoundApplication != null) {
1847 msg = msg + " to be run in process "
1848 + mBoundApplication.processName + " (with uid "
1849 + mBoundApplication.appInfo.uid + ")";
1850 }
1851 throw new SecurityException(msg);
1852 }
1853 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001854 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1855 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 }
1857
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001858 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1859 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001860 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
1862
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001863 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001864 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001865 WeakReference<LoadedApk> ref;
1866 if (includeCode) {
1867 ref = mPackages.get(packageName);
1868 } else {
1869 ref = mResourcePackages.get(packageName);
1870 }
1871 return ref != null ? ref.get() : null;
1872 }
1873 }
1874
Romain Guy65b345f2011-07-27 18:51:50 -07001875 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001876 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1877 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001878 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001879 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001880 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001881 if (differentUser) {
1882 // Caching not supported across users
1883 ref = null;
1884 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 ref = mPackages.get(aInfo.packageName);
1886 } else {
1887 ref = mResourcePackages.get(aInfo.packageName);
1888 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001889
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001890 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 if (packageInfo == null || (packageInfo.mResources != null
1892 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001893 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 : "Loading resource-only package ") + aInfo.packageName
1895 + " (in " + (mBoundApplication != null
1896 ? mBoundApplication.processName : null)
1897 + ")");
1898 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001899 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001901 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001902
1903 if (mSystemThread && "android".equals(aInfo.packageName)) {
1904 packageInfo.installSystemApplicationInfo(aInfo,
1905 getSystemContext().mPackageInfo.getClassLoader());
1906 }
1907
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001908 if (differentUser) {
1909 // Caching not supported across users
1910 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001912 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 } else {
1914 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001915 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917 }
1918 return packageInfo;
1919 }
1920 }
1921
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001923 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 }
1925
1926 public ApplicationThread getApplicationThread()
1927 {
1928 return mAppThread;
1929 }
1930
1931 public Instrumentation getInstrumentation()
1932 {
1933 return mInstrumentation;
1934 }
1935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001937 return mProfiler != null && mProfiler.profileFile != null
1938 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
1940
1941 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001942 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944
1945 public Looper getLooper() {
1946 return mLooper;
1947 }
1948
1949 public Application getApplication() {
1950 return mInitialApplication;
1951 }
Bob Leee5408332009-09-04 18:31:17 -07001952
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001953 public String getProcessName() {
1954 return mBoundApplication.processName;
1955 }
Bob Leee5408332009-09-04 18:31:17 -07001956
Dianne Hackborn21556372010-02-04 16:34:40 -08001957 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001958 synchronized (this) {
1959 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001960 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001962 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965
Narayan Kamath29564cd2014-08-07 10:57:40 +01001966 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001967 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001968 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001969
1970 // give ourselves a default profiler
1971 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001972 }
1973 }
1974
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001975 void ensureJitEnabled() {
1976 if (!mJitEnabled) {
1977 mJitEnabled = true;
1978 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1979 }
1980 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 void scheduleGcIdler() {
1983 if (!mGcIdlerScheduled) {
1984 mGcIdlerScheduled = true;
1985 Looper.myQueue().addIdleHandler(mGcIdler);
1986 }
1987 mH.removeMessages(H.GC_WHEN_IDLE);
1988 }
1989
1990 void unscheduleGcIdler() {
1991 if (mGcIdlerScheduled) {
1992 mGcIdlerScheduled = false;
1993 Looper.myQueue().removeIdleHandler(mGcIdler);
1994 }
1995 mH.removeMessages(H.GC_WHEN_IDLE);
1996 }
1997
1998 void doGcIfNeeded() {
1999 mGcIdlerScheduled = false;
2000 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002001 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 // + "m now=" + now);
2003 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002004 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 BinderInternal.forceGc("bg");
2006 }
2007 }
2008
Dianne Hackborne77187d2013-10-25 16:32:41 -07002009 private static final String HEAP_FULL_COLUMN
2010 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2011 private static final String HEAP_COLUMN
2012 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002013 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2014 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2015 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002016
2017 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002018 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002019
2020 static void printRow(PrintWriter pw, String format, Object...objs) {
2021 pw.println(String.format(format, objs));
2022 }
2023
2024 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002025 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2026 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002027 long nativeMax, long nativeAllocated, long nativeFree,
2028 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2029
2030 // For checkin, we print one long comma-separated list of values
2031 if (checkin) {
2032 // NOTE: if you change anything significant below, also consider changing
2033 // ACTIVITY_THREAD_CHECKIN_VERSION.
2034
2035 // Header
2036 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2037 pw.print(pid); pw.print(',');
2038 pw.print(processName); pw.print(',');
2039
2040 // Heap info - max
2041 pw.print(nativeMax); pw.print(',');
2042 pw.print(dalvikMax); pw.print(',');
2043 pw.print("N/A,");
2044 pw.print(nativeMax + dalvikMax); pw.print(',');
2045
2046 // Heap info - allocated
2047 pw.print(nativeAllocated); pw.print(',');
2048 pw.print(dalvikAllocated); pw.print(',');
2049 pw.print("N/A,");
2050 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2051
2052 // Heap info - free
2053 pw.print(nativeFree); pw.print(',');
2054 pw.print(dalvikFree); pw.print(',');
2055 pw.print("N/A,");
2056 pw.print(nativeFree + dalvikFree); pw.print(',');
2057
2058 // Heap info - proportional set size
2059 pw.print(memInfo.nativePss); pw.print(',');
2060 pw.print(memInfo.dalvikPss); pw.print(',');
2061 pw.print(memInfo.otherPss); pw.print(',');
2062 pw.print(memInfo.getTotalPss()); pw.print(',');
2063
2064 // Heap info - swappable set size
2065 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2066 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2067 pw.print(memInfo.otherSwappablePss); pw.print(',');
2068 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2069
2070 // Heap info - shared dirty
2071 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2072 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2073 pw.print(memInfo.otherSharedDirty); pw.print(',');
2074 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2075
2076 // Heap info - shared clean
2077 pw.print(memInfo.nativeSharedClean); pw.print(',');
2078 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2079 pw.print(memInfo.otherSharedClean); pw.print(',');
2080 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2081
2082 // Heap info - private Dirty
2083 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2084 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2085 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2086 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2087
2088 // Heap info - private Clean
2089 pw.print(memInfo.nativePrivateClean); pw.print(',');
2090 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2091 pw.print(memInfo.otherPrivateClean); pw.print(',');
2092 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2093
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002094 // Heap info - swapped out
2095 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2096 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2097 pw.print(memInfo.otherSwappedOut); pw.print(',');
2098 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2099
2100 // Heap info - swapped out pss
2101 if (memInfo.hasSwappedOutPss) {
2102 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2103 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2104 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2105 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2106 } else {
2107 pw.print("N/A,");
2108 pw.print("N/A,");
2109 pw.print("N/A,");
2110 pw.print("N/A,");
2111 }
2112
Dianne Hackborne77187d2013-10-25 16:32:41 -07002113 // Heap info - other areas
2114 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2115 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2116 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2117 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2118 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2119 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2120 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2121 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002122 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2123 if (memInfo.hasSwappedOutPss) {
2124 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2125 } else {
2126 pw.print("N/A,");
2127 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002128 }
2129 return;
2130 }
2131
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002132 if (!dumpSummaryOnly) {
2133 if (dumpFullInfo) {
2134 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002135 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2136 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002137 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002138 "Clean", "Clean", "Dirty",
2139 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002140 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2141 "------", "------", "------", "------", "------", "------");
2142 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2143 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2144 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002145 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2146 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002147 nativeMax, nativeAllocated, nativeFree);
2148 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2149 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2150 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002151 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2152 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002153 dalvikMax, dalvikAllocated, dalvikFree);
2154 } else {
2155 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002156 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2157 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002158 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2159 "Clean", "Dirty", "Size", "Alloc", "Free");
2160 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2161 "------", "------", "------", "------", "------");
2162 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2163 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002164 memInfo.nativePrivateClean,
2165 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2166 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002167 nativeMax, nativeAllocated, nativeFree);
2168 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2169 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002170 memInfo.dalvikPrivateClean,
2171 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2172 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002173 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002174 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002175
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002176 int otherPss = memInfo.otherPss;
2177 int otherSwappablePss = memInfo.otherSwappablePss;
2178 int otherSharedDirty = memInfo.otherSharedDirty;
2179 int otherPrivateDirty = memInfo.otherPrivateDirty;
2180 int otherSharedClean = memInfo.otherSharedClean;
2181 int otherPrivateClean = memInfo.otherPrivateClean;
2182 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002183 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002184
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002185 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002186 final int myPss = memInfo.getOtherPss(i);
2187 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2188 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2189 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2190 final int mySharedClean = memInfo.getOtherSharedClean(i);
2191 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2192 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002193 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002194 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002195 || mySharedClean != 0 || myPrivateClean != 0
2196 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002197 if (dumpFullInfo) {
2198 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2199 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002200 mySharedClean, myPrivateClean,
2201 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2202 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002203 } else {
2204 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2205 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002206 myPrivateClean,
2207 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2208 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002209 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002210 otherPss -= myPss;
2211 otherSwappablePss -= mySwappablePss;
2212 otherSharedDirty -= mySharedDirty;
2213 otherPrivateDirty -= myPrivateDirty;
2214 otherSharedClean -= mySharedClean;
2215 otherPrivateClean -= myPrivateClean;
2216 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002217 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002218 }
2219 }
2220
2221 if (dumpFullInfo) {
2222 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2223 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002224 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2225 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002226 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2227 memInfo.getTotalSwappablePss(),
2228 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2229 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002230 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOut() :
2231 memInfo.getTotalSwappedOutPss(),
2232 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2233 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002234 } else {
2235 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002236 otherPrivateDirty, otherPrivateClean,
2237 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002238 "", "", "");
2239 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2240 memInfo.getTotalPrivateDirty(),
2241 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002242 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002243 memInfo.getTotalSwappedOut(),
2244 nativeMax+dalvikMax,
2245 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2246 }
2247
2248 if (dumpDalvik) {
2249 pw.println(" ");
2250 pw.println(" Dalvik Details");
2251
2252 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2253 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2254 final int myPss = memInfo.getOtherPss(i);
2255 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2256 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2257 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2258 final int mySharedClean = memInfo.getOtherSharedClean(i);
2259 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2260 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002261 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002262 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002263 || mySharedClean != 0 || myPrivateClean != 0
2264 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002265 if (dumpFullInfo) {
2266 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2267 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002268 mySharedClean, myPrivateClean,
2269 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2270 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002271 } else {
2272 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2273 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002274 myPrivateClean,
2275 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2276 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002277 }
2278 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002279 }
2280 }
2281 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002282
2283 pw.println(" ");
2284 pw.println(" App Summary");
2285 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2286 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2287 printRow(pw, ONE_COUNT_COLUMN,
2288 "Java Heap:", memInfo.getSummaryJavaHeap());
2289 printRow(pw, ONE_COUNT_COLUMN,
2290 "Native Heap:", memInfo.getSummaryNativeHeap());
2291 printRow(pw, ONE_COUNT_COLUMN,
2292 "Code:", memInfo.getSummaryCode());
2293 printRow(pw, ONE_COUNT_COLUMN,
2294 "Stack:", memInfo.getSummaryStack());
2295 printRow(pw, ONE_COUNT_COLUMN,
2296 "Graphics:", memInfo.getSummaryGraphics());
2297 printRow(pw, ONE_COUNT_COLUMN,
2298 "Private Other:", memInfo.getSummaryPrivateOther());
2299 printRow(pw, ONE_COUNT_COLUMN,
2300 "System:", memInfo.getSummarySystem());
2301 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002302 if (memInfo.hasSwappedOutPss) {
2303 printRow(pw, TWO_COUNT_COLUMNS,
2304 "TOTAL:", memInfo.getSummaryTotalPss(),
2305 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2306 } else {
2307 printRow(pw, TWO_COUNT_COLUMNS,
2308 "TOTAL:", memInfo.getSummaryTotalPss(),
2309 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2310 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002311 }
2312
Jeff Hamilton52d32032011-01-08 15:31:26 -06002313 public void registerOnActivityPausedListener(Activity activity,
2314 OnActivityPausedListener listener) {
2315 synchronized (mOnPauseListeners) {
2316 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2317 if (list == null) {
2318 list = new ArrayList<OnActivityPausedListener>();
2319 mOnPauseListeners.put(activity, list);
2320 }
2321 list.add(listener);
2322 }
2323 }
2324
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002325 public void unregisterOnActivityPausedListener(Activity activity,
2326 OnActivityPausedListener listener) {
2327 synchronized (mOnPauseListeners) {
2328 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2329 if (list != null) {
2330 list.remove(listener);
2331 }
2332 }
2333 }
2334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335 public final ActivityInfo resolveActivityInfo(Intent intent) {
2336 ActivityInfo aInfo = intent.resolveActivityInfo(
2337 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2338 if (aInfo == null) {
2339 // Throw an exception.
2340 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002341 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 }
2343 return aInfo;
2344 }
Bob Leee5408332009-09-04 18:31:17 -07002345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002348 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002349 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002351 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 r.intent = intent;
2353 r.state = state;
2354 r.parent = parent;
2355 r.embeddedID = id;
2356 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002357 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 if (localLOGV) {
2359 ComponentName compname = intent.getComponent();
2360 String name;
2361 if (compname != null) {
2362 name = compname.toShortString();
2363 } else {
2364 name = "(Intent " + intent + ").getComponent() returned null";
2365 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002366 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 + ", comp=" + name
2368 + ", token=" + token);
2369 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002370 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372
2373 public final Activity getActivity(IBinder token) {
2374 return mActivities.get(token).activity;
2375 }
2376
2377 public final void sendActivityResult(
2378 IBinder token, String id, int requestCode,
2379 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002380 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002381 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2383 list.add(new ResultInfo(id, requestCode, resultCode, data));
2384 mAppThread.scheduleSendResult(token, list);
2385 }
2386
Jeff Brown9ef09972013-10-15 20:49:59 -07002387 private void sendMessage(int what, Object obj) {
2388 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 }
2390
Jeff Brown9ef09972013-10-15 20:49:59 -07002391 private void sendMessage(int what, Object obj, int arg1) {
2392 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
2394
Jeff Brown9ef09972013-10-15 20:49:59 -07002395 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2396 sendMessage(what, obj, arg1, arg2, false);
2397 }
2398
2399 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2400 if (DEBUG_MESSAGES) Slog.v(
2401 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2402 + ": " + arg1 + " / " + obj);
2403 Message msg = Message.obtain();
2404 msg.what = what;
2405 msg.obj = obj;
2406 msg.arg1 = arg1;
2407 msg.arg2 = arg2;
2408 if (async) {
2409 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002411 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 }
2413
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002414 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2415 if (DEBUG_MESSAGES) Slog.v(
2416 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2417 "seq= " + seq);
2418 Message msg = Message.obtain();
2419 msg.what = what;
2420 SomeArgs args = SomeArgs.obtain();
2421 args.arg1 = obj;
2422 args.argi1 = arg1;
2423 args.argi2 = arg2;
2424 args.argi3 = seq;
2425 msg.obj = args;
2426 mH.sendMessage(msg);
2427 }
2428
Dianne Hackborn21556372010-02-04 16:34:40 -08002429 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 String what) {
2431 ContextCleanupInfo cci = new ContextCleanupInfo();
2432 cci.context = context;
2433 cci.who = who;
2434 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002435 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 }
2437
Craig Mautner233ceee2014-05-09 17:05:11 -07002438 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2440
2441 ActivityInfo aInfo = r.activityInfo;
2442 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002443 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 Context.CONTEXT_INCLUDE_CODE);
2445 }
Bob Leee5408332009-09-04 18:31:17 -07002446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 ComponentName component = r.intent.getComponent();
2448 if (component == null) {
2449 component = r.intent.resolveActivity(
2450 mInitialApplication.getPackageManager());
2451 r.intent.setComponent(component);
2452 }
2453
2454 if (r.activityInfo.targetActivity != null) {
2455 component = new ComponentName(r.activityInfo.packageName,
2456 r.activityInfo.targetActivity);
2457 }
2458
2459 Activity activity = null;
2460 try {
2461 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2462 activity = mInstrumentation.newActivity(
2463 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002464 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002466 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 if (r.state != null) {
2468 r.state.setClassLoader(cl);
2469 }
2470 } catch (Exception e) {
2471 if (!mInstrumentation.onException(activity, e)) {
2472 throw new RuntimeException(
2473 "Unable to instantiate activity " + component
2474 + ": " + e.toString(), e);
2475 }
2476 }
2477
2478 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002479 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002480
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002481 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2482 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002483 TAG, r + ": app=" + app
2484 + ", appName=" + app.getPackageName()
2485 + ", pkg=" + r.packageInfo.getPackageName()
2486 + ", comp=" + r.intent.getComponent().toShortString()
2487 + ", dir=" + r.packageInfo.getAppDir());
2488
2489 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002490 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002492 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002493 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002494 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002495 Window window = null;
2496 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2497 window = r.mPendingRemoveWindow;
2498 r.mPendingRemoveWindow = null;
2499 r.mPendingRemoveWindowManager = null;
2500 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002501 activity.attach(appContext, this, getInstrumentation(), r.token,
2502 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002503 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002504 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002505
Christopher Tateb70f3df2009-04-07 16:07:59 -07002506 if (customIntent != null) {
2507 activity.mIntent = customIntent;
2508 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002509 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 activity.mStartedActivity = false;
2511 int theme = r.activityInfo.getThemeResource();
2512 if (theme != 0) {
2513 activity.setTheme(theme);
2514 }
2515
2516 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002517 if (r.isPersistable()) {
2518 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2519 } else {
2520 mInstrumentation.callActivityOnCreate(activity, r.state);
2521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002522 if (!activity.mCalled) {
2523 throw new SuperNotCalledException(
2524 "Activity " + r.intent.getComponent().toShortString() +
2525 " did not call through to super.onCreate()");
2526 }
2527 r.activity = activity;
2528 r.stopped = true;
2529 if (!r.activity.mFinished) {
2530 activity.performStart();
2531 r.stopped = false;
2532 }
2533 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002534 if (r.isPersistable()) {
2535 if (r.state != null || r.persistentState != null) {
2536 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2537 r.persistentState);
2538 }
2539 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2541 }
2542 }
2543 if (!r.activity.mFinished) {
2544 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002545 if (r.isPersistable()) {
2546 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2547 r.persistentState);
2548 } else {
2549 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 if (!activity.mCalled) {
2552 throw new SuperNotCalledException(
2553 "Activity " + r.intent.getComponent().toShortString() +
2554 " did not call through to super.onPostCreate()");
2555 }
2556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002557 }
2558 r.paused = true;
2559
2560 mActivities.put(r.token, r);
2561
2562 } catch (SuperNotCalledException e) {
2563 throw e;
2564
2565 } catch (Exception e) {
2566 if (!mInstrumentation.onException(activity, e)) {
2567 throw new RuntimeException(
2568 "Unable to start activity " + component
2569 + ": " + e.toString(), e);
2570 }
2571 }
2572
2573 return activity;
2574 }
2575
Wale Ogunwale7c726682015-02-06 17:34:28 -08002576 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2577 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002578 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002579 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002580 } catch (RemoteException e) {
2581 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002582
Wale Ogunwale7c726682015-02-06 17:34:28 -08002583 ContextImpl appContext = ContextImpl.createActivityContext(
2584 this, r.packageInfo, displayId, r.overrideConfig);
2585 appContext.setOuterContext(activity);
2586 Context baseContext = appContext;
2587
2588 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002589 // For debugging purposes, if the activity's package name contains the value of
2590 // the "debug.use-second-display" system property as a substring, then show
2591 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002592 String pkgName = SystemProperties.get("debug.second-display.pkg");
2593 if (pkgName != null && !pkgName.isEmpty()
2594 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002595 for (int id : dm.getDisplayIds()) {
2596 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002597 Display display =
2598 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002599 baseContext = appContext.createDisplayContext(display);
2600 break;
2601 }
2602 }
2603 }
2604 return baseContext;
2605 }
2606
Romain Guy65b345f2011-07-27 18:51:50 -07002607 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 // If we are getting ready to gc after going to the background, well
2609 // we are back active so skip it.
2610 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002611 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612
Jeff Hao1b012d32014-08-20 10:35:34 -07002613 if (r.profilerInfo != null) {
2614 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002615 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002616 }
2617
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002618 // Make sure we are running with the most recent config.
2619 handleConfigurationChanged(null, null);
2620
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002621 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002622 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002623
Chet Haase0d1c27a2014-11-03 18:35:16 +00002624 // Initialize before creating the activity
2625 WindowManagerGlobal.initialize();
2626
Craig Mautner233ceee2014-05-09 17:05:11 -07002627 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628
2629 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002630 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002631 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002632 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002633 handleResumeActivity(r.token, false, r.isForward,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002634 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002635
2636 if (!r.activity.mFinished && r.startsNotResumed) {
2637 // The activity manager actually wants this one to start out
2638 // paused, because it needs to be visible but isn't in the
2639 // foreground. We accomplish this by going through the
2640 // normal startup (because activities expect to go through
2641 // onResume() the first time they run, before their window
2642 // is displayed), and then pausing it. However, in this case
2643 // we do -not- need to do the full pause cycle (of freezing
2644 // and such) because the activity manager assumes it can just
2645 // retain the current state it has.
2646 try {
2647 r.activity.mCalled = false;
2648 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002649 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002650 // we need to be created again. But we only do this
2651 // for pre-Honeycomb apps, which always save their state
2652 // when pausing, so we can not have them save their state
2653 // when restarting from a paused state. For HC and later,
2654 // we want to (and can) let the state be saved as the normal
2655 // part of stopping the activity.
2656 if (r.isPreHoneycomb()) {
2657 r.state = oldState;
2658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 if (!r.activity.mCalled) {
2660 throw new SuperNotCalledException(
2661 "Activity " + r.intent.getComponent().toShortString() +
2662 " did not call through to super.onPause()");
2663 }
2664
2665 } catch (SuperNotCalledException e) {
2666 throw e;
2667
2668 } catch (Exception e) {
2669 if (!mInstrumentation.onException(r.activity, e)) {
2670 throw new RuntimeException(
2671 "Unable to pause activity "
2672 + r.intent.getComponent().toShortString()
2673 + ": " + e.toString(), e);
2674 }
2675 }
2676 r.paused = true;
2677 }
2678 } else {
2679 // If there was an error, for any reason, tell the activity
2680 // manager to stop us.
2681 try {
2682 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002683 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2684 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002686 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
2688 }
2689 }
2690
Filip Gruszczynski23493322015-07-29 17:02:59 -07002691 private void reportSizeConfigurations(ActivityClientRecord r) {
2692 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2693 if (configurations == null) {
2694 return;
2695 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002696 SparseIntArray horizontal = new SparseIntArray();
2697 SparseIntArray vertical = new SparseIntArray();
2698 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002699 for (int i = configurations.length - 1; i >= 0; i--) {
2700 Configuration config = configurations[i];
2701 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002702 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002703 }
2704 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002705 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002706 }
2707 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002708 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002709 }
2710 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002711 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002712 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2713 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002714 } catch (RemoteException ex) {
2715 }
2716
2717 }
2718
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002719 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 final int N = intents.size();
2721 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002722 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002724 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002725 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002726 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2727 }
2728 }
2729
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002730 public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002731 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 if (r != null) {
2733 final boolean resumed = !r.paused;
2734 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002735 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 mInstrumentation.callActivityOnPause(r.activity);
2737 }
2738 deliverNewIntents(r, intents);
2739 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002740 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002741 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 }
2743 }
2744 }
Bob Leee5408332009-09-04 18:31:17 -07002745
Romain Guy65b345f2011-07-27 18:51:50 -07002746 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 performNewIntents(data.token, data.intents);
2748 }
2749
Adam Skorydfc7fd72013-08-05 19:23:41 -07002750 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackborn782d4982015-07-08 17:36:37 -07002751 if (mLastAssistStructure != null) {
2752 AssistStructure structure = mLastAssistStructure.get();
2753 if (structure != null) {
2754 structure.clearSendChannel();
2755 }
2756 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002757 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002758 AssistStructure structure = null;
2759 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002760 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002761 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002762 if (r != null) {
2763 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2764 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002765 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002766 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002767 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002768 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002769 if (activityIntent != null && (r.window == null ||
2770 (r.window.getAttributes().flags
2771 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002772 Intent intent = new Intent(activityIntent);
2773 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2774 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2775 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002776 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002777 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002778 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002779 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002780 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002781 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002782 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002783 if (structure == null) {
2784 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002785 }
Dianne Hackborn782d4982015-07-08 17:36:37 -07002786 mLastAssistStructure = new WeakReference<>(structure);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002787 IActivityManager mgr = ActivityManagerNative.getDefault();
2788 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002789 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002790 } catch (RemoteException e) {
2791 }
2792 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002793
2794 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2795 ActivityClientRecord r = mActivities.get(token);
2796 if (r != null) {
2797 r.activity.onTranslucentConversionComplete(drawComplete);
2798 }
2799 }
2800
Craig Mautnereb8abf72014-07-02 15:04:09 -07002801 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2802 ActivityClientRecord r = mActivities.get(token);
2803 if (r != null) {
2804 r.activity.onNewActivityOptions(options);
2805 }
2806 }
2807
Jose Lima4b6c6692014-08-12 17:41:12 -07002808 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002809 ActivityClientRecord r = mActivities.get(token);
2810 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002811 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002812 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002813 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002814 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002815 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002816 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2817 if (!activity.mCalled) {
2818 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002819 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002820 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002821 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002822 }
2823 }
2824 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002825 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002826 } catch (RemoteException e) {
2827 }
2828 }
2829
Jose Lima4b6c6692014-08-12 17:41:12 -07002830 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002831 ActivityClientRecord r = mActivities.get(token);
2832 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002833 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002834 }
2835 }
2836
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002837 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002838 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2839 try {
2840 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2841 } finally {
2842 StrictMode.setThreadPolicy(oldPolicy);
2843 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002844 }
2845
Craig Mautner8746a472014-07-24 15:12:54 -07002846 private void handleEnterAnimationComplete(IBinder token) {
2847 ActivityClientRecord r = mActivities.get(token);
2848 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002849 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002850 }
2851 }
2852
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002853 private void handleStartBinderTracking() {
2854 Binder.enableTracing();
2855 }
2856
2857 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2858 try {
2859 Binder.disableTracing();
2860 Binder.getTransactionTracker().writeTracesToFile(fd);
2861 } finally {
2862 IoUtils.closeQuietly(fd);
2863 Binder.getTransactionTracker().clearTraces();
2864 }
2865 }
2866
Wale Ogunwale5f986092015-12-04 15:35:38 -08002867 private void handleMultiWindowModeChanged(IBinder token, boolean multiWindowMode) {
2868 final ActivityClientRecord r = mActivities.get(token);
2869 if (r != null) {
2870 r.activity.onMultiWindowModeChanged(multiWindowMode);
2871 }
2872 }
2873
2874 private void handlePictureInPictureModeChanged(IBinder token, boolean pipMode) {
2875 final ActivityClientRecord r = mActivities.get(token);
2876 if (r != null) {
2877 r.activity.onPictureInPictureModeChanged(pipMode);
2878 }
2879 }
2880
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002881 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2882
2883 /**
2884 * Return the Intent that's currently being handled by a
2885 * BroadcastReceiver on this thread, or null if none.
2886 * @hide
2887 */
2888 public static Intent getIntentBeingBroadcast() {
2889 return sCurrentBroadcastIntent.get();
2890 }
2891
Romain Guy65b345f2011-07-27 18:51:50 -07002892 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 // If we are getting ready to gc after going to the background, well
2894 // we are back active so skip it.
2895 unscheduleGcIdler();
2896
2897 String component = data.intent.getComponent().getClassName();
2898
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002899 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002900 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901
2902 IActivityManager mgr = ActivityManagerNative.getDefault();
2903
Romain Guy65b345f2011-07-27 18:51:50 -07002904 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 try {
2906 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2907 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002908 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002909 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2911 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002912 if (DEBUG_BROADCAST) Slog.i(TAG,
2913 "Finishing failed broadcast to " + data.intent.getComponent());
2914 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 throw new RuntimeException(
2916 "Unable to instantiate receiver " + component
2917 + ": " + e.toString(), e);
2918 }
2919
2920 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002921 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002922
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002923 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 TAG, "Performing receive of " + data.intent
2925 + ": app=" + app
2926 + ", appName=" + app.getPackageName()
2927 + ", pkg=" + packageInfo.getPackageName()
2928 + ", comp=" + data.intent.getComponent().toShortString()
2929 + ", dir=" + packageInfo.getAppDir());
2930
Dianne Hackborn21556372010-02-04 16:34:40 -08002931 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002932 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002933 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002934 receiver.onReceive(context.getReceiverRestrictedContext(),
2935 data.intent);
2936 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002937 if (DEBUG_BROADCAST) Slog.i(TAG,
2938 "Finishing failed broadcast to " + data.intent.getComponent());
2939 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 if (!mInstrumentation.onException(receiver, e)) {
2941 throw new RuntimeException(
2942 "Unable to start receiver " + component
2943 + ": " + e.toString(), e);
2944 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002945 } finally {
2946 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 }
2948
Dianne Hackborne829fef2010-10-26 17:44:01 -07002949 if (receiver.getPendingResult() != null) {
2950 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 }
2952 }
2953
Christopher Tate181fafa2009-05-14 11:12:14 -07002954 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002955 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002956 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002957
Christopher Tate346acb12012-10-15 19:20:25 -07002958 // Sanity check the requested target package's uid against ours
2959 try {
2960 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2961 data.appInfo.packageName, 0, UserHandle.myUserId());
2962 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2963 Slog.w(TAG, "Asked to instantiate non-matching package "
2964 + data.appInfo.packageName);
2965 return;
2966 }
2967 } catch (RemoteException e) {
2968 Slog.e(TAG, "Can't reach package manager", e);
2969 return;
2970 }
2971
Christopher Tate181fafa2009-05-14 11:12:14 -07002972 // no longer idle; we have backup work to do
2973 unscheduleGcIdler();
2974
2975 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002976 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002977 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002978 if (packageName == null) {
2979 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2980 return;
2981 }
2982
Christopher Tate181fafa2009-05-14 11:12:14 -07002983 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07002984 // full backup operation but no app-supplied agent? use the default implementation
2985 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2986 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002987 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002988 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002989
Christopher Tate181fafa2009-05-14 11:12:14 -07002990 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002991 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07002992 BackupAgent agent = mBackupAgents.get(packageName);
2993 if (agent != null) {
2994 // reusing the existing instance
2995 if (DEBUG_BACKUP) {
2996 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07002997 }
Christopher Tate2e40d112014-07-15 12:37:38 -07002998 binder = agent.onBind();
2999 } else {
3000 try {
3001 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3002
3003 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3004 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3005
3006 // set up the agent's context
3007 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3008 context.setOuterContext(agent);
3009 agent.attach(context);
3010
3011 agent.onCreate();
3012 binder = agent.onBind();
3013 mBackupAgents.put(packageName, agent);
3014 } catch (Exception e) {
3015 // If this is during restore, fail silently; otherwise go
3016 // ahead and let the user see the crash.
3017 Slog.e(TAG, "Agent threw during creation: " + e);
3018 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3019 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3020 throw e;
3021 }
3022 // falling through with 'binder' still null
3023 }
Christopher Tated1475e02009-07-09 15:36:17 -07003024 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003025
3026 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003027 try {
3028 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3029 } catch (RemoteException e) {
3030 // nothing to do.
3031 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003032 } catch (Exception e) {
3033 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003034 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003035 }
3036 }
3037
3038 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003039 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003040 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003041
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003042 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003043 String packageName = packageInfo.mPackageName;
3044 BackupAgent agent = mBackupAgents.get(packageName);
3045 if (agent != null) {
3046 try {
3047 agent.onDestroy();
3048 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003049 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003050 e.printStackTrace();
3051 }
3052 mBackupAgents.remove(packageName);
3053 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003054 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003055 }
3056 }
3057
Romain Guy65b345f2011-07-27 18:51:50 -07003058 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 // If we are getting ready to gc after going to the background, well
3060 // we are back active so skip it.
3061 unscheduleGcIdler();
3062
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003063 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003064 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 Service service = null;
3066 try {
3067 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3068 service = (Service) cl.loadClass(data.info.name).newInstance();
3069 } catch (Exception e) {
3070 if (!mInstrumentation.onException(service, e)) {
3071 throw new RuntimeException(
3072 "Unable to instantiate service " + data.info.name
3073 + ": " + e.toString(), e);
3074 }
3075 }
3076
3077 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003078 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079
Jeff Browndefd4a62014-03-10 21:24:37 -07003080 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3081 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003083 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 service.attach(context, this, data.info.name, data.token, app,
3085 ActivityManagerNative.getDefault());
3086 service.onCreate();
3087 mServices.put(data.token, service);
3088 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003089 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003090 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003091 } catch (RemoteException e) {
3092 // nothing to do.
3093 }
3094 } catch (Exception e) {
3095 if (!mInstrumentation.onException(service, e)) {
3096 throw new RuntimeException(
3097 "Unable to create service " + data.info.name
3098 + ": " + e.toString(), e);
3099 }
3100 }
3101 }
3102
Romain Guy65b345f2011-07-27 18:51:50 -07003103 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003105 if (DEBUG_SERVICE)
3106 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 if (s != null) {
3108 try {
3109 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003110 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 try {
3112 if (!data.rebind) {
3113 IBinder binder = s.onBind(data.intent);
3114 ActivityManagerNative.getDefault().publishService(
3115 data.token, data.intent, binder);
3116 } else {
3117 s.onRebind(data.intent);
3118 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003119 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003121 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 } catch (RemoteException ex) {
3123 }
3124 } catch (Exception e) {
3125 if (!mInstrumentation.onException(s, e)) {
3126 throw new RuntimeException(
3127 "Unable to bind to service " + s
3128 + " with " + data.intent + ": " + e.toString(), e);
3129 }
3130 }
3131 }
3132 }
3133
Romain Guy65b345f2011-07-27 18:51:50 -07003134 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 Service s = mServices.get(data.token);
3136 if (s != null) {
3137 try {
3138 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003139 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 boolean doRebind = s.onUnbind(data.intent);
3141 try {
3142 if (doRebind) {
3143 ActivityManagerNative.getDefault().unbindFinished(
3144 data.token, data.intent, doRebind);
3145 } else {
3146 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003147 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 }
3149 } catch (RemoteException ex) {
3150 }
3151 } catch (Exception e) {
3152 if (!mInstrumentation.onException(s, e)) {
3153 throw new RuntimeException(
3154 "Unable to unbind to service " + s
3155 + " with " + data.intent + ": " + e.toString(), e);
3156 }
3157 }
3158 }
3159 }
3160
Dianne Hackborn625ac272010-09-17 18:29:22 -07003161 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003162 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3163 try {
3164 Service s = mServices.get(info.token);
3165 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003166 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3167 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003168 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3169 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003171 } finally {
3172 IoUtils.closeQuietly(info.fd);
3173 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 }
3175 }
3176
Dianne Hackborn625ac272010-09-17 18:29:22 -07003177 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003178 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3179 try {
3180 ActivityClientRecord r = mActivities.get(info.token);
3181 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003182 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3183 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003184 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3185 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003186 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003187 } finally {
3188 IoUtils.closeQuietly(info.fd);
3189 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003190 }
3191 }
3192
Marco Nelissen18cb2872011-11-15 11:19:53 -08003193 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003194 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3195 try {
3196 ProviderClientRecord r = mLocalProviders.get(info.token);
3197 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003198 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3199 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003200 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3201 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003202 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003203 } finally {
3204 IoUtils.closeQuietly(info.fd);
3205 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003206 }
3207 }
3208
Romain Guy65b345f2011-07-27 18:51:50 -07003209 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 Service s = mServices.get(data.token);
3211 if (s != null) {
3212 try {
3213 if (data.args != null) {
3214 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003215 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003217 int res;
3218 if (!data.taskRemoved) {
3219 res = s.onStartCommand(data.args, data.flags, data.startId);
3220 } else {
3221 s.onTaskRemoved(data.args);
3222 res = Service.START_TASK_REMOVED_COMPLETE;
3223 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003224
3225 QueuedWork.waitToFinish();
3226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003228 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003229 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 } catch (RemoteException e) {
3231 // nothing to do.
3232 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003233 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 } catch (Exception e) {
3235 if (!mInstrumentation.onException(s, e)) {
3236 throw new RuntimeException(
3237 "Unable to start service " + s
3238 + " with " + data.args + ": " + e.toString(), e);
3239 }
3240 }
3241 }
3242 }
3243
Romain Guy65b345f2011-07-27 18:51:50 -07003244 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 Service s = mServices.remove(token);
3246 if (s != null) {
3247 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003248 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 s.onDestroy();
3250 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003251 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003253 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003255
3256 QueuedWork.waitToFinish();
3257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003259 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003260 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003261 } catch (RemoteException e) {
3262 // nothing to do.
Craig Mautner66c4a822015-01-16 12:48:16 -08003263 Slog.i(TAG, "handleStopService: unable to execute serviceDoneExecuting for "
3264 + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266 } catch (Exception e) {
3267 if (!mInstrumentation.onException(s, e)) {
3268 throw new RuntimeException(
3269 "Unable to stop service " + s
3270 + ": " + e.toString(), e);
3271 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003272 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003274 } else {
3275 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003277 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 }
3279
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003280 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003282 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003283 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 + " finished=" + r.activity.mFinished);
3285 if (r != null && !r.activity.mFinished) {
3286 if (clearHide) {
3287 r.hideForNow = false;
3288 r.activity.mStartedActivity = false;
3289 }
3290 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003291 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003292 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003293 if (r.pendingIntents != null) {
3294 deliverNewIntents(r, r.pendingIntents);
3295 r.pendingIntents = null;
3296 }
3297 if (r.pendingResults != null) {
3298 deliverResults(r, r.pendingResults);
3299 r.pendingResults = null;
3300 }
3301 r.activity.performResume();
3302
Craig Mautnere3119b72015-01-20 15:02:36 -08003303 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003304 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07003305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 r.paused = false;
3307 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003309 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 } catch (Exception e) {
3311 if (!mInstrumentation.onException(r.activity, e)) {
3312 throw new RuntimeException(
3313 "Unable to resume activity "
3314 + r.intent.getComponent().toShortString()
3315 + ": " + e.toString(), e);
3316 }
3317 }
3318 }
3319 return r;
3320 }
3321
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003322 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3323 if (r.mPreserveWindow && !force) {
3324 return;
3325 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003326 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003327 r.mPendingRemoveWindowManager.removeViewImmediate(
3328 r.mPendingRemoveWindow.getDecorView());
3329 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003330 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003331 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003332 r.activity.getClass().getName(), "Activity");
3333 }
3334 }
3335 r.mPendingRemoveWindow = null;
3336 r.mPendingRemoveWindowManager = null;
3337 }
3338
Craig Mautner233ceee2014-05-09 17:05:11 -07003339 final void handleResumeActivity(IBinder token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003340 boolean clearHide, boolean isForward, boolean reallyResume, int seq) {
3341 ActivityClientRecord r = mActivities.get(token);
3342 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3343 return;
3344 }
3345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 // If we are getting ready to gc after going to the background, well
3347 // we are back active so skip it.
3348 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003349 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350
Adam Powellcfbe9be2013-11-06 14:58:58 -08003351 // TODO Push resumeArgs into the activity for consideration
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003352 r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353
3354 if (r != null) {
3355 final Activity a = r.activity;
3356
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003357 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 TAG, "Resume " + r + " started activity: " +
3359 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3360 + ", finished: " + a.mFinished);
3361
3362 final int forwardBit = isForward ?
3363 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 // If the window hasn't yet been added to the window manager,
3366 // and this guy didn't finish itself or start another activity,
3367 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003368 boolean willBeVisible = !a.mStartedActivity;
3369 if (!willBeVisible) {
3370 try {
3371 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3372 a.getActivityToken());
3373 } catch (RemoteException e) {
3374 }
3375 }
3376 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 r.window = r.activity.getWindow();
3378 View decor = r.window.getDecorView();
3379 decor.setVisibility(View.INVISIBLE);
3380 ViewManager wm = a.getWindowManager();
3381 WindowManager.LayoutParams l = r.window.getAttributes();
3382 a.mDecor = decor;
3383 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3384 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003385 if (r.mPreserveWindow) {
3386 a.mWindowAdded = true;
3387 r.mPreserveWindow = false;
3388 }
3389 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 a.mWindowAdded = true;
3391 wm.addView(decor, l);
3392 }
3393
3394 // If the window has already been added, but during resume
3395 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003396 // window visible.
3397 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003398 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 TAG, "Launch " + r + " mStartedActivity set");
3400 r.hideForNow = true;
3401 }
3402
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003403 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003404 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003406 // The window is now visible if it has been added, we are not
3407 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003408 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003409 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003411 r.tmpConfig.setTo(r.newConfig);
3412 if (r.overrideConfig != null) {
3413 r.tmpConfig.updateFrom(r.overrideConfig);
3414 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003415 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003416 + r.activityInfo.name + " with newConfig " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003417 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003418 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003419 r.newConfig = null;
3420 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003421 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 + isForward);
3423 WindowManager.LayoutParams l = r.window.getAttributes();
3424 if ((l.softInputMode
3425 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3426 != forwardBit) {
3427 l.softInputMode = (l.softInputMode
3428 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3429 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003430 if (r.activity.mVisibleFromClient) {
3431 ViewManager wm = a.getWindowManager();
3432 View decor = r.window.getDecorView();
3433 wm.updateViewLayout(decor, l);
3434 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 }
3436 r.activity.mVisibleFromServer = true;
3437 mNumVisibleActivities++;
3438 if (r.activity.mVisibleFromClient) {
3439 r.activity.makeVisible();
3440 }
3441 }
3442
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003443 if (!r.onlyLocalRequest) {
3444 r.nextIdle = mNewActivities;
3445 mNewActivities = r;
3446 if (localLOGV) Slog.v(
3447 TAG, "Scheduling idle handler for " + r);
3448 Looper.myQueue().addIdleHandler(new Idler());
3449 }
3450 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003452 // Tell the activity manager we have resumed.
3453 if (reallyResume) {
3454 try {
3455 ActivityManagerNative.getDefault().activityResumed(token);
3456 } catch (RemoteException ex) {
3457 }
3458 }
3459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003460 } else {
3461 // If an exception was thrown when trying to resume, then
3462 // just end this activity.
3463 try {
3464 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003465 .finishActivity(token, Activity.RESULT_CANCELED, null,
3466 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 } catch (RemoteException ex) {
3468 }
3469 }
3470 }
3471
3472 private int mThumbnailWidth = -1;
3473 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003474 private Bitmap mAvailThumbnailBitmap = null;
3475 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476
Romain Guy65b345f2011-07-27 18:51:50 -07003477 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003478 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003480 if (thumbnail == null) {
3481 int w = mThumbnailWidth;
3482 int h;
3483 if (w < 0) {
3484 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003485 int wId = com.android.internal.R.dimen.thumbnail_width;
3486 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003487 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3488 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003489 } else {
3490 h = mThumbnailHeight;
3491 }
3492
3493 // On platforms where we don't want thumbnails, set dims to (0,0)
3494 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003495 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3496 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003497 thumbnail.eraseColor(0);
3498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 }
3500
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003501 if (thumbnail != null) {
3502 Canvas cv = mThumbnailCanvas;
3503 if (cv == null) {
3504 mThumbnailCanvas = cv = new Canvas();
3505 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003506
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003507 cv.setBitmap(thumbnail);
3508 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3509 mAvailThumbnailBitmap = thumbnail;
3510 thumbnail = null;
3511 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003512 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003513 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 } catch (Exception e) {
3516 if (!mInstrumentation.onException(r.activity, e)) {
3517 throw new RuntimeException(
3518 "Unable to create thumbnail of "
3519 + r.intent.getComponent().toShortString()
3520 + ": " + e.toString(), e);
3521 }
3522 thumbnail = null;
3523 }
3524
3525 return thumbnail;
3526 }
3527
Romain Guy65b345f2011-07-27 18:51:50 -07003528 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003529 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003530 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003531 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3532 return;
3533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003535 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 if (userLeaving) {
3537 performUserLeavingActivity(r);
3538 }
Bob Leee5408332009-09-04 18:31:17 -07003539
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003541 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003543 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003544 if (r.isPreHoneycomb()) {
3545 QueuedWork.waitToFinish();
3546 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003549 if (!dontReport) {
3550 try {
3551 ActivityManagerNative.getDefault().activityPaused(token);
3552 } catch (RemoteException ex) {
3553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003555 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 }
3557 }
3558
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003559 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003560 mInstrumentation.callActivityOnUserLeaving(r.activity);
3561 }
3562
3563 final Bundle performPauseActivity(IBinder token, boolean finished,
3564 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003565 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 return r != null ? performPauseActivity(r, finished, saveState) : null;
3567 }
3568
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003569 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 boolean saveState) {
3571 if (r.paused) {
3572 if (r.activity.mFinished) {
3573 // If we are finishing, we won't call onResume() in certain cases.
3574 // So here we likewise don't want to call onPause() if the activity
3575 // isn't resumed.
3576 return null;
3577 }
3578 RuntimeException e = new RuntimeException(
3579 "Performing pause of activity that is not resumed: "
3580 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003581 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 if (finished) {
3584 r.activity.mFinished = true;
3585 }
3586 try {
3587 // Next have the activity save its current state and managed dialogs...
3588 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003589 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 }
3591 // Now we are idle.
3592 r.activity.mCalled = false;
3593 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08003594 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003595 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 if (!r.activity.mCalled) {
3597 throw new SuperNotCalledException(
3598 "Activity " + r.intent.getComponent().toShortString() +
3599 " did not call through to super.onPause()");
3600 }
3601
3602 } catch (SuperNotCalledException e) {
3603 throw e;
3604
3605 } catch (Exception e) {
3606 if (!mInstrumentation.onException(r.activity, e)) {
3607 throw new RuntimeException(
3608 "Unable to pause activity "
3609 + r.intent.getComponent().toShortString()
3610 + ": " + e.toString(), e);
3611 }
3612 }
3613 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003614
3615 // Notify any outstanding on paused listeners
3616 ArrayList<OnActivityPausedListener> listeners;
3617 synchronized (mOnPauseListeners) {
3618 listeners = mOnPauseListeners.remove(r.activity);
3619 }
3620 int size = (listeners != null ? listeners.size() : 0);
3621 for (int i = 0; i < size; i++) {
3622 listeners.get(i).onPaused(r.activity);
3623 }
3624
Craig Mautnera0026042014-04-23 11:45:37 -07003625 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003626 }
3627
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003628 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003629 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003630 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 }
3632
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003633 private static class StopInfo implements Runnable {
3634 ActivityClientRecord activity;
3635 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003636 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003638
3639 @Override public void run() {
3640 // Tell activity manager we have been stopped.
3641 try {
3642 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3643 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003644 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003645 } catch (RemoteException ex) {
3646 }
3647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 }
3649
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003650 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003651 public final IActivityManager.ContentProviderHolder holder;
3652 public final ProviderClientRecord client;
3653 public int stableCount;
3654 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003655
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003656 // When this is set, the stable and unstable ref counts are 0 and
3657 // we have a pending operation scheduled to remove the ref count
3658 // from the activity manager. On the activity manager we are still
3659 // holding an unstable ref, though it is not reflected in the counts
3660 // here.
3661 public boolean removePending;
3662
3663 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3664 ProviderClientRecord inClient, int sCount, int uCount) {
3665 holder = inHolder;
3666 client = inClient;
3667 stableCount = sCount;
3668 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003669 }
3670 }
3671
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003672 /**
3673 * Core implementation of stopping an activity. Note this is a little
3674 * tricky because the server's meaning of stop is slightly different
3675 * than our client -- for the server, stop means to save state and give
3676 * it the result when it is done, but the window may still be visible.
3677 * For the client, we want to call onStop()/onStart() to indicate when
3678 * the activity's UI visibillity changes.
3679 */
Romain Guy65b345f2011-07-27 18:51:50 -07003680 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003681 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003682 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 if (r != null) {
3684 if (!keepShown && r.stopped) {
3685 if (r.activity.mFinished) {
3686 // If we are finishing, we won't call onResume() in certain
3687 // cases. So here we likewise don't want to call onStop()
3688 // if the activity isn't resumed.
3689 return;
3690 }
3691 RuntimeException e = new RuntimeException(
3692 "Performing stop of activity that is not resumed: "
3693 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003694 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 }
3696
3697 if (info != null) {
3698 try {
3699 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003700 // For now, don't create the thumbnail here; we are
3701 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 info.description = r.activity.onCreateDescription();
3703 } catch (Exception e) {
3704 if (!mInstrumentation.onException(r.activity, e)) {
3705 throw new RuntimeException(
3706 "Unable to save state of activity "
3707 + r.intent.getComponent().toShortString()
3708 + ": " + e.toString(), e);
3709 }
3710 }
3711 }
3712
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003713 // Next have the activity save its current state and managed dialogs...
3714 if (!r.activity.mFinished && saveState) {
3715 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003716 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003717 }
3718 }
3719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003720 if (!keepShown) {
3721 try {
3722 // Now we are idle.
3723 r.activity.performStop();
3724 } catch (Exception e) {
3725 if (!mInstrumentation.onException(r.activity, e)) {
3726 throw new RuntimeException(
3727 "Unable to stop activity "
3728 + r.intent.getComponent().toShortString()
3729 + ": " + e.toString(), e);
3730 }
3731 }
3732 r.stopped = true;
3733 }
3734
3735 r.paused = true;
3736 }
3737 }
3738
Romain Guy65b345f2011-07-27 18:51:50 -07003739 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 View v = r.activity.mDecor;
3741 if (v != null) {
3742 if (show) {
3743 if (!r.activity.mVisibleFromServer) {
3744 r.activity.mVisibleFromServer = true;
3745 mNumVisibleActivities++;
3746 if (r.activity.mVisibleFromClient) {
3747 r.activity.makeVisible();
3748 }
3749 }
3750 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003751 r.tmpConfig.setTo(r.newConfig);
3752 if (r.overrideConfig != null) {
3753 r.tmpConfig.updateFrom(r.overrideConfig);
3754 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003755 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003756 + r.activityInfo.name + " with new config " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003757 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003758 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 r.newConfig = null;
3760 }
3761 } else {
3762 if (r.activity.mVisibleFromServer) {
3763 r.activity.mVisibleFromServer = false;
3764 mNumVisibleActivities--;
3765 v.setVisibility(View.INVISIBLE);
3766 }
3767 }
3768 }
3769 }
3770
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003771 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003772 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003773 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3774 return;
3775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003776 r.activity.mConfigChangeFlags |= configChanges;
3777
3778 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003779 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003780
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003781 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003782 TAG, "Finishing stop of " + r + ": show=" + show
3783 + " win=" + r.window);
3784
3785 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003786
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003787 // Make sure any pending writes are now committed.
3788 if (!r.isPreHoneycomb()) {
3789 QueuedWork.waitToFinish();
3790 }
3791
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003792 // Schedule the call to tell the activity manager we have
3793 // stopped. We don't do this immediately, because we want to
3794 // have a chance for any other pending work (in particular memory
3795 // trim requests) to complete before you tell the activity
3796 // manager to proceed and allow us to go fully into the background.
3797 info.activity = r;
3798 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003799 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003800 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003801 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802 }
3803
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003804 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3805 String action) {
3806 if (r == null) {
3807 return true;
3808 }
3809 if (seq < r.lastProcessedSeq) {
3810 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3811 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3812 return false;
3813 }
3814 r.lastProcessedSeq = seq;
3815 return true;
3816 }
3817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003818 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003819 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 if (r.stopped) {
3821 r.activity.performRestart();
3822 r.stopped = false;
3823 }
3824 }
3825
Romain Guy65b345f2011-07-27 18:51:50 -07003826 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003827 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003828
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003829 if (r == null) {
3830 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3831 return;
3832 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003834 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003835 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003836 } else if (show && r.stopped) {
3837 // If we are getting ready to gc after going to the background, well
3838 // we are back active so skip it.
3839 unscheduleGcIdler();
3840
3841 r.activity.performRestart();
3842 r.stopped = false;
3843 }
3844 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003845 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 TAG, "Handle window " + r + " visibility: " + show);
3847 updateVisibility(r, show);
3848 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003849 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 }
3851
Romain Guy65b345f2011-07-27 18:51:50 -07003852 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003853 ActivityClientRecord r = mActivities.get(token);
3854
3855 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003856 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003857 return;
3858 }
3859
3860 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003861 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003862 try {
3863 // Now we are idle.
3864 r.activity.performStop();
3865 } catch (Exception e) {
3866 if (!mInstrumentation.onException(r.activity, e)) {
3867 throw new RuntimeException(
3868 "Unable to stop activity "
3869 + r.intent.getComponent().toShortString()
3870 + ": " + e.toString(), e);
3871 }
3872 }
3873 r.stopped = true;
3874 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003875
3876 // Make sure any pending writes are now committed.
3877 if (!r.isPreHoneycomb()) {
3878 QueuedWork.waitToFinish();
3879 }
3880
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003881 // Tell activity manager we slept.
3882 try {
3883 ActivityManagerNative.getDefault().activitySlept(r.token);
3884 } catch (RemoteException ex) {
3885 }
3886 } else {
3887 if (r.stopped && r.activity.mVisibleFromServer) {
3888 r.activity.performRestart();
3889 r.stopped = false;
3890 }
3891 }
3892 }
3893
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003894 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003895 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003896 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003897 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003898 onCoreSettingsChange();
3899 }
3900
3901 private void onCoreSettingsChange() {
3902 boolean debugViewAttributes =
3903 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3904 if (debugViewAttributes != View.mDebugViewAttributes) {
3905 View.mDebugViewAttributes = debugViewAttributes;
3906
3907 // request all activities to relaunch for the changes to take place
3908 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003909 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
3910 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07003911 }
3912 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003913 }
3914
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003915 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3916 LoadedApk apk = peekPackageInfo(data.pkg, false);
3917 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003918 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003919 }
3920 apk = peekPackageInfo(data.pkg, true);
3921 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003922 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003923 }
3924 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003925 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003926 }
3927
Romain Guy65b345f2011-07-27 18:51:50 -07003928 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003929 final int N = results.size();
3930 for (int i=0; i<N; i++) {
3931 ResultInfo ri = results.get(i);
3932 try {
3933 if (ri.mData != null) {
3934 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003935 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003937 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003938 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 r.activity.dispatchActivityResult(ri.mResultWho,
3940 ri.mRequestCode, ri.mResultCode, ri.mData);
3941 } catch (Exception e) {
3942 if (!mInstrumentation.onException(r.activity, e)) {
3943 throw new RuntimeException(
3944 "Failure delivering result " + ri + " to activity "
3945 + r.intent.getComponent().toShortString()
3946 + ": " + e.toString(), e);
3947 }
3948 }
3949 }
3950 }
3951
Romain Guy65b345f2011-07-27 18:51:50 -07003952 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003953 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003954 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 if (r != null) {
3956 final boolean resumed = !r.paused;
3957 if (!r.activity.mFinished && r.activity.mDecor != null
3958 && r.hideForNow && resumed) {
3959 // We had hidden the activity because it started another
3960 // one... we have gotten a result back and we are not
3961 // paused, so make sure our window is visible.
3962 updateVisibility(r, true);
3963 }
3964 if (resumed) {
3965 try {
3966 // Now we are idle.
3967 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003968 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003969 mInstrumentation.callActivityOnPause(r.activity);
3970 if (!r.activity.mCalled) {
3971 throw new SuperNotCalledException(
3972 "Activity " + r.intent.getComponent().toShortString()
3973 + " did not call through to super.onPause()");
3974 }
3975 } catch (SuperNotCalledException e) {
3976 throw e;
3977 } catch (Exception e) {
3978 if (!mInstrumentation.onException(r.activity, e)) {
3979 throw new RuntimeException(
3980 "Unable to pause activity "
3981 + r.intent.getComponent().toShortString()
3982 + ": " + e.toString(), e);
3983 }
3984 }
3985 }
3986 deliverResults(r, res.results);
3987 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003988 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003989 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003990 }
3991 }
3992 }
3993
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003994 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 return performDestroyActivity(token, finishing, 0, false);
3996 }
3997
Romain Guy65b345f2011-07-27 18:51:50 -07003998 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003999 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004000 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004001 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004002 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004004 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004005 r.activity.mConfigChangeFlags |= configChanges;
4006 if (finishing) {
4007 r.activity.mFinished = true;
4008 }
4009 if (!r.paused) {
4010 try {
4011 r.activity.mCalled = false;
4012 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08004013 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 r.activity.getComponentName().getClassName());
4015 if (!r.activity.mCalled) {
4016 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004017 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 + " did not call through to super.onPause()");
4019 }
4020 } catch (SuperNotCalledException e) {
4021 throw e;
4022 } catch (Exception e) {
4023 if (!mInstrumentation.onException(r.activity, e)) {
4024 throw new RuntimeException(
4025 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004026 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004027 + ": " + e.toString(), e);
4028 }
4029 }
4030 r.paused = true;
4031 }
4032 if (!r.stopped) {
4033 try {
4034 r.activity.performStop();
4035 } catch (SuperNotCalledException e) {
4036 throw e;
4037 } catch (Exception e) {
4038 if (!mInstrumentation.onException(r.activity, e)) {
4039 throw new RuntimeException(
4040 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004041 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004042 + ": " + e.toString(), e);
4043 }
4044 }
4045 r.stopped = true;
4046 }
4047 if (getNonConfigInstance) {
4048 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004049 r.lastNonConfigurationInstances
4050 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004051 } catch (Exception e) {
4052 if (!mInstrumentation.onException(r.activity, e)) {
4053 throw new RuntimeException(
4054 "Unable to retain activity "
4055 + r.intent.getComponent().toShortString()
4056 + ": " + e.toString(), e);
4057 }
4058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 }
4060 try {
4061 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004062 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 if (!r.activity.mCalled) {
4064 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004065 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004066 " did not call through to super.onDestroy()");
4067 }
4068 if (r.window != null) {
4069 r.window.closeAllPanels();
4070 }
4071 } catch (SuperNotCalledException e) {
4072 throw e;
4073 } catch (Exception e) {
4074 if (!mInstrumentation.onException(r.activity, e)) {
4075 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004076 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4077 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004078 }
4079 }
4080 }
4081 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004082 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 return r;
4084 }
4085
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004086 private static String safeToComponentShortString(Intent intent) {
4087 ComponentName component = intent.getComponent();
4088 return component == null ? "[Unknown]" : component.toShortString();
4089 }
4090
Romain Guy65b345f2011-07-27 18:51:50 -07004091 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004093 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 configChanges, getNonConfigInstance);
4095 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004096 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 WindowManager wm = r.activity.getWindowManager();
4098 View v = r.activity.mDecor;
4099 if (v != null) {
4100 if (r.activity.mVisibleFromServer) {
4101 mNumVisibleActivities--;
4102 }
4103 IBinder wtoken = v.getWindowToken();
4104 if (r.activity.mWindowAdded) {
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004105 if (r.onlyLocalRequest || r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004106 // Hold off on removing this until the new activity's
4107 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004108 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004109 r.mPendingRemoveWindowManager = wm;
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004110 if (r.mPreserveWindow) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004111 // We can only keep the part of the view hierarchy that we control,
4112 // everything else must be removed, because it might not be able to
4113 // behave properly when activity is relaunching.
4114 r.window.clearContentView();
4115 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004116 } else {
4117 wm.removeViewImmediate(v);
4118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004120 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004121 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 r.activity.getClass().getName(), "Activity");
4123 }
4124 r.activity.mDecor = null;
4125 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004126 if (r.mPendingRemoveWindow == null) {
4127 // If we are delaying the removal of the activity window, then
4128 // we can't clean up all windows here. Note that we can't do
4129 // so later either, which means any windows that aren't closed
4130 // by the app will leak. Well we try to warning them a lot
4131 // about leaking windows, because that is a bug, so if they are
4132 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004133 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004134 r.activity.getClass().getName(), "Activity");
4135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004136
4137 // Mocked out contexts won't be participating in the normal
4138 // process lifecycle, but if we're running with a proper
4139 // ApplicationContext we need to have it tear down things
4140 // cleanly.
4141 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004142 if (c instanceof ContextImpl) {
4143 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 r.activity.getClass().getName(), "Activity");
4145 }
4146 }
4147 if (finishing) {
4148 try {
4149 ActivityManagerNative.getDefault().activityDestroyed(token);
4150 } catch (RemoteException ex) {
4151 // If the system process has died, it's game over for everyone.
4152 }
4153 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004154 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
4156
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004157 /**
4158 * @param preserveWindow Whether the activity should try to reuse the window it created,
4159 * including the decor view after the relaunch.
4160 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004161 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004162 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004163 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004164 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004165 ActivityClientRecord target = null;
4166
Craig Mautner88c05892013-06-28 09:47:45 -07004167 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004168 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4169 ActivityClientRecord r = mRelaunchingActivities.get(i);
4170 if (r.token == token) {
4171 target = r;
4172 if (pendingResults != null) {
4173 if (r.pendingResults != null) {
4174 r.pendingResults.addAll(pendingResults);
4175 } else {
4176 r.pendingResults = pendingResults;
4177 }
4178 }
4179 if (pendingNewIntents != null) {
4180 if (r.pendingIntents != null) {
4181 r.pendingIntents.addAll(pendingNewIntents);
4182 } else {
4183 r.pendingIntents = pendingNewIntents;
4184 }
4185 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004186
4187 // For each relaunch request, activity manager expects an answer
4188 if (!r.onlyLocalRequest && fromServer) {
4189 try {
4190 ActivityManagerNative.getDefault().activityRelaunched(token);
4191 } catch (RemoteException e) {
4192 e.printStackTrace();
4193 }
4194 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004195 break;
4196 }
4197 }
4198
4199 if (target == null) {
4200 target = new ActivityClientRecord();
4201 target.token = token;
4202 target.pendingResults = pendingResults;
4203 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004204 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004205 if (!fromServer) {
4206 ActivityClientRecord existing = mActivities.get(token);
4207 if (existing != null) {
4208 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004209 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004210 }
4211 target.onlyLocalRequest = true;
4212 }
4213 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004214 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004215 }
4216
4217 if (fromServer) {
4218 target.startsNotResumed = notResumed;
4219 target.onlyLocalRequest = false;
4220 }
4221 if (config != null) {
4222 target.createdConfig = config;
4223 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004224 if (overrideConfig != null) {
4225 target.overrideConfig = overrideConfig;
4226 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004227 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004228 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004229 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004230 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this
4231 + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004232 }
4233
Romain Guy65b345f2011-07-27 18:51:50 -07004234 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 // If we are getting ready to gc after going to the background, well
4236 // we are back active so skip it.
4237 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004238 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004239
4240 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004241 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 // First: make sure we have the most recent configuration and most
4244 // recent version of the activity, or skip it if some previous call
4245 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004246 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004247 int N = mRelaunchingActivities.size();
4248 IBinder token = tmp.token;
4249 tmp = null;
4250 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004251 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004252 if (r.token == token) {
4253 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004254 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 mRelaunchingActivities.remove(i);
4256 i--;
4257 N--;
4258 }
4259 }
Bob Leee5408332009-09-04 18:31:17 -07004260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004262 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 return;
4264 }
Bob Leee5408332009-09-04 18:31:17 -07004265
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004266 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4267 + tmp.token + " with configChanges=0x"
4268 + Integer.toHexString(configChanges));
4269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270 if (mPendingConfiguration != null) {
4271 changedConfig = mPendingConfiguration;
4272 mPendingConfiguration = null;
4273 }
4274 }
Bob Leee5408332009-09-04 18:31:17 -07004275
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004276 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4277 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4278 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4279 } else {
4280 tmp.lastProcessedSeq = tmp.relaunchSeq;
4281 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004282 if (tmp.createdConfig != null) {
4283 // If the activity manager is passing us its current config,
4284 // assume that is really what we want regardless of what we
4285 // may have pending.
4286 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004287 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4288 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4289 if (changedConfig == null
4290 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4291 changedConfig = tmp.createdConfig;
4292 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004293 }
4294 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004295
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004296 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004297 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004299 // If there was a pending configuration change, execute it first.
4300 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004301 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004302 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004303 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004304 }
Bob Leee5408332009-09-04 18:31:17 -07004305
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004306 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004307 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004308 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004309 if (!tmp.onlyLocalRequest) {
4310 try {
4311 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4312 } catch (RemoteException e) {
4313 // If the system process has died, it's game over for everyone.
4314 }
4315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 return;
4317 }
Bob Leee5408332009-09-04 18:31:17 -07004318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004320 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004321 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004322 r.lastProcessedSeq = tmp.lastProcessedSeq;
4323 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004324 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004325
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004326 r.activity.mChangingConfigurations = true;
4327
Dianne Hackborne2b04802010-12-09 09:24:55 -08004328 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08004330 performPauseActivity(r.token, false, r.isPreHoneycomb());
4331 }
4332 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004333 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 }
Bob Leee5408332009-09-04 18:31:17 -07004335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004336 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 r.activity = null;
4339 r.window = null;
4340 r.hideForNow = false;
4341 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004342 // Merge any pending results and pending intents; don't just replace them
4343 if (tmp.pendingResults != null) {
4344 if (r.pendingResults == null) {
4345 r.pendingResults = tmp.pendingResults;
4346 } else {
4347 r.pendingResults.addAll(tmp.pendingResults);
4348 }
4349 }
4350 if (tmp.pendingIntents != null) {
4351 if (r.pendingIntents == null) {
4352 r.pendingIntents = tmp.pendingIntents;
4353 } else {
4354 r.pendingIntents.addAll(tmp.pendingIntents);
4355 }
4356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004358 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004359
Christopher Tateb70f3df2009-04-07 16:07:59 -07004360 handleLaunchActivity(r, currentIntent);
Jorim Jaggife89d122015-12-22 16:28:44 +01004361
4362 if (!tmp.onlyLocalRequest) {
4363 try {
4364 ActivityManagerNative.getDefault().activityRelaunched(r.token);
4365 } catch (RemoteException e) {
4366 // If the system process has died, it's game over for everyone.
4367 }
4368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004369 }
4370
Craig Mautnera0026042014-04-23 11:45:37 -07004371 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4372 r.state = new Bundle();
4373 r.state.setAllowFds(false);
4374 if (r.isPersistable()) {
4375 r.persistentState = new PersistableBundle();
4376 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4377 r.persistentState);
4378 } else {
4379 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4380 }
4381 }
4382
Dianne Hackborn73c14162012-09-19 15:45:06 -07004383 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004385 ArrayList<ComponentCallbacks2> callbacks
4386 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004387
Craig Mautner88c05892013-06-28 09:47:45 -07004388 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004389 final int NAPP = mAllApplications.size();
4390 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004391 callbacks.add(mAllApplications.get(i));
4392 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004393 final int NACT = mActivities.size();
4394 for (int i=0; i<NACT; i++) {
4395 ActivityClientRecord ar = mActivities.valueAt(i);
4396 Activity a = ar.activity;
4397 if (a != null) {
4398 Configuration thisConfig = applyConfigCompatMainThread(
4399 mCurDefaultDisplayDpi, newConfig,
4400 ar.packageInfo.getCompatibilityInfo());
4401 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4402 // If the activity is currently resumed, its configuration
4403 // needs to change right now.
4404 callbacks.add(a);
4405 } else if (thisConfig != null) {
4406 // Otherwise, we will tell it about the change
4407 // the next time it is resumed or shown. Note that
4408 // the activity manager may, before then, decide the
4409 // activity needs to be destroyed to handle its new
4410 // configuration.
4411 if (DEBUG_CONFIGURATION) {
4412 Slog.v(TAG, "Setting activity "
4413 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004414 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004415 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 }
4417 }
4418 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004419 final int NSVC = mServices.size();
4420 for (int i=0; i<NSVC; i++) {
4421 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 }
4423 }
4424 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004425 final int NPRV = mLocalProviders.size();
4426 for (int i=0; i<NPRV; i++) {
4427 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 }
4429 }
Bob Leee5408332009-09-04 18:31:17 -07004430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 return callbacks;
4432 }
Bob Leee5408332009-09-04 18:31:17 -07004433
Filip Gruszczynskica664812015-12-04 12:43:36 -08004434 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config,
4435 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004437 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 // we check the runtime type and act accordingly.
4439 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4440 if (activity != null) {
4441 activity.mCalled = false;
4442 }
Bob Leee5408332009-09-04 18:31:17 -07004443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 boolean shouldChangeConfig = false;
4445 if ((activity == null) || (activity.mCurrentConfig == null)) {
4446 shouldChangeConfig = true;
4447 } else {
Bob Leee5408332009-09-04 18:31:17 -07004448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 // If the new config is the same as the config this Activity
4450 // is already running with then don't bother calling
4451 // onConfigurationChanged
4452 int diff = activity.mCurrentConfig.diff(config);
4453 if (diff != 0) {
Wale Ogunwale14e811f2015-12-14 15:04:22 -08004454 // If this activity doesn't handle any of the config changes then don't bother
4455 // calling onConfigurationChanged as we're going to destroy it.
4456 // Except in the case where the configuration changed on the activity manager side,
4457 // but wasn't big enough to cause a resource change so the activity wasn't destroyed.
4458 // In this case we still want to change the configuration of the activity but not
4459 // report it to the app.
4460 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4461 || !reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004462 shouldChangeConfig = true;
4463 }
4464 }
4465 }
Bob Leee5408332009-09-04 18:31:17 -07004466
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004467 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004468 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 if (shouldChangeConfig) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004470 if (reportToActivity) {
4471 cb.onConfigurationChanged(config);
4472 }
Bob Leee5408332009-09-04 18:31:17 -07004473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004475 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 throw new SuperNotCalledException(
4477 "Activity " + activity.getLocalClassName() +
4478 " did not call through to super.onConfigurationChanged()");
4479 }
4480 activity.mConfigChangeFlags = 0;
4481 activity.mCurrentConfig = new Configuration(config);
4482 }
4483 }
4484 }
4485
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004486 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004487 synchronized (mResourcesManager) {
4488 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004489 }
4490 }
4491
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004492 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004493 Configuration config = mConfiguration;
4494 if (mCompatConfiguration == null) {
4495 mCompatConfiguration = new Configuration();
4496 }
4497 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004498 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004499 config = mCompatConfiguration;
4500 }
4501 return config;
4502 }
4503
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004504 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004505
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004506 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004507
Craig Mautner88c05892013-06-28 09:47:45 -07004508 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004509 if (mPendingConfiguration != null) {
4510 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4511 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004512 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004513 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004514 }
4515 mPendingConfiguration = null;
4516 }
4517
4518 if (config == null) {
4519 return;
4520 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004521
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004522 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004523 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004524
4525 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 if (mConfiguration == null) {
4528 mConfiguration = new Configuration();
4529 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004530 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004531 return;
4532 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004533
4534 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004535 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004536
4537 final Theme systemTheme = getSystemContext().getTheme();
4538 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4539 systemTheme.rebase();
4540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004542
4543 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4544
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004545 freeTextLayoutCachesIfNeeded(configDiff);
4546
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004547 if (callbacks != null) {
4548 final int N = callbacks.size();
4549 for (int i=0; i<N; i++) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004550 performConfigurationChanged(callbacks.get(i), config, REPORT_TO_ACTIVITY);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 }
4553 }
4554
Romain Guy46bfc482013-08-16 18:38:29 -07004555 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004556 if (configDiff != 0) {
4557 // Ask text layout engine to free its caches if there is a locale change
4558 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4559 if (hasLocaleConfigChange) {
4560 Canvas.freeTextLayoutCaches();
4561 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4562 }
4563 }
4564 }
4565
Filip Gruszczynskica664812015-12-04 12:43:36 -08004566 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4567 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004568 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 if (r == null || r.activity == null) {
4570 return;
4571 }
Bob Leee5408332009-09-04 18:31:17 -07004572
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004573 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004574 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004575
4576 r.tmpConfig.setTo(mCompatConfiguration);
4577 if (data.overrideConfig != null) {
4578 r.overrideConfig = data.overrideConfig;
4579 r.tmpConfig.updateFrom(data.overrideConfig);
4580 }
Filip Gruszczynskica664812015-12-04 12:43:36 -08004581 performConfigurationChanged(r.activity, r.tmpConfig, reportToActivity);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004582
4583 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004584
4585 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004586 }
4587
Jeff Hao1b012d32014-08-20 10:35:34 -07004588 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004589 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004590 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004591 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004592 default:
4593 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004594 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004595 break;
4596 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004597 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004598 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004599 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004600 } finally {
4601 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004602 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004603 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004604 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004605 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004606 }
4607 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004608 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004609 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004610 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004611 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004612 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004613 }
4614 }
Bob Leee5408332009-09-04 18:31:17 -07004615
Romain Guya998dff2012-03-23 18:58:36 -07004616 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004617 if (managed) {
4618 try {
4619 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4620 } catch (IOException e) {
4621 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4622 + " -- can the process access this path?");
4623 } finally {
4624 try {
4625 dhd.fd.close();
4626 } catch (IOException e) {
4627 Slog.w(TAG, "Failure closing profile fd", e);
4628 }
4629 }
4630 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004631 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004632 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004633 try {
4634 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4635 } catch (RemoteException e) {
4636 }
Andy McFadden824c5102010-07-09 16:26:57 -07004637 }
4638
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004639 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4640 boolean hasPkgInfo = false;
4641 if (packages != null) {
mark_chen89764e32014-12-12 15:38:48 +08004642 synchronized (mResourcesManager) {
4643 for (int i=packages.length-1; i>=0; i--) {
4644 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4645 if (!hasPkgInfo) {
4646 WeakReference<LoadedApk> ref;
4647 ref = mPackages.get(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004648 if (ref != null && ref.get() != null) {
4649 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004650 } else {
4651 ref = mResourcePackages.get(packages[i]);
4652 if (ref != null && ref.get() != null) {
4653 hasPkgInfo = true;
4654 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004655 }
4656 }
mark_chen89764e32014-12-12 15:38:48 +08004657 mPackages.remove(packages[i]);
4658 mResourcePackages.remove(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004659 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004660 }
4661 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004662 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004663 hasPkgInfo);
4664 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004666 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004667 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 final int N = callbacks.size();
4670 for (int i=0; i<N; i++) {
4671 callbacks.get(i).onLowMemory();
4672 }
4673
Chris Tatece229052009-03-25 16:44:52 -07004674 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4675 if (Process.myUid() != Process.SYSTEM_UID) {
4676 int sqliteReleased = SQLiteDatabase.releaseMemory();
4677 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4678 }
Bob Leee5408332009-09-04 18:31:17 -07004679
Mike Reedcaf0df12009-04-27 14:32:05 -04004680 // Ask graphics to free up as much as possible (font/image caches)
4681 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004682
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004683 // Ask text layout engine to free also as much as possible
4684 Canvas.freeTextLayoutCaches();
4685
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 BinderInternal.forceGc("mem");
4687 }
4688
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004689 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004690 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004691
Dianne Hackborn73c14162012-09-19 15:45:06 -07004692 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004693
4694 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004695 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004696 callbacks.get(i).onTrimMemory(level);
4697 }
Romain Guy19f86e82012-04-23 15:19:07 -07004698
John Reckf47a5942014-06-30 16:20:04 -07004699 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004700 }
4701
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004702 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004703 if (Process.isIsolated()) {
4704 // Isolated processes aren't going to do UI.
4705 return;
4706 }
Romain Guya9582652011-11-10 14:20:10 -08004707 try {
4708 int uid = Process.myUid();
4709 String[] packages = getPackageManager().getPackagesForUid(uid);
4710
4711 // If there are several packages in this application we won't
Tim Murraye1e6c662015-04-07 13:24:14 -07004712 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004713 if (packages != null && packages.length == 1) {
John Reck51aaf902015-12-02 15:08:07 -08004714 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004715 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004716 }
4717 } catch (RemoteException e) {
4718 // Ignore
4719 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004720 }
4721
Calin Juravle69052392015-12-17 17:06:49 +02004722 private static void setupJitProfileSupport(LoadedApk loadedApk, File cacheDir) {
Calin Juravledb4a79a2015-12-23 18:55:08 +02004723 if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
4724 return;
4725 }
Calin Juravle69052392015-12-17 17:06:49 +02004726 final ApplicationInfo appInfo = loadedApk.getApplicationInfo();
4727 if (isSharingRuntime(appInfo)) {
4728 // If sharing is enabled we do not have a unique application
4729 // in a process and therefore cannot rely on the package
4730 // name inside the runtime.
4731 return;
4732 }
4733 final List<String> codePaths = new ArrayList<>();
4734 if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
4735 codePaths.add(appInfo.sourceDir);
4736 }
4737 if (appInfo.splitSourceDirs != null) {
4738 Collections.addAll(codePaths, appInfo.splitSourceDirs);
4739 }
4740
4741 if (codePaths.isEmpty()) {
4742 // If there are no code paths there's no need to setup a profile file and register with
4743 // the runtime,
4744 return;
4745 }
4746
4747 // Add an extension to the file name to better reveal its intended use.
4748 // Keep in sync with BackgroundDexOptService.
4749 final String profileExtension = ".prof";
4750 final File profileFile = new File(cacheDir, loadedApk.mPackageName + profileExtension);
4751 if (!profileFile.exists()) {
4752 FileDescriptor fd = null;
4753 try {
4754 final int permissions = 0600; // read-write for user.
4755 fd = Os.open(profileFile.getAbsolutePath(), OsConstants.O_CREAT, permissions);
4756 Os.fchmod(fd, permissions);
4757 Os.fchown(fd, appInfo.uid, appInfo.uid);
4758 } catch (ErrnoException e) {
4759 Log.w(TAG, "Unable to create jit profile file " + profileFile, e);
4760 try {
4761 Os.unlink(profileFile.getAbsolutePath());
4762 } catch (ErrnoException unlinkErr) {
4763 Log.v(TAG, "Unable to unlink jit profile file " + profileFile, unlinkErr);
4764 }
4765 return;
4766 } finally {
4767 IoUtils.closeQuietly(fd);
4768 }
4769 }
4770
4771 VMRuntime.registerAppInfo(profileFile.getAbsolutePath(), appInfo.dataDir,
4772 codePaths.toArray(new String[codePaths.size()]));
4773 }
4774
4775 /*
4776 * Two possible indications that this package could be
4777 * sharing its runtime with other packages:
4778 *
4779 * 1) the sharedUserId attribute is set in the manifest,
4780 * indicating a request to share a VM with other
4781 * packages with the same sharedUserId.
4782 *
4783 * 2) the application element of the manifest has an
4784 * attribute specifying a non-default process name,
4785 * indicating the desire to run in another packages VM.
4786 */
4787 private static boolean isSharingRuntime(ApplicationInfo appInfo) {
4788 IPackageManager pm = getPackageManager();
4789 android.content.pm.PackageInfo pi = null;
4790 try {
4791 pi = pm.getPackageInfo(appInfo.packageName, 0, UserHandle.myUserId());
4792 } catch (RemoteException e) {
4793 }
4794 if (pi != null) {
4795 boolean sharedUserIdSet = (pi.sharedUserId != null);
4796 boolean processNameNotDefault = (pi.applicationInfo != null) &&
4797 !appInfo.packageName.equals(pi.applicationInfo.processName);
4798 boolean sharable = sharedUserIdSet || processNameNotDefault;
4799 return sharable;
4800 }
4801 // We couldn't get information for the package. Be pessimistic and assume
4802 // it's sharing the runtime.
4803 return true;
4804 }
4805
Dianne Hackborndde331c2012-08-03 14:01:57 -07004806 private void updateDefaultDensity() {
4807 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4808 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4809 && !mDensityCompatMode) {
4810 Slog.i(TAG, "Switching default density from "
4811 + DisplayMetrics.DENSITY_DEVICE + " to "
4812 + mCurDefaultDisplayDpi);
4813 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4814 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4815 }
4816 }
4817
Romain Guy65b345f2011-07-27 18:51:50 -07004818 private void handleBindApplication(AppBindData data) {
Man Caocfa78b22015-06-11 20:14:34 -07004819 if (data.trackAllocation) {
4820 DdmVmInternal.enableRecentAllocations(true);
4821 }
4822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 mBoundApplication = data;
4824 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004825 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004826
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004827 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004828 if (data.initProfilerInfo != null) {
4829 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4830 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4831 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4832 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4833 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004835 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004836 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004837 android.ddm.DdmHandleAppName.setAppName(data.processName,
4838 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004839
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004840 if (data.persistent) {
4841 // Persistent processes on low-memory devices do not get to
4842 // use hardware accelerated drawing, since this can add too much
4843 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004844 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08004845 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004846 }
4847 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004848
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004849 if (mProfiler.profileFd != null) {
4850 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004851 }
4852
Joe Onoratod630f102011-03-17 18:42:26 -07004853 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4854 // implementation to use the pool executor. Normally, we use the
4855 // serialized executor as the default. This has to happen in the
4856 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004857 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004858 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4859 }
4860
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004861 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004863 /*
4864 * Before spawning a new process, reset the time zone to be the system time zone.
4865 * This needs to be done because the system time zone could have changed after the
4866 * the spawning of this process. Without doing this this process would have the incorrect
4867 * system time zone.
4868 */
4869 TimeZone.setDefault(null);
4870
4871 /*
4872 * Initialize the default locale in this process for the reasons we set the time zone.
4873 */
4874 Locale.setDefault(data.config.locale);
4875
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004876 /*
4877 * Update the system configuration since its preloaded and might not
4878 * reflect configuration changes. The configuration object passed
4879 * in AppBindData can be safely assumed to be up to date
4880 */
Craig Mautner88c05892013-06-28 09:47:45 -07004881 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004882 mCurDefaultDisplayDpi = data.config.densityDpi;
4883 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004884
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004885 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886
Dianne Hackborndde331c2012-08-03 14:01:57 -07004887 /**
4888 * Switch this process to density compatibility mode if needed.
4889 */
4890 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4891 == 0) {
4892 mDensityCompatMode = true;
4893 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4894 }
4895 updateDefaultDensity();
4896
Narayan Kamathccb2a0862013-12-19 14:49:36 +00004897 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4898 DateFormat.set24HourTimePref(is24Hr);
4899
Jon Miranda836c0a82014-08-11 12:32:26 -07004900 View.mDebugViewAttributes =
4901 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4902
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004903 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004904 * For system applications on userdebug/eng builds, log stack
4905 * traces of disk and network access to dropbox for analysis.
4906 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004907 if ((data.appInfo.flags &
4908 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004909 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4910 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004911 }
4912
4913 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004914 * For apps targetting SDK Honeycomb or later, we don't allow
4915 * network usage on the main event loop / UI thread.
4916 *
4917 * Note to those grepping: this is what ultimately throws
4918 * NetworkOnMainThreadException ...
4919 */
4920 if (data.appInfo.targetSdkVersion > 9) {
4921 StrictMode.enableDeathOnNetwork();
4922 }
4923
Alex Klyubinf9034cc2015-02-12 11:43:09 -08004924 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
4925 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
4926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4928 // XXX should have option to change the port.
4929 Debug.changeDebugPort(8100);
4930 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004931 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 + " is waiting for the debugger on port 8100...");
4933
4934 IActivityManager mgr = ActivityManagerNative.getDefault();
4935 try {
4936 mgr.showWaitingForDebugger(mAppThread, true);
4937 } catch (RemoteException ex) {
4938 }
4939
4940 Debug.waitForDebugger();
4941
4942 try {
4943 mgr.showWaitingForDebugger(mAppThread, false);
4944 } catch (RemoteException ex) {
4945 }
4946
4947 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004948 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004949 + " can be debugged on port 8100...");
4950 }
4951 }
4952
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004953 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04004954 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4955 Trace.setAppTracingAllowed(isAppDebuggable);
4956 if (isAppDebuggable && data.enableBinderTracking) {
4957 Binder.enableTracing();
4958 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004959
Robert Greenwalt434203a2010-10-11 16:00:27 -07004960 /**
4961 * Initialize the default http proxy in this process for the reasons we set the time zone.
4962 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04004963 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004964 if (b != null) {
4965 // In pre-boot mode (doing initial launch to collect password), not
4966 // all system is up. This includes the connectivity service, so don't
4967 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04004968 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004969 try {
Paul Jensencee9b512015-05-06 07:32:40 -04004970 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04004971 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004972 } catch (RemoteException e) {}
4973 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004974
Alan Viverette2107d692015-09-03 14:55:27 -04004975 // Instrumentation info affects the class loader, so load it before
4976 // setting up the app context.
4977 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 try {
Alan Viverette2107d692015-09-03 14:55:27 -04004980 ii = new ApplicationPackageManager(null, getPackageManager())
4981 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004982 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04004984 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 }
4986
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004987 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004988 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004989 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
4990 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004991 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004992 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
4993 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04004994 } else {
4995 ii = null;
4996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997
Alan Viverette2107d692015-09-03 14:55:27 -04004998 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
4999 if (!Process.isIsolated()) {
5000 final File cacheDir = appContext.getCacheDir();
5001 if (cacheDir != null) {
5002 // Provide a usable directory for temporary files
5003 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5004 } else {
5005 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5006 + "due to missing cache directory");
5007 }
Alan Viverette346296b2015-09-01 13:08:05 -04005008
Calin Juravlec74d3382016-01-07 12:15:36 +00005009 // Use codeCacheDir to store generated/compiled graphics code and jit profiling data.
Alan Viverette2107d692015-09-03 14:55:27 -04005010 final File codeCacheDir = appContext.getCodeCacheDir();
5011 if (codeCacheDir != null) {
5012 setupGraphicsSupport(data.info, codeCacheDir);
Calin Juravlec74d3382016-01-07 12:15:36 +00005013 setupJitProfileSupport(data.info, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005014 } else {
Calin Juravlec74d3382016-01-07 12:15:36 +00005015 Log.e(TAG, "Unable to setupGraphicsSupport and setupJitProfileSupport " +
5016 "due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005017 }
5018 }
5019
Chad Brubaker78d47122015-11-17 22:26:58 -08005020 // Install the Network Security Config Provider. This must happen before the application
5021 // code is loaded to prevent issues with instances of TLS objects being created before
5022 // the provider is installed.
5023 NetworkSecurityConfigProvider.install(appContext);
5024
Alan Viverette2107d692015-09-03 14:55:27 -04005025 // Continue loading instrumentation.
5026 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005027 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005028 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005029 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005030 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005031 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005032 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005033
5034 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005035 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 mInstrumentation = (Instrumentation)
5037 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5038 } catch (Exception e) {
5039 throw new RuntimeException(
5040 "Unable to instantiate instrumentation "
5041 + data.instrumentationName + ": " + e.toString(), e);
5042 }
5043
Alan Viverettebe64eae2015-09-03 14:56:04 -04005044 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5045 mInstrumentation.init(this, instrContext, appContext, component,
5046 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005048 if (mProfiler.profileFile != null && !ii.handleProfiling
5049 && mProfiler.profileFd == null) {
5050 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005051 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 file.getParentFile().mkdirs();
5053 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5054 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 } else {
5056 mInstrumentation = new Instrumentation();
5057 }
5058
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005059 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005060 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005061 } else {
5062 // Small heap, clamp to the current growth limit and let the heap release
5063 // pages after the growth limit to the non growth limit capacity. b/18387825
5064 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005065 }
5066
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005067 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005068 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005069 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005070 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005071 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005072 // If the app is being launched for full backup or restore, bring it up in
5073 // a restricted environment with the base application class.
5074 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5075 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005076
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005077 // don't bring up providers in restricted mode; they may depend on the
5078 // app's custom Application class
5079 if (!data.restrictedBackupMode) {
5080 List<ProviderInfo> providers = data.providers;
5081 if (providers != null) {
5082 installContentProviders(app, providers);
5083 // For process that contains content providers, we want to
5084 // ensure that the JIT is enabled "at some point".
5085 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5086 }
5087 }
5088
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005089 // Do this after providers, since instrumentation tests generally start their
5090 // test thread at this point, and we don't want that racing.
5091 try {
5092 mInstrumentation.onCreate(data.instrumentationArgs);
5093 }
5094 catch (Exception e) {
5095 throw new RuntimeException(
5096 "Exception thrown in onCreate() of "
5097 + data.instrumentationName + ": " + e.toString(), e);
5098 }
5099
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005100 try {
5101 mInstrumentation.callApplicationOnCreate(app);
5102 } catch (Exception e) {
5103 if (!mInstrumentation.onException(app, e)) {
5104 throw new RuntimeException(
5105 "Unable to create application " + app.getClass().getName()
5106 + ": " + e.toString(), e);
5107 }
5108 }
5109 } finally {
5110 StrictMode.setThreadPolicy(savedPolicy);
5111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 }
5113
5114 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5115 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005116 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5117 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 Debug.stopMethodTracing();
5119 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005120 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 // + ", app thr: " + mAppThread);
5122 try {
5123 am.finishInstrumentation(mAppThread, resultCode, results);
5124 } catch (RemoteException ex) {
5125 }
5126 }
5127
Romain Guy65b345f2011-07-27 18:51:50 -07005128 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 Context context, List<ProviderInfo> providers) {
5130 final ArrayList<IActivityManager.ContentProviderHolder> results =
5131 new ArrayList<IActivityManager.ContentProviderHolder>();
5132
Romain Guya998dff2012-03-23 18:58:36 -07005133 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005134 if (DEBUG_PROVIDER) {
5135 StringBuilder buf = new StringBuilder(128);
5136 buf.append("Pub ");
5137 buf.append(cpi.authority);
5138 buf.append(": ");
5139 buf.append(cpi.name);
5140 Log.i(TAG, buf.toString());
5141 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005142 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5143 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5144 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005145 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005147 }
5148 }
5149
5150 try {
5151 ActivityManagerNative.getDefault().publishContentProviders(
5152 getApplicationThread(), results);
5153 } catch (RemoteException ex) {
5154 }
5155 }
5156
Jeff Sharkey6d515712012-09-20 16:06:08 -07005157 public final IContentProvider acquireProvider(
5158 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005159 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005160 if (provider != null) {
5161 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
5163
Wale Ogunwale1d646122015-04-24 14:45:14 -07005164 // There is a possible race here. Another thread may try to acquire
5165 // the same provider at the same time. When this happens, we want to ensure
5166 // that the first one wins.
5167 // Note that we cannot hold the lock while acquiring and installing the
5168 // provider since it might take a long time to run and it could also potentially
5169 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005171 try {
5172 holder = ActivityManagerNative.getDefault().getContentProvider(
5173 getApplicationThread(), auth, userId, stable);
5174 } catch (RemoteException ex) {
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005175 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005177 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178 return null;
5179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005181 // Install provider will increment the reference count for us, and break
5182 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005183 holder = installProvider(c, holder, holder.info,
5184 true /*noisy*/, holder.noReleaseNeeded, stable);
5185 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186 }
5187
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005188 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5189 if (stable) {
5190 prc.stableCount += 1;
5191 if (prc.stableCount == 1) {
5192 // We are acquiring a new stable reference on the provider.
5193 int unstableDelta;
5194 if (prc.removePending) {
5195 // We have a pending remove operation, which is holding the
5196 // last unstable reference. At this point we are converting
5197 // that unstable reference to our new stable reference.
5198 unstableDelta = -1;
5199 // Cancel the removal of the provider.
5200 if (DEBUG_PROVIDER) {
5201 Slog.v(TAG, "incProviderRef: stable "
5202 + "snatched provider from the jaws of death");
5203 }
5204 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005205 // There is a race! It fails to remove the message, which
5206 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005207 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5208 } else {
5209 unstableDelta = 0;
5210 }
5211 try {
5212 if (DEBUG_PROVIDER) {
5213 Slog.v(TAG, "incProviderRef Now stable - "
5214 + prc.holder.info.name + ": unstableDelta="
5215 + unstableDelta);
5216 }
5217 ActivityManagerNative.getDefault().refContentProvider(
5218 prc.holder.connection, 1, unstableDelta);
5219 } catch (RemoteException e) {
5220 //do nothing content provider object is dead any way
5221 }
5222 }
5223 } else {
5224 prc.unstableCount += 1;
5225 if (prc.unstableCount == 1) {
5226 // We are acquiring a new unstable reference on the provider.
5227 if (prc.removePending) {
5228 // Oh look, we actually have a remove pending for the
5229 // provider, which is still holding the last unstable
5230 // reference. We just need to cancel that to take new
5231 // ownership of the reference.
5232 if (DEBUG_PROVIDER) {
5233 Slog.v(TAG, "incProviderRef: unstable "
5234 + "snatched provider from the jaws of death");
5235 }
5236 prc.removePending = false;
5237 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5238 } else {
5239 // First unstable ref, increment our count in the
5240 // activity manager.
5241 try {
5242 if (DEBUG_PROVIDER) {
5243 Slog.v(TAG, "incProviderRef: Now unstable - "
5244 + prc.holder.info.name);
5245 }
5246 ActivityManagerNative.getDefault().refContentProvider(
5247 prc.holder.connection, 0, 1);
5248 } catch (RemoteException e) {
5249 //do nothing content provider object is dead any way
5250 }
5251 }
5252 }
5253 }
5254 }
5255
Jeff Sharkey6d515712012-09-20 16:06:08 -07005256 public final IContentProvider acquireExistingProvider(
5257 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005258 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005259 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005260 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005261 if (pr == null) {
5262 return null;
5263 }
5264
5265 IContentProvider provider = pr.mProvider;
5266 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005267 if (!jBinder.isBinderAlive()) {
5268 // The hosting process of the provider has died; we can't
5269 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005270 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005271 + ": existing object's process dead");
5272 handleUnstableProviderDiedLocked(jBinder, true);
5273 return null;
5274 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005275
5276 // Only increment the ref count if we have one. If we don't then the
5277 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005278 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005279 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005280 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005281 }
5282 return provider;
5283 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005284 }
5285
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005286 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5287 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 return false;
5289 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005292 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005294 if (prc == null) {
5295 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005297 }
5298
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005299 boolean lastRef = false;
5300 if (stable) {
5301 if (prc.stableCount == 0) {
5302 if (DEBUG_PROVIDER) Slog.v(TAG,
5303 "releaseProvider: stable ref count already 0, how?");
5304 return false;
5305 }
5306 prc.stableCount -= 1;
5307 if (prc.stableCount == 0) {
5308 // What we do at this point depends on whether there are
5309 // any unstable refs left: if there are, we just tell the
5310 // activity manager to decrement its stable count; if there
5311 // aren't, we need to enqueue this provider to be removed,
5312 // and convert to holding a single unstable ref while
5313 // doing so.
5314 lastRef = prc.unstableCount == 0;
5315 try {
5316 if (DEBUG_PROVIDER) {
5317 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5318 + lastRef + " - " + prc.holder.info.name);
5319 }
5320 ActivityManagerNative.getDefault().refContentProvider(
5321 prc.holder.connection, -1, lastRef ? 1 : 0);
5322 } catch (RemoteException e) {
5323 //do nothing content provider object is dead any way
5324 }
5325 }
5326 } else {
5327 if (prc.unstableCount == 0) {
5328 if (DEBUG_PROVIDER) Slog.v(TAG,
5329 "releaseProvider: unstable ref count already 0, how?");
5330 return false;
5331 }
5332 prc.unstableCount -= 1;
5333 if (prc.unstableCount == 0) {
5334 // If this is the last reference, we need to enqueue
5335 // this provider to be removed instead of telling the
5336 // activity manager to remove it at this point.
5337 lastRef = prc.stableCount == 0;
5338 if (!lastRef) {
5339 try {
5340 if (DEBUG_PROVIDER) {
5341 Slog.v(TAG, "releaseProvider: No longer unstable - "
5342 + prc.holder.info.name);
5343 }
5344 ActivityManagerNative.getDefault().refContentProvider(
5345 prc.holder.connection, 0, -1);
5346 } catch (RemoteException e) {
5347 //do nothing content provider object is dead any way
5348 }
5349 }
5350 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005351 }
5352
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005353 if (lastRef) {
5354 if (!prc.removePending) {
5355 // Schedule the actual remove asynchronously, since we don't know the context
5356 // this will be called in.
5357 // TODO: it would be nice to post a delayed message, so
5358 // if we come back and need the same provider quickly
5359 // we will still have it available.
5360 if (DEBUG_PROVIDER) {
5361 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5362 + prc.holder.info.name);
5363 }
5364 prc.removePending = true;
5365 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5366 mH.sendMessage(msg);
5367 } else {
5368 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5369 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005370 }
5371 return true;
5372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 }
5374
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005375 final void completeRemoveProvider(ProviderRefCount prc) {
5376 synchronized (mProviderMap) {
5377 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005378 // There was a race! Some other client managed to acquire
5379 // the provider before the removal was completed.
5380 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005381 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005382 + "provider still in use");
5383 return;
5384 }
5385
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005386 // More complicated race!! Some client managed to acquire the
5387 // provider and release it before the removal was completed.
5388 // Continue the removal, and abort the next remove message.
5389 prc.removePending = false;
5390
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005391 final IBinder jBinder = prc.holder.provider.asBinder();
5392 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5393 if (existingPrc == prc) {
5394 mProviderRefCountMap.remove(jBinder);
5395 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005396
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005397 for (int i=mProviderMap.size()-1; i>=0; i--) {
5398 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005399 IBinder myBinder = pr.mProvider.asBinder();
5400 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005401 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005402 }
5403 }
5404 }
5405
5406 try {
5407 if (DEBUG_PROVIDER) {
5408 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5409 + "removeContentProvider(" + prc.holder.info.name + ")");
5410 }
5411 ActivityManagerNative.getDefault().removeContentProvider(
5412 prc.holder.connection, false);
5413 } catch (RemoteException e) {
5414 //do nothing content provider object is dead any way
5415 }
5416 }
5417
5418 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005419 synchronized (mProviderMap) {
5420 handleUnstableProviderDiedLocked(provider, fromClient);
5421 }
5422 }
5423
5424 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5425 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5426 if (prc != null) {
5427 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5428 + provider + " " + prc.holder.info.name);
5429 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005430 for (int i=mProviderMap.size()-1; i>=0; i--) {
5431 ProviderClientRecord pr = mProviderMap.valueAt(i);
5432 if (pr != null && pr.mProvider.asBinder() == provider) {
5433 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5434 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005435 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005436 }
You Kimbc74de62013-10-01 00:13:26 +09005437
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005438 if (fromClient) {
5439 // We found out about this due to execution in our client
5440 // code. Tell the activity manager about it now, to ensure
5441 // that the next time we go to do anything with the provider
5442 // it knows it is dead (so we don't race with its death
5443 // notification).
5444 try {
5445 ActivityManagerNative.getDefault().unstableProviderDied(
5446 prc.holder.connection);
5447 } catch (RemoteException e) {
5448 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005449 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005450 }
5451 }
5452 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005453
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005454 final void appNotRespondingViaProvider(IBinder provider) {
5455 synchronized (mProviderMap) {
5456 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5457 if (prc != null) {
5458 try {
5459 ActivityManagerNative.getDefault()
5460 .appNotRespondingViaProvider(prc.holder.connection);
5461 } catch (RemoteException e) {
5462 }
5463 }
5464 }
5465 }
5466
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005467 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005468 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005469 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005470 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5471
5472 final ProviderClientRecord pcr = new ProviderClientRecord(
5473 auths, provider, localProvider, holder);
5474 for (String auth : auths) {
5475 final ProviderKey key = new ProviderKey(auth, userId);
5476 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005477 if (existing != null) {
5478 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005479 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005480 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005481 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 }
5483 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005484 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 }
5486
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005487 /**
5488 * Installs the provider.
5489 *
5490 * Providers that are local to the process or that come from the system server
5491 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5492 * Other remote providers are reference counted. The initial reference count
5493 * for all reference counted providers is one. Providers that are not reference
5494 * counted do not have a reference count (at all).
5495 *
5496 * This method detects when a provider has already been installed. When this happens,
5497 * it increments the reference count of the existing provider (if appropriate)
5498 * and returns the existing provider. This can happen due to concurrent
5499 * attempts to acquire the same provider.
5500 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005501 private IActivityManager.ContentProviderHolder installProvider(Context context,
5502 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5503 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005505 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005506 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005507 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005508 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 + info.name);
5510 }
5511 Context c = null;
5512 ApplicationInfo ai = info.applicationInfo;
5513 if (context.getPackageName().equals(ai.packageName)) {
5514 c = context;
5515 } else if (mInitialApplication != null &&
5516 mInitialApplication.getPackageName().equals(ai.packageName)) {
5517 c = mInitialApplication;
5518 } else {
5519 try {
5520 c = context.createPackageContext(ai.packageName,
5521 Context.CONTEXT_INCLUDE_CODE);
5522 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005523 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 }
5525 }
5526 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005527 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005528 ai.packageName +
5529 " while loading content provider " +
5530 info.name);
5531 return null;
5532 }
5533 try {
5534 final java.lang.ClassLoader cl = c.getClassLoader();
5535 localProvider = (ContentProvider)cl.
5536 loadClass(info.name).newInstance();
5537 provider = localProvider.getIContentProvider();
5538 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005539 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 info.name + " from sourceDir " +
5541 info.applicationInfo.sourceDir);
5542 return null;
5543 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005544 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005545 TAG, "Instantiating local provider " + info.name);
5546 // XXX Need to create the correct context for this provider.
5547 localProvider.attachInfo(c, info);
5548 } catch (java.lang.Exception e) {
5549 if (!mInstrumentation.onException(null, e)) {
5550 throw new RuntimeException(
5551 "Unable to get provider " + info.name
5552 + ": " + e.toString(), e);
5553 }
5554 return null;
5555 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005556 } else {
5557 provider = holder.provider;
5558 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 + info.name);
5560 }
5561
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005562 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005563
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005564 synchronized (mProviderMap) {
5565 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5566 + " / " + info.name);
5567 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005569 ComponentName cname = new ComponentName(info.packageName, info.name);
5570 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005571 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005572 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005573 Slog.v(TAG, "installProvider: lost the race, "
5574 + "using existing local provider");
5575 }
5576 provider = pr.mProvider;
5577 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005578 holder = new IActivityManager.ContentProviderHolder(info);
5579 holder.provider = provider;
5580 holder.noReleaseNeeded = true;
5581 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005582 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005583 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005584 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005585 retHolder = pr.mHolder;
5586 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005587 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5588 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005589 if (DEBUG_PROVIDER) {
5590 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005591 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005592 // We need to transfer our new reference to the existing
5593 // ref count, releasing the old one... but only if
5594 // release is needed (that is, it is not running in the
5595 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005596 if (!noReleaseNeeded) {
5597 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005598 try {
5599 ActivityManagerNative.getDefault().removeContentProvider(
5600 holder.connection, stable);
5601 } catch (RemoteException e) {
5602 //do nothing content provider object is dead any way
5603 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005604 }
5605 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005606 ProviderClientRecord client = installProviderAuthoritiesLocked(
5607 provider, localProvider, holder);
5608 if (noReleaseNeeded) {
5609 prc = new ProviderRefCount(holder, client, 1000, 1000);
5610 } else {
5611 prc = stable
5612 ? new ProviderRefCount(holder, client, 1, 0)
5613 : new ProviderRefCount(holder, client, 0, 1);
5614 }
5615 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005616 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005617 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 }
5619 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005620
5621 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 }
5623
Romain Guy65b345f2011-07-27 18:51:50 -07005624 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005625 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005627 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005628 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005629 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005630 public void run() {
5631 ensureJitEnabled();
5632 }
5633 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005634 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5635 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005637 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638 try {
5639 mgr.attachApplication(mAppThread);
5640 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005641 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005643 // Watch for getting close to heap limit.
5644 BinderInternal.addGcWatcher(new Runnable() {
5645 @Override public void run() {
5646 if (!mSomeActivitiesChanged) {
5647 return;
5648 }
5649 Runtime runtime = Runtime.getRuntime();
5650 long dalvikMax = runtime.maxMemory();
5651 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5652 if (dalvikUsed > ((3*dalvikMax)/4)) {
5653 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5654 + " total=" + (runtime.totalMemory()/1024)
5655 + " used=" + (dalvikUsed/1024));
5656 mSomeActivitiesChanged = false;
5657 try {
5658 mgr.releaseSomeActivities(mAppThread);
5659 } catch (RemoteException e) {
5660 }
5661 }
5662 }
5663 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 } else {
5665 // Don't set application object here -- if the system crashes,
5666 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005667 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005668 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 try {
5670 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005671 ContextImpl context = ContextImpl.createAppContext(
5672 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005673 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5674 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005675 } catch (Exception e) {
5676 throw new RuntimeException(
5677 "Unable to instantiate Application():" + e.toString(), e);
5678 }
5679 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005680
5681 // add dropbox logging to libcore
5682 DropBox.setReporter(new DropBoxReporter());
5683
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005684 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005685 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005686 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005687 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005688 // We need to apply this change to the resources
5689 // immediately, because upon returning the view
5690 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005691 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005692 // This actually changed the resources! Tell
5693 // everyone about it.
5694 if (mPendingConfiguration == null ||
5695 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5696 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005697
Jeff Brown9ef09972013-10-15 20:49:59 -07005698 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005699 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005700 }
5701 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005702 }
Craig Mautner88c05892013-06-28 09:47:45 -07005703 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005704 public void onLowMemory() {
5705 }
Craig Mautner88c05892013-06-28 09:47:45 -07005706 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005707 public void onTrimMemory(int level) {
5708 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005709 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 }
5711
Romain Guy5e9120d2012-01-30 12:17:22 -08005712 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005713 // The system process on low-memory devices do not get to use hardware
5714 // accelerated drawing, since this can add too much overhead to the
5715 // process.
5716 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005717 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005718 } else {
John Reck51aaf902015-12-02 15:08:07 -08005719 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 ActivityThread thread = new ActivityThread();
5722 thread.attach(true);
5723 return thread;
5724 }
5725
Jeff Brown10e89712011-07-08 18:52:57 -07005726 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005727 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005728 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 }
5730 }
5731
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005732 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005733 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005734 if (mCoreSettings != null) {
5735 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005736 }
Craig Mautner88c05892013-06-28 09:47:45 -07005737 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005738 }
5739 }
5740
Geremy Condra69689a72012-09-11 16:57:17 -07005741 private static class EventLoggingReporter implements EventLogger.Reporter {
5742 @Override
5743 public void report (int code, Object... list) {
5744 EventLog.writeEvent(code, list);
5745 }
5746 }
5747
Geremy Condrab7faaf42012-09-19 18:07:42 -07005748 private class DropBoxReporter implements DropBox.Reporter {
5749
5750 private DropBoxManager dropBox;
5751
Narayan Kamath7f062242015-04-08 13:24:13 +01005752 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005753
5754 @Override
5755 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005756 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005757 dropBox.addData(tag, data, flags);
5758 }
5759
5760 @Override
5761 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005762 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005763 dropBox.addText(tag, data);
5764 }
Narayan Kamath7f062242015-04-08 13:24:13 +01005765
5766 private synchronized void ensureInitialized() {
5767 if (dropBox == null) {
5768 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5769 }
5770 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005771 }
5772
Romain Guy65b345f2011-07-27 18:51:50 -07005773 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005774 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07005775 SamplingProfilerIntegration.start();
5776
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005777 // CloseGuard defaults to true and can be quite spammy. We
5778 // disable it here, but selectively enable it later (via
5779 // StrictMode) on debug builds, but using DropBox, not logs.
5780 CloseGuard.setEnabled(false);
5781
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005782 Environment.initForCurrentUser();
5783
Geremy Condra69689a72012-09-11 16:57:17 -07005784 // Set the reporter for event logging in libcore
5785 EventLogger.setReporter(new EventLoggingReporter());
5786
Alex Klyubin48125632015-04-29 13:16:30 -07005787 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08005788
Robin Lee3d076af2014-04-25 14:57:49 +01005789 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5790 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5791 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 Process.setArgV0("<pre-initialized>");
5794
5795 Looper.prepareMainLooper();
5796
5797 ActivityThread thread = new ActivityThread();
5798 thread.attach(false);
5799
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005800 if (sMainThreadHandler == null) {
5801 sMainThreadHandler = thread.getHandler();
5802 }
5803
Dianne Hackborn287952c2010-09-22 22:34:31 -07005804 if (false) {
5805 Looper.myLooper().setMessageLogging(new
5806 LogPrinter(Log.DEBUG, "ActivityThread"));
5807 }
5808
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005809 // End of event ActivityThreadMain.
5810 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 Looper.loop();
5812
Jeff Brown10e89712011-07-08 18:52:57 -07005813 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 }
5815}