blob: 3a8b6c76e7984770aab8f1d4ee00b139e7a1bd2e [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
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070021import android.app.assist.AssistContent;
22import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080023import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070025import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ComponentName;
27import android.content.ContentProvider;
28import android.content.Context;
29import android.content.IContentProvider;
30import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070031import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.pm.ActivityInfo;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.IPackageManager;
35import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070036import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070038import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.ProviderInfo;
40import android.content.pm.ServiceInfo;
41import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070042import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Configuration;
44import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070045import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.database.sqlite.SQLiteDatabase;
47import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080048import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.graphics.Bitmap;
50import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050052import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070053import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050054import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070055import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040056import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040057import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070058import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070059import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070060import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Bundle;
62import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070063import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070064import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Handler;
66import android.os.IBinder;
Adam Lesinskib61e4052016-05-19 18:23:05 -070067import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.os.Looper;
69import android.os.Message;
70import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080071import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070072import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070073import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.os.Process;
75import android.os.RemoteException;
76import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070077import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070079import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070080import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070081import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070082import android.os.UserHandle;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000083import android.provider.Settings;
Chad Brubakerc72875b2016-04-27 16:35:11 -070084import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080085import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070087import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import android.util.DisplayMetrics;
89import android.util.EventLog;
90import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070091import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070092import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080093import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080094import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -070095import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -070096import android.util.SuperNotCalledException;
Adam Lesinski3ad1b482016-04-01 16:41:41 -070097import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -080099import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.View;
101import android.view.ViewDebug;
102import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -0700103import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import android.view.Window;
105import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700106import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700107import android.renderscript.RenderScriptCacheDir;
Calin Juravle69052392015-12-17 17:06:49 +0200108import android.system.Os;
109import android.system.OsConstants;
110import android.system.ErrnoException;
Richard Uhler2c036192016-09-14 09:48:31 +0100111import android.webkit.WebView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700113import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700114import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800115import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import com.android.internal.os.BinderInternal;
117import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700118import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700119import com.android.internal.os.SomeArgs;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700120import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700121import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700122import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100123import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700124import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125
126import java.io.File;
127import java.io.FileDescriptor;
128import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700129import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.io.PrintWriter;
131import java.lang.ref.WeakReference;
Svet Ganov37e43272016-09-09 16:01:32 -0700132import java.lang.reflect.Field;
133import java.lang.reflect.InvocationTargetException;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700134import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000135import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000137import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138import java.util.List;
Adam Lesinskib61e4052016-05-19 18:23:05 -0700139import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800141import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Geremy Condrab7faaf42012-09-19 18:07:42 -0700144import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700145import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800146import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400147import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800148import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700149import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700150import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700151import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700152
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700153final class RemoteServiceException extends AndroidRuntimeException {
154 public RemoteServiceException(String msg) {
155 super(msg);
156 }
157}
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159/**
160 * This manages the execution of the main thread in an
161 * application process, scheduling and executing activities,
162 * broadcasts, and other operations on it as the activity
163 * manager requests.
164 *
165 * {@hide}
166 */
167public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700168 /** @hide */
169 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700170 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700171 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700172 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700173 /** @hide */
174 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700175 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700176 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700177 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800178 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700179 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700180 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700181 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800184 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
185 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800186 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Dianne Hackborn455625e2015-01-21 09:55:13 -0800188 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
189 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
190 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
191 public static final int SERVICE_DONE_EXECUTING_START = 1;
192 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
193 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
194
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700195 // Details for pausing activity.
196 private static final int USER_LEAVING = 1;
197 private static final int DONT_REPORT = 2;
198
Filip Gruszczynskica664812015-12-04 12:43:36 -0800199 // Whether to invoke an activity callback after delivering new configuration.
200 private static final boolean REPORT_TO_ACTIVITY = true;
201
Jeff Browndefd4a62014-03-10 21:24:37 -0700202 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700203
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700204 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700206 final ApplicationThread mAppThread = new ApplicationThread();
207 final Looper mLooper = Looper.myLooper();
208 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700209 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700210 // List of new activities (via ActivityRecord.nextIdle) that should
211 // be reported when next we idle.
212 ActivityClientRecord mNewActivities = null;
213 // Number of activities that are currently visible on-screen.
214 int mNumVisibleActivities = 0;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700215 ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
216 private int mLastSessionId;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700217 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700218 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700219 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700220 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700221 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700222 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700223 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700224 Application mInitialApplication;
225 final ArrayList<Application> mAllApplications
226 = new ArrayList<Application>();
227 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700228 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800229 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700230 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700231 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700232 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700233 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700234 String[] mInstrumentationSplitAppDirs = null;
235 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700236 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700237 String[] mInstrumentedSplitAppDirs = null;
238 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700239 boolean mSystemThread = false;
240 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700241 boolean mSomeActivitiesChanged = false;
Chong Zhang4951f9d2016-06-23 13:15:08 -0700242 boolean mUpdatingSystemConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700244 // These can be accessed by multiple threads; mPackages is the lock.
245 // XXX For now we keep around information about all packages we have
246 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800247 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700248 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800249 // which means this lock gets held while the activity and window managers
250 // holds their own lock. Thus you MUST NEVER call back into the activity manager
251 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700252 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700253 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
254 = new ArrayMap<String, WeakReference<LoadedApk>>();
255 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
256 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700257 final ArrayList<ActivityClientRecord> mRelaunchingActivities
258 = new ArrayList<ActivityClientRecord>();
259 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700260 // Because we merge activity relaunch operations we can't depend on the ordering provided by
261 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
262 // us to drop certain events, if we know that they happened before relaunch we already executed.
263 // This represents the order of receiving the request from AM.
264 @GuardedBy("mResourcesManager")
265 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Craig Mautner88c05892013-06-28 09:47:45 -0700267 private final ResourcesManager mResourcesManager;
268
Jeff Sharkey6d515712012-09-20 16:06:08 -0700269 private static final class ProviderKey {
270 final String authority;
271 final int userId;
272
273 public ProviderKey(String authority, int userId) {
274 this.authority = authority;
275 this.userId = userId;
276 }
277
278 @Override
279 public boolean equals(Object o) {
280 if (o instanceof ProviderKey) {
281 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800282 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700283 }
284 return false;
285 }
286
287 @Override
288 public int hashCode() {
289 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
290 }
291 }
292
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700293 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700294 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
295 = new ArrayMap<ProviderKey, ProviderClientRecord>();
296 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
297 = new ArrayMap<IBinder, ProviderRefCount>();
298 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
299 = new ArrayMap<IBinder, ProviderClientRecord>();
300 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
301 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700303 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700304 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600305
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700306 final GcIdler mGcIdler = new GcIdler();
307 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700309 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700310
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800311 Bundle mCoreSettings = null;
312
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400313 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700315 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800317 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700318 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700320 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 Activity activity;
322 Window window;
323 Activity parent;
324 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700325 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 boolean paused;
327 boolean stopped;
328 boolean hideForNow;
329 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700330 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800331 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800332 // Used for consolidating configs before sending on to Activity.
333 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700334 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Jeff Hao1b012d32014-08-20 10:35:34 -0700336 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400339 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700340 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341
342 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800343 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
345 boolean startsNotResumed;
346 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800347 int pendingConfigChanges;
348 boolean onlyLocalRequest;
349
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700350 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800351 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700352 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700354 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
355 // can be compared with other lifecycle operations.
356 int relaunchSeq = 0;
357
358 // Can only be accessed from the UI thread. This represents the latest processed message
359 // that is related to lifecycle events/
360 int lastProcessedSeq = 0;
361
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700362 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 parent = null;
364 embeddedID = null;
365 paused = false;
366 stopped = false;
367 hideForNow = false;
368 nextIdle = null;
369 }
370
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800371 public boolean isPreHoneycomb() {
372 if (activity != null) {
373 return activity.getApplicationInfo().targetSdkVersion
374 < android.os.Build.VERSION_CODES.HONEYCOMB;
375 }
376 return false;
377 }
378
Craig Mautnera0026042014-04-23 11:45:37 -0700379 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700380 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700381 }
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700384 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 return "ActivityRecord{"
386 + Integer.toHexString(System.identityHashCode(this))
387 + " token=" + token + " " + (componentName == null
388 ? "no component name" : componentName.toShortString())
389 + "}";
390 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700391
392 public String getStateString() {
393 StringBuilder sb = new StringBuilder();
394 sb.append("ActivityClientRecord{");
395 sb.append("paused=").append(paused);
396 sb.append(", stopped=").append(stopped);
397 sb.append(", hideForNow=").append(hideForNow);
398 sb.append(", startsNotResumed=").append(startsNotResumed);
399 sb.append(", isForward=").append(isForward);
400 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
401 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
402 sb.append(", preserveWindow=").append(mPreserveWindow);
403 if (activity != null) {
404 sb.append(", Activity{");
405 sb.append("resumed=").append(activity.mResumed);
406 sb.append(", stopped=").append(activity.mStopped);
407 sb.append(", finished=").append(activity.isFinishing());
408 sb.append(", destroyed=").append(activity.isDestroyed());
409 sb.append(", startedActivity=").append(activity.mStartedActivity);
410 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
411 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
412 sb.append(", visibleBehind=").append(activity.mVisibleBehind);
413 sb.append("}");
414 }
415 sb.append("}");
416 return sb.toString();
417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 }
419
Wale Ogunwale1d646122015-04-24 14:45:14 -0700420 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700421 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 final IContentProvider mProvider;
423 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700424 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700426 ProviderClientRecord(String[] names, IContentProvider provider,
427 ContentProvider localProvider,
428 IActivityManager.ContentProviderHolder holder) {
429 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 mProvider = provider;
431 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700432 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 }
434 }
435
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400436 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800437 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 IBinder token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700439 boolean andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 public String toString() {
Wale Ogunwale826c7062016-09-13 08:25:54 -0700441 return "NewIntentData{intents=" + intents + " token=" + token
442 + " andPause=" + andPause +"}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 }
444 }
445
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400446 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700447 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700448 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
449 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800450 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700451 this.intent = intent;
452 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 Intent intent;
455 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public String toString() {
458 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700459 info.packageName + " resultCode=" + getResultCode()
460 + " resultData=" + getResultData() + " resultExtras="
461 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 }
463 }
464
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700466 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400467 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700468 int backupMode;
469 public String toString() {
470 return "CreateBackupAgentData{appInfo=" + appInfo
471 + " backupAgent=" + appInfo.backupAgentName
472 + " mode=" + backupMode + "}";
473 }
474 }
Bob Leee5408332009-09-04 18:31:17 -0700475
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400476 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 IBinder token;
478 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400479 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 Intent intent;
481 public String toString() {
482 return "CreateServiceData{token=" + token + " className="
483 + info.name + " packageName=" + info.packageName
484 + " intent=" + intent + "}";
485 }
486 }
487
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400488 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 IBinder token;
490 Intent intent;
491 boolean rebind;
492 public String toString() {
493 return "BindServiceData{token=" + token + " intent=" + intent + "}";
494 }
495 }
496
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400497 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700499 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700501 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800502 Intent args;
503 public String toString() {
504 return "ServiceArgsData{token=" + token + " startId=" + startId
505 + " args=" + args + "}";
506 }
507 }
508
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400509 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700510 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 String processName;
512 ApplicationInfo appInfo;
513 List<ProviderInfo> providers;
514 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 Bundle instrumentationArgs;
516 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800517 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400519 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700520 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700521 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700522 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400524 CompatibilityInfo compatInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700525 String buildSerial;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700526
527 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700528 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 public String toString() {
531 return "AppBindData{appInfo=" + appInfo + "}";
532 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700533 }
534
535 static final class Profiler {
536 String profileFile;
537 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700538 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700539 boolean autoStopProfiler;
540 boolean profiling;
541 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700542 public void setProfiler(ProfilerInfo profilerInfo) {
543 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700544 if (profiling) {
545 if (fd != null) {
546 try {
547 fd.close();
548 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700549 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700550 }
551 }
552 return;
553 }
554 if (profileFd != null) {
555 try {
556 profileFd.close();
557 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700558 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700559 }
560 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700561 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700562 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700563 samplingInterval = profilerInfo.samplingInterval;
564 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700565 }
566 public void startProfiling() {
567 if (profileFd == null || profiling) {
568 return;
569 }
570 try {
Andreas Gampeeff06392016-05-10 12:51:45 -0700571 int bufferSize = SystemProperties.getInt("debug.traceview-buffer-size-mb", 8);
Jeff Hao1b012d32014-08-20 10:35:34 -0700572 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
Andreas Gampe0f03a012016-05-09 14:26:07 -0700573 bufferSize * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700574 profiling = true;
575 } catch (RuntimeException e) {
576 Slog.w(TAG, "Profiling failed on path " + profileFile);
577 try {
578 profileFd.close();
579 profileFd = null;
580 } catch (IOException e2) {
581 Slog.w(TAG, "Failure closing profile fd", e2);
582 }
583 }
584 }
585 public void stopProfiling() {
586 if (profiling) {
587 profiling = false;
588 Debug.stopMethodTracing();
589 if (profileFd != null) {
590 try {
591 profileFd.close();
592 } catch (IOException e) {
593 }
594 }
595 profileFd = null;
596 profileFile = null;
597 }
598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
600
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400601 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700602 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700603 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800604 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 }
607
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400608 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 IBinder token;
610 List<ResultInfo> results;
611 public String toString() {
612 return "ResultData{token=" + token + " results" + results + "}";
613 }
614 }
615
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400616 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800617 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 String what;
619 String who;
620 }
621
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400622 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700623 String path;
624 ParcelFileDescriptor fd;
625 }
626
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400627 static final class UpdateCompatibilityData {
628 String pkg;
629 CompatibilityInfo info;
630 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800631
Adam Skorydfc7fd72013-08-05 19:23:41 -0700632 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800633 IBinder activityToken;
634 IBinder requestToken;
635 int requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700636 int sessionId;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800637 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700638
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800639 static final class ActivityConfigChangeData {
640 final IBinder activityToken;
641 final Configuration overrideConfig;
642 public ActivityConfigChangeData(IBinder token, Configuration config) {
643 activityToken = token;
644 overrideConfig = config;
645 }
646 }
647
Romain Guy65b345f2011-07-27 18:51:50 -0700648 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700649
Romain Guy65b345f2011-07-27 18:51:50 -0700650 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700651 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700652
Dianne Hackborna413dc02013-07-12 12:02:55 -0700653 private int mLastProcessState = -1;
654
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700655 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700656 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700657 if (mPendingConfiguration == null ||
658 mPendingConfiguration.isOtherSeqNewer(config)) {
659 mPendingConfiguration = config;
660 }
661 }
662 }
663
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700665 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700666 int seq = getLifecycleSeq();
667 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
668 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700669 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
671 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700672 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
673 configChanges,
674 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 }
676
677 public final void scheduleStopActivity(IBinder token, boolean showWindow,
678 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700679 int seq = getLifecycleSeq();
680 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
681 + " operation received seq: " + seq);
682 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700684 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 }
686
687 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700688 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800689 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
690 token);
691 }
692
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800693 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700694 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800695 }
696
Dianne Hackborna413dc02013-07-12 12:02:55 -0700697 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800698 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700699 int seq = getLifecycleSeq();
700 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
701 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700702 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700703 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705
706 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
707 ResultData res = new ResultData();
708 res.token = token;
709 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700710 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 // we use token to identify this activity without having to send the
714 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800715 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700716 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800717 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
718 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
719 int procState, Bundle state, PersistableBundle persistentState,
720 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
721 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700722
723 updateProcessState(procState, false);
724
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700725 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726
727 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700728 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800730 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700731 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400733 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700735 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736
737 r.pendingResults = pendingResults;
738 r.pendingIntents = pendingNewIntents;
739
740 r.startsNotResumed = notResumed;
741 r.isForward = isForward;
742
Jeff Hao1b012d32014-08-20 10:35:34 -0700743 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700744
Wale Ogunwale60454db2015-01-23 16:05:07 -0800745 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700746 updatePendingConfiguration(curConfig);
747
Jeff Brown9ef09972013-10-15 20:49:59 -0700748 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
Wale Ogunwale60454db2015-01-23 16:05:07 -0800751 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800753 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800754 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700755 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800756 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700757 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759
Wale Ogunwale826c7062016-09-13 08:25:54 -0700760 public final void scheduleNewIntent(
761 List<ReferrerIntent> intents, IBinder token, boolean andPause) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 NewIntentData data = new NewIntentData();
763 data.intents = intents;
764 data.token = token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700765 data.andPause = andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
Jeff Brown9ef09972013-10-15 20:49:59 -0700767 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769
770 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
771 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700772 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 configChanges);
774 }
775
776 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400777 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700778 boolean sync, int sendingUser, int processState) {
779 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700780 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700781 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400783 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700784 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 }
786
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400787 public final void scheduleCreateBackupAgent(ApplicationInfo app,
788 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700789 CreateBackupAgentData d = new CreateBackupAgentData();
790 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400791 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700792 d.backupMode = backupMode;
793
Jeff Brown9ef09972013-10-15 20:49:59 -0700794 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700795 }
796
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400797 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
798 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700799 CreateBackupAgentData d = new CreateBackupAgentData();
800 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400801 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700802
Jeff Brown9ef09972013-10-15 20:49:59 -0700803 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700804 }
805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700807 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
808 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 CreateServiceData s = new CreateServiceData();
810 s.token = token;
811 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400812 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813
Jeff Brown9ef09972013-10-15 20:49:59 -0700814 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 }
816
817 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700818 boolean rebind, int processState) {
819 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 BindServiceData s = new BindServiceData();
821 s.token = token;
822 s.intent = intent;
823 s.rebind = rebind;
824
Amith Yamasani742a6712011-05-04 14:49:28 -0700825 if (DEBUG_SERVICE)
826 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
827 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700828 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 }
830
831 public final void scheduleUnbindService(IBinder token, Intent intent) {
832 BindServiceData s = new BindServiceData();
833 s.token = token;
834 s.intent = intent;
835
Jeff Brown9ef09972013-10-15 20:49:59 -0700836 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700839 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700840 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 ServiceArgsData s = new ServiceArgsData();
842 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700843 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700845 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 s.args = args;
847
Jeff Brown9ef09972013-10-15 20:49:59 -0700848 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850
851 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700852 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
854
Jeff Hao1b012d32014-08-20 10:35:34 -0700855 public final void bindApplication(String processName, ApplicationInfo appInfo,
856 List<ProviderInfo> providers, ComponentName instrumentationName,
857 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
858 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800859 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700860 boolean enableBinderTracking, boolean trackAllocation,
861 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
Svet Ganov37e43272016-09-09 16:01:32 -0700862 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings,
863 String buildSerial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864
865 if (services != null) {
866 // Setup the service cache in the ServiceManager
867 ServiceManager.initServiceCache(services);
868 }
869
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800870 setCoreSettings(coreSettings);
871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 AppBindData data = new AppBindData();
873 data.processName = processName;
874 data.appInfo = appInfo;
875 data.providers = providers;
876 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 data.instrumentationArgs = instrumentationArgs;
878 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800879 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400881 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700882 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700883 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700884 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400886 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700887 data.initProfilerInfo = profilerInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700888 data.buildSerial = buildSerial;
Jeff Brown9ef09972013-10-15 20:49:59 -0700889 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
891
892 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700893 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
Christopher Tate5e1ab332009-09-01 20:32:49 -0700896 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700897 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700898 }
899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700901 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700902 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
905 public void updateTimeZone() {
906 TimeZone.setDefault(null);
907 }
908
Robert Greenwalt03595d02010-11-02 14:08:23 -0700909 public void clearDnsCache() {
910 // a non-standard API to get this to libcore
911 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400912 // Allow libcore to perform the necessary actions as it sees fit upon a network
913 // configuration change.
914 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700915 }
916
Jason Monk83520b92014-05-09 15:16:06 -0400917 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400918 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
919 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500920 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400921 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500922 } else {
923 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
924 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700925 }
926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 public void processInBackground() {
928 mH.removeMessages(H.GC_WHEN_IDLE);
929 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
930 }
931
932 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700933 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700934 try {
935 data.fd = ParcelFileDescriptor.dup(fd);
936 data.token = servicetoken;
937 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700938 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700939 } catch (IOException e) {
940 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942 }
943
944 // This function exists to make sure all receiver dispatching is
945 // correctly ordered, since these are one-way calls and the binder driver
946 // applies transaction ordering per object for such calls.
947 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700948 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700949 boolean sticky, int sendingUser, int processState) throws RemoteException {
950 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700951 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
952 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 }
Bob Leee5408332009-09-04 18:31:17 -0700954
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800955 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700957 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 }
959
Wale Ogunwale60454db2015-01-23 16:05:07 -0800960 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800961 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800962 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800963 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800964 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 }
966
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800967 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700968 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
969 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800970 }
971
Andy McFadden824c5102010-07-09 16:26:57 -0700972 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
973 DumpHeapData dhd = new DumpHeapData();
974 dhd.path = path;
975 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700976 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700977 }
978
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700979 public void setSchedulingGroup(int group) {
980 // Note: do this immediately, since going into the foreground
981 // should happen regardless of what pending work we have to do
982 // and the activity manager will wait for us to report back that
983 // we are done before sending us to the background.
984 try {
985 Process.setProcessGroup(Process.myPid(), group);
986 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800987 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700988 }
989 }
Bob Leee5408332009-09-04 18:31:17 -0700990
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700991 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700992 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700993 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700994
995 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700996 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700997 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700998
Dianne Hackborn30d71892010-12-11 10:37:55 -0800999 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
1000 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07001001 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001002 try {
1003 data.fd = ParcelFileDescriptor.dup(fd);
1004 data.token = activitytoken;
1005 data.prefix = prefix;
1006 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001007 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001008 } catch (IOException e) {
1009 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001010 }
1011 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07001012
Marco Nelissen18cb2872011-11-15 11:19:53 -08001013 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
1014 String[] args) {
1015 DumpComponentInfo data = new DumpComponentInfo();
1016 try {
1017 data.fd = ParcelFileDescriptor.dup(fd);
1018 data.token = providertoken;
1019 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001020 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001021 } catch (IOException e) {
1022 Slog.w(TAG, "dumpProvider failed", e);
1023 }
1024 }
1025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001027 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001028 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1029 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001030 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001031 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001032 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001033 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001034 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001035 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07001036 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001037 }
1038
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001039 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001040 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 long nativeMax = Debug.getNativeHeapSize() / 1024;
1042 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1043 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001046 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 long dalvikMax = runtime.totalMemory() / 1024;
1048 long dalvikFree = runtime.freeMemory() / 1024;
1049 long dalvikAllocated = dalvikMax - dalvikFree;
Richard Uhler2c036192016-09-14 09:48:31 +01001050
1051 Class[] classesToCount = new Class[] {
1052 ContextImpl.class,
1053 Activity.class,
1054 WebView.class,
1055 OpenSSLSocketImpl.class
1056 };
1057 long[] instanceCounts = VMDebug.countInstancesOfClasses(classesToCount, true);
1058 long appContextInstanceCount = instanceCounts[0];
1059 long activityInstanceCount = instanceCounts[1];
1060 long webviewInstanceCount = instanceCounts[2];
1061 long openSslSocketCount = instanceCounts[3];
1062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001064 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 int globalAssetCount = AssetManager.getGlobalAssetCount();
1066 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1067 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1068 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1069 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001070 long parcelSize = Parcel.getGlobalAllocSize();
1071 long parcelCount = Parcel.getGlobalAllocCount();
Vasu Noric3849202010-03-09 10:47:25 -08001072 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001073
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001074 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1075 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001076 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1077 nativeMax, nativeAllocated, nativeFree,
1078 dalvikMax, dalvikAllocated, dalvikFree);
1079
Dianne Hackbornb437e092011-08-05 17:50:29 -07001080 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 // NOTE: if you change anything significant below, also consider changing
1082 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 // Object counts
1085 pw.print(viewInstanceCount); pw.print(',');
1086 pw.print(viewRootInstanceCount); pw.print(',');
1087 pw.print(appContextInstanceCount); pw.print(',');
1088 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 pw.print(globalAssetCount); pw.print(',');
1091 pw.print(globalAssetManagerCount); pw.print(',');
1092 pw.print(binderLocalObjectCount); pw.print(',');
1093 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001095 pw.print(binderDeathObjectCount); pw.print(',');
1096 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001099 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001100 pw.print(stats.memoryUsed / 1024); pw.print(',');
1101 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001102 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001103 for (int i = 0; i < stats.dbStats.size(); i++) {
1104 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001105 pw.print(','); pw.print(dbStats.dbName);
1106 pw.print(','); pw.print(dbStats.pageSize);
1107 pw.print(','); pw.print(dbStats.dbSize);
1108 pw.print(','); pw.print(dbStats.lookaside);
1109 pw.print(','); pw.print(dbStats.cache);
1110 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001111 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001112 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001113
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001114 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
Bob Leee5408332009-09-04 18:31:17 -07001116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 pw.println(" ");
1118 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001119 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 viewRootInstanceCount);
1121
1122 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1123 "Activities:", activityInstanceCount);
1124
1125 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1126 "AssetManagers:", globalAssetManagerCount);
1127
1128 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1129 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001130 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1131 "Parcel count:", parcelCount);
1132 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1133 "OpenSSL Sockets:", openSslSocketCount);
Richard Uhler2c036192016-09-14 09:48:31 +01001134 printRow(pw, ONE_COUNT_COLUMN, "WebViews:", webviewInstanceCount);
Bob Leee5408332009-09-04 18:31:17 -07001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 // SQLite mem info
1137 pw.println(" ");
1138 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001139 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1140 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1141 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001142 pw.println(" ");
1143 int N = stats.dbStats.size();
1144 if (N > 0) {
1145 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001146 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1147 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001148 for (int i = 0; i < N; i++) {
1149 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001150 printRow(pw, DB_INFO_FORMAT,
1151 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1152 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1153 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1154 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001155 }
1156 }
Bob Leee5408332009-09-04 18:31:17 -07001157
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001158 // Asset details.
1159 String assetAlloc = AssetManager.getAssetAllocations();
1160 if (assetAlloc != null) {
1161 pw.println(" ");
1162 pw.println(" Asset Allocations");
1163 pw.print(assetAlloc);
1164 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001165
1166 // Unreachable native memory
1167 if (dumpUnreachable) {
1168 boolean showContents = ((mBoundApplication != null)
1169 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1170 || android.os.Build.IS_DEBUGGABLE;
1171 pw.println(" ");
1172 pw.println(" Unreachable memory");
1173 pw.print(Debug.getUnreachableMemory(100, showContents));
1174 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001175 }
1176
1177 @Override
1178 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1179 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001180 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 }
1182
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001183 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001184 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001185 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1186 SQLiteDebug.dump(printer, args);
1187 pw.flush();
1188 }
1189
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001190 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001191 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1192 if (mSystemThread) {
Felipe Leme29de4922016-06-07 16:14:07 -07001193 // Ensure this invocation is asynchronous to prevent writer waiting if buffer cannot
1194 // be consumed. But it must duplicate the file descriptor first, since caller might
1195 // be closing it.
1196 final ParcelFileDescriptor dup;
1197 try {
1198 dup = ParcelFileDescriptor.dup(fd);
1199 } catch (IOException e) {
1200 Log.w(TAG, "Could not dup FD " + fd.getInt$());
1201 return;
1202 }
1203
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001204 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1205 @Override
1206 public void run() {
Felipe Lemec74972f2016-06-08 09:12:37 -07001207 try {
1208 dumpDatabaseInfo(dup.getFileDescriptor(), args);
1209 } finally {
1210 IoUtils.closeQuietly(dup);
1211 }
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001212 }
1213 });
1214 } else {
1215 dumpDatabaseInfo(fd, args);
1216 }
1217 }
1218
1219 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001220 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001221 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001222 }
1223
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001224 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001225 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Amith Yamasani4f128e42016-05-10 11:44:12 -07001226 int requestType, int sessionId) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001227 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001228 cmd.activityToken = activityToken;
1229 cmd.requestToken = requestToken;
1230 cmd.requestType = requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -07001231 cmd.sessionId = sessionId;
Jeff Brown9ef09972013-10-15 20:49:59 -07001232 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001233 }
1234
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001235 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001236 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001237 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001238
1239 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1240 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1241 ucd.pkg = pkg;
1242 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001243 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001244 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001245
1246 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001247 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001248 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001249
Craig Mautner5eda9b32013-07-02 11:58:16 -07001250 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001251 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001252 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001253
Craig Mautnereb8abf72014-07-02 15:04:09 -07001254 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1255 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1256 new Pair<IBinder, ActivityOptions>(token, options));
1257 }
1258
Dianne Hackborna413dc02013-07-12 12:02:55 -07001259 public void setProcessState(int state) {
1260 updateProcessState(state, true);
1261 }
1262
1263 public void updateProcessState(int processState, boolean fromIpc) {
1264 synchronized (this) {
1265 if (mLastProcessState != processState) {
1266 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001267 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1268 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1269 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1270 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1271 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1272 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1273 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1274 }
1275 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001276 if (false) {
1277 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1278 + (fromIpc ? " (from ipc": ""));
1279 }
1280 }
1281 }
1282 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001283
1284 @Override
1285 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001286 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001287 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001288
1289 @Override
1290 public final void updateTimePrefs(boolean is24Hour) {
1291 DateFormat.set24HourTimePref(is24Hour);
1292 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001293
1294 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001295 public void scheduleCancelVisibleBehind(IBinder token) {
1296 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001297 }
1298
1299 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001300 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1301 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001302 }
Craig Mautner8746a472014-07-24 15:12:54 -07001303
Jeff Sharkey605eb792014-11-04 13:34:06 -08001304 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001305 public void scheduleEnterAnimationComplete(IBinder token) {
1306 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1307 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001308
1309 @Override
1310 public void notifyCleartextNetwork(byte[] firstPacket) {
1311 if (StrictMode.vmCleartextNetworkEnabled()) {
1312 StrictMode.onCleartextNetworkDetected(firstPacket);
1313 }
1314 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001315
1316 @Override
1317 public void startBinderTracking() {
1318 sendMessage(H.START_BINDER_TRACKING, null);
1319 }
1320
1321 @Override
1322 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1323 try {
1324 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1325 } catch (IOException e) {
1326 }
1327 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001328
1329 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001330 public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001331 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001332 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, token, isInMultiWindowMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001333 }
1334
1335 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001336 public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001337 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001338 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, token, isInPipMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001339 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001340
1341 @Override
1342 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1343 IVoiceInteractor voiceInteractor) throws RemoteException {
1344 SomeArgs args = SomeArgs.obtain();
1345 args.arg1 = token;
1346 args.arg2 = voiceInteractor;
1347 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1348 }
Chad Brubakerc72875b2016-04-27 16:35:11 -07001349
1350 @Override
1351 public void handleTrustStorageUpdate() {
1352 NetworkSecurityPolicy.getInstance().handleTrustStorageUpdate();
1353 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001356 private int getLifecycleSeq() {
1357 synchronized (mResourcesManager) {
1358 return mLifecycleSeq++;
1359 }
1360 }
1361
Romain Guy65b345f2011-07-27 18:51:50 -07001362 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 public static final int LAUNCH_ACTIVITY = 100;
1364 public static final int PAUSE_ACTIVITY = 101;
1365 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1366 public static final int STOP_ACTIVITY_SHOW = 103;
1367 public static final int STOP_ACTIVITY_HIDE = 104;
1368 public static final int SHOW_WINDOW = 105;
1369 public static final int HIDE_WINDOW = 106;
1370 public static final int RESUME_ACTIVITY = 107;
1371 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001372 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 public static final int BIND_APPLICATION = 110;
1374 public static final int EXIT_APPLICATION = 111;
1375 public static final int NEW_INTENT = 112;
1376 public static final int RECEIVER = 113;
1377 public static final int CREATE_SERVICE = 114;
1378 public static final int SERVICE_ARGS = 115;
1379 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 public static final int CONFIGURATION_CHANGED = 118;
1382 public static final int CLEAN_UP_CONTEXT = 119;
1383 public static final int GC_WHEN_IDLE = 120;
1384 public static final int BIND_SERVICE = 121;
1385 public static final int UNBIND_SERVICE = 122;
1386 public static final int DUMP_SERVICE = 123;
1387 public static final int LOW_MEMORY = 124;
1388 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1389 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001390 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001391 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001392 public static final int DESTROY_BACKUP_AGENT = 129;
1393 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001394 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001395 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001396 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001397 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001398 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001399 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001400 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001401 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001402 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001403 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001404 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001405 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001406 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001407 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001408 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001409 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001410 public static final int CANCEL_VISIBLE_BEHIND = 147;
1411 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001412 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001413 public static final int START_BINDER_TRACKING = 150;
1414 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Andrii Kulian933076d2016-03-29 17:04:42 -07001415 public static final int MULTI_WINDOW_MODE_CHANGED = 152;
1416 public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001417 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001420 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 switch (code) {
1422 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1423 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1424 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1425 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1426 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1427 case SHOW_WINDOW: return "SHOW_WINDOW";
1428 case HIDE_WINDOW: return "HIDE_WINDOW";
1429 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1430 case SEND_RESULT: return "SEND_RESULT";
1431 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1432 case BIND_APPLICATION: return "BIND_APPLICATION";
1433 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1434 case NEW_INTENT: return "NEW_INTENT";
1435 case RECEIVER: return "RECEIVER";
1436 case CREATE_SERVICE: return "CREATE_SERVICE";
1437 case SERVICE_ARGS: return "SERVICE_ARGS";
1438 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1440 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1441 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1442 case BIND_SERVICE: return "BIND_SERVICE";
1443 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1444 case DUMP_SERVICE: return "DUMP_SERVICE";
1445 case LOW_MEMORY: return "LOW_MEMORY";
1446 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1447 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001448 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001449 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1450 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001451 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001452 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001453 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001454 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001455 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001456 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001457 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001458 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001459 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001460 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001461 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001462 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001463 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001464 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001465 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001466 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001467 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001468 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1469 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001470 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Andrii Kulian933076d2016-03-29 17:04:42 -07001471 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1472 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001473 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 }
1475 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001476 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001477 }
1478 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001479 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 switch (msg.what) {
1481 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001483 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484
1485 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001486 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001487 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001488 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 } break;
1490 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001491 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001492 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001493 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001494 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001496 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001497 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001498 SomeArgs args = (SomeArgs) msg.obj;
1499 handlePauseActivity((IBinder) args.arg1, false,
1500 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1501 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001502 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001503 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001504 } break;
1505 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001506 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001507 SomeArgs args = (SomeArgs) msg.obj;
1508 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1509 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001510 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001511 } break;
1512 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001513 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001514 SomeArgs args = (SomeArgs) msg.obj;
1515 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001516 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001517 } break;
1518 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001519 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001520 SomeArgs args = (SomeArgs) msg.obj;
1521 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001522 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001523 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001525 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001527 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 break;
1529 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001530 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001532 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 break;
1534 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001535 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001536 SomeArgs args = (SomeArgs) msg.obj;
1537 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001538 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001539 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 break;
1541 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001542 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001544 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 break;
1546 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001547 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1549 msg.arg2, false);
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 BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001553 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 AppBindData data = (AppBindData)msg.obj;
1555 handleBindApplication(data);
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;
1558 case EXIT_APPLICATION:
1559 if (mInitialApplication != null) {
1560 mInitialApplication.onTerminate();
1561 }
1562 Looper.myLooper().quit();
1563 break;
1564 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001565 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001567 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 break;
1569 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001570 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001572 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001573 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 break;
1575 case CREATE_SERVICE:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001576 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001578 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 break;
1580 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001581 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 handleBindService((BindServiceData)msg.obj);
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;
1585 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001586 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001588 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 break;
1590 case SERVICE_ARGS:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001591 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceStart: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001593 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 break;
1595 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001596 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001598 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001599 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001602 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001603 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Chong Zhang4951f9d2016-06-23 13:15:08 -07001604 mUpdatingSystemConfig = true;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001605 handleConfigurationChanged((Configuration)msg.obj, null);
Chong Zhang4951f9d2016-06-23 13:15:08 -07001606 mUpdatingSystemConfig = false;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001607 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 break;
1609 case CLEAN_UP_CONTEXT:
1610 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1611 cci.context.performFinalCleanup(cci.who, cci.what);
1612 break;
1613 case GC_WHEN_IDLE:
1614 scheduleGcIdler();
1615 break;
1616 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001617 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 break;
1619 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001620 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001621 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001622 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001623 break;
1624 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001625 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001626 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1627 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001628 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001630 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001631 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001632 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001633 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001634 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001635 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001636 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001637 break;
1638 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001639 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001640 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001641 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001642 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001643 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001644 Process.killProcess(Process.myPid());
1645 break;
1646 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001647 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001648 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001649 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001650 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001651 case ENABLE_JIT:
1652 ensureJitEnabled();
1653 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001654 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001655 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001656 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001657 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001658 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001659 case SCHEDULE_CRASH:
1660 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001661 case DUMP_HEAP:
1662 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1663 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001664 case DUMP_ACTIVITY:
1665 handleDumpActivity((DumpComponentInfo)msg.obj);
1666 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001667 case DUMP_PROVIDER:
1668 handleDumpProvider((DumpComponentInfo)msg.obj);
1669 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001670 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001671 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001672 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001673 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001674 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001675 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001676 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001677 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001678 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001679 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001680 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1681 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001682 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001683 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001684 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001685 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001686 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001687 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001688 case UNSTABLE_PROVIDER_DIED:
1689 handleUnstableProviderDied((IBinder)msg.obj, false);
1690 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001691 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1692 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001693 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001694 case TRANSLUCENT_CONVERSION_COMPLETE:
1695 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1696 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001697 case INSTALL_PROVIDER:
1698 handleInstallProvider((ProviderInfo) msg.obj);
1699 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001700 case ON_NEW_ACTIVITY_OPTIONS:
1701 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1702 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001703 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001704 case CANCEL_VISIBLE_BEHIND:
1705 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001706 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001707 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1708 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001709 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001710 case ENTER_ANIMATION_COMPLETE:
1711 handleEnterAnimationComplete((IBinder) msg.obj);
1712 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001713 case START_BINDER_TRACKING:
1714 handleStartBinderTracking();
1715 break;
1716 case STOP_BINDER_TRACKING_AND_DUMP:
1717 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1718 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001719 case MULTI_WINDOW_MODE_CHANGED:
1720 handleMultiWindowModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001721 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001722 case PICTURE_IN_PICTURE_MODE_CHANGED:
1723 handlePictureInPictureModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001724 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001725 case LOCAL_VOICE_INTERACTION_STARTED:
1726 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1727 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1728 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001730 Object obj = msg.obj;
1731 if (obj instanceof SomeArgs) {
1732 ((SomeArgs) obj).recycle();
1733 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001734 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001735 }
Bob Leee5408332009-09-04 18:31:17 -07001736
Brian Carlstromed7e0072011-03-24 13:27:57 -07001737 private void maybeSnapshot() {
1738 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001739 // convert the *private* ActivityThread.PackageInfo to *public* known
1740 // android.content.pm.PackageInfo
1741 String packageName = mBoundApplication.info.mPackageName;
1742 android.content.pm.PackageInfo packageInfo = null;
1743 try {
1744 Context context = getSystemContext();
1745 if(context == null) {
1746 Log.e(TAG, "cannot get a valid context");
1747 return;
1748 }
1749 PackageManager pm = context.getPackageManager();
1750 if(pm == null) {
1751 Log.e(TAG, "cannot get a valid PackageManager");
1752 return;
1753 }
1754 packageInfo = pm.getPackageInfo(
1755 packageName, PackageManager.GET_ACTIVITIES);
1756 } catch (NameNotFoundException e) {
1757 Log.e(TAG, "cannot get package info for " + packageName, e);
1758 }
1759 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001760 }
1761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
Romain Guy65b345f2011-07-27 18:51:50 -07001764 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001765 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001767 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001768 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001769 if (mBoundApplication != null && mProfiler.profileFd != null
1770 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001771 stopProfiling = true;
1772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (a != null) {
1774 mNewActivities = null;
1775 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001776 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001778 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 TAG, "Reporting idle of " + a +
1780 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001781 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 if (a.activity != null && !a.activity.mFinished) {
1783 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001784 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001785 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001787 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
1790 prev = a;
1791 a = a.nextIdle;
1792 prev.nextIdle = null;
1793 } while (a != null);
1794 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001795 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001796 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001797 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001798 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 return false;
1800 }
1801 }
1802
1803 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001804 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 public final boolean queueIdle() {
1806 doGcIfNeeded();
1807 return false;
1808 }
1809 }
1810
Romain Guy65b345f2011-07-27 18:51:50 -07001811 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001812 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001815 public static boolean isSystem() {
1816 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1817 }
1818
Svetoslavfbf0eca2015-05-01 16:52:41 -07001819 public static String currentOpPackageName() {
1820 ActivityThread am = currentActivityThread();
1821 return (am != null && am.getApplication() != null)
1822 ? am.getApplication().getOpPackageName() : null;
1823 }
1824
Romain Guy65b345f2011-07-27 18:51:50 -07001825 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001826 ActivityThread am = currentActivityThread();
1827 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001828 ? am.mBoundApplication.appInfo.packageName : null;
1829 }
1830
1831 public static String currentProcessName() {
1832 ActivityThread am = currentActivityThread();
1833 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001834 ? am.mBoundApplication.processName : null;
1835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836
Romain Guy65b345f2011-07-27 18:51:50 -07001837 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001838 ActivityThread am = currentActivityThread();
1839 return am != null ? am.mInitialApplication : null;
1840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001842 public static IPackageManager getPackageManager() {
1843 if (sPackageManager != null) {
1844 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1845 return sPackageManager;
1846 }
1847 IBinder b = ServiceManager.getService("package");
1848 //Slog.v("PackageManager", "default service binder = " + b);
1849 sPackageManager = IPackageManager.Stub.asInterface(b);
1850 //Slog.v("PackageManager", "default service = " + sPackageManager);
1851 return sPackageManager;
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853
Romain Guy65b345f2011-07-27 18:51:50 -07001854 private Configuration mMainThreadConfig = new Configuration();
Amith Yamasani4f128e42016-05-10 11:44:12 -07001855
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001856 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1857 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001858 if (config == null) {
1859 return null;
1860 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001861 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001862 mMainThreadConfig.setTo(config);
1863 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001864 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001865 }
1866 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001869 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001870 * Creates the top level resources for the given package. Will return an existing
1871 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001872 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001873 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001874 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1875 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1876 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001877 }
1878
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001879 final Handler getHandler() {
1880 return mH;
1881 }
1882
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001883 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1884 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001885 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1886 }
1887
1888 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1889 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001890 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001891 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001892 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001893 if (differentUser) {
1894 // Caching not supported across users
1895 ref = null;
1896 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 ref = mPackages.get(packageName);
1898 } else {
1899 ref = mResourcePackages.get(packageName);
1900 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001901
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001902 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001903 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001904 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1905 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001906 if (packageInfo != null && (packageInfo.mResources == null
1907 || packageInfo.mResources.getAssets().isUpToDate())) {
1908 if (packageInfo.isSecurityViolation()
1909 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1910 throw new SecurityException(
1911 "Requesting code from " + packageName
1912 + " to be run in process "
1913 + mBoundApplication.processName
1914 + "/" + mBoundApplication.appInfo.uid);
1915 }
1916 return packageInfo;
1917 }
1918 }
1919
1920 ApplicationInfo ai = null;
1921 try {
1922 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001923 PackageManager.GET_SHARED_LIBRARY_FILES
1924 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1925 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001927 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929
1930 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001931 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933
1934 return null;
1935 }
1936
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001937 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1938 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1940 boolean securityViolation = includeCode && ai.uid != 0
1941 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001942 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001943 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001944 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1946 |Context.CONTEXT_IGNORE_SECURITY))
1947 == Context.CONTEXT_INCLUDE_CODE) {
1948 if (securityViolation) {
1949 String msg = "Requesting code from " + ai.packageName
1950 + " (with uid " + ai.uid + ")";
1951 if (mBoundApplication != null) {
1952 msg = msg + " to be run in process "
1953 + mBoundApplication.processName + " (with uid "
1954 + mBoundApplication.appInfo.uid + ")";
1955 }
1956 throw new SecurityException(msg);
1957 }
1958 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001959 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1960 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001963 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1964 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001965 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 }
1967
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001968 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001969 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001970 WeakReference<LoadedApk> ref;
1971 if (includeCode) {
1972 ref = mPackages.get(packageName);
1973 } else {
1974 ref = mResourcePackages.get(packageName);
1975 }
1976 return ref != null ? ref.get() : null;
1977 }
1978 }
1979
Romain Guy65b345f2011-07-27 18:51:50 -07001980 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001981 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1982 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001983 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001984 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001985 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001986 if (differentUser) {
1987 // Caching not supported across users
1988 ref = null;
1989 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 ref = mPackages.get(aInfo.packageName);
1991 } else {
1992 ref = mResourcePackages.get(aInfo.packageName);
1993 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001994
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001995 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 if (packageInfo == null || (packageInfo.mResources != null
1997 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001998 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999 : "Loading resource-only package ") + aInfo.packageName
2000 + " (in " + (mBoundApplication != null
2001 ? mBoundApplication.processName : null)
2002 + ")");
2003 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07002004 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002006 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00002007
2008 if (mSystemThread && "android".equals(aInfo.packageName)) {
2009 packageInfo.installSystemApplicationInfo(aInfo,
2010 getSystemContext().mPackageInfo.getClassLoader());
2011 }
2012
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002013 if (differentUser) {
2014 // Caching not supported across users
2015 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002017 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 } else {
2019 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002020 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
2022 }
2023 return packageInfo;
2024 }
2025 }
2026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07002028 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
2030
2031 public ApplicationThread getApplicationThread()
2032 {
2033 return mAppThread;
2034 }
2035
2036 public Instrumentation getInstrumentation()
2037 {
2038 return mInstrumentation;
2039 }
2040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002042 return mProfiler != null && mProfiler.profileFile != null
2043 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045
2046 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002047 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 }
2049
2050 public Looper getLooper() {
2051 return mLooper;
2052 }
2053
2054 public Application getApplication() {
2055 return mInitialApplication;
2056 }
Bob Leee5408332009-09-04 18:31:17 -07002057
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002058 public String getProcessName() {
2059 return mBoundApplication.processName;
2060 }
Bob Leee5408332009-09-04 18:31:17 -07002061
Dianne Hackborn21556372010-02-04 16:34:40 -08002062 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002063 synchronized (this) {
2064 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002065 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002067 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 }
2070
Narayan Kamath29564cd2014-08-07 10:57:40 +01002071 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002072 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002073 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002074
2075 // give ourselves a default profiler
2076 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002077 }
2078 }
2079
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002080 void ensureJitEnabled() {
2081 if (!mJitEnabled) {
2082 mJitEnabled = true;
2083 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2084 }
2085 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 void scheduleGcIdler() {
2088 if (!mGcIdlerScheduled) {
2089 mGcIdlerScheduled = true;
2090 Looper.myQueue().addIdleHandler(mGcIdler);
2091 }
2092 mH.removeMessages(H.GC_WHEN_IDLE);
2093 }
2094
2095 void unscheduleGcIdler() {
2096 if (mGcIdlerScheduled) {
2097 mGcIdlerScheduled = false;
2098 Looper.myQueue().removeIdleHandler(mGcIdler);
2099 }
2100 mH.removeMessages(H.GC_WHEN_IDLE);
2101 }
2102
2103 void doGcIfNeeded() {
2104 mGcIdlerScheduled = false;
2105 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002106 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 // + "m now=" + now);
2108 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002109 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 BinderInternal.forceGc("bg");
2111 }
2112 }
2113
Dianne Hackborne77187d2013-10-25 16:32:41 -07002114 private static final String HEAP_FULL_COLUMN
2115 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2116 private static final String HEAP_COLUMN
2117 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002118 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2119 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2120 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002121
2122 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002123 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002124
2125 static void printRow(PrintWriter pw, String format, Object...objs) {
2126 pw.println(String.format(format, objs));
2127 }
2128
2129 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002130 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2131 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002132 long nativeMax, long nativeAllocated, long nativeFree,
2133 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2134
2135 // For checkin, we print one long comma-separated list of values
2136 if (checkin) {
2137 // NOTE: if you change anything significant below, also consider changing
2138 // ACTIVITY_THREAD_CHECKIN_VERSION.
2139
2140 // Header
2141 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2142 pw.print(pid); pw.print(',');
2143 pw.print(processName); pw.print(',');
2144
2145 // Heap info - max
2146 pw.print(nativeMax); pw.print(',');
2147 pw.print(dalvikMax); pw.print(',');
2148 pw.print("N/A,");
2149 pw.print(nativeMax + dalvikMax); pw.print(',');
2150
2151 // Heap info - allocated
2152 pw.print(nativeAllocated); pw.print(',');
2153 pw.print(dalvikAllocated); pw.print(',');
2154 pw.print("N/A,");
2155 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2156
2157 // Heap info - free
2158 pw.print(nativeFree); pw.print(',');
2159 pw.print(dalvikFree); pw.print(',');
2160 pw.print("N/A,");
2161 pw.print(nativeFree + dalvikFree); pw.print(',');
2162
2163 // Heap info - proportional set size
2164 pw.print(memInfo.nativePss); pw.print(',');
2165 pw.print(memInfo.dalvikPss); pw.print(',');
2166 pw.print(memInfo.otherPss); pw.print(',');
2167 pw.print(memInfo.getTotalPss()); pw.print(',');
2168
2169 // Heap info - swappable set size
2170 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2171 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2172 pw.print(memInfo.otherSwappablePss); pw.print(',');
2173 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2174
2175 // Heap info - shared dirty
2176 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2177 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2178 pw.print(memInfo.otherSharedDirty); pw.print(',');
2179 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2180
2181 // Heap info - shared clean
2182 pw.print(memInfo.nativeSharedClean); pw.print(',');
2183 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2184 pw.print(memInfo.otherSharedClean); pw.print(',');
2185 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2186
2187 // Heap info - private Dirty
2188 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2189 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2190 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2191 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2192
2193 // Heap info - private Clean
2194 pw.print(memInfo.nativePrivateClean); pw.print(',');
2195 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2196 pw.print(memInfo.otherPrivateClean); pw.print(',');
2197 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2198
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002199 // Heap info - swapped out
2200 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2201 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2202 pw.print(memInfo.otherSwappedOut); pw.print(',');
2203 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2204
2205 // Heap info - swapped out pss
2206 if (memInfo.hasSwappedOutPss) {
2207 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2208 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2209 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2210 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2211 } else {
2212 pw.print("N/A,");
2213 pw.print("N/A,");
2214 pw.print("N/A,");
2215 pw.print("N/A,");
2216 }
2217
Dianne Hackborne77187d2013-10-25 16:32:41 -07002218 // Heap info - other areas
2219 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2220 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2221 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2222 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2223 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2224 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2225 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2226 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002227 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2228 if (memInfo.hasSwappedOutPss) {
2229 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2230 } else {
2231 pw.print("N/A,");
2232 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002233 }
2234 return;
2235 }
2236
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002237 if (!dumpSummaryOnly) {
2238 if (dumpFullInfo) {
2239 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002240 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2241 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002242 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002243 "Clean", "Clean", "Dirty",
2244 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002245 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2246 "------", "------", "------", "------", "------", "------");
2247 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2248 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2249 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002250 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2251 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002252 nativeMax, nativeAllocated, nativeFree);
2253 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2254 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2255 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002256 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2257 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002258 dalvikMax, dalvikAllocated, dalvikFree);
2259 } else {
2260 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002261 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2262 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002263 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2264 "Clean", "Dirty", "Size", "Alloc", "Free");
2265 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2266 "------", "------", "------", "------", "------");
2267 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2268 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002269 memInfo.nativePrivateClean,
2270 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2271 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002272 nativeMax, nativeAllocated, nativeFree);
2273 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2274 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002275 memInfo.dalvikPrivateClean,
2276 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2277 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002278 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002279 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002280
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002281 int otherPss = memInfo.otherPss;
2282 int otherSwappablePss = memInfo.otherSwappablePss;
2283 int otherSharedDirty = memInfo.otherSharedDirty;
2284 int otherPrivateDirty = memInfo.otherPrivateDirty;
2285 int otherSharedClean = memInfo.otherSharedClean;
2286 int otherPrivateClean = memInfo.otherPrivateClean;
2287 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002288 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002289
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002290 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002291 final int myPss = memInfo.getOtherPss(i);
2292 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2293 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2294 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2295 final int mySharedClean = memInfo.getOtherSharedClean(i);
2296 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2297 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002298 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002299 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002300 || mySharedClean != 0 || myPrivateClean != 0
2301 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002302 if (dumpFullInfo) {
2303 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2304 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002305 mySharedClean, myPrivateClean,
2306 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2307 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002308 } else {
2309 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2310 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002311 myPrivateClean,
2312 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2313 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002314 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002315 otherPss -= myPss;
2316 otherSwappablePss -= mySwappablePss;
2317 otherSharedDirty -= mySharedDirty;
2318 otherPrivateDirty -= myPrivateDirty;
2319 otherSharedClean -= mySharedClean;
2320 otherPrivateClean -= myPrivateClean;
2321 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002322 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002323 }
2324 }
2325
2326 if (dumpFullInfo) {
2327 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2328 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002329 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2330 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002331 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2332 memInfo.getTotalSwappablePss(),
2333 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2334 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002335 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2336 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002337 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2338 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002339 } else {
2340 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002341 otherPrivateDirty, otherPrivateClean,
2342 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002343 "", "", "");
2344 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2345 memInfo.getTotalPrivateDirty(),
2346 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002347 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002348 memInfo.getTotalSwappedOut(),
2349 nativeMax+dalvikMax,
2350 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2351 }
2352
2353 if (dumpDalvik) {
2354 pw.println(" ");
2355 pw.println(" Dalvik Details");
2356
2357 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2358 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2359 final int myPss = memInfo.getOtherPss(i);
2360 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2361 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2362 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2363 final int mySharedClean = memInfo.getOtherSharedClean(i);
2364 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2365 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002366 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002367 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002368 || mySharedClean != 0 || myPrivateClean != 0
2369 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002370 if (dumpFullInfo) {
2371 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2372 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002373 mySharedClean, myPrivateClean,
2374 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2375 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002376 } else {
2377 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2378 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002379 myPrivateClean,
2380 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2381 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002382 }
2383 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002384 }
2385 }
2386 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002387
2388 pw.println(" ");
2389 pw.println(" App Summary");
2390 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2391 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2392 printRow(pw, ONE_COUNT_COLUMN,
2393 "Java Heap:", memInfo.getSummaryJavaHeap());
2394 printRow(pw, ONE_COUNT_COLUMN,
2395 "Native Heap:", memInfo.getSummaryNativeHeap());
2396 printRow(pw, ONE_COUNT_COLUMN,
2397 "Code:", memInfo.getSummaryCode());
2398 printRow(pw, ONE_COUNT_COLUMN,
2399 "Stack:", memInfo.getSummaryStack());
2400 printRow(pw, ONE_COUNT_COLUMN,
2401 "Graphics:", memInfo.getSummaryGraphics());
2402 printRow(pw, ONE_COUNT_COLUMN,
2403 "Private Other:", memInfo.getSummaryPrivateOther());
2404 printRow(pw, ONE_COUNT_COLUMN,
2405 "System:", memInfo.getSummarySystem());
2406 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002407 if (memInfo.hasSwappedOutPss) {
2408 printRow(pw, TWO_COUNT_COLUMNS,
2409 "TOTAL:", memInfo.getSummaryTotalPss(),
2410 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2411 } else {
2412 printRow(pw, TWO_COUNT_COLUMNS,
2413 "TOTAL:", memInfo.getSummaryTotalPss(),
2414 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2415 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002416 }
2417
Jeff Hamilton52d32032011-01-08 15:31:26 -06002418 public void registerOnActivityPausedListener(Activity activity,
2419 OnActivityPausedListener listener) {
2420 synchronized (mOnPauseListeners) {
2421 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2422 if (list == null) {
2423 list = new ArrayList<OnActivityPausedListener>();
2424 mOnPauseListeners.put(activity, list);
2425 }
2426 list.add(listener);
2427 }
2428 }
2429
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002430 public void unregisterOnActivityPausedListener(Activity activity,
2431 OnActivityPausedListener listener) {
2432 synchronized (mOnPauseListeners) {
2433 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2434 if (list != null) {
2435 list.remove(listener);
2436 }
2437 }
2438 }
2439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 public final ActivityInfo resolveActivityInfo(Intent intent) {
2441 ActivityInfo aInfo = intent.resolveActivityInfo(
2442 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2443 if (aInfo == null) {
2444 // Throw an exception.
2445 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002446 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 return aInfo;
2449 }
Bob Leee5408332009-09-04 18:31:17 -07002450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002453 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002454 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002456 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002457 r.intent = intent;
2458 r.state = state;
2459 r.parent = parent;
2460 r.embeddedID = id;
2461 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002462 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 if (localLOGV) {
2464 ComponentName compname = intent.getComponent();
2465 String name;
2466 if (compname != null) {
2467 name = compname.toShortString();
2468 } else {
2469 name = "(Intent " + intent + ").getComponent() returned null";
2470 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002471 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 + ", comp=" + name
2473 + ", token=" + token);
2474 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002475 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002476 }
2477
2478 public final Activity getActivity(IBinder token) {
2479 return mActivities.get(token).activity;
2480 }
2481
2482 public final void sendActivityResult(
2483 IBinder token, String id, int requestCode,
2484 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002485 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002486 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2488 list.add(new ResultInfo(id, requestCode, resultCode, data));
2489 mAppThread.scheduleSendResult(token, list);
2490 }
2491
Jeff Brown9ef09972013-10-15 20:49:59 -07002492 private void sendMessage(int what, Object obj) {
2493 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 }
2495
Jeff Brown9ef09972013-10-15 20:49:59 -07002496 private void sendMessage(int what, Object obj, int arg1) {
2497 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 }
2499
Jeff Brown9ef09972013-10-15 20:49:59 -07002500 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2501 sendMessage(what, obj, arg1, arg2, false);
2502 }
2503
2504 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2505 if (DEBUG_MESSAGES) Slog.v(
2506 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2507 + ": " + arg1 + " / " + obj);
2508 Message msg = Message.obtain();
2509 msg.what = what;
2510 msg.obj = obj;
2511 msg.arg1 = arg1;
2512 msg.arg2 = arg2;
2513 if (async) {
2514 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002516 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
2518
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002519 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2520 if (DEBUG_MESSAGES) Slog.v(
2521 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2522 "seq= " + seq);
2523 Message msg = Message.obtain();
2524 msg.what = what;
2525 SomeArgs args = SomeArgs.obtain();
2526 args.arg1 = obj;
2527 args.argi1 = arg1;
2528 args.argi2 = arg2;
2529 args.argi3 = seq;
2530 msg.obj = args;
2531 mH.sendMessage(msg);
2532 }
2533
Dianne Hackborn21556372010-02-04 16:34:40 -08002534 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 String what) {
2536 ContextCleanupInfo cci = new ContextCleanupInfo();
2537 cci.context = context;
2538 cci.who = who;
2539 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002540 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 }
2542
Craig Mautner233ceee2014-05-09 17:05:11 -07002543 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2545
2546 ActivityInfo aInfo = r.activityInfo;
2547 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002548 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 Context.CONTEXT_INCLUDE_CODE);
2550 }
Bob Leee5408332009-09-04 18:31:17 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 ComponentName component = r.intent.getComponent();
2553 if (component == null) {
2554 component = r.intent.resolveActivity(
2555 mInitialApplication.getPackageManager());
2556 r.intent.setComponent(component);
2557 }
2558
2559 if (r.activityInfo.targetActivity != null) {
2560 component = new ComponentName(r.activityInfo.packageName,
2561 r.activityInfo.targetActivity);
2562 }
2563
2564 Activity activity = null;
2565 try {
2566 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2567 activity = mInstrumentation.newActivity(
2568 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002569 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002571 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 if (r.state != null) {
2573 r.state.setClassLoader(cl);
2574 }
2575 } catch (Exception e) {
2576 if (!mInstrumentation.onException(activity, e)) {
2577 throw new RuntimeException(
2578 "Unable to instantiate activity " + component
2579 + ": " + e.toString(), e);
2580 }
2581 }
2582
2583 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002584 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002585
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002586 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2587 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 TAG, r + ": app=" + app
2589 + ", appName=" + app.getPackageName()
2590 + ", pkg=" + r.packageInfo.getPackageName()
2591 + ", comp=" + r.intent.getComponent().toShortString()
2592 + ", dir=" + r.packageInfo.getAppDir());
2593
2594 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002595 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002597 Configuration config = new Configuration(mCompatConfiguration);
Andrii Kuliand0ad9382016-04-18 20:54:20 -07002598 if (r.overrideConfig != null) {
2599 config.updateFrom(r.overrideConfig);
2600 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002601 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002602 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002603 Window window = null;
2604 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2605 window = r.mPendingRemoveWindow;
2606 r.mPendingRemoveWindow = null;
2607 r.mPendingRemoveWindowManager = null;
2608 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002609 activity.attach(appContext, this, getInstrumentation(), r.token,
2610 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002611 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002612 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002613
Christopher Tateb70f3df2009-04-07 16:07:59 -07002614 if (customIntent != null) {
2615 activity.mIntent = customIntent;
2616 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002617 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 activity.mStartedActivity = false;
2619 int theme = r.activityInfo.getThemeResource();
2620 if (theme != 0) {
2621 activity.setTheme(theme);
2622 }
2623
2624 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002625 if (r.isPersistable()) {
2626 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2627 } else {
2628 mInstrumentation.callActivityOnCreate(activity, r.state);
2629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002630 if (!activity.mCalled) {
2631 throw new SuperNotCalledException(
2632 "Activity " + r.intent.getComponent().toShortString() +
2633 " did not call through to super.onCreate()");
2634 }
2635 r.activity = activity;
2636 r.stopped = true;
2637 if (!r.activity.mFinished) {
2638 activity.performStart();
2639 r.stopped = false;
2640 }
2641 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002642 if (r.isPersistable()) {
2643 if (r.state != null || r.persistentState != null) {
2644 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2645 r.persistentState);
2646 }
2647 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002648 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2649 }
2650 }
2651 if (!r.activity.mFinished) {
2652 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002653 if (r.isPersistable()) {
2654 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2655 r.persistentState);
2656 } else {
2657 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002659 if (!activity.mCalled) {
2660 throw new SuperNotCalledException(
2661 "Activity " + r.intent.getComponent().toShortString() +
2662 " did not call through to super.onPostCreate()");
2663 }
2664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002665 }
2666 r.paused = true;
2667
2668 mActivities.put(r.token, r);
2669
2670 } catch (SuperNotCalledException e) {
2671 throw e;
2672
2673 } catch (Exception e) {
2674 if (!mInstrumentation.onException(activity, e)) {
2675 throw new RuntimeException(
2676 "Unable to start activity " + component
2677 + ": " + e.toString(), e);
2678 }
2679 }
2680
2681 return activity;
2682 }
2683
Wale Ogunwale7c726682015-02-06 17:34:28 -08002684 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2685 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002686 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002687 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002688 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002689 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002690 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002691
Wale Ogunwale7c726682015-02-06 17:34:28 -08002692 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski082614c2016-03-04 14:33:47 -08002693 this, r.packageInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002694 appContext.setOuterContext(activity);
2695 Context baseContext = appContext;
2696
2697 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002698 // For debugging purposes, if the activity's package name contains the value of
2699 // the "debug.use-second-display" system property as a substring, then show
2700 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002701 String pkgName = SystemProperties.get("debug.second-display.pkg");
2702 if (pkgName != null && !pkgName.isEmpty()
2703 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002704 for (int id : dm.getDisplayIds()) {
2705 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002706 Display display =
2707 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002708 baseContext = appContext.createDisplayContext(display);
2709 break;
2710 }
2711 }
2712 }
2713 return baseContext;
2714 }
2715
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002716 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 // If we are getting ready to gc after going to the background, well
2718 // we are back active so skip it.
2719 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002720 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721
Jeff Hao1b012d32014-08-20 10:35:34 -07002722 if (r.profilerInfo != null) {
2723 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002724 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002725 }
2726
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002727 // Make sure we are running with the most recent config.
2728 handleConfigurationChanged(null, null);
2729
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002730 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002732
Chet Haase0d1c27a2014-11-03 18:35:16 +00002733 // Initialize before creating the activity
2734 WindowManagerGlobal.initialize();
2735
Craig Mautner233ceee2014-05-09 17:05:11 -07002736 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737
2738 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002739 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002740 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002741 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002742 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002743 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744
2745 if (!r.activity.mFinished && r.startsNotResumed) {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002746 // The activity manager actually wants this one to start out paused, because it
2747 // needs to be visible but isn't in the foreground. We accomplish this by going
2748 // through the normal startup (because activities expect to go through onResume()
2749 // the first time they run, before their window is displayed), and then pausing it.
2750 // However, in this case we do -not- need to do the full pause cycle (of freezing
2751 // and such) because the activity manager assumes it can just retain the current
2752 // state it has.
2753 performPauseActivityIfNeeded(r, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002755 // We need to keep around the original state, in case we need to be created again.
2756 // But we only do this for pre-Honeycomb apps, which always save their state when
2757 // pausing, so we can not have them save their state when restarting from a paused
2758 // state. For HC and later, we want to (and can) let the state be saved as the
2759 // normal part of stopping the activity.
2760 if (r.isPreHoneycomb()) {
2761 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 }
2764 } else {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002765 // 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 -08002766 try {
2767 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002768 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2769 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002771 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 }
2773 }
2774 }
2775
Filip Gruszczynski23493322015-07-29 17:02:59 -07002776 private void reportSizeConfigurations(ActivityClientRecord r) {
2777 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2778 if (configurations == null) {
2779 return;
2780 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002781 SparseIntArray horizontal = new SparseIntArray();
2782 SparseIntArray vertical = new SparseIntArray();
2783 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002784 for (int i = configurations.length - 1; i >= 0; i--) {
2785 Configuration config = configurations[i];
2786 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002787 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002788 }
2789 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002790 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002791 }
2792 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002793 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002794 }
2795 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002796 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002797 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2798 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002799 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002800 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002801 }
2802
2803 }
2804
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002805 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 final int N = intents.size();
2807 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002808 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002809 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002810 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002811 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2813 }
2814 }
2815
Wale Ogunwale826c7062016-09-13 08:25:54 -07002816 void performNewIntents(IBinder token, List<ReferrerIntent> intents, boolean andPause) {
2817 final ActivityClientRecord r = mActivities.get(token);
2818 if (r == null) {
2819 return;
2820 }
2821
2822 final boolean resumed = !r.paused;
2823 if (resumed) {
2824 r.activity.mTemporaryPause = true;
2825 mInstrumentation.callActivityOnPause(r.activity);
2826 }
2827 deliverNewIntents(r, intents);
2828 if (resumed) {
2829 r.activity.performResume();
2830 r.activity.mTemporaryPause = false;
2831 }
2832
2833 if (r.paused && andPause) {
2834 // In this case the activity was in the paused state when we delivered the intent,
2835 // to guarantee onResume gets called after onNewIntent we temporarily resume the
2836 // activity and pause again as the caller wanted.
2837 performResumeActivity(token, false, "performNewIntents");
2838 performPauseActivityIfNeeded(r, "performNewIntents");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 }
2840 }
Bob Leee5408332009-09-04 18:31:17 -07002841
Romain Guy65b345f2011-07-27 18:51:50 -07002842 private void handleNewIntent(NewIntentData data) {
Wale Ogunwale826c7062016-09-13 08:25:54 -07002843 performNewIntents(data.token, data.intents, data.andPause);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 }
2845
Adam Skorydfc7fd72013-08-05 19:23:41 -07002846 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Amith Yamasani4f128e42016-05-10 11:44:12 -07002847 if (mLastSessionId != cmd.sessionId) {
2848 // Clear the existing structures
2849 mLastSessionId = cmd.sessionId;
2850 for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
2851 AssistStructure structure = mLastAssistStructures.get(i).get();
2852 if (structure != null) {
2853 structure.clearSendChannel();
2854 }
2855 mLastAssistStructures.remove(i);
Dianne Hackborn782d4982015-07-08 17:36:37 -07002856 }
2857 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002858 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002859 AssistStructure structure = null;
2860 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002861 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002862 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002863 if (r != null) {
2864 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2865 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002866 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002867 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002868 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002869 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002870 if (activityIntent != null && (r.window == null ||
2871 (r.window.getAttributes().flags
2872 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002873 Intent intent = new Intent(activityIntent);
2874 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2875 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2876 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002877 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002878 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002879 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002880 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002881 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002882 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002883 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002884 if (structure == null) {
2885 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002886 }
Amith Yamasani4f128e42016-05-10 11:44:12 -07002887 mLastAssistStructures.add(new WeakReference<>(structure));
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002888 IActivityManager mgr = ActivityManagerNative.getDefault();
2889 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002890 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002891 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002892 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002893 }
2894 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002895
2896 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2897 ActivityClientRecord r = mActivities.get(token);
2898 if (r != null) {
2899 r.activity.onTranslucentConversionComplete(drawComplete);
2900 }
2901 }
2902
Craig Mautnereb8abf72014-07-02 15:04:09 -07002903 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2904 ActivityClientRecord r = mActivities.get(token);
2905 if (r != null) {
2906 r.activity.onNewActivityOptions(options);
2907 }
2908 }
2909
Jose Lima4b6c6692014-08-12 17:41:12 -07002910 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002911 ActivityClientRecord r = mActivities.get(token);
2912 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002913 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002914 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002915 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002916 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002917 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002918 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2919 if (!activity.mCalled) {
2920 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002921 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002922 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002923 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002924 }
2925 }
2926 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002927 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002928 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002929 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002930 }
2931 }
2932
Jose Lima4b6c6692014-08-12 17:41:12 -07002933 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002934 ActivityClientRecord r = mActivities.get(token);
2935 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002936 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002937 }
2938 }
2939
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002940 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002941 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2942 try {
2943 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2944 } finally {
2945 StrictMode.setThreadPolicy(oldPolicy);
2946 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002947 }
2948
Craig Mautner8746a472014-07-24 15:12:54 -07002949 private void handleEnterAnimationComplete(IBinder token) {
2950 ActivityClientRecord r = mActivities.get(token);
2951 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002952 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002953 }
2954 }
2955
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002956 private void handleStartBinderTracking() {
2957 Binder.enableTracing();
2958 }
2959
2960 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2961 try {
2962 Binder.disableTracing();
2963 Binder.getTransactionTracker().writeTracesToFile(fd);
2964 } finally {
2965 IoUtils.closeQuietly(fd);
2966 Binder.getTransactionTracker().clearTraces();
2967 }
2968 }
2969
Andrii Kulian933076d2016-03-29 17:04:42 -07002970 private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002971 final ActivityClientRecord r = mActivities.get(token);
2972 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002973 r.activity.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002974 }
2975 }
2976
Andrii Kulian933076d2016-03-29 17:04:42 -07002977 private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002978 final ActivityClientRecord r = mActivities.get(token);
2979 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002980 r.activity.dispatchPictureInPictureModeChanged(isInPipMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002981 }
2982 }
2983
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002984 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2985 final ActivityClientRecord r = mActivities.get(token);
2986 if (r != null) {
2987 r.voiceInteractor = interactor;
2988 r.activity.setVoiceInteractor(interactor);
2989 if (interactor == null) {
2990 r.activity.onLocalVoiceInteractionStopped();
2991 } else {
2992 r.activity.onLocalVoiceInteractionStarted();
2993 }
2994 }
2995 }
2996
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002997 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2998
2999 /**
3000 * Return the Intent that's currently being handled by a
3001 * BroadcastReceiver on this thread, or null if none.
3002 * @hide
3003 */
3004 public static Intent getIntentBeingBroadcast() {
3005 return sCurrentBroadcastIntent.get();
3006 }
3007
Romain Guy65b345f2011-07-27 18:51:50 -07003008 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 // If we are getting ready to gc after going to the background, well
3010 // we are back active so skip it.
3011 unscheduleGcIdler();
3012
3013 String component = data.intent.getComponent().getClassName();
3014
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003015 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003016 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017
3018 IActivityManager mgr = ActivityManagerNative.getDefault();
3019
Romain Guy65b345f2011-07-27 18:51:50 -07003020 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003021 try {
3022 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3023 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003024 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07003025 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
3027 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003028 if (DEBUG_BROADCAST) Slog.i(TAG,
3029 "Finishing failed broadcast to " + data.intent.getComponent());
3030 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 throw new RuntimeException(
3032 "Unable to instantiate receiver " + component
3033 + ": " + e.toString(), e);
3034 }
3035
3036 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003037 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07003038
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003039 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003040 TAG, "Performing receive of " + data.intent
3041 + ": app=" + app
3042 + ", appName=" + app.getPackageName()
3043 + ", pkg=" + packageInfo.getPackageName()
3044 + ", comp=" + data.intent.getComponent().toShortString()
3045 + ", dir=" + packageInfo.getAppDir());
3046
Dianne Hackborn21556372010-02-04 16:34:40 -08003047 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003048 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07003049 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003050 receiver.onReceive(context.getReceiverRestrictedContext(),
3051 data.intent);
3052 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003053 if (DEBUG_BROADCAST) Slog.i(TAG,
3054 "Finishing failed broadcast to " + data.intent.getComponent());
3055 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003056 if (!mInstrumentation.onException(receiver, e)) {
3057 throw new RuntimeException(
3058 "Unable to start receiver " + component
3059 + ": " + e.toString(), e);
3060 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003061 } finally {
3062 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 }
3064
Dianne Hackborne829fef2010-10-26 17:44:01 -07003065 if (receiver.getPendingResult() != null) {
3066 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
3068 }
3069
Christopher Tate181fafa2009-05-14 11:12:14 -07003070 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003071 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003072 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003073
Christopher Tate346acb12012-10-15 19:20:25 -07003074 // Sanity check the requested target package's uid against ours
3075 try {
3076 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3077 data.appInfo.packageName, 0, UserHandle.myUserId());
3078 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3079 Slog.w(TAG, "Asked to instantiate non-matching package "
3080 + data.appInfo.packageName);
3081 return;
3082 }
3083 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003084 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003085 }
3086
Christopher Tate181fafa2009-05-14 11:12:14 -07003087 // no longer idle; we have backup work to do
3088 unscheduleGcIdler();
3089
3090 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003091 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003092 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003093 if (packageName == null) {
3094 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3095 return;
3096 }
3097
Christopher Tate181fafa2009-05-14 11:12:14 -07003098 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003099 // full backup operation but no app-supplied agent? use the default implementation
3100 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3101 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003102 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003103 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003104
Christopher Tate181fafa2009-05-14 11:12:14 -07003105 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003106 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003107 BackupAgent agent = mBackupAgents.get(packageName);
3108 if (agent != null) {
3109 // reusing the existing instance
3110 if (DEBUG_BACKUP) {
3111 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003112 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003113 binder = agent.onBind();
3114 } else {
3115 try {
3116 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3117
3118 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3119 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3120
3121 // set up the agent's context
3122 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3123 context.setOuterContext(agent);
3124 agent.attach(context);
3125
3126 agent.onCreate();
3127 binder = agent.onBind();
3128 mBackupAgents.put(packageName, agent);
3129 } catch (Exception e) {
3130 // If this is during restore, fail silently; otherwise go
3131 // ahead and let the user see the crash.
3132 Slog.e(TAG, "Agent threw during creation: " + e);
3133 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3134 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3135 throw e;
3136 }
3137 // falling through with 'binder' still null
3138 }
Christopher Tated1475e02009-07-09 15:36:17 -07003139 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003140
3141 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003142 try {
3143 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3144 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003145 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003146 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003147 } catch (Exception e) {
3148 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003149 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003150 }
3151 }
3152
3153 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003154 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003155 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003156
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003157 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003158 String packageName = packageInfo.mPackageName;
3159 BackupAgent agent = mBackupAgents.get(packageName);
3160 if (agent != null) {
3161 try {
3162 agent.onDestroy();
3163 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003164 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003165 e.printStackTrace();
3166 }
3167 mBackupAgents.remove(packageName);
3168 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003169 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003170 }
3171 }
3172
Romain Guy65b345f2011-07-27 18:51:50 -07003173 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 // If we are getting ready to gc after going to the background, well
3175 // we are back active so skip it.
3176 unscheduleGcIdler();
3177
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003178 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003179 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003180 Service service = null;
3181 try {
3182 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3183 service = (Service) cl.loadClass(data.info.name).newInstance();
3184 } catch (Exception e) {
3185 if (!mInstrumentation.onException(service, e)) {
3186 throw new RuntimeException(
3187 "Unable to instantiate service " + data.info.name
3188 + ": " + e.toString(), e);
3189 }
3190 }
3191
3192 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003193 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194
Jeff Browndefd4a62014-03-10 21:24:37 -07003195 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3196 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003198 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 service.attach(context, this, data.info.name, data.token, app,
3200 ActivityManagerNative.getDefault());
3201 service.onCreate();
3202 mServices.put(data.token, service);
3203 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003204 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003205 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003207 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
3209 } catch (Exception e) {
3210 if (!mInstrumentation.onException(service, e)) {
3211 throw new RuntimeException(
3212 "Unable to create service " + data.info.name
3213 + ": " + e.toString(), e);
3214 }
3215 }
3216 }
3217
Romain Guy65b345f2011-07-27 18:51:50 -07003218 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003220 if (DEBUG_SERVICE)
3221 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 if (s != null) {
3223 try {
3224 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003225 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 try {
3227 if (!data.rebind) {
3228 IBinder binder = s.onBind(data.intent);
3229 ActivityManagerNative.getDefault().publishService(
3230 data.token, data.intent, binder);
3231 } else {
3232 s.onRebind(data.intent);
3233 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003234 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003236 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003238 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 }
3240 } catch (Exception e) {
3241 if (!mInstrumentation.onException(s, e)) {
3242 throw new RuntimeException(
3243 "Unable to bind to service " + s
3244 + " with " + data.intent + ": " + e.toString(), e);
3245 }
3246 }
3247 }
3248 }
3249
Romain Guy65b345f2011-07-27 18:51:50 -07003250 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 Service s = mServices.get(data.token);
3252 if (s != null) {
3253 try {
3254 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003255 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 boolean doRebind = s.onUnbind(data.intent);
3257 try {
3258 if (doRebind) {
3259 ActivityManagerNative.getDefault().unbindFinished(
3260 data.token, data.intent, doRebind);
3261 } else {
3262 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003263 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 }
3265 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003266 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268 } catch (Exception e) {
3269 if (!mInstrumentation.onException(s, e)) {
3270 throw new RuntimeException(
3271 "Unable to unbind to service " + s
3272 + " with " + data.intent + ": " + e.toString(), e);
3273 }
3274 }
3275 }
3276 }
3277
Dianne Hackborn625ac272010-09-17 18:29:22 -07003278 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003279 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3280 try {
3281 Service s = mServices.get(info.token);
3282 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003283 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3284 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003285 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3286 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003288 } finally {
3289 IoUtils.closeQuietly(info.fd);
3290 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003291 }
3292 }
3293
Dianne Hackborn625ac272010-09-17 18:29:22 -07003294 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003295 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3296 try {
3297 ActivityClientRecord r = mActivities.get(info.token);
3298 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003299 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3300 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003301 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3302 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003303 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003304 } finally {
3305 IoUtils.closeQuietly(info.fd);
3306 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003307 }
3308 }
3309
Marco Nelissen18cb2872011-11-15 11:19:53 -08003310 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003311 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3312 try {
3313 ProviderClientRecord r = mLocalProviders.get(info.token);
3314 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003315 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3316 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003317 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3318 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003319 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003320 } finally {
3321 IoUtils.closeQuietly(info.fd);
3322 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003323 }
3324 }
3325
Romain Guy65b345f2011-07-27 18:51:50 -07003326 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 Service s = mServices.get(data.token);
3328 if (s != null) {
3329 try {
3330 if (data.args != null) {
3331 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003332 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003334 int res;
3335 if (!data.taskRemoved) {
3336 res = s.onStartCommand(data.args, data.flags, data.startId);
3337 } else {
3338 s.onTaskRemoved(data.args);
3339 res = Service.START_TASK_REMOVED_COMPLETE;
3340 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003341
3342 QueuedWork.waitToFinish();
3343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003345 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003346 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003348 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003350 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 } catch (Exception e) {
3352 if (!mInstrumentation.onException(s, e)) {
3353 throw new RuntimeException(
3354 "Unable to start service " + s
3355 + " with " + data.args + ": " + e.toString(), e);
3356 }
3357 }
3358 }
3359 }
3360
Romain Guy65b345f2011-07-27 18:51:50 -07003361 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 Service s = mServices.remove(token);
3363 if (s != null) {
3364 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003365 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 s.onDestroy();
3367 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003368 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003370 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003372
3373 QueuedWork.waitToFinish();
3374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003376 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003377 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003379 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 }
3381 } catch (Exception e) {
3382 if (!mInstrumentation.onException(s, e)) {
3383 throw new RuntimeException(
3384 "Unable to stop service " + s
3385 + ": " + e.toString(), e);
3386 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003387 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003389 } else {
3390 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003392 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 }
3394
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003395 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003396 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003397 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003398 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 + " finished=" + r.activity.mFinished);
3400 if (r != null && !r.activity.mFinished) {
3401 if (clearHide) {
3402 r.hideForNow = false;
3403 r.activity.mStartedActivity = false;
3404 }
3405 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003406 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003407 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 if (r.pendingIntents != null) {
3409 deliverNewIntents(r, r.pendingIntents);
3410 r.pendingIntents = null;
3411 }
3412 if (r.pendingResults != null) {
3413 deliverResults(r, r.pendingResults);
3414 r.pendingResults = null;
3415 }
3416 r.activity.performResume();
3417
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003418 // If there is a pending local relaunch that was requested when the activity was
3419 // paused, it will put the activity into paused state when it finally happens.
3420 // Since the activity resumed before being relaunched, we don't want that to happen,
3421 // so we need to clear the request to relaunch paused.
3422 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3423 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3424 if (relaunching.token == r.token
3425 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3426 relaunching.startsNotResumed = false;
3427 }
3428 }
3429
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003430 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3431 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003432
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 r.paused = false;
3434 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003436 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437 } catch (Exception e) {
3438 if (!mInstrumentation.onException(r.activity, e)) {
3439 throw new RuntimeException(
3440 "Unable to resume activity "
3441 + r.intent.getComponent().toShortString()
3442 + ": " + e.toString(), e);
3443 }
3444 }
3445 }
3446 return r;
3447 }
3448
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003449 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3450 if (r.mPreserveWindow && !force) {
3451 return;
3452 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003453 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003454 r.mPendingRemoveWindowManager.removeViewImmediate(
3455 r.mPendingRemoveWindow.getDecorView());
3456 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003457 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003458 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003459 r.activity.getClass().getName(), "Activity");
3460 }
3461 }
3462 r.mPendingRemoveWindow = null;
3463 r.mPendingRemoveWindowManager = null;
3464 }
3465
Craig Mautner233ceee2014-05-09 17:05:11 -07003466 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003467 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003468 ActivityClientRecord r = mActivities.get(token);
3469 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3470 return;
3471 }
3472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003473 // If we are getting ready to gc after going to the background, well
3474 // we are back active so skip it.
3475 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003476 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477
Adam Powellcfbe9be2013-11-06 14:58:58 -08003478 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003479 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003480
3481 if (r != null) {
3482 final Activity a = r.activity;
3483
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003484 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 TAG, "Resume " + r + " started activity: " +
3486 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3487 + ", finished: " + a.mFinished);
3488
3489 final int forwardBit = isForward ?
3490 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003492 // If the window hasn't yet been added to the window manager,
3493 // and this guy didn't finish itself or start another activity,
3494 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003495 boolean willBeVisible = !a.mStartedActivity;
3496 if (!willBeVisible) {
3497 try {
3498 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3499 a.getActivityToken());
3500 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003501 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003502 }
3503 }
3504 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003505 r.window = r.activity.getWindow();
3506 View decor = r.window.getDecorView();
3507 decor.setVisibility(View.INVISIBLE);
3508 ViewManager wm = a.getWindowManager();
3509 WindowManager.LayoutParams l = r.window.getAttributes();
3510 a.mDecor = decor;
3511 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3512 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003513 if (r.mPreserveWindow) {
3514 a.mWindowAdded = true;
3515 r.mPreserveWindow = false;
Robert Carrb2594852016-04-25 16:21:13 -07003516 // Normally the ViewRoot sets up callbacks with the Activity
3517 // in addView->ViewRootImpl#setView. If we are instead reusing
3518 // the decor view we have to notify the view root that the
3519 // callbacks may have changed.
3520 ViewRootImpl impl = decor.getViewRootImpl();
Robert Carr311ecba2016-04-27 12:59:45 -07003521 if (impl != null) {
3522 impl.notifyChildRebuilt();
3523 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003524 }
3525 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 a.mWindowAdded = true;
3527 wm.addView(decor, l);
3528 }
3529
3530 // If the window has already been added, but during resume
3531 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003532 // window visible.
3533 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003534 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 TAG, "Launch " + r + " mStartedActivity set");
3536 r.hideForNow = true;
3537 }
3538
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003539 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003540 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003542 // The window is now visible if it has been added, we are not
3543 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003544 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003545 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003547 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003548 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003549 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 r.newConfig = null;
3551 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003552 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 + isForward);
3554 WindowManager.LayoutParams l = r.window.getAttributes();
3555 if ((l.softInputMode
3556 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3557 != forwardBit) {
3558 l.softInputMode = (l.softInputMode
3559 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3560 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003561 if (r.activity.mVisibleFromClient) {
3562 ViewManager wm = a.getWindowManager();
3563 View decor = r.window.getDecorView();
3564 wm.updateViewLayout(decor, l);
3565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003566 }
3567 r.activity.mVisibleFromServer = true;
3568 mNumVisibleActivities++;
3569 if (r.activity.mVisibleFromClient) {
3570 r.activity.makeVisible();
3571 }
3572 }
3573
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003574 if (!r.onlyLocalRequest) {
3575 r.nextIdle = mNewActivities;
3576 mNewActivities = r;
3577 if (localLOGV) Slog.v(
3578 TAG, "Scheduling idle handler for " + r);
3579 Looper.myQueue().addIdleHandler(new Idler());
3580 }
3581 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003582
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003583 // Tell the activity manager we have resumed.
3584 if (reallyResume) {
3585 try {
3586 ActivityManagerNative.getDefault().activityResumed(token);
3587 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003588 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003589 }
3590 }
3591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 } else {
3593 // If an exception was thrown when trying to resume, then
3594 // just end this activity.
3595 try {
3596 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003597 .finishActivity(token, Activity.RESULT_CANCELED, null,
3598 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003600 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 }
3602 }
3603 }
3604
3605 private int mThumbnailWidth = -1;
3606 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003607 private Bitmap mAvailThumbnailBitmap = null;
3608 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609
Romain Guy65b345f2011-07-27 18:51:50 -07003610 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003611 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003613 if (thumbnail == null) {
3614 int w = mThumbnailWidth;
3615 int h;
3616 if (w < 0) {
3617 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003618 int wId = com.android.internal.R.dimen.thumbnail_width;
3619 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003620 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3621 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003622 } else {
3623 h = mThumbnailHeight;
3624 }
3625
3626 // On platforms where we don't want thumbnails, set dims to (0,0)
3627 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003628 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3629 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003630 thumbnail.eraseColor(0);
3631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 }
3633
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003634 if (thumbnail != null) {
3635 Canvas cv = mThumbnailCanvas;
3636 if (cv == null) {
3637 mThumbnailCanvas = cv = new Canvas();
3638 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003639
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003640 cv.setBitmap(thumbnail);
3641 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3642 mAvailThumbnailBitmap = thumbnail;
3643 thumbnail = null;
3644 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003645 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003648 } catch (Exception e) {
3649 if (!mInstrumentation.onException(r.activity, e)) {
3650 throw new RuntimeException(
3651 "Unable to create thumbnail of "
3652 + r.intent.getComponent().toShortString()
3653 + ": " + e.toString(), e);
3654 }
3655 thumbnail = null;
3656 }
3657
3658 return thumbnail;
3659 }
3660
Romain Guy65b345f2011-07-27 18:51:50 -07003661 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003662 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003663 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003664 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003665 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3666 return;
3667 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003669 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 if (userLeaving) {
3671 performUserLeavingActivity(r);
3672 }
Bob Leee5408332009-09-04 18:31:17 -07003673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003675 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003677 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003678 if (r.isPreHoneycomb()) {
3679 QueuedWork.waitToFinish();
3680 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003683 if (!dontReport) {
3684 try {
3685 ActivityManagerNative.getDefault().activityPaused(token);
3686 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003687 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003688 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003690 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003691 }
3692 }
3693
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003694 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 mInstrumentation.callActivityOnUserLeaving(r.activity);
3696 }
3697
3698 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003699 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003700 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003701 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 }
3703
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003704 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003705 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003706 if (r.paused) {
3707 if (r.activity.mFinished) {
3708 // If we are finishing, we won't call onResume() in certain cases.
3709 // So here we likewise don't want to call onPause() if the activity
3710 // isn't resumed.
3711 return null;
3712 }
3713 RuntimeException e = new RuntimeException(
3714 "Performing pause of activity that is not resumed: "
3715 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003716 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 if (finished) {
3719 r.activity.mFinished = true;
3720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003722 // Next have the activity save its current state and managed dialogs...
3723 if (!r.activity.mFinished && saveState) {
3724 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003726
3727 performPauseActivityIfNeeded(r, reason);
Jeff Hamilton52d32032011-01-08 15:31:26 -06003728
3729 // Notify any outstanding on paused listeners
3730 ArrayList<OnActivityPausedListener> listeners;
3731 synchronized (mOnPauseListeners) {
3732 listeners = mOnPauseListeners.remove(r.activity);
3733 }
3734 int size = (listeners != null ? listeners.size() : 0);
3735 for (int i = 0; i < size; i++) {
3736 listeners.get(i).onPaused(r.activity);
3737 }
3738
Craig Mautnera0026042014-04-23 11:45:37 -07003739 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003740 }
3741
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003742 private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
3743 if (r.paused) {
3744 // You are already paused silly...
3745 return;
3746 }
3747
3748 try {
3749 r.activity.mCalled = false;
3750 mInstrumentation.callActivityOnPause(r.activity);
3751 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
3752 r.activity.getComponentName().getClassName(), reason);
3753 if (!r.activity.mCalled) {
3754 throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
3755 + " did not call through to super.onPause()");
3756 }
3757 } catch (SuperNotCalledException e) {
3758 throw e;
3759 } catch (Exception e) {
3760 if (!mInstrumentation.onException(r.activity, e)) {
3761 throw new RuntimeException("Unable to pause activity "
3762 + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
3763 }
3764 }
3765 r.paused = true;
3766 }
3767
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003768 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003769 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003770 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003771 }
3772
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003773 private static class StopInfo implements Runnable {
3774 ActivityClientRecord activity;
3775 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003776 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003778
3779 @Override public void run() {
3780 // Tell activity manager we have been stopped.
3781 try {
3782 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3783 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003784 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003785 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003786 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003787 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003788 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003789 return;
3790 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003791 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003792 }
3793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 }
3795
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003796 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003797 public final IActivityManager.ContentProviderHolder holder;
3798 public final ProviderClientRecord client;
3799 public int stableCount;
3800 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003801
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003802 // When this is set, the stable and unstable ref counts are 0 and
3803 // we have a pending operation scheduled to remove the ref count
3804 // from the activity manager. On the activity manager we are still
3805 // holding an unstable ref, though it is not reflected in the counts
3806 // here.
3807 public boolean removePending;
3808
3809 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3810 ProviderClientRecord inClient, int sCount, int uCount) {
3811 holder = inHolder;
3812 client = inClient;
3813 stableCount = sCount;
3814 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 }
3816 }
3817
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003818 /**
3819 * Core implementation of stopping an activity. Note this is a little
3820 * tricky because the server's meaning of stop is slightly different
3821 * than our client -- for the server, stop means to save state and give
3822 * it the result when it is done, but the window may still be visible.
3823 * For the client, we want to call onStop()/onStart() to indicate when
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07003824 * the activity's UI visibility changes.
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003825 */
Romain Guy65b345f2011-07-27 18:51:50 -07003826 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003827 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003828 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829 if (r != null) {
3830 if (!keepShown && r.stopped) {
3831 if (r.activity.mFinished) {
3832 // If we are finishing, we won't call onResume() in certain
3833 // cases. So here we likewise don't want to call onStop()
3834 // if the activity isn't resumed.
3835 return;
3836 }
3837 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07003838 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003840 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07003841 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 }
3843
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003844 // One must first be paused before stopped...
3845 performPauseActivityIfNeeded(r, reason);
3846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 if (info != null) {
3848 try {
3849 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003850 // For now, don't create the thumbnail here; we are
3851 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 info.description = r.activity.onCreateDescription();
3853 } catch (Exception e) {
3854 if (!mInstrumentation.onException(r.activity, e)) {
3855 throw new RuntimeException(
3856 "Unable to save state of activity "
3857 + r.intent.getComponent().toShortString()
3858 + ": " + e.toString(), e);
3859 }
3860 }
3861 }
3862
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003863 // Next have the activity save its current state and managed dialogs...
3864 if (!r.activity.mFinished && saveState) {
3865 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003866 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003867 }
3868 }
3869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003870 if (!keepShown) {
3871 try {
3872 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003873 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003874 } catch (Exception e) {
3875 if (!mInstrumentation.onException(r.activity, e)) {
3876 throw new RuntimeException(
3877 "Unable to stop activity "
3878 + r.intent.getComponent().toShortString()
3879 + ": " + e.toString(), e);
3880 }
3881 }
3882 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003883 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3884 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003886 }
3887 }
3888
Romain Guy65b345f2011-07-27 18:51:50 -07003889 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 View v = r.activity.mDecor;
3891 if (v != null) {
3892 if (show) {
3893 if (!r.activity.mVisibleFromServer) {
3894 r.activity.mVisibleFromServer = true;
3895 mNumVisibleActivities++;
3896 if (r.activity.mVisibleFromClient) {
3897 r.activity.makeVisible();
3898 }
3899 }
3900 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003901 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003902 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08003903 + r.activityInfo.name + " with new config "
3904 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 r.newConfig = null;
3906 }
3907 } else {
3908 if (r.activity.mVisibleFromServer) {
3909 r.activity.mVisibleFromServer = false;
3910 mNumVisibleActivities--;
3911 v.setVisibility(View.INVISIBLE);
3912 }
3913 }
3914 }
3915 }
3916
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003917 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003918 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003919 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3920 return;
3921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 r.activity.mConfigChangeFlags |= configChanges;
3923
3924 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003925 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003927 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 TAG, "Finishing stop of " + r + ": show=" + show
3929 + " win=" + r.window);
3930
3931 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003932
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003933 // Make sure any pending writes are now committed.
3934 if (!r.isPreHoneycomb()) {
3935 QueuedWork.waitToFinish();
3936 }
3937
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003938 // Schedule the call to tell the activity manager we have
3939 // stopped. We don't do this immediately, because we want to
3940 // have a chance for any other pending work (in particular memory
3941 // trim requests) to complete before you tell the activity
3942 // manager to proceed and allow us to go fully into the background.
3943 info.activity = r;
3944 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003945 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003946 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003947 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 }
3949
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003950 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3951 String action) {
3952 if (r == null) {
3953 return true;
3954 }
3955 if (seq < r.lastProcessedSeq) {
3956 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3957 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3958 return false;
3959 }
3960 r.lastProcessedSeq = seq;
3961 return true;
3962 }
3963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003964 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003965 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 if (r.stopped) {
3967 r.activity.performRestart();
3968 r.stopped = false;
3969 }
3970 }
3971
Romain Guy65b345f2011-07-27 18:51:50 -07003972 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003973 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003974
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003975 if (r == null) {
3976 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3977 return;
3978 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003981 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003982 } else if (show && r.stopped) {
3983 // If we are getting ready to gc after going to the background, well
3984 // we are back active so skip it.
3985 unscheduleGcIdler();
3986
3987 r.activity.performRestart();
3988 r.stopped = false;
3989 }
3990 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003991 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 TAG, "Handle window " + r + " visibility: " + show);
3993 updateVisibility(r, show);
3994 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003995 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 }
3997
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07003998 // TODO: This method should be changed to use {@link #performStopActivityInner} to perform to
3999 // stop operation on the activity to reduce code duplication and the chance of fixing a bug in
4000 // one place and missing the other.
Romain Guy65b345f2011-07-27 18:51:50 -07004001 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004002 ActivityClientRecord r = mActivities.get(token);
4003
4004 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004005 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004006 return;
4007 }
4008
4009 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004010 if (!r.stopped && !r.isPreHoneycomb()) {
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004011 if (!r.activity.mFinished && r.state == null) {
4012 callCallActivityOnSaveInstanceState(r);
4013 }
4014
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004015 try {
4016 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004017 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004018 } catch (Exception e) {
4019 if (!mInstrumentation.onException(r.activity, e)) {
4020 throw new RuntimeException(
4021 "Unable to stop activity "
4022 + r.intent.getComponent().toShortString()
4023 + ": " + e.toString(), e);
4024 }
4025 }
4026 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004027 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4028 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004029 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004030
4031 // Make sure any pending writes are now committed.
4032 if (!r.isPreHoneycomb()) {
4033 QueuedWork.waitToFinish();
4034 }
4035
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004036 // Tell activity manager we slept.
4037 try {
4038 ActivityManagerNative.getDefault().activitySlept(r.token);
4039 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004040 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004041 }
4042 } else {
4043 if (r.stopped && r.activity.mVisibleFromServer) {
4044 r.activity.performRestart();
4045 r.stopped = false;
4046 }
4047 }
4048 }
4049
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004050 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07004051 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004052 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004053 }
Jon Miranda836c0a82014-08-11 12:32:26 -07004054 onCoreSettingsChange();
4055 }
4056
4057 private void onCoreSettingsChange() {
4058 boolean debugViewAttributes =
4059 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4060 if (debugViewAttributes != View.mDebugViewAttributes) {
4061 View.mDebugViewAttributes = debugViewAttributes;
4062
4063 // request all activities to relaunch for the changes to take place
4064 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004065 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
4066 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07004067 }
4068 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004069 }
4070
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004071 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4072 LoadedApk apk = peekPackageInfo(data.pkg, false);
4073 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004074 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004075 }
4076 apk = peekPackageInfo(data.pkg, true);
4077 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004078 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004079 }
4080 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004081 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004082 }
4083
Romain Guy65b345f2011-07-27 18:51:50 -07004084 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 final int N = results.size();
4086 for (int i=0; i<N; i++) {
4087 ResultInfo ri = results.get(i);
4088 try {
4089 if (ri.mData != null) {
4090 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004091 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004093 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004094 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 r.activity.dispatchActivityResult(ri.mResultWho,
4096 ri.mRequestCode, ri.mResultCode, ri.mData);
4097 } catch (Exception e) {
4098 if (!mInstrumentation.onException(r.activity, e)) {
4099 throw new RuntimeException(
4100 "Failure delivering result " + ri + " to activity "
4101 + r.intent.getComponent().toShortString()
4102 + ": " + e.toString(), e);
4103 }
4104 }
4105 }
4106 }
4107
Romain Guy65b345f2011-07-27 18:51:50 -07004108 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004109 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004110 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004111 if (r != null) {
4112 final boolean resumed = !r.paused;
4113 if (!r.activity.mFinished && r.activity.mDecor != null
4114 && r.hideForNow && resumed) {
4115 // We had hidden the activity because it started another
4116 // one... we have gotten a result back and we are not
4117 // paused, so make sure our window is visible.
4118 updateVisibility(r, true);
4119 }
4120 if (resumed) {
4121 try {
4122 // Now we are idle.
4123 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004124 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 mInstrumentation.callActivityOnPause(r.activity);
4126 if (!r.activity.mCalled) {
4127 throw new SuperNotCalledException(
4128 "Activity " + r.intent.getComponent().toShortString()
4129 + " did not call through to super.onPause()");
4130 }
4131 } catch (SuperNotCalledException e) {
4132 throw e;
4133 } catch (Exception e) {
4134 if (!mInstrumentation.onException(r.activity, e)) {
4135 throw new RuntimeException(
4136 "Unable to pause activity "
4137 + r.intent.getComponent().toShortString()
4138 + ": " + e.toString(), e);
4139 }
4140 }
4141 }
4142 deliverResults(r, res.results);
4143 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004144 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004145 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 }
4147 }
4148 }
4149
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004150 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 return performDestroyActivity(token, finishing, 0, false);
4152 }
4153
Romain Guy65b345f2011-07-27 18:51:50 -07004154 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004156 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004157 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004158 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004160 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004161 r.activity.mConfigChangeFlags |= configChanges;
4162 if (finishing) {
4163 r.activity.mFinished = true;
4164 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004165
4166 performPauseActivityIfNeeded(r, "destroy");
4167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 if (!r.stopped) {
4169 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004170 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 } catch (SuperNotCalledException e) {
4172 throw e;
4173 } catch (Exception e) {
4174 if (!mInstrumentation.onException(r.activity, e)) {
4175 throw new RuntimeException(
4176 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004177 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004178 + ": " + e.toString(), e);
4179 }
4180 }
4181 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004182 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4183 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 }
4185 if (getNonConfigInstance) {
4186 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004187 r.lastNonConfigurationInstances
4188 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 } catch (Exception e) {
4190 if (!mInstrumentation.onException(r.activity, e)) {
4191 throw new RuntimeException(
4192 "Unable to retain activity "
4193 + r.intent.getComponent().toShortString()
4194 + ": " + e.toString(), e);
4195 }
4196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 try {
4199 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004200 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004201 if (!r.activity.mCalled) {
4202 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004203 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 " did not call through to super.onDestroy()");
4205 }
4206 if (r.window != null) {
4207 r.window.closeAllPanels();
4208 }
4209 } catch (SuperNotCalledException e) {
4210 throw e;
4211 } catch (Exception e) {
4212 if (!mInstrumentation.onException(r.activity, e)) {
4213 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004214 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4215 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217 }
4218 }
4219 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004220 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 return r;
4222 }
4223
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004224 private static String safeToComponentShortString(Intent intent) {
4225 ComponentName component = intent.getComponent();
4226 return component == null ? "[Unknown]" : component.toShortString();
4227 }
4228
Romain Guy65b345f2011-07-27 18:51:50 -07004229 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004231 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004232 configChanges, getNonConfigInstance);
4233 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004234 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 WindowManager wm = r.activity.getWindowManager();
4236 View v = r.activity.mDecor;
4237 if (v != null) {
4238 if (r.activity.mVisibleFromServer) {
4239 mNumVisibleActivities--;
4240 }
4241 IBinder wtoken = v.getWindowToken();
4242 if (r.activity.mWindowAdded) {
Robert Carr77bdfb52016-05-02 18:18:31 -07004243 if (r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004244 // Hold off on removing this until the new activity's
4245 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004246 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004247 r.mPendingRemoveWindowManager = wm;
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004248 // We can only keep the part of the view hierarchy that we control,
4249 // everything else must be removed, because it might not be able to
4250 // behave properly when activity is relaunching.
4251 r.window.clearContentView();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004252 } else {
4253 wm.removeViewImmediate(v);
4254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004256 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004257 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258 r.activity.getClass().getName(), "Activity");
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004259 } else if (r.mPendingRemoveWindow != null) {
4260 // We're preserving only one window, others should be closed so app views
4261 // will be detached before the final tear down. It should be done now because
4262 // some components (e.g. WebView) rely on detach callbacks to perform receiver
4263 // unregister and other cleanup.
4264 WindowManagerGlobal.getInstance().closeAllExceptView(token, v,
4265 r.activity.getClass().getName(), "Activity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004266 }
4267 r.activity.mDecor = null;
4268 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004269 if (r.mPendingRemoveWindow == null) {
4270 // If we are delaying the removal of the activity window, then
4271 // we can't clean up all windows here. Note that we can't do
4272 // so later either, which means any windows that aren't closed
4273 // by the app will leak. Well we try to warning them a lot
4274 // about leaking windows, because that is a bug, so if they are
4275 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004276 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004277 r.activity.getClass().getName(), "Activity");
4278 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004279
4280 // Mocked out contexts won't be participating in the normal
4281 // process lifecycle, but if we're running with a proper
4282 // ApplicationContext we need to have it tear down things
4283 // cleanly.
4284 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004285 if (c instanceof ContextImpl) {
4286 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004287 r.activity.getClass().getName(), "Activity");
4288 }
4289 }
4290 if (finishing) {
4291 try {
4292 ActivityManagerNative.getDefault().activityDestroyed(token);
4293 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004294 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 }
4296 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004297 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004298 }
4299
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004300 /**
4301 * @param preserveWindow Whether the activity should try to reuse the window it created,
4302 * including the decor view after the relaunch.
4303 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004304 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004305 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004306 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004307 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004308 ActivityClientRecord target = null;
4309
Craig Mautner88c05892013-06-28 09:47:45 -07004310 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004311 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4312 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004313 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004314 if (r.token == token) {
4315 target = r;
4316 if (pendingResults != null) {
4317 if (r.pendingResults != null) {
4318 r.pendingResults.addAll(pendingResults);
4319 } else {
4320 r.pendingResults = pendingResults;
4321 }
4322 }
4323 if (pendingNewIntents != null) {
4324 if (r.pendingIntents != null) {
4325 r.pendingIntents.addAll(pendingNewIntents);
4326 } else {
4327 r.pendingIntents = pendingNewIntents;
4328 }
4329 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004330
4331 // For each relaunch request, activity manager expects an answer
4332 if (!r.onlyLocalRequest && fromServer) {
4333 try {
4334 ActivityManagerNative.getDefault().activityRelaunched(token);
4335 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004336 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004337 }
4338 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004339 break;
4340 }
4341 }
4342
4343 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004344 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4345 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004346 target = new ActivityClientRecord();
4347 target.token = token;
4348 target.pendingResults = pendingResults;
4349 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004350 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004351 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004352 final ActivityClientRecord existing = mActivities.get(token);
4353 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004354 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004355 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4356 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004357 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004358 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004359 }
4360 target.onlyLocalRequest = true;
4361 }
4362 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004363 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004364 }
4365
4366 if (fromServer) {
4367 target.startsNotResumed = notResumed;
4368 target.onlyLocalRequest = false;
4369 }
4370 if (config != null) {
4371 target.createdConfig = config;
4372 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004373 if (overrideConfig != null) {
4374 target.overrideConfig = overrideConfig;
4375 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004376 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004377 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004378 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004379 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4380 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004381 }
4382
Romain Guy65b345f2011-07-27 18:51:50 -07004383 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004384 // If we are getting ready to gc after going to the background, well
4385 // we are back active so skip it.
4386 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004387 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388
4389 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004390 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 // First: make sure we have the most recent configuration and most
4393 // recent version of the activity, or skip it if some previous call
4394 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004395 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 int N = mRelaunchingActivities.size();
4397 IBinder token = tmp.token;
4398 tmp = null;
4399 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004400 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 if (r.token == token) {
4402 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004403 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 mRelaunchingActivities.remove(i);
4405 i--;
4406 N--;
4407 }
4408 }
Bob Leee5408332009-09-04 18:31:17 -07004409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004411 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004412 return;
4413 }
Bob Leee5408332009-09-04 18:31:17 -07004414
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004415 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4416 + tmp.token + " with configChanges=0x"
4417 + Integer.toHexString(configChanges));
4418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 if (mPendingConfiguration != null) {
4420 changedConfig = mPendingConfiguration;
4421 mPendingConfiguration = null;
4422 }
4423 }
Bob Leee5408332009-09-04 18:31:17 -07004424
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004425 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4426 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4427 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4428 } else {
4429 tmp.lastProcessedSeq = tmp.relaunchSeq;
4430 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004431 if (tmp.createdConfig != null) {
4432 // If the activity manager is passing us its current config,
4433 // assume that is really what we want regardless of what we
4434 // may have pending.
4435 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004436 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4437 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4438 if (changedConfig == null
4439 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4440 changedConfig = tmp.createdConfig;
4441 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004442 }
4443 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004444
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004445 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004446 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 // If there was a pending configuration change, execute it first.
4449 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004450 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004451 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004452 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 }
Bob Leee5408332009-09-04 18:31:17 -07004454
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004455 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004456 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004458 if (!tmp.onlyLocalRequest) {
4459 try {
4460 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4461 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004462 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004463 }
4464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 return;
4466 }
Bob Leee5408332009-09-04 18:31:17 -07004467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004469 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004470 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004471 r.lastProcessedSeq = tmp.lastProcessedSeq;
4472 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004473 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004474
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004475 r.activity.mChangingConfigurations = true;
4476
Robert Carr23fa16b2016-01-13 13:19:58 -08004477 // If we are preserving the main window across relaunches we would also like to preserve
4478 // the children. However the client side view system does not support preserving
4479 // the child views so we notify the window manager to expect these windows to
4480 // be replaced and defer requests to destroy or hide them. This way we can achieve
4481 // visual continuity. It's important that we do this here prior to pause and destroy
4482 // as that is when we may hide or remove the child views.
Robert Carr77bdfb52016-05-02 18:18:31 -07004483 //
4484 // There is another scenario, if we have decided locally to relaunch the app from a
4485 // call to recreate, then none of the windows will be prepared for replacement or
4486 // preserved by the server, so we want to notify it that we are preparing to replace
4487 // everything
Robert Carr23fa16b2016-01-13 13:19:58 -08004488 try {
Robert Carr77bdfb52016-05-02 18:18:31 -07004489 if (r.mPreserveWindow || r.onlyLocalRequest) {
4490 WindowManagerGlobal.getWindowSession().prepareToReplaceWindows(
4491 r.token, !r.onlyLocalRequest);
Robert Carr23fa16b2016-01-13 13:19:58 -08004492 }
4493 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004494 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004495 }
4496
Dianne Hackborne2b04802010-12-09 09:24:55 -08004497 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004499 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004500 }
4501 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004502 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 }
Bob Leee5408332009-09-04 18:31:17 -07004504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 r.activity = null;
4508 r.window = null;
4509 r.hideForNow = false;
4510 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004511 // Merge any pending results and pending intents; don't just replace them
4512 if (tmp.pendingResults != null) {
4513 if (r.pendingResults == null) {
4514 r.pendingResults = tmp.pendingResults;
4515 } else {
4516 r.pendingResults.addAll(tmp.pendingResults);
4517 }
4518 }
4519 if (tmp.pendingIntents != null) {
4520 if (r.pendingIntents == null) {
4521 r.pendingIntents = tmp.pendingIntents;
4522 } else {
4523 r.pendingIntents.addAll(tmp.pendingIntents);
4524 }
4525 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004526 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004527 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004528
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004529 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004530
4531 if (!tmp.onlyLocalRequest) {
4532 try {
4533 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004534 if (r.window != null) {
4535 r.window.reportActivityRelaunched();
4536 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004537 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004538 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004539 }
4540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 }
4542
Craig Mautnera0026042014-04-23 11:45:37 -07004543 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4544 r.state = new Bundle();
4545 r.state.setAllowFds(false);
4546 if (r.isPersistable()) {
4547 r.persistentState = new PersistableBundle();
4548 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4549 r.persistentState);
4550 } else {
4551 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4552 }
4553 }
4554
Dianne Hackborn73c14162012-09-19 15:45:06 -07004555 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004556 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004557 ArrayList<ComponentCallbacks2> callbacks
4558 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004559
Craig Mautner88c05892013-06-28 09:47:45 -07004560 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004561 final int NAPP = mAllApplications.size();
4562 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004563 callbacks.add(mAllApplications.get(i));
4564 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004565 final int NACT = mActivities.size();
4566 for (int i=0; i<NACT; i++) {
4567 ActivityClientRecord ar = mActivities.valueAt(i);
4568 Activity a = ar.activity;
4569 if (a != null) {
4570 Configuration thisConfig = applyConfigCompatMainThread(
4571 mCurDefaultDisplayDpi, newConfig,
4572 ar.packageInfo.getCompatibilityInfo());
4573 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4574 // If the activity is currently resumed, its configuration
4575 // needs to change right now.
4576 callbacks.add(a);
4577 } else if (thisConfig != null) {
4578 // Otherwise, we will tell it about the change
4579 // the next time it is resumed or shown. Note that
4580 // the activity manager may, before then, decide the
4581 // activity needs to be destroyed to handle its new
4582 // configuration.
4583 if (DEBUG_CONFIGURATION) {
4584 Slog.v(TAG, "Setting activity "
4585 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004586 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004587 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 }
4589 }
4590 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004591 final int NSVC = mServices.size();
4592 for (int i=0; i<NSVC; i++) {
4593 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 }
4595 }
4596 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004597 final int NPRV = mLocalProviders.size();
4598 for (int i=0; i<NPRV; i++) {
4599 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 }
4601 }
Bob Leee5408332009-09-04 18:31:17 -07004602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 return callbacks;
4604 }
Bob Leee5408332009-09-04 18:31:17 -07004605
Adam Lesinski082614c2016-03-04 14:33:47 -08004606 /**
4607 * Updates the configuration for an Activity. The ActivityClientRecord's
4608 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4609 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4610 * the updated Configuration.
4611 * @param r ActivityClientRecord representing the Activity.
4612 * @param newBaseConfig The new configuration to use. This may be augmented with
4613 * {@link ActivityClientRecord#overrideConfig}.
4614 * @param reportToActivity true if the change should be reported to the Activity's callback.
4615 */
4616 private void performConfigurationChangedForActivity(ActivityClientRecord r,
4617 Configuration newBaseConfig,
4618 boolean reportToActivity) {
4619 r.tmpConfig.setTo(newBaseConfig);
4620 if (r.overrideConfig != null) {
4621 r.tmpConfig.updateFrom(r.overrideConfig);
4622 }
4623 performConfigurationChanged(r.activity, r.token, r.tmpConfig, r.overrideConfig,
4624 reportToActivity);
4625 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
4626 }
4627
4628 /**
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004629 * Creates a new Configuration only if override would modify base. Otherwise returns base.
4630 * @param base The base configuration.
4631 * @param override The update to apply to the base configuration. Can be null.
4632 * @return A Configuration representing base with override applied.
4633 */
4634 private static Configuration createNewConfigAndUpdateIfNotNull(@NonNull Configuration base,
4635 @Nullable Configuration override) {
4636 if (override == null) {
4637 return base;
4638 }
4639 Configuration newConfig = new Configuration(base);
4640 newConfig.updateFrom(override);
4641 return newConfig;
4642 }
4643
4644 /**
Adam Lesinski082614c2016-03-04 14:33:47 -08004645 * Decides whether to update an Activity's configuration and whether to tell the
4646 * Activity/Component about it.
4647 * @param cb The component callback to notify of configuration change.
4648 * @param activityToken The Activity binder token for which this configuration change happened.
4649 * If the change is global, this is null.
4650 * @param newConfig The new configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004651 * @param amOverrideConfig The override config that differentiates the Activity's configuration
Adam Lesinski082614c2016-03-04 14:33:47 -08004652 * from the base global configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004653 * This is supplied by ActivityManager.
Adam Lesinski082614c2016-03-04 14:33:47 -08004654 * @param reportToActivity Notify the Activity of the change.
4655 */
4656 private void performConfigurationChanged(ComponentCallbacks2 cb,
4657 IBinder activityToken,
4658 Configuration newConfig,
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004659 Configuration amOverrideConfig,
Adam Lesinski082614c2016-03-04 14:33:47 -08004660 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004661 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004662 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 // we check the runtime type and act accordingly.
4664 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4665 if (activity != null) {
4666 activity.mCalled = false;
4667 }
Bob Leee5408332009-09-04 18:31:17 -07004668
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004669 boolean shouldChangeConfig = false;
4670 if ((activity == null) || (activity.mCurrentConfig == null)) {
4671 shouldChangeConfig = true;
4672 } else {
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004673 // If the new config is the same as the config this Activity is already
4674 // running with and the override config also didn't change, then don't
4675 // bother calling onConfigurationChanged.
Adam Lesinski082614c2016-03-04 14:33:47 -08004676 int diff = activity.mCurrentConfig.diff(newConfig);
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004677 if (diff != 0 || !mResourcesManager.isSameResourcesOverrideConfig(activityToken,
4678 amOverrideConfig)) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004679 // Always send the task-level config changes. For system-level configuration, if
4680 // this activity doesn't handle any of the config changes, then don't bother
4681 // calling onConfigurationChanged as we're going to destroy it.
4682 if (!mUpdatingSystemConfig
4683 || (~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4684 || !reportToActivity) {
4685 shouldChangeConfig = true;
4686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004687 }
4688 }
Bob Leee5408332009-09-04 18:31:17 -07004689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004690 if (shouldChangeConfig) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004691 // Propagate the configuration change to the Activity and ResourcesManager.
4692
4693 // ContextThemeWrappers may override the configuration for that context.
4694 // We must check and apply any overrides defined.
4695 Configuration contextThemeWrapperOverrideConfig = null;
4696 if (cb instanceof ContextThemeWrapper) {
4697 final ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
4698 contextThemeWrapperOverrideConfig = contextThemeWrapper.getOverrideConfiguration();
4699 }
4700
4701 // We only update an Activity's configuration if this is not a global
4702 // configuration change. This must also be done before the callback,
4703 // or else we violate the contract that the new resources are available
4704 // in {@link ComponentCallbacks2#onConfigurationChanged(Configuration)}.
Adam Lesinski082614c2016-03-04 14:33:47 -08004705 if (activityToken != null) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004706 // Apply the ContextThemeWrapper override if necessary.
4707 // NOTE: Make sure the configurations are not modified, as they are treated
4708 // as immutable in many places.
4709 final Configuration finalOverrideConfig = createNewConfigAndUpdateIfNotNull(
4710 amOverrideConfig, contextThemeWrapperOverrideConfig);
4711 mResourcesManager.updateResourcesForActivity(activityToken, finalOverrideConfig);
Adam Lesinski082614c2016-03-04 14:33:47 -08004712 }
4713
Filip Gruszczynskica664812015-12-04 12:43:36 -08004714 if (reportToActivity) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004715 // Apply the ContextThemeWrapper override if necessary.
4716 // NOTE: Make sure the configurations are not modified, as they are treated
4717 // as immutable in many places.
4718 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(
4719 newConfig, contextThemeWrapperOverrideConfig);
Adam Lesinski3ad1b482016-04-01 16:41:41 -07004720 cb.onConfigurationChanged(configToReport);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004721 }
Bob Leee5408332009-09-04 18:31:17 -07004722
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004724 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 throw new SuperNotCalledException(
4726 "Activity " + activity.getLocalClassName() +
Adam Lesinski082614c2016-03-04 14:33:47 -08004727 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004728 }
4729 activity.mConfigChangeFlags = 0;
Adam Lesinski082614c2016-03-04 14:33:47 -08004730 activity.mCurrentConfig = new Configuration(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004731 }
4732 }
4733 }
4734
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004735 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004736 synchronized (mResourcesManager) {
4737 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004738 }
4739 }
4740
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004741 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004742 Configuration config = mConfiguration;
4743 if (mCompatConfiguration == null) {
4744 mCompatConfiguration = new Configuration();
4745 }
4746 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004747 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4748 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004749 config = mCompatConfiguration;
4750 }
4751 return config;
4752 }
4753
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004754 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004755
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004756 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004757
Craig Mautner88c05892013-06-28 09:47:45 -07004758 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004759 if (mPendingConfiguration != null) {
4760 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4761 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004762 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004763 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004764 }
4765 mPendingConfiguration = null;
4766 }
4767
4768 if (config == null) {
4769 return;
4770 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004771
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004772 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004773 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004774
4775 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
Adam Lesinskib61e4052016-05-19 18:23:05 -07004776 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
4777 mResourcesManager.getConfiguration().getLocales());
Craig Mautner88c05892013-06-28 09:47:45 -07004778
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004779 if (mConfiguration == null) {
4780 mConfiguration = new Configuration();
4781 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004782 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004783 return;
4784 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004785
4786 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004787 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004788
4789 final Theme systemTheme = getSystemContext().getTheme();
4790 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4791 systemTheme.rebase();
4792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004794
4795 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4796
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004797 freeTextLayoutCachesIfNeeded(configDiff);
4798
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004799 if (callbacks != null) {
4800 final int N = callbacks.size();
4801 for (int i=0; i<N; i++) {
Andrii Kulian701214b2016-04-07 09:36:33 -07004802 ComponentCallbacks2 cb = callbacks.get(i);
4803 if (cb instanceof Activity) {
4804 // If callback is an Activity - call corresponding method to consider override
4805 // config and avoid onConfigurationChanged if it hasn't changed.
4806 Activity a = (Activity) cb;
4807 performConfigurationChangedForActivity(mActivities.get(a.getActivityToken()),
4808 config, REPORT_TO_ACTIVITY);
4809 } else {
4810 performConfigurationChanged(cb, null, config, null, REPORT_TO_ACTIVITY);
4811 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 }
4814 }
4815
Romain Guy46bfc482013-08-16 18:38:29 -07004816 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004817 if (configDiff != 0) {
4818 // Ask text layout engine to free its caches if there is a locale change
4819 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4820 if (hasLocaleConfigChange) {
4821 Canvas.freeTextLayoutCaches();
4822 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4823 }
4824 }
4825 }
4826
Filip Gruszczynskica664812015-12-04 12:43:36 -08004827 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4828 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004829 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004830 if (r == null || r.activity == null) {
4831 return;
4832 }
Bob Leee5408332009-09-04 18:31:17 -07004833
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004834 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004835 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004836
Adam Lesinski082614c2016-03-04 14:33:47 -08004837 r.overrideConfig = data.overrideConfig;
4838 performConfigurationChangedForActivity(r, mCompatConfiguration, reportToActivity);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004839 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 }
4841
Jeff Hao1b012d32014-08-20 10:35:34 -07004842 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004843 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004844 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004845 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004846 default:
4847 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004848 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004849 break;
4850 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004851 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004852 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004853 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004854 } finally {
4855 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004856 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004857 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004858 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004859 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004860 }
4861 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004862 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004863 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004864 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004865 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004866 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004867 }
4868 }
Bob Leee5408332009-09-04 18:31:17 -07004869
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004870 /**
4871 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4872 * so that profiler data won't be lost.
4873 *
4874 * @hide
4875 */
4876 public void stopProfiling() {
4877 mProfiler.stopProfiling();
4878 }
4879
Romain Guya998dff2012-03-23 18:58:36 -07004880 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004881 if (managed) {
4882 try {
4883 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4884 } catch (IOException e) {
4885 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4886 + " -- can the process access this path?");
4887 } finally {
4888 try {
4889 dhd.fd.close();
4890 } catch (IOException e) {
4891 Slog.w(TAG, "Failure closing profile fd", e);
4892 }
4893 }
4894 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004895 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004896 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004897 try {
4898 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4899 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004900 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004901 }
Andy McFadden824c5102010-07-09 16:26:57 -07004902 }
4903
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004904 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4905 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08004906 switch (cmd) {
4907 case IApplicationThread.PACKAGE_REMOVED:
4908 case IApplicationThread.PACKAGE_REMOVED_DONT_KILL:
4909 {
4910 final boolean killApp = cmd == IApplicationThread.PACKAGE_REMOVED;
4911 if (packages == null) {
4912 break;
4913 }
4914 synchronized (mResourcesManager) {
4915 for (int i = packages.length - 1; i >= 0; i--) {
4916 if (!hasPkgInfo) {
4917 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4918 if (ref != null && ref.get() != null) {
4919 hasPkgInfo = true;
4920 } else {
4921 ref = mResourcePackages.get(packages[i]);
4922 if (ref != null && ref.get() != null) {
4923 hasPkgInfo = true;
4924 }
4925 }
4926 }
4927 if (killApp) {
4928 mPackages.remove(packages[i]);
4929 mResourcePackages.remove(packages[i]);
4930 }
4931 }
4932 }
4933 break;
4934 }
4935 case IApplicationThread.PACKAGE_REPLACED:
4936 {
4937 if (packages == null) {
4938 break;
4939 }
4940 synchronized (mResourcesManager) {
4941 for (int i = packages.length - 1; i >= 0; i--) {
4942 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4943 LoadedApk pkgInfo = ref != null ? ref.get() : null;
4944 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004945 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004946 } else {
4947 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08004948 pkgInfo = ref != null ? ref.get() : null;
4949 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08004950 hasPkgInfo = true;
4951 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004952 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004953 // If the package is being replaced, yet it still has a valid
4954 // LoadedApk object, the package was updated with _DONT_KILL.
4955 // Adjust it's internal references to the application info and
4956 // resources.
4957 if (pkgInfo != null) {
4958 try {
4959 final String packageName = packages[i];
4960 final ApplicationInfo aInfo =
4961 sPackageManager.getApplicationInfo(
4962 packageName,
4963 0 /*flags*/,
4964 UserHandle.myUserId());
4965
4966 if (mActivities.size() > 0) {
4967 for (ActivityClientRecord ar : mActivities.values()) {
4968 if (ar.activityInfo.applicationInfo.packageName
4969 .equals(packageName)) {
4970 ar.activityInfo.applicationInfo = aInfo;
4971 ar.packageInfo = pkgInfo;
4972 }
4973 }
4974 }
4975 final List<String> oldPaths =
4976 sPackageManager.getPreviousCodePaths(packageName);
4977 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
4978 } catch (RemoteException e) {
4979 }
4980 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004981 }
4982 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004983 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004984 }
4985 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004986 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004987 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004990 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 final int N = callbacks.size();
4993 for (int i=0; i<N; i++) {
4994 callbacks.get(i).onLowMemory();
4995 }
4996
Chris Tatece229052009-03-25 16:44:52 -07004997 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4998 if (Process.myUid() != Process.SYSTEM_UID) {
4999 int sqliteReleased = SQLiteDatabase.releaseMemory();
5000 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
5001 }
Bob Leee5408332009-09-04 18:31:17 -07005002
Mike Reedcaf0df12009-04-27 14:32:05 -04005003 // Ask graphics to free up as much as possible (font/image caches)
5004 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005005
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005006 // Ask text layout engine to free also as much as possible
5007 Canvas.freeTextLayoutCaches();
5008
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 BinderInternal.forceGc("mem");
5010 }
5011
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005012 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07005013 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005014
Dianne Hackborn73c14162012-09-19 15:45:06 -07005015 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005016
5017 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07005018 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005019 callbacks.get(i).onTrimMemory(level);
5020 }
Romain Guy19f86e82012-04-23 15:19:07 -07005021
John Reckf47a5942014-06-30 16:20:04 -07005022 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005023 }
5024
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07005025 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005026 if (Process.isIsolated()) {
5027 // Isolated processes aren't going to do UI.
5028 return;
5029 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005030 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupGraphicsSupport");
Romain Guya9582652011-11-10 14:20:10 -08005031 try {
5032 int uid = Process.myUid();
5033 String[] packages = getPackageManager().getPackagesForUid(uid);
5034
Shuo Gao7c69a662016-03-01 13:32:03 +08005035 if (packages != null) {
John Reck51aaf902015-12-02 15:08:07 -08005036 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07005037 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08005038 }
5039 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005040 throw e.rethrowFromSystemServer();
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005041 } finally {
5042 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Romain Guya9582652011-11-10 14:20:10 -08005043 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07005044 }
5045
5046 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05005047 final int densityDpi = mCurDefaultDisplayDpi;
5048 if (!mDensityCompatMode
5049 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
5050 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
5051 DisplayMetrics.DENSITY_DEVICE = densityDpi;
5052 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005053 }
5054 }
5055
Todd Kennedye713efc2016-05-10 13:45:40 -07005056 /**
5057 * Returns the correct library directory for the current ABI.
5058 * <p>
5059 * If we're dealing with a multi-arch application that has both 32 and 64 bit shared
5060 * libraries, we might need to choose the secondary depending on what the current
5061 * runtime's instruction set is.
5062 */
5063 private String getInstrumentationLibrary(ApplicationInfo appInfo, InstrumentationInfo insInfo) {
5064 if (appInfo.primaryCpuAbi != null && appInfo.secondaryCpuAbi != null) {
5065 // Get the instruction set supported by the secondary ABI. In the presence
5066 // of a native bridge this might be different than the one secondary ABI used.
5067 String secondaryIsa =
5068 VMRuntime.getInstructionSet(appInfo.secondaryCpuAbi);
5069 final String secondaryDexCodeIsa =
5070 SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
5071 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
5072
5073 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
5074 if (runtimeIsa.equals(secondaryIsa)) {
5075 return insInfo.secondaryNativeLibraryDir;
5076 }
5077 }
5078 return insInfo.nativeLibraryDir;
5079 }
5080
Adam Lesinskib61e4052016-05-19 18:23:05 -07005081 /**
5082 * The LocaleList set for the app's resources may have been shuffled so that the preferred
5083 * Locale is at position 0. We must find the index of this preferred Locale in the
5084 * original LocaleList.
5085 */
5086 private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
5087 final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
5088 final int newLocaleListSize = newLocaleList.size();
5089 for (int i = 0; i < newLocaleListSize; i++) {
5090 if (bestLocale.equals(newLocaleList.get(i))) {
5091 LocaleList.setDefault(newLocaleList, i);
5092 return;
5093 }
5094 }
Adam Lesinski27d30162016-05-25 16:45:14 -07005095
5096 // The app may have overridden the LocaleList with its own Locale
5097 // (not present in the available list). Push the chosen Locale
5098 // to the front of the list.
5099 LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
Adam Lesinskib61e4052016-05-19 18:23:05 -07005100 }
5101
Romain Guy65b345f2011-07-27 18:51:50 -07005102 private void handleBindApplication(AppBindData data) {
Calin Juravle8f5770d2016-04-12 14:12:04 +01005103 // Register the UI Thread as a sensitive thread to the runtime.
5104 VMRuntime.registerSensitiveThread();
Man Caocfa78b22015-06-11 20:14:34 -07005105 if (data.trackAllocation) {
5106 DdmVmInternal.enableRecentAllocations(true);
5107 }
5108
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005109 // Note when this process has started.
5110 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005112 mBoundApplication = data;
5113 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005114 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005115
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005116 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005117 if (data.initProfilerInfo != null) {
5118 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5119 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5120 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5121 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
5122 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005125 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005126 android.ddm.DdmHandleAppName.setAppName(data.processName,
5127 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005128
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005129 if (data.persistent) {
5130 // Persistent processes on low-memory devices do not get to
5131 // use hardware accelerated drawing, since this can add too much
5132 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005133 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005134 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005135 }
5136 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005137
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005138 if (mProfiler.profileFd != null) {
5139 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005140 }
5141
Joe Onoratod630f102011-03-17 18:42:26 -07005142 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5143 // implementation to use the pool executor. Normally, we use the
5144 // serialized executor as the default. This has to happen in the
5145 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005146 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005147 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5148 }
5149
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005150 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005152 /*
5153 * Before spawning a new process, reset the time zone to be the system time zone.
5154 * This needs to be done because the system time zone could have changed after the
5155 * the spawning of this process. Without doing this this process would have the incorrect
5156 * system time zone.
5157 */
5158 TimeZone.setDefault(null);
5159
Adam Lesinskib61e4052016-05-19 18:23:05 -07005160 /*
5161 * Set the LocaleList. This may change once we create the App Context.
5162 */
5163 LocaleList.setDefault(data.config.getLocales());
5164
5165 synchronized (mResourcesManager) {
5166 /*
5167 * Update the system configuration since its preloaded and might not
5168 * reflect configuration changes. The configuration object passed
5169 * in AppBindData can be safely assumed to be up to date
5170 */
5171 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5172 mCurDefaultDisplayDpi = data.config.densityDpi;
5173
5174 // This calls mResourcesManager so keep it within the synchronized block.
5175 applyCompatConfiguration(mCurDefaultDisplayDpi);
5176 }
5177
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005178 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179
Dianne Hackborndde331c2012-08-03 14:01:57 -07005180 /**
5181 * Switch this process to density compatibility mode if needed.
5182 */
5183 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5184 == 0) {
5185 mDensityCompatMode = true;
5186 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5187 }
5188 updateDefaultDensity();
5189
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005190 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
5191 DateFormat.set24HourTimePref(is24Hr);
5192
Jon Miranda836c0a82014-08-11 12:32:26 -07005193 View.mDebugViewAttributes =
5194 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5195
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005196 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005197 * For system applications on userdebug/eng builds, log stack
5198 * traces of disk and network access to dropbox for analysis.
5199 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005200 if ((data.appInfo.flags &
5201 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005202 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5203 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005204 }
5205
5206 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005207 * For apps targetting Honeycomb or later, we don't allow network usage
5208 * on the main event loop / UI thread. This is what ultimately throws
5209 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005210 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005211 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005212 StrictMode.enableDeathOnNetwork();
5213 }
5214
Jeff Sharkey344744b2016-01-28 19:03:30 -07005215 /**
5216 * For apps targetting N or later, we don't allow file:// Uri exposure.
5217 * This is what ultimately throws {@link FileUriExposedException}.
5218 */
5219 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005220 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005221 }
5222
Svet Ganov37e43272016-09-09 16:01:32 -07005223 // We deprecated Build.SERIAL and only apps that target pre NMR1
5224 // SDK can see it. Since access to the serial is now behind a
5225 // permission we push down the value and here we fix it up
5226 // before any app code has been loaded.
5227 try {
5228 Field field = Build.class.getDeclaredField("SERIAL");
5229 field.setAccessible(true);
5230 field.set(Build.class, data.buildSerial);
5231 } catch (NoSuchFieldException | IllegalAccessException e) {
5232 /* ignore */
5233 }
5234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5236 // XXX should have option to change the port.
5237 Debug.changeDebugPort(8100);
5238 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005239 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 + " is waiting for the debugger on port 8100...");
5241
5242 IActivityManager mgr = ActivityManagerNative.getDefault();
5243 try {
5244 mgr.showWaitingForDebugger(mAppThread, true);
5245 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005246 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005247 }
5248
5249 Debug.waitForDebugger();
5250
5251 try {
5252 mgr.showWaitingForDebugger(mAppThread, false);
5253 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005254 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005255 }
5256
5257 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005258 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 + " can be debugged on port 8100...");
5260 }
5261 }
5262
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005263 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005264 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5265 Trace.setAppTracingAllowed(isAppDebuggable);
5266 if (isAppDebuggable && data.enableBinderTracking) {
5267 Binder.enableTracing();
5268 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005269
Robert Greenwalt434203a2010-10-11 16:00:27 -07005270 /**
5271 * Initialize the default http proxy in this process for the reasons we set the time zone.
5272 */
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005273 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Setup proxies");
Alan Viverettebe64eae2015-09-03 14:56:04 -04005274 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005275 if (b != null) {
5276 // In pre-boot mode (doing initial launch to collect password), not
5277 // all system is up. This includes the connectivity service, so don't
5278 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005279 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005280 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005281 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005282 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005283 } catch (RemoteException e) {
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005284 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005285 throw e.rethrowFromSystemServer();
5286 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005287 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005288 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Robert Greenwalt434203a2010-10-11 16:00:27 -07005289
Alan Viverette2107d692015-09-03 14:55:27 -04005290 // Instrumentation info affects the class loader, so load it before
5291 // setting up the app context.
5292 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005294 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005295 ii = new ApplicationPackageManager(null, getPackageManager())
5296 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005297 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005299 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005300 }
5301
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005302 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005303 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005304 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
Todd Kennedye713efc2016-05-10 13:45:40 -07005305 mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005307 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5308 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005309 } else {
5310 ii = null;
5311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312
Alan Viverette2107d692015-09-03 14:55:27 -04005313 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005314 updateLocaleListFromAppContext(appContext,
5315 mResourcesManager.getConfiguration().getLocales());
Seigo Nonakac14dd782016-03-30 23:09:16 +09005316
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005317 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005318 // This cache location probably points at credential-encrypted
5319 // storage which may not be accessible yet; assign it anyway instead
5320 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005321 final File cacheDir = appContext.getCacheDir();
5322 if (cacheDir != null) {
5323 // Provide a usable directory for temporary files
5324 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5325 } else {
5326 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5327 + "due to missing cache directory");
5328 }
Alan Viverette346296b2015-09-01 13:08:05 -04005329
Calin Juravle2a727d72016-04-15 19:33:46 +01005330 // Setup a location to store generated/compiled graphics code.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005331 final Context deviceContext = appContext.createDeviceProtectedStorageContext();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005332 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005333 if (codeCacheDir != null) {
5334 setupGraphicsSupport(data.info, codeCacheDir);
5335 } else {
Calin Juravle2a727d72016-04-15 19:33:46 +01005336 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005337 }
Michael Lentine2ba303f2016-02-01 20:44:34 -06005338 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005339
Chad Brubaker78d47122015-11-17 22:26:58 -08005340 // Install the Network Security Config Provider. This must happen before the application
5341 // code is loaded to prevent issues with instances of TLS objects being created before
5342 // the provider is installed.
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005343 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "NetworkSecurityConfigProvider.install");
Chad Brubaker276ee962016-06-08 12:57:46 -07005344 NetworkSecurityConfigProvider.install(appContext);
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005345 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Chad Brubaker78d47122015-11-17 22:26:58 -08005346
Alan Viverette2107d692015-09-03 14:55:27 -04005347 // Continue loading instrumentation.
5348 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005349 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005350 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005351 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005352 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005353 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005354 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005355
5356 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005357 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005358 mInstrumentation = (Instrumentation)
5359 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5360 } catch (Exception e) {
5361 throw new RuntimeException(
5362 "Unable to instantiate instrumentation "
5363 + data.instrumentationName + ": " + e.toString(), e);
5364 }
5365
Alan Viverettebe64eae2015-09-03 14:56:04 -04005366 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5367 mInstrumentation.init(this, instrContext, appContext, component,
5368 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005369
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005370 if (mProfiler.profileFile != null && !ii.handleProfiling
5371 && mProfiler.profileFd == null) {
5372 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005373 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005374 file.getParentFile().mkdirs();
5375 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 } else {
5378 mInstrumentation = new Instrumentation();
5379 }
5380
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005381 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005382 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005383 } else {
5384 // Small heap, clamp to the current growth limit and let the heap release
5385 // pages after the growth limit to the non growth limit capacity. b/18387825
5386 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005387 }
5388
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005389 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005390 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005391 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005392 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005393 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005394 // If the app is being launched for full backup or restore, bring it up in
5395 // a restricted environment with the base application class.
5396 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5397 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005398
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005399 // don't bring up providers in restricted mode; they may depend on the
5400 // app's custom Application class
5401 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005402 if (!ArrayUtils.isEmpty(data.providers)) {
5403 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005404 // For process that contains content providers, we want to
5405 // ensure that the JIT is enabled "at some point".
5406 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5407 }
5408 }
5409
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005410 // Do this after providers, since instrumentation tests generally start their
5411 // test thread at this point, and we don't want that racing.
5412 try {
5413 mInstrumentation.onCreate(data.instrumentationArgs);
5414 }
5415 catch (Exception e) {
5416 throw new RuntimeException(
5417 "Exception thrown in onCreate() of "
5418 + data.instrumentationName + ": " + e.toString(), e);
5419 }
5420
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005421 try {
5422 mInstrumentation.callApplicationOnCreate(app);
5423 } catch (Exception e) {
5424 if (!mInstrumentation.onException(app, e)) {
5425 throw new RuntimeException(
5426 "Unable to create application " + app.getClass().getName()
5427 + ": " + e.toString(), e);
5428 }
5429 }
5430 } finally {
5431 StrictMode.setThreadPolicy(savedPolicy);
5432 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005433 }
5434
5435 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5436 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005437 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5438 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 Debug.stopMethodTracing();
5440 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005441 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442 // + ", app thr: " + mAppThread);
5443 try {
5444 am.finishInstrumentation(mAppThread, resultCode, results);
5445 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005446 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005447 }
5448 }
5449
Romain Guy65b345f2011-07-27 18:51:50 -07005450 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 Context context, List<ProviderInfo> providers) {
5452 final ArrayList<IActivityManager.ContentProviderHolder> results =
5453 new ArrayList<IActivityManager.ContentProviderHolder>();
5454
Romain Guya998dff2012-03-23 18:58:36 -07005455 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005456 if (DEBUG_PROVIDER) {
5457 StringBuilder buf = new StringBuilder(128);
5458 buf.append("Pub ");
5459 buf.append(cpi.authority);
5460 buf.append(": ");
5461 buf.append(cpi.name);
5462 Log.i(TAG, buf.toString());
5463 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005464 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5465 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5466 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005467 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005469 }
5470 }
5471
5472 try {
5473 ActivityManagerNative.getDefault().publishContentProviders(
5474 getApplicationThread(), results);
5475 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005476 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005477 }
5478 }
5479
Jeff Sharkey6d515712012-09-20 16:06:08 -07005480 public final IContentProvider acquireProvider(
5481 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005482 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005483 if (provider != null) {
5484 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 }
5486
Wale Ogunwale1d646122015-04-24 14:45:14 -07005487 // There is a possible race here. Another thread may try to acquire
5488 // the same provider at the same time. When this happens, we want to ensure
5489 // that the first one wins.
5490 // Note that we cannot hold the lock while acquiring and installing the
5491 // provider since it might take a long time to run and it could also potentially
5492 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005494 try {
5495 holder = ActivityManagerNative.getDefault().getContentProvider(
5496 getApplicationThread(), auth, userId, stable);
5497 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005498 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005499 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005500 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005501 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005502 return null;
5503 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005505 // Install provider will increment the reference count for us, and break
5506 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005507 holder = installProvider(c, holder, holder.info,
5508 true /*noisy*/, holder.noReleaseNeeded, stable);
5509 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005510 }
5511
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005512 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5513 if (stable) {
5514 prc.stableCount += 1;
5515 if (prc.stableCount == 1) {
5516 // We are acquiring a new stable reference on the provider.
5517 int unstableDelta;
5518 if (prc.removePending) {
5519 // We have a pending remove operation, which is holding the
5520 // last unstable reference. At this point we are converting
5521 // that unstable reference to our new stable reference.
5522 unstableDelta = -1;
5523 // Cancel the removal of the provider.
5524 if (DEBUG_PROVIDER) {
5525 Slog.v(TAG, "incProviderRef: stable "
5526 + "snatched provider from the jaws of death");
5527 }
5528 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005529 // There is a race! It fails to remove the message, which
5530 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005531 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5532 } else {
5533 unstableDelta = 0;
5534 }
5535 try {
5536 if (DEBUG_PROVIDER) {
5537 Slog.v(TAG, "incProviderRef Now stable - "
5538 + prc.holder.info.name + ": unstableDelta="
5539 + unstableDelta);
5540 }
5541 ActivityManagerNative.getDefault().refContentProvider(
5542 prc.holder.connection, 1, unstableDelta);
5543 } catch (RemoteException e) {
5544 //do nothing content provider object is dead any way
5545 }
5546 }
5547 } else {
5548 prc.unstableCount += 1;
5549 if (prc.unstableCount == 1) {
5550 // We are acquiring a new unstable reference on the provider.
5551 if (prc.removePending) {
5552 // Oh look, we actually have a remove pending for the
5553 // provider, which is still holding the last unstable
5554 // reference. We just need to cancel that to take new
5555 // ownership of the reference.
5556 if (DEBUG_PROVIDER) {
5557 Slog.v(TAG, "incProviderRef: unstable "
5558 + "snatched provider from the jaws of death");
5559 }
5560 prc.removePending = false;
5561 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5562 } else {
5563 // First unstable ref, increment our count in the
5564 // activity manager.
5565 try {
5566 if (DEBUG_PROVIDER) {
5567 Slog.v(TAG, "incProviderRef: Now unstable - "
5568 + prc.holder.info.name);
5569 }
5570 ActivityManagerNative.getDefault().refContentProvider(
5571 prc.holder.connection, 0, 1);
5572 } catch (RemoteException e) {
5573 //do nothing content provider object is dead any way
5574 }
5575 }
5576 }
5577 }
5578 }
5579
Jeff Sharkey6d515712012-09-20 16:06:08 -07005580 public final IContentProvider acquireExistingProvider(
5581 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005582 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005583 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005584 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005585 if (pr == null) {
5586 return null;
5587 }
5588
5589 IContentProvider provider = pr.mProvider;
5590 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005591 if (!jBinder.isBinderAlive()) {
5592 // The hosting process of the provider has died; we can't
5593 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005594 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005595 + ": existing object's process dead");
5596 handleUnstableProviderDiedLocked(jBinder, true);
5597 return null;
5598 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005599
5600 // Only increment the ref count if we have one. If we don't then the
5601 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005602 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005603 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005604 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005605 }
5606 return provider;
5607 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005608 }
5609
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005610 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5611 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 return false;
5613 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005616 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005618 if (prc == null) {
5619 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005621 }
5622
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005623 boolean lastRef = false;
5624 if (stable) {
5625 if (prc.stableCount == 0) {
5626 if (DEBUG_PROVIDER) Slog.v(TAG,
5627 "releaseProvider: stable ref count already 0, how?");
5628 return false;
5629 }
5630 prc.stableCount -= 1;
5631 if (prc.stableCount == 0) {
5632 // What we do at this point depends on whether there are
5633 // any unstable refs left: if there are, we just tell the
5634 // activity manager to decrement its stable count; if there
5635 // aren't, we need to enqueue this provider to be removed,
5636 // and convert to holding a single unstable ref while
5637 // doing so.
5638 lastRef = prc.unstableCount == 0;
5639 try {
5640 if (DEBUG_PROVIDER) {
5641 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5642 + lastRef + " - " + prc.holder.info.name);
5643 }
5644 ActivityManagerNative.getDefault().refContentProvider(
5645 prc.holder.connection, -1, lastRef ? 1 : 0);
5646 } catch (RemoteException e) {
5647 //do nothing content provider object is dead any way
5648 }
5649 }
5650 } else {
5651 if (prc.unstableCount == 0) {
5652 if (DEBUG_PROVIDER) Slog.v(TAG,
5653 "releaseProvider: unstable ref count already 0, how?");
5654 return false;
5655 }
5656 prc.unstableCount -= 1;
5657 if (prc.unstableCount == 0) {
5658 // If this is the last reference, we need to enqueue
5659 // this provider to be removed instead of telling the
5660 // activity manager to remove it at this point.
5661 lastRef = prc.stableCount == 0;
5662 if (!lastRef) {
5663 try {
5664 if (DEBUG_PROVIDER) {
5665 Slog.v(TAG, "releaseProvider: No longer unstable - "
5666 + prc.holder.info.name);
5667 }
5668 ActivityManagerNative.getDefault().refContentProvider(
5669 prc.holder.connection, 0, -1);
5670 } catch (RemoteException e) {
5671 //do nothing content provider object is dead any way
5672 }
5673 }
5674 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005675 }
5676
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005677 if (lastRef) {
5678 if (!prc.removePending) {
5679 // Schedule the actual remove asynchronously, since we don't know the context
5680 // this will be called in.
5681 // TODO: it would be nice to post a delayed message, so
5682 // if we come back and need the same provider quickly
5683 // we will still have it available.
5684 if (DEBUG_PROVIDER) {
5685 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5686 + prc.holder.info.name);
5687 }
5688 prc.removePending = true;
5689 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5690 mH.sendMessage(msg);
5691 } else {
5692 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5693 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005694 }
5695 return true;
5696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 }
5698
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005699 final void completeRemoveProvider(ProviderRefCount prc) {
5700 synchronized (mProviderMap) {
5701 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005702 // There was a race! Some other client managed to acquire
5703 // the provider before the removal was completed.
5704 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005705 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005706 + "provider still in use");
5707 return;
5708 }
5709
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005710 // More complicated race!! Some client managed to acquire the
5711 // provider and release it before the removal was completed.
5712 // Continue the removal, and abort the next remove message.
5713 prc.removePending = false;
5714
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005715 final IBinder jBinder = prc.holder.provider.asBinder();
5716 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5717 if (existingPrc == prc) {
5718 mProviderRefCountMap.remove(jBinder);
5719 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005720
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005721 for (int i=mProviderMap.size()-1; i>=0; i--) {
5722 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005723 IBinder myBinder = pr.mProvider.asBinder();
5724 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005725 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005726 }
5727 }
5728 }
5729
5730 try {
5731 if (DEBUG_PROVIDER) {
5732 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5733 + "removeContentProvider(" + prc.holder.info.name + ")");
5734 }
5735 ActivityManagerNative.getDefault().removeContentProvider(
5736 prc.holder.connection, false);
5737 } catch (RemoteException e) {
5738 //do nothing content provider object is dead any way
5739 }
5740 }
5741
5742 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005743 synchronized (mProviderMap) {
5744 handleUnstableProviderDiedLocked(provider, fromClient);
5745 }
5746 }
5747
5748 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5749 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5750 if (prc != null) {
5751 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5752 + provider + " " + prc.holder.info.name);
5753 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005754 for (int i=mProviderMap.size()-1; i>=0; i--) {
5755 ProviderClientRecord pr = mProviderMap.valueAt(i);
5756 if (pr != null && pr.mProvider.asBinder() == provider) {
5757 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5758 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005759 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005760 }
You Kimbc74de62013-10-01 00:13:26 +09005761
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005762 if (fromClient) {
5763 // We found out about this due to execution in our client
5764 // code. Tell the activity manager about it now, to ensure
5765 // that the next time we go to do anything with the provider
5766 // it knows it is dead (so we don't race with its death
5767 // notification).
5768 try {
5769 ActivityManagerNative.getDefault().unstableProviderDied(
5770 prc.holder.connection);
5771 } catch (RemoteException e) {
5772 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005773 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005774 }
5775 }
5776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005777
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005778 final void appNotRespondingViaProvider(IBinder provider) {
5779 synchronized (mProviderMap) {
5780 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5781 if (prc != null) {
5782 try {
5783 ActivityManagerNative.getDefault()
5784 .appNotRespondingViaProvider(prc.holder.connection);
5785 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005786 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005787 }
5788 }
5789 }
5790 }
5791
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005792 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005793 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005794 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005795 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5796
5797 final ProviderClientRecord pcr = new ProviderClientRecord(
5798 auths, provider, localProvider, holder);
5799 for (String auth : auths) {
5800 final ProviderKey key = new ProviderKey(auth, userId);
5801 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005802 if (existing != null) {
5803 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005804 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005805 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005806 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 }
5808 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005809 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 }
5811
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005812 /**
5813 * Installs the provider.
5814 *
5815 * Providers that are local to the process or that come from the system server
5816 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5817 * Other remote providers are reference counted. The initial reference count
5818 * for all reference counted providers is one. Providers that are not reference
5819 * counted do not have a reference count (at all).
5820 *
5821 * This method detects when a provider has already been installed. When this happens,
5822 * it increments the reference count of the existing provider (if appropriate)
5823 * and returns the existing provider. This can happen due to concurrent
5824 * attempts to acquire the same provider.
5825 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005826 private IActivityManager.ContentProviderHolder installProvider(Context context,
5827 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5828 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005829 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005830 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005831 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005832 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005833 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005834 + info.name);
5835 }
5836 Context c = null;
5837 ApplicationInfo ai = info.applicationInfo;
5838 if (context.getPackageName().equals(ai.packageName)) {
5839 c = context;
5840 } else if (mInitialApplication != null &&
5841 mInitialApplication.getPackageName().equals(ai.packageName)) {
5842 c = mInitialApplication;
5843 } else {
5844 try {
5845 c = context.createPackageContext(ai.packageName,
5846 Context.CONTEXT_INCLUDE_CODE);
5847 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005848 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 }
5850 }
5851 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005852 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 ai.packageName +
5854 " while loading content provider " +
5855 info.name);
5856 return null;
5857 }
5858 try {
5859 final java.lang.ClassLoader cl = c.getClassLoader();
5860 localProvider = (ContentProvider)cl.
5861 loadClass(info.name).newInstance();
5862 provider = localProvider.getIContentProvider();
5863 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005864 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 info.name + " from sourceDir " +
5866 info.applicationInfo.sourceDir);
5867 return null;
5868 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005869 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005870 TAG, "Instantiating local provider " + info.name);
5871 // XXX Need to create the correct context for this provider.
5872 localProvider.attachInfo(c, info);
5873 } catch (java.lang.Exception e) {
5874 if (!mInstrumentation.onException(null, e)) {
5875 throw new RuntimeException(
5876 "Unable to get provider " + info.name
5877 + ": " + e.toString(), e);
5878 }
5879 return null;
5880 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005881 } else {
5882 provider = holder.provider;
5883 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005884 + info.name);
5885 }
5886
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005887 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005888
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005889 synchronized (mProviderMap) {
5890 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5891 + " / " + info.name);
5892 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005893 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005894 ComponentName cname = new ComponentName(info.packageName, info.name);
5895 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005896 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005897 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005898 Slog.v(TAG, "installProvider: lost the race, "
5899 + "using existing local provider");
5900 }
5901 provider = pr.mProvider;
5902 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005903 holder = new IActivityManager.ContentProviderHolder(info);
5904 holder.provider = provider;
5905 holder.noReleaseNeeded = true;
5906 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005907 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005908 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005909 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005910 retHolder = pr.mHolder;
5911 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005912 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5913 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005914 if (DEBUG_PROVIDER) {
5915 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005916 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005917 // We need to transfer our new reference to the existing
5918 // ref count, releasing the old one... but only if
5919 // release is needed (that is, it is not running in the
5920 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005921 if (!noReleaseNeeded) {
5922 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005923 try {
5924 ActivityManagerNative.getDefault().removeContentProvider(
5925 holder.connection, stable);
5926 } catch (RemoteException e) {
5927 //do nothing content provider object is dead any way
5928 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005929 }
5930 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005931 ProviderClientRecord client = installProviderAuthoritiesLocked(
5932 provider, localProvider, holder);
5933 if (noReleaseNeeded) {
5934 prc = new ProviderRefCount(holder, client, 1000, 1000);
5935 } else {
5936 prc = stable
5937 ? new ProviderRefCount(holder, client, 1, 0)
5938 : new ProviderRefCount(holder, client, 0, 1);
5939 }
5940 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005941 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005942 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005943 }
5944 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005945
5946 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005947 }
5948
Romain Guy65b345f2011-07-27 18:51:50 -07005949 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005950 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005951 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005953 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005954 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005955 public void run() {
5956 ensureJitEnabled();
5957 }
5958 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005959 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5960 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005962 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005963 try {
5964 mgr.attachApplication(mAppThread);
5965 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005966 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005968 // Watch for getting close to heap limit.
5969 BinderInternal.addGcWatcher(new Runnable() {
5970 @Override public void run() {
5971 if (!mSomeActivitiesChanged) {
5972 return;
5973 }
5974 Runtime runtime = Runtime.getRuntime();
5975 long dalvikMax = runtime.maxMemory();
5976 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5977 if (dalvikUsed > ((3*dalvikMax)/4)) {
5978 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5979 + " total=" + (runtime.totalMemory()/1024)
5980 + " used=" + (dalvikUsed/1024));
5981 mSomeActivitiesChanged = false;
5982 try {
5983 mgr.releaseSomeActivities(mAppThread);
5984 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005985 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005986 }
5987 }
5988 }
5989 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 } else {
5991 // Don't set application object here -- if the system crashes,
5992 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005993 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005994 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005995 try {
5996 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005997 ContextImpl context = ContextImpl.createAppContext(
5998 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005999 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
6000 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006001 } catch (Exception e) {
6002 throw new RuntimeException(
6003 "Unable to instantiate Application():" + e.toString(), e);
6004 }
6005 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006006
6007 // add dropbox logging to libcore
6008 DropBox.setReporter(new DropBoxReporter());
6009
Dianne Hackbornc68c9132011-07-29 01:25:18 -07006010 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07006011 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006012 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07006013 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07006014 // We need to apply this change to the resources
6015 // immediately, because upon returning the view
6016 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07006017 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Adam Lesinskib61e4052016-05-19 18:23:05 -07006018 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
6019 mResourcesManager.getConfiguration().getLocales());
6020
Dianne Hackbornae078162010-03-18 11:29:37 -07006021 // This actually changed the resources! Tell
6022 // everyone about it.
6023 if (mPendingConfiguration == null ||
6024 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
6025 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07006026
Jeff Brown9ef09972013-10-15 20:49:59 -07006027 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07006028 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006029 }
6030 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006031 }
Craig Mautner88c05892013-06-28 09:47:45 -07006032 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006033 public void onLowMemory() {
6034 }
Craig Mautner88c05892013-06-28 09:47:45 -07006035 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07006036 public void onTrimMemory(int level) {
6037 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006038 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 }
6040
Romain Guy5e9120d2012-01-30 12:17:22 -08006041 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07006042 // The system process on low-memory devices do not get to use hardware
6043 // accelerated drawing, since this can add too much overhead to the
6044 // process.
6045 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08006046 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07006047 } else {
John Reck51aaf902015-12-02 15:08:07 -08006048 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07006049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006050 ActivityThread thread = new ActivityThread();
6051 thread.attach(true);
6052 return thread;
6053 }
6054
Jeff Brown10e89712011-07-08 18:52:57 -07006055 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07006057 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006058 }
6059 }
6060
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006061 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07006062 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08006063 if (mCoreSettings != null) {
6064 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006065 }
Craig Mautner88c05892013-06-28 09:47:45 -07006066 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006067 }
6068 }
6069
Geremy Condra69689a72012-09-11 16:57:17 -07006070 private static class EventLoggingReporter implements EventLogger.Reporter {
6071 @Override
6072 public void report (int code, Object... list) {
6073 EventLog.writeEvent(code, list);
6074 }
6075 }
6076
Geremy Condrab7faaf42012-09-19 18:07:42 -07006077 private class DropBoxReporter implements DropBox.Reporter {
6078
6079 private DropBoxManager dropBox;
6080
Narayan Kamath7f062242015-04-08 13:24:13 +01006081 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07006082
6083 @Override
6084 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006085 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006086 dropBox.addData(tag, data, flags);
6087 }
6088
6089 @Override
6090 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006091 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006092 dropBox.addText(tag, data);
6093 }
Narayan Kamath7f062242015-04-08 13:24:13 +01006094
6095 private synchronized void ensureInitialized() {
6096 if (dropBox == null) {
6097 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
6098 }
6099 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006100 }
6101
Romain Guy65b345f2011-07-27 18:51:50 -07006102 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006103 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07006104 SamplingProfilerIntegration.start();
6105
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08006106 // CloseGuard defaults to true and can be quite spammy. We
6107 // disable it here, but selectively enable it later (via
6108 // StrictMode) on debug builds, but using DropBox, not logs.
6109 CloseGuard.setEnabled(false);
6110
Andreas Gamped281b422016-07-08 03:50:27 +00006111 Environment.initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -07006112
Geremy Condra69689a72012-09-11 16:57:17 -07006113 // Set the reporter for event logging in libcore
6114 EventLogger.setReporter(new EventLoggingReporter());
6115
Robin Lee3d076af2014-04-25 14:57:49 +01006116 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6117 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6118 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006120 Process.setArgV0("<pre-initialized>");
6121
6122 Looper.prepareMainLooper();
6123
6124 ActivityThread thread = new ActivityThread();
6125 thread.attach(false);
6126
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006127 if (sMainThreadHandler == null) {
6128 sMainThreadHandler = thread.getHandler();
6129 }
6130
Dianne Hackborn287952c2010-09-22 22:34:31 -07006131 if (false) {
6132 Looper.myLooper().setMessageLogging(new
6133 LogPrinter(Log.DEBUG, "ActivityThread"));
6134 }
6135
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006136 // End of event ActivityThreadMain.
6137 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006138 Looper.loop();
6139
Jeff Brown10e89712011-07-08 18:52:57 -07006140 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006141 }
6142}