blob: 177234c28f4185a7f9cc3fc636d5b07082faaaba [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070019import android.app.assist.AssistContent;
20import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070023import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ComponentName;
25import android.content.ContentProvider;
26import android.content.Context;
27import android.content.IContentProvider;
28import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.IPackageManager;
33import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070034import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ProviderInfo;
38import android.content.pm.ServiceInfo;
39import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070040import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.res.Configuration;
42import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070043import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.database.sqlite.SQLiteDatabase;
45import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080046import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.graphics.Bitmap;
48import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070049import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050050import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070051import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050052import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070053import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040054import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040055import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070056import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070057import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070058import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Bundle;
60import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070061import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.Looper;
66import android.os.Message;
67import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080068import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070069import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Process;
72import android.os.RemoteException;
73import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070074import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070076import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070077import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070078import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000080import android.provider.Settings;
Alex Klyubinf9034cc2015-02-12 11:43:09 -080081import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080082import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070084import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.DisplayMetrics;
86import android.util.EventLog;
87import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070088import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070089import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080090import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080091import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -070092import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -070093import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -080095import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.View;
97import android.view.ViewDebug;
98import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070099import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.Window;
101import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700102import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700103import android.renderscript.RenderScriptCacheDir;
Alex Klyubindcdaf872015-05-13 15:57:09 -0700104import android.security.keystore.AndroidKeyStoreProvider;
Calin Juravle69052392015-12-17 17:06:49 +0200105import android.system.Os;
106import android.system.OsConstants;
107import android.system.ErrnoException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700109import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import com.android.internal.os.BinderInternal;
113import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700114import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700115import com.android.internal.os.SomeArgs;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700116import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700117import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100118import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700119import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
121import java.io.File;
122import java.io.FileDescriptor;
123import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700124import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125import java.io.PrintWriter;
126import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700127import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000128import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000130import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import 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;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800178 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179
Dianne Hackborn455625e2015-01-21 09:55:13 -0800180 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
181 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
182 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
183 public static final int SERVICE_DONE_EXECUTING_START = 1;
184 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
185 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
186
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700187 // Details for pausing activity.
188 private static final int USER_LEAVING = 1;
189 private static final int DONT_REPORT = 2;
190
Filip Gruszczynskica664812015-12-04 12:43:36 -0800191 // Whether to invoke an activity callback after delivering new configuration.
192 private static final boolean REPORT_TO_ACTIVITY = true;
193
Jeff Browndefd4a62014-03-10 21:24:37 -0700194 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700195
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700198 final ApplicationThread mAppThread = new ApplicationThread();
199 final Looper mLooper = Looper.myLooper();
200 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700201 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700202 // List of new activities (via ActivityRecord.nextIdle) that should
203 // be reported when next we idle.
204 ActivityClientRecord mNewActivities = null;
205 // Number of activities that are currently visible on-screen.
206 int mNumVisibleActivities = 0;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700207 WeakReference<AssistStructure> mLastAssistStructure;
208 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700210 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700211 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700212 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700213 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700214 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700215 Application mInitialApplication;
216 final ArrayList<Application> mAllApplications
217 = new ArrayList<Application>();
218 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700219 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800220 /** Reference to singleton {@link ActivityThread} */
221 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700222 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700223 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700224 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700225 String[] mInstrumentationSplitAppDirs = null;
226 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700228 String[] mInstrumentedSplitAppDirs = null;
229 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700230 boolean mSystemThread = false;
231 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700232 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700234 // These can be accessed by multiple threads; mPackages is the lock.
235 // XXX For now we keep around information about all packages we have
236 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800237 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700238 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800239 // which means this lock gets held while the activity and window managers
240 // holds their own lock. Thus you MUST NEVER call back into the activity manager
241 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700242 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700243 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
244 = new ArrayMap<String, WeakReference<LoadedApk>>();
245 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
246 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 final ArrayList<ActivityClientRecord> mRelaunchingActivities
248 = new ArrayList<ActivityClientRecord>();
249 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700250 // Because we merge activity relaunch operations we can't depend on the ordering provided by
251 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
252 // us to drop certain events, if we know that they happened before relaunch we already executed.
253 // This represents the order of receiving the request from AM.
254 @GuardedBy("mResourcesManager")
255 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
Craig Mautner88c05892013-06-28 09:47:45 -0700257 private final ResourcesManager mResourcesManager;
258
Jeff Sharkey6d515712012-09-20 16:06:08 -0700259 private static final class ProviderKey {
260 final String authority;
261 final int userId;
262
263 public ProviderKey(String authority, int userId) {
264 this.authority = authority;
265 this.userId = userId;
266 }
267
268 @Override
269 public boolean equals(Object o) {
270 if (o instanceof ProviderKey) {
271 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800272 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700273 }
274 return false;
275 }
276
277 @Override
278 public int hashCode() {
279 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
280 }
281 }
282
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700283 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700284 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
285 = new ArrayMap<ProviderKey, ProviderClientRecord>();
286 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
287 = new ArrayMap<IBinder, ProviderRefCount>();
288 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
289 = new ArrayMap<IBinder, ProviderClientRecord>();
290 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
291 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700293 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700294 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600295
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700296 final GcIdler mGcIdler = new GcIdler();
297 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700299 static Handler sMainThreadHandler; // set once in main()
300
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800301 Bundle mCoreSettings = null;
302
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400303 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700305 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800307 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700308 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700310 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 Activity activity;
312 Window window;
313 Activity parent;
314 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700315 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 boolean paused;
317 boolean stopped;
318 boolean hideForNow;
319 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700320 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800321 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800322 // Used for consolidating configs before sending on to Activity.
323 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700324 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
Jeff Hao1b012d32014-08-20 10:35:34 -0700326 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400329 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700330 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
332 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800333 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
335 boolean startsNotResumed;
336 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800337 int pendingConfigChanges;
338 boolean onlyLocalRequest;
339
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700340 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800341 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700342 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700344 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
345 // can be compared with other lifecycle operations.
346 int relaunchSeq = 0;
347
348 // Can only be accessed from the UI thread. This represents the latest processed message
349 // that is related to lifecycle events/
350 int lastProcessedSeq = 0;
351
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700352 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 parent = null;
354 embeddedID = null;
355 paused = false;
356 stopped = false;
357 hideForNow = false;
358 nextIdle = null;
359 }
360
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800361 public boolean isPreHoneycomb() {
362 if (activity != null) {
363 return activity.getApplicationInfo().targetSdkVersion
364 < android.os.Build.VERSION_CODES.HONEYCOMB;
365 }
366 return false;
367 }
368
Craig Mautnera0026042014-04-23 11:45:37 -0700369 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700370 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700371 }
372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700374 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 return "ActivityRecord{"
376 + Integer.toHexString(System.identityHashCode(this))
377 + " token=" + token + " " + (componentName == null
378 ? "no component name" : componentName.toShortString())
379 + "}";
380 }
381 }
382
Wale Ogunwale1d646122015-04-24 14:45:14 -0700383 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700384 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 final IContentProvider mProvider;
386 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700387 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700389 ProviderClientRecord(String[] names, IContentProvider provider,
390 ContentProvider localProvider,
391 IActivityManager.ContentProviderHolder holder) {
392 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 mProvider = provider;
394 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700395 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 }
397 }
398
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400399 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800400 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 IBinder token;
402 public String toString() {
403 return "NewIntentData{intents=" + intents + " token=" + token + "}";
404 }
405 }
406
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400407 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700408 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700409 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
410 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800411 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700412 this.intent = intent;
413 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 Intent intent;
416 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400417 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 public String toString() {
419 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700420 info.packageName + " resultCode=" + getResultCode()
421 + " resultData=" + getResultData() + " resultExtras="
422 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 }
424 }
425
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400426 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700427 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400428 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700429 int backupMode;
430 public String toString() {
431 return "CreateBackupAgentData{appInfo=" + appInfo
432 + " backupAgent=" + appInfo.backupAgentName
433 + " mode=" + backupMode + "}";
434 }
435 }
Bob Leee5408332009-09-04 18:31:17 -0700436
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400437 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 IBinder token;
439 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400440 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 Intent intent;
442 public String toString() {
443 return "CreateServiceData{token=" + token + " className="
444 + info.name + " packageName=" + info.packageName
445 + " intent=" + intent + "}";
446 }
447 }
448
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400449 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 IBinder token;
451 Intent intent;
452 boolean rebind;
453 public String toString() {
454 return "BindServiceData{token=" + token + " intent=" + intent + "}";
455 }
456 }
457
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400458 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700460 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700462 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 Intent args;
464 public String toString() {
465 return "ServiceArgsData{token=" + token + " startId=" + startId
466 + " args=" + args + "}";
467 }
468 }
469
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400470 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700471 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 String processName;
473 ApplicationInfo appInfo;
474 List<ProviderInfo> providers;
475 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 Bundle instrumentationArgs;
477 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800478 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400480 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700481 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700482 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700483 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400485 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700486
487 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700488 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public String toString() {
491 return "AppBindData{appInfo=" + appInfo + "}";
492 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700493 }
494
495 static final class Profiler {
496 String profileFile;
497 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700498 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700499 boolean autoStopProfiler;
500 boolean profiling;
501 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700502 public void setProfiler(ProfilerInfo profilerInfo) {
503 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700504 if (profiling) {
505 if (fd != null) {
506 try {
507 fd.close();
508 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700509 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700510 }
511 }
512 return;
513 }
514 if (profileFd != null) {
515 try {
516 profileFd.close();
517 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700518 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700519 }
520 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700521 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700522 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700523 samplingInterval = profilerInfo.samplingInterval;
524 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700525 }
526 public void startProfiling() {
527 if (profileFd == null || profiling) {
528 return;
529 }
530 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700531 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
532 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700533 profiling = true;
534 } catch (RuntimeException e) {
535 Slog.w(TAG, "Profiling failed on path " + profileFile);
536 try {
537 profileFd.close();
538 profileFd = null;
539 } catch (IOException e2) {
540 Slog.w(TAG, "Failure closing profile fd", e2);
541 }
542 }
543 }
544 public void stopProfiling() {
545 if (profiling) {
546 profiling = false;
547 Debug.stopMethodTracing();
548 if (profileFd != null) {
549 try {
550 profileFd.close();
551 } catch (IOException e) {
552 }
553 }
554 profileFd = null;
555 profileFile = null;
556 }
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400560 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700561 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700562 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800563 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400567 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 IBinder token;
569 List<ResultInfo> results;
570 public String toString() {
571 return "ResultData{token=" + token + " results" + results + "}";
572 }
573 }
574
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400575 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800576 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 String what;
578 String who;
579 }
580
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400581 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700582 String path;
583 ParcelFileDescriptor fd;
584 }
585
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400586 static final class UpdateCompatibilityData {
587 String pkg;
588 CompatibilityInfo info;
589 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800590
Adam Skorydfc7fd72013-08-05 19:23:41 -0700591 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800592 IBinder activityToken;
593 IBinder requestToken;
594 int requestType;
595 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700596
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800597 static final class ActivityConfigChangeData {
598 final IBinder activityToken;
599 final Configuration overrideConfig;
600 public ActivityConfigChangeData(IBinder token, Configuration config) {
601 activityToken = token;
602 overrideConfig = config;
603 }
604 }
605
Romain Guy65b345f2011-07-27 18:51:50 -0700606 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700607
Romain Guy65b345f2011-07-27 18:51:50 -0700608 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700609 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700610
Dianne Hackborna413dc02013-07-12 12:02:55 -0700611 private int mLastProcessState = -1;
612
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700613 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700614 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700615 if (mPendingConfiguration == null ||
616 mPendingConfiguration.isOtherSeqNewer(config)) {
617 mPendingConfiguration = config;
618 }
619 }
620 }
621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700623 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700624 int seq = getLifecycleSeq();
625 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
626 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700627 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
629 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700630 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
631 configChanges,
632 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 }
634
635 public final void scheduleStopActivity(IBinder token, boolean showWindow,
636 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700637 int seq = getLifecycleSeq();
638 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
639 + " operation received seq: " + seq);
640 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700642 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 }
644
645 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700646 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
648 token);
649 }
650
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800651 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700652 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800653 }
654
Dianne Hackborna413dc02013-07-12 12:02:55 -0700655 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800656 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700657 int seq = getLifecycleSeq();
658 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
659 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700660 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700661 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
663
664 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
665 ResultData res = new ResultData();
666 res.token = token;
667 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700668 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670
671 // we use token to identify this activity without having to send the
672 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800673 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700674 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800675 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
676 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
677 int procState, Bundle state, PersistableBundle persistentState,
678 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
679 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700680
681 updateProcessState(procState, false);
682
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700683 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684
685 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700686 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800688 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700689 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400691 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700693 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694
695 r.pendingResults = pendingResults;
696 r.pendingIntents = pendingNewIntents;
697
698 r.startsNotResumed = notResumed;
699 r.isForward = isForward;
700
Jeff Hao1b012d32014-08-20 10:35:34 -0700701 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700702
Wale Ogunwale60454db2015-01-23 16:05:07 -0800703 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700704 updatePendingConfiguration(curConfig);
705
Jeff Brown9ef09972013-10-15 20:49:59 -0700706 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
708
Wale Ogunwale60454db2015-01-23 16:05:07 -0800709 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800711 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800712 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700713 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800714 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700715 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800718 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 NewIntentData data = new NewIntentData();
720 data.intents = intents;
721 data.token = token;
722
Jeff Brown9ef09972013-10-15 20:49:59 -0700723 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 }
725
726 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
727 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700728 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 configChanges);
730 }
731
732 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400733 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700734 boolean sync, int sendingUser, int processState) {
735 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700736 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700737 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400739 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700740 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400743 public final void scheduleCreateBackupAgent(ApplicationInfo app,
744 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700745 CreateBackupAgentData d = new CreateBackupAgentData();
746 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400747 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700748 d.backupMode = backupMode;
749
Jeff Brown9ef09972013-10-15 20:49:59 -0700750 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700751 }
752
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400753 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
754 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700755 CreateBackupAgentData d = new CreateBackupAgentData();
756 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400757 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700758
Jeff Brown9ef09972013-10-15 20:49:59 -0700759 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700760 }
761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700763 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
764 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765 CreateServiceData s = new CreateServiceData();
766 s.token = token;
767 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400768 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769
Jeff Brown9ef09972013-10-15 20:49:59 -0700770 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
773 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700774 boolean rebind, int processState) {
775 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 BindServiceData s = new BindServiceData();
777 s.token = token;
778 s.intent = intent;
779 s.rebind = rebind;
780
Amith Yamasani742a6712011-05-04 14:49:28 -0700781 if (DEBUG_SERVICE)
782 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
783 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700784 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
787 public final void scheduleUnbindService(IBinder token, Intent intent) {
788 BindServiceData s = new BindServiceData();
789 s.token = token;
790 s.intent = intent;
791
Jeff Brown9ef09972013-10-15 20:49:59 -0700792 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700795 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700796 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 ServiceArgsData s = new ServiceArgsData();
798 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700799 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700801 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 s.args = args;
803
Jeff Brown9ef09972013-10-15 20:49:59 -0700804 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806
807 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700808 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 }
810
Jeff Hao1b012d32014-08-20 10:35:34 -0700811 public final void bindApplication(String processName, ApplicationInfo appInfo,
812 List<ProviderInfo> providers, ComponentName instrumentationName,
813 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
814 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800815 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700816 boolean enableBinderTracking, boolean trackAllocation,
817 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
818 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819
820 if (services != null) {
821 // Setup the service cache in the ServiceManager
822 ServiceManager.initServiceCache(services);
823 }
824
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800825 setCoreSettings(coreSettings);
826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 AppBindData data = new AppBindData();
828 data.processName = processName;
829 data.appInfo = appInfo;
830 data.providers = providers;
831 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 data.instrumentationArgs = instrumentationArgs;
833 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800834 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400836 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700837 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700838 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700839 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400841 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700842 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700843 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 }
845
846 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700847 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 }
849
Christopher Tate5e1ab332009-09-01 20:32:49 -0700850 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700851 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700852 }
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700855 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700856 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 }
858
859 public void updateTimeZone() {
860 TimeZone.setDefault(null);
861 }
862
Robert Greenwalt03595d02010-11-02 14:08:23 -0700863 public void clearDnsCache() {
864 // a non-standard API to get this to libcore
865 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400866 // Allow libcore to perform the necessary actions as it sees fit upon a network
867 // configuration change.
868 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700869 }
870
Jason Monk83520b92014-05-09 15:16:06 -0400871 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400872 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
873 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500874 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400875 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500876 } else {
877 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
878 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700879 }
880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 public void processInBackground() {
882 mH.removeMessages(H.GC_WHEN_IDLE);
883 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
884 }
885
886 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700887 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700888 try {
889 data.fd = ParcelFileDescriptor.dup(fd);
890 data.token = servicetoken;
891 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700892 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700893 } catch (IOException e) {
894 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 }
896 }
897
898 // This function exists to make sure all receiver dispatching is
899 // correctly ordered, since these are one-way calls and the binder driver
900 // applies transaction ordering per object for such calls.
901 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700902 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700903 boolean sticky, int sendingUser, int processState) throws RemoteException {
904 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700905 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
906 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
Bob Leee5408332009-09-04 18:31:17 -0700908
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800909 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700911 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913
Wale Ogunwale60454db2015-01-23 16:05:07 -0800914 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800915 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800916 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800917 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800918 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800921 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700922 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
923 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800924 }
925
Andy McFadden824c5102010-07-09 16:26:57 -0700926 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
927 DumpHeapData dhd = new DumpHeapData();
928 dhd.path = path;
929 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700930 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700931 }
932
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700933 public void setSchedulingGroup(int group) {
934 // Note: do this immediately, since going into the foreground
935 // should happen regardless of what pending work we have to do
936 // and the activity manager will wait for us to report back that
937 // we are done before sending us to the background.
938 try {
939 Process.setProcessGroup(Process.myPid(), group);
940 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800941 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700942 }
943 }
Bob Leee5408332009-09-04 18:31:17 -0700944
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700945 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700946 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700947 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700948
949 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700950 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700951 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700952
Dianne Hackborn30d71892010-12-11 10:37:55 -0800953 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
954 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700955 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700956 try {
957 data.fd = ParcelFileDescriptor.dup(fd);
958 data.token = activitytoken;
959 data.prefix = prefix;
960 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700961 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700962 } catch (IOException e) {
963 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700964 }
965 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700966
Marco Nelissen18cb2872011-11-15 11:19:53 -0800967 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
968 String[] args) {
969 DumpComponentInfo data = new DumpComponentInfo();
970 try {
971 data.fd = ParcelFileDescriptor.dup(fd);
972 data.token = providertoken;
973 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700974 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800975 } catch (IOException e) {
976 Slog.w(TAG, "dumpProvider failed", e);
977 }
978 }
979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700981 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -0800982 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
983 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700984 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700985 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700986 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -0800987 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700988 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700989 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700990 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700991 }
992
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700993 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -0800994 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 long nativeMax = Debug.getNativeHeapSize() / 1024;
996 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
997 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001000 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 long dalvikMax = runtime.totalMemory() / 1024;
1002 long dalvikFree = runtime.freeMemory() / 1024;
1003 long dalvikAllocated = dalvikMax - dalvikFree;
1004 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001005 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001006 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1007 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 int globalAssetCount = AssetManager.getGlobalAssetCount();
1009 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1010 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1011 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1012 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001013 long parcelSize = Parcel.getGlobalAllocSize();
1014 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001015 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001016 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001017
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001018 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1019 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001020 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1021 nativeMax, nativeAllocated, nativeFree,
1022 dalvikMax, dalvikAllocated, dalvikFree);
1023
Dianne Hackbornb437e092011-08-05 17:50:29 -07001024 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 // NOTE: if you change anything significant below, also consider changing
1026 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 // Object counts
1029 pw.print(viewInstanceCount); pw.print(',');
1030 pw.print(viewRootInstanceCount); pw.print(',');
1031 pw.print(appContextInstanceCount); pw.print(',');
1032 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 pw.print(globalAssetCount); pw.print(',');
1035 pw.print(globalAssetManagerCount); pw.print(',');
1036 pw.print(binderLocalObjectCount); pw.print(',');
1037 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 pw.print(binderDeathObjectCount); pw.print(',');
1040 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001043 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001044 pw.print(stats.memoryUsed / 1024); pw.print(',');
1045 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001046 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001047 for (int i = 0; i < stats.dbStats.size(); i++) {
1048 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001049 pw.print(','); pw.print(dbStats.dbName);
1050 pw.print(','); pw.print(dbStats.pageSize);
1051 pw.print(','); pw.print(dbStats.dbSize);
1052 pw.print(','); pw.print(dbStats.lookaside);
1053 pw.print(','); pw.print(dbStats.cache);
1054 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001055 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001056 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001057
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001058 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 }
Bob Leee5408332009-09-04 18:31:17 -07001060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 pw.println(" ");
1062 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001063 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 viewRootInstanceCount);
1065
1066 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1067 "Activities:", activityInstanceCount);
1068
1069 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1070 "AssetManagers:", globalAssetManagerCount);
1071
1072 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1073 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001074 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1075 "Parcel count:", parcelCount);
1076 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1077 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 // SQLite mem info
1080 pw.println(" ");
1081 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001082 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1083 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1084 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001085 pw.println(" ");
1086 int N = stats.dbStats.size();
1087 if (N > 0) {
1088 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001089 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1090 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001091 for (int i = 0; i < N; i++) {
1092 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001093 printRow(pw, DB_INFO_FORMAT,
1094 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1095 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1096 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1097 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001098 }
1099 }
Bob Leee5408332009-09-04 18:31:17 -07001100
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001101 // Asset details.
1102 String assetAlloc = AssetManager.getAssetAllocations();
1103 if (assetAlloc != null) {
1104 pw.println(" ");
1105 pw.println(" Asset Allocations");
1106 pw.print(assetAlloc);
1107 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001108
1109 // Unreachable native memory
1110 if (dumpUnreachable) {
1111 boolean showContents = ((mBoundApplication != null)
1112 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1113 || android.os.Build.IS_DEBUGGABLE;
1114 pw.println(" ");
1115 pw.println(" Unreachable memory");
1116 pw.print(Debug.getUnreachableMemory(100, showContents));
1117 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001118 }
1119
1120 @Override
1121 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1122 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001123 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 }
1125
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001126 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001127 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001128 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1129 SQLiteDebug.dump(printer, args);
1130 pw.flush();
1131 }
1132
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001133 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001134 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1135 if (mSystemThread) {
1136 // Ensure this invocation is asynchronous to prevent
1137 // writer waiting due to buffer cannot be consumed.
1138 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1139 @Override
1140 public void run() {
1141 dumpDatabaseInfo(fd, args);
1142 }
1143 });
1144 } else {
1145 dumpDatabaseInfo(fd, args);
1146 }
1147 }
1148
1149 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001150 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001151 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001152 }
1153
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001154 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001155 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001156 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001157 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001158 cmd.activityToken = activityToken;
1159 cmd.requestToken = requestToken;
1160 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001161 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001162 }
1163
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001164 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001165 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001166 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001167
1168 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1169 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1170 ucd.pkg = pkg;
1171 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001172 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001173 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001174
1175 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001176 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001177 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001178
Craig Mautner5eda9b32013-07-02 11:58:16 -07001179 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001180 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001181 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001182
Craig Mautnereb8abf72014-07-02 15:04:09 -07001183 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1184 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1185 new Pair<IBinder, ActivityOptions>(token, options));
1186 }
1187
Dianne Hackborna413dc02013-07-12 12:02:55 -07001188 public void setProcessState(int state) {
1189 updateProcessState(state, true);
1190 }
1191
1192 public void updateProcessState(int processState, boolean fromIpc) {
1193 synchronized (this) {
1194 if (mLastProcessState != processState) {
1195 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001196 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1197 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1198 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1199 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1200 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1201 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1202 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1203 }
1204 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001205 if (false) {
1206 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1207 + (fromIpc ? " (from ipc": ""));
1208 }
1209 }
1210 }
1211 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001212
1213 @Override
1214 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001215 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001216 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001217
1218 @Override
1219 public final void updateTimePrefs(boolean is24Hour) {
1220 DateFormat.set24HourTimePref(is24Hour);
1221 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001222
1223 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001224 public void scheduleCancelVisibleBehind(IBinder token) {
1225 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001226 }
1227
1228 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001229 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1230 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001231 }
Craig Mautner8746a472014-07-24 15:12:54 -07001232
Jeff Sharkey605eb792014-11-04 13:34:06 -08001233 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001234 public void scheduleEnterAnimationComplete(IBinder token) {
1235 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1236 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001237
1238 @Override
1239 public void notifyCleartextNetwork(byte[] firstPacket) {
1240 if (StrictMode.vmCleartextNetworkEnabled()) {
1241 StrictMode.onCleartextNetworkDetected(firstPacket);
1242 }
1243 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001244
1245 @Override
1246 public void startBinderTracking() {
1247 sendMessage(H.START_BINDER_TRACKING, null);
1248 }
1249
1250 @Override
1251 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1252 try {
1253 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1254 } catch (IOException e) {
1255 }
1256 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001257
1258 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001259 public void scheduleMultiWindowChanged(IBinder token, boolean inMultiWindow)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001260 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001261 sendMessage(H.MULTI_WINDOW_CHANGED, token, inMultiWindow ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001262 }
1263
1264 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001265 public void schedulePictureInPictureChanged(IBinder token, boolean inPip)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001266 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001267 sendMessage(H.PICTURE_IN_PICTURE_CHANGED, token, inPip ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001268 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001269
1270 @Override
1271 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1272 IVoiceInteractor voiceInteractor) throws RemoteException {
1273 SomeArgs args = SomeArgs.obtain();
1274 args.arg1 = token;
1275 args.arg2 = voiceInteractor;
1276 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1277 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 }
1279
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001280 private int getLifecycleSeq() {
1281 synchronized (mResourcesManager) {
1282 return mLifecycleSeq++;
1283 }
1284 }
1285
Romain Guy65b345f2011-07-27 18:51:50 -07001286 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 public static final int LAUNCH_ACTIVITY = 100;
1288 public static final int PAUSE_ACTIVITY = 101;
1289 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1290 public static final int STOP_ACTIVITY_SHOW = 103;
1291 public static final int STOP_ACTIVITY_HIDE = 104;
1292 public static final int SHOW_WINDOW = 105;
1293 public static final int HIDE_WINDOW = 106;
1294 public static final int RESUME_ACTIVITY = 107;
1295 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001296 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 public static final int BIND_APPLICATION = 110;
1298 public static final int EXIT_APPLICATION = 111;
1299 public static final int NEW_INTENT = 112;
1300 public static final int RECEIVER = 113;
1301 public static final int CREATE_SERVICE = 114;
1302 public static final int SERVICE_ARGS = 115;
1303 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 public static final int CONFIGURATION_CHANGED = 118;
1306 public static final int CLEAN_UP_CONTEXT = 119;
1307 public static final int GC_WHEN_IDLE = 120;
1308 public static final int BIND_SERVICE = 121;
1309 public static final int UNBIND_SERVICE = 122;
1310 public static final int DUMP_SERVICE = 123;
1311 public static final int LOW_MEMORY = 124;
1312 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1313 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001314 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001315 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001316 public static final int DESTROY_BACKUP_AGENT = 129;
1317 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001318 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001319 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001320 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001321 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001322 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001323 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001324 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001325 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001326 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001327 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001328 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001329 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001330 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001331 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001332 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001333 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001334 public static final int CANCEL_VISIBLE_BEHIND = 147;
1335 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001336 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001337 public static final int START_BINDER_TRACKING = 150;
1338 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001339 public static final int MULTI_WINDOW_CHANGED = 152;
1340 public static final int PICTURE_IN_PICTURE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001341 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001344 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 switch (code) {
1346 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1347 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1348 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1349 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1350 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1351 case SHOW_WINDOW: return "SHOW_WINDOW";
1352 case HIDE_WINDOW: return "HIDE_WINDOW";
1353 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1354 case SEND_RESULT: return "SEND_RESULT";
1355 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1356 case BIND_APPLICATION: return "BIND_APPLICATION";
1357 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1358 case NEW_INTENT: return "NEW_INTENT";
1359 case RECEIVER: return "RECEIVER";
1360 case CREATE_SERVICE: return "CREATE_SERVICE";
1361 case SERVICE_ARGS: return "SERVICE_ARGS";
1362 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1364 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1365 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1366 case BIND_SERVICE: return "BIND_SERVICE";
1367 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1368 case DUMP_SERVICE: return "DUMP_SERVICE";
1369 case LOW_MEMORY: return "LOW_MEMORY";
1370 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1371 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001372 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001373 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1374 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001375 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001376 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001377 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001378 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001379 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001380 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001381 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001382 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001383 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001384 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001385 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001386 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001387 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001388 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001389 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001390 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001391 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001392 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1393 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001394 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001395 case MULTI_WINDOW_CHANGED: return "MULTI_WINDOW_CHANGED";
1396 case PICTURE_IN_PICTURE_CHANGED: return "PICTURE_IN_PICTURE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001397 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001400 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 }
1402 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001403 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 switch (msg.what) {
1405 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001406 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001407 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408
1409 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001410 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001411 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001412 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 } break;
1414 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001415 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001416 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001417 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001418 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001420 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001421 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001422 SomeArgs args = (SomeArgs) msg.obj;
1423 handlePauseActivity((IBinder) args.arg1, false,
1424 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1425 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001426 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001427 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001428 } break;
1429 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001431 SomeArgs args = (SomeArgs) msg.obj;
1432 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1433 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001434 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001435 } break;
1436 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001437 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001438 SomeArgs args = (SomeArgs) msg.obj;
1439 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001440 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001441 } break;
1442 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001444 SomeArgs args = (SomeArgs) msg.obj;
1445 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001446 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001447 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001449 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 handleWindowVisibility((IBinder)msg.obj, true);
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 HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001454 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001456 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 break;
1458 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001459 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001460 SomeArgs args = (SomeArgs) msg.obj;
1461 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001462 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001463 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 break;
1465 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001466 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 handleSendResult((ResultData)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 DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001471 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1473 msg.arg2, false);
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 BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001477 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 AppBindData data = (AppBindData)msg.obj;
1479 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001480 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 break;
1482 case EXIT_APPLICATION:
1483 if (mInitialApplication != null) {
1484 mInitialApplication.onTerminate();
1485 }
1486 Looper.myLooper().quit();
1487 break;
1488 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001489 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001491 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 break;
1493 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001494 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001496 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001497 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 break;
1499 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001500 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001502 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 break;
1504 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001505 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001507 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 break;
1509 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001510 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001512 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 break;
1514 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001515 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001517 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 break;
1519 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001520 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001522 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001523 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001526 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001527 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001528 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001529 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 break;
1531 case CLEAN_UP_CONTEXT:
1532 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1533 cci.context.performFinalCleanup(cci.who, cci.what);
1534 break;
1535 case GC_WHEN_IDLE:
1536 scheduleGcIdler();
1537 break;
1538 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001539 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 break;
1541 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001542 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001544 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 break;
1546 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001547 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001548 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1549 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001550 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001552 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001553 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001554 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001555 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001556 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001557 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001558 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001559 break;
1560 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001561 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001562 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001563 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001564 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001565 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001566 Process.killProcess(Process.myPid());
1567 break;
1568 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001569 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001570 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001571 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001572 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001573 case ENABLE_JIT:
1574 ensureJitEnabled();
1575 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001576 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001577 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001578 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001579 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001580 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001581 case SCHEDULE_CRASH:
1582 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001583 case DUMP_HEAP:
1584 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1585 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001586 case DUMP_ACTIVITY:
1587 handleDumpActivity((DumpComponentInfo)msg.obj);
1588 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001589 case DUMP_PROVIDER:
1590 handleDumpProvider((DumpComponentInfo)msg.obj);
1591 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001592 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001593 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001594 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001595 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001596 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001597 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001598 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001599 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001600 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001601 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001602 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1603 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001604 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001605 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001606 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001607 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001608 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001609 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001610 case UNSTABLE_PROVIDER_DIED:
1611 handleUnstableProviderDied((IBinder)msg.obj, false);
1612 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001613 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1614 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001615 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001616 case TRANSLUCENT_CONVERSION_COMPLETE:
1617 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1618 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001619 case INSTALL_PROVIDER:
1620 handleInstallProvider((ProviderInfo) msg.obj);
1621 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001622 case ON_NEW_ACTIVITY_OPTIONS:
1623 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1624 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001625 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001626 case CANCEL_VISIBLE_BEHIND:
1627 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001628 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001629 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1630 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001631 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001632 case ENTER_ANIMATION_COMPLETE:
1633 handleEnterAnimationComplete((IBinder) msg.obj);
1634 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001635 case START_BINDER_TRACKING:
1636 handleStartBinderTracking();
1637 break;
1638 case STOP_BINDER_TRACKING_AND_DUMP:
1639 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1640 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001641 case MULTI_WINDOW_CHANGED:
1642 handleMultiWindowChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001643 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001644 case PICTURE_IN_PICTURE_CHANGED:
1645 handlePictureInPictureChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001646 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001647 case LOCAL_VOICE_INTERACTION_STARTED:
1648 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1649 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1650 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001652 Object obj = msg.obj;
1653 if (obj instanceof SomeArgs) {
1654 ((SomeArgs) obj).recycle();
1655 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001656 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 }
Bob Leee5408332009-09-04 18:31:17 -07001658
Brian Carlstromed7e0072011-03-24 13:27:57 -07001659 private void maybeSnapshot() {
1660 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001661 // convert the *private* ActivityThread.PackageInfo to *public* known
1662 // android.content.pm.PackageInfo
1663 String packageName = mBoundApplication.info.mPackageName;
1664 android.content.pm.PackageInfo packageInfo = null;
1665 try {
1666 Context context = getSystemContext();
1667 if(context == null) {
1668 Log.e(TAG, "cannot get a valid context");
1669 return;
1670 }
1671 PackageManager pm = context.getPackageManager();
1672 if(pm == null) {
1673 Log.e(TAG, "cannot get a valid PackageManager");
1674 return;
1675 }
1676 packageInfo = pm.getPackageInfo(
1677 packageName, PackageManager.GET_ACTIVITIES);
1678 } catch (NameNotFoundException e) {
1679 Log.e(TAG, "cannot get package info for " + packageName, e);
1680 }
1681 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001682 }
1683 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
1685
Romain Guy65b345f2011-07-27 18:51:50 -07001686 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001687 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001689 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001690 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001691 if (mBoundApplication != null && mProfiler.profileFd != null
1692 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001693 stopProfiling = true;
1694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695 if (a != null) {
1696 mNewActivities = null;
1697 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001698 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001700 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 TAG, "Reporting idle of " + a +
1702 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001703 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 if (a.activity != null && !a.activity.mFinished) {
1705 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001706 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001707 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001708 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001709 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 }
1711 }
1712 prev = a;
1713 a = a.nextIdle;
1714 prev.nextIdle = null;
1715 } while (a != null);
1716 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001717 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001718 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001719 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001720 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 return false;
1722 }
1723 }
1724
1725 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001726 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 public final boolean queueIdle() {
1728 doGcIfNeeded();
1729 return false;
1730 }
1731 }
1732
Romain Guy65b345f2011-07-27 18:51:50 -07001733 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001734 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001735 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001737 public static boolean isSystem() {
1738 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1739 }
1740
Svetoslavfbf0eca2015-05-01 16:52:41 -07001741 public static String currentOpPackageName() {
1742 ActivityThread am = currentActivityThread();
1743 return (am != null && am.getApplication() != null)
1744 ? am.getApplication().getOpPackageName() : null;
1745 }
1746
Romain Guy65b345f2011-07-27 18:51:50 -07001747 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001748 ActivityThread am = currentActivityThread();
1749 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001750 ? am.mBoundApplication.appInfo.packageName : null;
1751 }
1752
1753 public static String currentProcessName() {
1754 ActivityThread am = currentActivityThread();
1755 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001756 ? am.mBoundApplication.processName : null;
1757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758
Romain Guy65b345f2011-07-27 18:51:50 -07001759 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001760 ActivityThread am = currentActivityThread();
1761 return am != null ? am.mInitialApplication : null;
1762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001764 public static IPackageManager getPackageManager() {
1765 if (sPackageManager != null) {
1766 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1767 return sPackageManager;
1768 }
1769 IBinder b = ServiceManager.getService("package");
1770 //Slog.v("PackageManager", "default service binder = " + b);
1771 sPackageManager = IPackageManager.Stub.asInterface(b);
1772 //Slog.v("PackageManager", "default service = " + sPackageManager);
1773 return sPackageManager;
1774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775
Romain Guy65b345f2011-07-27 18:51:50 -07001776 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001777 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1778 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001779 if (config == null) {
1780 return null;
1781 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001782 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001783 mMainThreadConfig.setTo(config);
1784 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001785 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001786 }
1787 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001790 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001791 * Creates the top level resources for the given package.
1792 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001793 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1794 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001795 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001796 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
Alan Viverette02fc5fe2015-08-27 13:16:09 -04001797 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(),
1798 pkgInfo.getClassLoader());
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001799 }
1800
1801 final Handler getHandler() {
1802 return mH;
1803 }
1804
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001805 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1806 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001807 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1808 }
1809
1810 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1811 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001812 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001813 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001814 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001815 if (differentUser) {
1816 // Caching not supported across users
1817 ref = null;
1818 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 ref = mPackages.get(packageName);
1820 } else {
1821 ref = mResourcePackages.get(packageName);
1822 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001823
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001824 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001825 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001826 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1827 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 if (packageInfo != null && (packageInfo.mResources == null
1829 || packageInfo.mResources.getAssets().isUpToDate())) {
1830 if (packageInfo.isSecurityViolation()
1831 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1832 throw new SecurityException(
1833 "Requesting code from " + packageName
1834 + " to be run in process "
1835 + mBoundApplication.processName
1836 + "/" + mBoundApplication.appInfo.uid);
1837 }
1838 return packageInfo;
1839 }
1840 }
1841
1842 ApplicationInfo ai = null;
1843 try {
1844 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001845 PackageManager.GET_SHARED_LIBRARY_FILES
1846 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1847 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001849 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 }
1851
1852 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001853 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 }
1855
1856 return null;
1857 }
1858
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001859 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1860 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1862 boolean securityViolation = includeCode && ai.uid != 0
1863 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001864 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001865 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001866 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1868 |Context.CONTEXT_IGNORE_SECURITY))
1869 == Context.CONTEXT_INCLUDE_CODE) {
1870 if (securityViolation) {
1871 String msg = "Requesting code from " + ai.packageName
1872 + " (with uid " + ai.uid + ")";
1873 if (mBoundApplication != null) {
1874 msg = msg + " to be run in process "
1875 + mBoundApplication.processName + " (with uid "
1876 + mBoundApplication.appInfo.uid + ")";
1877 }
1878 throw new SecurityException(msg);
1879 }
1880 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001881 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1882 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001885 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1886 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001887 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
1889
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001890 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001891 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001892 WeakReference<LoadedApk> ref;
1893 if (includeCode) {
1894 ref = mPackages.get(packageName);
1895 } else {
1896 ref = mResourcePackages.get(packageName);
1897 }
1898 return ref != null ? ref.get() : null;
1899 }
1900 }
1901
Romain Guy65b345f2011-07-27 18:51:50 -07001902 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001903 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1904 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001905 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001906 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001907 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001908 if (differentUser) {
1909 // Caching not supported across users
1910 ref = null;
1911 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 ref = mPackages.get(aInfo.packageName);
1913 } else {
1914 ref = mResourcePackages.get(aInfo.packageName);
1915 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001916
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001917 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 if (packageInfo == null || (packageInfo.mResources != null
1919 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001920 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 : "Loading resource-only package ") + aInfo.packageName
1922 + " (in " + (mBoundApplication != null
1923 ? mBoundApplication.processName : null)
1924 + ")");
1925 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001926 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001928 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001929
1930 if (mSystemThread && "android".equals(aInfo.packageName)) {
1931 packageInfo.installSystemApplicationInfo(aInfo,
1932 getSystemContext().mPackageInfo.getClassLoader());
1933 }
1934
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001935 if (differentUser) {
1936 // Caching not supported across users
1937 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001939 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 } else {
1941 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001942 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 }
1944 }
1945 return packageInfo;
1946 }
1947 }
1948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001950 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952
1953 public ApplicationThread getApplicationThread()
1954 {
1955 return mAppThread;
1956 }
1957
1958 public Instrumentation getInstrumentation()
1959 {
1960 return mInstrumentation;
1961 }
1962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001964 return mProfiler != null && mProfiler.profileFile != null
1965 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
1968 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001969 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971
1972 public Looper getLooper() {
1973 return mLooper;
1974 }
1975
1976 public Application getApplication() {
1977 return mInitialApplication;
1978 }
Bob Leee5408332009-09-04 18:31:17 -07001979
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001980 public String getProcessName() {
1981 return mBoundApplication.processName;
1982 }
Bob Leee5408332009-09-04 18:31:17 -07001983
Dianne Hackborn21556372010-02-04 16:34:40 -08001984 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 synchronized (this) {
1986 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001987 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001989 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 }
1992
Narayan Kamath29564cd2014-08-07 10:57:40 +01001993 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001994 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001995 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001996
1997 // give ourselves a default profiler
1998 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001999 }
2000 }
2001
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002002 void ensureJitEnabled() {
2003 if (!mJitEnabled) {
2004 mJitEnabled = true;
2005 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2006 }
2007 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 void scheduleGcIdler() {
2010 if (!mGcIdlerScheduled) {
2011 mGcIdlerScheduled = true;
2012 Looper.myQueue().addIdleHandler(mGcIdler);
2013 }
2014 mH.removeMessages(H.GC_WHEN_IDLE);
2015 }
2016
2017 void unscheduleGcIdler() {
2018 if (mGcIdlerScheduled) {
2019 mGcIdlerScheduled = false;
2020 Looper.myQueue().removeIdleHandler(mGcIdler);
2021 }
2022 mH.removeMessages(H.GC_WHEN_IDLE);
2023 }
2024
2025 void doGcIfNeeded() {
2026 mGcIdlerScheduled = false;
2027 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002028 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 // + "m now=" + now);
2030 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002031 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 BinderInternal.forceGc("bg");
2033 }
2034 }
2035
Dianne Hackborne77187d2013-10-25 16:32:41 -07002036 private static final String HEAP_FULL_COLUMN
2037 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2038 private static final String HEAP_COLUMN
2039 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002040 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2041 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2042 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002043
2044 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002045 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002046
2047 static void printRow(PrintWriter pw, String format, Object...objs) {
2048 pw.println(String.format(format, objs));
2049 }
2050
2051 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002052 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2053 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002054 long nativeMax, long nativeAllocated, long nativeFree,
2055 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2056
2057 // For checkin, we print one long comma-separated list of values
2058 if (checkin) {
2059 // NOTE: if you change anything significant below, also consider changing
2060 // ACTIVITY_THREAD_CHECKIN_VERSION.
2061
2062 // Header
2063 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2064 pw.print(pid); pw.print(',');
2065 pw.print(processName); pw.print(',');
2066
2067 // Heap info - max
2068 pw.print(nativeMax); pw.print(',');
2069 pw.print(dalvikMax); pw.print(',');
2070 pw.print("N/A,");
2071 pw.print(nativeMax + dalvikMax); pw.print(',');
2072
2073 // Heap info - allocated
2074 pw.print(nativeAllocated); pw.print(',');
2075 pw.print(dalvikAllocated); pw.print(',');
2076 pw.print("N/A,");
2077 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2078
2079 // Heap info - free
2080 pw.print(nativeFree); pw.print(',');
2081 pw.print(dalvikFree); pw.print(',');
2082 pw.print("N/A,");
2083 pw.print(nativeFree + dalvikFree); pw.print(',');
2084
2085 // Heap info - proportional set size
2086 pw.print(memInfo.nativePss); pw.print(',');
2087 pw.print(memInfo.dalvikPss); pw.print(',');
2088 pw.print(memInfo.otherPss); pw.print(',');
2089 pw.print(memInfo.getTotalPss()); pw.print(',');
2090
2091 // Heap info - swappable set size
2092 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2093 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2094 pw.print(memInfo.otherSwappablePss); pw.print(',');
2095 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2096
2097 // Heap info - shared dirty
2098 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2099 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2100 pw.print(memInfo.otherSharedDirty); pw.print(',');
2101 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2102
2103 // Heap info - shared clean
2104 pw.print(memInfo.nativeSharedClean); pw.print(',');
2105 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2106 pw.print(memInfo.otherSharedClean); pw.print(',');
2107 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2108
2109 // Heap info - private Dirty
2110 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2111 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2112 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2113 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2114
2115 // Heap info - private Clean
2116 pw.print(memInfo.nativePrivateClean); pw.print(',');
2117 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2118 pw.print(memInfo.otherPrivateClean); pw.print(',');
2119 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2120
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002121 // Heap info - swapped out
2122 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2123 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2124 pw.print(memInfo.otherSwappedOut); pw.print(',');
2125 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2126
2127 // Heap info - swapped out pss
2128 if (memInfo.hasSwappedOutPss) {
2129 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2130 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2131 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2132 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2133 } else {
2134 pw.print("N/A,");
2135 pw.print("N/A,");
2136 pw.print("N/A,");
2137 pw.print("N/A,");
2138 }
2139
Dianne Hackborne77187d2013-10-25 16:32:41 -07002140 // Heap info - other areas
2141 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2142 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2143 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2144 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2145 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2146 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2147 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2148 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002149 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2150 if (memInfo.hasSwappedOutPss) {
2151 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2152 } else {
2153 pw.print("N/A,");
2154 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002155 }
2156 return;
2157 }
2158
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002159 if (!dumpSummaryOnly) {
2160 if (dumpFullInfo) {
2161 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002162 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2163 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002164 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002165 "Clean", "Clean", "Dirty",
2166 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002167 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2168 "------", "------", "------", "------", "------", "------");
2169 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2170 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2171 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002172 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2173 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002174 nativeMax, nativeAllocated, nativeFree);
2175 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2176 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2177 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002178 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2179 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002180 dalvikMax, dalvikAllocated, dalvikFree);
2181 } else {
2182 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002183 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2184 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002185 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2186 "Clean", "Dirty", "Size", "Alloc", "Free");
2187 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2188 "------", "------", "------", "------", "------");
2189 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2190 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002191 memInfo.nativePrivateClean,
2192 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2193 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002194 nativeMax, nativeAllocated, nativeFree);
2195 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2196 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002197 memInfo.dalvikPrivateClean,
2198 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2199 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002200 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002201 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002202
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002203 int otherPss = memInfo.otherPss;
2204 int otherSwappablePss = memInfo.otherSwappablePss;
2205 int otherSharedDirty = memInfo.otherSharedDirty;
2206 int otherPrivateDirty = memInfo.otherPrivateDirty;
2207 int otherSharedClean = memInfo.otherSharedClean;
2208 int otherPrivateClean = memInfo.otherPrivateClean;
2209 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002210 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002211
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002212 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002213 final int myPss = memInfo.getOtherPss(i);
2214 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2215 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2216 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2217 final int mySharedClean = memInfo.getOtherSharedClean(i);
2218 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2219 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002220 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002221 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002222 || mySharedClean != 0 || myPrivateClean != 0
2223 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002224 if (dumpFullInfo) {
2225 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2226 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002227 mySharedClean, myPrivateClean,
2228 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2229 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002230 } else {
2231 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2232 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002233 myPrivateClean,
2234 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2235 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002236 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002237 otherPss -= myPss;
2238 otherSwappablePss -= mySwappablePss;
2239 otherSharedDirty -= mySharedDirty;
2240 otherPrivateDirty -= myPrivateDirty;
2241 otherSharedClean -= mySharedClean;
2242 otherPrivateClean -= myPrivateClean;
2243 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002244 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002245 }
2246 }
2247
2248 if (dumpFullInfo) {
2249 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2250 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002251 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2252 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002253 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2254 memInfo.getTotalSwappablePss(),
2255 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2256 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002257 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2258 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002259 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2260 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002261 } else {
2262 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002263 otherPrivateDirty, otherPrivateClean,
2264 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002265 "", "", "");
2266 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2267 memInfo.getTotalPrivateDirty(),
2268 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002269 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002270 memInfo.getTotalSwappedOut(),
2271 nativeMax+dalvikMax,
2272 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2273 }
2274
2275 if (dumpDalvik) {
2276 pw.println(" ");
2277 pw.println(" Dalvik Details");
2278
2279 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2280 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2281 final int myPss = memInfo.getOtherPss(i);
2282 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2283 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2284 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2285 final int mySharedClean = memInfo.getOtherSharedClean(i);
2286 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2287 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002288 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002289 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002290 || mySharedClean != 0 || myPrivateClean != 0
2291 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002292 if (dumpFullInfo) {
2293 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2294 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002295 mySharedClean, myPrivateClean,
2296 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2297 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002298 } else {
2299 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2300 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002301 myPrivateClean,
2302 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2303 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002304 }
2305 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002306 }
2307 }
2308 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002309
2310 pw.println(" ");
2311 pw.println(" App Summary");
2312 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2313 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2314 printRow(pw, ONE_COUNT_COLUMN,
2315 "Java Heap:", memInfo.getSummaryJavaHeap());
2316 printRow(pw, ONE_COUNT_COLUMN,
2317 "Native Heap:", memInfo.getSummaryNativeHeap());
2318 printRow(pw, ONE_COUNT_COLUMN,
2319 "Code:", memInfo.getSummaryCode());
2320 printRow(pw, ONE_COUNT_COLUMN,
2321 "Stack:", memInfo.getSummaryStack());
2322 printRow(pw, ONE_COUNT_COLUMN,
2323 "Graphics:", memInfo.getSummaryGraphics());
2324 printRow(pw, ONE_COUNT_COLUMN,
2325 "Private Other:", memInfo.getSummaryPrivateOther());
2326 printRow(pw, ONE_COUNT_COLUMN,
2327 "System:", memInfo.getSummarySystem());
2328 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002329 if (memInfo.hasSwappedOutPss) {
2330 printRow(pw, TWO_COUNT_COLUMNS,
2331 "TOTAL:", memInfo.getSummaryTotalPss(),
2332 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2333 } else {
2334 printRow(pw, TWO_COUNT_COLUMNS,
2335 "TOTAL:", memInfo.getSummaryTotalPss(),
2336 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2337 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002338 }
2339
Jeff Hamilton52d32032011-01-08 15:31:26 -06002340 public void registerOnActivityPausedListener(Activity activity,
2341 OnActivityPausedListener listener) {
2342 synchronized (mOnPauseListeners) {
2343 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2344 if (list == null) {
2345 list = new ArrayList<OnActivityPausedListener>();
2346 mOnPauseListeners.put(activity, list);
2347 }
2348 list.add(listener);
2349 }
2350 }
2351
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002352 public void unregisterOnActivityPausedListener(Activity activity,
2353 OnActivityPausedListener listener) {
2354 synchronized (mOnPauseListeners) {
2355 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2356 if (list != null) {
2357 list.remove(listener);
2358 }
2359 }
2360 }
2361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 public final ActivityInfo resolveActivityInfo(Intent intent) {
2363 ActivityInfo aInfo = intent.resolveActivityInfo(
2364 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2365 if (aInfo == null) {
2366 // Throw an exception.
2367 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002368 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370 return aInfo;
2371 }
Bob Leee5408332009-09-04 18:31:17 -07002372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002375 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002376 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002378 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 r.intent = intent;
2380 r.state = state;
2381 r.parent = parent;
2382 r.embeddedID = id;
2383 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002384 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 if (localLOGV) {
2386 ComponentName compname = intent.getComponent();
2387 String name;
2388 if (compname != null) {
2389 name = compname.toShortString();
2390 } else {
2391 name = "(Intent " + intent + ").getComponent() returned null";
2392 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002393 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 + ", comp=" + name
2395 + ", token=" + token);
2396 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002397 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 }
2399
2400 public final Activity getActivity(IBinder token) {
2401 return mActivities.get(token).activity;
2402 }
2403
2404 public final void sendActivityResult(
2405 IBinder token, String id, int requestCode,
2406 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002407 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002408 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2410 list.add(new ResultInfo(id, requestCode, resultCode, data));
2411 mAppThread.scheduleSendResult(token, list);
2412 }
2413
Jeff Brown9ef09972013-10-15 20:49:59 -07002414 private void sendMessage(int what, Object obj) {
2415 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 }
2417
Jeff Brown9ef09972013-10-15 20:49:59 -07002418 private void sendMessage(int what, Object obj, int arg1) {
2419 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002420 }
2421
Jeff Brown9ef09972013-10-15 20:49:59 -07002422 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2423 sendMessage(what, obj, arg1, arg2, false);
2424 }
2425
2426 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2427 if (DEBUG_MESSAGES) Slog.v(
2428 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2429 + ": " + arg1 + " / " + obj);
2430 Message msg = Message.obtain();
2431 msg.what = what;
2432 msg.obj = obj;
2433 msg.arg1 = arg1;
2434 msg.arg2 = arg2;
2435 if (async) {
2436 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002438 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
2440
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002441 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2442 if (DEBUG_MESSAGES) Slog.v(
2443 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2444 "seq= " + seq);
2445 Message msg = Message.obtain();
2446 msg.what = what;
2447 SomeArgs args = SomeArgs.obtain();
2448 args.arg1 = obj;
2449 args.argi1 = arg1;
2450 args.argi2 = arg2;
2451 args.argi3 = seq;
2452 msg.obj = args;
2453 mH.sendMessage(msg);
2454 }
2455
Dianne Hackborn21556372010-02-04 16:34:40 -08002456 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 String what) {
2458 ContextCleanupInfo cci = new ContextCleanupInfo();
2459 cci.context = context;
2460 cci.who = who;
2461 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002462 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 }
2464
Craig Mautner233ceee2014-05-09 17:05:11 -07002465 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2467
2468 ActivityInfo aInfo = r.activityInfo;
2469 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002470 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 Context.CONTEXT_INCLUDE_CODE);
2472 }
Bob Leee5408332009-09-04 18:31:17 -07002473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 ComponentName component = r.intent.getComponent();
2475 if (component == null) {
2476 component = r.intent.resolveActivity(
2477 mInitialApplication.getPackageManager());
2478 r.intent.setComponent(component);
2479 }
2480
2481 if (r.activityInfo.targetActivity != null) {
2482 component = new ComponentName(r.activityInfo.packageName,
2483 r.activityInfo.targetActivity);
2484 }
2485
2486 Activity activity = null;
2487 try {
2488 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2489 activity = mInstrumentation.newActivity(
2490 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002491 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002492 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002493 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 if (r.state != null) {
2495 r.state.setClassLoader(cl);
2496 }
2497 } catch (Exception e) {
2498 if (!mInstrumentation.onException(activity, e)) {
2499 throw new RuntimeException(
2500 "Unable to instantiate activity " + component
2501 + ": " + e.toString(), e);
2502 }
2503 }
2504
2505 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002506 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002507
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002508 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2509 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 TAG, r + ": app=" + app
2511 + ", appName=" + app.getPackageName()
2512 + ", pkg=" + r.packageInfo.getPackageName()
2513 + ", comp=" + r.intent.getComponent().toShortString()
2514 + ", dir=" + r.packageInfo.getAppDir());
2515
2516 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002517 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002519 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002520 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002521 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002522 Window window = null;
2523 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2524 window = r.mPendingRemoveWindow;
2525 r.mPendingRemoveWindow = null;
2526 r.mPendingRemoveWindowManager = null;
2527 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002528 activity.attach(appContext, this, getInstrumentation(), r.token,
2529 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002530 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002531 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002532
Christopher Tateb70f3df2009-04-07 16:07:59 -07002533 if (customIntent != null) {
2534 activity.mIntent = customIntent;
2535 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002536 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 activity.mStartedActivity = false;
2538 int theme = r.activityInfo.getThemeResource();
2539 if (theme != 0) {
2540 activity.setTheme(theme);
2541 }
2542
2543 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002544 if (r.isPersistable()) {
2545 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2546 } else {
2547 mInstrumentation.callActivityOnCreate(activity, r.state);
2548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 if (!activity.mCalled) {
2550 throw new SuperNotCalledException(
2551 "Activity " + r.intent.getComponent().toShortString() +
2552 " did not call through to super.onCreate()");
2553 }
2554 r.activity = activity;
2555 r.stopped = true;
2556 if (!r.activity.mFinished) {
2557 activity.performStart();
2558 r.stopped = false;
2559 }
2560 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002561 if (r.isPersistable()) {
2562 if (r.state != null || r.persistentState != null) {
2563 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2564 r.persistentState);
2565 }
2566 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2568 }
2569 }
2570 if (!r.activity.mFinished) {
2571 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002572 if (r.isPersistable()) {
2573 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2574 r.persistentState);
2575 } else {
2576 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2577 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 if (!activity.mCalled) {
2579 throw new SuperNotCalledException(
2580 "Activity " + r.intent.getComponent().toShortString() +
2581 " did not call through to super.onPostCreate()");
2582 }
2583 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002584 }
2585 r.paused = true;
2586
2587 mActivities.put(r.token, r);
2588
2589 } catch (SuperNotCalledException e) {
2590 throw e;
2591
2592 } catch (Exception e) {
2593 if (!mInstrumentation.onException(activity, e)) {
2594 throw new RuntimeException(
2595 "Unable to start activity " + component
2596 + ": " + e.toString(), e);
2597 }
2598 }
2599
2600 return activity;
2601 }
2602
Wale Ogunwale7c726682015-02-06 17:34:28 -08002603 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2604 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002605 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002606 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002607 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002608 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002609 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002610
Wale Ogunwale7c726682015-02-06 17:34:28 -08002611 ContextImpl appContext = ContextImpl.createActivityContext(
2612 this, r.packageInfo, displayId, r.overrideConfig);
2613 appContext.setOuterContext(activity);
2614 Context baseContext = appContext;
2615
2616 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002617 // For debugging purposes, if the activity's package name contains the value of
2618 // the "debug.use-second-display" system property as a substring, then show
2619 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002620 String pkgName = SystemProperties.get("debug.second-display.pkg");
2621 if (pkgName != null && !pkgName.isEmpty()
2622 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002623 for (int id : dm.getDisplayIds()) {
2624 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002625 Display display =
2626 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002627 baseContext = appContext.createDisplayContext(display);
2628 break;
2629 }
2630 }
2631 }
2632 return baseContext;
2633 }
2634
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002635 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 // If we are getting ready to gc after going to the background, well
2637 // we are back active so skip it.
2638 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002639 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002640
Jeff Hao1b012d32014-08-20 10:35:34 -07002641 if (r.profilerInfo != null) {
2642 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002643 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002644 }
2645
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002646 // Make sure we are running with the most recent config.
2647 handleConfigurationChanged(null, null);
2648
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002649 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002651
Chet Haase0d1c27a2014-11-03 18:35:16 +00002652 // Initialize before creating the activity
2653 WindowManagerGlobal.initialize();
2654
Craig Mautner233ceee2014-05-09 17:05:11 -07002655 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656
2657 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002658 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002659 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002660 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002661 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002662 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663
2664 if (!r.activity.mFinished && r.startsNotResumed) {
2665 // The activity manager actually wants this one to start out
2666 // paused, because it needs to be visible but isn't in the
2667 // foreground. We accomplish this by going through the
2668 // normal startup (because activities expect to go through
2669 // onResume() the first time they run, before their window
2670 // is displayed), and then pausing it. However, in this case
2671 // we do -not- need to do the full pause cycle (of freezing
2672 // and such) because the activity manager assumes it can just
2673 // retain the current state it has.
2674 try {
2675 r.activity.mCalled = false;
2676 mInstrumentation.callActivityOnPause(r.activity);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002677 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
2678 r.activity.getComponentName().getClassName(), reason);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002679 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002680 // we need to be created again. But we only do this
2681 // for pre-Honeycomb apps, which always save their state
2682 // when pausing, so we can not have them save their state
2683 // when restarting from a paused state. For HC and later,
2684 // we want to (and can) let the state be saved as the normal
2685 // part of stopping the activity.
2686 if (r.isPreHoneycomb()) {
2687 r.state = oldState;
2688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 if (!r.activity.mCalled) {
2690 throw new SuperNotCalledException(
2691 "Activity " + r.intent.getComponent().toShortString() +
2692 " did not call through to super.onPause()");
2693 }
2694
2695 } catch (SuperNotCalledException e) {
2696 throw e;
2697
2698 } catch (Exception e) {
2699 if (!mInstrumentation.onException(r.activity, e)) {
2700 throw new RuntimeException(
2701 "Unable to pause activity "
2702 + r.intent.getComponent().toShortString()
2703 + ": " + e.toString(), e);
2704 }
2705 }
2706 r.paused = true;
2707 }
2708 } else {
2709 // If there was an error, for any reason, tell the activity
2710 // manager to stop us.
2711 try {
2712 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002713 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2714 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002716 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 }
2718 }
2719 }
2720
Filip Gruszczynski23493322015-07-29 17:02:59 -07002721 private void reportSizeConfigurations(ActivityClientRecord r) {
2722 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2723 if (configurations == null) {
2724 return;
2725 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002726 SparseIntArray horizontal = new SparseIntArray();
2727 SparseIntArray vertical = new SparseIntArray();
2728 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002729 for (int i = configurations.length - 1; i >= 0; i--) {
2730 Configuration config = configurations[i];
2731 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002732 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002733 }
2734 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002735 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002736 }
2737 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002738 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002739 }
2740 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002741 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002742 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2743 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002744 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002745 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002746 }
2747
2748 }
2749
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002750 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 final int N = intents.size();
2752 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002753 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002755 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002756 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2758 }
2759 }
2760
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002761 public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002762 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 if (r != null) {
2764 final boolean resumed = !r.paused;
2765 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002766 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 mInstrumentation.callActivityOnPause(r.activity);
2768 }
2769 deliverNewIntents(r, intents);
2770 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002771 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002772 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 }
2774 }
2775 }
Bob Leee5408332009-09-04 18:31:17 -07002776
Romain Guy65b345f2011-07-27 18:51:50 -07002777 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 performNewIntents(data.token, data.intents);
2779 }
2780
Adam Skorydfc7fd72013-08-05 19:23:41 -07002781 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackborn782d4982015-07-08 17:36:37 -07002782 if (mLastAssistStructure != null) {
2783 AssistStructure structure = mLastAssistStructure.get();
2784 if (structure != null) {
2785 structure.clearSendChannel();
2786 }
2787 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002788 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002789 AssistStructure structure = null;
2790 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002791 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002792 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002793 if (r != null) {
2794 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2795 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002796 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002797 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002798 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002799 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002800 if (activityIntent != null && (r.window == null ||
2801 (r.window.getAttributes().flags
2802 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002803 Intent intent = new Intent(activityIntent);
2804 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2805 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2806 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002807 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002808 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002809 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002810 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002811 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002812 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002813 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002814 if (structure == null) {
2815 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002816 }
Dianne Hackborn782d4982015-07-08 17:36:37 -07002817 mLastAssistStructure = new WeakReference<>(structure);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002818 IActivityManager mgr = ActivityManagerNative.getDefault();
2819 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002820 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002821 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002822 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002823 }
2824 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002825
2826 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2827 ActivityClientRecord r = mActivities.get(token);
2828 if (r != null) {
2829 r.activity.onTranslucentConversionComplete(drawComplete);
2830 }
2831 }
2832
Craig Mautnereb8abf72014-07-02 15:04:09 -07002833 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2834 ActivityClientRecord r = mActivities.get(token);
2835 if (r != null) {
2836 r.activity.onNewActivityOptions(options);
2837 }
2838 }
2839
Jose Lima4b6c6692014-08-12 17:41:12 -07002840 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002841 ActivityClientRecord r = mActivities.get(token);
2842 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002843 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002844 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002845 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002846 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002847 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002848 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2849 if (!activity.mCalled) {
2850 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002851 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002852 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002853 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002854 }
2855 }
2856 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002857 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002858 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002859 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002860 }
2861 }
2862
Jose Lima4b6c6692014-08-12 17:41:12 -07002863 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002864 ActivityClientRecord r = mActivities.get(token);
2865 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002866 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002867 }
2868 }
2869
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002870 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002871 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2872 try {
2873 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2874 } finally {
2875 StrictMode.setThreadPolicy(oldPolicy);
2876 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002877 }
2878
Craig Mautner8746a472014-07-24 15:12:54 -07002879 private void handleEnterAnimationComplete(IBinder token) {
2880 ActivityClientRecord r = mActivities.get(token);
2881 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002882 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002883 }
2884 }
2885
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002886 private void handleStartBinderTracking() {
2887 Binder.enableTracing();
2888 }
2889
2890 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2891 try {
2892 Binder.disableTracing();
2893 Binder.getTransactionTracker().writeTracesToFile(fd);
2894 } finally {
2895 IoUtils.closeQuietly(fd);
2896 Binder.getTransactionTracker().clearTraces();
2897 }
2898 }
2899
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002900 private void handleMultiWindowChanged(IBinder token, boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002901 final ActivityClientRecord r = mActivities.get(token);
2902 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002903 r.activity.onMultiWindowChanged(inMultiWindow);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002904 }
2905 }
2906
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002907 private void handlePictureInPictureChanged(IBinder token, boolean inPip) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002908 final ActivityClientRecord r = mActivities.get(token);
2909 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002910 r.activity.onPictureInPictureChanged(inPip);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002911 }
2912 }
2913
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002914 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2915 final ActivityClientRecord r = mActivities.get(token);
2916 if (r != null) {
2917 r.voiceInteractor = interactor;
2918 r.activity.setVoiceInteractor(interactor);
2919 if (interactor == null) {
2920 r.activity.onLocalVoiceInteractionStopped();
2921 } else {
2922 r.activity.onLocalVoiceInteractionStarted();
2923 }
2924 }
2925 }
2926
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002927 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2928
2929 /**
2930 * Return the Intent that's currently being handled by a
2931 * BroadcastReceiver on this thread, or null if none.
2932 * @hide
2933 */
2934 public static Intent getIntentBeingBroadcast() {
2935 return sCurrentBroadcastIntent.get();
2936 }
2937
Romain Guy65b345f2011-07-27 18:51:50 -07002938 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 // If we are getting ready to gc after going to the background, well
2940 // we are back active so skip it.
2941 unscheduleGcIdler();
2942
2943 String component = data.intent.getComponent().getClassName();
2944
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002945 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002946 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947
2948 IActivityManager mgr = ActivityManagerNative.getDefault();
2949
Romain Guy65b345f2011-07-27 18:51:50 -07002950 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 try {
2952 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2953 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002954 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002955 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2957 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002958 if (DEBUG_BROADCAST) Slog.i(TAG,
2959 "Finishing failed broadcast to " + data.intent.getComponent());
2960 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 throw new RuntimeException(
2962 "Unable to instantiate receiver " + component
2963 + ": " + e.toString(), e);
2964 }
2965
2966 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002967 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002968
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002969 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 TAG, "Performing receive of " + data.intent
2971 + ": app=" + app
2972 + ", appName=" + app.getPackageName()
2973 + ", pkg=" + packageInfo.getPackageName()
2974 + ", comp=" + data.intent.getComponent().toShortString()
2975 + ", dir=" + packageInfo.getAppDir());
2976
Dianne Hackborn21556372010-02-04 16:34:40 -08002977 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002978 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002979 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 receiver.onReceive(context.getReceiverRestrictedContext(),
2981 data.intent);
2982 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002983 if (DEBUG_BROADCAST) Slog.i(TAG,
2984 "Finishing failed broadcast to " + data.intent.getComponent());
2985 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 if (!mInstrumentation.onException(receiver, e)) {
2987 throw new RuntimeException(
2988 "Unable to start receiver " + component
2989 + ": " + e.toString(), e);
2990 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002991 } finally {
2992 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 }
2994
Dianne Hackborne829fef2010-10-26 17:44:01 -07002995 if (receiver.getPendingResult() != null) {
2996 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 }
2998 }
2999
Christopher Tate181fafa2009-05-14 11:12:14 -07003000 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003001 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003002 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003003
Christopher Tate346acb12012-10-15 19:20:25 -07003004 // Sanity check the requested target package's uid against ours
3005 try {
3006 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3007 data.appInfo.packageName, 0, UserHandle.myUserId());
3008 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3009 Slog.w(TAG, "Asked to instantiate non-matching package "
3010 + data.appInfo.packageName);
3011 return;
3012 }
3013 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003014 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003015 }
3016
Christopher Tate181fafa2009-05-14 11:12:14 -07003017 // no longer idle; we have backup work to do
3018 unscheduleGcIdler();
3019
3020 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003021 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003022 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003023 if (packageName == null) {
3024 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3025 return;
3026 }
3027
Christopher Tate181fafa2009-05-14 11:12:14 -07003028 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003029 // full backup operation but no app-supplied agent? use the default implementation
3030 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3031 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003032 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003033 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003034
Christopher Tate181fafa2009-05-14 11:12:14 -07003035 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003036 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003037 BackupAgent agent = mBackupAgents.get(packageName);
3038 if (agent != null) {
3039 // reusing the existing instance
3040 if (DEBUG_BACKUP) {
3041 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003042 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003043 binder = agent.onBind();
3044 } else {
3045 try {
3046 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3047
3048 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3049 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3050
3051 // set up the agent's context
3052 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3053 context.setOuterContext(agent);
3054 agent.attach(context);
3055
3056 agent.onCreate();
3057 binder = agent.onBind();
3058 mBackupAgents.put(packageName, agent);
3059 } catch (Exception e) {
3060 // If this is during restore, fail silently; otherwise go
3061 // ahead and let the user see the crash.
3062 Slog.e(TAG, "Agent threw during creation: " + e);
3063 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3064 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3065 throw e;
3066 }
3067 // falling through with 'binder' still null
3068 }
Christopher Tated1475e02009-07-09 15:36:17 -07003069 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003070
3071 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003072 try {
3073 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3074 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003075 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003076 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003077 } catch (Exception e) {
3078 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003079 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003080 }
3081 }
3082
3083 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003084 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003085 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003086
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003087 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003088 String packageName = packageInfo.mPackageName;
3089 BackupAgent agent = mBackupAgents.get(packageName);
3090 if (agent != null) {
3091 try {
3092 agent.onDestroy();
3093 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003094 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003095 e.printStackTrace();
3096 }
3097 mBackupAgents.remove(packageName);
3098 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003099 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003100 }
3101 }
3102
Romain Guy65b345f2011-07-27 18:51:50 -07003103 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 // If we are getting ready to gc after going to the background, well
3105 // we are back active so skip it.
3106 unscheduleGcIdler();
3107
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003108 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003109 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 Service service = null;
3111 try {
3112 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3113 service = (Service) cl.loadClass(data.info.name).newInstance();
3114 } catch (Exception e) {
3115 if (!mInstrumentation.onException(service, e)) {
3116 throw new RuntimeException(
3117 "Unable to instantiate service " + data.info.name
3118 + ": " + e.toString(), e);
3119 }
3120 }
3121
3122 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003123 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124
Jeff Browndefd4a62014-03-10 21:24:37 -07003125 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3126 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003128 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003129 service.attach(context, this, data.info.name, data.token, app,
3130 ActivityManagerNative.getDefault());
3131 service.onCreate();
3132 mServices.put(data.token, service);
3133 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003134 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003135 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003137 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 }
3139 } catch (Exception e) {
3140 if (!mInstrumentation.onException(service, e)) {
3141 throw new RuntimeException(
3142 "Unable to create service " + data.info.name
3143 + ": " + e.toString(), e);
3144 }
3145 }
3146 }
3147
Romain Guy65b345f2011-07-27 18:51:50 -07003148 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003150 if (DEBUG_SERVICE)
3151 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 if (s != null) {
3153 try {
3154 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003155 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 try {
3157 if (!data.rebind) {
3158 IBinder binder = s.onBind(data.intent);
3159 ActivityManagerNative.getDefault().publishService(
3160 data.token, data.intent, binder);
3161 } else {
3162 s.onRebind(data.intent);
3163 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003164 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003166 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003168 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 }
3170 } catch (Exception e) {
3171 if (!mInstrumentation.onException(s, e)) {
3172 throw new RuntimeException(
3173 "Unable to bind to service " + s
3174 + " with " + data.intent + ": " + e.toString(), e);
3175 }
3176 }
3177 }
3178 }
3179
Romain Guy65b345f2011-07-27 18:51:50 -07003180 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 Service s = mServices.get(data.token);
3182 if (s != null) {
3183 try {
3184 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003185 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003186 boolean doRebind = s.onUnbind(data.intent);
3187 try {
3188 if (doRebind) {
3189 ActivityManagerNative.getDefault().unbindFinished(
3190 data.token, data.intent, doRebind);
3191 } else {
3192 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003193 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 }
3195 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003196 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
3198 } catch (Exception e) {
3199 if (!mInstrumentation.onException(s, e)) {
3200 throw new RuntimeException(
3201 "Unable to unbind to service " + s
3202 + " with " + data.intent + ": " + e.toString(), e);
3203 }
3204 }
3205 }
3206 }
3207
Dianne Hackborn625ac272010-09-17 18:29:22 -07003208 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003209 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3210 try {
3211 Service s = mServices.get(info.token);
3212 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003213 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3214 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003215 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3216 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003218 } finally {
3219 IoUtils.closeQuietly(info.fd);
3220 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 }
3222 }
3223
Dianne Hackborn625ac272010-09-17 18:29:22 -07003224 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003225 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3226 try {
3227 ActivityClientRecord r = mActivities.get(info.token);
3228 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003229 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3230 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003231 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3232 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003233 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003234 } finally {
3235 IoUtils.closeQuietly(info.fd);
3236 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003237 }
3238 }
3239
Marco Nelissen18cb2872011-11-15 11:19:53 -08003240 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003241 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3242 try {
3243 ProviderClientRecord r = mLocalProviders.get(info.token);
3244 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003245 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3246 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003247 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3248 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003249 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003250 } finally {
3251 IoUtils.closeQuietly(info.fd);
3252 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003253 }
3254 }
3255
Romain Guy65b345f2011-07-27 18:51:50 -07003256 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003257 Service s = mServices.get(data.token);
3258 if (s != null) {
3259 try {
3260 if (data.args != null) {
3261 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003262 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003264 int res;
3265 if (!data.taskRemoved) {
3266 res = s.onStartCommand(data.args, data.flags, data.startId);
3267 } else {
3268 s.onTaskRemoved(data.args);
3269 res = Service.START_TASK_REMOVED_COMPLETE;
3270 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003271
3272 QueuedWork.waitToFinish();
3273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003274 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003275 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003276 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003277 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003278 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003279 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003280 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003281 } catch (Exception e) {
3282 if (!mInstrumentation.onException(s, e)) {
3283 throw new RuntimeException(
3284 "Unable to start service " + s
3285 + " with " + data.args + ": " + e.toString(), e);
3286 }
3287 }
3288 }
3289 }
3290
Romain Guy65b345f2011-07-27 18:51:50 -07003291 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 Service s = mServices.remove(token);
3293 if (s != null) {
3294 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003295 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 s.onDestroy();
3297 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003298 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003299 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003300 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003302
3303 QueuedWork.waitToFinish();
3304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003305 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003306 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003307 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003309 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
3311 } catch (Exception e) {
3312 if (!mInstrumentation.onException(s, e)) {
3313 throw new RuntimeException(
3314 "Unable to stop service " + s
3315 + ": " + e.toString(), e);
3316 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003317 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003319 } else {
3320 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003321 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003322 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 }
3324
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003325 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003326 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003327 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003328 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003329 + " finished=" + r.activity.mFinished);
3330 if (r != null && !r.activity.mFinished) {
3331 if (clearHide) {
3332 r.hideForNow = false;
3333 r.activity.mStartedActivity = false;
3334 }
3335 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003336 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003337 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 if (r.pendingIntents != null) {
3339 deliverNewIntents(r, r.pendingIntents);
3340 r.pendingIntents = null;
3341 }
3342 if (r.pendingResults != null) {
3343 deliverResults(r, r.pendingResults);
3344 r.pendingResults = null;
3345 }
3346 r.activity.performResume();
3347
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003348 // If there is a pending local relaunch that was requested when the activity was
3349 // paused, it will put the activity into paused state when it finally happens.
3350 // Since the activity resumed before being relaunched, we don't want that to happen,
3351 // so we need to clear the request to relaunch paused.
3352 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3353 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3354 if (relaunching.token == r.token
3355 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3356 relaunching.startsNotResumed = false;
3357 }
3358 }
3359
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003360 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3361 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003363 r.paused = false;
3364 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003366 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 } catch (Exception e) {
3368 if (!mInstrumentation.onException(r.activity, e)) {
3369 throw new RuntimeException(
3370 "Unable to resume activity "
3371 + r.intent.getComponent().toShortString()
3372 + ": " + e.toString(), e);
3373 }
3374 }
3375 }
3376 return r;
3377 }
3378
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003379 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3380 if (r.mPreserveWindow && !force) {
3381 return;
3382 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003383 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003384 r.mPendingRemoveWindowManager.removeViewImmediate(
3385 r.mPendingRemoveWindow.getDecorView());
3386 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003387 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003388 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003389 r.activity.getClass().getName(), "Activity");
3390 }
3391 }
3392 r.mPendingRemoveWindow = null;
3393 r.mPendingRemoveWindowManager = null;
3394 }
3395
Craig Mautner233ceee2014-05-09 17:05:11 -07003396 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003397 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003398 ActivityClientRecord r = mActivities.get(token);
3399 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3400 return;
3401 }
3402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403 // If we are getting ready to gc after going to the background, well
3404 // we are back active so skip it.
3405 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003406 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407
Adam Powellcfbe9be2013-11-06 14:58:58 -08003408 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003409 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410
3411 if (r != null) {
3412 final Activity a = r.activity;
3413
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003414 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003415 TAG, "Resume " + r + " started activity: " +
3416 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3417 + ", finished: " + a.mFinished);
3418
3419 final int forwardBit = isForward ?
3420 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 // If the window hasn't yet been added to the window manager,
3423 // and this guy didn't finish itself or start another activity,
3424 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003425 boolean willBeVisible = !a.mStartedActivity;
3426 if (!willBeVisible) {
3427 try {
3428 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3429 a.getActivityToken());
3430 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003431 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003432 }
3433 }
3434 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 r.window = r.activity.getWindow();
3436 View decor = r.window.getDecorView();
3437 decor.setVisibility(View.INVISIBLE);
3438 ViewManager wm = a.getWindowManager();
3439 WindowManager.LayoutParams l = r.window.getAttributes();
3440 a.mDecor = decor;
3441 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3442 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003443 if (r.mPreserveWindow) {
3444 a.mWindowAdded = true;
3445 r.mPreserveWindow = false;
3446 }
3447 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 a.mWindowAdded = true;
3449 wm.addView(decor, l);
3450 }
3451
3452 // If the window has already been added, but during resume
3453 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003454 // window visible.
3455 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003456 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003457 TAG, "Launch " + r + " mStartedActivity set");
3458 r.hideForNow = true;
3459 }
3460
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003461 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003462 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 // The window is now visible if it has been added, we are not
3465 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003466 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003467 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003469 r.tmpConfig.setTo(r.newConfig);
3470 if (r.overrideConfig != null) {
3471 r.tmpConfig.updateFrom(r.overrideConfig);
3472 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003473 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003474 + r.activityInfo.name + " with newConfig " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003475 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003476 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 r.newConfig = null;
3478 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003479 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 + isForward);
3481 WindowManager.LayoutParams l = r.window.getAttributes();
3482 if ((l.softInputMode
3483 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3484 != forwardBit) {
3485 l.softInputMode = (l.softInputMode
3486 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3487 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003488 if (r.activity.mVisibleFromClient) {
3489 ViewManager wm = a.getWindowManager();
3490 View decor = r.window.getDecorView();
3491 wm.updateViewLayout(decor, l);
3492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003493 }
3494 r.activity.mVisibleFromServer = true;
3495 mNumVisibleActivities++;
3496 if (r.activity.mVisibleFromClient) {
3497 r.activity.makeVisible();
3498 }
3499 }
3500
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003501 if (!r.onlyLocalRequest) {
3502 r.nextIdle = mNewActivities;
3503 mNewActivities = r;
3504 if (localLOGV) Slog.v(
3505 TAG, "Scheduling idle handler for " + r);
3506 Looper.myQueue().addIdleHandler(new Idler());
3507 }
3508 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003510 // Tell the activity manager we have resumed.
3511 if (reallyResume) {
3512 try {
3513 ActivityManagerNative.getDefault().activityResumed(token);
3514 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003515 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003516 }
3517 }
3518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 } else {
3520 // If an exception was thrown when trying to resume, then
3521 // just end this activity.
3522 try {
3523 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003524 .finishActivity(token, Activity.RESULT_CANCELED, null,
3525 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003527 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003528 }
3529 }
3530 }
3531
3532 private int mThumbnailWidth = -1;
3533 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003534 private Bitmap mAvailThumbnailBitmap = null;
3535 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536
Romain Guy65b345f2011-07-27 18:51:50 -07003537 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003538 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003539 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003540 if (thumbnail == null) {
3541 int w = mThumbnailWidth;
3542 int h;
3543 if (w < 0) {
3544 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003545 int wId = com.android.internal.R.dimen.thumbnail_width;
3546 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003547 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3548 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003549 } else {
3550 h = mThumbnailHeight;
3551 }
3552
3553 // On platforms where we don't want thumbnails, set dims to (0,0)
3554 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003555 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3556 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003557 thumbnail.eraseColor(0);
3558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003559 }
3560
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003561 if (thumbnail != null) {
3562 Canvas cv = mThumbnailCanvas;
3563 if (cv == null) {
3564 mThumbnailCanvas = cv = new Canvas();
3565 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003566
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003567 cv.setBitmap(thumbnail);
3568 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3569 mAvailThumbnailBitmap = thumbnail;
3570 thumbnail = null;
3571 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003572 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 } catch (Exception e) {
3576 if (!mInstrumentation.onException(r.activity, e)) {
3577 throw new RuntimeException(
3578 "Unable to create thumbnail of "
3579 + r.intent.getComponent().toShortString()
3580 + ": " + e.toString(), e);
3581 }
3582 thumbnail = null;
3583 }
3584
3585 return thumbnail;
3586 }
3587
Romain Guy65b345f2011-07-27 18:51:50 -07003588 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003589 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003590 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003591 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003592 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3593 return;
3594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003596 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 if (userLeaving) {
3598 performUserLeavingActivity(r);
3599 }
Bob Leee5408332009-09-04 18:31:17 -07003600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003602 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003604 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003605 if (r.isPreHoneycomb()) {
3606 QueuedWork.waitToFinish();
3607 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003610 if (!dontReport) {
3611 try {
3612 ActivityManagerNative.getDefault().activityPaused(token);
3613 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003614 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003615 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003617 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 }
3619 }
3620
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003621 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 mInstrumentation.callActivityOnUserLeaving(r.activity);
3623 }
3624
3625 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003626 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003627 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003628 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629 }
3630
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003631 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003632 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 if (r.paused) {
3634 if (r.activity.mFinished) {
3635 // If we are finishing, we won't call onResume() in certain cases.
3636 // So here we likewise don't want to call onPause() if the activity
3637 // isn't resumed.
3638 return null;
3639 }
3640 RuntimeException e = new RuntimeException(
3641 "Performing pause of activity that is not resumed: "
3642 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003643 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645 if (finished) {
3646 r.activity.mFinished = true;
3647 }
3648 try {
3649 // Next have the activity save its current state and managed dialogs...
3650 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003651 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 }
3653 // Now we are idle.
3654 r.activity.mCalled = false;
3655 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08003656 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003657 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003658 if (!r.activity.mCalled) {
3659 throw new SuperNotCalledException(
3660 "Activity " + r.intent.getComponent().toShortString() +
3661 " did not call through to super.onPause()");
3662 }
3663
3664 } catch (SuperNotCalledException e) {
3665 throw e;
3666
3667 } catch (Exception e) {
3668 if (!mInstrumentation.onException(r.activity, e)) {
3669 throw new RuntimeException(
3670 "Unable to pause activity "
3671 + r.intent.getComponent().toShortString()
3672 + ": " + e.toString(), e);
3673 }
3674 }
3675 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003676
3677 // Notify any outstanding on paused listeners
3678 ArrayList<OnActivityPausedListener> listeners;
3679 synchronized (mOnPauseListeners) {
3680 listeners = mOnPauseListeners.remove(r.activity);
3681 }
3682 int size = (listeners != null ? listeners.size() : 0);
3683 for (int i = 0; i < size; i++) {
3684 listeners.get(i).onPaused(r.activity);
3685 }
3686
Craig Mautnera0026042014-04-23 11:45:37 -07003687 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 }
3689
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003690 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003691 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003692 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
3694
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003695 private static class StopInfo implements Runnable {
3696 ActivityClientRecord activity;
3697 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003698 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003700
3701 @Override public void run() {
3702 // Tell activity manager we have been stopped.
3703 try {
3704 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3705 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003706 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003707 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003708 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003709 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
3710 Log.e(TAG, "App tried sending too much data in instance state", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003711 return;
3712 }
3713
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003714 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003715 }
3716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 }
3718
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003719 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003720 public final IActivityManager.ContentProviderHolder holder;
3721 public final ProviderClientRecord client;
3722 public int stableCount;
3723 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003724
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003725 // When this is set, the stable and unstable ref counts are 0 and
3726 // we have a pending operation scheduled to remove the ref count
3727 // from the activity manager. On the activity manager we are still
3728 // holding an unstable ref, though it is not reflected in the counts
3729 // here.
3730 public boolean removePending;
3731
3732 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3733 ProviderClientRecord inClient, int sCount, int uCount) {
3734 holder = inHolder;
3735 client = inClient;
3736 stableCount = sCount;
3737 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 }
3739 }
3740
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003741 /**
3742 * Core implementation of stopping an activity. Note this is a little
3743 * tricky because the server's meaning of stop is slightly different
3744 * than our client -- for the server, stop means to save state and give
3745 * it the result when it is done, but the window may still be visible.
3746 * For the client, we want to call onStop()/onStart() to indicate when
3747 * the activity's UI visibillity changes.
3748 */
Romain Guy65b345f2011-07-27 18:51:50 -07003749 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003750 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003751 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 if (r != null) {
3753 if (!keepShown && r.stopped) {
3754 if (r.activity.mFinished) {
3755 // If we are finishing, we won't call onResume() in certain
3756 // cases. So here we likewise don't want to call onStop()
3757 // if the activity isn't resumed.
3758 return;
3759 }
3760 RuntimeException e = new RuntimeException(
3761 "Performing stop of activity that is not resumed: "
3762 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003763 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 }
3765
3766 if (info != null) {
3767 try {
3768 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003769 // For now, don't create the thumbnail here; we are
3770 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 info.description = r.activity.onCreateDescription();
3772 } catch (Exception e) {
3773 if (!mInstrumentation.onException(r.activity, e)) {
3774 throw new RuntimeException(
3775 "Unable to save state of activity "
3776 + r.intent.getComponent().toShortString()
3777 + ": " + e.toString(), e);
3778 }
3779 }
3780 }
3781
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003782 // Next have the activity save its current state and managed dialogs...
3783 if (!r.activity.mFinished && saveState) {
3784 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003785 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003786 }
3787 }
3788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003789 if (!keepShown) {
3790 try {
3791 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003792 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 } catch (Exception e) {
3794 if (!mInstrumentation.onException(r.activity, e)) {
3795 throw new RuntimeException(
3796 "Unable to stop activity "
3797 + r.intent.getComponent().toShortString()
3798 + ": " + e.toString(), e);
3799 }
3800 }
3801 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003802 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3803 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 }
3805
3806 r.paused = true;
3807 }
3808 }
3809
Romain Guy65b345f2011-07-27 18:51:50 -07003810 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 View v = r.activity.mDecor;
3812 if (v != null) {
3813 if (show) {
3814 if (!r.activity.mVisibleFromServer) {
3815 r.activity.mVisibleFromServer = true;
3816 mNumVisibleActivities++;
3817 if (r.activity.mVisibleFromClient) {
3818 r.activity.makeVisible();
3819 }
3820 }
3821 if (r.newConfig != null) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003822 r.tmpConfig.setTo(r.newConfig);
3823 if (r.overrideConfig != null) {
3824 r.tmpConfig.updateFrom(r.overrideConfig);
3825 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003826 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003827 + r.activityInfo.name + " with new config " + r.tmpConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08003828 performConfigurationChanged(r.activity, r.tmpConfig, REPORT_TO_ACTIVITY);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08003829 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 r.newConfig = null;
3831 }
3832 } else {
3833 if (r.activity.mVisibleFromServer) {
3834 r.activity.mVisibleFromServer = false;
3835 mNumVisibleActivities--;
3836 v.setVisibility(View.INVISIBLE);
3837 }
3838 }
3839 }
3840 }
3841
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003842 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003843 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003844 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3845 return;
3846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 r.activity.mConfigChangeFlags |= configChanges;
3848
3849 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003850 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003852 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 TAG, "Finishing stop of " + r + ": show=" + show
3854 + " win=" + r.window);
3855
3856 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003857
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003858 // Make sure any pending writes are now committed.
3859 if (!r.isPreHoneycomb()) {
3860 QueuedWork.waitToFinish();
3861 }
3862
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003863 // Schedule the call to tell the activity manager we have
3864 // stopped. We don't do this immediately, because we want to
3865 // have a chance for any other pending work (in particular memory
3866 // trim requests) to complete before you tell the activity
3867 // manager to proceed and allow us to go fully into the background.
3868 info.activity = r;
3869 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003870 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003871 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003872 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 }
3874
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003875 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3876 String action) {
3877 if (r == null) {
3878 return true;
3879 }
3880 if (seq < r.lastProcessedSeq) {
3881 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3882 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3883 return false;
3884 }
3885 r.lastProcessedSeq = seq;
3886 return true;
3887 }
3888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003890 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 if (r.stopped) {
3892 r.activity.performRestart();
3893 r.stopped = false;
3894 }
3895 }
3896
Romain Guy65b345f2011-07-27 18:51:50 -07003897 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003898 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003899
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003900 if (r == null) {
3901 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3902 return;
3903 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003906 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 } else if (show && r.stopped) {
3908 // If we are getting ready to gc after going to the background, well
3909 // we are back active so skip it.
3910 unscheduleGcIdler();
3911
3912 r.activity.performRestart();
3913 r.stopped = false;
3914 }
3915 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003916 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917 TAG, "Handle window " + r + " visibility: " + show);
3918 updateVisibility(r, show);
3919 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003920 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003921 }
3922
Romain Guy65b345f2011-07-27 18:51:50 -07003923 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003924 ActivityClientRecord r = mActivities.get(token);
3925
3926 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003927 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003928 return;
3929 }
3930
3931 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003932 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003933 try {
3934 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003935 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003936 } catch (Exception e) {
3937 if (!mInstrumentation.onException(r.activity, e)) {
3938 throw new RuntimeException(
3939 "Unable to stop activity "
3940 + r.intent.getComponent().toShortString()
3941 + ": " + e.toString(), e);
3942 }
3943 }
3944 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003945 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3946 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003947 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003948
3949 // Make sure any pending writes are now committed.
3950 if (!r.isPreHoneycomb()) {
3951 QueuedWork.waitToFinish();
3952 }
3953
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003954 // Tell activity manager we slept.
3955 try {
3956 ActivityManagerNative.getDefault().activitySlept(r.token);
3957 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003958 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003959 }
3960 } else {
3961 if (r.stopped && r.activity.mVisibleFromServer) {
3962 r.activity.performRestart();
3963 r.stopped = false;
3964 }
3965 }
3966 }
3967
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003968 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003969 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003970 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003971 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003972 onCoreSettingsChange();
3973 }
3974
3975 private void onCoreSettingsChange() {
3976 boolean debugViewAttributes =
3977 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3978 if (debugViewAttributes != View.mDebugViewAttributes) {
3979 View.mDebugViewAttributes = debugViewAttributes;
3980
3981 // request all activities to relaunch for the changes to take place
3982 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003983 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
3984 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07003985 }
3986 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003987 }
3988
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003989 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3990 LoadedApk apk = peekPackageInfo(data.pkg, false);
3991 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003992 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003993 }
3994 apk = peekPackageInfo(data.pkg, true);
3995 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003996 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003997 }
3998 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003999 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004000 }
4001
Romain Guy65b345f2011-07-27 18:51:50 -07004002 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 final int N = results.size();
4004 for (int i=0; i<N; i++) {
4005 ResultInfo ri = results.get(i);
4006 try {
4007 if (ri.mData != null) {
4008 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004009 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004011 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004012 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 r.activity.dispatchActivityResult(ri.mResultWho,
4014 ri.mRequestCode, ri.mResultCode, ri.mData);
4015 } catch (Exception e) {
4016 if (!mInstrumentation.onException(r.activity, e)) {
4017 throw new RuntimeException(
4018 "Failure delivering result " + ri + " to activity "
4019 + r.intent.getComponent().toShortString()
4020 + ": " + e.toString(), e);
4021 }
4022 }
4023 }
4024 }
4025
Romain Guy65b345f2011-07-27 18:51:50 -07004026 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004027 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004028 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 if (r != null) {
4030 final boolean resumed = !r.paused;
4031 if (!r.activity.mFinished && r.activity.mDecor != null
4032 && r.hideForNow && resumed) {
4033 // We had hidden the activity because it started another
4034 // one... we have gotten a result back and we are not
4035 // paused, so make sure our window is visible.
4036 updateVisibility(r, true);
4037 }
4038 if (resumed) {
4039 try {
4040 // Now we are idle.
4041 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004042 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004043 mInstrumentation.callActivityOnPause(r.activity);
4044 if (!r.activity.mCalled) {
4045 throw new SuperNotCalledException(
4046 "Activity " + r.intent.getComponent().toShortString()
4047 + " did not call through to super.onPause()");
4048 }
4049 } catch (SuperNotCalledException e) {
4050 throw e;
4051 } catch (Exception e) {
4052 if (!mInstrumentation.onException(r.activity, e)) {
4053 throw new RuntimeException(
4054 "Unable to pause activity "
4055 + r.intent.getComponent().toShortString()
4056 + ": " + e.toString(), e);
4057 }
4058 }
4059 }
4060 deliverResults(r, res.results);
4061 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004062 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004063 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 }
4065 }
4066 }
4067
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004068 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 return performDestroyActivity(token, finishing, 0, false);
4070 }
4071
Romain Guy65b345f2011-07-27 18:51:50 -07004072 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004074 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004075 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004076 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004078 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 r.activity.mConfigChangeFlags |= configChanges;
4080 if (finishing) {
4081 r.activity.mFinished = true;
4082 }
4083 if (!r.paused) {
4084 try {
4085 r.activity.mCalled = false;
4086 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08004087 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004088 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 if (!r.activity.mCalled) {
4090 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004091 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 + " did not call through to super.onPause()");
4093 }
4094 } catch (SuperNotCalledException e) {
4095 throw e;
4096 } catch (Exception e) {
4097 if (!mInstrumentation.onException(r.activity, e)) {
4098 throw new RuntimeException(
4099 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004100 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004101 + ": " + e.toString(), e);
4102 }
4103 }
4104 r.paused = true;
4105 }
4106 if (!r.stopped) {
4107 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004108 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 } catch (SuperNotCalledException e) {
4110 throw e;
4111 } catch (Exception e) {
4112 if (!mInstrumentation.onException(r.activity, e)) {
4113 throw new RuntimeException(
4114 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004115 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 + ": " + e.toString(), e);
4117 }
4118 }
4119 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004120 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4121 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 }
4123 if (getNonConfigInstance) {
4124 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004125 r.lastNonConfigurationInstances
4126 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 } catch (Exception e) {
4128 if (!mInstrumentation.onException(r.activity, e)) {
4129 throw new RuntimeException(
4130 "Unable to retain activity "
4131 + r.intent.getComponent().toShortString()
4132 + ": " + e.toString(), e);
4133 }
4134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
4136 try {
4137 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004138 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 if (!r.activity.mCalled) {
4140 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004141 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 " did not call through to super.onDestroy()");
4143 }
4144 if (r.window != null) {
4145 r.window.closeAllPanels();
4146 }
4147 } catch (SuperNotCalledException e) {
4148 throw e;
4149 } catch (Exception e) {
4150 if (!mInstrumentation.onException(r.activity, e)) {
4151 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004152 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4153 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 }
4155 }
4156 }
4157 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004158 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 return r;
4160 }
4161
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004162 private static String safeToComponentShortString(Intent intent) {
4163 ComponentName component = intent.getComponent();
4164 return component == null ? "[Unknown]" : component.toShortString();
4165 }
4166
Romain Guy65b345f2011-07-27 18:51:50 -07004167 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004169 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 configChanges, getNonConfigInstance);
4171 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004172 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 WindowManager wm = r.activity.getWindowManager();
4174 View v = r.activity.mDecor;
4175 if (v != null) {
4176 if (r.activity.mVisibleFromServer) {
4177 mNumVisibleActivities--;
4178 }
4179 IBinder wtoken = v.getWindowToken();
4180 if (r.activity.mWindowAdded) {
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004181 if (r.onlyLocalRequest || r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004182 // Hold off on removing this until the new activity's
4183 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004184 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004185 r.mPendingRemoveWindowManager = wm;
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004186 if (r.mPreserveWindow) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004187 // We can only keep the part of the view hierarchy that we control,
4188 // everything else must be removed, because it might not be able to
4189 // behave properly when activity is relaunching.
4190 r.window.clearContentView();
4191 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004192 } else {
4193 wm.removeViewImmediate(v);
4194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004196 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004197 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004198 r.activity.getClass().getName(), "Activity");
4199 }
4200 r.activity.mDecor = null;
4201 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004202 if (r.mPendingRemoveWindow == null) {
4203 // If we are delaying the removal of the activity window, then
4204 // we can't clean up all windows here. Note that we can't do
4205 // so later either, which means any windows that aren't closed
4206 // by the app will leak. Well we try to warning them a lot
4207 // about leaking windows, because that is a bug, so if they are
4208 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004209 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004210 r.activity.getClass().getName(), "Activity");
4211 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212
4213 // Mocked out contexts won't be participating in the normal
4214 // process lifecycle, but if we're running with a proper
4215 // ApplicationContext we need to have it tear down things
4216 // cleanly.
4217 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004218 if (c instanceof ContextImpl) {
4219 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004220 r.activity.getClass().getName(), "Activity");
4221 }
4222 }
4223 if (finishing) {
4224 try {
4225 ActivityManagerNative.getDefault().activityDestroyed(token);
4226 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004227 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 }
4229 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004230 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 }
4232
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004233 /**
4234 * @param preserveWindow Whether the activity should try to reuse the window it created,
4235 * including the decor view after the relaunch.
4236 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004237 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004238 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004239 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004240 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004241 ActivityClientRecord target = null;
4242
Craig Mautner88c05892013-06-28 09:47:45 -07004243 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004244 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4245 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004246 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004247 if (r.token == token) {
4248 target = r;
4249 if (pendingResults != null) {
4250 if (r.pendingResults != null) {
4251 r.pendingResults.addAll(pendingResults);
4252 } else {
4253 r.pendingResults = pendingResults;
4254 }
4255 }
4256 if (pendingNewIntents != null) {
4257 if (r.pendingIntents != null) {
4258 r.pendingIntents.addAll(pendingNewIntents);
4259 } else {
4260 r.pendingIntents = pendingNewIntents;
4261 }
4262 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004263
4264 // For each relaunch request, activity manager expects an answer
4265 if (!r.onlyLocalRequest && fromServer) {
4266 try {
4267 ActivityManagerNative.getDefault().activityRelaunched(token);
4268 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004269 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004270 }
4271 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004272 break;
4273 }
4274 }
4275
4276 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004277 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4278 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004279 target = new ActivityClientRecord();
4280 target.token = token;
4281 target.pendingResults = pendingResults;
4282 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004283 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004284 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004285 final ActivityClientRecord existing = mActivities.get(token);
4286 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004287 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004288 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4289 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004290 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004291 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004292 }
4293 target.onlyLocalRequest = true;
4294 }
4295 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004296 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004297 }
4298
4299 if (fromServer) {
4300 target.startsNotResumed = notResumed;
4301 target.onlyLocalRequest = false;
4302 }
4303 if (config != null) {
4304 target.createdConfig = config;
4305 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004306 if (overrideConfig != null) {
4307 target.overrideConfig = overrideConfig;
4308 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004309 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004310 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004311 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004312 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4313 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004314 }
4315
Romain Guy65b345f2011-07-27 18:51:50 -07004316 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004317 // If we are getting ready to gc after going to the background, well
4318 // we are back active so skip it.
4319 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004320 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321
4322 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004323 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 // First: make sure we have the most recent configuration and most
4326 // recent version of the activity, or skip it if some previous call
4327 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004328 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 int N = mRelaunchingActivities.size();
4330 IBinder token = tmp.token;
4331 tmp = null;
4332 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004333 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 if (r.token == token) {
4335 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004336 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 mRelaunchingActivities.remove(i);
4338 i--;
4339 N--;
4340 }
4341 }
Bob Leee5408332009-09-04 18:31:17 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004344 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 return;
4346 }
Bob Leee5408332009-09-04 18:31:17 -07004347
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004348 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4349 + tmp.token + " with configChanges=0x"
4350 + Integer.toHexString(configChanges));
4351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 if (mPendingConfiguration != null) {
4353 changedConfig = mPendingConfiguration;
4354 mPendingConfiguration = null;
4355 }
4356 }
Bob Leee5408332009-09-04 18:31:17 -07004357
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004358 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4359 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4360 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4361 } else {
4362 tmp.lastProcessedSeq = tmp.relaunchSeq;
4363 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004364 if (tmp.createdConfig != null) {
4365 // If the activity manager is passing us its current config,
4366 // assume that is really what we want regardless of what we
4367 // may have pending.
4368 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004369 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4370 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4371 if (changedConfig == null
4372 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4373 changedConfig = tmp.createdConfig;
4374 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004375 }
4376 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004377
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004378 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004379 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 // If there was a pending configuration change, execute it first.
4382 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004383 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004384 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004385 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 }
Bob Leee5408332009-09-04 18:31:17 -07004387
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004388 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004389 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004391 if (!tmp.onlyLocalRequest) {
4392 try {
4393 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4394 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004395 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004396 }
4397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 return;
4399 }
Bob Leee5408332009-09-04 18:31:17 -07004400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004402 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004403 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004404 r.lastProcessedSeq = tmp.lastProcessedSeq;
4405 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004406 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004407
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004408 r.activity.mChangingConfigurations = true;
4409
Robert Carr23fa16b2016-01-13 13:19:58 -08004410 // If we are preserving the main window across relaunches we would also like to preserve
4411 // the children. However the client side view system does not support preserving
4412 // the child views so we notify the window manager to expect these windows to
4413 // be replaced and defer requests to destroy or hide them. This way we can achieve
4414 // visual continuity. It's important that we do this here prior to pause and destroy
4415 // as that is when we may hide or remove the child views.
4416 try {
4417 if (r.mPreserveWindow) {
4418 WindowManagerGlobal.getWindowSession().prepareToReplaceChildren(r.token);
4419 }
4420 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004421 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004422 }
4423
4424
Dianne Hackborne2b04802010-12-09 09:24:55 -08004425 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004426 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004427 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004428 }
4429 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004430 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004431 }
Bob Leee5408332009-09-04 18:31:17 -07004432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004433 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004434
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 r.activity = null;
4436 r.window = null;
4437 r.hideForNow = false;
4438 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004439 // Merge any pending results and pending intents; don't just replace them
4440 if (tmp.pendingResults != null) {
4441 if (r.pendingResults == null) {
4442 r.pendingResults = tmp.pendingResults;
4443 } else {
4444 r.pendingResults.addAll(tmp.pendingResults);
4445 }
4446 }
4447 if (tmp.pendingIntents != null) {
4448 if (r.pendingIntents == null) {
4449 r.pendingIntents = tmp.pendingIntents;
4450 } else {
4451 r.pendingIntents.addAll(tmp.pendingIntents);
4452 }
4453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004455 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004456
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004457 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004458
4459 if (!tmp.onlyLocalRequest) {
4460 try {
4461 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004462 if (r.window != null) {
4463 r.window.reportActivityRelaunched();
4464 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004465 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004466 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004467 }
4468 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 }
4470
Craig Mautnera0026042014-04-23 11:45:37 -07004471 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4472 r.state = new Bundle();
4473 r.state.setAllowFds(false);
4474 if (r.isPersistable()) {
4475 r.persistentState = new PersistableBundle();
4476 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4477 r.persistentState);
4478 } else {
4479 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4480 }
4481 }
4482
Dianne Hackborn73c14162012-09-19 15:45:06 -07004483 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004485 ArrayList<ComponentCallbacks2> callbacks
4486 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004487
Craig Mautner88c05892013-06-28 09:47:45 -07004488 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004489 final int NAPP = mAllApplications.size();
4490 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004491 callbacks.add(mAllApplications.get(i));
4492 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004493 final int NACT = mActivities.size();
4494 for (int i=0; i<NACT; i++) {
4495 ActivityClientRecord ar = mActivities.valueAt(i);
4496 Activity a = ar.activity;
4497 if (a != null) {
4498 Configuration thisConfig = applyConfigCompatMainThread(
4499 mCurDefaultDisplayDpi, newConfig,
4500 ar.packageInfo.getCompatibilityInfo());
4501 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4502 // If the activity is currently resumed, its configuration
4503 // needs to change right now.
4504 callbacks.add(a);
4505 } else if (thisConfig != null) {
4506 // Otherwise, we will tell it about the change
4507 // the next time it is resumed or shown. Note that
4508 // the activity manager may, before then, decide the
4509 // activity needs to be destroyed to handle its new
4510 // configuration.
4511 if (DEBUG_CONFIGURATION) {
4512 Slog.v(TAG, "Setting activity "
4513 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004514 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004515 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 }
4517 }
4518 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004519 final int NSVC = mServices.size();
4520 for (int i=0; i<NSVC; i++) {
4521 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004522 }
4523 }
4524 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004525 final int NPRV = mLocalProviders.size();
4526 for (int i=0; i<NPRV; i++) {
4527 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 }
4529 }
Bob Leee5408332009-09-04 18:31:17 -07004530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 return callbacks;
4532 }
Bob Leee5408332009-09-04 18:31:17 -07004533
Filip Gruszczynskica664812015-12-04 12:43:36 -08004534 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config,
4535 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004537 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 // we check the runtime type and act accordingly.
4539 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4540 if (activity != null) {
4541 activity.mCalled = false;
4542 }
Bob Leee5408332009-09-04 18:31:17 -07004543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004544 boolean shouldChangeConfig = false;
4545 if ((activity == null) || (activity.mCurrentConfig == null)) {
4546 shouldChangeConfig = true;
4547 } else {
Bob Leee5408332009-09-04 18:31:17 -07004548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 // If the new config is the same as the config this Activity
4550 // is already running with then don't bother calling
4551 // onConfigurationChanged
4552 int diff = activity.mCurrentConfig.diff(config);
4553 if (diff != 0) {
Wale Ogunwale14e811f2015-12-14 15:04:22 -08004554 // If this activity doesn't handle any of the config changes then don't bother
4555 // calling onConfigurationChanged as we're going to destroy it.
4556 // Except in the case where the configuration changed on the activity manager side,
4557 // but wasn't big enough to cause a resource change so the activity wasn't destroyed.
4558 // In this case we still want to change the configuration of the activity but not
4559 // report it to the app.
4560 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4561 || !reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 shouldChangeConfig = true;
4563 }
4564 }
4565 }
Bob Leee5408332009-09-04 18:31:17 -07004566
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004567 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004568 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004569 if (shouldChangeConfig) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004570 if (reportToActivity) {
4571 cb.onConfigurationChanged(config);
4572 }
Bob Leee5408332009-09-04 18:31:17 -07004573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004575 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004576 throw new SuperNotCalledException(
4577 "Activity " + activity.getLocalClassName() +
4578 " did not call through to super.onConfigurationChanged()");
4579 }
4580 activity.mConfigChangeFlags = 0;
4581 activity.mCurrentConfig = new Configuration(config);
4582 }
4583 }
4584 }
4585
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004586 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004587 synchronized (mResourcesManager) {
4588 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004589 }
4590 }
4591
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004592 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004593 Configuration config = mConfiguration;
4594 if (mCompatConfiguration == null) {
4595 mCompatConfiguration = new Configuration();
4596 }
4597 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004598 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004599 config = mCompatConfiguration;
4600 }
4601 return config;
4602 }
4603
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004604 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004605
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004606 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004607
Craig Mautner88c05892013-06-28 09:47:45 -07004608 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004609 if (mPendingConfiguration != null) {
4610 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4611 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004612 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004613 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004614 }
4615 mPendingConfiguration = null;
4616 }
4617
4618 if (config == null) {
4619 return;
4620 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004621
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004622 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004623 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004624
4625 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 if (mConfiguration == null) {
4628 mConfiguration = new Configuration();
4629 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004630 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004631 return;
4632 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004633
4634 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004635 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004636
4637 final Theme systemTheme = getSystemContext().getTheme();
4638 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4639 systemTheme.rebase();
4640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004641 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004642
4643 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4644
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004645 freeTextLayoutCachesIfNeeded(configDiff);
4646
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004647 if (callbacks != null) {
4648 final int N = callbacks.size();
4649 for (int i=0; i<N; i++) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004650 performConfigurationChanged(callbacks.get(i), config, REPORT_TO_ACTIVITY);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004651 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 }
4653 }
4654
Romain Guy46bfc482013-08-16 18:38:29 -07004655 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004656 if (configDiff != 0) {
4657 // Ask text layout engine to free its caches if there is a locale change
4658 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4659 if (hasLocaleConfigChange) {
4660 Canvas.freeTextLayoutCaches();
4661 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4662 }
4663 }
4664 }
4665
Filip Gruszczynskica664812015-12-04 12:43:36 -08004666 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4667 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004668 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 if (r == null || r.activity == null) {
4670 return;
4671 }
Bob Leee5408332009-09-04 18:31:17 -07004672
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004673 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004674 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004675
4676 r.tmpConfig.setTo(mCompatConfiguration);
4677 if (data.overrideConfig != null) {
4678 r.overrideConfig = data.overrideConfig;
4679 r.tmpConfig.updateFrom(data.overrideConfig);
4680 }
Filip Gruszczynskica664812015-12-04 12:43:36 -08004681 performConfigurationChanged(r.activity, r.tmpConfig, reportToActivity);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004682
4683 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004684
4685 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
4687
Jeff Hao1b012d32014-08-20 10:35:34 -07004688 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004689 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004690 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004691 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004692 default:
4693 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004694 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004695 break;
4696 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004697 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004698 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004699 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004700 } finally {
4701 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004702 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004703 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004704 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004705 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004706 }
4707 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004708 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004709 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004710 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004711 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004712 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004713 }
4714 }
Bob Leee5408332009-09-04 18:31:17 -07004715
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004716 /**
4717 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4718 * so that profiler data won't be lost.
4719 *
4720 * @hide
4721 */
4722 public void stopProfiling() {
4723 mProfiler.stopProfiling();
4724 }
4725
Romain Guya998dff2012-03-23 18:58:36 -07004726 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004727 if (managed) {
4728 try {
4729 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4730 } catch (IOException e) {
4731 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4732 + " -- can the process access this path?");
4733 } finally {
4734 try {
4735 dhd.fd.close();
4736 } catch (IOException e) {
4737 Slog.w(TAG, "Failure closing profile fd", e);
4738 }
4739 }
4740 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004741 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004742 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004743 try {
4744 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4745 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004746 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004747 }
Andy McFadden824c5102010-07-09 16:26:57 -07004748 }
4749
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004750 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4751 boolean hasPkgInfo = false;
4752 if (packages != null) {
mark_chen89764e32014-12-12 15:38:48 +08004753 synchronized (mResourcesManager) {
4754 for (int i=packages.length-1; i>=0; i--) {
4755 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4756 if (!hasPkgInfo) {
4757 WeakReference<LoadedApk> ref;
4758 ref = mPackages.get(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004759 if (ref != null && ref.get() != null) {
4760 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004761 } else {
4762 ref = mResourcePackages.get(packages[i]);
4763 if (ref != null && ref.get() != null) {
4764 hasPkgInfo = true;
4765 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004766 }
4767 }
mark_chen89764e32014-12-12 15:38:48 +08004768 mPackages.remove(packages[i]);
4769 mResourcePackages.remove(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004770 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004771 }
4772 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004773 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004774 hasPkgInfo);
4775 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004777 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004778 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004780 final int N = callbacks.size();
4781 for (int i=0; i<N; i++) {
4782 callbacks.get(i).onLowMemory();
4783 }
4784
Chris Tatece229052009-03-25 16:44:52 -07004785 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4786 if (Process.myUid() != Process.SYSTEM_UID) {
4787 int sqliteReleased = SQLiteDatabase.releaseMemory();
4788 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4789 }
Bob Leee5408332009-09-04 18:31:17 -07004790
Mike Reedcaf0df12009-04-27 14:32:05 -04004791 // Ask graphics to free up as much as possible (font/image caches)
4792 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004794 // Ask text layout engine to free also as much as possible
4795 Canvas.freeTextLayoutCaches();
4796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 BinderInternal.forceGc("mem");
4798 }
4799
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004800 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004801 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004802
Dianne Hackborn73c14162012-09-19 15:45:06 -07004803 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004804
4805 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004806 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004807 callbacks.get(i).onTrimMemory(level);
4808 }
Romain Guy19f86e82012-04-23 15:19:07 -07004809
John Reckf47a5942014-06-30 16:20:04 -07004810 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004811 }
4812
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004813 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004814 if (Process.isIsolated()) {
4815 // Isolated processes aren't going to do UI.
4816 return;
4817 }
Romain Guya9582652011-11-10 14:20:10 -08004818 try {
4819 int uid = Process.myUid();
4820 String[] packages = getPackageManager().getPackagesForUid(uid);
4821
4822 // If there are several packages in this application we won't
Tim Murraye1e6c662015-04-07 13:24:14 -07004823 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004824 if (packages != null && packages.length == 1) {
John Reck51aaf902015-12-02 15:08:07 -08004825 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004826 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004827 }
4828 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004829 throw e.rethrowFromSystemServer();
Romain Guya9582652011-11-10 14:20:10 -08004830 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004831 }
4832
Calin Juravled96d2dd2016-02-02 23:28:39 +00004833 // Keep in sync with installd (frameworks/native/cmds/installd/commands.cpp).
4834 private static File getPrimaryProfileFile(String packageName) {
Calin Juravled479b522016-02-24 16:22:03 +00004835 File profileDir = Environment.getDataProfilesDePackageDirectory(
4836 UserHandle.myUserId(), packageName);
4837 return new File(profileDir, "primary.prof");
Calin Juravled96d2dd2016-02-02 23:28:39 +00004838 }
4839
Calin Juravle69052392015-12-17 17:06:49 +02004840 private static void setupJitProfileSupport(LoadedApk loadedApk, File cacheDir) {
Calin Juravledb4a79a2015-12-23 18:55:08 +02004841 if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
4842 return;
4843 }
Calin Juravle69052392015-12-17 17:06:49 +02004844 final ApplicationInfo appInfo = loadedApk.getApplicationInfo();
Calin Juravle69052392015-12-17 17:06:49 +02004845 final List<String> codePaths = new ArrayList<>();
4846 if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
4847 codePaths.add(appInfo.sourceDir);
4848 }
4849 if (appInfo.splitSourceDirs != null) {
4850 Collections.addAll(codePaths, appInfo.splitSourceDirs);
4851 }
4852
4853 if (codePaths.isEmpty()) {
4854 // If there are no code paths there's no need to setup a profile file and register with
4855 // the runtime,
4856 return;
4857 }
4858
Calin Juravled96d2dd2016-02-02 23:28:39 +00004859 final File profileFile = getPrimaryProfileFile(loadedApk.mPackageName);
Calin Juravle69052392015-12-17 17:06:49 +02004860 if (!profileFile.exists()) {
4861 FileDescriptor fd = null;
4862 try {
4863 final int permissions = 0600; // read-write for user.
4864 fd = Os.open(profileFile.getAbsolutePath(), OsConstants.O_CREAT, permissions);
4865 Os.fchmod(fd, permissions);
4866 Os.fchown(fd, appInfo.uid, appInfo.uid);
4867 } catch (ErrnoException e) {
Calin Juravled96d2dd2016-02-02 23:28:39 +00004868 Log.v(TAG, "Unable to create jit profile file " + profileFile, e);
Calin Juravle69052392015-12-17 17:06:49 +02004869 try {
4870 Os.unlink(profileFile.getAbsolutePath());
4871 } catch (ErrnoException unlinkErr) {
4872 Log.v(TAG, "Unable to unlink jit profile file " + profileFile, unlinkErr);
4873 }
4874 return;
4875 } finally {
4876 IoUtils.closeQuietly(fd);
4877 }
4878 }
4879
Calin Juravled479b522016-02-24 16:22:03 +00004880 final File foreignDexProfilesFile =
4881 Environment.getDataProfilesDeForeignDexDirectory(UserHandle.myUserId());
4882 String foreignDexProfilesPath = null;
4883 if (!foreignDexProfilesFile.exists()) {
4884 Log.v(TAG, "ForeignDexProfilesPath does not exists:" +
4885 foreignDexProfilesFile.getPath());
4886 } else {
4887 foreignDexProfilesPath = foreignDexProfilesFile.getAbsolutePath();
4888 }
Calin Juravle69052392015-12-17 17:06:49 +02004889 VMRuntime.registerAppInfo(profileFile.getAbsolutePath(), appInfo.dataDir,
Calin Juravled479b522016-02-24 16:22:03 +00004890 codePaths.toArray(new String[codePaths.size()]), foreignDexProfilesPath);
Calin Juravle69052392015-12-17 17:06:49 +02004891 }
4892
Dianne Hackborndde331c2012-08-03 14:01:57 -07004893 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05004894 final int densityDpi = mCurDefaultDisplayDpi;
4895 if (!mDensityCompatMode
4896 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
4897 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
4898 DisplayMetrics.DENSITY_DEVICE = densityDpi;
4899 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07004900 }
4901 }
4902
Romain Guy65b345f2011-07-27 18:51:50 -07004903 private void handleBindApplication(AppBindData data) {
Man Caocfa78b22015-06-11 20:14:34 -07004904 if (data.trackAllocation) {
4905 DdmVmInternal.enableRecentAllocations(true);
4906 }
4907
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 mBoundApplication = data;
4909 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004910 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004912 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004913 if (data.initProfilerInfo != null) {
4914 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4915 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4916 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4917 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4918 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004921 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004922 android.ddm.DdmHandleAppName.setAppName(data.processName,
4923 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004924
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004925 if (data.persistent) {
4926 // Persistent processes on low-memory devices do not get to
4927 // use hardware accelerated drawing, since this can add too much
4928 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004929 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08004930 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004931 }
4932 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004933
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004934 if (mProfiler.profileFd != null) {
4935 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004936 }
4937
Joe Onoratod630f102011-03-17 18:42:26 -07004938 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4939 // implementation to use the pool executor. Normally, we use the
4940 // serialized executor as the default. This has to happen in the
4941 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004942 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004943 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4944 }
4945
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004946 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004948 /*
4949 * Before spawning a new process, reset the time zone to be the system time zone.
4950 * This needs to be done because the system time zone could have changed after the
4951 * the spawning of this process. Without doing this this process would have the incorrect
4952 * system time zone.
4953 */
4954 TimeZone.setDefault(null);
4955
4956 /*
Seigo Nonakacada57a2016-01-22 17:22:11 +09004957 * Initialize the default locales in this process for the reasons we set the time zone.
Roozbeh Pournader834641b2016-01-23 22:34:57 -08004958 *
4959 * We do this through ResourcesManager, since we need to do locale negotiation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004960 */
Roozbeh Pournader834641b2016-01-23 22:34:57 -08004961 mResourcesManager.setDefaultLocalesLocked(data.config.getLocales());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004963 /*
4964 * Update the system configuration since its preloaded and might not
4965 * reflect configuration changes. The configuration object passed
4966 * in AppBindData can be safely assumed to be up to date
4967 */
Craig Mautner88c05892013-06-28 09:47:45 -07004968 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004969 mCurDefaultDisplayDpi = data.config.densityDpi;
4970 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004971
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004972 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004973
Dianne Hackborndde331c2012-08-03 14:01:57 -07004974 /**
4975 * Switch this process to density compatibility mode if needed.
4976 */
4977 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4978 == 0) {
4979 mDensityCompatMode = true;
4980 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4981 }
4982 updateDefaultDensity();
4983
Narayan Kamathccb2a0862013-12-19 14:49:36 +00004984 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4985 DateFormat.set24HourTimePref(is24Hr);
4986
Jon Miranda836c0a82014-08-11 12:32:26 -07004987 View.mDebugViewAttributes =
4988 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4989
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004990 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004991 * For system applications on userdebug/eng builds, log stack
4992 * traces of disk and network access to dropbox for analysis.
4993 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004994 if ((data.appInfo.flags &
4995 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004996 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4997 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004998 }
4999
5000 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005001 * For apps targetting Honeycomb or later, we don't allow network usage
5002 * on the main event loop / UI thread. This is what ultimately throws
5003 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005004 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005005 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005006 StrictMode.enableDeathOnNetwork();
5007 }
5008
Jeff Sharkey344744b2016-01-28 19:03:30 -07005009 /**
5010 * For apps targetting N or later, we don't allow file:// Uri exposure.
5011 * This is what ultimately throws {@link FileUriExposedException}.
5012 */
5013 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005014 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005015 }
5016
Alex Klyubinf9034cc2015-02-12 11:43:09 -08005017 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
5018 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
5019
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5021 // XXX should have option to change the port.
5022 Debug.changeDebugPort(8100);
5023 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005024 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005025 + " is waiting for the debugger on port 8100...");
5026
5027 IActivityManager mgr = ActivityManagerNative.getDefault();
5028 try {
5029 mgr.showWaitingForDebugger(mAppThread, true);
5030 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005031 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 }
5033
5034 Debug.waitForDebugger();
5035
5036 try {
5037 mgr.showWaitingForDebugger(mAppThread, false);
5038 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005039 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005040 }
5041
5042 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005043 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 + " can be debugged on port 8100...");
5045 }
5046 }
5047
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005048 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005049 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5050 Trace.setAppTracingAllowed(isAppDebuggable);
5051 if (isAppDebuggable && data.enableBinderTracking) {
5052 Binder.enableTracing();
5053 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005054
Robert Greenwalt434203a2010-10-11 16:00:27 -07005055 /**
5056 * Initialize the default http proxy in this process for the reasons we set the time zone.
5057 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04005058 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005059 if (b != null) {
5060 // In pre-boot mode (doing initial launch to collect password), not
5061 // all system is up. This includes the connectivity service, so don't
5062 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005063 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005064 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005065 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005066 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005067 } catch (RemoteException e) {
5068 throw e.rethrowFromSystemServer();
5069 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005070 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07005071
Alan Viverette2107d692015-09-03 14:55:27 -04005072 // Instrumentation info affects the class loader, so load it before
5073 // setting up the app context.
5074 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005077 ii = new ApplicationPackageManager(null, getPackageManager())
5078 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005081 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 }
5083
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005084 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005085 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005086 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
5087 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005089 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5090 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005091 } else {
5092 ii = null;
5093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094
Alan Viverette2107d692015-09-03 14:55:27 -04005095 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005096 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005097 // This cache location probably points at credential-encrypted
5098 // storage which may not be accessible yet; assign it anyway instead
5099 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005100 final File cacheDir = appContext.getCacheDir();
5101 if (cacheDir != null) {
5102 // Provide a usable directory for temporary files
5103 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5104 } else {
5105 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5106 + "due to missing cache directory");
5107 }
Alan Viverette346296b2015-09-01 13:08:05 -04005108
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005109 // Setup a location to store generated/compiled graphics code and
5110 // JIT profiling data. Note that this data is stored in a
5111 // device-encrypted storage area, so these caches must never contain
5112 // user sensitive user data.
5113 final Context deviceContext = appContext.createDeviceEncryptedStorageContext();
5114 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005115 if (codeCacheDir != null) {
5116 setupGraphicsSupport(data.info, codeCacheDir);
Calin Juravlec74d3382016-01-07 12:15:36 +00005117 setupJitProfileSupport(data.info, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005118 } else {
Calin Juravlec74d3382016-01-07 12:15:36 +00005119 Log.e(TAG, "Unable to setupGraphicsSupport and setupJitProfileSupport " +
5120 "due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005121 }
Alan Viverette2107d692015-09-03 14:55:27 -04005122
Michael Lentine2ba303f2016-02-01 20:44:34 -06005123 // Add the lib dir path to hardware renderer so that vulkan layers
5124 // can be searched for within that directory.
5125 ThreadedRenderer.setLibDir(data.info.getLibDir());
5126 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005127
Chad Brubaker78d47122015-11-17 22:26:58 -08005128 // Install the Network Security Config Provider. This must happen before the application
5129 // code is loaded to prevent issues with instances of TLS objects being created before
5130 // the provider is installed.
5131 NetworkSecurityConfigProvider.install(appContext);
5132
Alan Viverette2107d692015-09-03 14:55:27 -04005133 // Continue loading instrumentation.
5134 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005135 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005136 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005137 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005138 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005139 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005140 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005141
5142 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005143 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 mInstrumentation = (Instrumentation)
5145 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5146 } catch (Exception e) {
5147 throw new RuntimeException(
5148 "Unable to instantiate instrumentation "
5149 + data.instrumentationName + ": " + e.toString(), e);
5150 }
5151
Alan Viverettebe64eae2015-09-03 14:56:04 -04005152 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5153 mInstrumentation.init(this, instrContext, appContext, component,
5154 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005156 if (mProfiler.profileFile != null && !ii.handleProfiling
5157 && mProfiler.profileFd == null) {
5158 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005159 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 file.getParentFile().mkdirs();
5161 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5162 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005163 } else {
5164 mInstrumentation = new Instrumentation();
5165 }
5166
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005167 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005168 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005169 } else {
5170 // Small heap, clamp to the current growth limit and let the heap release
5171 // pages after the growth limit to the non growth limit capacity. b/18387825
5172 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005173 }
5174
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005175 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005176 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005177 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005178 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005180 // If the app is being launched for full backup or restore, bring it up in
5181 // a restricted environment with the base application class.
5182 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5183 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005184
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005185 // don't bring up providers in restricted mode; they may depend on the
5186 // app's custom Application class
5187 if (!data.restrictedBackupMode) {
5188 List<ProviderInfo> providers = data.providers;
5189 if (providers != null) {
5190 installContentProviders(app, providers);
5191 // For process that contains content providers, we want to
5192 // ensure that the JIT is enabled "at some point".
5193 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5194 }
5195 }
5196
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005197 // Do this after providers, since instrumentation tests generally start their
5198 // test thread at this point, and we don't want that racing.
5199 try {
5200 mInstrumentation.onCreate(data.instrumentationArgs);
5201 }
5202 catch (Exception e) {
5203 throw new RuntimeException(
5204 "Exception thrown in onCreate() of "
5205 + data.instrumentationName + ": " + e.toString(), e);
5206 }
5207
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005208 try {
5209 mInstrumentation.callApplicationOnCreate(app);
5210 } catch (Exception e) {
5211 if (!mInstrumentation.onException(app, e)) {
5212 throw new RuntimeException(
5213 "Unable to create application " + app.getClass().getName()
5214 + ": " + e.toString(), e);
5215 }
5216 }
5217 } finally {
5218 StrictMode.setThreadPolicy(savedPolicy);
5219 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005220 }
5221
5222 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5223 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005224 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5225 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226 Debug.stopMethodTracing();
5227 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005228 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 // + ", app thr: " + mAppThread);
5230 try {
5231 am.finishInstrumentation(mAppThread, resultCode, results);
5232 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005233 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 }
5235 }
5236
Romain Guy65b345f2011-07-27 18:51:50 -07005237 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 Context context, List<ProviderInfo> providers) {
5239 final ArrayList<IActivityManager.ContentProviderHolder> results =
5240 new ArrayList<IActivityManager.ContentProviderHolder>();
5241
Romain Guya998dff2012-03-23 18:58:36 -07005242 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005243 if (DEBUG_PROVIDER) {
5244 StringBuilder buf = new StringBuilder(128);
5245 buf.append("Pub ");
5246 buf.append(cpi.authority);
5247 buf.append(": ");
5248 buf.append(cpi.name);
5249 Log.i(TAG, buf.toString());
5250 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005251 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5252 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5253 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005254 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 }
5257 }
5258
5259 try {
5260 ActivityManagerNative.getDefault().publishContentProviders(
5261 getApplicationThread(), results);
5262 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005263 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 }
5265 }
5266
Jeff Sharkey6d515712012-09-20 16:06:08 -07005267 public final IContentProvider acquireProvider(
5268 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005269 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005270 if (provider != null) {
5271 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 }
5273
Wale Ogunwale1d646122015-04-24 14:45:14 -07005274 // There is a possible race here. Another thread may try to acquire
5275 // the same provider at the same time. When this happens, we want to ensure
5276 // that the first one wins.
5277 // Note that we cannot hold the lock while acquiring and installing the
5278 // provider since it might take a long time to run and it could also potentially
5279 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005281 try {
5282 holder = ActivityManagerNative.getDefault().getContentProvider(
5283 getApplicationThread(), auth, userId, stable);
5284 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005285 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005288 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 return null;
5290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005292 // Install provider will increment the reference count for us, and break
5293 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005294 holder = installProvider(c, holder, holder.info,
5295 true /*noisy*/, holder.noReleaseNeeded, stable);
5296 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 }
5298
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005299 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5300 if (stable) {
5301 prc.stableCount += 1;
5302 if (prc.stableCount == 1) {
5303 // We are acquiring a new stable reference on the provider.
5304 int unstableDelta;
5305 if (prc.removePending) {
5306 // We have a pending remove operation, which is holding the
5307 // last unstable reference. At this point we are converting
5308 // that unstable reference to our new stable reference.
5309 unstableDelta = -1;
5310 // Cancel the removal of the provider.
5311 if (DEBUG_PROVIDER) {
5312 Slog.v(TAG, "incProviderRef: stable "
5313 + "snatched provider from the jaws of death");
5314 }
5315 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005316 // There is a race! It fails to remove the message, which
5317 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005318 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5319 } else {
5320 unstableDelta = 0;
5321 }
5322 try {
5323 if (DEBUG_PROVIDER) {
5324 Slog.v(TAG, "incProviderRef Now stable - "
5325 + prc.holder.info.name + ": unstableDelta="
5326 + unstableDelta);
5327 }
5328 ActivityManagerNative.getDefault().refContentProvider(
5329 prc.holder.connection, 1, unstableDelta);
5330 } catch (RemoteException e) {
5331 //do nothing content provider object is dead any way
5332 }
5333 }
5334 } else {
5335 prc.unstableCount += 1;
5336 if (prc.unstableCount == 1) {
5337 // We are acquiring a new unstable reference on the provider.
5338 if (prc.removePending) {
5339 // Oh look, we actually have a remove pending for the
5340 // provider, which is still holding the last unstable
5341 // reference. We just need to cancel that to take new
5342 // ownership of the reference.
5343 if (DEBUG_PROVIDER) {
5344 Slog.v(TAG, "incProviderRef: unstable "
5345 + "snatched provider from the jaws of death");
5346 }
5347 prc.removePending = false;
5348 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5349 } else {
5350 // First unstable ref, increment our count in the
5351 // activity manager.
5352 try {
5353 if (DEBUG_PROVIDER) {
5354 Slog.v(TAG, "incProviderRef: Now unstable - "
5355 + prc.holder.info.name);
5356 }
5357 ActivityManagerNative.getDefault().refContentProvider(
5358 prc.holder.connection, 0, 1);
5359 } catch (RemoteException e) {
5360 //do nothing content provider object is dead any way
5361 }
5362 }
5363 }
5364 }
5365 }
5366
Jeff Sharkey6d515712012-09-20 16:06:08 -07005367 public final IContentProvider acquireExistingProvider(
5368 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005369 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005370 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005371 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005372 if (pr == null) {
5373 return null;
5374 }
5375
5376 IContentProvider provider = pr.mProvider;
5377 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005378 if (!jBinder.isBinderAlive()) {
5379 // The hosting process of the provider has died; we can't
5380 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005381 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005382 + ": existing object's process dead");
5383 handleUnstableProviderDiedLocked(jBinder, true);
5384 return null;
5385 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005386
5387 // Only increment the ref count if we have one. If we don't then the
5388 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005389 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005390 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005391 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005392 }
5393 return provider;
5394 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005395 }
5396
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005397 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5398 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 return false;
5400 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005402 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005403 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005405 if (prc == null) {
5406 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005408 }
5409
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005410 boolean lastRef = false;
5411 if (stable) {
5412 if (prc.stableCount == 0) {
5413 if (DEBUG_PROVIDER) Slog.v(TAG,
5414 "releaseProvider: stable ref count already 0, how?");
5415 return false;
5416 }
5417 prc.stableCount -= 1;
5418 if (prc.stableCount == 0) {
5419 // What we do at this point depends on whether there are
5420 // any unstable refs left: if there are, we just tell the
5421 // activity manager to decrement its stable count; if there
5422 // aren't, we need to enqueue this provider to be removed,
5423 // and convert to holding a single unstable ref while
5424 // doing so.
5425 lastRef = prc.unstableCount == 0;
5426 try {
5427 if (DEBUG_PROVIDER) {
5428 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5429 + lastRef + " - " + prc.holder.info.name);
5430 }
5431 ActivityManagerNative.getDefault().refContentProvider(
5432 prc.holder.connection, -1, lastRef ? 1 : 0);
5433 } catch (RemoteException e) {
5434 //do nothing content provider object is dead any way
5435 }
5436 }
5437 } else {
5438 if (prc.unstableCount == 0) {
5439 if (DEBUG_PROVIDER) Slog.v(TAG,
5440 "releaseProvider: unstable ref count already 0, how?");
5441 return false;
5442 }
5443 prc.unstableCount -= 1;
5444 if (prc.unstableCount == 0) {
5445 // If this is the last reference, we need to enqueue
5446 // this provider to be removed instead of telling the
5447 // activity manager to remove it at this point.
5448 lastRef = prc.stableCount == 0;
5449 if (!lastRef) {
5450 try {
5451 if (DEBUG_PROVIDER) {
5452 Slog.v(TAG, "releaseProvider: No longer unstable - "
5453 + prc.holder.info.name);
5454 }
5455 ActivityManagerNative.getDefault().refContentProvider(
5456 prc.holder.connection, 0, -1);
5457 } catch (RemoteException e) {
5458 //do nothing content provider object is dead any way
5459 }
5460 }
5461 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005462 }
5463
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005464 if (lastRef) {
5465 if (!prc.removePending) {
5466 // Schedule the actual remove asynchronously, since we don't know the context
5467 // this will be called in.
5468 // TODO: it would be nice to post a delayed message, so
5469 // if we come back and need the same provider quickly
5470 // we will still have it available.
5471 if (DEBUG_PROVIDER) {
5472 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5473 + prc.holder.info.name);
5474 }
5475 prc.removePending = true;
5476 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5477 mH.sendMessage(msg);
5478 } else {
5479 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5480 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005481 }
5482 return true;
5483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005484 }
5485
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005486 final void completeRemoveProvider(ProviderRefCount prc) {
5487 synchronized (mProviderMap) {
5488 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005489 // There was a race! Some other client managed to acquire
5490 // the provider before the removal was completed.
5491 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005492 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005493 + "provider still in use");
5494 return;
5495 }
5496
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005497 // More complicated race!! Some client managed to acquire the
5498 // provider and release it before the removal was completed.
5499 // Continue the removal, and abort the next remove message.
5500 prc.removePending = false;
5501
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005502 final IBinder jBinder = prc.holder.provider.asBinder();
5503 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5504 if (existingPrc == prc) {
5505 mProviderRefCountMap.remove(jBinder);
5506 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005507
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005508 for (int i=mProviderMap.size()-1; i>=0; i--) {
5509 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005510 IBinder myBinder = pr.mProvider.asBinder();
5511 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005512 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005513 }
5514 }
5515 }
5516
5517 try {
5518 if (DEBUG_PROVIDER) {
5519 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5520 + "removeContentProvider(" + prc.holder.info.name + ")");
5521 }
5522 ActivityManagerNative.getDefault().removeContentProvider(
5523 prc.holder.connection, false);
5524 } catch (RemoteException e) {
5525 //do nothing content provider object is dead any way
5526 }
5527 }
5528
5529 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005530 synchronized (mProviderMap) {
5531 handleUnstableProviderDiedLocked(provider, fromClient);
5532 }
5533 }
5534
5535 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5536 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5537 if (prc != null) {
5538 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5539 + provider + " " + prc.holder.info.name);
5540 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005541 for (int i=mProviderMap.size()-1; i>=0; i--) {
5542 ProviderClientRecord pr = mProviderMap.valueAt(i);
5543 if (pr != null && pr.mProvider.asBinder() == provider) {
5544 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5545 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005546 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005547 }
You Kimbc74de62013-10-01 00:13:26 +09005548
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005549 if (fromClient) {
5550 // We found out about this due to execution in our client
5551 // code. Tell the activity manager about it now, to ensure
5552 // that the next time we go to do anything with the provider
5553 // it knows it is dead (so we don't race with its death
5554 // notification).
5555 try {
5556 ActivityManagerNative.getDefault().unstableProviderDied(
5557 prc.holder.connection);
5558 } catch (RemoteException e) {
5559 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005560 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005561 }
5562 }
5563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005564
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005565 final void appNotRespondingViaProvider(IBinder provider) {
5566 synchronized (mProviderMap) {
5567 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5568 if (prc != null) {
5569 try {
5570 ActivityManagerNative.getDefault()
5571 .appNotRespondingViaProvider(prc.holder.connection);
5572 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005573 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005574 }
5575 }
5576 }
5577 }
5578
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005579 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005580 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005581 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005582 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5583
5584 final ProviderClientRecord pcr = new ProviderClientRecord(
5585 auths, provider, localProvider, holder);
5586 for (String auth : auths) {
5587 final ProviderKey key = new ProviderKey(auth, userId);
5588 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005589 if (existing != null) {
5590 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005591 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005592 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005593 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 }
5595 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005596 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005597 }
5598
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005599 /**
5600 * Installs the provider.
5601 *
5602 * Providers that are local to the process or that come from the system server
5603 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5604 * Other remote providers are reference counted. The initial reference count
5605 * for all reference counted providers is one. Providers that are not reference
5606 * counted do not have a reference count (at all).
5607 *
5608 * This method detects when a provider has already been installed. When this happens,
5609 * it increments the reference count of the existing provider (if appropriate)
5610 * and returns the existing provider. This can happen due to concurrent
5611 * attempts to acquire the same provider.
5612 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005613 private IActivityManager.ContentProviderHolder installProvider(Context context,
5614 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5615 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005617 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005618 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005619 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005620 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005621 + info.name);
5622 }
5623 Context c = null;
5624 ApplicationInfo ai = info.applicationInfo;
5625 if (context.getPackageName().equals(ai.packageName)) {
5626 c = context;
5627 } else if (mInitialApplication != null &&
5628 mInitialApplication.getPackageName().equals(ai.packageName)) {
5629 c = mInitialApplication;
5630 } else {
5631 try {
5632 c = context.createPackageContext(ai.packageName,
5633 Context.CONTEXT_INCLUDE_CODE);
5634 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005635 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 }
5637 }
5638 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005639 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005640 ai.packageName +
5641 " while loading content provider " +
5642 info.name);
5643 return null;
5644 }
5645 try {
5646 final java.lang.ClassLoader cl = c.getClassLoader();
5647 localProvider = (ContentProvider)cl.
5648 loadClass(info.name).newInstance();
5649 provider = localProvider.getIContentProvider();
5650 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005651 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 info.name + " from sourceDir " +
5653 info.applicationInfo.sourceDir);
5654 return null;
5655 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005656 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 TAG, "Instantiating local provider " + info.name);
5658 // XXX Need to create the correct context for this provider.
5659 localProvider.attachInfo(c, info);
5660 } catch (java.lang.Exception e) {
5661 if (!mInstrumentation.onException(null, e)) {
5662 throw new RuntimeException(
5663 "Unable to get provider " + info.name
5664 + ": " + e.toString(), e);
5665 }
5666 return null;
5667 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005668 } else {
5669 provider = holder.provider;
5670 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671 + info.name);
5672 }
5673
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005674 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005675
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005676 synchronized (mProviderMap) {
5677 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5678 + " / " + info.name);
5679 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005681 ComponentName cname = new ComponentName(info.packageName, info.name);
5682 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005683 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005684 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005685 Slog.v(TAG, "installProvider: lost the race, "
5686 + "using existing local provider");
5687 }
5688 provider = pr.mProvider;
5689 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005690 holder = new IActivityManager.ContentProviderHolder(info);
5691 holder.provider = provider;
5692 holder.noReleaseNeeded = true;
5693 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005694 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005695 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005696 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005697 retHolder = pr.mHolder;
5698 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005699 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5700 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005701 if (DEBUG_PROVIDER) {
5702 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005703 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005704 // We need to transfer our new reference to the existing
5705 // ref count, releasing the old one... but only if
5706 // release is needed (that is, it is not running in the
5707 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005708 if (!noReleaseNeeded) {
5709 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005710 try {
5711 ActivityManagerNative.getDefault().removeContentProvider(
5712 holder.connection, stable);
5713 } catch (RemoteException e) {
5714 //do nothing content provider object is dead any way
5715 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005716 }
5717 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005718 ProviderClientRecord client = installProviderAuthoritiesLocked(
5719 provider, localProvider, holder);
5720 if (noReleaseNeeded) {
5721 prc = new ProviderRefCount(holder, client, 1000, 1000);
5722 } else {
5723 prc = stable
5724 ? new ProviderRefCount(holder, client, 1, 0)
5725 : new ProviderRefCount(holder, client, 0, 1);
5726 }
5727 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005728 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005729 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 }
5731 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005732
5733 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 }
5735
Romain Guy65b345f2011-07-27 18:51:50 -07005736 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005737 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005740 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005741 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005742 public void run() {
5743 ensureJitEnabled();
5744 }
5745 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005746 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5747 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005749 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005750 try {
5751 mgr.attachApplication(mAppThread);
5752 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005753 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005754 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005755 // Watch for getting close to heap limit.
5756 BinderInternal.addGcWatcher(new Runnable() {
5757 @Override public void run() {
5758 if (!mSomeActivitiesChanged) {
5759 return;
5760 }
5761 Runtime runtime = Runtime.getRuntime();
5762 long dalvikMax = runtime.maxMemory();
5763 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5764 if (dalvikUsed > ((3*dalvikMax)/4)) {
5765 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5766 + " total=" + (runtime.totalMemory()/1024)
5767 + " used=" + (dalvikUsed/1024));
5768 mSomeActivitiesChanged = false;
5769 try {
5770 mgr.releaseSomeActivities(mAppThread);
5771 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005772 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005773 }
5774 }
5775 }
5776 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777 } else {
5778 // Don't set application object here -- if the system crashes,
5779 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005780 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005781 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005782 try {
5783 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005784 ContextImpl context = ContextImpl.createAppContext(
5785 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005786 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5787 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005788 } catch (Exception e) {
5789 throw new RuntimeException(
5790 "Unable to instantiate Application():" + e.toString(), e);
5791 }
5792 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005793
5794 // add dropbox logging to libcore
5795 DropBox.setReporter(new DropBoxReporter());
5796
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005797 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005798 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005799 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005800 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005801 // We need to apply this change to the resources
5802 // immediately, because upon returning the view
5803 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005804 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005805 // This actually changed the resources! Tell
5806 // everyone about it.
5807 if (mPendingConfiguration == null ||
5808 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5809 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005810
Jeff Brown9ef09972013-10-15 20:49:59 -07005811 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005812 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005813 }
5814 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005815 }
Craig Mautner88c05892013-06-28 09:47:45 -07005816 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005817 public void onLowMemory() {
5818 }
Craig Mautner88c05892013-06-28 09:47:45 -07005819 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005820 public void onTrimMemory(int level) {
5821 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005822 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 }
5824
Romain Guy5e9120d2012-01-30 12:17:22 -08005825 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005826 // The system process on low-memory devices do not get to use hardware
5827 // accelerated drawing, since this can add too much overhead to the
5828 // process.
5829 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005830 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005831 } else {
John Reck51aaf902015-12-02 15:08:07 -08005832 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 ActivityThread thread = new ActivityThread();
5835 thread.attach(true);
5836 return thread;
5837 }
5838
Jeff Brown10e89712011-07-08 18:52:57 -07005839 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005841 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 }
5843 }
5844
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005845 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005846 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005847 if (mCoreSettings != null) {
5848 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005849 }
Craig Mautner88c05892013-06-28 09:47:45 -07005850 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005851 }
5852 }
5853
Geremy Condra69689a72012-09-11 16:57:17 -07005854 private static class EventLoggingReporter implements EventLogger.Reporter {
5855 @Override
5856 public void report (int code, Object... list) {
5857 EventLog.writeEvent(code, list);
5858 }
5859 }
5860
Geremy Condrab7faaf42012-09-19 18:07:42 -07005861 private class DropBoxReporter implements DropBox.Reporter {
5862
5863 private DropBoxManager dropBox;
5864
Narayan Kamath7f062242015-04-08 13:24:13 +01005865 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005866
5867 @Override
5868 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005869 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005870 dropBox.addData(tag, data, flags);
5871 }
5872
5873 @Override
5874 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005875 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005876 dropBox.addText(tag, data);
5877 }
Narayan Kamath7f062242015-04-08 13:24:13 +01005878
5879 private synchronized void ensureInitialized() {
5880 if (dropBox == null) {
5881 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5882 }
5883 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005884 }
5885
Romain Guy65b345f2011-07-27 18:51:50 -07005886 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005887 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07005888 SamplingProfilerIntegration.start();
5889
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005890 // CloseGuard defaults to true and can be quite spammy. We
5891 // disable it here, but selectively enable it later (via
5892 // StrictMode) on debug builds, but using DropBox, not logs.
5893 CloseGuard.setEnabled(false);
5894
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005895 Environment.initForCurrentUser();
5896
Geremy Condra69689a72012-09-11 16:57:17 -07005897 // Set the reporter for event logging in libcore
5898 EventLogger.setReporter(new EventLoggingReporter());
5899
Alex Klyubin48125632015-04-29 13:16:30 -07005900 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08005901
Robin Lee3d076af2014-04-25 14:57:49 +01005902 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5903 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5904 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 Process.setArgV0("<pre-initialized>");
5907
5908 Looper.prepareMainLooper();
5909
5910 ActivityThread thread = new ActivityThread();
5911 thread.attach(false);
5912
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005913 if (sMainThreadHandler == null) {
5914 sMainThreadHandler = thread.getHandler();
5915 }
5916
Dianne Hackborn287952c2010-09-22 22:34:31 -07005917 if (false) {
5918 Looper.myLooper().setMessageLogging(new
5919 LogPrinter(Log.DEBUG, "ActivityThread"));
5920 }
5921
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005922 // End of event ActivityThreadMain.
5923 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 Looper.loop();
5925
Jeff Brown10e89712011-07-08 18:52:57 -07005926 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005927 }
5928}