blob: 060ac5e578e4e46129dc1c7aed512e0ae992f94e [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;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700116import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700117import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700118import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100119import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700120import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122import java.io.File;
123import java.io.FileDescriptor;
124import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700125import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import java.io.PrintWriter;
127import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700128import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000129import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000131import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800134import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Geremy Condrab7faaf42012-09-19 18:07:42 -0700137import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700138import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800139import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400140import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800141import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700142import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700143import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700144import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700145
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700146final class RemoteServiceException extends AndroidRuntimeException {
147 public RemoteServiceException(String msg) {
148 super(msg);
149 }
150}
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152/**
153 * This manages the execution of the main thread in an
154 * application process, scheduling and executing activities,
155 * broadcasts, and other operations on it as the activity
156 * manager requests.
157 *
158 * {@hide}
159 */
160public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700161 /** @hide */
162 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700163 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700164 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700165 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700166 /** @hide */
167 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700168 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700169 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700170 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800171 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700172 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700173 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700174 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800177 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
178 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800179 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn455625e2015-01-21 09:55:13 -0800181 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
182 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
183 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
184 public static final int SERVICE_DONE_EXECUTING_START = 1;
185 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
186 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
187
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700188 // Details for pausing activity.
189 private static final int USER_LEAVING = 1;
190 private static final int DONT_REPORT = 2;
191
Filip Gruszczynskica664812015-12-04 12:43:36 -0800192 // Whether to invoke an activity callback after delivering new configuration.
193 private static final boolean REPORT_TO_ACTIVITY = true;
194
Jeff Browndefd4a62014-03-10 21:24:37 -0700195 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700196
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700197 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 final ApplicationThread mAppThread = new ApplicationThread();
200 final Looper mLooper = Looper.myLooper();
201 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700202 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700203 // List of new activities (via ActivityRecord.nextIdle) that should
204 // be reported when next we idle.
205 ActivityClientRecord mNewActivities = null;
206 // Number of activities that are currently visible on-screen.
207 int mNumVisibleActivities = 0;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700208 WeakReference<AssistStructure> mLastAssistStructure;
209 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700210 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700211 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700212 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700213 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700214 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700215 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700216 Application mInitialApplication;
217 final ArrayList<Application> mAllApplications
218 = new ArrayList<Application>();
219 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700220 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800221 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700222 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700224 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700225 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700226 String[] mInstrumentationSplitAppDirs = null;
227 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700228 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700229 String[] mInstrumentedSplitAppDirs = null;
230 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700231 boolean mSystemThread = false;
232 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700233 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700235 // These can be accessed by multiple threads; mPackages is the lock.
236 // XXX For now we keep around information about all packages we have
237 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800238 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700239 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800240 // which means this lock gets held while the activity and window managers
241 // holds their own lock. Thus you MUST NEVER call back into the activity manager
242 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700243 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700244 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
245 = new ArrayMap<String, WeakReference<LoadedApk>>();
246 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
247 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 final ArrayList<ActivityClientRecord> mRelaunchingActivities
249 = new ArrayList<ActivityClientRecord>();
250 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700251 // Because we merge activity relaunch operations we can't depend on the ordering provided by
252 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
253 // us to drop certain events, if we know that they happened before relaunch we already executed.
254 // This represents the order of receiving the request from AM.
255 @GuardedBy("mResourcesManager")
256 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Craig Mautner88c05892013-06-28 09:47:45 -0700258 private final ResourcesManager mResourcesManager;
259
Jeff Sharkey6d515712012-09-20 16:06:08 -0700260 private static final class ProviderKey {
261 final String authority;
262 final int userId;
263
264 public ProviderKey(String authority, int userId) {
265 this.authority = authority;
266 this.userId = userId;
267 }
268
269 @Override
270 public boolean equals(Object o) {
271 if (o instanceof ProviderKey) {
272 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800273 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700274 }
275 return false;
276 }
277
278 @Override
279 public int hashCode() {
280 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
281 }
282 }
283
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700284 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700285 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
286 = new ArrayMap<ProviderKey, ProviderClientRecord>();
287 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
288 = new ArrayMap<IBinder, ProviderRefCount>();
289 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
290 = new ArrayMap<IBinder, ProviderClientRecord>();
291 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
292 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700294 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700295 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600296
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700297 final GcIdler mGcIdler = new GcIdler();
298 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700300 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700301
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800302 Bundle mCoreSettings = null;
303
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400304 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700306 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800308 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700309 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700311 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 Activity activity;
313 Window window;
314 Activity parent;
315 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700316 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 boolean paused;
318 boolean stopped;
319 boolean hideForNow;
320 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700321 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800322 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800323 // Used for consolidating configs before sending on to Activity.
324 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700325 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Jeff Hao1b012d32014-08-20 10:35:34 -0700327 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700331 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800334 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
336 boolean startsNotResumed;
337 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800338 int pendingConfigChanges;
339 boolean onlyLocalRequest;
340
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700341 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800342 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700343 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700345 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
346 // can be compared with other lifecycle operations.
347 int relaunchSeq = 0;
348
349 // Can only be accessed from the UI thread. This represents the latest processed message
350 // that is related to lifecycle events/
351 int lastProcessedSeq = 0;
352
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700353 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 parent = null;
355 embeddedID = null;
356 paused = false;
357 stopped = false;
358 hideForNow = false;
359 nextIdle = null;
360 }
361
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800362 public boolean isPreHoneycomb() {
363 if (activity != null) {
364 return activity.getApplicationInfo().targetSdkVersion
365 < android.os.Build.VERSION_CODES.HONEYCOMB;
366 }
367 return false;
368 }
369
Craig Mautnera0026042014-04-23 11:45:37 -0700370 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700371 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700372 }
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700375 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 return "ActivityRecord{"
377 + Integer.toHexString(System.identityHashCode(this))
378 + " token=" + token + " " + (componentName == null
379 ? "no component name" : componentName.toShortString())
380 + "}";
381 }
382 }
383
Wale Ogunwale1d646122015-04-24 14:45:14 -0700384 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700385 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 final IContentProvider mProvider;
387 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700388 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700390 ProviderClientRecord(String[] names, IContentProvider provider,
391 ContentProvider localProvider,
392 IActivityManager.ContentProviderHolder holder) {
393 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 mProvider = provider;
395 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700396 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 }
398 }
399
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400400 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800401 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 IBinder token;
403 public String toString() {
404 return "NewIntentData{intents=" + intents + " token=" + token + "}";
405 }
406 }
407
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400408 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700409 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700410 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
411 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800412 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700413 this.intent = intent;
414 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 Intent intent;
417 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400418 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 public String toString() {
420 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700421 info.packageName + " resultCode=" + getResultCode()
422 + " resultData=" + getResultData() + " resultExtras="
423 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
426
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400427 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700428 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400429 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700430 int backupMode;
431 public String toString() {
432 return "CreateBackupAgentData{appInfo=" + appInfo
433 + " backupAgent=" + appInfo.backupAgentName
434 + " mode=" + backupMode + "}";
435 }
436 }
Bob Leee5408332009-09-04 18:31:17 -0700437
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400438 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 IBinder token;
440 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400441 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 Intent intent;
443 public String toString() {
444 return "CreateServiceData{token=" + token + " className="
445 + info.name + " packageName=" + info.packageName
446 + " intent=" + intent + "}";
447 }
448 }
449
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400450 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 IBinder token;
452 Intent intent;
453 boolean rebind;
454 public String toString() {
455 return "BindServiceData{token=" + token + " intent=" + intent + "}";
456 }
457 }
458
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400459 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700461 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700463 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 Intent args;
465 public String toString() {
466 return "ServiceArgsData{token=" + token + " startId=" + startId
467 + " args=" + args + "}";
468 }
469 }
470
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700472 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 String processName;
474 ApplicationInfo appInfo;
475 List<ProviderInfo> providers;
476 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 Bundle instrumentationArgs;
478 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800479 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400481 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700482 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700483 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700484 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700487
488 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700489 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public String toString() {
492 return "AppBindData{appInfo=" + appInfo + "}";
493 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700494 }
495
496 static final class Profiler {
497 String profileFile;
498 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700499 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700500 boolean autoStopProfiler;
501 boolean profiling;
502 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700503 public void setProfiler(ProfilerInfo profilerInfo) {
504 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700505 if (profiling) {
506 if (fd != null) {
507 try {
508 fd.close();
509 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700510 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700511 }
512 }
513 return;
514 }
515 if (profileFd != null) {
516 try {
517 profileFd.close();
518 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700519 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700520 }
521 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700522 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700523 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700524 samplingInterval = profilerInfo.samplingInterval;
525 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700526 }
527 public void startProfiling() {
528 if (profileFd == null || profiling) {
529 return;
530 }
531 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700532 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
533 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700534 profiling = true;
535 } catch (RuntimeException e) {
536 Slog.w(TAG, "Profiling failed on path " + profileFile);
537 try {
538 profileFd.close();
539 profileFd = null;
540 } catch (IOException e2) {
541 Slog.w(TAG, "Failure closing profile fd", e2);
542 }
543 }
544 }
545 public void stopProfiling() {
546 if (profiling) {
547 profiling = false;
548 Debug.stopMethodTracing();
549 if (profileFd != null) {
550 try {
551 profileFd.close();
552 } catch (IOException e) {
553 }
554 }
555 profileFd = null;
556 profileFile = null;
557 }
558 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400561 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700562 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700563 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800564 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 }
567
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400568 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 IBinder token;
570 List<ResultInfo> results;
571 public String toString() {
572 return "ResultData{token=" + token + " results" + results + "}";
573 }
574 }
575
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400576 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800577 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 String what;
579 String who;
580 }
581
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400582 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700583 String path;
584 ParcelFileDescriptor fd;
585 }
586
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400587 static final class UpdateCompatibilityData {
588 String pkg;
589 CompatibilityInfo info;
590 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800591
Adam Skorydfc7fd72013-08-05 19:23:41 -0700592 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800593 IBinder activityToken;
594 IBinder requestToken;
595 int requestType;
596 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700597
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800598 static final class ActivityConfigChangeData {
599 final IBinder activityToken;
600 final Configuration overrideConfig;
601 public ActivityConfigChangeData(IBinder token, Configuration config) {
602 activityToken = token;
603 overrideConfig = config;
604 }
605 }
606
Romain Guy65b345f2011-07-27 18:51:50 -0700607 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700608
Romain Guy65b345f2011-07-27 18:51:50 -0700609 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700610 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700611
Dianne Hackborna413dc02013-07-12 12:02:55 -0700612 private int mLastProcessState = -1;
613
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700614 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700615 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700616 if (mPendingConfiguration == null ||
617 mPendingConfiguration.isOtherSeqNewer(config)) {
618 mPendingConfiguration = config;
619 }
620 }
621 }
622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700624 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700625 int seq = getLifecycleSeq();
626 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
627 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700628 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
630 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700631 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
632 configChanges,
633 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 }
635
636 public final void scheduleStopActivity(IBinder token, boolean showWindow,
637 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700638 int seq = getLifecycleSeq();
639 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
640 + " operation received seq: " + seq);
641 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700643 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645
646 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700647 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
649 token);
650 }
651
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800652 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700653 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800654 }
655
Dianne Hackborna413dc02013-07-12 12:02:55 -0700656 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800657 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700658 int seq = getLifecycleSeq();
659 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
660 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700661 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700662 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 }
664
665 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
666 ResultData res = new ResultData();
667 res.token = token;
668 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700669 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 }
671
672 // we use token to identify this activity without having to send the
673 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800674 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700675 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800676 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
677 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
678 int procState, Bundle state, PersistableBundle persistentState,
679 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
680 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700681
682 updateProcessState(procState, false);
683
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700684 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685
686 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700687 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800689 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700690 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400692 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700694 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
696 r.pendingResults = pendingResults;
697 r.pendingIntents = pendingNewIntents;
698
699 r.startsNotResumed = notResumed;
700 r.isForward = isForward;
701
Jeff Hao1b012d32014-08-20 10:35:34 -0700702 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700703
Wale Ogunwale60454db2015-01-23 16:05:07 -0800704 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700705 updatePendingConfiguration(curConfig);
706
Jeff Brown9ef09972013-10-15 20:49:59 -0700707 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 }
709
Wale Ogunwale60454db2015-01-23 16:05:07 -0800710 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800712 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800713 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700714 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800715 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700716 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800719 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 NewIntentData data = new NewIntentData();
721 data.intents = intents;
722 data.token = token;
723
Jeff Brown9ef09972013-10-15 20:49:59 -0700724 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
728 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700729 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 configChanges);
731 }
732
733 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400734 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700735 boolean sync, int sendingUser, int processState) {
736 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700737 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700738 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400740 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700741 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
743
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400744 public final void scheduleCreateBackupAgent(ApplicationInfo app,
745 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700746 CreateBackupAgentData d = new CreateBackupAgentData();
747 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400748 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700749 d.backupMode = backupMode;
750
Jeff Brown9ef09972013-10-15 20:49:59 -0700751 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700752 }
753
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400754 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
755 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700756 CreateBackupAgentData d = new CreateBackupAgentData();
757 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400758 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700759
Jeff Brown9ef09972013-10-15 20:49:59 -0700760 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700761 }
762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700764 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
765 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 CreateServiceData s = new CreateServiceData();
767 s.token = token;
768 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400769 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
Jeff Brown9ef09972013-10-15 20:49:59 -0700771 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773
774 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700775 boolean rebind, int processState) {
776 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 BindServiceData s = new BindServiceData();
778 s.token = token;
779 s.intent = intent;
780 s.rebind = rebind;
781
Amith Yamasani742a6712011-05-04 14:49:28 -0700782 if (DEBUG_SERVICE)
783 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
784 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700785 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 }
787
788 public final void scheduleUnbindService(IBinder token, Intent intent) {
789 BindServiceData s = new BindServiceData();
790 s.token = token;
791 s.intent = intent;
792
Jeff Brown9ef09972013-10-15 20:49:59 -0700793 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 }
795
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700796 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700797 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 ServiceArgsData s = new ServiceArgsData();
799 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700800 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700802 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 s.args = args;
804
Jeff Brown9ef09972013-10-15 20:49:59 -0700805 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807
808 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700809 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811
Jeff Hao1b012d32014-08-20 10:35:34 -0700812 public final void bindApplication(String processName, ApplicationInfo appInfo,
813 List<ProviderInfo> providers, ComponentName instrumentationName,
814 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
815 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800816 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700817 boolean enableBinderTracking, boolean trackAllocation,
818 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
819 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
821 if (services != null) {
822 // Setup the service cache in the ServiceManager
823 ServiceManager.initServiceCache(services);
824 }
825
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800826 setCoreSettings(coreSettings);
827
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 AppBindData data = new AppBindData();
829 data.processName = processName;
830 data.appInfo = appInfo;
831 data.providers = providers;
832 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 data.instrumentationArgs = instrumentationArgs;
834 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800835 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400837 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700838 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700839 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700840 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400842 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700843 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700844 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846
847 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700848 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850
Christopher Tate5e1ab332009-09-01 20:32:49 -0700851 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700852 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700853 }
854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700856 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700857 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 }
859
860 public void updateTimeZone() {
861 TimeZone.setDefault(null);
862 }
863
Robert Greenwalt03595d02010-11-02 14:08:23 -0700864 public void clearDnsCache() {
865 // a non-standard API to get this to libcore
866 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400867 // Allow libcore to perform the necessary actions as it sees fit upon a network
868 // configuration change.
869 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700870 }
871
Jason Monk83520b92014-05-09 15:16:06 -0400872 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400873 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
874 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500875 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400876 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500877 } else {
878 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
879 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700880 }
881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 public void processInBackground() {
883 mH.removeMessages(H.GC_WHEN_IDLE);
884 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
885 }
886
887 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700888 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700889 try {
890 data.fd = ParcelFileDescriptor.dup(fd);
891 data.token = servicetoken;
892 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700893 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700894 } catch (IOException e) {
895 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897 }
898
899 // This function exists to make sure all receiver dispatching is
900 // correctly ordered, since these are one-way calls and the binder driver
901 // applies transaction ordering per object for such calls.
902 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700903 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700904 boolean sticky, int sendingUser, int processState) throws RemoteException {
905 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700906 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
907 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 }
Bob Leee5408332009-09-04 18:31:17 -0700909
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800910 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700912 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 }
914
Wale Ogunwale60454db2015-01-23 16:05:07 -0800915 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800916 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800917 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800918 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800919 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 }
921
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800922 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700923 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
924 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800925 }
926
Andy McFadden824c5102010-07-09 16:26:57 -0700927 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
928 DumpHeapData dhd = new DumpHeapData();
929 dhd.path = path;
930 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700931 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700932 }
933
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700934 public void setSchedulingGroup(int group) {
935 // Note: do this immediately, since going into the foreground
936 // should happen regardless of what pending work we have to do
937 // and the activity manager will wait for us to report back that
938 // we are done before sending us to the background.
939 try {
940 Process.setProcessGroup(Process.myPid(), group);
941 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800942 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700943 }
944 }
Bob Leee5408332009-09-04 18:31:17 -0700945
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700946 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700947 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700948 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700949
950 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700951 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700952 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700953
Dianne Hackborn30d71892010-12-11 10:37:55 -0800954 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
955 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700956 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700957 try {
958 data.fd = ParcelFileDescriptor.dup(fd);
959 data.token = activitytoken;
960 data.prefix = prefix;
961 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700962 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700963 } catch (IOException e) {
964 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700965 }
966 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700967
Marco Nelissen18cb2872011-11-15 11:19:53 -0800968 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
969 String[] args) {
970 DumpComponentInfo data = new DumpComponentInfo();
971 try {
972 data.fd = ParcelFileDescriptor.dup(fd);
973 data.token = providertoken;
974 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700975 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800976 } catch (IOException e) {
977 Slog.w(TAG, "dumpProvider failed", e);
978 }
979 }
980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700982 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -0800983 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
984 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700985 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700986 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700987 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -0800988 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700989 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700990 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700991 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700992 }
993
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700994 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -0800995 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 long nativeMax = Debug.getNativeHeapSize() / 1024;
997 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
998 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001001 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 long dalvikMax = runtime.totalMemory() / 1024;
1003 long dalvikFree = runtime.freeMemory() / 1024;
1004 long dalvikAllocated = dalvikMax - dalvikFree;
1005 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001006 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001007 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1008 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001009 int globalAssetCount = AssetManager.getGlobalAssetCount();
1010 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1011 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1012 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1013 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001014 long parcelSize = Parcel.getGlobalAllocSize();
1015 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001016 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001017 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001018
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001019 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1020 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001021 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1022 nativeMax, nativeAllocated, nativeFree,
1023 dalvikMax, dalvikAllocated, dalvikFree);
1024
Dianne Hackbornb437e092011-08-05 17:50:29 -07001025 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 // NOTE: if you change anything significant below, also consider changing
1027 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 // Object counts
1030 pw.print(viewInstanceCount); pw.print(',');
1031 pw.print(viewRootInstanceCount); pw.print(',');
1032 pw.print(appContextInstanceCount); pw.print(',');
1033 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 pw.print(globalAssetCount); pw.print(',');
1036 pw.print(globalAssetManagerCount); pw.print(',');
1037 pw.print(binderLocalObjectCount); pw.print(',');
1038 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 pw.print(binderDeathObjectCount); pw.print(',');
1041 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001044 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001045 pw.print(stats.memoryUsed / 1024); pw.print(',');
1046 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001047 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001048 for (int i = 0; i < stats.dbStats.size(); i++) {
1049 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001050 pw.print(','); pw.print(dbStats.dbName);
1051 pw.print(','); pw.print(dbStats.pageSize);
1052 pw.print(','); pw.print(dbStats.dbSize);
1053 pw.print(','); pw.print(dbStats.lookaside);
1054 pw.print(','); pw.print(dbStats.cache);
1055 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001056 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001057 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001058
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001059 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
Bob Leee5408332009-09-04 18:31:17 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 pw.println(" ");
1063 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001064 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 viewRootInstanceCount);
1066
1067 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1068 "Activities:", activityInstanceCount);
1069
1070 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1071 "AssetManagers:", globalAssetManagerCount);
1072
1073 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1074 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001075 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1076 "Parcel count:", parcelCount);
1077 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1078 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 // SQLite mem info
1081 pw.println(" ");
1082 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001083 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1084 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1085 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001086 pw.println(" ");
1087 int N = stats.dbStats.size();
1088 if (N > 0) {
1089 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001090 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1091 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001092 for (int i = 0; i < N; i++) {
1093 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001094 printRow(pw, DB_INFO_FORMAT,
1095 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1096 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1097 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1098 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001099 }
1100 }
Bob Leee5408332009-09-04 18:31:17 -07001101
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001102 // Asset details.
1103 String assetAlloc = AssetManager.getAssetAllocations();
1104 if (assetAlloc != null) {
1105 pw.println(" ");
1106 pw.println(" Asset Allocations");
1107 pw.print(assetAlloc);
1108 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001109
1110 // Unreachable native memory
1111 if (dumpUnreachable) {
1112 boolean showContents = ((mBoundApplication != null)
1113 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1114 || android.os.Build.IS_DEBUGGABLE;
1115 pw.println(" ");
1116 pw.println(" Unreachable memory");
1117 pw.print(Debug.getUnreachableMemory(100, showContents));
1118 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001119 }
1120
1121 @Override
1122 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1123 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001124 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 }
1126
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001127 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001128 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001129 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1130 SQLiteDebug.dump(printer, args);
1131 pw.flush();
1132 }
1133
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001134 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001135 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1136 if (mSystemThread) {
1137 // Ensure this invocation is asynchronous to prevent
1138 // writer waiting due to buffer cannot be consumed.
1139 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1140 @Override
1141 public void run() {
1142 dumpDatabaseInfo(fd, args);
1143 }
1144 });
1145 } else {
1146 dumpDatabaseInfo(fd, args);
1147 }
1148 }
1149
1150 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001151 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001152 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001153 }
1154
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001155 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001156 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001157 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001158 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001159 cmd.activityToken = activityToken;
1160 cmd.requestToken = requestToken;
1161 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001162 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001163 }
1164
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001165 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001166 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001167 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001168
1169 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1170 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1171 ucd.pkg = pkg;
1172 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001173 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001174 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001175
1176 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001177 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001178 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001179
Craig Mautner5eda9b32013-07-02 11:58:16 -07001180 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001181 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001182 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001183
Craig Mautnereb8abf72014-07-02 15:04:09 -07001184 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1185 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1186 new Pair<IBinder, ActivityOptions>(token, options));
1187 }
1188
Dianne Hackborna413dc02013-07-12 12:02:55 -07001189 public void setProcessState(int state) {
1190 updateProcessState(state, true);
1191 }
1192
1193 public void updateProcessState(int processState, boolean fromIpc) {
1194 synchronized (this) {
1195 if (mLastProcessState != processState) {
1196 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001197 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1198 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1199 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1200 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1201 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1202 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1203 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1204 }
1205 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001206 if (false) {
1207 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1208 + (fromIpc ? " (from ipc": ""));
1209 }
1210 }
1211 }
1212 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001213
1214 @Override
1215 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001216 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001217 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001218
1219 @Override
1220 public final void updateTimePrefs(boolean is24Hour) {
1221 DateFormat.set24HourTimePref(is24Hour);
1222 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001223
1224 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001225 public void scheduleCancelVisibleBehind(IBinder token) {
1226 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001227 }
1228
1229 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001230 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1231 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001232 }
Craig Mautner8746a472014-07-24 15:12:54 -07001233
Jeff Sharkey605eb792014-11-04 13:34:06 -08001234 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001235 public void scheduleEnterAnimationComplete(IBinder token) {
1236 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1237 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001238
1239 @Override
1240 public void notifyCleartextNetwork(byte[] firstPacket) {
1241 if (StrictMode.vmCleartextNetworkEnabled()) {
1242 StrictMode.onCleartextNetworkDetected(firstPacket);
1243 }
1244 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001245
1246 @Override
1247 public void startBinderTracking() {
1248 sendMessage(H.START_BINDER_TRACKING, null);
1249 }
1250
1251 @Override
1252 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1253 try {
1254 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1255 } catch (IOException e) {
1256 }
1257 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001258
1259 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001260 public void scheduleMultiWindowChanged(IBinder token, boolean inMultiWindow)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001261 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001262 sendMessage(H.MULTI_WINDOW_CHANGED, token, inMultiWindow ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001263 }
1264
1265 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001266 public void schedulePictureInPictureChanged(IBinder token, boolean inPip)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001267 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001268 sendMessage(H.PICTURE_IN_PICTURE_CHANGED, token, inPip ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001269 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001270
1271 @Override
1272 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1273 IVoiceInteractor voiceInteractor) throws RemoteException {
1274 SomeArgs args = SomeArgs.obtain();
1275 args.arg1 = token;
1276 args.arg2 = voiceInteractor;
1277 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 }
1280
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001281 private int getLifecycleSeq() {
1282 synchronized (mResourcesManager) {
1283 return mLifecycleSeq++;
1284 }
1285 }
1286
Romain Guy65b345f2011-07-27 18:51:50 -07001287 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 public static final int LAUNCH_ACTIVITY = 100;
1289 public static final int PAUSE_ACTIVITY = 101;
1290 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1291 public static final int STOP_ACTIVITY_SHOW = 103;
1292 public static final int STOP_ACTIVITY_HIDE = 104;
1293 public static final int SHOW_WINDOW = 105;
1294 public static final int HIDE_WINDOW = 106;
1295 public static final int RESUME_ACTIVITY = 107;
1296 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001297 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 public static final int BIND_APPLICATION = 110;
1299 public static final int EXIT_APPLICATION = 111;
1300 public static final int NEW_INTENT = 112;
1301 public static final int RECEIVER = 113;
1302 public static final int CREATE_SERVICE = 114;
1303 public static final int SERVICE_ARGS = 115;
1304 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 public static final int CONFIGURATION_CHANGED = 118;
1307 public static final int CLEAN_UP_CONTEXT = 119;
1308 public static final int GC_WHEN_IDLE = 120;
1309 public static final int BIND_SERVICE = 121;
1310 public static final int UNBIND_SERVICE = 122;
1311 public static final int DUMP_SERVICE = 123;
1312 public static final int LOW_MEMORY = 124;
1313 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1314 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001315 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001316 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001317 public static final int DESTROY_BACKUP_AGENT = 129;
1318 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001319 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001320 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001321 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001322 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001323 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001324 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001325 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001326 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001327 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001328 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001329 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001330 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001331 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001332 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001333 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001334 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001335 public static final int CANCEL_VISIBLE_BEHIND = 147;
1336 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001337 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001338 public static final int START_BINDER_TRACKING = 150;
1339 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001340 public static final int MULTI_WINDOW_CHANGED = 152;
1341 public static final int PICTURE_IN_PICTURE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001342 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001345 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 switch (code) {
1347 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1348 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1349 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1350 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1351 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1352 case SHOW_WINDOW: return "SHOW_WINDOW";
1353 case HIDE_WINDOW: return "HIDE_WINDOW";
1354 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1355 case SEND_RESULT: return "SEND_RESULT";
1356 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1357 case BIND_APPLICATION: return "BIND_APPLICATION";
1358 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1359 case NEW_INTENT: return "NEW_INTENT";
1360 case RECEIVER: return "RECEIVER";
1361 case CREATE_SERVICE: return "CREATE_SERVICE";
1362 case SERVICE_ARGS: return "SERVICE_ARGS";
1363 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1365 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1366 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1367 case BIND_SERVICE: return "BIND_SERVICE";
1368 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1369 case DUMP_SERVICE: return "DUMP_SERVICE";
1370 case LOW_MEMORY: return "LOW_MEMORY";
1371 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1372 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001373 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001374 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1375 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001376 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001377 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001378 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001379 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001380 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001381 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001382 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001383 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001384 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001385 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001386 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001387 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001388 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001389 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001390 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001391 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001392 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001393 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1394 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001395 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001396 case MULTI_WINDOW_CHANGED: return "MULTI_WINDOW_CHANGED";
1397 case PICTURE_IN_PICTURE_CHANGED: return "PICTURE_IN_PICTURE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001398 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 }
1400 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001401 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
1403 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001404 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 switch (msg.what) {
1406 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001407 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001408 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409
1410 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001411 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001412 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001413 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 } break;
1415 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001416 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001417 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001418 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001419 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001421 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001422 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001423 SomeArgs args = (SomeArgs) msg.obj;
1424 handlePauseActivity((IBinder) args.arg1, false,
1425 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1426 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001427 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001428 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001429 } break;
1430 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001431 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001432 SomeArgs args = (SomeArgs) msg.obj;
1433 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1434 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001435 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001436 } break;
1437 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001438 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001439 SomeArgs args = (SomeArgs) msg.obj;
1440 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001441 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001442 } break;
1443 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001444 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001445 SomeArgs args = (SomeArgs) msg.obj;
1446 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001447 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001448 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001449 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001450 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001451 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001452 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 break;
1454 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001455 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001456 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001457 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 break;
1459 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001460 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001461 SomeArgs args = (SomeArgs) msg.obj;
1462 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001463 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001464 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 break;
1466 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001467 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001469 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 break;
1471 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001472 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1474 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001475 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 break;
1477 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001478 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 AppBindData data = (AppBindData)msg.obj;
1480 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001481 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 break;
1483 case EXIT_APPLICATION:
1484 if (mInitialApplication != null) {
1485 mInitialApplication.onTerminate();
1486 }
1487 Looper.myLooper().quit();
1488 break;
1489 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001490 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001491 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001492 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001493 break;
1494 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001495 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001497 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001498 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 break;
1500 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001501 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001503 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 break;
1505 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001506 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001508 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 break;
1510 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001511 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001512 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001513 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 break;
1515 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001516 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001518 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 break;
1520 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001521 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001523 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001524 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001527 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001528 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001529 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001530 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 break;
1532 case CLEAN_UP_CONTEXT:
1533 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1534 cci.context.performFinalCleanup(cci.who, cci.what);
1535 break;
1536 case GC_WHEN_IDLE:
1537 scheduleGcIdler();
1538 break;
1539 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001540 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 break;
1542 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001543 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001545 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 break;
1547 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001548 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001549 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1550 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001551 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001553 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001554 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001555 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001556 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001557 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001558 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001559 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001560 break;
1561 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001562 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001563 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001564 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001565 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001566 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001567 Process.killProcess(Process.myPid());
1568 break;
1569 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001570 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001571 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001572 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001573 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001574 case ENABLE_JIT:
1575 ensureJitEnabled();
1576 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001577 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001578 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001579 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001580 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001581 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001582 case SCHEDULE_CRASH:
1583 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001584 case DUMP_HEAP:
1585 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1586 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001587 case DUMP_ACTIVITY:
1588 handleDumpActivity((DumpComponentInfo)msg.obj);
1589 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001590 case DUMP_PROVIDER:
1591 handleDumpProvider((DumpComponentInfo)msg.obj);
1592 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001593 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001594 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001595 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001596 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001597 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001598 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001599 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001600 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001601 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001602 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001603 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1604 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001605 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001606 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001607 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001608 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001609 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001610 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001611 case UNSTABLE_PROVIDER_DIED:
1612 handleUnstableProviderDied((IBinder)msg.obj, false);
1613 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001614 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1615 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001616 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001617 case TRANSLUCENT_CONVERSION_COMPLETE:
1618 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1619 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001620 case INSTALL_PROVIDER:
1621 handleInstallProvider((ProviderInfo) msg.obj);
1622 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001623 case ON_NEW_ACTIVITY_OPTIONS:
1624 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1625 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001626 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001627 case CANCEL_VISIBLE_BEHIND:
1628 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001629 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001630 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1631 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001632 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001633 case ENTER_ANIMATION_COMPLETE:
1634 handleEnterAnimationComplete((IBinder) msg.obj);
1635 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001636 case START_BINDER_TRACKING:
1637 handleStartBinderTracking();
1638 break;
1639 case STOP_BINDER_TRACKING_AND_DUMP:
1640 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1641 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001642 case MULTI_WINDOW_CHANGED:
1643 handleMultiWindowChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001644 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001645 case PICTURE_IN_PICTURE_CHANGED:
1646 handlePictureInPictureChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001647 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001648 case LOCAL_VOICE_INTERACTION_STARTED:
1649 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1650 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1651 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001653 Object obj = msg.obj;
1654 if (obj instanceof SomeArgs) {
1655 ((SomeArgs) obj).recycle();
1656 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001657 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 }
Bob Leee5408332009-09-04 18:31:17 -07001659
Brian Carlstromed7e0072011-03-24 13:27:57 -07001660 private void maybeSnapshot() {
1661 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001662 // convert the *private* ActivityThread.PackageInfo to *public* known
1663 // android.content.pm.PackageInfo
1664 String packageName = mBoundApplication.info.mPackageName;
1665 android.content.pm.PackageInfo packageInfo = null;
1666 try {
1667 Context context = getSystemContext();
1668 if(context == null) {
1669 Log.e(TAG, "cannot get a valid context");
1670 return;
1671 }
1672 PackageManager pm = context.getPackageManager();
1673 if(pm == null) {
1674 Log.e(TAG, "cannot get a valid PackageManager");
1675 return;
1676 }
1677 packageInfo = pm.getPackageInfo(
1678 packageName, PackageManager.GET_ACTIVITIES);
1679 } catch (NameNotFoundException e) {
1680 Log.e(TAG, "cannot get package info for " + packageName, e);
1681 }
1682 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001683 }
1684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
1686
Romain Guy65b345f2011-07-27 18:51:50 -07001687 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001688 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001690 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001691 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001692 if (mBoundApplication != null && mProfiler.profileFd != null
1693 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001694 stopProfiling = true;
1695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 if (a != null) {
1697 mNewActivities = null;
1698 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001699 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001701 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 TAG, "Reporting idle of " + a +
1703 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001704 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (a.activity != null && !a.activity.mFinished) {
1706 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001707 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001708 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001710 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 }
1712 }
1713 prev = a;
1714 a = a.nextIdle;
1715 prev.nextIdle = null;
1716 } while (a != null);
1717 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001718 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001719 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001720 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001721 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 return false;
1723 }
1724 }
1725
1726 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001727 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 public final boolean queueIdle() {
1729 doGcIfNeeded();
1730 return false;
1731 }
1732 }
1733
Romain Guy65b345f2011-07-27 18:51:50 -07001734 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001735 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001738 public static boolean isSystem() {
1739 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1740 }
1741
Svetoslavfbf0eca2015-05-01 16:52:41 -07001742 public static String currentOpPackageName() {
1743 ActivityThread am = currentActivityThread();
1744 return (am != null && am.getApplication() != null)
1745 ? am.getApplication().getOpPackageName() : null;
1746 }
1747
Romain Guy65b345f2011-07-27 18:51:50 -07001748 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001749 ActivityThread am = currentActivityThread();
1750 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001751 ? am.mBoundApplication.appInfo.packageName : null;
1752 }
1753
1754 public static String currentProcessName() {
1755 ActivityThread am = currentActivityThread();
1756 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001757 ? am.mBoundApplication.processName : null;
1758 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759
Romain Guy65b345f2011-07-27 18:51:50 -07001760 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001761 ActivityThread am = currentActivityThread();
1762 return am != null ? am.mInitialApplication : null;
1763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001765 public static IPackageManager getPackageManager() {
1766 if (sPackageManager != null) {
1767 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1768 return sPackageManager;
1769 }
1770 IBinder b = ServiceManager.getService("package");
1771 //Slog.v("PackageManager", "default service binder = " + b);
1772 sPackageManager = IPackageManager.Stub.asInterface(b);
1773 //Slog.v("PackageManager", "default service = " + sPackageManager);
1774 return sPackageManager;
1775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776
Romain Guy65b345f2011-07-27 18:51:50 -07001777 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001778 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1779 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001780 if (config == null) {
1781 return null;
1782 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001783 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001784 mMainThreadConfig.setTo(config);
1785 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001786 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001787 }
1788 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001791 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001792 * Creates the top level resources for the given package. Will return an existing
1793 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001794 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001795 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001796 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1797 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1798 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001799 }
1800
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001801 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(
Adam Lesinski082614c2016-03-04 14:33:47 -08002612 this, r.packageInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002613 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) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003469 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003470 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003471 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472 r.newConfig = null;
3473 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003474 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 + isForward);
3476 WindowManager.LayoutParams l = r.window.getAttributes();
3477 if ((l.softInputMode
3478 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3479 != forwardBit) {
3480 l.softInputMode = (l.softInputMode
3481 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3482 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003483 if (r.activity.mVisibleFromClient) {
3484 ViewManager wm = a.getWindowManager();
3485 View decor = r.window.getDecorView();
3486 wm.updateViewLayout(decor, l);
3487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488 }
3489 r.activity.mVisibleFromServer = true;
3490 mNumVisibleActivities++;
3491 if (r.activity.mVisibleFromClient) {
3492 r.activity.makeVisible();
3493 }
3494 }
3495
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003496 if (!r.onlyLocalRequest) {
3497 r.nextIdle = mNewActivities;
3498 mNewActivities = r;
3499 if (localLOGV) Slog.v(
3500 TAG, "Scheduling idle handler for " + r);
3501 Looper.myQueue().addIdleHandler(new Idler());
3502 }
3503 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003505 // Tell the activity manager we have resumed.
3506 if (reallyResume) {
3507 try {
3508 ActivityManagerNative.getDefault().activityResumed(token);
3509 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003510 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003511 }
3512 }
3513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 } else {
3515 // If an exception was thrown when trying to resume, then
3516 // just end this activity.
3517 try {
3518 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003519 .finishActivity(token, Activity.RESULT_CANCELED, null,
3520 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003521 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003522 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003523 }
3524 }
3525 }
3526
3527 private int mThumbnailWidth = -1;
3528 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003529 private Bitmap mAvailThumbnailBitmap = null;
3530 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531
Romain Guy65b345f2011-07-27 18:51:50 -07003532 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003533 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003534 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003535 if (thumbnail == null) {
3536 int w = mThumbnailWidth;
3537 int h;
3538 if (w < 0) {
3539 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003540 int wId = com.android.internal.R.dimen.thumbnail_width;
3541 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003542 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3543 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003544 } else {
3545 h = mThumbnailHeight;
3546 }
3547
3548 // On platforms where we don't want thumbnails, set dims to (0,0)
3549 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003550 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3551 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003552 thumbnail.eraseColor(0);
3553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 }
3555
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003556 if (thumbnail != null) {
3557 Canvas cv = mThumbnailCanvas;
3558 if (cv == null) {
3559 mThumbnailCanvas = cv = new Canvas();
3560 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003561
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003562 cv.setBitmap(thumbnail);
3563 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3564 mAvailThumbnailBitmap = thumbnail;
3565 thumbnail = null;
3566 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003567 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003568 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 } catch (Exception e) {
3571 if (!mInstrumentation.onException(r.activity, e)) {
3572 throw new RuntimeException(
3573 "Unable to create thumbnail of "
3574 + r.intent.getComponent().toShortString()
3575 + ": " + e.toString(), e);
3576 }
3577 thumbnail = null;
3578 }
3579
3580 return thumbnail;
3581 }
3582
Romain Guy65b345f2011-07-27 18:51:50 -07003583 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003584 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003585 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003586 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003587 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3588 return;
3589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003591 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 if (userLeaving) {
3593 performUserLeavingActivity(r);
3594 }
Bob Leee5408332009-09-04 18:31:17 -07003595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003597 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003599 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003600 if (r.isPreHoneycomb()) {
3601 QueuedWork.waitToFinish();
3602 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003605 if (!dontReport) {
3606 try {
3607 ActivityManagerNative.getDefault().activityPaused(token);
3608 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003609 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003612 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 }
3614 }
3615
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003616 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 mInstrumentation.callActivityOnUserLeaving(r.activity);
3618 }
3619
3620 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003621 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003622 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003623 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 }
3625
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003626 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003627 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003628 if (r.paused) {
3629 if (r.activity.mFinished) {
3630 // If we are finishing, we won't call onResume() in certain cases.
3631 // So here we likewise don't want to call onPause() if the activity
3632 // isn't resumed.
3633 return null;
3634 }
3635 RuntimeException e = new RuntimeException(
3636 "Performing pause of activity that is not resumed: "
3637 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003638 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 if (finished) {
3641 r.activity.mFinished = true;
3642 }
3643 try {
3644 // Next have the activity save its current state and managed dialogs...
3645 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003646 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
3648 // Now we are idle.
3649 r.activity.mCalled = false;
3650 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08003651 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003652 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 if (!r.activity.mCalled) {
3654 throw new SuperNotCalledException(
3655 "Activity " + r.intent.getComponent().toShortString() +
3656 " did not call through to super.onPause()");
3657 }
3658
3659 } catch (SuperNotCalledException e) {
3660 throw e;
3661
3662 } catch (Exception e) {
3663 if (!mInstrumentation.onException(r.activity, e)) {
3664 throw new RuntimeException(
3665 "Unable to pause activity "
3666 + r.intent.getComponent().toShortString()
3667 + ": " + e.toString(), e);
3668 }
3669 }
3670 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003671
3672 // Notify any outstanding on paused listeners
3673 ArrayList<OnActivityPausedListener> listeners;
3674 synchronized (mOnPauseListeners) {
3675 listeners = mOnPauseListeners.remove(r.activity);
3676 }
3677 int size = (listeners != null ? listeners.size() : 0);
3678 for (int i = 0; i < size; i++) {
3679 listeners.get(i).onPaused(r.activity);
3680 }
3681
Craig Mautnera0026042014-04-23 11:45:37 -07003682 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 }
3684
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003685 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003686 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003687 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 }
3689
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003690 private static class StopInfo implements Runnable {
3691 ActivityClientRecord activity;
3692 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003693 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003695
3696 @Override public void run() {
3697 // Tell activity manager we have been stopped.
3698 try {
3699 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3700 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003701 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003702 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003703 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003704 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003705 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003706 return;
3707 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003708 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003709 }
3710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 }
3712
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003713 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003714 public final IActivityManager.ContentProviderHolder holder;
3715 public final ProviderClientRecord client;
3716 public int stableCount;
3717 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003718
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003719 // When this is set, the stable and unstable ref counts are 0 and
3720 // we have a pending operation scheduled to remove the ref count
3721 // from the activity manager. On the activity manager we are still
3722 // holding an unstable ref, though it is not reflected in the counts
3723 // here.
3724 public boolean removePending;
3725
3726 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3727 ProviderClientRecord inClient, int sCount, int uCount) {
3728 holder = inHolder;
3729 client = inClient;
3730 stableCount = sCount;
3731 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 }
3733 }
3734
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003735 /**
3736 * Core implementation of stopping an activity. Note this is a little
3737 * tricky because the server's meaning of stop is slightly different
3738 * than our client -- for the server, stop means to save state and give
3739 * it the result when it is done, but the window may still be visible.
3740 * For the client, we want to call onStop()/onStart() to indicate when
3741 * the activity's UI visibillity changes.
3742 */
Romain Guy65b345f2011-07-27 18:51:50 -07003743 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003744 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003745 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003746 if (r != null) {
3747 if (!keepShown && r.stopped) {
3748 if (r.activity.mFinished) {
3749 // If we are finishing, we won't call onResume() in certain
3750 // cases. So here we likewise don't want to call onStop()
3751 // if the activity isn't resumed.
3752 return;
3753 }
3754 RuntimeException e = new RuntimeException(
3755 "Performing stop of activity that is not resumed: "
3756 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003757 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 }
3759
3760 if (info != null) {
3761 try {
3762 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003763 // For now, don't create the thumbnail here; we are
3764 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003765 info.description = r.activity.onCreateDescription();
3766 } catch (Exception e) {
3767 if (!mInstrumentation.onException(r.activity, e)) {
3768 throw new RuntimeException(
3769 "Unable to save state of activity "
3770 + r.intent.getComponent().toShortString()
3771 + ": " + e.toString(), e);
3772 }
3773 }
3774 }
3775
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003776 // Next have the activity save its current state and managed dialogs...
3777 if (!r.activity.mFinished && saveState) {
3778 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003779 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003780 }
3781 }
3782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 if (!keepShown) {
3784 try {
3785 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003786 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 } catch (Exception e) {
3788 if (!mInstrumentation.onException(r.activity, e)) {
3789 throw new RuntimeException(
3790 "Unable to stop activity "
3791 + r.intent.getComponent().toShortString()
3792 + ": " + e.toString(), e);
3793 }
3794 }
3795 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003796 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3797 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003798 }
3799
3800 r.paused = true;
3801 }
3802 }
3803
Romain Guy65b345f2011-07-27 18:51:50 -07003804 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003805 View v = r.activity.mDecor;
3806 if (v != null) {
3807 if (show) {
3808 if (!r.activity.mVisibleFromServer) {
3809 r.activity.mVisibleFromServer = true;
3810 mNumVisibleActivities++;
3811 if (r.activity.mVisibleFromClient) {
3812 r.activity.makeVisible();
3813 }
3814 }
3815 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003816 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003817 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08003818 + r.activityInfo.name + " with new config "
3819 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 r.newConfig = null;
3821 }
3822 } else {
3823 if (r.activity.mVisibleFromServer) {
3824 r.activity.mVisibleFromServer = false;
3825 mNumVisibleActivities--;
3826 v.setVisibility(View.INVISIBLE);
3827 }
3828 }
3829 }
3830 }
3831
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003832 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003833 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003834 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3835 return;
3836 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 r.activity.mConfigChangeFlags |= configChanges;
3838
3839 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003840 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003841
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003842 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 TAG, "Finishing stop of " + r + ": show=" + show
3844 + " win=" + r.window);
3845
3846 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003847
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003848 // Make sure any pending writes are now committed.
3849 if (!r.isPreHoneycomb()) {
3850 QueuedWork.waitToFinish();
3851 }
3852
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003853 // Schedule the call to tell the activity manager we have
3854 // stopped. We don't do this immediately, because we want to
3855 // have a chance for any other pending work (in particular memory
3856 // trim requests) to complete before you tell the activity
3857 // manager to proceed and allow us to go fully into the background.
3858 info.activity = r;
3859 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003860 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003861 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003862 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003863 }
3864
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003865 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3866 String action) {
3867 if (r == null) {
3868 return true;
3869 }
3870 if (seq < r.lastProcessedSeq) {
3871 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3872 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3873 return false;
3874 }
3875 r.lastProcessedSeq = seq;
3876 return true;
3877 }
3878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003879 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003880 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 if (r.stopped) {
3882 r.activity.performRestart();
3883 r.stopped = false;
3884 }
3885 }
3886
Romain Guy65b345f2011-07-27 18:51:50 -07003887 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003888 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003889
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003890 if (r == null) {
3891 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3892 return;
3893 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003895 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003896 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003897 } else if (show && r.stopped) {
3898 // If we are getting ready to gc after going to the background, well
3899 // we are back active so skip it.
3900 unscheduleGcIdler();
3901
3902 r.activity.performRestart();
3903 r.stopped = false;
3904 }
3905 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003906 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 TAG, "Handle window " + r + " visibility: " + show);
3908 updateVisibility(r, show);
3909 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003910 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 }
3912
Romain Guy65b345f2011-07-27 18:51:50 -07003913 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003914 ActivityClientRecord r = mActivities.get(token);
3915
3916 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003917 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003918 return;
3919 }
3920
3921 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003922 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003923 try {
3924 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003925 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003926 } catch (Exception e) {
3927 if (!mInstrumentation.onException(r.activity, e)) {
3928 throw new RuntimeException(
3929 "Unable to stop activity "
3930 + r.intent.getComponent().toShortString()
3931 + ": " + e.toString(), e);
3932 }
3933 }
3934 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003935 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3936 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003937 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003938
3939 // Make sure any pending writes are now committed.
3940 if (!r.isPreHoneycomb()) {
3941 QueuedWork.waitToFinish();
3942 }
3943
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003944 // Tell activity manager we slept.
3945 try {
3946 ActivityManagerNative.getDefault().activitySlept(r.token);
3947 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003948 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003949 }
3950 } else {
3951 if (r.stopped && r.activity.mVisibleFromServer) {
3952 r.activity.performRestart();
3953 r.stopped = false;
3954 }
3955 }
3956 }
3957
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003958 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003959 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003960 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003961 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003962 onCoreSettingsChange();
3963 }
3964
3965 private void onCoreSettingsChange() {
3966 boolean debugViewAttributes =
3967 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3968 if (debugViewAttributes != View.mDebugViewAttributes) {
3969 View.mDebugViewAttributes = debugViewAttributes;
3970
3971 // request all activities to relaunch for the changes to take place
3972 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003973 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
3974 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07003975 }
3976 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003977 }
3978
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003979 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3980 LoadedApk apk = peekPackageInfo(data.pkg, false);
3981 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003982 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003983 }
3984 apk = peekPackageInfo(data.pkg, true);
3985 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003986 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003987 }
3988 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003989 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003990 }
3991
Romain Guy65b345f2011-07-27 18:51:50 -07003992 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 final int N = results.size();
3994 for (int i=0; i<N; i++) {
3995 ResultInfo ri = results.get(i);
3996 try {
3997 if (ri.mData != null) {
3998 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003999 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004001 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004002 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004003 r.activity.dispatchActivityResult(ri.mResultWho,
4004 ri.mRequestCode, ri.mResultCode, ri.mData);
4005 } catch (Exception e) {
4006 if (!mInstrumentation.onException(r.activity, e)) {
4007 throw new RuntimeException(
4008 "Failure delivering result " + ri + " to activity "
4009 + r.intent.getComponent().toShortString()
4010 + ": " + e.toString(), e);
4011 }
4012 }
4013 }
4014 }
4015
Romain Guy65b345f2011-07-27 18:51:50 -07004016 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004017 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004018 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004019 if (r != null) {
4020 final boolean resumed = !r.paused;
4021 if (!r.activity.mFinished && r.activity.mDecor != null
4022 && r.hideForNow && resumed) {
4023 // We had hidden the activity because it started another
4024 // one... we have gotten a result back and we are not
4025 // paused, so make sure our window is visible.
4026 updateVisibility(r, true);
4027 }
4028 if (resumed) {
4029 try {
4030 // Now we are idle.
4031 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004032 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 mInstrumentation.callActivityOnPause(r.activity);
4034 if (!r.activity.mCalled) {
4035 throw new SuperNotCalledException(
4036 "Activity " + r.intent.getComponent().toShortString()
4037 + " did not call through to super.onPause()");
4038 }
4039 } catch (SuperNotCalledException e) {
4040 throw e;
4041 } catch (Exception e) {
4042 if (!mInstrumentation.onException(r.activity, e)) {
4043 throw new RuntimeException(
4044 "Unable to pause activity "
4045 + r.intent.getComponent().toShortString()
4046 + ": " + e.toString(), e);
4047 }
4048 }
4049 }
4050 deliverResults(r, res.results);
4051 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004052 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004053 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004054 }
4055 }
4056 }
4057
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004058 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 return performDestroyActivity(token, finishing, 0, false);
4060 }
4061
Romain Guy65b345f2011-07-27 18:51:50 -07004062 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004064 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004065 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004066 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004068 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004069 r.activity.mConfigChangeFlags |= configChanges;
4070 if (finishing) {
4071 r.activity.mFinished = true;
4072 }
4073 if (!r.paused) {
4074 try {
4075 r.activity.mCalled = false;
4076 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08004077 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004078 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004079 if (!r.activity.mCalled) {
4080 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004081 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 + " did not call through to super.onPause()");
4083 }
4084 } catch (SuperNotCalledException e) {
4085 throw e;
4086 } catch (Exception e) {
4087 if (!mInstrumentation.onException(r.activity, e)) {
4088 throw new RuntimeException(
4089 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004090 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 + ": " + e.toString(), e);
4092 }
4093 }
4094 r.paused = true;
4095 }
4096 if (!r.stopped) {
4097 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004098 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 } catch (SuperNotCalledException e) {
4100 throw e;
4101 } catch (Exception e) {
4102 if (!mInstrumentation.onException(r.activity, e)) {
4103 throw new RuntimeException(
4104 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004105 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 + ": " + e.toString(), e);
4107 }
4108 }
4109 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004110 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4111 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113 if (getNonConfigInstance) {
4114 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004115 r.lastNonConfigurationInstances
4116 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004117 } catch (Exception e) {
4118 if (!mInstrumentation.onException(r.activity, e)) {
4119 throw new RuntimeException(
4120 "Unable to retain activity "
4121 + r.intent.getComponent().toShortString()
4122 + ": " + e.toString(), e);
4123 }
4124 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 }
4126 try {
4127 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004128 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004129 if (!r.activity.mCalled) {
4130 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004131 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132 " did not call through to super.onDestroy()");
4133 }
4134 if (r.window != null) {
4135 r.window.closeAllPanels();
4136 }
4137 } catch (SuperNotCalledException e) {
4138 throw e;
4139 } catch (Exception e) {
4140 if (!mInstrumentation.onException(r.activity, e)) {
4141 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004142 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4143 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004144 }
4145 }
4146 }
4147 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004148 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004149 return r;
4150 }
4151
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004152 private static String safeToComponentShortString(Intent intent) {
4153 ComponentName component = intent.getComponent();
4154 return component == null ? "[Unknown]" : component.toShortString();
4155 }
4156
Romain Guy65b345f2011-07-27 18:51:50 -07004157 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004158 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004159 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 configChanges, getNonConfigInstance);
4161 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004162 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 WindowManager wm = r.activity.getWindowManager();
4164 View v = r.activity.mDecor;
4165 if (v != null) {
4166 if (r.activity.mVisibleFromServer) {
4167 mNumVisibleActivities--;
4168 }
4169 IBinder wtoken = v.getWindowToken();
4170 if (r.activity.mWindowAdded) {
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004171 if (r.onlyLocalRequest || r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004172 // Hold off on removing this until the new activity's
4173 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004174 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004175 r.mPendingRemoveWindowManager = wm;
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004176 if (r.mPreserveWindow) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004177 // We can only keep the part of the view hierarchy that we control,
4178 // everything else must be removed, because it might not be able to
4179 // behave properly when activity is relaunching.
4180 r.window.clearContentView();
4181 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004182 } else {
4183 wm.removeViewImmediate(v);
4184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004185 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004186 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004187 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 r.activity.getClass().getName(), "Activity");
4189 }
4190 r.activity.mDecor = null;
4191 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004192 if (r.mPendingRemoveWindow == null) {
4193 // If we are delaying the removal of the activity window, then
4194 // we can't clean up all windows here. Note that we can't do
4195 // so later either, which means any windows that aren't closed
4196 // by the app will leak. Well we try to warning them a lot
4197 // about leaking windows, because that is a bug, so if they are
4198 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004199 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004200 r.activity.getClass().getName(), "Activity");
4201 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202
4203 // Mocked out contexts won't be participating in the normal
4204 // process lifecycle, but if we're running with a proper
4205 // ApplicationContext we need to have it tear down things
4206 // cleanly.
4207 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004208 if (c instanceof ContextImpl) {
4209 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004210 r.activity.getClass().getName(), "Activity");
4211 }
4212 }
4213 if (finishing) {
4214 try {
4215 ActivityManagerNative.getDefault().activityDestroyed(token);
4216 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004217 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004218 }
4219 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004220 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 }
4222
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004223 /**
4224 * @param preserveWindow Whether the activity should try to reuse the window it created,
4225 * including the decor view after the relaunch.
4226 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004227 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004228 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004229 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004230 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004231 ActivityClientRecord target = null;
4232
Craig Mautner88c05892013-06-28 09:47:45 -07004233 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004234 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4235 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004236 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004237 if (r.token == token) {
4238 target = r;
4239 if (pendingResults != null) {
4240 if (r.pendingResults != null) {
4241 r.pendingResults.addAll(pendingResults);
4242 } else {
4243 r.pendingResults = pendingResults;
4244 }
4245 }
4246 if (pendingNewIntents != null) {
4247 if (r.pendingIntents != null) {
4248 r.pendingIntents.addAll(pendingNewIntents);
4249 } else {
4250 r.pendingIntents = pendingNewIntents;
4251 }
4252 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004253
4254 // For each relaunch request, activity manager expects an answer
4255 if (!r.onlyLocalRequest && fromServer) {
4256 try {
4257 ActivityManagerNative.getDefault().activityRelaunched(token);
4258 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004259 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004260 }
4261 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004262 break;
4263 }
4264 }
4265
4266 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004267 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4268 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004269 target = new ActivityClientRecord();
4270 target.token = token;
4271 target.pendingResults = pendingResults;
4272 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004273 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004274 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004275 final ActivityClientRecord existing = mActivities.get(token);
4276 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004277 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004278 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4279 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004280 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004281 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004282 }
4283 target.onlyLocalRequest = true;
4284 }
4285 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004286 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004287 }
4288
4289 if (fromServer) {
4290 target.startsNotResumed = notResumed;
4291 target.onlyLocalRequest = false;
4292 }
4293 if (config != null) {
4294 target.createdConfig = config;
4295 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004296 if (overrideConfig != null) {
4297 target.overrideConfig = overrideConfig;
4298 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004299 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004300 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004301 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004302 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4303 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004304 }
4305
Romain Guy65b345f2011-07-27 18:51:50 -07004306 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 // If we are getting ready to gc after going to the background, well
4308 // we are back active so skip it.
4309 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004310 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311
4312 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004313 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 // First: make sure we have the most recent configuration and most
4316 // recent version of the activity, or skip it if some previous call
4317 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004318 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004319 int N = mRelaunchingActivities.size();
4320 IBinder token = tmp.token;
4321 tmp = null;
4322 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004323 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004324 if (r.token == token) {
4325 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004326 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 mRelaunchingActivities.remove(i);
4328 i--;
4329 N--;
4330 }
4331 }
Bob Leee5408332009-09-04 18:31:17 -07004332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004334 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 return;
4336 }
Bob Leee5408332009-09-04 18:31:17 -07004337
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004338 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4339 + tmp.token + " with configChanges=0x"
4340 + Integer.toHexString(configChanges));
4341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 if (mPendingConfiguration != null) {
4343 changedConfig = mPendingConfiguration;
4344 mPendingConfiguration = null;
4345 }
4346 }
Bob Leee5408332009-09-04 18:31:17 -07004347
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004348 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4349 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4350 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4351 } else {
4352 tmp.lastProcessedSeq = tmp.relaunchSeq;
4353 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004354 if (tmp.createdConfig != null) {
4355 // If the activity manager is passing us its current config,
4356 // assume that is really what we want regardless of what we
4357 // may have pending.
4358 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004359 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4360 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4361 if (changedConfig == null
4362 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4363 changedConfig = tmp.createdConfig;
4364 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004365 }
4366 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004367
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004368 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004369 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 // If there was a pending configuration change, execute it first.
4372 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004373 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004374 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004375 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 }
Bob Leee5408332009-09-04 18:31:17 -07004377
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004378 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004379 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004380 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004381 if (!tmp.onlyLocalRequest) {
4382 try {
4383 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4384 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004385 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004386 }
4387 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 return;
4389 }
Bob Leee5408332009-09-04 18:31:17 -07004390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004392 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004393 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004394 r.lastProcessedSeq = tmp.lastProcessedSeq;
4395 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004396 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004397
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004398 r.activity.mChangingConfigurations = true;
4399
Robert Carr23fa16b2016-01-13 13:19:58 -08004400 // If we are preserving the main window across relaunches we would also like to preserve
4401 // the children. However the client side view system does not support preserving
4402 // the child views so we notify the window manager to expect these windows to
4403 // be replaced and defer requests to destroy or hide them. This way we can achieve
4404 // visual continuity. It's important that we do this here prior to pause and destroy
4405 // as that is when we may hide or remove the child views.
4406 try {
4407 if (r.mPreserveWindow) {
4408 WindowManagerGlobal.getWindowSession().prepareToReplaceChildren(r.token);
4409 }
4410 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004411 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004412 }
4413
4414
Dianne Hackborne2b04802010-12-09 09:24:55 -08004415 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004417 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004418 }
4419 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004420 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 }
Bob Leee5408332009-09-04 18:31:17 -07004422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004423 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 r.activity = null;
4426 r.window = null;
4427 r.hideForNow = false;
4428 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004429 // Merge any pending results and pending intents; don't just replace them
4430 if (tmp.pendingResults != null) {
4431 if (r.pendingResults == null) {
4432 r.pendingResults = tmp.pendingResults;
4433 } else {
4434 r.pendingResults.addAll(tmp.pendingResults);
4435 }
4436 }
4437 if (tmp.pendingIntents != null) {
4438 if (r.pendingIntents == null) {
4439 r.pendingIntents = tmp.pendingIntents;
4440 } else {
4441 r.pendingIntents.addAll(tmp.pendingIntents);
4442 }
4443 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004445 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004446
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004447 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004448
4449 if (!tmp.onlyLocalRequest) {
4450 try {
4451 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004452 if (r.window != null) {
4453 r.window.reportActivityRelaunched();
4454 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004455 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004456 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004457 }
4458 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 }
4460
Craig Mautnera0026042014-04-23 11:45:37 -07004461 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4462 r.state = new Bundle();
4463 r.state.setAllowFds(false);
4464 if (r.isPersistable()) {
4465 r.persistentState = new PersistableBundle();
4466 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4467 r.persistentState);
4468 } else {
4469 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4470 }
4471 }
4472
Dianne Hackborn73c14162012-09-19 15:45:06 -07004473 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004474 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004475 ArrayList<ComponentCallbacks2> callbacks
4476 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004477
Craig Mautner88c05892013-06-28 09:47:45 -07004478 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004479 final int NAPP = mAllApplications.size();
4480 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004481 callbacks.add(mAllApplications.get(i));
4482 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004483 final int NACT = mActivities.size();
4484 for (int i=0; i<NACT; i++) {
4485 ActivityClientRecord ar = mActivities.valueAt(i);
4486 Activity a = ar.activity;
4487 if (a != null) {
4488 Configuration thisConfig = applyConfigCompatMainThread(
4489 mCurDefaultDisplayDpi, newConfig,
4490 ar.packageInfo.getCompatibilityInfo());
4491 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4492 // If the activity is currently resumed, its configuration
4493 // needs to change right now.
4494 callbacks.add(a);
4495 } else if (thisConfig != null) {
4496 // Otherwise, we will tell it about the change
4497 // the next time it is resumed or shown. Note that
4498 // the activity manager may, before then, decide the
4499 // activity needs to be destroyed to handle its new
4500 // configuration.
4501 if (DEBUG_CONFIGURATION) {
4502 Slog.v(TAG, "Setting activity "
4503 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004504 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004505 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004506 }
4507 }
4508 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004509 final int NSVC = mServices.size();
4510 for (int i=0; i<NSVC; i++) {
4511 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004512 }
4513 }
4514 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004515 final int NPRV = mLocalProviders.size();
4516 for (int i=0; i<NPRV; i++) {
4517 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519 }
Bob Leee5408332009-09-04 18:31:17 -07004520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 return callbacks;
4522 }
Bob Leee5408332009-09-04 18:31:17 -07004523
Adam Lesinski082614c2016-03-04 14:33:47 -08004524 /**
4525 * Updates the configuration for an Activity. The ActivityClientRecord's
4526 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4527 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4528 * the updated Configuration.
4529 * @param r ActivityClientRecord representing the Activity.
4530 * @param newBaseConfig The new configuration to use. This may be augmented with
4531 * {@link ActivityClientRecord#overrideConfig}.
4532 * @param reportToActivity true if the change should be reported to the Activity's callback.
4533 */
4534 private void performConfigurationChangedForActivity(ActivityClientRecord r,
4535 Configuration newBaseConfig,
4536 boolean reportToActivity) {
4537 r.tmpConfig.setTo(newBaseConfig);
4538 if (r.overrideConfig != null) {
4539 r.tmpConfig.updateFrom(r.overrideConfig);
4540 }
4541 performConfigurationChanged(r.activity, r.token, r.tmpConfig, r.overrideConfig,
4542 reportToActivity);
4543 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
4544 }
4545
4546 /**
4547 * Decides whether to update an Activity's configuration and whether to tell the
4548 * Activity/Component about it.
4549 * @param cb The component callback to notify of configuration change.
4550 * @param activityToken The Activity binder token for which this configuration change happened.
4551 * If the change is global, this is null.
4552 * @param newConfig The new configuration.
4553 * @param overrideConfig The override config that differentiates the Activity's configuration
4554 * from the base global configuration.
4555 * @param reportToActivity Notify the Activity of the change.
4556 */
4557 private void performConfigurationChanged(ComponentCallbacks2 cb,
4558 IBinder activityToken,
4559 Configuration newConfig,
4560 Configuration overrideConfig,
4561 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004563 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 // we check the runtime type and act accordingly.
4565 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4566 if (activity != null) {
4567 activity.mCalled = false;
4568 }
Bob Leee5408332009-09-04 18:31:17 -07004569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 boolean shouldChangeConfig = false;
4571 if ((activity == null) || (activity.mCurrentConfig == null)) {
4572 shouldChangeConfig = true;
4573 } else {
Bob Leee5408332009-09-04 18:31:17 -07004574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004575 // If the new config is the same as the config this Activity
4576 // is already running with then don't bother calling
4577 // onConfigurationChanged
Adam Lesinski082614c2016-03-04 14:33:47 -08004578 int diff = activity.mCurrentConfig.diff(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 if (diff != 0) {
Wale Ogunwale14e811f2015-12-14 15:04:22 -08004580 // If this activity doesn't handle any of the config changes then don't bother
4581 // calling onConfigurationChanged as we're going to destroy it.
4582 // Except in the case where the configuration changed on the activity manager side,
4583 // but wasn't big enough to cause a resource change so the activity wasn't destroyed.
4584 // In this case we still want to change the configuration of the activity but not
4585 // report it to the app.
4586 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4587 || !reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 shouldChangeConfig = true;
4589 }
4590 }
4591 }
Bob Leee5408332009-09-04 18:31:17 -07004592
Adam Lesinski082614c2016-03-04 14:33:47 -08004593 if (DEBUG_CONFIGURATION) {
4594 Slog.v(TAG, "Config callback " + cb + ": shouldChangeConfig=" + shouldChangeConfig);
4595 }
4596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 if (shouldChangeConfig) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004598 if (activityToken != null) {
4599 // We only update an Activity's configuration if this is not a global
4600 // configuration change. This must also be done before the callback,
4601 // or else we violate the contract that the new resources are available
4602 // in {@link ComponentCallbacks2#onConfigurationChanged(Configuration)}.
4603 mResourcesManager.updateResourcesForActivity(activityToken, overrideConfig);
4604 }
4605
Filip Gruszczynskica664812015-12-04 12:43:36 -08004606 if (reportToActivity) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004607 cb.onConfigurationChanged(newConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004608 }
Bob Leee5408332009-09-04 18:31:17 -07004609
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004610 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004611 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 throw new SuperNotCalledException(
4613 "Activity " + activity.getLocalClassName() +
Adam Lesinski082614c2016-03-04 14:33:47 -08004614 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004615 }
4616 activity.mConfigChangeFlags = 0;
Adam Lesinski082614c2016-03-04 14:33:47 -08004617 activity.mCurrentConfig = new Configuration(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 }
4619 }
4620 }
4621
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004622 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004623 synchronized (mResourcesManager) {
4624 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004625 }
4626 }
4627
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004628 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004629 Configuration config = mConfiguration;
4630 if (mCompatConfiguration == null) {
4631 mCompatConfiguration = new Configuration();
4632 }
4633 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004634 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4635 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004636 config = mCompatConfiguration;
4637 }
4638 return config;
4639 }
4640
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004641 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004642
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004643 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004644
Craig Mautner88c05892013-06-28 09:47:45 -07004645 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004646 if (mPendingConfiguration != null) {
4647 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4648 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004649 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004650 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004651 }
4652 mPendingConfiguration = null;
4653 }
4654
4655 if (config == null) {
4656 return;
4657 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004658
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004659 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004660 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004661
4662 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 if (mConfiguration == null) {
4665 mConfiguration = new Configuration();
4666 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004667 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004668 return;
4669 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004670
4671 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004672 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004673
4674 final Theme systemTheme = getSystemContext().getTheme();
4675 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4676 systemTheme.rebase();
4677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004679
4680 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4681
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004682 freeTextLayoutCachesIfNeeded(configDiff);
4683
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004684 if (callbacks != null) {
4685 final int N = callbacks.size();
4686 for (int i=0; i<N; i++) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004687 performConfigurationChanged(callbacks.get(i), null, config, null,
4688 REPORT_TO_ACTIVITY);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 }
4691 }
4692
Romain Guy46bfc482013-08-16 18:38:29 -07004693 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004694 if (configDiff != 0) {
4695 // Ask text layout engine to free its caches if there is a locale change
4696 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4697 if (hasLocaleConfigChange) {
4698 Canvas.freeTextLayoutCaches();
4699 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4700 }
4701 }
4702 }
4703
Filip Gruszczynskica664812015-12-04 12:43:36 -08004704 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4705 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004706 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 if (r == null || r.activity == null) {
4708 return;
4709 }
Bob Leee5408332009-09-04 18:31:17 -07004710
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004711 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004712 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004713
Adam Lesinski082614c2016-03-04 14:33:47 -08004714 r.overrideConfig = data.overrideConfig;
4715 performConfigurationChangedForActivity(r, mCompatConfiguration, reportToActivity);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004716 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 }
4718
Jeff Hao1b012d32014-08-20 10:35:34 -07004719 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004720 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004721 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004722 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004723 default:
4724 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004725 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004726 break;
4727 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004728 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004729 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004730 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004731 } finally {
4732 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004733 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004734 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004735 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004736 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004737 }
4738 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004739 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004740 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004741 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004742 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004743 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004744 }
4745 }
Bob Leee5408332009-09-04 18:31:17 -07004746
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004747 /**
4748 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4749 * so that profiler data won't be lost.
4750 *
4751 * @hide
4752 */
4753 public void stopProfiling() {
4754 mProfiler.stopProfiling();
4755 }
4756
Romain Guya998dff2012-03-23 18:58:36 -07004757 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004758 if (managed) {
4759 try {
4760 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4761 } catch (IOException e) {
4762 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4763 + " -- can the process access this path?");
4764 } finally {
4765 try {
4766 dhd.fd.close();
4767 } catch (IOException e) {
4768 Slog.w(TAG, "Failure closing profile fd", e);
4769 }
4770 }
4771 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004772 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004773 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004774 try {
4775 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4776 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004777 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004778 }
Andy McFadden824c5102010-07-09 16:26:57 -07004779 }
4780
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004781 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4782 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08004783 switch (cmd) {
4784 case IApplicationThread.PACKAGE_REMOVED:
4785 case IApplicationThread.PACKAGE_REMOVED_DONT_KILL:
4786 {
4787 final boolean killApp = cmd == IApplicationThread.PACKAGE_REMOVED;
4788 if (packages == null) {
4789 break;
4790 }
4791 synchronized (mResourcesManager) {
4792 for (int i = packages.length - 1; i >= 0; i--) {
4793 if (!hasPkgInfo) {
4794 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4795 if (ref != null && ref.get() != null) {
4796 hasPkgInfo = true;
4797 } else {
4798 ref = mResourcePackages.get(packages[i]);
4799 if (ref != null && ref.get() != null) {
4800 hasPkgInfo = true;
4801 }
4802 }
4803 }
4804 if (killApp) {
4805 mPackages.remove(packages[i]);
4806 mResourcePackages.remove(packages[i]);
4807 }
4808 }
4809 }
4810 break;
4811 }
4812 case IApplicationThread.PACKAGE_REPLACED:
4813 {
4814 if (packages == null) {
4815 break;
4816 }
4817 synchronized (mResourcesManager) {
4818 for (int i = packages.length - 1; i >= 0; i--) {
4819 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4820 LoadedApk pkgInfo = ref != null ? ref.get() : null;
4821 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004822 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004823 } else {
4824 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08004825 pkgInfo = ref != null ? ref.get() : null;
4826 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08004827 hasPkgInfo = true;
4828 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004829 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004830 // If the package is being replaced, yet it still has a valid
4831 // LoadedApk object, the package was updated with _DONT_KILL.
4832 // Adjust it's internal references to the application info and
4833 // resources.
4834 if (pkgInfo != null) {
4835 try {
4836 final String packageName = packages[i];
4837 final ApplicationInfo aInfo =
4838 sPackageManager.getApplicationInfo(
4839 packageName,
4840 0 /*flags*/,
4841 UserHandle.myUserId());
4842
4843 if (mActivities.size() > 0) {
4844 for (ActivityClientRecord ar : mActivities.values()) {
4845 if (ar.activityInfo.applicationInfo.packageName
4846 .equals(packageName)) {
4847 ar.activityInfo.applicationInfo = aInfo;
4848 ar.packageInfo = pkgInfo;
4849 }
4850 }
4851 }
4852 final List<String> oldPaths =
4853 sPackageManager.getPreviousCodePaths(packageName);
4854 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
4855 } catch (RemoteException e) {
4856 }
4857 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004858 }
4859 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004860 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004861 }
4862 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004863 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004864 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004866 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004867 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004869 final int N = callbacks.size();
4870 for (int i=0; i<N; i++) {
4871 callbacks.get(i).onLowMemory();
4872 }
4873
Chris Tatece229052009-03-25 16:44:52 -07004874 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4875 if (Process.myUid() != Process.SYSTEM_UID) {
4876 int sqliteReleased = SQLiteDatabase.releaseMemory();
4877 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4878 }
Bob Leee5408332009-09-04 18:31:17 -07004879
Mike Reedcaf0df12009-04-27 14:32:05 -04004880 // Ask graphics to free up as much as possible (font/image caches)
4881 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004882
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004883 // Ask text layout engine to free also as much as possible
4884 Canvas.freeTextLayoutCaches();
4885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004886 BinderInternal.forceGc("mem");
4887 }
4888
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004889 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004890 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004891
Dianne Hackborn73c14162012-09-19 15:45:06 -07004892 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004893
4894 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004895 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004896 callbacks.get(i).onTrimMemory(level);
4897 }
Romain Guy19f86e82012-04-23 15:19:07 -07004898
John Reckf47a5942014-06-30 16:20:04 -07004899 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004900 }
4901
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004902 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004903 if (Process.isIsolated()) {
4904 // Isolated processes aren't going to do UI.
4905 return;
4906 }
Romain Guya9582652011-11-10 14:20:10 -08004907 try {
4908 int uid = Process.myUid();
4909 String[] packages = getPackageManager().getPackagesForUid(uid);
4910
4911 // If there are several packages in this application we won't
Tim Murraye1e6c662015-04-07 13:24:14 -07004912 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004913 if (packages != null && packages.length == 1) {
John Reck51aaf902015-12-02 15:08:07 -08004914 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004915 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004916 }
4917 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004918 throw e.rethrowFromSystemServer();
Romain Guya9582652011-11-10 14:20:10 -08004919 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004920 }
4921
Calin Juravled96d2dd2016-02-02 23:28:39 +00004922 // Keep in sync with installd (frameworks/native/cmds/installd/commands.cpp).
4923 private static File getPrimaryProfileFile(String packageName) {
Calin Juravled479b522016-02-24 16:22:03 +00004924 File profileDir = Environment.getDataProfilesDePackageDirectory(
4925 UserHandle.myUserId(), packageName);
4926 return new File(profileDir, "primary.prof");
Calin Juravled96d2dd2016-02-02 23:28:39 +00004927 }
4928
Calin Juravle69052392015-12-17 17:06:49 +02004929 private static void setupJitProfileSupport(LoadedApk loadedApk, File cacheDir) {
Calin Juravledb4a79a2015-12-23 18:55:08 +02004930 if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
4931 return;
4932 }
Calin Juravle69052392015-12-17 17:06:49 +02004933 final ApplicationInfo appInfo = loadedApk.getApplicationInfo();
Calin Juravle69052392015-12-17 17:06:49 +02004934 final List<String> codePaths = new ArrayList<>();
4935 if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
4936 codePaths.add(appInfo.sourceDir);
4937 }
4938 if (appInfo.splitSourceDirs != null) {
4939 Collections.addAll(codePaths, appInfo.splitSourceDirs);
4940 }
4941
4942 if (codePaths.isEmpty()) {
4943 // If there are no code paths there's no need to setup a profile file and register with
4944 // the runtime,
4945 return;
4946 }
4947
Calin Juravled96d2dd2016-02-02 23:28:39 +00004948 final File profileFile = getPrimaryProfileFile(loadedApk.mPackageName);
Calin Juravle69052392015-12-17 17:06:49 +02004949 if (!profileFile.exists()) {
4950 FileDescriptor fd = null;
4951 try {
4952 final int permissions = 0600; // read-write for user.
4953 fd = Os.open(profileFile.getAbsolutePath(), OsConstants.O_CREAT, permissions);
4954 Os.fchmod(fd, permissions);
4955 Os.fchown(fd, appInfo.uid, appInfo.uid);
4956 } catch (ErrnoException e) {
Calin Juravle6bd212b2016-02-29 16:38:28 +00004957 Log.v(TAG, "Unable to create jit profile file "
4958 + profileFile + ": " + e.getMessage());
Calin Juravle69052392015-12-17 17:06:49 +02004959 try {
4960 Os.unlink(profileFile.getAbsolutePath());
4961 } catch (ErrnoException unlinkErr) {
Calin Juravle6bd212b2016-02-29 16:38:28 +00004962 if (unlinkErr.errno != OsConstants.ENOENT) {
4963 Log.v(TAG, "Unable to unlink jit profile file "
4964 + profileFile + ": " + unlinkErr.getMessage());
4965 }
Calin Juravle69052392015-12-17 17:06:49 +02004966 }
4967 return;
4968 } finally {
4969 IoUtils.closeQuietly(fd);
4970 }
4971 }
4972
Calin Juravled479b522016-02-24 16:22:03 +00004973 final File foreignDexProfilesFile =
4974 Environment.getDataProfilesDeForeignDexDirectory(UserHandle.myUserId());
4975 String foreignDexProfilesPath = null;
4976 if (!foreignDexProfilesFile.exists()) {
4977 Log.v(TAG, "ForeignDexProfilesPath does not exists:" +
4978 foreignDexProfilesFile.getPath());
4979 } else {
4980 foreignDexProfilesPath = foreignDexProfilesFile.getAbsolutePath();
4981 }
Calin Juravle69052392015-12-17 17:06:49 +02004982 VMRuntime.registerAppInfo(profileFile.getAbsolutePath(), appInfo.dataDir,
Calin Juravled479b522016-02-24 16:22:03 +00004983 codePaths.toArray(new String[codePaths.size()]), foreignDexProfilesPath);
Calin Juravle69052392015-12-17 17:06:49 +02004984 }
4985
Dianne Hackborndde331c2012-08-03 14:01:57 -07004986 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05004987 final int densityDpi = mCurDefaultDisplayDpi;
4988 if (!mDensityCompatMode
4989 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
4990 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
4991 DisplayMetrics.DENSITY_DEVICE = densityDpi;
4992 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07004993 }
4994 }
4995
Romain Guy65b345f2011-07-27 18:51:50 -07004996 private void handleBindApplication(AppBindData data) {
Man Caocfa78b22015-06-11 20:14:34 -07004997 if (data.trackAllocation) {
4998 DdmVmInternal.enableRecentAllocations(true);
4999 }
5000
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005001 // Note when this process has started.
5002 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 mBoundApplication = data;
5005 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005006 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005008 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005009 if (data.initProfilerInfo != null) {
5010 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5011 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5012 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5013 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
5014 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005017 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005018 android.ddm.DdmHandleAppName.setAppName(data.processName,
5019 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005021 if (data.persistent) {
5022 // Persistent processes on low-memory devices do not get to
5023 // use hardware accelerated drawing, since this can add too much
5024 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005025 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005026 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005027 }
5028 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005029
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005030 if (mProfiler.profileFd != null) {
5031 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005032 }
5033
Joe Onoratod630f102011-03-17 18:42:26 -07005034 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5035 // implementation to use the pool executor. Normally, we use the
5036 // serialized executor as the default. This has to happen in the
5037 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005038 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005039 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5040 }
5041
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005042 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 /*
5045 * Before spawning a new process, reset the time zone to be the system time zone.
5046 * This needs to be done because the system time zone could have changed after the
5047 * the spawning of this process. Without doing this this process would have the incorrect
5048 * system time zone.
5049 */
5050 TimeZone.setDefault(null);
5051
Adam Lesinski082614c2016-03-04 14:33:47 -08005052 synchronized (mResourcesManager) {
5053 /*
5054 * Initialize the default locales in this process for the reasons we set the time zone.
5055 *
5056 * We do this through ResourcesManager, since we need to do locale negotiation.
5057 */
5058 mResourcesManager.setDefaultLocalesLocked(data.config.getLocales());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059
Adam Lesinski082614c2016-03-04 14:33:47 -08005060 /*
5061 * Update the system configuration since its preloaded and might not
5062 * reflect configuration changes. The configuration object passed
5063 * in AppBindData can be safely assumed to be up to date
5064 */
5065 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5066 mCurDefaultDisplayDpi = data.config.densityDpi;
5067
5068 // This calls mResourcesManager so keep it within the synchronized block.
5069 applyCompatConfiguration(mCurDefaultDisplayDpi);
5070 }
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07005071
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005072 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005073
Dianne Hackborndde331c2012-08-03 14:01:57 -07005074 /**
5075 * Switch this process to density compatibility mode if needed.
5076 */
5077 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5078 == 0) {
5079 mDensityCompatMode = true;
5080 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5081 }
5082 updateDefaultDensity();
5083
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005084 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
5085 DateFormat.set24HourTimePref(is24Hr);
5086
Jon Miranda836c0a82014-08-11 12:32:26 -07005087 View.mDebugViewAttributes =
5088 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5089
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005090 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005091 * For system applications on userdebug/eng builds, log stack
5092 * traces of disk and network access to dropbox for analysis.
5093 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005094 if ((data.appInfo.flags &
5095 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005096 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5097 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005098 }
5099
5100 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005101 * For apps targetting Honeycomb or later, we don't allow network usage
5102 * on the main event loop / UI thread. This is what ultimately throws
5103 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005104 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005105 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005106 StrictMode.enableDeathOnNetwork();
5107 }
5108
Jeff Sharkey344744b2016-01-28 19:03:30 -07005109 /**
5110 * For apps targetting N or later, we don't allow file:// Uri exposure.
5111 * This is what ultimately throws {@link FileUriExposedException}.
5112 */
5113 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005114 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005115 }
5116
Alex Klyubinf9034cc2015-02-12 11:43:09 -08005117 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
5118 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
5119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5121 // XXX should have option to change the port.
5122 Debug.changeDebugPort(8100);
5123 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005124 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 + " is waiting for the debugger on port 8100...");
5126
5127 IActivityManager mgr = ActivityManagerNative.getDefault();
5128 try {
5129 mgr.showWaitingForDebugger(mAppThread, true);
5130 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005131 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 }
5133
5134 Debug.waitForDebugger();
5135
5136 try {
5137 mgr.showWaitingForDebugger(mAppThread, false);
5138 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005139 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
5141
5142 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005143 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 + " can be debugged on port 8100...");
5145 }
5146 }
5147
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005148 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005149 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5150 Trace.setAppTracingAllowed(isAppDebuggable);
5151 if (isAppDebuggable && data.enableBinderTracking) {
5152 Binder.enableTracing();
5153 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005154
Robert Greenwalt434203a2010-10-11 16:00:27 -07005155 /**
5156 * Initialize the default http proxy in this process for the reasons we set the time zone.
5157 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04005158 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005159 if (b != null) {
5160 // In pre-boot mode (doing initial launch to collect password), not
5161 // all system is up. This includes the connectivity service, so don't
5162 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005163 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005164 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005165 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005166 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005167 } catch (RemoteException e) {
5168 throw e.rethrowFromSystemServer();
5169 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005170 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07005171
Alan Viverette2107d692015-09-03 14:55:27 -04005172 // Instrumentation info affects the class loader, so load it before
5173 // setting up the app context.
5174 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005177 ii = new ApplicationPackageManager(null, getPackageManager())
5178 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005180 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005181 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 }
5183
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005184 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005186 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
5187 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005189 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5190 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005191 } else {
5192 ii = null;
5193 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005194
Alan Viverette2107d692015-09-03 14:55:27 -04005195 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005196 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005197 // This cache location probably points at credential-encrypted
5198 // storage which may not be accessible yet; assign it anyway instead
5199 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005200 final File cacheDir = appContext.getCacheDir();
5201 if (cacheDir != null) {
5202 // Provide a usable directory for temporary files
5203 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5204 } else {
5205 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5206 + "due to missing cache directory");
5207 }
Alan Viverette346296b2015-09-01 13:08:05 -04005208
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005209 // Setup a location to store generated/compiled graphics code and
5210 // JIT profiling data. Note that this data is stored in a
5211 // device-encrypted storage area, so these caches must never contain
5212 // user sensitive user data.
5213 final Context deviceContext = appContext.createDeviceEncryptedStorageContext();
5214 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005215 if (codeCacheDir != null) {
5216 setupGraphicsSupport(data.info, codeCacheDir);
Calin Juravlec74d3382016-01-07 12:15:36 +00005217 setupJitProfileSupport(data.info, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005218 } else {
Calin Juravlec74d3382016-01-07 12:15:36 +00005219 Log.e(TAG, "Unable to setupGraphicsSupport and setupJitProfileSupport " +
5220 "due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005221 }
Alan Viverette2107d692015-09-03 14:55:27 -04005222
Michael Lentine2ba303f2016-02-01 20:44:34 -06005223 // Add the lib dir path to hardware renderer so that vulkan layers
5224 // can be searched for within that directory.
5225 ThreadedRenderer.setLibDir(data.info.getLibDir());
5226 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005227
Chad Brubaker78d47122015-11-17 22:26:58 -08005228 // Install the Network Security Config Provider. This must happen before the application
5229 // code is loaded to prevent issues with instances of TLS objects being created before
5230 // the provider is installed.
5231 NetworkSecurityConfigProvider.install(appContext);
5232
Alan Viverette2107d692015-09-03 14:55:27 -04005233 // Continue loading instrumentation.
5234 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005235 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005236 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005237 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005238 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005239 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005240 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241
5242 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005243 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 mInstrumentation = (Instrumentation)
5245 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5246 } catch (Exception e) {
5247 throw new RuntimeException(
5248 "Unable to instantiate instrumentation "
5249 + data.instrumentationName + ": " + e.toString(), e);
5250 }
5251
Alan Viverettebe64eae2015-09-03 14:56:04 -04005252 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5253 mInstrumentation.init(this, instrContext, appContext, component,
5254 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005256 if (mProfiler.profileFile != null && !ii.handleProfiling
5257 && mProfiler.profileFd == null) {
5258 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005259 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005260 file.getParentFile().mkdirs();
5261 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263 } else {
5264 mInstrumentation = new Instrumentation();
5265 }
5266
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005267 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005268 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005269 } else {
5270 // Small heap, clamp to the current growth limit and let the heap release
5271 // pages after the growth limit to the non growth limit capacity. b/18387825
5272 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005273 }
5274
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005275 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005276 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005277 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005278 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005279 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005280 // If the app is being launched for full backup or restore, bring it up in
5281 // a restricted environment with the base application class.
5282 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5283 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005284
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005285 // don't bring up providers in restricted mode; they may depend on the
5286 // app's custom Application class
5287 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005288 if (!ArrayUtils.isEmpty(data.providers)) {
5289 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005290 // For process that contains content providers, we want to
5291 // ensure that the JIT is enabled "at some point".
5292 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5293 }
5294 }
5295
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005296 // Do this after providers, since instrumentation tests generally start their
5297 // test thread at this point, and we don't want that racing.
5298 try {
5299 mInstrumentation.onCreate(data.instrumentationArgs);
5300 }
5301 catch (Exception e) {
5302 throw new RuntimeException(
5303 "Exception thrown in onCreate() of "
5304 + data.instrumentationName + ": " + e.toString(), e);
5305 }
5306
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005307 try {
5308 mInstrumentation.callApplicationOnCreate(app);
5309 } catch (Exception e) {
5310 if (!mInstrumentation.onException(app, e)) {
5311 throw new RuntimeException(
5312 "Unable to create application " + app.getClass().getName()
5313 + ": " + e.toString(), e);
5314 }
5315 }
5316 } finally {
5317 StrictMode.setThreadPolicy(savedPolicy);
5318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 }
5320
5321 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5322 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005323 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5324 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005325 Debug.stopMethodTracing();
5326 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005327 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005328 // + ", app thr: " + mAppThread);
5329 try {
5330 am.finishInstrumentation(mAppThread, resultCode, results);
5331 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005332 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005333 }
5334 }
5335
Romain Guy65b345f2011-07-27 18:51:50 -07005336 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337 Context context, List<ProviderInfo> providers) {
5338 final ArrayList<IActivityManager.ContentProviderHolder> results =
5339 new ArrayList<IActivityManager.ContentProviderHolder>();
5340
Romain Guya998dff2012-03-23 18:58:36 -07005341 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005342 if (DEBUG_PROVIDER) {
5343 StringBuilder buf = new StringBuilder(128);
5344 buf.append("Pub ");
5345 buf.append(cpi.authority);
5346 buf.append(": ");
5347 buf.append(cpi.name);
5348 Log.i(TAG, buf.toString());
5349 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005350 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5351 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5352 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005353 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355 }
5356 }
5357
5358 try {
5359 ActivityManagerNative.getDefault().publishContentProviders(
5360 getApplicationThread(), results);
5361 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005362 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005363 }
5364 }
5365
Jeff Sharkey6d515712012-09-20 16:06:08 -07005366 public final IContentProvider acquireProvider(
5367 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005368 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005369 if (provider != null) {
5370 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 }
5372
Wale Ogunwale1d646122015-04-24 14:45:14 -07005373 // There is a possible race here. Another thread may try to acquire
5374 // the same provider at the same time. When this happens, we want to ensure
5375 // that the first one wins.
5376 // Note that we cannot hold the lock while acquiring and installing the
5377 // provider since it might take a long time to run and it could also potentially
5378 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005380 try {
5381 holder = ActivityManagerNative.getDefault().getContentProvider(
5382 getApplicationThread(), auth, userId, stable);
5383 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005384 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005385 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005387 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005388 return null;
5389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005391 // Install provider will increment the reference count for us, and break
5392 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005393 holder = installProvider(c, holder, holder.info,
5394 true /*noisy*/, holder.noReleaseNeeded, stable);
5395 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005396 }
5397
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005398 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5399 if (stable) {
5400 prc.stableCount += 1;
5401 if (prc.stableCount == 1) {
5402 // We are acquiring a new stable reference on the provider.
5403 int unstableDelta;
5404 if (prc.removePending) {
5405 // We have a pending remove operation, which is holding the
5406 // last unstable reference. At this point we are converting
5407 // that unstable reference to our new stable reference.
5408 unstableDelta = -1;
5409 // Cancel the removal of the provider.
5410 if (DEBUG_PROVIDER) {
5411 Slog.v(TAG, "incProviderRef: stable "
5412 + "snatched provider from the jaws of death");
5413 }
5414 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005415 // There is a race! It fails to remove the message, which
5416 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005417 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5418 } else {
5419 unstableDelta = 0;
5420 }
5421 try {
5422 if (DEBUG_PROVIDER) {
5423 Slog.v(TAG, "incProviderRef Now stable - "
5424 + prc.holder.info.name + ": unstableDelta="
5425 + unstableDelta);
5426 }
5427 ActivityManagerNative.getDefault().refContentProvider(
5428 prc.holder.connection, 1, unstableDelta);
5429 } catch (RemoteException e) {
5430 //do nothing content provider object is dead any way
5431 }
5432 }
5433 } else {
5434 prc.unstableCount += 1;
5435 if (prc.unstableCount == 1) {
5436 // We are acquiring a new unstable reference on the provider.
5437 if (prc.removePending) {
5438 // Oh look, we actually have a remove pending for the
5439 // provider, which is still holding the last unstable
5440 // reference. We just need to cancel that to take new
5441 // ownership of the reference.
5442 if (DEBUG_PROVIDER) {
5443 Slog.v(TAG, "incProviderRef: unstable "
5444 + "snatched provider from the jaws of death");
5445 }
5446 prc.removePending = false;
5447 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5448 } else {
5449 // First unstable ref, increment our count in the
5450 // activity manager.
5451 try {
5452 if (DEBUG_PROVIDER) {
5453 Slog.v(TAG, "incProviderRef: Now unstable - "
5454 + prc.holder.info.name);
5455 }
5456 ActivityManagerNative.getDefault().refContentProvider(
5457 prc.holder.connection, 0, 1);
5458 } catch (RemoteException e) {
5459 //do nothing content provider object is dead any way
5460 }
5461 }
5462 }
5463 }
5464 }
5465
Jeff Sharkey6d515712012-09-20 16:06:08 -07005466 public final IContentProvider acquireExistingProvider(
5467 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005468 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005469 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005470 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005471 if (pr == null) {
5472 return null;
5473 }
5474
5475 IContentProvider provider = pr.mProvider;
5476 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005477 if (!jBinder.isBinderAlive()) {
5478 // The hosting process of the provider has died; we can't
5479 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005480 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005481 + ": existing object's process dead");
5482 handleUnstableProviderDiedLocked(jBinder, true);
5483 return null;
5484 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005485
5486 // Only increment the ref count if we have one. If we don't then the
5487 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005488 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005489 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005490 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005491 }
5492 return provider;
5493 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005494 }
5495
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005496 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5497 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 return false;
5499 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005501 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005502 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005503 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005504 if (prc == null) {
5505 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005506 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005507 }
5508
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005509 boolean lastRef = false;
5510 if (stable) {
5511 if (prc.stableCount == 0) {
5512 if (DEBUG_PROVIDER) Slog.v(TAG,
5513 "releaseProvider: stable ref count already 0, how?");
5514 return false;
5515 }
5516 prc.stableCount -= 1;
5517 if (prc.stableCount == 0) {
5518 // What we do at this point depends on whether there are
5519 // any unstable refs left: if there are, we just tell the
5520 // activity manager to decrement its stable count; if there
5521 // aren't, we need to enqueue this provider to be removed,
5522 // and convert to holding a single unstable ref while
5523 // doing so.
5524 lastRef = prc.unstableCount == 0;
5525 try {
5526 if (DEBUG_PROVIDER) {
5527 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5528 + lastRef + " - " + prc.holder.info.name);
5529 }
5530 ActivityManagerNative.getDefault().refContentProvider(
5531 prc.holder.connection, -1, lastRef ? 1 : 0);
5532 } catch (RemoteException e) {
5533 //do nothing content provider object is dead any way
5534 }
5535 }
5536 } else {
5537 if (prc.unstableCount == 0) {
5538 if (DEBUG_PROVIDER) Slog.v(TAG,
5539 "releaseProvider: unstable ref count already 0, how?");
5540 return false;
5541 }
5542 prc.unstableCount -= 1;
5543 if (prc.unstableCount == 0) {
5544 // If this is the last reference, we need to enqueue
5545 // this provider to be removed instead of telling the
5546 // activity manager to remove it at this point.
5547 lastRef = prc.stableCount == 0;
5548 if (!lastRef) {
5549 try {
5550 if (DEBUG_PROVIDER) {
5551 Slog.v(TAG, "releaseProvider: No longer unstable - "
5552 + prc.holder.info.name);
5553 }
5554 ActivityManagerNative.getDefault().refContentProvider(
5555 prc.holder.connection, 0, -1);
5556 } catch (RemoteException e) {
5557 //do nothing content provider object is dead any way
5558 }
5559 }
5560 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005561 }
5562
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005563 if (lastRef) {
5564 if (!prc.removePending) {
5565 // Schedule the actual remove asynchronously, since we don't know the context
5566 // this will be called in.
5567 // TODO: it would be nice to post a delayed message, so
5568 // if we come back and need the same provider quickly
5569 // we will still have it available.
5570 if (DEBUG_PROVIDER) {
5571 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5572 + prc.holder.info.name);
5573 }
5574 prc.removePending = true;
5575 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5576 mH.sendMessage(msg);
5577 } else {
5578 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5579 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005580 }
5581 return true;
5582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 }
5584
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005585 final void completeRemoveProvider(ProviderRefCount prc) {
5586 synchronized (mProviderMap) {
5587 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005588 // There was a race! Some other client managed to acquire
5589 // the provider before the removal was completed.
5590 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005591 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005592 + "provider still in use");
5593 return;
5594 }
5595
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005596 // More complicated race!! Some client managed to acquire the
5597 // provider and release it before the removal was completed.
5598 // Continue the removal, and abort the next remove message.
5599 prc.removePending = false;
5600
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005601 final IBinder jBinder = prc.holder.provider.asBinder();
5602 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5603 if (existingPrc == prc) {
5604 mProviderRefCountMap.remove(jBinder);
5605 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005606
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005607 for (int i=mProviderMap.size()-1; i>=0; i--) {
5608 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005609 IBinder myBinder = pr.mProvider.asBinder();
5610 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005611 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005612 }
5613 }
5614 }
5615
5616 try {
5617 if (DEBUG_PROVIDER) {
5618 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5619 + "removeContentProvider(" + prc.holder.info.name + ")");
5620 }
5621 ActivityManagerNative.getDefault().removeContentProvider(
5622 prc.holder.connection, false);
5623 } catch (RemoteException e) {
5624 //do nothing content provider object is dead any way
5625 }
5626 }
5627
5628 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005629 synchronized (mProviderMap) {
5630 handleUnstableProviderDiedLocked(provider, fromClient);
5631 }
5632 }
5633
5634 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5635 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5636 if (prc != null) {
5637 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5638 + provider + " " + prc.holder.info.name);
5639 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005640 for (int i=mProviderMap.size()-1; i>=0; i--) {
5641 ProviderClientRecord pr = mProviderMap.valueAt(i);
5642 if (pr != null && pr.mProvider.asBinder() == provider) {
5643 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5644 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005645 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005646 }
You Kimbc74de62013-10-01 00:13:26 +09005647
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005648 if (fromClient) {
5649 // We found out about this due to execution in our client
5650 // code. Tell the activity manager about it now, to ensure
5651 // that the next time we go to do anything with the provider
5652 // it knows it is dead (so we don't race with its death
5653 // notification).
5654 try {
5655 ActivityManagerNative.getDefault().unstableProviderDied(
5656 prc.holder.connection);
5657 } catch (RemoteException e) {
5658 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005659 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005660 }
5661 }
5662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005663
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005664 final void appNotRespondingViaProvider(IBinder provider) {
5665 synchronized (mProviderMap) {
5666 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5667 if (prc != null) {
5668 try {
5669 ActivityManagerNative.getDefault()
5670 .appNotRespondingViaProvider(prc.holder.connection);
5671 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005672 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005673 }
5674 }
5675 }
5676 }
5677
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005678 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005679 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005680 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005681 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5682
5683 final ProviderClientRecord pcr = new ProviderClientRecord(
5684 auths, provider, localProvider, holder);
5685 for (String auth : auths) {
5686 final ProviderKey key = new ProviderKey(auth, userId);
5687 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005688 if (existing != null) {
5689 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005690 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005691 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005692 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 }
5694 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005695 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 }
5697
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005698 /**
5699 * Installs the provider.
5700 *
5701 * Providers that are local to the process or that come from the system server
5702 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5703 * Other remote providers are reference counted. The initial reference count
5704 * for all reference counted providers is one. Providers that are not reference
5705 * counted do not have a reference count (at all).
5706 *
5707 * This method detects when a provider has already been installed. When this happens,
5708 * it increments the reference count of the existing provider (if appropriate)
5709 * and returns the existing provider. This can happen due to concurrent
5710 * attempts to acquire the same provider.
5711 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005712 private IActivityManager.ContentProviderHolder installProvider(Context context,
5713 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5714 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005716 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005717 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005718 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005719 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 + info.name);
5721 }
5722 Context c = null;
5723 ApplicationInfo ai = info.applicationInfo;
5724 if (context.getPackageName().equals(ai.packageName)) {
5725 c = context;
5726 } else if (mInitialApplication != null &&
5727 mInitialApplication.getPackageName().equals(ai.packageName)) {
5728 c = mInitialApplication;
5729 } else {
5730 try {
5731 c = context.createPackageContext(ai.packageName,
5732 Context.CONTEXT_INCLUDE_CODE);
5733 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005734 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 }
5736 }
5737 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005738 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 ai.packageName +
5740 " while loading content provider " +
5741 info.name);
5742 return null;
5743 }
5744 try {
5745 final java.lang.ClassLoader cl = c.getClassLoader();
5746 localProvider = (ContentProvider)cl.
5747 loadClass(info.name).newInstance();
5748 provider = localProvider.getIContentProvider();
5749 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005750 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 info.name + " from sourceDir " +
5752 info.applicationInfo.sourceDir);
5753 return null;
5754 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005755 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005756 TAG, "Instantiating local provider " + info.name);
5757 // XXX Need to create the correct context for this provider.
5758 localProvider.attachInfo(c, info);
5759 } catch (java.lang.Exception e) {
5760 if (!mInstrumentation.onException(null, e)) {
5761 throw new RuntimeException(
5762 "Unable to get provider " + info.name
5763 + ": " + e.toString(), e);
5764 }
5765 return null;
5766 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005767 } else {
5768 provider = holder.provider;
5769 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 + info.name);
5771 }
5772
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005773 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005774
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005775 synchronized (mProviderMap) {
5776 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5777 + " / " + info.name);
5778 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005780 ComponentName cname = new ComponentName(info.packageName, info.name);
5781 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005782 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005783 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005784 Slog.v(TAG, "installProvider: lost the race, "
5785 + "using existing local provider");
5786 }
5787 provider = pr.mProvider;
5788 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005789 holder = new IActivityManager.ContentProviderHolder(info);
5790 holder.provider = provider;
5791 holder.noReleaseNeeded = true;
5792 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005793 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005794 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005795 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005796 retHolder = pr.mHolder;
5797 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005798 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5799 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005800 if (DEBUG_PROVIDER) {
5801 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005802 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005803 // We need to transfer our new reference to the existing
5804 // ref count, releasing the old one... but only if
5805 // release is needed (that is, it is not running in the
5806 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005807 if (!noReleaseNeeded) {
5808 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005809 try {
5810 ActivityManagerNative.getDefault().removeContentProvider(
5811 holder.connection, stable);
5812 } catch (RemoteException e) {
5813 //do nothing content provider object is dead any way
5814 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005815 }
5816 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005817 ProviderClientRecord client = installProviderAuthoritiesLocked(
5818 provider, localProvider, holder);
5819 if (noReleaseNeeded) {
5820 prc = new ProviderRefCount(holder, client, 1000, 1000);
5821 } else {
5822 prc = stable
5823 ? new ProviderRefCount(holder, client, 1, 0)
5824 : new ProviderRefCount(holder, client, 0, 1);
5825 }
5826 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005827 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005828 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 }
5830 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005831
5832 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005833 }
5834
Romain Guy65b345f2011-07-27 18:51:50 -07005835 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005836 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005837 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005839 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005840 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005841 public void run() {
5842 ensureJitEnabled();
5843 }
5844 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005845 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5846 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005847 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005848 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 try {
5850 mgr.attachApplication(mAppThread);
5851 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005852 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005854 // Watch for getting close to heap limit.
5855 BinderInternal.addGcWatcher(new Runnable() {
5856 @Override public void run() {
5857 if (!mSomeActivitiesChanged) {
5858 return;
5859 }
5860 Runtime runtime = Runtime.getRuntime();
5861 long dalvikMax = runtime.maxMemory();
5862 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5863 if (dalvikUsed > ((3*dalvikMax)/4)) {
5864 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5865 + " total=" + (runtime.totalMemory()/1024)
5866 + " used=" + (dalvikUsed/1024));
5867 mSomeActivitiesChanged = false;
5868 try {
5869 mgr.releaseSomeActivities(mAppThread);
5870 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005871 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005872 }
5873 }
5874 }
5875 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 } else {
5877 // Don't set application object here -- if the system crashes,
5878 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005879 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005880 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 try {
5882 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005883 ContextImpl context = ContextImpl.createAppContext(
5884 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005885 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5886 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005887 } catch (Exception e) {
5888 throw new RuntimeException(
5889 "Unable to instantiate Application():" + e.toString(), e);
5890 }
5891 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005892
5893 // add dropbox logging to libcore
5894 DropBox.setReporter(new DropBoxReporter());
5895
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005896 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005897 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005898 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005899 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005900 // We need to apply this change to the resources
5901 // immediately, because upon returning the view
5902 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005903 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005904 // This actually changed the resources! Tell
5905 // everyone about it.
5906 if (mPendingConfiguration == null ||
5907 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5908 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005909
Jeff Brown9ef09972013-10-15 20:49:59 -07005910 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005911 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005912 }
5913 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005914 }
Craig Mautner88c05892013-06-28 09:47:45 -07005915 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005916 public void onLowMemory() {
5917 }
Craig Mautner88c05892013-06-28 09:47:45 -07005918 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005919 public void onTrimMemory(int level) {
5920 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005921 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005922 }
5923
Romain Guy5e9120d2012-01-30 12:17:22 -08005924 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005925 // The system process on low-memory devices do not get to use hardware
5926 // accelerated drawing, since this can add too much overhead to the
5927 // process.
5928 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005929 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005930 } else {
John Reck51aaf902015-12-02 15:08:07 -08005931 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005933 ActivityThread thread = new ActivityThread();
5934 thread.attach(true);
5935 return thread;
5936 }
5937
Jeff Brown10e89712011-07-08 18:52:57 -07005938 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005939 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005940 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005941 }
5942 }
5943
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005944 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005945 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005946 if (mCoreSettings != null) {
5947 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005948 }
Craig Mautner88c05892013-06-28 09:47:45 -07005949 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005950 }
5951 }
5952
Geremy Condra69689a72012-09-11 16:57:17 -07005953 private static class EventLoggingReporter implements EventLogger.Reporter {
5954 @Override
5955 public void report (int code, Object... list) {
5956 EventLog.writeEvent(code, list);
5957 }
5958 }
5959
Geremy Condrab7faaf42012-09-19 18:07:42 -07005960 private class DropBoxReporter implements DropBox.Reporter {
5961
5962 private DropBoxManager dropBox;
5963
Narayan Kamath7f062242015-04-08 13:24:13 +01005964 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005965
5966 @Override
5967 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005968 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005969 dropBox.addData(tag, data, flags);
5970 }
5971
5972 @Override
5973 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005974 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005975 dropBox.addText(tag, data);
5976 }
Narayan Kamath7f062242015-04-08 13:24:13 +01005977
5978 private synchronized void ensureInitialized() {
5979 if (dropBox == null) {
5980 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5981 }
5982 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005983 }
5984
Romain Guy65b345f2011-07-27 18:51:50 -07005985 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005986 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07005987 SamplingProfilerIntegration.start();
5988
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005989 // CloseGuard defaults to true and can be quite spammy. We
5990 // disable it here, but selectively enable it later (via
5991 // StrictMode) on debug builds, but using DropBox, not logs.
5992 CloseGuard.setEnabled(false);
5993
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005994 Environment.initForCurrentUser();
5995
Geremy Condra69689a72012-09-11 16:57:17 -07005996 // Set the reporter for event logging in libcore
5997 EventLogger.setReporter(new EventLoggingReporter());
5998
Alex Klyubin48125632015-04-29 13:16:30 -07005999 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08006000
Robin Lee3d076af2014-04-25 14:57:49 +01006001 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6002 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6003 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006005 Process.setArgV0("<pre-initialized>");
6006
6007 Looper.prepareMainLooper();
6008
6009 ActivityThread thread = new ActivityThread();
6010 thread.attach(false);
6011
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006012 if (sMainThreadHandler == null) {
6013 sMainThreadHandler = thread.getHandler();
6014 }
6015
Dianne Hackborn287952c2010-09-22 22:34:31 -07006016 if (false) {
6017 Looper.myLooper().setMessageLogging(new
6018 LogPrinter(Log.DEBUG, "ActivityThread"));
6019 }
6020
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006021 // End of event ActivityThreadMain.
6022 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006023 Looper.loop();
6024
Jeff Brown10e89712011-07-08 18:52:57 -07006025 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006026 }
6027}