blob: c96757456b32dd288251d0cf270a44e756ea8aca [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;
Adam Lesinski3ad1b482016-04-01 16:41:41 -070094import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -080096import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.view.View;
98import android.view.ViewDebug;
99import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -0700100import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.Window;
102import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700103import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700104import android.renderscript.RenderScriptCacheDir;
Alex Klyubindcdaf872015-05-13 15:57:09 -0700105import android.security.keystore.AndroidKeyStoreProvider;
Calin Juravle69052392015-12-17 17:06:49 +0200106import android.system.Os;
107import android.system.OsConstants;
108import android.system.ErrnoException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700110import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700111import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800112import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113import com.android.internal.os.BinderInternal;
114import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700115import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700116import com.android.internal.os.SomeArgs;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700117import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700118import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700119import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100120import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700121import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123import java.io.File;
124import java.io.FileDescriptor;
125import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700126import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127import java.io.PrintWriter;
128import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700129import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000130import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000132import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800135import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Geremy Condrab7faaf42012-09-19 18:07:42 -0700138import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700139import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800140import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400141import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800142import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700143import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700144import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700145import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700146
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700147final class RemoteServiceException extends AndroidRuntimeException {
148 public RemoteServiceException(String msg) {
149 super(msg);
150 }
151}
152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153/**
154 * This manages the execution of the main thread in an
155 * application process, scheduling and executing activities,
156 * broadcasts, and other operations on it as the activity
157 * manager requests.
158 *
159 * {@hide}
160 */
161public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700162 /** @hide */
163 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700164 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700165 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700166 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700167 /** @hide */
168 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700169 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700170 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700171 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800172 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700173 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700174 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700175 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800178 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
179 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800180 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181
Dianne Hackborn455625e2015-01-21 09:55:13 -0800182 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
183 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
184 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
185 public static final int SERVICE_DONE_EXECUTING_START = 1;
186 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
187 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
188
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700189 // Details for pausing activity.
190 private static final int USER_LEAVING = 1;
191 private static final int DONT_REPORT = 2;
192
Filip Gruszczynskica664812015-12-04 12:43:36 -0800193 // Whether to invoke an activity callback after delivering new configuration.
194 private static final boolean REPORT_TO_ACTIVITY = true;
195
Jeff Browndefd4a62014-03-10 21:24:37 -0700196 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700197
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700198 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700200 final ApplicationThread mAppThread = new ApplicationThread();
201 final Looper mLooper = Looper.myLooper();
202 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700203 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700204 // List of new activities (via ActivityRecord.nextIdle) that should
205 // be reported when next we idle.
206 ActivityClientRecord mNewActivities = null;
207 // Number of activities that are currently visible on-screen.
208 int mNumVisibleActivities = 0;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700209 ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
210 private int mLastSessionId;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700211 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700212 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700213 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700214 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700215 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700216 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700217 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700218 Application mInitialApplication;
219 final ArrayList<Application> mAllApplications
220 = new ArrayList<Application>();
221 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700222 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800223 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700224 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700225 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700226 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700227 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700228 String[] mInstrumentationSplitAppDirs = null;
229 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700230 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700231 String[] mInstrumentedSplitAppDirs = null;
232 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700233 boolean mSystemThread = false;
234 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700235 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700237 // These can be accessed by multiple threads; mPackages is the lock.
238 // XXX For now we keep around information about all packages we have
239 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800240 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700241 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800242 // which means this lock gets held while the activity and window managers
243 // holds their own lock. Thus you MUST NEVER call back into the activity manager
244 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700245 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700246 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
247 = new ArrayMap<String, WeakReference<LoadedApk>>();
248 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
249 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700250 final ArrayList<ActivityClientRecord> mRelaunchingActivities
251 = new ArrayList<ActivityClientRecord>();
252 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700253 // Because we merge activity relaunch operations we can't depend on the ordering provided by
254 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
255 // us to drop certain events, if we know that they happened before relaunch we already executed.
256 // This represents the order of receiving the request from AM.
257 @GuardedBy("mResourcesManager")
258 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259
Craig Mautner88c05892013-06-28 09:47:45 -0700260 private final ResourcesManager mResourcesManager;
261
Jeff Sharkey6d515712012-09-20 16:06:08 -0700262 private static final class ProviderKey {
263 final String authority;
264 final int userId;
265
266 public ProviderKey(String authority, int userId) {
267 this.authority = authority;
268 this.userId = userId;
269 }
270
271 @Override
272 public boolean equals(Object o) {
273 if (o instanceof ProviderKey) {
274 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800275 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700276 }
277 return false;
278 }
279
280 @Override
281 public int hashCode() {
282 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
283 }
284 }
285
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700286 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700287 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
288 = new ArrayMap<ProviderKey, ProviderClientRecord>();
289 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
290 = new ArrayMap<IBinder, ProviderRefCount>();
291 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
292 = new ArrayMap<IBinder, ProviderClientRecord>();
293 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
294 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700296 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700297 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600298
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700299 final GcIdler mGcIdler = new GcIdler();
300 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700302 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700303
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800304 Bundle mCoreSettings = null;
305
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400306 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700308 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800310 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700311 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700313 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 Activity activity;
315 Window window;
316 Activity parent;
317 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700318 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 boolean paused;
320 boolean stopped;
321 boolean hideForNow;
322 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700323 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800324 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800325 // Used for consolidating configs before sending on to Activity.
326 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700327 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328
Jeff Hao1b012d32014-08-20 10:35:34 -0700329 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400332 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700333 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
335 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800336 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
338 boolean startsNotResumed;
339 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800340 int pendingConfigChanges;
341 boolean onlyLocalRequest;
342
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700343 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800344 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700345 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700347 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
348 // can be compared with other lifecycle operations.
349 int relaunchSeq = 0;
350
351 // Can only be accessed from the UI thread. This represents the latest processed message
352 // that is related to lifecycle events/
353 int lastProcessedSeq = 0;
354
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700355 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 parent = null;
357 embeddedID = null;
358 paused = false;
359 stopped = false;
360 hideForNow = false;
361 nextIdle = null;
362 }
363
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800364 public boolean isPreHoneycomb() {
365 if (activity != null) {
366 return activity.getApplicationInfo().targetSdkVersion
367 < android.os.Build.VERSION_CODES.HONEYCOMB;
368 }
369 return false;
370 }
371
Craig Mautnera0026042014-04-23 11:45:37 -0700372 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700373 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700374 }
375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700377 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 return "ActivityRecord{"
379 + Integer.toHexString(System.identityHashCode(this))
380 + " token=" + token + " " + (componentName == null
381 ? "no component name" : componentName.toShortString())
382 + "}";
383 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700384
385 public String getStateString() {
386 StringBuilder sb = new StringBuilder();
387 sb.append("ActivityClientRecord{");
388 sb.append("paused=").append(paused);
389 sb.append(", stopped=").append(stopped);
390 sb.append(", hideForNow=").append(hideForNow);
391 sb.append(", startsNotResumed=").append(startsNotResumed);
392 sb.append(", isForward=").append(isForward);
393 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
394 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
395 sb.append(", preserveWindow=").append(mPreserveWindow);
396 if (activity != null) {
397 sb.append(", Activity{");
398 sb.append("resumed=").append(activity.mResumed);
399 sb.append(", stopped=").append(activity.mStopped);
400 sb.append(", finished=").append(activity.isFinishing());
401 sb.append(", destroyed=").append(activity.isDestroyed());
402 sb.append(", startedActivity=").append(activity.mStartedActivity);
403 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
404 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
405 sb.append(", visibleBehind=").append(activity.mVisibleBehind);
406 sb.append("}");
407 }
408 sb.append("}");
409 return sb.toString();
410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 }
412
Wale Ogunwale1d646122015-04-24 14:45:14 -0700413 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700414 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 final IContentProvider mProvider;
416 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700417 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700419 ProviderClientRecord(String[] names, IContentProvider provider,
420 ContentProvider localProvider,
421 IActivityManager.ContentProviderHolder holder) {
422 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 mProvider = provider;
424 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700425 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 }
427 }
428
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400429 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800430 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 IBinder token;
432 public String toString() {
433 return "NewIntentData{intents=" + intents + " token=" + token + "}";
434 }
435 }
436
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400437 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700438 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700439 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
440 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800441 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700442 this.intent = intent;
443 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 Intent intent;
446 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400447 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 public String toString() {
449 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700450 info.packageName + " resultCode=" + getResultCode()
451 + " resultData=" + getResultData() + " resultExtras="
452 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 }
454 }
455
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700457 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400458 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700459 int backupMode;
460 public String toString() {
461 return "CreateBackupAgentData{appInfo=" + appInfo
462 + " backupAgent=" + appInfo.backupAgentName
463 + " mode=" + backupMode + "}";
464 }
465 }
Bob Leee5408332009-09-04 18:31:17 -0700466
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 IBinder token;
469 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400470 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 Intent intent;
472 public String toString() {
473 return "CreateServiceData{token=" + token + " className="
474 + info.name + " packageName=" + info.packageName
475 + " intent=" + intent + "}";
476 }
477 }
478
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400479 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 IBinder token;
481 Intent intent;
482 boolean rebind;
483 public String toString() {
484 return "BindServiceData{token=" + token + " intent=" + intent + "}";
485 }
486 }
487
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400488 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700490 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700492 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 Intent args;
494 public String toString() {
495 return "ServiceArgsData{token=" + token + " startId=" + startId
496 + " args=" + args + "}";
497 }
498 }
499
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400500 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700501 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 String processName;
503 ApplicationInfo appInfo;
504 List<ProviderInfo> providers;
505 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 Bundle instrumentationArgs;
507 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800508 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400510 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700511 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700512 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700513 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400515 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700516
517 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700518 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 public String toString() {
521 return "AppBindData{appInfo=" + appInfo + "}";
522 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700523 }
524
525 static final class Profiler {
526 String profileFile;
527 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700528 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700529 boolean autoStopProfiler;
530 boolean profiling;
531 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700532 public void setProfiler(ProfilerInfo profilerInfo) {
533 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700534 if (profiling) {
535 if (fd != null) {
536 try {
537 fd.close();
538 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700539 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700540 }
541 }
542 return;
543 }
544 if (profileFd != null) {
545 try {
546 profileFd.close();
547 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700548 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700549 }
550 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700551 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700552 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700553 samplingInterval = profilerInfo.samplingInterval;
554 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700555 }
556 public void startProfiling() {
557 if (profileFd == null || profiling) {
558 return;
559 }
560 try {
Andreas Gampeeff06392016-05-10 12:51:45 -0700561 int bufferSize = SystemProperties.getInt("debug.traceview-buffer-size-mb", 8);
Jeff Hao1b012d32014-08-20 10:35:34 -0700562 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
Andreas Gampe0f03a012016-05-09 14:26:07 -0700563 bufferSize * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700564 profiling = true;
565 } catch (RuntimeException e) {
566 Slog.w(TAG, "Profiling failed on path " + profileFile);
567 try {
568 profileFd.close();
569 profileFd = null;
570 } catch (IOException e2) {
571 Slog.w(TAG, "Failure closing profile fd", e2);
572 }
573 }
574 }
575 public void stopProfiling() {
576 if (profiling) {
577 profiling = false;
578 Debug.stopMethodTracing();
579 if (profileFd != null) {
580 try {
581 profileFd.close();
582 } catch (IOException e) {
583 }
584 }
585 profileFd = null;
586 profileFile = null;
587 }
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 }
590
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400591 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700592 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700593 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800594 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 }
597
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400598 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 IBinder token;
600 List<ResultInfo> results;
601 public String toString() {
602 return "ResultData{token=" + token + " results" + results + "}";
603 }
604 }
605
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800607 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 String what;
609 String who;
610 }
611
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400612 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700613 String path;
614 ParcelFileDescriptor fd;
615 }
616
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400617 static final class UpdateCompatibilityData {
618 String pkg;
619 CompatibilityInfo info;
620 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800621
Adam Skorydfc7fd72013-08-05 19:23:41 -0700622 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800623 IBinder activityToken;
624 IBinder requestToken;
625 int requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700626 int sessionId;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800627 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700628
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800629 static final class ActivityConfigChangeData {
630 final IBinder activityToken;
631 final Configuration overrideConfig;
632 public ActivityConfigChangeData(IBinder token, Configuration config) {
633 activityToken = token;
634 overrideConfig = config;
635 }
636 }
637
Romain Guy65b345f2011-07-27 18:51:50 -0700638 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700639
Romain Guy65b345f2011-07-27 18:51:50 -0700640 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700641 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700642
Dianne Hackborna413dc02013-07-12 12:02:55 -0700643 private int mLastProcessState = -1;
644
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700645 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700646 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700647 if (mPendingConfiguration == null ||
648 mPendingConfiguration.isOtherSeqNewer(config)) {
649 mPendingConfiguration = config;
650 }
651 }
652 }
653
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700655 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700656 int seq = getLifecycleSeq();
657 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
658 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700659 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
661 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700662 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
663 configChanges,
664 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666
667 public final void scheduleStopActivity(IBinder token, boolean showWindow,
668 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700669 int seq = getLifecycleSeq();
670 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
671 + " operation received seq: " + seq);
672 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700674 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676
677 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700678 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
680 token);
681 }
682
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800683 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700684 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800685 }
686
Dianne Hackborna413dc02013-07-12 12:02:55 -0700687 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800688 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700689 int seq = getLifecycleSeq();
690 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
691 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700692 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700693 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695
696 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
697 ResultData res = new ResultData();
698 res.token = token;
699 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700700 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 }
702
703 // we use token to identify this activity without having to send the
704 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800705 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700706 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800707 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
708 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
709 int procState, Bundle state, PersistableBundle persistentState,
710 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
711 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700712
713 updateProcessState(procState, false);
714
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700715 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716
717 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700718 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800720 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700721 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400723 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700725 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 r.pendingResults = pendingResults;
728 r.pendingIntents = pendingNewIntents;
729
730 r.startsNotResumed = notResumed;
731 r.isForward = isForward;
732
Jeff Hao1b012d32014-08-20 10:35:34 -0700733 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700734
Wale Ogunwale60454db2015-01-23 16:05:07 -0800735 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700736 updatePendingConfiguration(curConfig);
737
Jeff Brown9ef09972013-10-15 20:49:59 -0700738 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 }
740
Wale Ogunwale60454db2015-01-23 16:05:07 -0800741 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800743 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800744 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700745 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800746 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700747 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800750 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 NewIntentData data = new NewIntentData();
752 data.intents = intents;
753 data.token = token;
754
Jeff Brown9ef09972013-10-15 20:49:59 -0700755 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
758 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
759 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700760 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 configChanges);
762 }
763
764 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400765 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700766 boolean sync, int sendingUser, int processState) {
767 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700768 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700769 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400771 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700772 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400775 public final void scheduleCreateBackupAgent(ApplicationInfo app,
776 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700777 CreateBackupAgentData d = new CreateBackupAgentData();
778 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400779 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700780 d.backupMode = backupMode;
781
Jeff Brown9ef09972013-10-15 20:49:59 -0700782 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700783 }
784
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400785 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
786 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700787 CreateBackupAgentData d = new CreateBackupAgentData();
788 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400789 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700790
Jeff Brown9ef09972013-10-15 20:49:59 -0700791 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700792 }
793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700795 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
796 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 CreateServiceData s = new CreateServiceData();
798 s.token = token;
799 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400800 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
Jeff Brown9ef09972013-10-15 20:49:59 -0700802 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 }
804
805 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700806 boolean rebind, int processState) {
807 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 BindServiceData s = new BindServiceData();
809 s.token = token;
810 s.intent = intent;
811 s.rebind = rebind;
812
Amith Yamasani742a6712011-05-04 14:49:28 -0700813 if (DEBUG_SERVICE)
814 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
815 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700816 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818
819 public final void scheduleUnbindService(IBinder token, Intent intent) {
820 BindServiceData s = new BindServiceData();
821 s.token = token;
822 s.intent = intent;
823
Jeff Brown9ef09972013-10-15 20:49:59 -0700824 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700827 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700828 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 ServiceArgsData s = new ServiceArgsData();
830 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700831 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700833 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 s.args = args;
835
Jeff Brown9ef09972013-10-15 20:49:59 -0700836 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
839 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700840 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842
Jeff Hao1b012d32014-08-20 10:35:34 -0700843 public final void bindApplication(String processName, ApplicationInfo appInfo,
844 List<ProviderInfo> providers, ComponentName instrumentationName,
845 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
846 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800847 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700848 boolean enableBinderTracking, boolean trackAllocation,
849 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
850 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851
852 if (services != null) {
853 // Setup the service cache in the ServiceManager
854 ServiceManager.initServiceCache(services);
855 }
856
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800857 setCoreSettings(coreSettings);
858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 AppBindData data = new AppBindData();
860 data.processName = processName;
861 data.appInfo = appInfo;
862 data.providers = providers;
863 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 data.instrumentationArgs = instrumentationArgs;
865 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800866 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400868 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700869 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700870 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700871 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400873 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700874 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700875 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
878 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700879 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881
Christopher Tate5e1ab332009-09-01 20:32:49 -0700882 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700883 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700884 }
885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700887 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700888 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 }
890
891 public void updateTimeZone() {
892 TimeZone.setDefault(null);
893 }
894
Robert Greenwalt03595d02010-11-02 14:08:23 -0700895 public void clearDnsCache() {
896 // a non-standard API to get this to libcore
897 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400898 // Allow libcore to perform the necessary actions as it sees fit upon a network
899 // configuration change.
900 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700901 }
902
Jason Monk83520b92014-05-09 15:16:06 -0400903 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400904 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
905 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500906 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400907 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500908 } else {
909 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
910 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700911 }
912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 public void processInBackground() {
914 mH.removeMessages(H.GC_WHEN_IDLE);
915 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
916 }
917
918 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700919 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700920 try {
921 data.fd = ParcelFileDescriptor.dup(fd);
922 data.token = servicetoken;
923 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700924 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700925 } catch (IOException e) {
926 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 }
928 }
929
930 // This function exists to make sure all receiver dispatching is
931 // correctly ordered, since these are one-way calls and the binder driver
932 // applies transaction ordering per object for such calls.
933 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700934 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700935 boolean sticky, int sendingUser, int processState) throws RemoteException {
936 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700937 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
938 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
Bob Leee5408332009-09-04 18:31:17 -0700940
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800941 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700943 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 }
945
Wale Ogunwale60454db2015-01-23 16:05:07 -0800946 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800947 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800948 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800949 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800950 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800953 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700954 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
955 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800956 }
957
Andy McFadden824c5102010-07-09 16:26:57 -0700958 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
959 DumpHeapData dhd = new DumpHeapData();
960 dhd.path = path;
961 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700962 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700963 }
964
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700965 public void setSchedulingGroup(int group) {
966 // Note: do this immediately, since going into the foreground
967 // should happen regardless of what pending work we have to do
968 // and the activity manager will wait for us to report back that
969 // we are done before sending us to the background.
970 try {
971 Process.setProcessGroup(Process.myPid(), group);
972 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800973 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700974 }
975 }
Bob Leee5408332009-09-04 18:31:17 -0700976
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700977 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700978 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700979 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700980
981 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700982 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700983 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700984
Dianne Hackborn30d71892010-12-11 10:37:55 -0800985 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
986 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700987 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700988 try {
989 data.fd = ParcelFileDescriptor.dup(fd);
990 data.token = activitytoken;
991 data.prefix = prefix;
992 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700993 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700994 } catch (IOException e) {
995 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700996 }
997 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700998
Marco Nelissen18cb2872011-11-15 11:19:53 -0800999 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
1000 String[] args) {
1001 DumpComponentInfo data = new DumpComponentInfo();
1002 try {
1003 data.fd = ParcelFileDescriptor.dup(fd);
1004 data.token = providertoken;
1005 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001006 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001007 } catch (IOException e) {
1008 Slog.w(TAG, "dumpProvider failed", e);
1009 }
1010 }
1011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001013 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001014 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1015 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001016 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001017 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001018 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001019 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001020 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001021 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07001022 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001023 }
1024
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001025 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001026 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 long nativeMax = Debug.getNativeHeapSize() / 1024;
1028 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1029 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001031 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001032 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 long dalvikMax = runtime.totalMemory() / 1024;
1034 long dalvikFree = runtime.freeMemory() / 1024;
1035 long dalvikAllocated = dalvikMax - dalvikFree;
1036 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001037 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001038 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1039 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 int globalAssetCount = AssetManager.getGlobalAssetCount();
1041 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1042 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1043 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1044 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001045 long parcelSize = Parcel.getGlobalAllocSize();
1046 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001047 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001048 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001049
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001050 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1051 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001052 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1053 nativeMax, nativeAllocated, nativeFree,
1054 dalvikMax, dalvikAllocated, dalvikFree);
1055
Dianne Hackbornb437e092011-08-05 17:50:29 -07001056 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 // NOTE: if you change anything significant below, also consider changing
1058 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 // Object counts
1061 pw.print(viewInstanceCount); pw.print(',');
1062 pw.print(viewRootInstanceCount); pw.print(',');
1063 pw.print(appContextInstanceCount); pw.print(',');
1064 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 pw.print(globalAssetCount); pw.print(',');
1067 pw.print(globalAssetManagerCount); pw.print(',');
1068 pw.print(binderLocalObjectCount); pw.print(',');
1069 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 pw.print(binderDeathObjectCount); pw.print(',');
1072 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001075 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001076 pw.print(stats.memoryUsed / 1024); pw.print(',');
1077 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001078 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001079 for (int i = 0; i < stats.dbStats.size(); i++) {
1080 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001081 pw.print(','); pw.print(dbStats.dbName);
1082 pw.print(','); pw.print(dbStats.pageSize);
1083 pw.print(','); pw.print(dbStats.dbSize);
1084 pw.print(','); pw.print(dbStats.lookaside);
1085 pw.print(','); pw.print(dbStats.cache);
1086 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001087 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001088 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001089
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001090 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Bob Leee5408332009-09-04 18:31:17 -07001092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 pw.println(" ");
1094 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001095 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 viewRootInstanceCount);
1097
1098 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1099 "Activities:", activityInstanceCount);
1100
1101 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1102 "AssetManagers:", globalAssetManagerCount);
1103
1104 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1105 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001106 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1107 "Parcel count:", parcelCount);
1108 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1109 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 // SQLite mem info
1112 pw.println(" ");
1113 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001114 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1115 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1116 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001117 pw.println(" ");
1118 int N = stats.dbStats.size();
1119 if (N > 0) {
1120 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001121 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1122 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001123 for (int i = 0; i < N; i++) {
1124 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001125 printRow(pw, DB_INFO_FORMAT,
1126 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1127 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1128 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1129 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001130 }
1131 }
Bob Leee5408332009-09-04 18:31:17 -07001132
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001133 // Asset details.
1134 String assetAlloc = AssetManager.getAssetAllocations();
1135 if (assetAlloc != null) {
1136 pw.println(" ");
1137 pw.println(" Asset Allocations");
1138 pw.print(assetAlloc);
1139 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001140
1141 // Unreachable native memory
1142 if (dumpUnreachable) {
1143 boolean showContents = ((mBoundApplication != null)
1144 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1145 || android.os.Build.IS_DEBUGGABLE;
1146 pw.println(" ");
1147 pw.println(" Unreachable memory");
1148 pw.print(Debug.getUnreachableMemory(100, showContents));
1149 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001150 }
1151
1152 @Override
1153 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1154 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001155 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001158 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001159 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001160 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1161 SQLiteDebug.dump(printer, args);
1162 pw.flush();
1163 }
1164
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001165 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001166 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1167 if (mSystemThread) {
1168 // Ensure this invocation is asynchronous to prevent
1169 // writer waiting due to buffer cannot be consumed.
1170 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1171 @Override
1172 public void run() {
1173 dumpDatabaseInfo(fd, args);
1174 }
1175 });
1176 } else {
1177 dumpDatabaseInfo(fd, args);
1178 }
1179 }
1180
1181 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001182 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001183 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001184 }
1185
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001186 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001187 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Amith Yamasani4f128e42016-05-10 11:44:12 -07001188 int requestType, int sessionId) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001189 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001190 cmd.activityToken = activityToken;
1191 cmd.requestToken = requestToken;
1192 cmd.requestType = requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -07001193 cmd.sessionId = sessionId;
Jeff Brown9ef09972013-10-15 20:49:59 -07001194 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001195 }
1196
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001197 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001198 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001199 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001200
1201 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1202 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1203 ucd.pkg = pkg;
1204 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001205 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001206 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001207
1208 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001209 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001210 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001211
Craig Mautner5eda9b32013-07-02 11:58:16 -07001212 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001213 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001214 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001215
Craig Mautnereb8abf72014-07-02 15:04:09 -07001216 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1217 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1218 new Pair<IBinder, ActivityOptions>(token, options));
1219 }
1220
Dianne Hackborna413dc02013-07-12 12:02:55 -07001221 public void setProcessState(int state) {
1222 updateProcessState(state, true);
1223 }
1224
1225 public void updateProcessState(int processState, boolean fromIpc) {
1226 synchronized (this) {
1227 if (mLastProcessState != processState) {
1228 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001229 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1230 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1231 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1232 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1233 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1234 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1235 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1236 }
1237 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001238 if (false) {
1239 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1240 + (fromIpc ? " (from ipc": ""));
1241 }
1242 }
1243 }
1244 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001245
1246 @Override
1247 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001248 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001249 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001250
1251 @Override
1252 public final void updateTimePrefs(boolean is24Hour) {
1253 DateFormat.set24HourTimePref(is24Hour);
1254 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001255
1256 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001257 public void scheduleCancelVisibleBehind(IBinder token) {
1258 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001259 }
1260
1261 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001262 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1263 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001264 }
Craig Mautner8746a472014-07-24 15:12:54 -07001265
Jeff Sharkey605eb792014-11-04 13:34:06 -08001266 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001267 public void scheduleEnterAnimationComplete(IBinder token) {
1268 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1269 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001270
1271 @Override
1272 public void notifyCleartextNetwork(byte[] firstPacket) {
1273 if (StrictMode.vmCleartextNetworkEnabled()) {
1274 StrictMode.onCleartextNetworkDetected(firstPacket);
1275 }
1276 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001277
1278 @Override
1279 public void startBinderTracking() {
1280 sendMessage(H.START_BINDER_TRACKING, null);
1281 }
1282
1283 @Override
1284 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1285 try {
1286 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1287 } catch (IOException e) {
1288 }
1289 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001290
1291 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001292 public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001293 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001294 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, token, isInMultiWindowMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001295 }
1296
1297 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001298 public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001299 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001300 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, token, isInPipMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001301 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001302
1303 @Override
1304 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1305 IVoiceInteractor voiceInteractor) throws RemoteException {
1306 SomeArgs args = SomeArgs.obtain();
1307 args.arg1 = token;
1308 args.arg2 = voiceInteractor;
1309 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 }
1312
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001313 private int getLifecycleSeq() {
1314 synchronized (mResourcesManager) {
1315 return mLifecycleSeq++;
1316 }
1317 }
1318
Romain Guy65b345f2011-07-27 18:51:50 -07001319 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public static final int LAUNCH_ACTIVITY = 100;
1321 public static final int PAUSE_ACTIVITY = 101;
1322 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1323 public static final int STOP_ACTIVITY_SHOW = 103;
1324 public static final int STOP_ACTIVITY_HIDE = 104;
1325 public static final int SHOW_WINDOW = 105;
1326 public static final int HIDE_WINDOW = 106;
1327 public static final int RESUME_ACTIVITY = 107;
1328 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001329 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 public static final int BIND_APPLICATION = 110;
1331 public static final int EXIT_APPLICATION = 111;
1332 public static final int NEW_INTENT = 112;
1333 public static final int RECEIVER = 113;
1334 public static final int CREATE_SERVICE = 114;
1335 public static final int SERVICE_ARGS = 115;
1336 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 public static final int CONFIGURATION_CHANGED = 118;
1339 public static final int CLEAN_UP_CONTEXT = 119;
1340 public static final int GC_WHEN_IDLE = 120;
1341 public static final int BIND_SERVICE = 121;
1342 public static final int UNBIND_SERVICE = 122;
1343 public static final int DUMP_SERVICE = 123;
1344 public static final int LOW_MEMORY = 124;
1345 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1346 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001347 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001348 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001349 public static final int DESTROY_BACKUP_AGENT = 129;
1350 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001351 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001352 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001353 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001354 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001355 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001356 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001357 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001358 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001359 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001360 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001361 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001362 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001363 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001364 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001365 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001366 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001367 public static final int CANCEL_VISIBLE_BEHIND = 147;
1368 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001369 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001370 public static final int START_BINDER_TRACKING = 150;
1371 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Andrii Kulian933076d2016-03-29 17:04:42 -07001372 public static final int MULTI_WINDOW_MODE_CHANGED = 152;
1373 public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001374 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001377 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 switch (code) {
1379 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1380 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1381 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1382 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1383 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1384 case SHOW_WINDOW: return "SHOW_WINDOW";
1385 case HIDE_WINDOW: return "HIDE_WINDOW";
1386 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1387 case SEND_RESULT: return "SEND_RESULT";
1388 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1389 case BIND_APPLICATION: return "BIND_APPLICATION";
1390 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1391 case NEW_INTENT: return "NEW_INTENT";
1392 case RECEIVER: return "RECEIVER";
1393 case CREATE_SERVICE: return "CREATE_SERVICE";
1394 case SERVICE_ARGS: return "SERVICE_ARGS";
1395 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1397 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1398 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1399 case BIND_SERVICE: return "BIND_SERVICE";
1400 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1401 case DUMP_SERVICE: return "DUMP_SERVICE";
1402 case LOW_MEMORY: return "LOW_MEMORY";
1403 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1404 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001405 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001406 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1407 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001408 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001409 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001410 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001411 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001412 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001413 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001414 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001415 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001416 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001417 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001418 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001419 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001420 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001421 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001422 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001423 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001424 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001425 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1426 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001427 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Andrii Kulian933076d2016-03-29 17:04:42 -07001428 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1429 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001430 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 }
1432 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001433 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 }
1435 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001436 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 switch (msg.what) {
1438 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001439 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001440 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441
1442 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001443 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001444 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001445 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 } break;
1447 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001448 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001449 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001450 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001451 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001453 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001454 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001455 SomeArgs args = (SomeArgs) msg.obj;
1456 handlePauseActivity((IBinder) args.arg1, false,
1457 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1458 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001459 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001460 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001461 } break;
1462 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001463 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001464 SomeArgs args = (SomeArgs) msg.obj;
1465 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1466 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001467 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001468 } break;
1469 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001470 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001471 SomeArgs args = (SomeArgs) msg.obj;
1472 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001473 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001474 } break;
1475 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001476 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001477 SomeArgs args = (SomeArgs) msg.obj;
1478 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001479 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001480 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001484 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 break;
1486 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001487 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001489 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 break;
1491 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001492 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001493 SomeArgs args = (SomeArgs) msg.obj;
1494 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001495 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001496 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 break;
1498 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001499 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001501 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 break;
1503 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001504 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1506 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001507 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 break;
1509 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001510 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 AppBindData data = (AppBindData)msg.obj;
1512 handleBindApplication(data);
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 EXIT_APPLICATION:
1516 if (mInitialApplication != null) {
1517 mInitialApplication.onTerminate();
1518 }
1519 Looper.myLooper().quit();
1520 break;
1521 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001522 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 handleNewIntent((NewIntentData)msg.obj);
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;
1526 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001527 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001529 maybeSnapshot();
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 CREATE_SERVICE:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001533 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001535 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 break;
1537 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001538 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001540 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 break;
1542 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001543 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 handleUnbindService((BindServiceData)msg.obj);
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 SERVICE_ARGS:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001548 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceStart: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001550 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 break;
1552 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001553 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001555 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001556 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001559 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001560 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001561 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001562 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 break;
1564 case CLEAN_UP_CONTEXT:
1565 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1566 cci.context.performFinalCleanup(cci.who, cci.what);
1567 break;
1568 case GC_WHEN_IDLE:
1569 scheduleGcIdler();
1570 break;
1571 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001572 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 break;
1574 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001575 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001577 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 break;
1579 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001580 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001581 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1582 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001583 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001585 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001586 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001587 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001588 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001589 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001590 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001591 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001592 break;
1593 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001594 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001595 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001596 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001597 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001598 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001599 Process.killProcess(Process.myPid());
1600 break;
1601 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001602 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001603 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001604 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001605 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001606 case ENABLE_JIT:
1607 ensureJitEnabled();
1608 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001609 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001610 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001611 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001612 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001613 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001614 case SCHEDULE_CRASH:
1615 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001616 case DUMP_HEAP:
1617 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1618 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001619 case DUMP_ACTIVITY:
1620 handleDumpActivity((DumpComponentInfo)msg.obj);
1621 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001622 case DUMP_PROVIDER:
1623 handleDumpProvider((DumpComponentInfo)msg.obj);
1624 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001625 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001626 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001627 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001628 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001629 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001630 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001631 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001632 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001633 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001634 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001635 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1636 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001637 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001638 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001639 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001640 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001641 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001642 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001643 case UNSTABLE_PROVIDER_DIED:
1644 handleUnstableProviderDied((IBinder)msg.obj, false);
1645 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001646 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1647 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001648 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001649 case TRANSLUCENT_CONVERSION_COMPLETE:
1650 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1651 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001652 case INSTALL_PROVIDER:
1653 handleInstallProvider((ProviderInfo) msg.obj);
1654 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001655 case ON_NEW_ACTIVITY_OPTIONS:
1656 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1657 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001658 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001659 case CANCEL_VISIBLE_BEHIND:
1660 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001661 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001662 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1663 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001664 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001665 case ENTER_ANIMATION_COMPLETE:
1666 handleEnterAnimationComplete((IBinder) msg.obj);
1667 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001668 case START_BINDER_TRACKING:
1669 handleStartBinderTracking();
1670 break;
1671 case STOP_BINDER_TRACKING_AND_DUMP:
1672 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1673 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001674 case MULTI_WINDOW_MODE_CHANGED:
1675 handleMultiWindowModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001676 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001677 case PICTURE_IN_PICTURE_MODE_CHANGED:
1678 handlePictureInPictureModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001679 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001680 case LOCAL_VOICE_INTERACTION_STARTED:
1681 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1682 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1683 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001685 Object obj = msg.obj;
1686 if (obj instanceof SomeArgs) {
1687 ((SomeArgs) obj).recycle();
1688 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001689 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
Bob Leee5408332009-09-04 18:31:17 -07001691
Brian Carlstromed7e0072011-03-24 13:27:57 -07001692 private void maybeSnapshot() {
1693 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001694 // convert the *private* ActivityThread.PackageInfo to *public* known
1695 // android.content.pm.PackageInfo
1696 String packageName = mBoundApplication.info.mPackageName;
1697 android.content.pm.PackageInfo packageInfo = null;
1698 try {
1699 Context context = getSystemContext();
1700 if(context == null) {
1701 Log.e(TAG, "cannot get a valid context");
1702 return;
1703 }
1704 PackageManager pm = context.getPackageManager();
1705 if(pm == null) {
1706 Log.e(TAG, "cannot get a valid PackageManager");
1707 return;
1708 }
1709 packageInfo = pm.getPackageInfo(
1710 packageName, PackageManager.GET_ACTIVITIES);
1711 } catch (NameNotFoundException e) {
1712 Log.e(TAG, "cannot get package info for " + packageName, e);
1713 }
1714 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001715 }
1716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717 }
1718
Romain Guy65b345f2011-07-27 18:51:50 -07001719 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001720 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001722 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001723 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001724 if (mBoundApplication != null && mProfiler.profileFd != null
1725 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001726 stopProfiling = true;
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 if (a != null) {
1729 mNewActivities = null;
1730 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001731 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001733 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 TAG, "Reporting idle of " + a +
1735 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001736 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 if (a.activity != null && !a.activity.mFinished) {
1738 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001739 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001740 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001742 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 }
1744 }
1745 prev = a;
1746 a = a.nextIdle;
1747 prev.nextIdle = null;
1748 } while (a != null);
1749 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001750 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001751 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001752 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001753 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 return false;
1755 }
1756 }
1757
1758 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001759 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 public final boolean queueIdle() {
1761 doGcIfNeeded();
1762 return false;
1763 }
1764 }
1765
Romain Guy65b345f2011-07-27 18:51:50 -07001766 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001767 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001770 public static boolean isSystem() {
1771 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1772 }
1773
Svetoslavfbf0eca2015-05-01 16:52:41 -07001774 public static String currentOpPackageName() {
1775 ActivityThread am = currentActivityThread();
1776 return (am != null && am.getApplication() != null)
1777 ? am.getApplication().getOpPackageName() : null;
1778 }
1779
Romain Guy65b345f2011-07-27 18:51:50 -07001780 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001781 ActivityThread am = currentActivityThread();
1782 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001783 ? am.mBoundApplication.appInfo.packageName : null;
1784 }
1785
1786 public static String currentProcessName() {
1787 ActivityThread am = currentActivityThread();
1788 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001789 ? am.mBoundApplication.processName : null;
1790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791
Romain Guy65b345f2011-07-27 18:51:50 -07001792 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001793 ActivityThread am = currentActivityThread();
1794 return am != null ? am.mInitialApplication : null;
1795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001797 public static IPackageManager getPackageManager() {
1798 if (sPackageManager != null) {
1799 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1800 return sPackageManager;
1801 }
1802 IBinder b = ServiceManager.getService("package");
1803 //Slog.v("PackageManager", "default service binder = " + b);
1804 sPackageManager = IPackageManager.Stub.asInterface(b);
1805 //Slog.v("PackageManager", "default service = " + sPackageManager);
1806 return sPackageManager;
1807 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001808
Romain Guy65b345f2011-07-27 18:51:50 -07001809 private Configuration mMainThreadConfig = new Configuration();
Amith Yamasani4f128e42016-05-10 11:44:12 -07001810
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001811 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1812 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001813 if (config == null) {
1814 return null;
1815 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001816 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001817 mMainThreadConfig.setTo(config);
1818 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001819 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001820 }
1821 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001824 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001825 * Creates the top level resources for the given package. Will return an existing
1826 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001827 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001828 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001829 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1830 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1831 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001832 }
1833
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001834 final Handler getHandler() {
1835 return mH;
1836 }
1837
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001838 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1839 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001840 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1841 }
1842
1843 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1844 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001845 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001846 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001847 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001848 if (differentUser) {
1849 // Caching not supported across users
1850 ref = null;
1851 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 ref = mPackages.get(packageName);
1853 } else {
1854 ref = mResourcePackages.get(packageName);
1855 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001856
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001857 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001858 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001859 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1860 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 if (packageInfo != null && (packageInfo.mResources == null
1862 || packageInfo.mResources.getAssets().isUpToDate())) {
1863 if (packageInfo.isSecurityViolation()
1864 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1865 throw new SecurityException(
1866 "Requesting code from " + packageName
1867 + " to be run in process "
1868 + mBoundApplication.processName
1869 + "/" + mBoundApplication.appInfo.uid);
1870 }
1871 return packageInfo;
1872 }
1873 }
1874
1875 ApplicationInfo ai = null;
1876 try {
1877 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001878 PackageManager.GET_SHARED_LIBRARY_FILES
1879 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1880 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001882 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001883 }
1884
1885 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001886 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
1888
1889 return null;
1890 }
1891
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001892 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1893 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1895 boolean securityViolation = includeCode && ai.uid != 0
1896 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001897 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001898 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001899 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1901 |Context.CONTEXT_IGNORE_SECURITY))
1902 == Context.CONTEXT_INCLUDE_CODE) {
1903 if (securityViolation) {
1904 String msg = "Requesting code from " + ai.packageName
1905 + " (with uid " + ai.uid + ")";
1906 if (mBoundApplication != null) {
1907 msg = msg + " to be run in process "
1908 + mBoundApplication.processName + " (with uid "
1909 + mBoundApplication.appInfo.uid + ")";
1910 }
1911 throw new SecurityException(msg);
1912 }
1913 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001914 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1915 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001918 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1919 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001920 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 }
1922
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001923 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001924 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001925 WeakReference<LoadedApk> ref;
1926 if (includeCode) {
1927 ref = mPackages.get(packageName);
1928 } else {
1929 ref = mResourcePackages.get(packageName);
1930 }
1931 return ref != null ? ref.get() : null;
1932 }
1933 }
1934
Romain Guy65b345f2011-07-27 18:51:50 -07001935 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001936 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1937 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001938 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001939 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001940 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001941 if (differentUser) {
1942 // Caching not supported across users
1943 ref = null;
1944 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 ref = mPackages.get(aInfo.packageName);
1946 } else {
1947 ref = mResourcePackages.get(aInfo.packageName);
1948 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001949
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001950 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 if (packageInfo == null || (packageInfo.mResources != null
1952 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001953 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 : "Loading resource-only package ") + aInfo.packageName
1955 + " (in " + (mBoundApplication != null
1956 ? mBoundApplication.processName : null)
1957 + ")");
1958 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001959 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001961 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001962
1963 if (mSystemThread && "android".equals(aInfo.packageName)) {
1964 packageInfo.installSystemApplicationInfo(aInfo,
1965 getSystemContext().mPackageInfo.getClassLoader());
1966 }
1967
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001968 if (differentUser) {
1969 // Caching not supported across users
1970 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001972 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 } else {
1974 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001975 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
1977 }
1978 return packageInfo;
1979 }
1980 }
1981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001983 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 }
1985
1986 public ApplicationThread getApplicationThread()
1987 {
1988 return mAppThread;
1989 }
1990
1991 public Instrumentation getInstrumentation()
1992 {
1993 return mInstrumentation;
1994 }
1995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001997 return mProfiler != null && mProfiler.profileFile != null
1998 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 }
2000
2001 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002002 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004
2005 public Looper getLooper() {
2006 return mLooper;
2007 }
2008
2009 public Application getApplication() {
2010 return mInitialApplication;
2011 }
Bob Leee5408332009-09-04 18:31:17 -07002012
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002013 public String getProcessName() {
2014 return mBoundApplication.processName;
2015 }
Bob Leee5408332009-09-04 18:31:17 -07002016
Dianne Hackborn21556372010-02-04 16:34:40 -08002017 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 synchronized (this) {
2019 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002020 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002022 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025
Narayan Kamath29564cd2014-08-07 10:57:40 +01002026 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002027 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002028 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002029
2030 // give ourselves a default profiler
2031 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002032 }
2033 }
2034
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002035 void ensureJitEnabled() {
2036 if (!mJitEnabled) {
2037 mJitEnabled = true;
2038 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2039 }
2040 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 void scheduleGcIdler() {
2043 if (!mGcIdlerScheduled) {
2044 mGcIdlerScheduled = true;
2045 Looper.myQueue().addIdleHandler(mGcIdler);
2046 }
2047 mH.removeMessages(H.GC_WHEN_IDLE);
2048 }
2049
2050 void unscheduleGcIdler() {
2051 if (mGcIdlerScheduled) {
2052 mGcIdlerScheduled = false;
2053 Looper.myQueue().removeIdleHandler(mGcIdler);
2054 }
2055 mH.removeMessages(H.GC_WHEN_IDLE);
2056 }
2057
2058 void doGcIfNeeded() {
2059 mGcIdlerScheduled = false;
2060 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002061 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 // + "m now=" + now);
2063 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002064 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 BinderInternal.forceGc("bg");
2066 }
2067 }
2068
Dianne Hackborne77187d2013-10-25 16:32:41 -07002069 private static final String HEAP_FULL_COLUMN
2070 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2071 private static final String HEAP_COLUMN
2072 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002073 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2074 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2075 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002076
2077 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002078 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002079
2080 static void printRow(PrintWriter pw, String format, Object...objs) {
2081 pw.println(String.format(format, objs));
2082 }
2083
2084 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002085 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2086 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002087 long nativeMax, long nativeAllocated, long nativeFree,
2088 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2089
2090 // For checkin, we print one long comma-separated list of values
2091 if (checkin) {
2092 // NOTE: if you change anything significant below, also consider changing
2093 // ACTIVITY_THREAD_CHECKIN_VERSION.
2094
2095 // Header
2096 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2097 pw.print(pid); pw.print(',');
2098 pw.print(processName); pw.print(',');
2099
2100 // Heap info - max
2101 pw.print(nativeMax); pw.print(',');
2102 pw.print(dalvikMax); pw.print(',');
2103 pw.print("N/A,");
2104 pw.print(nativeMax + dalvikMax); pw.print(',');
2105
2106 // Heap info - allocated
2107 pw.print(nativeAllocated); pw.print(',');
2108 pw.print(dalvikAllocated); pw.print(',');
2109 pw.print("N/A,");
2110 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2111
2112 // Heap info - free
2113 pw.print(nativeFree); pw.print(',');
2114 pw.print(dalvikFree); pw.print(',');
2115 pw.print("N/A,");
2116 pw.print(nativeFree + dalvikFree); pw.print(',');
2117
2118 // Heap info - proportional set size
2119 pw.print(memInfo.nativePss); pw.print(',');
2120 pw.print(memInfo.dalvikPss); pw.print(',');
2121 pw.print(memInfo.otherPss); pw.print(',');
2122 pw.print(memInfo.getTotalPss()); pw.print(',');
2123
2124 // Heap info - swappable set size
2125 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2126 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2127 pw.print(memInfo.otherSwappablePss); pw.print(',');
2128 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2129
2130 // Heap info - shared dirty
2131 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2132 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2133 pw.print(memInfo.otherSharedDirty); pw.print(',');
2134 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2135
2136 // Heap info - shared clean
2137 pw.print(memInfo.nativeSharedClean); pw.print(',');
2138 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2139 pw.print(memInfo.otherSharedClean); pw.print(',');
2140 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2141
2142 // Heap info - private Dirty
2143 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2144 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2145 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2146 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2147
2148 // Heap info - private Clean
2149 pw.print(memInfo.nativePrivateClean); pw.print(',');
2150 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2151 pw.print(memInfo.otherPrivateClean); pw.print(',');
2152 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2153
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002154 // Heap info - swapped out
2155 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2156 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2157 pw.print(memInfo.otherSwappedOut); pw.print(',');
2158 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2159
2160 // Heap info - swapped out pss
2161 if (memInfo.hasSwappedOutPss) {
2162 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2163 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2164 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2165 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2166 } else {
2167 pw.print("N/A,");
2168 pw.print("N/A,");
2169 pw.print("N/A,");
2170 pw.print("N/A,");
2171 }
2172
Dianne Hackborne77187d2013-10-25 16:32:41 -07002173 // Heap info - other areas
2174 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2175 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2176 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2177 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2178 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2179 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2180 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2181 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002182 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2183 if (memInfo.hasSwappedOutPss) {
2184 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2185 } else {
2186 pw.print("N/A,");
2187 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002188 }
2189 return;
2190 }
2191
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002192 if (!dumpSummaryOnly) {
2193 if (dumpFullInfo) {
2194 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002195 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2196 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002197 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002198 "Clean", "Clean", "Dirty",
2199 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002200 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2201 "------", "------", "------", "------", "------", "------");
2202 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2203 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2204 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002205 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2206 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002207 nativeMax, nativeAllocated, nativeFree);
2208 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2209 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2210 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002211 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2212 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002213 dalvikMax, dalvikAllocated, dalvikFree);
2214 } else {
2215 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002216 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2217 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002218 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2219 "Clean", "Dirty", "Size", "Alloc", "Free");
2220 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2221 "------", "------", "------", "------", "------");
2222 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2223 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002224 memInfo.nativePrivateClean,
2225 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2226 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002227 nativeMax, nativeAllocated, nativeFree);
2228 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2229 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002230 memInfo.dalvikPrivateClean,
2231 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2232 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002233 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002234 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002235
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002236 int otherPss = memInfo.otherPss;
2237 int otherSwappablePss = memInfo.otherSwappablePss;
2238 int otherSharedDirty = memInfo.otherSharedDirty;
2239 int otherPrivateDirty = memInfo.otherPrivateDirty;
2240 int otherSharedClean = memInfo.otherSharedClean;
2241 int otherPrivateClean = memInfo.otherPrivateClean;
2242 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002243 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002244
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002245 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002246 final int myPss = memInfo.getOtherPss(i);
2247 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2248 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2249 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2250 final int mySharedClean = memInfo.getOtherSharedClean(i);
2251 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2252 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002253 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002254 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002255 || mySharedClean != 0 || myPrivateClean != 0
2256 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002257 if (dumpFullInfo) {
2258 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2259 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002260 mySharedClean, myPrivateClean,
2261 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2262 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002263 } else {
2264 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2265 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002266 myPrivateClean,
2267 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2268 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002269 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002270 otherPss -= myPss;
2271 otherSwappablePss -= mySwappablePss;
2272 otherSharedDirty -= mySharedDirty;
2273 otherPrivateDirty -= myPrivateDirty;
2274 otherSharedClean -= mySharedClean;
2275 otherPrivateClean -= myPrivateClean;
2276 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002277 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002278 }
2279 }
2280
2281 if (dumpFullInfo) {
2282 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2283 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002284 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2285 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002286 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2287 memInfo.getTotalSwappablePss(),
2288 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2289 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002290 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2291 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002292 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2293 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002294 } else {
2295 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002296 otherPrivateDirty, otherPrivateClean,
2297 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002298 "", "", "");
2299 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2300 memInfo.getTotalPrivateDirty(),
2301 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002302 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002303 memInfo.getTotalSwappedOut(),
2304 nativeMax+dalvikMax,
2305 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2306 }
2307
2308 if (dumpDalvik) {
2309 pw.println(" ");
2310 pw.println(" Dalvik Details");
2311
2312 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2313 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2314 final int myPss = memInfo.getOtherPss(i);
2315 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2316 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2317 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2318 final int mySharedClean = memInfo.getOtherSharedClean(i);
2319 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2320 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002321 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002322 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002323 || mySharedClean != 0 || myPrivateClean != 0
2324 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002325 if (dumpFullInfo) {
2326 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2327 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002328 mySharedClean, myPrivateClean,
2329 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2330 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002331 } else {
2332 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2333 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002334 myPrivateClean,
2335 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2336 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002337 }
2338 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002339 }
2340 }
2341 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002342
2343 pw.println(" ");
2344 pw.println(" App Summary");
2345 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2346 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2347 printRow(pw, ONE_COUNT_COLUMN,
2348 "Java Heap:", memInfo.getSummaryJavaHeap());
2349 printRow(pw, ONE_COUNT_COLUMN,
2350 "Native Heap:", memInfo.getSummaryNativeHeap());
2351 printRow(pw, ONE_COUNT_COLUMN,
2352 "Code:", memInfo.getSummaryCode());
2353 printRow(pw, ONE_COUNT_COLUMN,
2354 "Stack:", memInfo.getSummaryStack());
2355 printRow(pw, ONE_COUNT_COLUMN,
2356 "Graphics:", memInfo.getSummaryGraphics());
2357 printRow(pw, ONE_COUNT_COLUMN,
2358 "Private Other:", memInfo.getSummaryPrivateOther());
2359 printRow(pw, ONE_COUNT_COLUMN,
2360 "System:", memInfo.getSummarySystem());
2361 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002362 if (memInfo.hasSwappedOutPss) {
2363 printRow(pw, TWO_COUNT_COLUMNS,
2364 "TOTAL:", memInfo.getSummaryTotalPss(),
2365 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2366 } else {
2367 printRow(pw, TWO_COUNT_COLUMNS,
2368 "TOTAL:", memInfo.getSummaryTotalPss(),
2369 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2370 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002371 }
2372
Jeff Hamilton52d32032011-01-08 15:31:26 -06002373 public void registerOnActivityPausedListener(Activity activity,
2374 OnActivityPausedListener listener) {
2375 synchronized (mOnPauseListeners) {
2376 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2377 if (list == null) {
2378 list = new ArrayList<OnActivityPausedListener>();
2379 mOnPauseListeners.put(activity, list);
2380 }
2381 list.add(listener);
2382 }
2383 }
2384
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002385 public void unregisterOnActivityPausedListener(Activity activity,
2386 OnActivityPausedListener listener) {
2387 synchronized (mOnPauseListeners) {
2388 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2389 if (list != null) {
2390 list.remove(listener);
2391 }
2392 }
2393 }
2394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 public final ActivityInfo resolveActivityInfo(Intent intent) {
2396 ActivityInfo aInfo = intent.resolveActivityInfo(
2397 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2398 if (aInfo == null) {
2399 // Throw an exception.
2400 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002401 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 }
2403 return aInfo;
2404 }
Bob Leee5408332009-09-04 18:31:17 -07002405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002408 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002409 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002411 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 r.intent = intent;
2413 r.state = state;
2414 r.parent = parent;
2415 r.embeddedID = id;
2416 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002417 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 if (localLOGV) {
2419 ComponentName compname = intent.getComponent();
2420 String name;
2421 if (compname != null) {
2422 name = compname.toShortString();
2423 } else {
2424 name = "(Intent " + intent + ").getComponent() returned null";
2425 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002426 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 + ", comp=" + name
2428 + ", token=" + token);
2429 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002430 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 }
2432
2433 public final Activity getActivity(IBinder token) {
2434 return mActivities.get(token).activity;
2435 }
2436
2437 public final void sendActivityResult(
2438 IBinder token, String id, int requestCode,
2439 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002440 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002441 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2443 list.add(new ResultInfo(id, requestCode, resultCode, data));
2444 mAppThread.scheduleSendResult(token, list);
2445 }
2446
Jeff Brown9ef09972013-10-15 20:49:59 -07002447 private void sendMessage(int what, Object obj) {
2448 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 }
2450
Jeff Brown9ef09972013-10-15 20:49:59 -07002451 private void sendMessage(int what, Object obj, int arg1) {
2452 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 }
2454
Jeff Brown9ef09972013-10-15 20:49:59 -07002455 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2456 sendMessage(what, obj, arg1, arg2, false);
2457 }
2458
2459 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2460 if (DEBUG_MESSAGES) Slog.v(
2461 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2462 + ": " + arg1 + " / " + obj);
2463 Message msg = Message.obtain();
2464 msg.what = what;
2465 msg.obj = obj;
2466 msg.arg1 = arg1;
2467 msg.arg2 = arg2;
2468 if (async) {
2469 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002471 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002474 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2475 if (DEBUG_MESSAGES) Slog.v(
2476 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2477 "seq= " + seq);
2478 Message msg = Message.obtain();
2479 msg.what = what;
2480 SomeArgs args = SomeArgs.obtain();
2481 args.arg1 = obj;
2482 args.argi1 = arg1;
2483 args.argi2 = arg2;
2484 args.argi3 = seq;
2485 msg.obj = args;
2486 mH.sendMessage(msg);
2487 }
2488
Dianne Hackborn21556372010-02-04 16:34:40 -08002489 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 String what) {
2491 ContextCleanupInfo cci = new ContextCleanupInfo();
2492 cci.context = context;
2493 cci.who = who;
2494 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002495 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 }
2497
Craig Mautner233ceee2014-05-09 17:05:11 -07002498 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2500
2501 ActivityInfo aInfo = r.activityInfo;
2502 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002503 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 Context.CONTEXT_INCLUDE_CODE);
2505 }
Bob Leee5408332009-09-04 18:31:17 -07002506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 ComponentName component = r.intent.getComponent();
2508 if (component == null) {
2509 component = r.intent.resolveActivity(
2510 mInitialApplication.getPackageManager());
2511 r.intent.setComponent(component);
2512 }
2513
2514 if (r.activityInfo.targetActivity != null) {
2515 component = new ComponentName(r.activityInfo.packageName,
2516 r.activityInfo.targetActivity);
2517 }
2518
2519 Activity activity = null;
2520 try {
2521 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2522 activity = mInstrumentation.newActivity(
2523 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002524 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002525 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002526 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002527 if (r.state != null) {
2528 r.state.setClassLoader(cl);
2529 }
2530 } catch (Exception e) {
2531 if (!mInstrumentation.onException(activity, e)) {
2532 throw new RuntimeException(
2533 "Unable to instantiate activity " + component
2534 + ": " + e.toString(), e);
2535 }
2536 }
2537
2538 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002539 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002540
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002541 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2542 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 TAG, r + ": app=" + app
2544 + ", appName=" + app.getPackageName()
2545 + ", pkg=" + r.packageInfo.getPackageName()
2546 + ", comp=" + r.intent.getComponent().toShortString()
2547 + ", dir=" + r.packageInfo.getAppDir());
2548
2549 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002550 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002552 Configuration config = new Configuration(mCompatConfiguration);
Andrii Kuliand0ad9382016-04-18 20:54:20 -07002553 if (r.overrideConfig != null) {
2554 config.updateFrom(r.overrideConfig);
2555 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002556 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002557 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002558 Window window = null;
2559 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2560 window = r.mPendingRemoveWindow;
2561 r.mPendingRemoveWindow = null;
2562 r.mPendingRemoveWindowManager = null;
2563 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002564 activity.attach(appContext, this, getInstrumentation(), r.token,
2565 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002566 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002567 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002568
Christopher Tateb70f3df2009-04-07 16:07:59 -07002569 if (customIntent != null) {
2570 activity.mIntent = customIntent;
2571 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002572 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 activity.mStartedActivity = false;
2574 int theme = r.activityInfo.getThemeResource();
2575 if (theme != 0) {
2576 activity.setTheme(theme);
2577 }
2578
2579 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002580 if (r.isPersistable()) {
2581 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2582 } else {
2583 mInstrumentation.callActivityOnCreate(activity, r.state);
2584 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 if (!activity.mCalled) {
2586 throw new SuperNotCalledException(
2587 "Activity " + r.intent.getComponent().toShortString() +
2588 " did not call through to super.onCreate()");
2589 }
2590 r.activity = activity;
2591 r.stopped = true;
2592 if (!r.activity.mFinished) {
2593 activity.performStart();
2594 r.stopped = false;
2595 }
2596 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002597 if (r.isPersistable()) {
2598 if (r.state != null || r.persistentState != null) {
2599 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2600 r.persistentState);
2601 }
2602 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002603 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2604 }
2605 }
2606 if (!r.activity.mFinished) {
2607 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002608 if (r.isPersistable()) {
2609 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2610 r.persistentState);
2611 } else {
2612 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 if (!activity.mCalled) {
2615 throw new SuperNotCalledException(
2616 "Activity " + r.intent.getComponent().toShortString() +
2617 " did not call through to super.onPostCreate()");
2618 }
2619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 }
2621 r.paused = true;
2622
2623 mActivities.put(r.token, r);
2624
2625 } catch (SuperNotCalledException e) {
2626 throw e;
2627
2628 } catch (Exception e) {
2629 if (!mInstrumentation.onException(activity, e)) {
2630 throw new RuntimeException(
2631 "Unable to start activity " + component
2632 + ": " + e.toString(), e);
2633 }
2634 }
2635
2636 return activity;
2637 }
2638
Wale Ogunwale7c726682015-02-06 17:34:28 -08002639 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2640 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002641 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002642 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002643 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002644 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002645 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002646
Wale Ogunwale7c726682015-02-06 17:34:28 -08002647 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski082614c2016-03-04 14:33:47 -08002648 this, r.packageInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002649 appContext.setOuterContext(activity);
2650 Context baseContext = appContext;
2651
2652 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002653 // For debugging purposes, if the activity's package name contains the value of
2654 // the "debug.use-second-display" system property as a substring, then show
2655 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002656 String pkgName = SystemProperties.get("debug.second-display.pkg");
2657 if (pkgName != null && !pkgName.isEmpty()
2658 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002659 for (int id : dm.getDisplayIds()) {
2660 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002661 Display display =
2662 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002663 baseContext = appContext.createDisplayContext(display);
2664 break;
2665 }
2666 }
2667 }
2668 return baseContext;
2669 }
2670
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002671 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 // If we are getting ready to gc after going to the background, well
2673 // we are back active so skip it.
2674 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002675 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002676
Jeff Hao1b012d32014-08-20 10:35:34 -07002677 if (r.profilerInfo != null) {
2678 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002679 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002680 }
2681
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002682 // Make sure we are running with the most recent config.
2683 handleConfigurationChanged(null, null);
2684
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002685 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002687
Chet Haase0d1c27a2014-11-03 18:35:16 +00002688 // Initialize before creating the activity
2689 WindowManagerGlobal.initialize();
2690
Craig Mautner233ceee2014-05-09 17:05:11 -07002691 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692
2693 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002694 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002695 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002696 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002697 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002698 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699
2700 if (!r.activity.mFinished && r.startsNotResumed) {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002701 // The activity manager actually wants this one to start out paused, because it
2702 // needs to be visible but isn't in the foreground. We accomplish this by going
2703 // through the normal startup (because activities expect to go through onResume()
2704 // the first time they run, before their window is displayed), and then pausing it.
2705 // However, in this case we do -not- need to do the full pause cycle (of freezing
2706 // and such) because the activity manager assumes it can just retain the current
2707 // state it has.
2708 performPauseActivityIfNeeded(r, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002710 // We need to keep around the original state, in case we need to be created again.
2711 // But we only do this for pre-Honeycomb apps, which always save their state when
2712 // pausing, so we can not have them save their state when restarting from a paused
2713 // state. For HC and later, we want to (and can) let the state be saved as the
2714 // normal part of stopping the activity.
2715 if (r.isPreHoneycomb()) {
2716 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002718 }
2719 } else {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002720 // If there was an error, for any reason, tell the activity manager to stop us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 try {
2722 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002723 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2724 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002726 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 }
2728 }
2729 }
2730
Filip Gruszczynski23493322015-07-29 17:02:59 -07002731 private void reportSizeConfigurations(ActivityClientRecord r) {
2732 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2733 if (configurations == null) {
2734 return;
2735 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002736 SparseIntArray horizontal = new SparseIntArray();
2737 SparseIntArray vertical = new SparseIntArray();
2738 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002739 for (int i = configurations.length - 1; i >= 0; i--) {
2740 Configuration config = configurations[i];
2741 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002742 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002743 }
2744 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002745 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002746 }
2747 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002748 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002749 }
2750 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002751 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002752 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2753 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002754 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002755 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002756 }
2757
2758 }
2759
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002760 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 final int N = intents.size();
2762 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002763 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002765 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002766 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2768 }
2769 }
2770
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002771 public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002772 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (r != null) {
2774 final boolean resumed = !r.paused;
2775 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002776 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 mInstrumentation.callActivityOnPause(r.activity);
2778 }
2779 deliverNewIntents(r, intents);
2780 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002781 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002782 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 }
2784 }
2785 }
Bob Leee5408332009-09-04 18:31:17 -07002786
Romain Guy65b345f2011-07-27 18:51:50 -07002787 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 performNewIntents(data.token, data.intents);
2789 }
2790
Adam Skorydfc7fd72013-08-05 19:23:41 -07002791 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Amith Yamasani4f128e42016-05-10 11:44:12 -07002792 if (mLastSessionId != cmd.sessionId) {
2793 // Clear the existing structures
2794 mLastSessionId = cmd.sessionId;
2795 for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
2796 AssistStructure structure = mLastAssistStructures.get(i).get();
2797 if (structure != null) {
2798 structure.clearSendChannel();
2799 }
2800 mLastAssistStructures.remove(i);
Dianne Hackborn782d4982015-07-08 17:36:37 -07002801 }
2802 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002803 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002804 AssistStructure structure = null;
2805 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002806 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002807 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002808 if (r != null) {
2809 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2810 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002811 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002812 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002813 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002814 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002815 if (activityIntent != null && (r.window == null ||
2816 (r.window.getAttributes().flags
2817 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002818 Intent intent = new Intent(activityIntent);
2819 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2820 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2821 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002822 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002823 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002824 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002825 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002826 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002827 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002828 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002829 if (structure == null) {
2830 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002831 }
Amith Yamasani4f128e42016-05-10 11:44:12 -07002832 mLastAssistStructures.add(new WeakReference<>(structure));
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002833 IActivityManager mgr = ActivityManagerNative.getDefault();
2834 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002835 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002836 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002837 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002838 }
2839 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002840
2841 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2842 ActivityClientRecord r = mActivities.get(token);
2843 if (r != null) {
2844 r.activity.onTranslucentConversionComplete(drawComplete);
2845 }
2846 }
2847
Craig Mautnereb8abf72014-07-02 15:04:09 -07002848 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2849 ActivityClientRecord r = mActivities.get(token);
2850 if (r != null) {
2851 r.activity.onNewActivityOptions(options);
2852 }
2853 }
2854
Jose Lima4b6c6692014-08-12 17:41:12 -07002855 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002856 ActivityClientRecord r = mActivities.get(token);
2857 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002858 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002859 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002860 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002861 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002862 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002863 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2864 if (!activity.mCalled) {
2865 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002866 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002867 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002868 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002869 }
2870 }
2871 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002872 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002873 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002874 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002875 }
2876 }
2877
Jose Lima4b6c6692014-08-12 17:41:12 -07002878 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002879 ActivityClientRecord r = mActivities.get(token);
2880 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002881 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002882 }
2883 }
2884
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002885 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002886 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2887 try {
2888 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2889 } finally {
2890 StrictMode.setThreadPolicy(oldPolicy);
2891 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002892 }
2893
Craig Mautner8746a472014-07-24 15:12:54 -07002894 private void handleEnterAnimationComplete(IBinder token) {
2895 ActivityClientRecord r = mActivities.get(token);
2896 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002897 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002898 }
2899 }
2900
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002901 private void handleStartBinderTracking() {
2902 Binder.enableTracing();
2903 }
2904
2905 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2906 try {
2907 Binder.disableTracing();
2908 Binder.getTransactionTracker().writeTracesToFile(fd);
2909 } finally {
2910 IoUtils.closeQuietly(fd);
2911 Binder.getTransactionTracker().clearTraces();
2912 }
2913 }
2914
Andrii Kulian933076d2016-03-29 17:04:42 -07002915 private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002916 final ActivityClientRecord r = mActivities.get(token);
2917 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002918 r.activity.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002919 }
2920 }
2921
Andrii Kulian933076d2016-03-29 17:04:42 -07002922 private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002923 final ActivityClientRecord r = mActivities.get(token);
2924 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002925 r.activity.dispatchPictureInPictureModeChanged(isInPipMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002926 }
2927 }
2928
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002929 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2930 final ActivityClientRecord r = mActivities.get(token);
2931 if (r != null) {
2932 r.voiceInteractor = interactor;
2933 r.activity.setVoiceInteractor(interactor);
2934 if (interactor == null) {
2935 r.activity.onLocalVoiceInteractionStopped();
2936 } else {
2937 r.activity.onLocalVoiceInteractionStarted();
2938 }
2939 }
2940 }
2941
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002942 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2943
2944 /**
2945 * Return the Intent that's currently being handled by a
2946 * BroadcastReceiver on this thread, or null if none.
2947 * @hide
2948 */
2949 public static Intent getIntentBeingBroadcast() {
2950 return sCurrentBroadcastIntent.get();
2951 }
2952
Romain Guy65b345f2011-07-27 18:51:50 -07002953 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954 // If we are getting ready to gc after going to the background, well
2955 // we are back active so skip it.
2956 unscheduleGcIdler();
2957
2958 String component = data.intent.getComponent().getClassName();
2959
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002960 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002961 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962
2963 IActivityManager mgr = ActivityManagerNative.getDefault();
2964
Romain Guy65b345f2011-07-27 18:51:50 -07002965 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 try {
2967 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2968 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002969 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002970 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2972 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002973 if (DEBUG_BROADCAST) Slog.i(TAG,
2974 "Finishing failed broadcast to " + data.intent.getComponent());
2975 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 throw new RuntimeException(
2977 "Unable to instantiate receiver " + component
2978 + ": " + e.toString(), e);
2979 }
2980
2981 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002982 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002983
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002984 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 TAG, "Performing receive of " + data.intent
2986 + ": app=" + app
2987 + ", appName=" + app.getPackageName()
2988 + ", pkg=" + packageInfo.getPackageName()
2989 + ", comp=" + data.intent.getComponent().toShortString()
2990 + ", dir=" + packageInfo.getAppDir());
2991
Dianne Hackborn21556372010-02-04 16:34:40 -08002992 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002993 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002994 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 receiver.onReceive(context.getReceiverRestrictedContext(),
2996 data.intent);
2997 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002998 if (DEBUG_BROADCAST) Slog.i(TAG,
2999 "Finishing failed broadcast to " + data.intent.getComponent());
3000 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 if (!mInstrumentation.onException(receiver, e)) {
3002 throw new RuntimeException(
3003 "Unable to start receiver " + component
3004 + ": " + e.toString(), e);
3005 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003006 } finally {
3007 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008 }
3009
Dianne Hackborne829fef2010-10-26 17:44:01 -07003010 if (receiver.getPendingResult() != null) {
3011 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 }
3013 }
3014
Christopher Tate181fafa2009-05-14 11:12:14 -07003015 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003016 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003017 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003018
Christopher Tate346acb12012-10-15 19:20:25 -07003019 // Sanity check the requested target package's uid against ours
3020 try {
3021 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3022 data.appInfo.packageName, 0, UserHandle.myUserId());
3023 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3024 Slog.w(TAG, "Asked to instantiate non-matching package "
3025 + data.appInfo.packageName);
3026 return;
3027 }
3028 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003029 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003030 }
3031
Christopher Tate181fafa2009-05-14 11:12:14 -07003032 // no longer idle; we have backup work to do
3033 unscheduleGcIdler();
3034
3035 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003036 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003037 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003038 if (packageName == null) {
3039 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3040 return;
3041 }
3042
Christopher Tate181fafa2009-05-14 11:12:14 -07003043 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003044 // full backup operation but no app-supplied agent? use the default implementation
3045 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3046 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003047 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003048 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003049
Christopher Tate181fafa2009-05-14 11:12:14 -07003050 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003051 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003052 BackupAgent agent = mBackupAgents.get(packageName);
3053 if (agent != null) {
3054 // reusing the existing instance
3055 if (DEBUG_BACKUP) {
3056 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003057 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003058 binder = agent.onBind();
3059 } else {
3060 try {
3061 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3062
3063 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3064 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3065
3066 // set up the agent's context
3067 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3068 context.setOuterContext(agent);
3069 agent.attach(context);
3070
3071 agent.onCreate();
3072 binder = agent.onBind();
3073 mBackupAgents.put(packageName, agent);
3074 } catch (Exception e) {
3075 // If this is during restore, fail silently; otherwise go
3076 // ahead and let the user see the crash.
3077 Slog.e(TAG, "Agent threw during creation: " + e);
3078 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3079 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3080 throw e;
3081 }
3082 // falling through with 'binder' still null
3083 }
Christopher Tated1475e02009-07-09 15:36:17 -07003084 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003085
3086 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003087 try {
3088 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3089 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003090 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003091 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003092 } catch (Exception e) {
3093 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003094 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003095 }
3096 }
3097
3098 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003099 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003100 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003101
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003102 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003103 String packageName = packageInfo.mPackageName;
3104 BackupAgent agent = mBackupAgents.get(packageName);
3105 if (agent != null) {
3106 try {
3107 agent.onDestroy();
3108 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003109 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003110 e.printStackTrace();
3111 }
3112 mBackupAgents.remove(packageName);
3113 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003114 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003115 }
3116 }
3117
Romain Guy65b345f2011-07-27 18:51:50 -07003118 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 // If we are getting ready to gc after going to the background, well
3120 // we are back active so skip it.
3121 unscheduleGcIdler();
3122
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003123 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003124 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003125 Service service = null;
3126 try {
3127 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3128 service = (Service) cl.loadClass(data.info.name).newInstance();
3129 } catch (Exception e) {
3130 if (!mInstrumentation.onException(service, e)) {
3131 throw new RuntimeException(
3132 "Unable to instantiate service " + data.info.name
3133 + ": " + e.toString(), e);
3134 }
3135 }
3136
3137 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003138 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003139
Jeff Browndefd4a62014-03-10 21:24:37 -07003140 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3141 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003143 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 service.attach(context, this, data.info.name, data.token, app,
3145 ActivityManagerNative.getDefault());
3146 service.onCreate();
3147 mServices.put(data.token, service);
3148 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003149 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003150 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003152 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003153 }
3154 } catch (Exception e) {
3155 if (!mInstrumentation.onException(service, e)) {
3156 throw new RuntimeException(
3157 "Unable to create service " + data.info.name
3158 + ": " + e.toString(), e);
3159 }
3160 }
3161 }
3162
Romain Guy65b345f2011-07-27 18:51:50 -07003163 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003165 if (DEBUG_SERVICE)
3166 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 if (s != null) {
3168 try {
3169 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003170 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 try {
3172 if (!data.rebind) {
3173 IBinder binder = s.onBind(data.intent);
3174 ActivityManagerNative.getDefault().publishService(
3175 data.token, data.intent, binder);
3176 } else {
3177 s.onRebind(data.intent);
3178 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003179 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003181 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003182 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003183 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 }
3185 } catch (Exception e) {
3186 if (!mInstrumentation.onException(s, e)) {
3187 throw new RuntimeException(
3188 "Unable to bind to service " + s
3189 + " with " + data.intent + ": " + e.toString(), e);
3190 }
3191 }
3192 }
3193 }
3194
Romain Guy65b345f2011-07-27 18:51:50 -07003195 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 Service s = mServices.get(data.token);
3197 if (s != null) {
3198 try {
3199 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003200 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 boolean doRebind = s.onUnbind(data.intent);
3202 try {
3203 if (doRebind) {
3204 ActivityManagerNative.getDefault().unbindFinished(
3205 data.token, data.intent, doRebind);
3206 } else {
3207 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003208 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 }
3210 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003211 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003212 }
3213 } catch (Exception e) {
3214 if (!mInstrumentation.onException(s, e)) {
3215 throw new RuntimeException(
3216 "Unable to unbind to service " + s
3217 + " with " + data.intent + ": " + e.toString(), e);
3218 }
3219 }
3220 }
3221 }
3222
Dianne Hackborn625ac272010-09-17 18:29:22 -07003223 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003224 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3225 try {
3226 Service s = mServices.get(info.token);
3227 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003228 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3229 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003230 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3231 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003233 } finally {
3234 IoUtils.closeQuietly(info.fd);
3235 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 }
3237 }
3238
Dianne Hackborn625ac272010-09-17 18:29:22 -07003239 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003240 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3241 try {
3242 ActivityClientRecord r = mActivities.get(info.token);
3243 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003244 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3245 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003246 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3247 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003248 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003249 } finally {
3250 IoUtils.closeQuietly(info.fd);
3251 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003252 }
3253 }
3254
Marco Nelissen18cb2872011-11-15 11:19:53 -08003255 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003256 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3257 try {
3258 ProviderClientRecord r = mLocalProviders.get(info.token);
3259 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003260 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3261 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003262 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3263 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003264 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003265 } finally {
3266 IoUtils.closeQuietly(info.fd);
3267 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003268 }
3269 }
3270
Romain Guy65b345f2011-07-27 18:51:50 -07003271 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 Service s = mServices.get(data.token);
3273 if (s != null) {
3274 try {
3275 if (data.args != null) {
3276 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003277 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003279 int res;
3280 if (!data.taskRemoved) {
3281 res = s.onStartCommand(data.args, data.flags, data.startId);
3282 } else {
3283 s.onTaskRemoved(data.args);
3284 res = Service.START_TASK_REMOVED_COMPLETE;
3285 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003286
3287 QueuedWork.waitToFinish();
3288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003290 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003291 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003293 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003294 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003295 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 } catch (Exception e) {
3297 if (!mInstrumentation.onException(s, e)) {
3298 throw new RuntimeException(
3299 "Unable to start service " + s
3300 + " with " + data.args + ": " + e.toString(), e);
3301 }
3302 }
3303 }
3304 }
3305
Romain Guy65b345f2011-07-27 18:51:50 -07003306 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 Service s = mServices.remove(token);
3308 if (s != null) {
3309 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003310 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003311 s.onDestroy();
3312 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003313 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003315 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003317
3318 QueuedWork.waitToFinish();
3319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003321 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003322 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003324 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 }
3326 } catch (Exception e) {
3327 if (!mInstrumentation.onException(s, e)) {
3328 throw new RuntimeException(
3329 "Unable to stop service " + s
3330 + ": " + e.toString(), e);
3331 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003332 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003334 } else {
3335 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003337 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 }
3339
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003340 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003341 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003342 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003343 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 + " finished=" + r.activity.mFinished);
3345 if (r != null && !r.activity.mFinished) {
3346 if (clearHide) {
3347 r.hideForNow = false;
3348 r.activity.mStartedActivity = false;
3349 }
3350 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003351 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003352 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 if (r.pendingIntents != null) {
3354 deliverNewIntents(r, r.pendingIntents);
3355 r.pendingIntents = null;
3356 }
3357 if (r.pendingResults != null) {
3358 deliverResults(r, r.pendingResults);
3359 r.pendingResults = null;
3360 }
3361 r.activity.performResume();
3362
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003363 // If there is a pending local relaunch that was requested when the activity was
3364 // paused, it will put the activity into paused state when it finally happens.
3365 // Since the activity resumed before being relaunched, we don't want that to happen,
3366 // so we need to clear the request to relaunch paused.
3367 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3368 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3369 if (relaunching.token == r.token
3370 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3371 relaunching.startsNotResumed = false;
3372 }
3373 }
3374
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003375 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3376 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 r.paused = false;
3379 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003381 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 } catch (Exception e) {
3383 if (!mInstrumentation.onException(r.activity, e)) {
3384 throw new RuntimeException(
3385 "Unable to resume activity "
3386 + r.intent.getComponent().toShortString()
3387 + ": " + e.toString(), e);
3388 }
3389 }
3390 }
3391 return r;
3392 }
3393
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003394 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3395 if (r.mPreserveWindow && !force) {
3396 return;
3397 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003398 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003399 r.mPendingRemoveWindowManager.removeViewImmediate(
3400 r.mPendingRemoveWindow.getDecorView());
3401 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003402 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003403 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003404 r.activity.getClass().getName(), "Activity");
3405 }
3406 }
3407 r.mPendingRemoveWindow = null;
3408 r.mPendingRemoveWindowManager = null;
3409 }
3410
Craig Mautner233ceee2014-05-09 17:05:11 -07003411 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003412 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003413 ActivityClientRecord r = mActivities.get(token);
3414 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3415 return;
3416 }
3417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 // If we are getting ready to gc after going to the background, well
3419 // we are back active so skip it.
3420 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003421 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422
Adam Powellcfbe9be2013-11-06 14:58:58 -08003423 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003424 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425
3426 if (r != null) {
3427 final Activity a = r.activity;
3428
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003429 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 TAG, "Resume " + r + " started activity: " +
3431 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3432 + ", finished: " + a.mFinished);
3433
3434 final int forwardBit = isForward ?
3435 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 // If the window hasn't yet been added to the window manager,
3438 // and this guy didn't finish itself or start another activity,
3439 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003440 boolean willBeVisible = !a.mStartedActivity;
3441 if (!willBeVisible) {
3442 try {
3443 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3444 a.getActivityToken());
3445 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003446 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003447 }
3448 }
3449 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003450 r.window = r.activity.getWindow();
3451 View decor = r.window.getDecorView();
3452 decor.setVisibility(View.INVISIBLE);
3453 ViewManager wm = a.getWindowManager();
3454 WindowManager.LayoutParams l = r.window.getAttributes();
3455 a.mDecor = decor;
3456 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3457 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003458 if (r.mPreserveWindow) {
3459 a.mWindowAdded = true;
3460 r.mPreserveWindow = false;
Robert Carrb2594852016-04-25 16:21:13 -07003461 // Normally the ViewRoot sets up callbacks with the Activity
3462 // in addView->ViewRootImpl#setView. If we are instead reusing
3463 // the decor view we have to notify the view root that the
3464 // callbacks may have changed.
3465 ViewRootImpl impl = decor.getViewRootImpl();
Robert Carr311ecba2016-04-27 12:59:45 -07003466 if (impl != null) {
3467 impl.notifyChildRebuilt();
3468 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003469 }
3470 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 a.mWindowAdded = true;
3472 wm.addView(decor, l);
3473 }
3474
3475 // If the window has already been added, but during resume
3476 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003477 // window visible.
3478 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003479 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480 TAG, "Launch " + r + " mStartedActivity set");
3481 r.hideForNow = true;
3482 }
3483
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003484 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003485 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 // The window is now visible if it has been added, we are not
3488 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003489 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003490 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003492 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003493 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003494 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 r.newConfig = null;
3496 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003497 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003498 + isForward);
3499 WindowManager.LayoutParams l = r.window.getAttributes();
3500 if ((l.softInputMode
3501 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3502 != forwardBit) {
3503 l.softInputMode = (l.softInputMode
3504 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3505 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003506 if (r.activity.mVisibleFromClient) {
3507 ViewManager wm = a.getWindowManager();
3508 View decor = r.window.getDecorView();
3509 wm.updateViewLayout(decor, l);
3510 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003511 }
3512 r.activity.mVisibleFromServer = true;
3513 mNumVisibleActivities++;
3514 if (r.activity.mVisibleFromClient) {
3515 r.activity.makeVisible();
3516 }
3517 }
3518
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003519 if (!r.onlyLocalRequest) {
3520 r.nextIdle = mNewActivities;
3521 mNewActivities = r;
3522 if (localLOGV) Slog.v(
3523 TAG, "Scheduling idle handler for " + r);
3524 Looper.myQueue().addIdleHandler(new Idler());
3525 }
3526 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003528 // Tell the activity manager we have resumed.
3529 if (reallyResume) {
3530 try {
3531 ActivityManagerNative.getDefault().activityResumed(token);
3532 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003533 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003534 }
3535 }
3536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 } else {
3538 // If an exception was thrown when trying to resume, then
3539 // just end this activity.
3540 try {
3541 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003542 .finishActivity(token, Activity.RESULT_CANCELED, null,
3543 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003545 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 }
3547 }
3548 }
3549
3550 private int mThumbnailWidth = -1;
3551 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003552 private Bitmap mAvailThumbnailBitmap = null;
3553 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554
Romain Guy65b345f2011-07-27 18:51:50 -07003555 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003556 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003558 if (thumbnail == null) {
3559 int w = mThumbnailWidth;
3560 int h;
3561 if (w < 0) {
3562 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003563 int wId = com.android.internal.R.dimen.thumbnail_width;
3564 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003565 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3566 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003567 } else {
3568 h = mThumbnailHeight;
3569 }
3570
3571 // On platforms where we don't want thumbnails, set dims to (0,0)
3572 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003573 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3574 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003575 thumbnail.eraseColor(0);
3576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 }
3578
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003579 if (thumbnail != null) {
3580 Canvas cv = mThumbnailCanvas;
3581 if (cv == null) {
3582 mThumbnailCanvas = cv = new Canvas();
3583 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003584
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003585 cv.setBitmap(thumbnail);
3586 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3587 mAvailThumbnailBitmap = thumbnail;
3588 thumbnail = null;
3589 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003590 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003593 } catch (Exception e) {
3594 if (!mInstrumentation.onException(r.activity, e)) {
3595 throw new RuntimeException(
3596 "Unable to create thumbnail of "
3597 + r.intent.getComponent().toShortString()
3598 + ": " + e.toString(), e);
3599 }
3600 thumbnail = null;
3601 }
3602
3603 return thumbnail;
3604 }
3605
Romain Guy65b345f2011-07-27 18:51:50 -07003606 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003607 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003608 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003609 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003610 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3611 return;
3612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003613 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003614 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 if (userLeaving) {
3616 performUserLeavingActivity(r);
3617 }
Bob Leee5408332009-09-04 18:31:17 -07003618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003620 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003622 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003623 if (r.isPreHoneycomb()) {
3624 QueuedWork.waitToFinish();
3625 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003626
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003628 if (!dontReport) {
3629 try {
3630 ActivityManagerNative.getDefault().activityPaused(token);
3631 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003632 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003635 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 }
3637 }
3638
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003639 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 mInstrumentation.callActivityOnUserLeaving(r.activity);
3641 }
3642
3643 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003644 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003645 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003646 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 }
3648
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003649 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003650 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 if (r.paused) {
3652 if (r.activity.mFinished) {
3653 // If we are finishing, we won't call onResume() in certain cases.
3654 // So here we likewise don't want to call onPause() if the activity
3655 // isn't resumed.
3656 return null;
3657 }
3658 RuntimeException e = new RuntimeException(
3659 "Performing pause of activity that is not resumed: "
3660 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003661 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003663 if (finished) {
3664 r.activity.mFinished = true;
3665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003667 // Next have the activity save its current state and managed dialogs...
3668 if (!r.activity.mFinished && saveState) {
3669 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003671
3672 performPauseActivityIfNeeded(r, reason);
Jeff Hamilton52d32032011-01-08 15:31:26 -06003673
3674 // Notify any outstanding on paused listeners
3675 ArrayList<OnActivityPausedListener> listeners;
3676 synchronized (mOnPauseListeners) {
3677 listeners = mOnPauseListeners.remove(r.activity);
3678 }
3679 int size = (listeners != null ? listeners.size() : 0);
3680 for (int i = 0; i < size; i++) {
3681 listeners.get(i).onPaused(r.activity);
3682 }
3683
Craig Mautnera0026042014-04-23 11:45:37 -07003684 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003687 private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
3688 if (r.paused) {
3689 // You are already paused silly...
3690 return;
3691 }
3692
3693 try {
3694 r.activity.mCalled = false;
3695 mInstrumentation.callActivityOnPause(r.activity);
3696 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
3697 r.activity.getComponentName().getClassName(), reason);
3698 if (!r.activity.mCalled) {
3699 throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
3700 + " did not call through to super.onPause()");
3701 }
3702 } catch (SuperNotCalledException e) {
3703 throw e;
3704 } catch (Exception e) {
3705 if (!mInstrumentation.onException(r.activity, e)) {
3706 throw new RuntimeException("Unable to pause activity "
3707 + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
3708 }
3709 }
3710 r.paused = true;
3711 }
3712
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003713 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003714 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003715 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
3717
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003718 private static class StopInfo implements Runnable {
3719 ActivityClientRecord activity;
3720 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003721 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003723
3724 @Override public void run() {
3725 // Tell activity manager we have been stopped.
3726 try {
3727 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3728 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003729 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003730 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003731 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003732 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003733 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003734 return;
3735 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003736 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003737 }
3738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 }
3740
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003741 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003742 public final IActivityManager.ContentProviderHolder holder;
3743 public final ProviderClientRecord client;
3744 public int stableCount;
3745 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003746
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003747 // When this is set, the stable and unstable ref counts are 0 and
3748 // we have a pending operation scheduled to remove the ref count
3749 // from the activity manager. On the activity manager we are still
3750 // holding an unstable ref, though it is not reflected in the counts
3751 // here.
3752 public boolean removePending;
3753
3754 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3755 ProviderClientRecord inClient, int sCount, int uCount) {
3756 holder = inHolder;
3757 client = inClient;
3758 stableCount = sCount;
3759 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 }
3761 }
3762
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003763 /**
3764 * Core implementation of stopping an activity. Note this is a little
3765 * tricky because the server's meaning of stop is slightly different
3766 * than our client -- for the server, stop means to save state and give
3767 * it the result when it is done, but the window may still be visible.
3768 * For the client, we want to call onStop()/onStart() to indicate when
3769 * the activity's UI visibillity changes.
3770 */
Romain Guy65b345f2011-07-27 18:51:50 -07003771 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003772 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003773 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003774 if (r != null) {
3775 if (!keepShown && r.stopped) {
3776 if (r.activity.mFinished) {
3777 // If we are finishing, we won't call onResume() in certain
3778 // cases. So here we likewise don't want to call onStop()
3779 // if the activity isn't resumed.
3780 return;
3781 }
3782 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07003783 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003784 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003785 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07003786 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003787 }
3788
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003789 // One must first be paused before stopped...
3790 performPauseActivityIfNeeded(r, reason);
3791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 if (info != null) {
3793 try {
3794 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003795 // For now, don't create the thumbnail here; we are
3796 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 info.description = r.activity.onCreateDescription();
3798 } catch (Exception e) {
3799 if (!mInstrumentation.onException(r.activity, e)) {
3800 throw new RuntimeException(
3801 "Unable to save state of activity "
3802 + r.intent.getComponent().toShortString()
3803 + ": " + e.toString(), e);
3804 }
3805 }
3806 }
3807
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003808 // Next have the activity save its current state and managed dialogs...
3809 if (!r.activity.mFinished && saveState) {
3810 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003811 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003812 }
3813 }
3814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 if (!keepShown) {
3816 try {
3817 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003818 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 } catch (Exception e) {
3820 if (!mInstrumentation.onException(r.activity, e)) {
3821 throw new RuntimeException(
3822 "Unable to stop activity "
3823 + r.intent.getComponent().toShortString()
3824 + ": " + e.toString(), e);
3825 }
3826 }
3827 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003828 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3829 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 }
3832 }
3833
Romain Guy65b345f2011-07-27 18:51:50 -07003834 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 View v = r.activity.mDecor;
3836 if (v != null) {
3837 if (show) {
3838 if (!r.activity.mVisibleFromServer) {
3839 r.activity.mVisibleFromServer = true;
3840 mNumVisibleActivities++;
3841 if (r.activity.mVisibleFromClient) {
3842 r.activity.makeVisible();
3843 }
3844 }
3845 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003846 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003847 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08003848 + r.activityInfo.name + " with new config "
3849 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003850 r.newConfig = null;
3851 }
3852 } else {
3853 if (r.activity.mVisibleFromServer) {
3854 r.activity.mVisibleFromServer = false;
3855 mNumVisibleActivities--;
3856 v.setVisibility(View.INVISIBLE);
3857 }
3858 }
3859 }
3860 }
3861
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003862 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003863 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003864 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3865 return;
3866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 r.activity.mConfigChangeFlags |= configChanges;
3868
3869 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003870 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003872 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003873 TAG, "Finishing stop of " + r + ": show=" + show
3874 + " win=" + r.window);
3875
3876 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003877
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003878 // Make sure any pending writes are now committed.
3879 if (!r.isPreHoneycomb()) {
3880 QueuedWork.waitToFinish();
3881 }
3882
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003883 // Schedule the call to tell the activity manager we have
3884 // stopped. We don't do this immediately, because we want to
3885 // have a chance for any other pending work (in particular memory
3886 // trim requests) to complete before you tell the activity
3887 // manager to proceed and allow us to go fully into the background.
3888 info.activity = r;
3889 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003890 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003891 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003892 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003893 }
3894
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003895 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3896 String action) {
3897 if (r == null) {
3898 return true;
3899 }
3900 if (seq < r.lastProcessedSeq) {
3901 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3902 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3903 return false;
3904 }
3905 r.lastProcessedSeq = seq;
3906 return true;
3907 }
3908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003910 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 if (r.stopped) {
3912 r.activity.performRestart();
3913 r.stopped = false;
3914 }
3915 }
3916
Romain Guy65b345f2011-07-27 18:51:50 -07003917 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003918 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003919
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003920 if (r == null) {
3921 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3922 return;
3923 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003926 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003927 } else if (show && r.stopped) {
3928 // If we are getting ready to gc after going to the background, well
3929 // we are back active so skip it.
3930 unscheduleGcIdler();
3931
3932 r.activity.performRestart();
3933 r.stopped = false;
3934 }
3935 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003936 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 TAG, "Handle window " + r + " visibility: " + show);
3938 updateVisibility(r, show);
3939 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003940 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941 }
3942
Romain Guy65b345f2011-07-27 18:51:50 -07003943 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003944 ActivityClientRecord r = mActivities.get(token);
3945
3946 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003947 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003948 return;
3949 }
3950
3951 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003952 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003953 try {
3954 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003955 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003956 } catch (Exception e) {
3957 if (!mInstrumentation.onException(r.activity, e)) {
3958 throw new RuntimeException(
3959 "Unable to stop activity "
3960 + r.intent.getComponent().toShortString()
3961 + ": " + e.toString(), e);
3962 }
3963 }
3964 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003965 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3966 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003967 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003968
3969 // Make sure any pending writes are now committed.
3970 if (!r.isPreHoneycomb()) {
3971 QueuedWork.waitToFinish();
3972 }
3973
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003974 // Tell activity manager we slept.
3975 try {
3976 ActivityManagerNative.getDefault().activitySlept(r.token);
3977 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003978 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003979 }
3980 } else {
3981 if (r.stopped && r.activity.mVisibleFromServer) {
3982 r.activity.performRestart();
3983 r.stopped = false;
3984 }
3985 }
3986 }
3987
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003988 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003989 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003990 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003991 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003992 onCoreSettingsChange();
3993 }
3994
3995 private void onCoreSettingsChange() {
3996 boolean debugViewAttributes =
3997 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3998 if (debugViewAttributes != View.mDebugViewAttributes) {
3999 View.mDebugViewAttributes = debugViewAttributes;
4000
4001 // request all activities to relaunch for the changes to take place
4002 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004003 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
4004 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07004005 }
4006 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004007 }
4008
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004009 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4010 LoadedApk apk = peekPackageInfo(data.pkg, false);
4011 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004012 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004013 }
4014 apk = peekPackageInfo(data.pkg, true);
4015 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004016 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004017 }
4018 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004019 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004020 }
4021
Romain Guy65b345f2011-07-27 18:51:50 -07004022 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004023 final int N = results.size();
4024 for (int i=0; i<N; i++) {
4025 ResultInfo ri = results.get(i);
4026 try {
4027 if (ri.mData != null) {
4028 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004029 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004031 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004032 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 r.activity.dispatchActivityResult(ri.mResultWho,
4034 ri.mRequestCode, ri.mResultCode, ri.mData);
4035 } catch (Exception e) {
4036 if (!mInstrumentation.onException(r.activity, e)) {
4037 throw new RuntimeException(
4038 "Failure delivering result " + ri + " to activity "
4039 + r.intent.getComponent().toShortString()
4040 + ": " + e.toString(), e);
4041 }
4042 }
4043 }
4044 }
4045
Romain Guy65b345f2011-07-27 18:51:50 -07004046 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004047 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004048 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 if (r != null) {
4050 final boolean resumed = !r.paused;
4051 if (!r.activity.mFinished && r.activity.mDecor != null
4052 && r.hideForNow && resumed) {
4053 // We had hidden the activity because it started another
4054 // one... we have gotten a result back and we are not
4055 // paused, so make sure our window is visible.
4056 updateVisibility(r, true);
4057 }
4058 if (resumed) {
4059 try {
4060 // Now we are idle.
4061 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004062 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004063 mInstrumentation.callActivityOnPause(r.activity);
4064 if (!r.activity.mCalled) {
4065 throw new SuperNotCalledException(
4066 "Activity " + r.intent.getComponent().toShortString()
4067 + " did not call through to super.onPause()");
4068 }
4069 } catch (SuperNotCalledException e) {
4070 throw e;
4071 } catch (Exception e) {
4072 if (!mInstrumentation.onException(r.activity, e)) {
4073 throw new RuntimeException(
4074 "Unable to pause activity "
4075 + r.intent.getComponent().toShortString()
4076 + ": " + e.toString(), e);
4077 }
4078 }
4079 }
4080 deliverResults(r, res.results);
4081 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004082 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004083 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 }
4085 }
4086 }
4087
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004088 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004089 return performDestroyActivity(token, finishing, 0, false);
4090 }
4091
Romain Guy65b345f2011-07-27 18:51:50 -07004092 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004094 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004095 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004096 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004098 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004099 r.activity.mConfigChangeFlags |= configChanges;
4100 if (finishing) {
4101 r.activity.mFinished = true;
4102 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004103
4104 performPauseActivityIfNeeded(r, "destroy");
4105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 if (!r.stopped) {
4107 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004108 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004109 } catch (SuperNotCalledException e) {
4110 throw e;
4111 } catch (Exception e) {
4112 if (!mInstrumentation.onException(r.activity, e)) {
4113 throw new RuntimeException(
4114 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004115 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 + ": " + e.toString(), e);
4117 }
4118 }
4119 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004120 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4121 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004122 }
4123 if (getNonConfigInstance) {
4124 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004125 r.lastNonConfigurationInstances
4126 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 } catch (Exception e) {
4128 if (!mInstrumentation.onException(r.activity, e)) {
4129 throw new RuntimeException(
4130 "Unable to retain activity "
4131 + r.intent.getComponent().toShortString()
4132 + ": " + e.toString(), e);
4133 }
4134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
4136 try {
4137 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004138 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 if (!r.activity.mCalled) {
4140 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004141 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 " did not call through to super.onDestroy()");
4143 }
4144 if (r.window != null) {
4145 r.window.closeAllPanels();
4146 }
4147 } catch (SuperNotCalledException e) {
4148 throw e;
4149 } catch (Exception e) {
4150 if (!mInstrumentation.onException(r.activity, e)) {
4151 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004152 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4153 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004154 }
4155 }
4156 }
4157 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004158 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 return r;
4160 }
4161
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004162 private static String safeToComponentShortString(Intent intent) {
4163 ComponentName component = intent.getComponent();
4164 return component == null ? "[Unknown]" : component.toShortString();
4165 }
4166
Romain Guy65b345f2011-07-27 18:51:50 -07004167 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004169 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004170 configChanges, getNonConfigInstance);
4171 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004172 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004173 WindowManager wm = r.activity.getWindowManager();
4174 View v = r.activity.mDecor;
4175 if (v != null) {
4176 if (r.activity.mVisibleFromServer) {
4177 mNumVisibleActivities--;
4178 }
4179 IBinder wtoken = v.getWindowToken();
4180 if (r.activity.mWindowAdded) {
Robert Carr77bdfb52016-05-02 18:18:31 -07004181 if (r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004182 // Hold off on removing this until the new activity's
4183 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004184 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004185 r.mPendingRemoveWindowManager = wm;
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004186 // We can only keep the part of the view hierarchy that we control,
4187 // everything else must be removed, because it might not be able to
4188 // behave properly when activity is relaunching.
4189 r.window.clearContentView();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004190 } else {
4191 wm.removeViewImmediate(v);
4192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004194 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004195 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004196 r.activity.getClass().getName(), "Activity");
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004197 } else if (r.mPendingRemoveWindow != null) {
4198 // We're preserving only one window, others should be closed so app views
4199 // will be detached before the final tear down. It should be done now because
4200 // some components (e.g. WebView) rely on detach callbacks to perform receiver
4201 // unregister and other cleanup.
4202 WindowManagerGlobal.getInstance().closeAllExceptView(token, v,
4203 r.activity.getClass().getName(), "Activity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 }
4205 r.activity.mDecor = null;
4206 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004207 if (r.mPendingRemoveWindow == null) {
4208 // If we are delaying the removal of the activity window, then
4209 // we can't clean up all windows here. Note that we can't do
4210 // so later either, which means any windows that aren't closed
4211 // by the app will leak. Well we try to warning them a lot
4212 // about leaking windows, because that is a bug, so if they are
4213 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004214 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004215 r.activity.getClass().getName(), "Activity");
4216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004217
4218 // Mocked out contexts won't be participating in the normal
4219 // process lifecycle, but if we're running with a proper
4220 // ApplicationContext we need to have it tear down things
4221 // cleanly.
4222 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004223 if (c instanceof ContextImpl) {
4224 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225 r.activity.getClass().getName(), "Activity");
4226 }
4227 }
4228 if (finishing) {
4229 try {
4230 ActivityManagerNative.getDefault().activityDestroyed(token);
4231 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004232 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004233 }
4234 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004235 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 }
4237
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004238 /**
4239 * @param preserveWindow Whether the activity should try to reuse the window it created,
4240 * including the decor view after the relaunch.
4241 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004242 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004243 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004244 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004245 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004246 ActivityClientRecord target = null;
4247
Craig Mautner88c05892013-06-28 09:47:45 -07004248 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004249 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4250 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004251 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004252 if (r.token == token) {
4253 target = r;
4254 if (pendingResults != null) {
4255 if (r.pendingResults != null) {
4256 r.pendingResults.addAll(pendingResults);
4257 } else {
4258 r.pendingResults = pendingResults;
4259 }
4260 }
4261 if (pendingNewIntents != null) {
4262 if (r.pendingIntents != null) {
4263 r.pendingIntents.addAll(pendingNewIntents);
4264 } else {
4265 r.pendingIntents = pendingNewIntents;
4266 }
4267 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004268
4269 // For each relaunch request, activity manager expects an answer
4270 if (!r.onlyLocalRequest && fromServer) {
4271 try {
4272 ActivityManagerNative.getDefault().activityRelaunched(token);
4273 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004274 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004275 }
4276 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004277 break;
4278 }
4279 }
4280
4281 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004282 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4283 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004284 target = new ActivityClientRecord();
4285 target.token = token;
4286 target.pendingResults = pendingResults;
4287 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004288 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004289 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004290 final ActivityClientRecord existing = mActivities.get(token);
4291 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004292 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004293 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4294 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004295 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004296 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004297 }
4298 target.onlyLocalRequest = true;
4299 }
4300 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004301 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004302 }
4303
4304 if (fromServer) {
4305 target.startsNotResumed = notResumed;
4306 target.onlyLocalRequest = false;
4307 }
4308 if (config != null) {
4309 target.createdConfig = config;
4310 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004311 if (overrideConfig != null) {
4312 target.overrideConfig = overrideConfig;
4313 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004314 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004315 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004316 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004317 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4318 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004319 }
4320
Romain Guy65b345f2011-07-27 18:51:50 -07004321 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004322 // If we are getting ready to gc after going to the background, well
4323 // we are back active so skip it.
4324 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004325 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326
4327 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004328 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004330 // First: make sure we have the most recent configuration and most
4331 // recent version of the activity, or skip it if some previous call
4332 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004333 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004334 int N = mRelaunchingActivities.size();
4335 IBinder token = tmp.token;
4336 tmp = null;
4337 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004338 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339 if (r.token == token) {
4340 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004341 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 mRelaunchingActivities.remove(i);
4343 i--;
4344 N--;
4345 }
4346 }
Bob Leee5408332009-09-04 18:31:17 -07004347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004349 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004350 return;
4351 }
Bob Leee5408332009-09-04 18:31:17 -07004352
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004353 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4354 + tmp.token + " with configChanges=0x"
4355 + Integer.toHexString(configChanges));
4356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 if (mPendingConfiguration != null) {
4358 changedConfig = mPendingConfiguration;
4359 mPendingConfiguration = null;
4360 }
4361 }
Bob Leee5408332009-09-04 18:31:17 -07004362
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004363 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4364 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4365 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4366 } else {
4367 tmp.lastProcessedSeq = tmp.relaunchSeq;
4368 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004369 if (tmp.createdConfig != null) {
4370 // If the activity manager is passing us its current config,
4371 // assume that is really what we want regardless of what we
4372 // may have pending.
4373 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004374 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4375 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4376 if (changedConfig == null
4377 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4378 changedConfig = tmp.createdConfig;
4379 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004380 }
4381 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004382
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004383 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004384 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 // If there was a pending configuration change, execute it first.
4387 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004388 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004389 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004390 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 }
Bob Leee5408332009-09-04 18:31:17 -07004392
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004393 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004394 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004396 if (!tmp.onlyLocalRequest) {
4397 try {
4398 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4399 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004400 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004401 }
4402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 return;
4404 }
Bob Leee5408332009-09-04 18:31:17 -07004405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004407 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004408 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004409 r.lastProcessedSeq = tmp.lastProcessedSeq;
4410 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004411 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004412
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004413 r.activity.mChangingConfigurations = true;
4414
Robert Carr23fa16b2016-01-13 13:19:58 -08004415 // If we are preserving the main window across relaunches we would also like to preserve
4416 // the children. However the client side view system does not support preserving
4417 // the child views so we notify the window manager to expect these windows to
4418 // be replaced and defer requests to destroy or hide them. This way we can achieve
4419 // visual continuity. It's important that we do this here prior to pause and destroy
4420 // as that is when we may hide or remove the child views.
Robert Carr77bdfb52016-05-02 18:18:31 -07004421 //
4422 // There is another scenario, if we have decided locally to relaunch the app from a
4423 // call to recreate, then none of the windows will be prepared for replacement or
4424 // preserved by the server, so we want to notify it that we are preparing to replace
4425 // everything
Robert Carr23fa16b2016-01-13 13:19:58 -08004426 try {
Robert Carr77bdfb52016-05-02 18:18:31 -07004427 if (r.mPreserveWindow || r.onlyLocalRequest) {
4428 WindowManagerGlobal.getWindowSession().prepareToReplaceWindows(
4429 r.token, !r.onlyLocalRequest);
Robert Carr23fa16b2016-01-13 13:19:58 -08004430 }
4431 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004432 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004433 }
4434
Dianne Hackborne2b04802010-12-09 09:24:55 -08004435 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004437 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004438 }
4439 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004440 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 }
Bob Leee5408332009-09-04 18:31:17 -07004442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004443 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 r.activity = null;
4446 r.window = null;
4447 r.hideForNow = false;
4448 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004449 // Merge any pending results and pending intents; don't just replace them
4450 if (tmp.pendingResults != null) {
4451 if (r.pendingResults == null) {
4452 r.pendingResults = tmp.pendingResults;
4453 } else {
4454 r.pendingResults.addAll(tmp.pendingResults);
4455 }
4456 }
4457 if (tmp.pendingIntents != null) {
4458 if (r.pendingIntents == null) {
4459 r.pendingIntents = tmp.pendingIntents;
4460 } else {
4461 r.pendingIntents.addAll(tmp.pendingIntents);
4462 }
4463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004465 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004466
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004467 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004468
4469 if (!tmp.onlyLocalRequest) {
4470 try {
4471 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004472 if (r.window != null) {
4473 r.window.reportActivityRelaunched();
4474 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004475 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004476 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004477 }
4478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 }
4480
Craig Mautnera0026042014-04-23 11:45:37 -07004481 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4482 r.state = new Bundle();
4483 r.state.setAllowFds(false);
4484 if (r.isPersistable()) {
4485 r.persistentState = new PersistableBundle();
4486 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4487 r.persistentState);
4488 } else {
4489 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4490 }
4491 }
4492
Dianne Hackborn73c14162012-09-19 15:45:06 -07004493 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004495 ArrayList<ComponentCallbacks2> callbacks
4496 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004497
Craig Mautner88c05892013-06-28 09:47:45 -07004498 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004499 final int NAPP = mAllApplications.size();
4500 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004501 callbacks.add(mAllApplications.get(i));
4502 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004503 final int NACT = mActivities.size();
4504 for (int i=0; i<NACT; i++) {
4505 ActivityClientRecord ar = mActivities.valueAt(i);
4506 Activity a = ar.activity;
4507 if (a != null) {
4508 Configuration thisConfig = applyConfigCompatMainThread(
4509 mCurDefaultDisplayDpi, newConfig,
4510 ar.packageInfo.getCompatibilityInfo());
4511 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4512 // If the activity is currently resumed, its configuration
4513 // needs to change right now.
4514 callbacks.add(a);
4515 } else if (thisConfig != null) {
4516 // Otherwise, we will tell it about the change
4517 // the next time it is resumed or shown. Note that
4518 // the activity manager may, before then, decide the
4519 // activity needs to be destroyed to handle its new
4520 // configuration.
4521 if (DEBUG_CONFIGURATION) {
4522 Slog.v(TAG, "Setting activity "
4523 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004524 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004525 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 }
4527 }
4528 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004529 final int NSVC = mServices.size();
4530 for (int i=0; i<NSVC; i++) {
4531 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
4533 }
4534 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004535 final int NPRV = mLocalProviders.size();
4536 for (int i=0; i<NPRV; i++) {
4537 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 }
4539 }
Bob Leee5408332009-09-04 18:31:17 -07004540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 return callbacks;
4542 }
Bob Leee5408332009-09-04 18:31:17 -07004543
Adam Lesinski082614c2016-03-04 14:33:47 -08004544 /**
4545 * Updates the configuration for an Activity. The ActivityClientRecord's
4546 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4547 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4548 * the updated Configuration.
4549 * @param r ActivityClientRecord representing the Activity.
4550 * @param newBaseConfig The new configuration to use. This may be augmented with
4551 * {@link ActivityClientRecord#overrideConfig}.
4552 * @param reportToActivity true if the change should be reported to the Activity's callback.
4553 */
4554 private void performConfigurationChangedForActivity(ActivityClientRecord r,
4555 Configuration newBaseConfig,
4556 boolean reportToActivity) {
4557 r.tmpConfig.setTo(newBaseConfig);
4558 if (r.overrideConfig != null) {
4559 r.tmpConfig.updateFrom(r.overrideConfig);
4560 }
4561 performConfigurationChanged(r.activity, r.token, r.tmpConfig, r.overrideConfig,
4562 reportToActivity);
4563 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
4564 }
4565
4566 /**
4567 * Decides whether to update an Activity's configuration and whether to tell the
4568 * Activity/Component about it.
4569 * @param cb The component callback to notify of configuration change.
4570 * @param activityToken The Activity binder token for which this configuration change happened.
4571 * If the change is global, this is null.
4572 * @param newConfig The new configuration.
4573 * @param overrideConfig The override config that differentiates the Activity's configuration
4574 * from the base global configuration.
4575 * @param reportToActivity Notify the Activity of the change.
4576 */
4577 private void performConfigurationChanged(ComponentCallbacks2 cb,
4578 IBinder activityToken,
4579 Configuration newConfig,
4580 Configuration overrideConfig,
4581 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004583 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 // we check the runtime type and act accordingly.
4585 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4586 if (activity != null) {
4587 activity.mCalled = false;
4588 }
Bob Leee5408332009-09-04 18:31:17 -07004589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 boolean shouldChangeConfig = false;
4591 if ((activity == null) || (activity.mCurrentConfig == null)) {
4592 shouldChangeConfig = true;
4593 } else {
Bob Leee5408332009-09-04 18:31:17 -07004594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 // If the new config is the same as the config this Activity
4596 // is already running with then don't bother calling
4597 // onConfigurationChanged
Adam Lesinski082614c2016-03-04 14:33:47 -08004598 int diff = activity.mCurrentConfig.diff(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 if (diff != 0) {
Jorim Jaggi1793d472016-04-27 20:59:40 -07004600 shouldChangeConfig = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 }
4602 }
Bob Leee5408332009-09-04 18:31:17 -07004603
Adam Lesinski082614c2016-03-04 14:33:47 -08004604 if (DEBUG_CONFIGURATION) {
4605 Slog.v(TAG, "Config callback " + cb + ": shouldChangeConfig=" + shouldChangeConfig);
4606 }
4607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608 if (shouldChangeConfig) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004609 if (activityToken != null) {
4610 // We only update an Activity's configuration if this is not a global
4611 // configuration change. This must also be done before the callback,
4612 // or else we violate the contract that the new resources are available
4613 // in {@link ComponentCallbacks2#onConfigurationChanged(Configuration)}.
4614 mResourcesManager.updateResourcesForActivity(activityToken, overrideConfig);
4615 }
4616
Filip Gruszczynskica664812015-12-04 12:43:36 -08004617 if (reportToActivity) {
Adam Lesinski3ad1b482016-04-01 16:41:41 -07004618 Configuration configToReport = newConfig;
4619
4620 if (cb instanceof ContextThemeWrapper) {
4621 // ContextThemeWrappers may override the configuration for that context.
4622 // We must check and apply any overrides defined.
4623 ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
4624 final Configuration localOverrideConfig =
4625 contextThemeWrapper.getOverrideConfiguration();
4626 if (localOverrideConfig != null) {
4627 configToReport = new Configuration(newConfig);
4628 configToReport.updateFrom(localOverrideConfig);
4629 }
4630 }
4631
4632 cb.onConfigurationChanged(configToReport);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004633 }
Bob Leee5408332009-09-04 18:31:17 -07004634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004636 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004637 throw new SuperNotCalledException(
4638 "Activity " + activity.getLocalClassName() +
Adam Lesinski082614c2016-03-04 14:33:47 -08004639 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 }
4641 activity.mConfigChangeFlags = 0;
Adam Lesinski082614c2016-03-04 14:33:47 -08004642 activity.mCurrentConfig = new Configuration(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 }
4644 }
4645 }
4646
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004647 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004648 synchronized (mResourcesManager) {
4649 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004650 }
4651 }
4652
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004653 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004654 Configuration config = mConfiguration;
4655 if (mCompatConfiguration == null) {
4656 mCompatConfiguration = new Configuration();
4657 }
4658 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004659 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4660 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004661 config = mCompatConfiguration;
4662 }
4663 return config;
4664 }
4665
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004666 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004667
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004668 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004669
Craig Mautner88c05892013-06-28 09:47:45 -07004670 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004671 if (mPendingConfiguration != null) {
4672 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4673 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004674 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004675 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004676 }
4677 mPendingConfiguration = null;
4678 }
4679
4680 if (config == null) {
4681 return;
4682 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004683
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004684 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004685 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004686
4687 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4688
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004689 if (mConfiguration == null) {
4690 mConfiguration = new Configuration();
4691 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004692 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004693 return;
4694 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004695
4696 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004697 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004698
4699 final Theme systemTheme = getSystemContext().getTheme();
4700 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4701 systemTheme.rebase();
4702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004704
4705 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4706
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004707 freeTextLayoutCachesIfNeeded(configDiff);
4708
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004709 if (callbacks != null) {
4710 final int N = callbacks.size();
4711 for (int i=0; i<N; i++) {
Andrii Kulian701214b2016-04-07 09:36:33 -07004712 ComponentCallbacks2 cb = callbacks.get(i);
4713 if (cb instanceof Activity) {
4714 // If callback is an Activity - call corresponding method to consider override
4715 // config and avoid onConfigurationChanged if it hasn't changed.
4716 Activity a = (Activity) cb;
4717 performConfigurationChangedForActivity(mActivities.get(a.getActivityToken()),
4718 config, REPORT_TO_ACTIVITY);
4719 } else {
4720 performConfigurationChanged(cb, null, config, null, REPORT_TO_ACTIVITY);
4721 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 }
4724 }
4725
Romain Guy46bfc482013-08-16 18:38:29 -07004726 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004727 if (configDiff != 0) {
4728 // Ask text layout engine to free its caches if there is a locale change
4729 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4730 if (hasLocaleConfigChange) {
4731 Canvas.freeTextLayoutCaches();
4732 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4733 }
4734 }
4735 }
4736
Filip Gruszczynskica664812015-12-04 12:43:36 -08004737 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4738 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004739 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 if (r == null || r.activity == null) {
4741 return;
4742 }
Bob Leee5408332009-09-04 18:31:17 -07004743
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004744 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004745 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004746
Adam Lesinski082614c2016-03-04 14:33:47 -08004747 r.overrideConfig = data.overrideConfig;
4748 performConfigurationChangedForActivity(r, mCompatConfiguration, reportToActivity);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004749 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004750 }
4751
Jeff Hao1b012d32014-08-20 10:35:34 -07004752 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004753 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004754 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004755 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004756 default:
4757 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004758 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004759 break;
4760 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004761 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004762 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004763 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004764 } finally {
4765 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004766 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004767 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004768 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004769 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004770 }
4771 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004772 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004773 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004774 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004775 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004776 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004777 }
4778 }
Bob Leee5408332009-09-04 18:31:17 -07004779
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004780 /**
4781 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4782 * so that profiler data won't be lost.
4783 *
4784 * @hide
4785 */
4786 public void stopProfiling() {
4787 mProfiler.stopProfiling();
4788 }
4789
Romain Guya998dff2012-03-23 18:58:36 -07004790 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004791 if (managed) {
4792 try {
4793 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4794 } catch (IOException e) {
4795 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4796 + " -- can the process access this path?");
4797 } finally {
4798 try {
4799 dhd.fd.close();
4800 } catch (IOException e) {
4801 Slog.w(TAG, "Failure closing profile fd", e);
4802 }
4803 }
4804 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004805 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004806 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004807 try {
4808 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4809 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004810 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004811 }
Andy McFadden824c5102010-07-09 16:26:57 -07004812 }
4813
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004814 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4815 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08004816 switch (cmd) {
4817 case IApplicationThread.PACKAGE_REMOVED:
4818 case IApplicationThread.PACKAGE_REMOVED_DONT_KILL:
4819 {
4820 final boolean killApp = cmd == IApplicationThread.PACKAGE_REMOVED;
4821 if (packages == null) {
4822 break;
4823 }
4824 synchronized (mResourcesManager) {
4825 for (int i = packages.length - 1; i >= 0; i--) {
4826 if (!hasPkgInfo) {
4827 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4828 if (ref != null && ref.get() != null) {
4829 hasPkgInfo = true;
4830 } else {
4831 ref = mResourcePackages.get(packages[i]);
4832 if (ref != null && ref.get() != null) {
4833 hasPkgInfo = true;
4834 }
4835 }
4836 }
4837 if (killApp) {
4838 mPackages.remove(packages[i]);
4839 mResourcePackages.remove(packages[i]);
4840 }
4841 }
4842 }
4843 break;
4844 }
4845 case IApplicationThread.PACKAGE_REPLACED:
4846 {
4847 if (packages == null) {
4848 break;
4849 }
4850 synchronized (mResourcesManager) {
4851 for (int i = packages.length - 1; i >= 0; i--) {
4852 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4853 LoadedApk pkgInfo = ref != null ? ref.get() : null;
4854 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004855 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004856 } else {
4857 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08004858 pkgInfo = ref != null ? ref.get() : null;
4859 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08004860 hasPkgInfo = true;
4861 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004862 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004863 // If the package is being replaced, yet it still has a valid
4864 // LoadedApk object, the package was updated with _DONT_KILL.
4865 // Adjust it's internal references to the application info and
4866 // resources.
4867 if (pkgInfo != null) {
4868 try {
4869 final String packageName = packages[i];
4870 final ApplicationInfo aInfo =
4871 sPackageManager.getApplicationInfo(
4872 packageName,
4873 0 /*flags*/,
4874 UserHandle.myUserId());
4875
4876 if (mActivities.size() > 0) {
4877 for (ActivityClientRecord ar : mActivities.values()) {
4878 if (ar.activityInfo.applicationInfo.packageName
4879 .equals(packageName)) {
4880 ar.activityInfo.applicationInfo = aInfo;
4881 ar.packageInfo = pkgInfo;
4882 }
4883 }
4884 }
4885 final List<String> oldPaths =
4886 sPackageManager.getPreviousCodePaths(packageName);
4887 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
4888 } catch (RemoteException e) {
4889 }
4890 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004891 }
4892 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004893 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004894 }
4895 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004896 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004897 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004900 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 final int N = callbacks.size();
4903 for (int i=0; i<N; i++) {
4904 callbacks.get(i).onLowMemory();
4905 }
4906
Chris Tatece229052009-03-25 16:44:52 -07004907 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4908 if (Process.myUid() != Process.SYSTEM_UID) {
4909 int sqliteReleased = SQLiteDatabase.releaseMemory();
4910 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4911 }
Bob Leee5408332009-09-04 18:31:17 -07004912
Mike Reedcaf0df12009-04-27 14:32:05 -04004913 // Ask graphics to free up as much as possible (font/image caches)
4914 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004915
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004916 // Ask text layout engine to free also as much as possible
4917 Canvas.freeTextLayoutCaches();
4918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004919 BinderInternal.forceGc("mem");
4920 }
4921
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004922 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004923 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004924
Dianne Hackborn73c14162012-09-19 15:45:06 -07004925 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004926
4927 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004928 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004929 callbacks.get(i).onTrimMemory(level);
4930 }
Romain Guy19f86e82012-04-23 15:19:07 -07004931
John Reckf47a5942014-06-30 16:20:04 -07004932 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004933 }
4934
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004935 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004936 if (Process.isIsolated()) {
4937 // Isolated processes aren't going to do UI.
4938 return;
4939 }
Romain Guya9582652011-11-10 14:20:10 -08004940 try {
4941 int uid = Process.myUid();
4942 String[] packages = getPackageManager().getPackagesForUid(uid);
4943
Shuo Gao7c69a662016-03-01 13:32:03 +08004944 if (packages != null) {
John Reck51aaf902015-12-02 15:08:07 -08004945 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004946 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004947 }
4948 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004949 throw e.rethrowFromSystemServer();
Romain Guya9582652011-11-10 14:20:10 -08004950 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004951 }
4952
4953 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05004954 final int densityDpi = mCurDefaultDisplayDpi;
4955 if (!mDensityCompatMode
4956 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
4957 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
4958 DisplayMetrics.DENSITY_DEVICE = densityDpi;
4959 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07004960 }
4961 }
4962
Todd Kennedye713efc2016-05-10 13:45:40 -07004963 /**
4964 * Returns the correct library directory for the current ABI.
4965 * <p>
4966 * If we're dealing with a multi-arch application that has both 32 and 64 bit shared
4967 * libraries, we might need to choose the secondary depending on what the current
4968 * runtime's instruction set is.
4969 */
4970 private String getInstrumentationLibrary(ApplicationInfo appInfo, InstrumentationInfo insInfo) {
4971 if (appInfo.primaryCpuAbi != null && appInfo.secondaryCpuAbi != null) {
4972 // Get the instruction set supported by the secondary ABI. In the presence
4973 // of a native bridge this might be different than the one secondary ABI used.
4974 String secondaryIsa =
4975 VMRuntime.getInstructionSet(appInfo.secondaryCpuAbi);
4976 final String secondaryDexCodeIsa =
4977 SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
4978 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
4979
4980 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
4981 if (runtimeIsa.equals(secondaryIsa)) {
4982 return insInfo.secondaryNativeLibraryDir;
4983 }
4984 }
4985 return insInfo.nativeLibraryDir;
4986 }
4987
Romain Guy65b345f2011-07-27 18:51:50 -07004988 private void handleBindApplication(AppBindData data) {
Calin Juravle8f5770d2016-04-12 14:12:04 +01004989 // Register the UI Thread as a sensitive thread to the runtime.
4990 VMRuntime.registerSensitiveThread();
Man Caocfa78b22015-06-11 20:14:34 -07004991 if (data.trackAllocation) {
4992 DdmVmInternal.enableRecentAllocations(true);
4993 }
4994
Dianne Hackbornd98885c2016-03-01 17:13:03 -08004995 // Note when this process has started.
4996 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
4997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 mBoundApplication = data;
4999 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005000 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005002 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005003 if (data.initProfilerInfo != null) {
5004 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5005 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5006 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5007 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
5008 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005010 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005011 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005012 android.ddm.DdmHandleAppName.setAppName(data.processName,
5013 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005014
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005015 if (data.persistent) {
5016 // Persistent processes on low-memory devices do not get to
5017 // use hardware accelerated drawing, since this can add too much
5018 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005019 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005020 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005021 }
5022 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005023
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005024 if (mProfiler.profileFd != null) {
5025 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005026 }
5027
Joe Onoratod630f102011-03-17 18:42:26 -07005028 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5029 // implementation to use the pool executor. Normally, we use the
5030 // serialized executor as the default. This has to happen in the
5031 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005032 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005033 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5034 }
5035
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005036 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5037
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 /*
5039 * Before spawning a new process, reset the time zone to be the system time zone.
5040 * This needs to be done because the system time zone could have changed after the
5041 * the spawning of this process. Without doing this this process would have the incorrect
5042 * system time zone.
5043 */
5044 TimeZone.setDefault(null);
5045
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005046 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047
Dianne Hackborndde331c2012-08-03 14:01:57 -07005048 /**
5049 * Switch this process to density compatibility mode if needed.
5050 */
5051 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5052 == 0) {
5053 mDensityCompatMode = true;
5054 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5055 }
5056 updateDefaultDensity();
5057
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005058 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
5059 DateFormat.set24HourTimePref(is24Hr);
5060
Jon Miranda836c0a82014-08-11 12:32:26 -07005061 View.mDebugViewAttributes =
5062 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5063
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005064 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005065 * For system applications on userdebug/eng builds, log stack
5066 * traces of disk and network access to dropbox for analysis.
5067 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005068 if ((data.appInfo.flags &
5069 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005070 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5071 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005072 }
5073
5074 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005075 * For apps targetting Honeycomb or later, we don't allow network usage
5076 * on the main event loop / UI thread. This is what ultimately throws
5077 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005078 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005079 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005080 StrictMode.enableDeathOnNetwork();
5081 }
5082
Jeff Sharkey344744b2016-01-28 19:03:30 -07005083 /**
5084 * For apps targetting N or later, we don't allow file:// Uri exposure.
5085 * This is what ultimately throws {@link FileUriExposedException}.
5086 */
5087 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005088 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005089 }
5090
Alex Klyubinf9034cc2015-02-12 11:43:09 -08005091 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
5092 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
5093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5095 // XXX should have option to change the port.
5096 Debug.changeDebugPort(8100);
5097 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005098 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005099 + " is waiting for the debugger on port 8100...");
5100
5101 IActivityManager mgr = ActivityManagerNative.getDefault();
5102 try {
5103 mgr.showWaitingForDebugger(mAppThread, true);
5104 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005105 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005106 }
5107
5108 Debug.waitForDebugger();
5109
5110 try {
5111 mgr.showWaitingForDebugger(mAppThread, false);
5112 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005113 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 }
5115
5116 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005117 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 + " can be debugged on port 8100...");
5119 }
5120 }
5121
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005122 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005123 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5124 Trace.setAppTracingAllowed(isAppDebuggable);
5125 if (isAppDebuggable && data.enableBinderTracking) {
5126 Binder.enableTracing();
5127 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005128
Robert Greenwalt434203a2010-10-11 16:00:27 -07005129 /**
5130 * Initialize the default http proxy in this process for the reasons we set the time zone.
5131 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04005132 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005133 if (b != null) {
5134 // In pre-boot mode (doing initial launch to collect password), not
5135 // all system is up. This includes the connectivity service, so don't
5136 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005137 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005138 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005139 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005140 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005141 } catch (RemoteException e) {
5142 throw e.rethrowFromSystemServer();
5143 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005144 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07005145
Alan Viverette2107d692015-09-03 14:55:27 -04005146 // Instrumentation info affects the class loader, so load it before
5147 // setting up the app context.
5148 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005150 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005151 ii = new ApplicationPackageManager(null, getPackageManager())
5152 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005154 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005155 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156 }
5157
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005158 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005160 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
Todd Kennedye713efc2016-05-10 13:45:40 -07005161 mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005163 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5164 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005165 } else {
5166 ii = null;
5167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168
Alan Viverette2107d692015-09-03 14:55:27 -04005169 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Seigo Nonakac14dd782016-03-30 23:09:16 +09005170 synchronized (mResourcesManager) {
5171 /*
5172 * Initialize the default locales in this process for the reasons we set the time zone.
5173 *
5174 * We do this through ResourcesManager, since we need to do locale negotiation.
5175 */
5176 mResourcesManager.setDefaultLocalesLocked(data.config.getLocales());
5177
5178 /*
5179 * Update the system configuration since its preloaded and might not
5180 * reflect configuration changes. The configuration object passed
5181 * in AppBindData can be safely assumed to be up to date
5182 */
5183 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5184 mCurDefaultDisplayDpi = data.config.densityDpi;
5185
5186 // This calls mResourcesManager so keep it within the synchronized block.
5187 applyCompatConfiguration(mCurDefaultDisplayDpi);
5188 }
5189
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005190 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005191 // This cache location probably points at credential-encrypted
5192 // storage which may not be accessible yet; assign it anyway instead
5193 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005194 final File cacheDir = appContext.getCacheDir();
5195 if (cacheDir != null) {
5196 // Provide a usable directory for temporary files
5197 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5198 } else {
5199 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5200 + "due to missing cache directory");
5201 }
Alan Viverette346296b2015-09-01 13:08:05 -04005202
Calin Juravle2a727d72016-04-15 19:33:46 +01005203 // Setup a location to store generated/compiled graphics code.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005204 final Context deviceContext = appContext.createDeviceProtectedStorageContext();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005205 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005206 if (codeCacheDir != null) {
5207 setupGraphicsSupport(data.info, codeCacheDir);
5208 } else {
Calin Juravle2a727d72016-04-15 19:33:46 +01005209 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005210 }
Michael Lentine2ba303f2016-02-01 20:44:34 -06005211 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005212
Chad Brubaker78d47122015-11-17 22:26:58 -08005213 // Install the Network Security Config Provider. This must happen before the application
5214 // code is loaded to prevent issues with instances of TLS objects being created before
5215 // the provider is installed.
5216 NetworkSecurityConfigProvider.install(appContext);
5217
Alan Viverette2107d692015-09-03 14:55:27 -04005218 // Continue loading instrumentation.
5219 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005220 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005221 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005222 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005223 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005224 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005225 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005226
5227 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005228 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 mInstrumentation = (Instrumentation)
5230 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5231 } catch (Exception e) {
5232 throw new RuntimeException(
5233 "Unable to instantiate instrumentation "
5234 + data.instrumentationName + ": " + e.toString(), e);
5235 }
5236
Alan Viverettebe64eae2015-09-03 14:56:04 -04005237 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5238 mInstrumentation.init(this, instrContext, appContext, component,
5239 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005241 if (mProfiler.profileFile != null && !ii.handleProfiling
5242 && mProfiler.profileFd == null) {
5243 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005244 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 file.getParentFile().mkdirs();
5246 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 } else {
5249 mInstrumentation = new Instrumentation();
5250 }
5251
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005252 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005253 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005254 } else {
5255 // Small heap, clamp to the current growth limit and let the heap release
5256 // pages after the growth limit to the non growth limit capacity. b/18387825
5257 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005258 }
5259
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005260 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005261 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005262 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005263 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005264 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005265 // If the app is being launched for full backup or restore, bring it up in
5266 // a restricted environment with the base application class.
5267 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5268 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005269
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005270 // don't bring up providers in restricted mode; they may depend on the
5271 // app's custom Application class
5272 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005273 if (!ArrayUtils.isEmpty(data.providers)) {
5274 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005275 // For process that contains content providers, we want to
5276 // ensure that the JIT is enabled "at some point".
5277 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5278 }
5279 }
5280
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005281 // Do this after providers, since instrumentation tests generally start their
5282 // test thread at this point, and we don't want that racing.
5283 try {
5284 mInstrumentation.onCreate(data.instrumentationArgs);
5285 }
5286 catch (Exception e) {
5287 throw new RuntimeException(
5288 "Exception thrown in onCreate() of "
5289 + data.instrumentationName + ": " + e.toString(), e);
5290 }
5291
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005292 try {
5293 mInstrumentation.callApplicationOnCreate(app);
5294 } catch (Exception e) {
5295 if (!mInstrumentation.onException(app, e)) {
5296 throw new RuntimeException(
5297 "Unable to create application " + app.getClass().getName()
5298 + ": " + e.toString(), e);
5299 }
5300 }
5301 } finally {
5302 StrictMode.setThreadPolicy(savedPolicy);
5303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 }
5305
5306 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5307 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005308 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5309 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005310 Debug.stopMethodTracing();
5311 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005312 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 // + ", app thr: " + mAppThread);
5314 try {
5315 am.finishInstrumentation(mAppThread, resultCode, results);
5316 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005317 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 }
5319 }
5320
Romain Guy65b345f2011-07-27 18:51:50 -07005321 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005322 Context context, List<ProviderInfo> providers) {
5323 final ArrayList<IActivityManager.ContentProviderHolder> results =
5324 new ArrayList<IActivityManager.ContentProviderHolder>();
5325
Romain Guya998dff2012-03-23 18:58:36 -07005326 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005327 if (DEBUG_PROVIDER) {
5328 StringBuilder buf = new StringBuilder(128);
5329 buf.append("Pub ");
5330 buf.append(cpi.authority);
5331 buf.append(": ");
5332 buf.append(cpi.name);
5333 Log.i(TAG, buf.toString());
5334 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005335 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5336 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5337 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005338 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005339 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005340 }
5341 }
5342
5343 try {
5344 ActivityManagerNative.getDefault().publishContentProviders(
5345 getApplicationThread(), results);
5346 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005347 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005348 }
5349 }
5350
Jeff Sharkey6d515712012-09-20 16:06:08 -07005351 public final IContentProvider acquireProvider(
5352 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005353 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005354 if (provider != null) {
5355 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 }
5357
Wale Ogunwale1d646122015-04-24 14:45:14 -07005358 // There is a possible race here. Another thread may try to acquire
5359 // the same provider at the same time. When this happens, we want to ensure
5360 // that the first one wins.
5361 // Note that we cannot hold the lock while acquiring and installing the
5362 // provider since it might take a long time to run and it could also potentially
5363 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005365 try {
5366 holder = ActivityManagerNative.getDefault().getContentProvider(
5367 getApplicationThread(), auth, userId, stable);
5368 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005369 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005371 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005372 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373 return null;
5374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005376 // Install provider will increment the reference count for us, and break
5377 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005378 holder = installProvider(c, holder, holder.info,
5379 true /*noisy*/, holder.noReleaseNeeded, stable);
5380 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005381 }
5382
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005383 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5384 if (stable) {
5385 prc.stableCount += 1;
5386 if (prc.stableCount == 1) {
5387 // We are acquiring a new stable reference on the provider.
5388 int unstableDelta;
5389 if (prc.removePending) {
5390 // We have a pending remove operation, which is holding the
5391 // last unstable reference. At this point we are converting
5392 // that unstable reference to our new stable reference.
5393 unstableDelta = -1;
5394 // Cancel the removal of the provider.
5395 if (DEBUG_PROVIDER) {
5396 Slog.v(TAG, "incProviderRef: stable "
5397 + "snatched provider from the jaws of death");
5398 }
5399 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005400 // There is a race! It fails to remove the message, which
5401 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005402 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5403 } else {
5404 unstableDelta = 0;
5405 }
5406 try {
5407 if (DEBUG_PROVIDER) {
5408 Slog.v(TAG, "incProviderRef Now stable - "
5409 + prc.holder.info.name + ": unstableDelta="
5410 + unstableDelta);
5411 }
5412 ActivityManagerNative.getDefault().refContentProvider(
5413 prc.holder.connection, 1, unstableDelta);
5414 } catch (RemoteException e) {
5415 //do nothing content provider object is dead any way
5416 }
5417 }
5418 } else {
5419 prc.unstableCount += 1;
5420 if (prc.unstableCount == 1) {
5421 // We are acquiring a new unstable reference on the provider.
5422 if (prc.removePending) {
5423 // Oh look, we actually have a remove pending for the
5424 // provider, which is still holding the last unstable
5425 // reference. We just need to cancel that to take new
5426 // ownership of the reference.
5427 if (DEBUG_PROVIDER) {
5428 Slog.v(TAG, "incProviderRef: unstable "
5429 + "snatched provider from the jaws of death");
5430 }
5431 prc.removePending = false;
5432 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5433 } else {
5434 // First unstable ref, increment our count in the
5435 // activity manager.
5436 try {
5437 if (DEBUG_PROVIDER) {
5438 Slog.v(TAG, "incProviderRef: Now unstable - "
5439 + prc.holder.info.name);
5440 }
5441 ActivityManagerNative.getDefault().refContentProvider(
5442 prc.holder.connection, 0, 1);
5443 } catch (RemoteException e) {
5444 //do nothing content provider object is dead any way
5445 }
5446 }
5447 }
5448 }
5449 }
5450
Jeff Sharkey6d515712012-09-20 16:06:08 -07005451 public final IContentProvider acquireExistingProvider(
5452 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005453 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005454 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005455 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005456 if (pr == null) {
5457 return null;
5458 }
5459
5460 IContentProvider provider = pr.mProvider;
5461 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005462 if (!jBinder.isBinderAlive()) {
5463 // The hosting process of the provider has died; we can't
5464 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005465 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005466 + ": existing object's process dead");
5467 handleUnstableProviderDiedLocked(jBinder, true);
5468 return null;
5469 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005470
5471 // Only increment the ref count if we have one. If we don't then the
5472 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005473 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005474 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005475 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005476 }
5477 return provider;
5478 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005479 }
5480
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005481 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5482 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 return false;
5484 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005487 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005489 if (prc == null) {
5490 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005491 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005492 }
5493
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005494 boolean lastRef = false;
5495 if (stable) {
5496 if (prc.stableCount == 0) {
5497 if (DEBUG_PROVIDER) Slog.v(TAG,
5498 "releaseProvider: stable ref count already 0, how?");
5499 return false;
5500 }
5501 prc.stableCount -= 1;
5502 if (prc.stableCount == 0) {
5503 // What we do at this point depends on whether there are
5504 // any unstable refs left: if there are, we just tell the
5505 // activity manager to decrement its stable count; if there
5506 // aren't, we need to enqueue this provider to be removed,
5507 // and convert to holding a single unstable ref while
5508 // doing so.
5509 lastRef = prc.unstableCount == 0;
5510 try {
5511 if (DEBUG_PROVIDER) {
5512 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5513 + lastRef + " - " + prc.holder.info.name);
5514 }
5515 ActivityManagerNative.getDefault().refContentProvider(
5516 prc.holder.connection, -1, lastRef ? 1 : 0);
5517 } catch (RemoteException e) {
5518 //do nothing content provider object is dead any way
5519 }
5520 }
5521 } else {
5522 if (prc.unstableCount == 0) {
5523 if (DEBUG_PROVIDER) Slog.v(TAG,
5524 "releaseProvider: unstable ref count already 0, how?");
5525 return false;
5526 }
5527 prc.unstableCount -= 1;
5528 if (prc.unstableCount == 0) {
5529 // If this is the last reference, we need to enqueue
5530 // this provider to be removed instead of telling the
5531 // activity manager to remove it at this point.
5532 lastRef = prc.stableCount == 0;
5533 if (!lastRef) {
5534 try {
5535 if (DEBUG_PROVIDER) {
5536 Slog.v(TAG, "releaseProvider: No longer unstable - "
5537 + prc.holder.info.name);
5538 }
5539 ActivityManagerNative.getDefault().refContentProvider(
5540 prc.holder.connection, 0, -1);
5541 } catch (RemoteException e) {
5542 //do nothing content provider object is dead any way
5543 }
5544 }
5545 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005546 }
5547
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005548 if (lastRef) {
5549 if (!prc.removePending) {
5550 // Schedule the actual remove asynchronously, since we don't know the context
5551 // this will be called in.
5552 // TODO: it would be nice to post a delayed message, so
5553 // if we come back and need the same provider quickly
5554 // we will still have it available.
5555 if (DEBUG_PROVIDER) {
5556 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5557 + prc.holder.info.name);
5558 }
5559 prc.removePending = true;
5560 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5561 mH.sendMessage(msg);
5562 } else {
5563 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5564 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005565 }
5566 return true;
5567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 }
5569
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005570 final void completeRemoveProvider(ProviderRefCount prc) {
5571 synchronized (mProviderMap) {
5572 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005573 // There was a race! Some other client managed to acquire
5574 // the provider before the removal was completed.
5575 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005576 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005577 + "provider still in use");
5578 return;
5579 }
5580
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005581 // More complicated race!! Some client managed to acquire the
5582 // provider and release it before the removal was completed.
5583 // Continue the removal, and abort the next remove message.
5584 prc.removePending = false;
5585
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005586 final IBinder jBinder = prc.holder.provider.asBinder();
5587 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5588 if (existingPrc == prc) {
5589 mProviderRefCountMap.remove(jBinder);
5590 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005591
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005592 for (int i=mProviderMap.size()-1; i>=0; i--) {
5593 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005594 IBinder myBinder = pr.mProvider.asBinder();
5595 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005596 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005597 }
5598 }
5599 }
5600
5601 try {
5602 if (DEBUG_PROVIDER) {
5603 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5604 + "removeContentProvider(" + prc.holder.info.name + ")");
5605 }
5606 ActivityManagerNative.getDefault().removeContentProvider(
5607 prc.holder.connection, false);
5608 } catch (RemoteException e) {
5609 //do nothing content provider object is dead any way
5610 }
5611 }
5612
5613 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005614 synchronized (mProviderMap) {
5615 handleUnstableProviderDiedLocked(provider, fromClient);
5616 }
5617 }
5618
5619 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5620 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5621 if (prc != null) {
5622 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5623 + provider + " " + prc.holder.info.name);
5624 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005625 for (int i=mProviderMap.size()-1; i>=0; i--) {
5626 ProviderClientRecord pr = mProviderMap.valueAt(i);
5627 if (pr != null && pr.mProvider.asBinder() == provider) {
5628 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5629 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005630 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005631 }
You Kimbc74de62013-10-01 00:13:26 +09005632
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005633 if (fromClient) {
5634 // We found out about this due to execution in our client
5635 // code. Tell the activity manager about it now, to ensure
5636 // that the next time we go to do anything with the provider
5637 // it knows it is dead (so we don't race with its death
5638 // notification).
5639 try {
5640 ActivityManagerNative.getDefault().unstableProviderDied(
5641 prc.holder.connection);
5642 } catch (RemoteException e) {
5643 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005644 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005645 }
5646 }
5647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005649 final void appNotRespondingViaProvider(IBinder provider) {
5650 synchronized (mProviderMap) {
5651 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5652 if (prc != null) {
5653 try {
5654 ActivityManagerNative.getDefault()
5655 .appNotRespondingViaProvider(prc.holder.connection);
5656 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005657 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005658 }
5659 }
5660 }
5661 }
5662
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005663 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005664 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005665 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005666 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5667
5668 final ProviderClientRecord pcr = new ProviderClientRecord(
5669 auths, provider, localProvider, holder);
5670 for (String auth : auths) {
5671 final ProviderKey key = new ProviderKey(auth, userId);
5672 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005673 if (existing != null) {
5674 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005675 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005676 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005677 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005678 }
5679 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005680 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005681 }
5682
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005683 /**
5684 * Installs the provider.
5685 *
5686 * Providers that are local to the process or that come from the system server
5687 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5688 * Other remote providers are reference counted. The initial reference count
5689 * for all reference counted providers is one. Providers that are not reference
5690 * counted do not have a reference count (at all).
5691 *
5692 * This method detects when a provider has already been installed. When this happens,
5693 * it increments the reference count of the existing provider (if appropriate)
5694 * and returns the existing provider. This can happen due to concurrent
5695 * attempts to acquire the same provider.
5696 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005697 private IActivityManager.ContentProviderHolder installProvider(Context context,
5698 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5699 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005701 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005702 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005703 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005704 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 + info.name);
5706 }
5707 Context c = null;
5708 ApplicationInfo ai = info.applicationInfo;
5709 if (context.getPackageName().equals(ai.packageName)) {
5710 c = context;
5711 } else if (mInitialApplication != null &&
5712 mInitialApplication.getPackageName().equals(ai.packageName)) {
5713 c = mInitialApplication;
5714 } else {
5715 try {
5716 c = context.createPackageContext(ai.packageName,
5717 Context.CONTEXT_INCLUDE_CODE);
5718 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005719 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 }
5721 }
5722 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005723 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 ai.packageName +
5725 " while loading content provider " +
5726 info.name);
5727 return null;
5728 }
5729 try {
5730 final java.lang.ClassLoader cl = c.getClassLoader();
5731 localProvider = (ContentProvider)cl.
5732 loadClass(info.name).newInstance();
5733 provider = localProvider.getIContentProvider();
5734 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005735 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005736 info.name + " from sourceDir " +
5737 info.applicationInfo.sourceDir);
5738 return null;
5739 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005740 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005741 TAG, "Instantiating local provider " + info.name);
5742 // XXX Need to create the correct context for this provider.
5743 localProvider.attachInfo(c, info);
5744 } catch (java.lang.Exception e) {
5745 if (!mInstrumentation.onException(null, e)) {
5746 throw new RuntimeException(
5747 "Unable to get provider " + info.name
5748 + ": " + e.toString(), e);
5749 }
5750 return null;
5751 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005752 } else {
5753 provider = holder.provider;
5754 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005755 + info.name);
5756 }
5757
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005758 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005759
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005760 synchronized (mProviderMap) {
5761 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5762 + " / " + info.name);
5763 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005764 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005765 ComponentName cname = new ComponentName(info.packageName, info.name);
5766 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005767 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005768 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005769 Slog.v(TAG, "installProvider: lost the race, "
5770 + "using existing local provider");
5771 }
5772 provider = pr.mProvider;
5773 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005774 holder = new IActivityManager.ContentProviderHolder(info);
5775 holder.provider = provider;
5776 holder.noReleaseNeeded = true;
5777 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005778 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005779 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005780 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005781 retHolder = pr.mHolder;
5782 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005783 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5784 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005785 if (DEBUG_PROVIDER) {
5786 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005787 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005788 // We need to transfer our new reference to the existing
5789 // ref count, releasing the old one... but only if
5790 // release is needed (that is, it is not running in the
5791 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005792 if (!noReleaseNeeded) {
5793 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005794 try {
5795 ActivityManagerNative.getDefault().removeContentProvider(
5796 holder.connection, stable);
5797 } catch (RemoteException e) {
5798 //do nothing content provider object is dead any way
5799 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005800 }
5801 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005802 ProviderClientRecord client = installProviderAuthoritiesLocked(
5803 provider, localProvider, holder);
5804 if (noReleaseNeeded) {
5805 prc = new ProviderRefCount(holder, client, 1000, 1000);
5806 } else {
5807 prc = stable
5808 ? new ProviderRefCount(holder, client, 1, 0)
5809 : new ProviderRefCount(holder, client, 0, 1);
5810 }
5811 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005812 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005813 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005814 }
5815 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005816
5817 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 }
5819
Romain Guy65b345f2011-07-27 18:51:50 -07005820 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005821 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005822 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005824 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005825 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005826 public void run() {
5827 ensureJitEnabled();
5828 }
5829 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005830 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5831 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005833 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 try {
5835 mgr.attachApplication(mAppThread);
5836 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005837 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005838 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005839 // Watch for getting close to heap limit.
5840 BinderInternal.addGcWatcher(new Runnable() {
5841 @Override public void run() {
5842 if (!mSomeActivitiesChanged) {
5843 return;
5844 }
5845 Runtime runtime = Runtime.getRuntime();
5846 long dalvikMax = runtime.maxMemory();
5847 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5848 if (dalvikUsed > ((3*dalvikMax)/4)) {
5849 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5850 + " total=" + (runtime.totalMemory()/1024)
5851 + " used=" + (dalvikUsed/1024));
5852 mSomeActivitiesChanged = false;
5853 try {
5854 mgr.releaseSomeActivities(mAppThread);
5855 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005856 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005857 }
5858 }
5859 }
5860 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 } else {
5862 // Don't set application object here -- if the system crashes,
5863 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005864 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005865 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 try {
5867 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005868 ContextImpl context = ContextImpl.createAppContext(
5869 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005870 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5871 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005872 } catch (Exception e) {
5873 throw new RuntimeException(
5874 "Unable to instantiate Application():" + e.toString(), e);
5875 }
5876 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005877
5878 // add dropbox logging to libcore
5879 DropBox.setReporter(new DropBoxReporter());
5880
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005881 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005882 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005883 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005884 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005885 // We need to apply this change to the resources
5886 // immediately, because upon returning the view
5887 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005888 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005889 // This actually changed the resources! Tell
5890 // everyone about it.
5891 if (mPendingConfiguration == null ||
5892 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5893 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005894
Jeff Brown9ef09972013-10-15 20:49:59 -07005895 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005896 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005897 }
5898 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005899 }
Craig Mautner88c05892013-06-28 09:47:45 -07005900 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005901 public void onLowMemory() {
5902 }
Craig Mautner88c05892013-06-28 09:47:45 -07005903 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005904 public void onTrimMemory(int level) {
5905 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005906 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005907 }
5908
Romain Guy5e9120d2012-01-30 12:17:22 -08005909 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005910 // The system process on low-memory devices do not get to use hardware
5911 // accelerated drawing, since this can add too much overhead to the
5912 // process.
5913 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005914 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005915 } else {
John Reck51aaf902015-12-02 15:08:07 -08005916 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005917 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005918 ActivityThread thread = new ActivityThread();
5919 thread.attach(true);
5920 return thread;
5921 }
5922
Jeff Brown10e89712011-07-08 18:52:57 -07005923 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005924 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005925 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 }
5927 }
5928
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005929 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005930 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005931 if (mCoreSettings != null) {
5932 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005933 }
Craig Mautner88c05892013-06-28 09:47:45 -07005934 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005935 }
5936 }
5937
Geremy Condra69689a72012-09-11 16:57:17 -07005938 private static class EventLoggingReporter implements EventLogger.Reporter {
5939 @Override
5940 public void report (int code, Object... list) {
5941 EventLog.writeEvent(code, list);
5942 }
5943 }
5944
Geremy Condrab7faaf42012-09-19 18:07:42 -07005945 private class DropBoxReporter implements DropBox.Reporter {
5946
5947 private DropBoxManager dropBox;
5948
Narayan Kamath7f062242015-04-08 13:24:13 +01005949 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005950
5951 @Override
5952 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005953 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005954 dropBox.addData(tag, data, flags);
5955 }
5956
5957 @Override
5958 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005959 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005960 dropBox.addText(tag, data);
5961 }
Narayan Kamath7f062242015-04-08 13:24:13 +01005962
5963 private synchronized void ensureInitialized() {
5964 if (dropBox == null) {
5965 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5966 }
5967 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005968 }
5969
Romain Guy65b345f2011-07-27 18:51:50 -07005970 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01005971 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07005972 SamplingProfilerIntegration.start();
5973
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005974 // CloseGuard defaults to true and can be quite spammy. We
5975 // disable it here, but selectively enable it later (via
5976 // StrictMode) on debug builds, but using DropBox, not logs.
5977 CloseGuard.setEnabled(false);
5978
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005979 Environment.initForCurrentUser();
5980
Geremy Condra69689a72012-09-11 16:57:17 -07005981 // Set the reporter for event logging in libcore
5982 EventLogger.setReporter(new EventLoggingReporter());
5983
Alex Klyubin48125632015-04-29 13:16:30 -07005984 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08005985
Robin Lee3d076af2014-04-25 14:57:49 +01005986 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5987 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5988 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 Process.setArgV0("<pre-initialized>");
5991
5992 Looper.prepareMainLooper();
5993
5994 ActivityThread thread = new ActivityThread();
5995 thread.attach(false);
5996
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005997 if (sMainThreadHandler == null) {
5998 sMainThreadHandler = thread.getHandler();
5999 }
6000
Dianne Hackborn287952c2010-09-22 22:34:31 -07006001 if (false) {
6002 Looper.myLooper().setMessageLogging(new
6003 LogPrinter(Log.DEBUG, "ActivityThread"));
6004 }
6005
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006006 // End of event ActivityThreadMain.
6007 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006008 Looper.loop();
6009
Jeff Brown10e89712011-07-08 18:52:57 -07006010 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006011 }
6012}