blob: 182982a638f1f85419dd769e7cbfc3bf750b0cad [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
Andrii Kulianb047b8b2017-02-08 18:38:26 -080019import static android.view.Display.INVALID_DISPLAY;
20
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -070021import android.annotation.NonNull;
22import android.annotation.Nullable;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070023import android.app.assist.AssistContent;
24import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080025import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070027import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.ComponentName;
29import android.content.ContentProvider;
30import android.content.Context;
31import android.content.IContentProvider;
32import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070033import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.pm.ActivityInfo;
35import android.content.pm.ApplicationInfo;
36import android.content.pm.IPackageManager;
37import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070038import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070040import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn3f7c9f22017-04-04 15:36:33 -070041import android.content.pm.ParceledListSlice;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.ProviderInfo;
43import android.content.pm.ServiceInfo;
44import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070045import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.content.res.Configuration;
47import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070048import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.database.sqlite.SQLiteDatabase;
50import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080051import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Clara Bayarriff221612017-03-15 17:27:31 +000054import android.graphics.Typeface;
Jeff Brownbd6e1502012-08-28 03:27:37 -070055import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050056import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070057import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050058import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070059import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040060import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040061import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070062import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070063import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070064import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Bundle;
66import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070067import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070068import android.os.Environment;
Jesse Hallb12249b2016-12-12 12:53:02 -080069import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.os.Handler;
71import android.os.IBinder;
Adam Lesinskib61e4052016-05-19 18:23:05 -070072import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.os.Looper;
74import android.os.Message;
75import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080076import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070077import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070078import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.Process;
80import android.os.RemoteException;
81import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070082import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070084import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070085import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070086import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070087import android.os.UserHandle;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060088import android.provider.BlockedNumberContract;
89import android.provider.CalendarContract;
90import android.provider.CallLog;
91import android.provider.ContactsContract;
92import android.provider.Downloads;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000093import android.provider.Settings;
Chad Brubakerc72875b2016-04-27 16:35:11 -070094import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080095import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070097import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098import android.util.DisplayMetrics;
99import android.util.EventLog;
100import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700101import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700102import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -0800103import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800104import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700105import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -0700106import android.util.SuperNotCalledException;
Adam Lesinski3ad1b482016-04-01 16:41:41 -0700107import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -0800109import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.View;
111import android.view.ViewDebug;
112import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -0700113import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import android.view.Window;
115import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700116import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700117import android.renderscript.RenderScriptCacheDir;
Richard Uhler2c036192016-09-14 09:48:31 +0100118import android.webkit.WebView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700120import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700121import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800122import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123import com.android.internal.os.BinderInternal;
124import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700125import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700126import com.android.internal.os.SomeArgs;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700127import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700128import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700129import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100130import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700131import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132
133import java.io.File;
134import java.io.FileDescriptor;
135import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700136import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.io.PrintWriter;
138import java.lang.ref.WeakReference;
Svet Ganov37e43272016-09-09 16:01:32 -0700139import java.lang.reflect.Field;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700140import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000141import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143import java.util.List;
Adam Lesinskib61e4052016-05-19 18:23:05 -0700144import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800146import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
Geremy Condrab7faaf42012-09-19 18:07:42 -0700149import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700150import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800151import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400152import libcore.net.event.NetworkEventDispatcher;
Calin Juravle37dfc8e2017-03-13 19:02:32 -0700153import dalvik.system.BaseDexClassLoader;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800154import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700155import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700156import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700157import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700158
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700159final class RemoteServiceException extends AndroidRuntimeException {
160 public RemoteServiceException(String msg) {
161 super(msg);
162 }
163}
164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165/**
166 * This manages the execution of the main thread in an
167 * application process, scheduling and executing activities,
168 * broadcasts, and other operations on it as the activity
169 * manager requests.
170 *
171 * {@hide}
172 */
173public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700174 /** @hide */
175 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700176 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700177 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700178 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700179 /** @hide */
180 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700181 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700182 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700183 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800184 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700185 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700186 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700187 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800190 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
191 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800192 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
Dianne Hackborn455625e2015-01-21 09:55:13 -0800194 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
195 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
196 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
197 public static final int SERVICE_DONE_EXECUTING_START = 1;
198 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
199 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
200
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700201 // Details for pausing activity.
202 private static final int USER_LEAVING = 1;
203 private static final int DONT_REPORT = 2;
204
Filip Gruszczynskica664812015-12-04 12:43:36 -0800205 // Whether to invoke an activity callback after delivering new configuration.
206 private static final boolean REPORT_TO_ACTIVITY = true;
207
Sudheer Shanka84a48952017-03-08 18:19:01 -0800208 /**
209 * Denotes an invalid sequence number corresponding to a process state change.
210 */
211 public static final long INVALID_PROC_STATE_SEQ = -1;
212
213 private final Object mNetworkPolicyLock = new Object();
214
215 /**
216 * Denotes the sequence number of the process state change for which the main thread needs
217 * to block until the network rules are updated for it.
218 *
219 * Value of {@link #INVALID_PROC_STATE_SEQ} indicates there is no need for blocking.
220 */
221 @GuardedBy("mNetworkPolicyLock")
222 private long mNetworkBlockSeq = INVALID_PROC_STATE_SEQ;
223
Jeff Browndefd4a62014-03-10 21:24:37 -0700224 private ContextImpl mSystemContext;
Adam Lesinskia82b6262017-03-21 16:56:17 -0700225 private ContextImpl mSystemUiContext;
Bob Leee5408332009-09-04 18:31:17 -0700226
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700227 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700229 final ApplicationThread mAppThread = new ApplicationThread();
230 final Looper mLooper = Looper.myLooper();
231 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700232 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700233 // List of new activities (via ActivityRecord.nextIdle) that should
234 // be reported when next we idle.
235 ActivityClientRecord mNewActivities = null;
236 // Number of activities that are currently visible on-screen.
237 int mNumVisibleActivities = 0;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700238 ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
239 private int mLastSessionId;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700240 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700241 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700242 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700243 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700244 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700245 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700246 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 Application mInitialApplication;
248 final ArrayList<Application> mAllApplications
249 = new ArrayList<Application>();
250 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700251 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800252 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700253 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700254 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700255 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700256 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700257 String[] mInstrumentationSplitAppDirs = null;
258 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700259 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700260 String[] mInstrumentedSplitAppDirs = null;
261 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700262 boolean mSystemThread = false;
263 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700264 boolean mSomeActivitiesChanged = false;
Chong Zhang4951f9d2016-06-23 13:15:08 -0700265 boolean mUpdatingSystemConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100267 // These can be accessed by multiple threads; mResourcesManager is the lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700268 // XXX For now we keep around information about all packages we have
269 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800270 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700271 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800272 // which means this lock gets held while the activity and window managers
273 // holds their own lock. Thus you MUST NEVER call back into the activity manager
274 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700275 // These LoadedApk are only valid for the userId that we're running as.
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100276 @GuardedBy("mResourcesManager")
277 final ArrayMap<String, WeakReference<LoadedApk>> mPackages = new ArrayMap<>();
278 @GuardedBy("mResourcesManager")
279 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages = new ArrayMap<>();
280 @GuardedBy("mResourcesManager")
281 final ArrayList<ActivityClientRecord> mRelaunchingActivities = new ArrayList<>();
282 @GuardedBy("mResourcesManager")
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700283 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700284 // Because we merge activity relaunch operations we can't depend on the ordering provided by
285 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
286 // us to drop certain events, if we know that they happened before relaunch we already executed.
287 // This represents the order of receiving the request from AM.
288 @GuardedBy("mResourcesManager")
289 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
Craig Mautner88c05892013-06-28 09:47:45 -0700291 private final ResourcesManager mResourcesManager;
292
Jeff Sharkey6d515712012-09-20 16:06:08 -0700293 private static final class ProviderKey {
294 final String authority;
295 final int userId;
296
297 public ProviderKey(String authority, int userId) {
298 this.authority = authority;
299 this.userId = userId;
300 }
301
302 @Override
303 public boolean equals(Object o) {
304 if (o instanceof ProviderKey) {
305 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800306 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700307 }
308 return false;
309 }
310
311 @Override
312 public int hashCode() {
313 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
314 }
315 }
316
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700317 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700318 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
319 = new ArrayMap<ProviderKey, ProviderClientRecord>();
320 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
321 = new ArrayMap<IBinder, ProviderRefCount>();
322 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
323 = new ArrayMap<IBinder, ProviderClientRecord>();
324 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
325 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700327 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700328 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600329
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700330 final GcIdler mGcIdler = new GcIdler();
331 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700333 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700334
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800335 Bundle mCoreSettings = null;
336
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400337 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700339 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800341 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700342 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700344 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 Activity activity;
346 Window window;
347 Activity parent;
348 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700349 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 boolean paused;
351 boolean stopped;
352 boolean hideForNow;
353 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700354 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800355 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800356 // Used for consolidating configs before sending on to Activity.
357 private Configuration tmpConfig = new Configuration();
Andrii Kulian44607962017-03-16 11:06:24 -0700358 // Callback used for updating activity override config.
359 ViewRootImpl.ActivityConfigCallback configCallback;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700360 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Jeff Hao1b012d32014-08-20 10:35:34 -0700362 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400365 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700366 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367
368 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800369 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
371 boolean startsNotResumed;
372 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800373 int pendingConfigChanges;
374 boolean onlyLocalRequest;
375
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700376 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800377 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700378 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700380 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
381 // can be compared with other lifecycle operations.
382 int relaunchSeq = 0;
383
384 // Can only be accessed from the UI thread. This represents the latest processed message
385 // that is related to lifecycle events/
386 int lastProcessedSeq = 0;
387
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700388 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 parent = null;
390 embeddedID = null;
391 paused = false;
392 stopped = false;
393 hideForNow = false;
394 nextIdle = null;
Andrii Kulian44607962017-03-16 11:06:24 -0700395 configCallback = (Configuration overrideConfig, int newDisplayId) -> {
396 if (activity == null) {
397 throw new IllegalStateException(
398 "Received config update for non-existing activity");
399 }
400 activity.mMainThread.handleActivityConfigurationChanged(
401 new ActivityConfigChangeData(token, overrideConfig), newDisplayId);
402 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 }
404
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800405 public boolean isPreHoneycomb() {
406 if (activity != null) {
407 return activity.getApplicationInfo().targetSdkVersion
408 < android.os.Build.VERSION_CODES.HONEYCOMB;
409 }
410 return false;
411 }
412
Craig Mautnera0026042014-04-23 11:45:37 -0700413 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700414 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700415 }
416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700418 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 return "ActivityRecord{"
420 + Integer.toHexString(System.identityHashCode(this))
421 + " token=" + token + " " + (componentName == null
422 ? "no component name" : componentName.toShortString())
423 + "}";
424 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700425
426 public String getStateString() {
427 StringBuilder sb = new StringBuilder();
428 sb.append("ActivityClientRecord{");
429 sb.append("paused=").append(paused);
430 sb.append(", stopped=").append(stopped);
431 sb.append(", hideForNow=").append(hideForNow);
432 sb.append(", startsNotResumed=").append(startsNotResumed);
433 sb.append(", isForward=").append(isForward);
434 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
435 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
436 sb.append(", preserveWindow=").append(mPreserveWindow);
437 if (activity != null) {
438 sb.append(", Activity{");
439 sb.append("resumed=").append(activity.mResumed);
440 sb.append(", stopped=").append(activity.mStopped);
441 sb.append(", finished=").append(activity.isFinishing());
442 sb.append(", destroyed=").append(activity.isDestroyed());
443 sb.append(", startedActivity=").append(activity.mStartedActivity);
444 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
445 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
446 sb.append(", visibleBehind=").append(activity.mVisibleBehind);
447 sb.append("}");
448 }
449 sb.append("}");
450 return sb.toString();
451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 }
453
Wale Ogunwale1d646122015-04-24 14:45:14 -0700454 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700455 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 final IContentProvider mProvider;
457 final ContentProvider mLocalProvider;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700458 final ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700460 ProviderClientRecord(String[] names, IContentProvider provider,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700461 ContentProvider localProvider, ContentProviderHolder holder) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700462 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 mProvider = provider;
464 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700465 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
467 }
468
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400469 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800470 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 IBinder token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700472 boolean andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 public String toString() {
Wale Ogunwale826c7062016-09-13 08:25:54 -0700474 return "NewIntentData{intents=" + intents + " token=" + token
475 + " andPause=" + andPause +"}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 }
477 }
478
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400479 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700480 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700481 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
482 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800483 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700484 this.intent = intent;
485 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 Intent intent;
488 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400489 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public String toString() {
491 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700492 info.packageName + " resultCode=" + getResultCode()
493 + " resultData=" + getResultData() + " resultExtras="
494 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 }
496 }
497
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400498 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700499 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400500 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700501 int backupMode;
502 public String toString() {
503 return "CreateBackupAgentData{appInfo=" + appInfo
504 + " backupAgent=" + appInfo.backupAgentName
505 + " mode=" + backupMode + "}";
506 }
507 }
Bob Leee5408332009-09-04 18:31:17 -0700508
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400509 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 IBinder token;
511 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400512 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 Intent intent;
514 public String toString() {
515 return "CreateServiceData{token=" + token + " className="
516 + info.name + " packageName=" + info.packageName
517 + " intent=" + intent + "}";
518 }
519 }
520
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400521 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 IBinder token;
523 Intent intent;
524 boolean rebind;
525 public String toString() {
526 return "BindServiceData{token=" + token + " intent=" + intent + "}";
527 }
528 }
529
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400530 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700532 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700534 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 Intent args;
536 public String toString() {
537 return "ServiceArgsData{token=" + token + " startId=" + startId
538 + " args=" + args + "}";
539 }
540 }
541
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400542 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700543 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 String processName;
545 ApplicationInfo appInfo;
546 List<ProviderInfo> providers;
547 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 Bundle instrumentationArgs;
549 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800550 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400552 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700553 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700554 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700555 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400557 CompatibilityInfo compatInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700558 String buildSerial;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700559
560 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700561 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 public String toString() {
564 return "AppBindData{appInfo=" + appInfo + "}";
565 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700566 }
567
568 static final class Profiler {
569 String profileFile;
570 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700571 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700572 boolean autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800573 boolean streamingOutput;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700574 boolean profiling;
575 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700576 public void setProfiler(ProfilerInfo profilerInfo) {
577 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700578 if (profiling) {
579 if (fd != null) {
580 try {
581 fd.close();
582 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700583 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700584 }
585 }
586 return;
587 }
588 if (profileFd != null) {
589 try {
590 profileFd.close();
591 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700592 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700593 }
594 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700595 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700596 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700597 samplingInterval = profilerInfo.samplingInterval;
598 autoStopProfiler = profilerInfo.autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800599 streamingOutput = profilerInfo.streamingOutput;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700600 }
601 public void startProfiling() {
602 if (profileFd == null || profiling) {
603 return;
604 }
605 try {
Andreas Gampeeff06392016-05-10 12:51:45 -0700606 int bufferSize = SystemProperties.getInt("debug.traceview-buffer-size-mb", 8);
Jeff Hao1b012d32014-08-20 10:35:34 -0700607 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800608 bufferSize * 1024 * 1024, 0, samplingInterval != 0, samplingInterval,
609 streamingOutput);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700610 profiling = true;
611 } catch (RuntimeException e) {
612 Slog.w(TAG, "Profiling failed on path " + profileFile);
613 try {
614 profileFd.close();
615 profileFd = null;
616 } catch (IOException e2) {
617 Slog.w(TAG, "Failure closing profile fd", e2);
618 }
619 }
620 }
621 public void stopProfiling() {
622 if (profiling) {
623 profiling = false;
624 Debug.stopMethodTracing();
625 if (profileFd != null) {
626 try {
627 profileFd.close();
628 } catch (IOException e) {
629 }
630 }
631 profileFd = null;
632 profileFile = null;
633 }
634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400637 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700638 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700639 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800640 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400644 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 IBinder token;
646 List<ResultInfo> results;
647 public String toString() {
648 return "ResultData{token=" + token + " results" + results + "}";
649 }
650 }
651
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400652 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800653 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800654 String what;
655 String who;
656 }
657
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400658 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700659 String path;
660 ParcelFileDescriptor fd;
661 }
662
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400663 static final class UpdateCompatibilityData {
664 String pkg;
665 CompatibilityInfo info;
666 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800667
Adam Skorydfc7fd72013-08-05 19:23:41 -0700668 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800669 IBinder activityToken;
670 IBinder requestToken;
671 int requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700672 int sessionId;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800673 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700674
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800675 static final class ActivityConfigChangeData {
676 final IBinder activityToken;
677 final Configuration overrideConfig;
678 public ActivityConfigChangeData(IBinder token, Configuration config) {
679 activityToken = token;
680 overrideConfig = config;
681 }
682 }
683
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700684 private class ApplicationThread extends IApplicationThread.Stub {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700685 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700686
Dianne Hackborna413dc02013-07-12 12:02:55 -0700687 private int mLastProcessState = -1;
688
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700689 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700690 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700691 if (mPendingConfiguration == null ||
692 mPendingConfiguration.isOtherSeqNewer(config)) {
693 mPendingConfiguration = config;
694 }
695 }
696 }
697
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700699 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700700 int seq = getLifecycleSeq();
701 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
702 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700703 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
705 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700706 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
707 configChanges,
708 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
711 public final void scheduleStopActivity(IBinder token, boolean showWindow,
712 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700713 int seq = getLifecycleSeq();
714 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
715 + " operation received seq: " + seq);
716 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700718 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
721 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700722 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
724 token);
725 }
726
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800727 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700728 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800729 }
730
Dianne Hackborna413dc02013-07-12 12:02:55 -0700731 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800732 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700733 int seq = getLifecycleSeq();
734 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
735 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700736 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700737 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739
740 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
741 ResultData res = new ResultData();
742 res.token = token;
743 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700744 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746
747 // we use token to identify this activity without having to send the
748 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800749 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700750 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800751 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
752 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
753 int procState, Bundle state, PersistableBundle persistentState,
754 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
755 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700756
757 updateProcessState(procState, false);
758
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700759 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760
761 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700762 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800764 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700765 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400767 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700769 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
771 r.pendingResults = pendingResults;
772 r.pendingIntents = pendingNewIntents;
773
774 r.startsNotResumed = notResumed;
775 r.isForward = isForward;
776
Jeff Hao1b012d32014-08-20 10:35:34 -0700777 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700778
Wale Ogunwale60454db2015-01-23 16:05:07 -0800779 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700780 updatePendingConfiguration(curConfig);
781
Jeff Brown9ef09972013-10-15 20:49:59 -0700782 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
Wale Ogunwale60454db2015-01-23 16:05:07 -0800785 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800787 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800788 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700789 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800790 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700791 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 }
793
Wale Ogunwale826c7062016-09-13 08:25:54 -0700794 public final void scheduleNewIntent(
795 List<ReferrerIntent> intents, IBinder token, boolean andPause) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 NewIntentData data = new NewIntentData();
797 data.intents = intents;
798 data.token = token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700799 data.andPause = andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800
Jeff Brown9ef09972013-10-15 20:49:59 -0700801 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803
804 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
805 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700806 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 configChanges);
808 }
809
810 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400811 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700812 boolean sync, int sendingUser, int processState) {
813 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700814 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700815 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400817 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700818 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400821 public final void scheduleCreateBackupAgent(ApplicationInfo app,
822 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700823 CreateBackupAgentData d = new CreateBackupAgentData();
824 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400825 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700826 d.backupMode = backupMode;
827
Jeff Brown9ef09972013-10-15 20:49:59 -0700828 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700829 }
830
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400831 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
832 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700833 CreateBackupAgentData d = new CreateBackupAgentData();
834 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400835 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700836
Jeff Brown9ef09972013-10-15 20:49:59 -0700837 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700838 }
839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700841 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
842 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 CreateServiceData s = new CreateServiceData();
844 s.token = token;
845 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400846 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
Jeff Brown9ef09972013-10-15 20:49:59 -0700848 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850
851 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700852 boolean rebind, int processState) {
853 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 BindServiceData s = new BindServiceData();
855 s.token = token;
856 s.intent = intent;
857 s.rebind = rebind;
858
Amith Yamasani742a6712011-05-04 14:49:28 -0700859 if (DEBUG_SERVICE)
860 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
861 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700862 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
864
865 public final void scheduleUnbindService(IBinder token, Intent intent) {
866 BindServiceData s = new BindServiceData();
867 s.token = token;
868 s.intent = intent;
869
Jeff Brown9ef09972013-10-15 20:49:59 -0700870 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
Dianne Hackborn3f7c9f22017-04-04 15:36:33 -0700873 public final void scheduleServiceArgs(IBinder token, ParceledListSlice args) {
874 List<ServiceStartArgs> list = args.getList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875
Dianne Hackborn3f7c9f22017-04-04 15:36:33 -0700876 for (int i = 0; i < list.size(); i++) {
877 ServiceStartArgs ssa = list.get(i);
878 ServiceArgsData s = new ServiceArgsData();
879 s.token = token;
880 s.taskRemoved = ssa.taskRemoved;
881 s.startId = ssa.startId;
882 s.flags = ssa.flags;
883 s.args = ssa.args;
884
885 sendMessage(H.SERVICE_ARGS, s);
886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888
889 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700890 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892
Jeff Hao1b012d32014-08-20 10:35:34 -0700893 public final void bindApplication(String processName, ApplicationInfo appInfo,
894 List<ProviderInfo> providers, ComponentName instrumentationName,
895 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
896 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800897 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700898 boolean enableBinderTracking, boolean trackAllocation,
899 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700900 CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
Svet Ganov37e43272016-09-09 16:01:32 -0700901 String buildSerial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902
903 if (services != null) {
904 // Setup the service cache in the ServiceManager
905 ServiceManager.initServiceCache(services);
906 }
907
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800908 setCoreSettings(coreSettings);
909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 AppBindData data = new AppBindData();
911 data.processName = processName;
912 data.appInfo = appInfo;
913 data.providers = providers;
914 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 data.instrumentationArgs = instrumentationArgs;
916 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800917 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400919 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700920 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700921 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700922 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400924 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700925 data.initProfilerInfo = profilerInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700926 data.buildSerial = buildSerial;
Jeff Brown9ef09972013-10-15 20:49:59 -0700927 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929
930 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700931 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933
Christopher Tate5e1ab332009-09-01 20:32:49 -0700934 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700935 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700936 }
937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700939 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700940 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
942
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100943 public void scheduleApplicationInfoChanged(ApplicationInfo ai) {
944 sendMessage(H.APPLICATION_INFO_CHANGED, ai);
945 }
946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 public void updateTimeZone() {
948 TimeZone.setDefault(null);
949 }
950
Robert Greenwalt03595d02010-11-02 14:08:23 -0700951 public void clearDnsCache() {
952 // a non-standard API to get this to libcore
953 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400954 // Allow libcore to perform the necessary actions as it sees fit upon a network
955 // configuration change.
956 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700957 }
958
Jason Monk83520b92014-05-09 15:16:06 -0400959 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400960 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
961 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500962 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400963 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500964 } else {
965 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
966 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700967 }
968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969 public void processInBackground() {
970 mH.removeMessages(H.GC_WHEN_IDLE);
971 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
972 }
973
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700974 public void dumpService(ParcelFileDescriptor pfd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700975 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700976 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700977 data.fd = pfd.dup();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700978 data.token = servicetoken;
979 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700980 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700981 } catch (IOException e) {
982 Slog.w(TAG, "dumpService failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700983 } finally {
984 IoUtils.closeQuietly(pfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 }
987
988 // This function exists to make sure all receiver dispatching is
989 // correctly ordered, since these are one-way calls and the binder driver
990 // applies transaction ordering per object for such calls.
991 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700992 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700993 boolean sticky, int sendingUser, int processState) throws RemoteException {
994 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700995 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
996 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800997 }
Bob Leee5408332009-09-04 18:31:17 -0700998
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800999 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -07001001 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 }
1003
Wale Ogunwale60454db2015-01-23 16:05:07 -08001004 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001005 public void scheduleActivityConfigurationChanged(
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001006 IBinder token, Configuration overrideConfig) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001007 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001008 new ActivityConfigChangeData(token, overrideConfig));
1009 }
1010
1011 @Override
1012 public void scheduleActivityMovedToDisplay(IBinder token, int displayId,
1013 Configuration overrideConfig) {
1014 sendMessage(H.ACTIVITY_MOVED_TO_DISPLAY,
1015 new ActivityConfigChangeData(token, overrideConfig), displayId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 }
1017
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001018 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -07001019 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
1020 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001021 }
1022
Andy McFadden824c5102010-07-09 16:26:57 -07001023 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
1024 DumpHeapData dhd = new DumpHeapData();
1025 dhd.path = path;
1026 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -07001027 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -07001028 }
1029
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001030 public void attachAgent(String agent) {
1031 sendMessage(H.ATTACH_AGENT, agent);
1032 }
1033
Dianne Hackborn06de2ea2009-05-21 12:56:43 -07001034 public void setSchedulingGroup(int group) {
1035 // Note: do this immediately, since going into the foreground
1036 // should happen regardless of what pending work we have to do
1037 // and the activity manager will wait for us to report back that
1038 // we are done before sending us to the background.
1039 try {
1040 Process.setProcessGroup(Process.myPid(), group);
1041 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001042 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -07001043 }
1044 }
Bob Leee5408332009-09-04 18:31:17 -07001045
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001046 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001047 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001048 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001049
1050 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001051 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001052 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001053
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001054 public void dumpActivity(ParcelFileDescriptor pfd, IBinder activitytoken,
Dianne Hackborn30d71892010-12-11 10:37:55 -08001055 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07001056 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001057 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001058 data.fd = pfd.dup();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001059 data.token = activitytoken;
1060 data.prefix = prefix;
1061 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001062 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001063 } catch (IOException e) {
1064 Slog.w(TAG, "dumpActivity failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001065 } finally {
1066 IoUtils.closeQuietly(pfd);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001067 }
1068 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07001069
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001070 public void dumpProvider(ParcelFileDescriptor pfd, IBinder providertoken,
Marco Nelissen18cb2872011-11-15 11:19:53 -08001071 String[] args) {
1072 DumpComponentInfo data = new DumpComponentInfo();
1073 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001074 data.fd = pfd.dup();
Marco Nelissen18cb2872011-11-15 11:19:53 -08001075 data.token = providertoken;
1076 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001077 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001078 } catch (IOException e) {
1079 Slog.w(TAG, "dumpProvider failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001080 } finally {
1081 IoUtils.closeQuietly(pfd);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001082 }
1083 }
1084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001086 public void dumpMemInfo(ParcelFileDescriptor pfd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001087 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1088 boolean dumpUnreachable, String[] args) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001089 FileOutputStream fout = new FileOutputStream(pfd.getFileDescriptor());
Dianne Hackborn8c841092013-06-24 13:46:13 -07001090 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001091 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001092 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001093 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001094 pw.flush();
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001095 IoUtils.closeQuietly(pfd);
Chet Haase9c1e23b2011-03-24 10:51:31 -07001096 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001097 }
1098
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001099 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001100 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 long nativeMax = Debug.getNativeHeapSize() / 1024;
1102 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1103 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001106 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 long dalvikMax = runtime.totalMemory() / 1024;
1108 long dalvikFree = runtime.freeMemory() / 1024;
1109 long dalvikAllocated = dalvikMax - dalvikFree;
Richard Uhler2c036192016-09-14 09:48:31 +01001110
1111 Class[] classesToCount = new Class[] {
1112 ContextImpl.class,
1113 Activity.class,
1114 WebView.class,
1115 OpenSSLSocketImpl.class
1116 };
1117 long[] instanceCounts = VMDebug.countInstancesOfClasses(classesToCount, true);
1118 long appContextInstanceCount = instanceCounts[0];
1119 long activityInstanceCount = instanceCounts[1];
1120 long webviewInstanceCount = instanceCounts[2];
1121 long openSslSocketCount = instanceCounts[3];
1122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001124 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 int globalAssetCount = AssetManager.getGlobalAssetCount();
1126 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1127 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1128 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1129 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001130 long parcelSize = Parcel.getGlobalAllocSize();
1131 long parcelCount = Parcel.getGlobalAllocCount();
Vasu Noric3849202010-03-09 10:47:25 -08001132 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001133
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001134 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1135 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001136 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1137 nativeMax, nativeAllocated, nativeFree,
1138 dalvikMax, dalvikAllocated, dalvikFree);
1139
Dianne Hackbornb437e092011-08-05 17:50:29 -07001140 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 // NOTE: if you change anything significant below, also consider changing
1142 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 // Object counts
1145 pw.print(viewInstanceCount); pw.print(',');
1146 pw.print(viewRootInstanceCount); pw.print(',');
1147 pw.print(appContextInstanceCount); pw.print(',');
1148 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 pw.print(globalAssetCount); pw.print(',');
1151 pw.print(globalAssetManagerCount); pw.print(',');
1152 pw.print(binderLocalObjectCount); pw.print(',');
1153 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 pw.print(binderDeathObjectCount); pw.print(',');
1156 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001159 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001160 pw.print(stats.memoryUsed / 1024); pw.print(',');
1161 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001162 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001163 for (int i = 0; i < stats.dbStats.size(); i++) {
1164 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001165 pw.print(','); pw.print(dbStats.dbName);
1166 pw.print(','); pw.print(dbStats.pageSize);
1167 pw.print(','); pw.print(dbStats.dbSize);
1168 pw.print(','); pw.print(dbStats.lookaside);
1169 pw.print(','); pw.print(dbStats.cache);
1170 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001171 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001172 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001173
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001174 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
Bob Leee5408332009-09-04 18:31:17 -07001176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 pw.println(" ");
1178 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001179 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 viewRootInstanceCount);
1181
1182 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1183 "Activities:", activityInstanceCount);
1184
1185 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1186 "AssetManagers:", globalAssetManagerCount);
1187
1188 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1189 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001190 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1191 "Parcel count:", parcelCount);
1192 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1193 "OpenSSL Sockets:", openSslSocketCount);
Richard Uhler2c036192016-09-14 09:48:31 +01001194 printRow(pw, ONE_COUNT_COLUMN, "WebViews:", webviewInstanceCount);
Bob Leee5408332009-09-04 18:31:17 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 // SQLite mem info
1197 pw.println(" ");
1198 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001199 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1200 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1201 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001202 pw.println(" ");
1203 int N = stats.dbStats.size();
1204 if (N > 0) {
1205 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001206 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1207 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001208 for (int i = 0; i < N; i++) {
1209 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001210 printRow(pw, DB_INFO_FORMAT,
1211 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1212 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1213 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1214 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001215 }
1216 }
Bob Leee5408332009-09-04 18:31:17 -07001217
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001218 // Asset details.
1219 String assetAlloc = AssetManager.getAssetAllocations();
1220 if (assetAlloc != null) {
1221 pw.println(" ");
1222 pw.println(" Asset Allocations");
1223 pw.print(assetAlloc);
1224 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001225
1226 // Unreachable native memory
1227 if (dumpUnreachable) {
1228 boolean showContents = ((mBoundApplication != null)
1229 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1230 || android.os.Build.IS_DEBUGGABLE;
1231 pw.println(" ");
1232 pw.println(" Unreachable memory");
1233 pw.print(Debug.getUnreachableMemory(100, showContents));
1234 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001235 }
1236
1237 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001238 public void dumpGfxInfo(ParcelFileDescriptor pfd, String[] args) {
Chris Craikfc294242016-12-13 18:10:46 -08001239 nDumpGraphicsInfo(pfd.getFileDescriptor());
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001240 WindowManagerGlobal.getInstance().dumpGfxInfo(pfd.getFileDescriptor(), args);
1241 IoUtils.closeQuietly(pfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001242 }
1243
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001244 private void dumpDatabaseInfo(ParcelFileDescriptor pfd, String[] args) {
1245 PrintWriter pw = new FastPrintWriter(
1246 new FileOutputStream(pfd.getFileDescriptor()));
Jeff Brown6754ba22011-12-14 20:20:01 -08001247 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1248 SQLiteDebug.dump(printer, args);
1249 pw.flush();
1250 }
1251
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001252 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001253 public void dumpDbInfo(final ParcelFileDescriptor pfd, final String[] args) {
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001254 if (mSystemThread) {
Felipe Leme29de4922016-06-07 16:14:07 -07001255 // Ensure this invocation is asynchronous to prevent writer waiting if buffer cannot
1256 // be consumed. But it must duplicate the file descriptor first, since caller might
1257 // be closing it.
1258 final ParcelFileDescriptor dup;
1259 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001260 dup = pfd.dup();
Felipe Leme29de4922016-06-07 16:14:07 -07001261 } catch (IOException e) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001262 Log.w(TAG, "Could not dup FD " + pfd.getFileDescriptor().getInt$());
Felipe Leme29de4922016-06-07 16:14:07 -07001263 return;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001264 } finally {
1265 IoUtils.closeQuietly(pfd);
Felipe Leme29de4922016-06-07 16:14:07 -07001266 }
1267
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001268 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1269 @Override
1270 public void run() {
Felipe Lemec74972f2016-06-08 09:12:37 -07001271 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001272 dumpDatabaseInfo(dup, args);
Felipe Lemec74972f2016-06-08 09:12:37 -07001273 } finally {
1274 IoUtils.closeQuietly(dup);
1275 }
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001276 }
1277 });
1278 } else {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001279 dumpDatabaseInfo(pfd, args);
1280 IoUtils.closeQuietly(pfd);
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001281 }
1282 }
1283
1284 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001285 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001286 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001287 }
1288
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001289 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001290 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Felipe Leme0200d9e2017-01-24 15:10:26 -08001291 int requestType, int sessionId) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001292 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001293 cmd.activityToken = activityToken;
1294 cmd.requestToken = requestToken;
1295 cmd.requestType = requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -07001296 cmd.sessionId = sessionId;
Jeff Brown9ef09972013-10-15 20:49:59 -07001297 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001298 }
1299
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001300 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001301 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001302 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001303
1304 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1305 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1306 ucd.pkg = pkg;
1307 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001308 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001309 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001310
1311 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001312 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001313 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001314
Craig Mautner5eda9b32013-07-02 11:58:16 -07001315 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001316 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001317 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001318
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001319 public void scheduleOnNewActivityOptions(IBinder token, Bundle options) {
Craig Mautnereb8abf72014-07-02 15:04:09 -07001320 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001321 new Pair<IBinder, ActivityOptions>(token, ActivityOptions.fromBundle(options)));
Craig Mautnereb8abf72014-07-02 15:04:09 -07001322 }
1323
Dianne Hackborna413dc02013-07-12 12:02:55 -07001324 public void setProcessState(int state) {
1325 updateProcessState(state, true);
1326 }
1327
1328 public void updateProcessState(int processState, boolean fromIpc) {
1329 synchronized (this) {
1330 if (mLastProcessState != processState) {
1331 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001332 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1333 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1334 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1335 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1336 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1337 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1338 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1339 }
1340 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001341 if (false) {
1342 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1343 + (fromIpc ? " (from ipc": ""));
1344 }
1345 }
1346 }
1347 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001348
Sudheer Shanka84a48952017-03-08 18:19:01 -08001349 /**
1350 * Updates {@link #mNetworkBlockSeq}. This is used by ActivityManagerService to inform
1351 * the main thread that it needs to wait for the network rules to get updated before
1352 * launching an activity.
1353 */
1354 @Override
1355 public void setNetworkBlockSeq(long procStateSeq) {
1356 synchronized (mNetworkPolicyLock) {
1357 mNetworkBlockSeq = procStateSeq;
1358 }
1359 }
1360
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001361 @Override
1362 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001363 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001364 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001365
1366 @Override
Neil Fullerb7146fe2016-11-15 16:52:15 +00001367 public final void updateTimePrefs(int timeFormatPreference) {
1368 final Boolean timeFormatPreferenceBool;
1369 // For convenience we are using the Intent extra values.
1370 if (timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR) {
1371 timeFormatPreferenceBool = Boolean.FALSE;
1372 } else if (timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR) {
1373 timeFormatPreferenceBool = Boolean.TRUE;
1374 } else {
1375 // timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT
1376 // (or unknown).
1377 timeFormatPreferenceBool = null;
1378 }
1379 DateFormat.set24HourTimePref(timeFormatPreferenceBool);
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001380 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001381
1382 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001383 public void scheduleCancelVisibleBehind(IBinder token) {
1384 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001385 }
1386
1387 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001388 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1389 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001390 }
Craig Mautner8746a472014-07-24 15:12:54 -07001391
Jeff Sharkey605eb792014-11-04 13:34:06 -08001392 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001393 public void scheduleEnterAnimationComplete(IBinder token) {
1394 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1395 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001396
1397 @Override
1398 public void notifyCleartextNetwork(byte[] firstPacket) {
1399 if (StrictMode.vmCleartextNetworkEnabled()) {
1400 StrictMode.onCleartextNetworkDetected(firstPacket);
1401 }
1402 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001403
1404 @Override
1405 public void startBinderTracking() {
1406 sendMessage(H.START_BINDER_TRACKING, null);
1407 }
1408
1409 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001410 public void stopBinderTrackingAndDump(ParcelFileDescriptor pfd) {
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001411 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001412 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, pfd.dup());
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001413 } catch (IOException e) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001414 } finally {
1415 IoUtils.closeQuietly(pfd);
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001416 }
1417 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001418
1419 @Override
Winson Chung5af42fc2017-03-24 17:11:33 -07001420 public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode,
1421 Configuration overrideConfig) throws RemoteException {
1422 SomeArgs args = SomeArgs.obtain();
1423 args.arg1 = token;
1424 args.arg2 = overrideConfig;
1425 args.argi1 = isInMultiWindowMode ? 1 : 0;
1426 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, args);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001427 }
1428
1429 @Override
Winson Chung5af42fc2017-03-24 17:11:33 -07001430 public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode,
1431 Configuration overrideConfig) throws RemoteException {
1432 SomeArgs args = SomeArgs.obtain();
1433 args.arg1 = token;
1434 args.arg2 = overrideConfig;
1435 args.argi1 = isInPipMode ? 1 : 0;
1436 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, args);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001437 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001438
1439 @Override
1440 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1441 IVoiceInteractor voiceInteractor) throws RemoteException {
1442 SomeArgs args = SomeArgs.obtain();
1443 args.arg1 = token;
1444 args.arg2 = voiceInteractor;
1445 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1446 }
Chad Brubakerc72875b2016-04-27 16:35:11 -07001447
1448 @Override
1449 public void handleTrustStorageUpdate() {
1450 NetworkSecurityPolicy.getInstance().handleTrustStorageUpdate();
1451 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 }
1453
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001454 private int getLifecycleSeq() {
1455 synchronized (mResourcesManager) {
1456 return mLifecycleSeq++;
1457 }
1458 }
1459
Romain Guy65b345f2011-07-27 18:51:50 -07001460 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461 public static final int LAUNCH_ACTIVITY = 100;
1462 public static final int PAUSE_ACTIVITY = 101;
1463 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1464 public static final int STOP_ACTIVITY_SHOW = 103;
1465 public static final int STOP_ACTIVITY_HIDE = 104;
1466 public static final int SHOW_WINDOW = 105;
1467 public static final int HIDE_WINDOW = 106;
1468 public static final int RESUME_ACTIVITY = 107;
1469 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001470 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 public static final int BIND_APPLICATION = 110;
1472 public static final int EXIT_APPLICATION = 111;
1473 public static final int NEW_INTENT = 112;
1474 public static final int RECEIVER = 113;
1475 public static final int CREATE_SERVICE = 114;
1476 public static final int SERVICE_ARGS = 115;
1477 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 public static final int CONFIGURATION_CHANGED = 118;
1480 public static final int CLEAN_UP_CONTEXT = 119;
1481 public static final int GC_WHEN_IDLE = 120;
1482 public static final int BIND_SERVICE = 121;
1483 public static final int UNBIND_SERVICE = 122;
1484 public static final int DUMP_SERVICE = 123;
1485 public static final int LOW_MEMORY = 124;
1486 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1487 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001488 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001489 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001490 public static final int DESTROY_BACKUP_AGENT = 129;
1491 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001492 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001493 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001494 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001495 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001496 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001497 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001498 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001499 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001500 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001501 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001502 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001503 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001504 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001505 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001506 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001507 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001508 public static final int CANCEL_VISIBLE_BEHIND = 147;
1509 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001510 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001511 public static final int START_BINDER_TRACKING = 150;
1512 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Andrii Kulian933076d2016-03-29 17:04:42 -07001513 public static final int MULTI_WINDOW_MODE_CHANGED = 152;
1514 public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001515 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001516 public static final int ATTACH_AGENT = 155;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001517 public static final int APPLICATION_INFO_CHANGED = 156;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001518 public static final int ACTIVITY_MOVED_TO_DISPLAY = 157;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001521 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 switch (code) {
1523 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1524 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1525 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1526 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1527 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1528 case SHOW_WINDOW: return "SHOW_WINDOW";
1529 case HIDE_WINDOW: return "HIDE_WINDOW";
1530 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1531 case SEND_RESULT: return "SEND_RESULT";
1532 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1533 case BIND_APPLICATION: return "BIND_APPLICATION";
1534 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1535 case NEW_INTENT: return "NEW_INTENT";
1536 case RECEIVER: return "RECEIVER";
1537 case CREATE_SERVICE: return "CREATE_SERVICE";
1538 case SERVICE_ARGS: return "SERVICE_ARGS";
1539 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1541 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1542 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1543 case BIND_SERVICE: return "BIND_SERVICE";
1544 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1545 case DUMP_SERVICE: return "DUMP_SERVICE";
1546 case LOW_MEMORY: return "LOW_MEMORY";
1547 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001548 case ACTIVITY_MOVED_TO_DISPLAY: return "ACTIVITY_MOVED_TO_DISPLAY";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001550 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001551 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1552 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001553 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001554 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001555 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001556 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001557 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001558 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001559 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001560 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001561 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001562 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001563 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001564 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001565 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001566 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001567 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001568 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001569 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001570 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1571 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001572 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Andrii Kulian933076d2016-03-29 17:04:42 -07001573 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1574 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001575 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001576 case ATTACH_AGENT: return "ATTACH_AGENT";
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001577 case APPLICATION_INFO_CHANGED: return "APPLICATION_INFO_CHANGED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 }
1579 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001580 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001583 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 switch (msg.what) {
1585 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001586 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001587 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588
1589 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001590 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001591 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001592 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 } break;
1594 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001595 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001596 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001597 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001598 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001600 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001601 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001602 SomeArgs args = (SomeArgs) msg.obj;
1603 handlePauseActivity((IBinder) args.arg1, false,
1604 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1605 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001606 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001607 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001608 } break;
1609 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001610 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001611 SomeArgs args = (SomeArgs) msg.obj;
1612 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1613 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001614 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001615 } break;
1616 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001617 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001618 SomeArgs args = (SomeArgs) msg.obj;
1619 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001620 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001621 } break;
1622 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001623 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001624 SomeArgs args = (SomeArgs) msg.obj;
1625 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001626 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001627 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001629 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001631 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 break;
1633 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001634 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001636 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 break;
1638 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001639 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001640 SomeArgs args = (SomeArgs) msg.obj;
1641 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001642 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001643 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 break;
1645 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001646 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001648 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 break;
1650 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001651 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1653 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001654 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 break;
1656 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001657 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 AppBindData data = (AppBindData)msg.obj;
1659 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001660 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 break;
1662 case EXIT_APPLICATION:
1663 if (mInitialApplication != null) {
1664 mInitialApplication.onTerminate();
1665 }
1666 Looper.myLooper().quit();
1667 break;
1668 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001669 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001671 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 break;
1673 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001674 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001676 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001677 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 break;
1679 case CREATE_SERVICE:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001680 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001682 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 break;
1684 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001685 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001687 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 break;
1689 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001690 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001692 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693 break;
1694 case SERVICE_ARGS:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001695 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceStart: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001697 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 break;
1699 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001700 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001702 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001703 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001706 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001707 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Chong Zhang4951f9d2016-06-23 13:15:08 -07001708 mUpdatingSystemConfig = true;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001709 try {
1710 handleConfigurationChanged((Configuration) msg.obj, null);
1711 } finally {
1712 mUpdatingSystemConfig = false;
1713 }
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001714 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 break;
1716 case CLEAN_UP_CONTEXT:
1717 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1718 cci.context.performFinalCleanup(cci.who, cci.what);
1719 break;
1720 case GC_WHEN_IDLE:
1721 scheduleGcIdler();
1722 break;
1723 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001724 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 break;
1726 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001727 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001729 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 break;
1731 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001732 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001733 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001734 INVALID_DISPLAY);
1735 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1736 break;
1737 case ACTIVITY_MOVED_TO_DISPLAY:
1738 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityMovedToDisplay");
1739 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1740 msg.arg1 /* displayId */);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001741 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001743 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001744 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001745 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001746 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001747 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001748 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001749 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001750 break;
1751 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001752 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001753 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001754 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001755 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001756 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001757 Process.killProcess(Process.myPid());
1758 break;
1759 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001760 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001761 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001762 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001763 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001764 case ENABLE_JIT:
1765 ensureJitEnabled();
1766 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001767 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001768 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001769 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001770 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001771 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001772 case SCHEDULE_CRASH:
1773 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001774 case DUMP_HEAP:
1775 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1776 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001777 case DUMP_ACTIVITY:
1778 handleDumpActivity((DumpComponentInfo)msg.obj);
1779 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001780 case DUMP_PROVIDER:
1781 handleDumpProvider((DumpComponentInfo)msg.obj);
1782 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001783 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001784 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001785 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001786 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001787 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001788 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001789 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001790 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001791 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001792 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001793 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1794 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001795 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001796 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001797 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001798 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001799 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001800 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001801 case UNSTABLE_PROVIDER_DIED:
1802 handleUnstableProviderDied((IBinder)msg.obj, false);
1803 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001804 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1805 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001806 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001807 case TRANSLUCENT_CONVERSION_COMPLETE:
1808 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1809 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001810 case INSTALL_PROVIDER:
1811 handleInstallProvider((ProviderInfo) msg.obj);
1812 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001813 case ON_NEW_ACTIVITY_OPTIONS:
1814 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1815 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001816 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001817 case CANCEL_VISIBLE_BEHIND:
1818 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001819 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001820 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1821 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001822 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001823 case ENTER_ANIMATION_COMPLETE:
1824 handleEnterAnimationComplete((IBinder) msg.obj);
1825 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001826 case START_BINDER_TRACKING:
1827 handleStartBinderTracking();
1828 break;
1829 case STOP_BINDER_TRACKING_AND_DUMP:
1830 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1831 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001832 case MULTI_WINDOW_MODE_CHANGED:
Winson Chung5af42fc2017-03-24 17:11:33 -07001833 handleMultiWindowModeChanged((IBinder) ((SomeArgs) msg.obj).arg1,
1834 ((SomeArgs) msg.obj).argi1 == 1,
1835 (Configuration) ((SomeArgs) msg.obj).arg2);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001836 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001837 case PICTURE_IN_PICTURE_MODE_CHANGED:
Winson Chung5af42fc2017-03-24 17:11:33 -07001838 handlePictureInPictureModeChanged((IBinder) ((SomeArgs) msg.obj).arg1,
1839 ((SomeArgs) msg.obj).argi1 == 1,
1840 (Configuration) ((SomeArgs) msg.obj).arg2);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001841 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001842 case LOCAL_VOICE_INTERACTION_STARTED:
1843 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1844 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
Amith Yamasani61019112017-01-10 15:05:00 -08001845 break;
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001846 case ATTACH_AGENT:
1847 handleAttachAgent((String) msg.obj);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001848 break;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001849 case APPLICATION_INFO_CHANGED:
1850 mUpdatingSystemConfig = true;
1851 try {
1852 handleApplicationInfoChanged((ApplicationInfo) msg.obj);
1853 } finally {
1854 mUpdatingSystemConfig = false;
1855 }
1856 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001858 Object obj = msg.obj;
1859 if (obj instanceof SomeArgs) {
1860 ((SomeArgs) obj).recycle();
1861 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001862 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
Bob Leee5408332009-09-04 18:31:17 -07001864
Brian Carlstromed7e0072011-03-24 13:27:57 -07001865 private void maybeSnapshot() {
1866 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001867 // convert the *private* ActivityThread.PackageInfo to *public* known
1868 // android.content.pm.PackageInfo
1869 String packageName = mBoundApplication.info.mPackageName;
1870 android.content.pm.PackageInfo packageInfo = null;
1871 try {
1872 Context context = getSystemContext();
1873 if(context == null) {
1874 Log.e(TAG, "cannot get a valid context");
1875 return;
1876 }
1877 PackageManager pm = context.getPackageManager();
1878 if(pm == null) {
1879 Log.e(TAG, "cannot get a valid PackageManager");
1880 return;
1881 }
1882 packageInfo = pm.getPackageInfo(
1883 packageName, PackageManager.GET_ACTIVITIES);
1884 } catch (NameNotFoundException e) {
1885 Log.e(TAG, "cannot get package info for " + packageName, e);
1886 }
1887 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001888 }
1889 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 }
1891
Romain Guy65b345f2011-07-27 18:51:50 -07001892 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001893 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001895 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001896 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001897 if (mBoundApplication != null && mProfiler.profileFd != null
1898 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001899 stopProfiling = true;
1900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 if (a != null) {
1902 mNewActivities = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001903 IActivityManager am = ActivityManager.getService();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001904 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001906 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 TAG, "Reporting idle of " + a +
1908 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001909 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 if (a.activity != null && !a.activity.mFinished) {
1911 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001912 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001913 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001915 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001916 }
1917 }
1918 prev = a;
1919 a = a.nextIdle;
1920 prev.nextIdle = null;
1921 } while (a != null);
1922 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001923 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001924 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001925 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001926 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 return false;
1928 }
1929 }
1930
1931 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001932 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 public final boolean queueIdle() {
1934 doGcIfNeeded();
1935 return false;
1936 }
1937 }
1938
Romain Guy65b345f2011-07-27 18:51:50 -07001939 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001940 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001941 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001943 public static boolean isSystem() {
1944 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1945 }
1946
Svetoslavfbf0eca2015-05-01 16:52:41 -07001947 public static String currentOpPackageName() {
1948 ActivityThread am = currentActivityThread();
1949 return (am != null && am.getApplication() != null)
1950 ? am.getApplication().getOpPackageName() : null;
1951 }
1952
Romain Guy65b345f2011-07-27 18:51:50 -07001953 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001954 ActivityThread am = currentActivityThread();
1955 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001956 ? am.mBoundApplication.appInfo.packageName : null;
1957 }
1958
1959 public static String currentProcessName() {
1960 ActivityThread am = currentActivityThread();
1961 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001962 ? am.mBoundApplication.processName : null;
1963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964
Romain Guy65b345f2011-07-27 18:51:50 -07001965 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001966 ActivityThread am = currentActivityThread();
1967 return am != null ? am.mInitialApplication : null;
1968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001969
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001970 public static IPackageManager getPackageManager() {
1971 if (sPackageManager != null) {
1972 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1973 return sPackageManager;
1974 }
1975 IBinder b = ServiceManager.getService("package");
1976 //Slog.v("PackageManager", "default service binder = " + b);
1977 sPackageManager = IPackageManager.Stub.asInterface(b);
1978 //Slog.v("PackageManager", "default service = " + sPackageManager);
1979 return sPackageManager;
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981
Romain Guy65b345f2011-07-27 18:51:50 -07001982 private Configuration mMainThreadConfig = new Configuration();
Amith Yamasani4f128e42016-05-10 11:44:12 -07001983
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001984 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1985 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001986 if (config == null) {
1987 return null;
1988 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001989 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001990 mMainThreadConfig.setTo(config);
1991 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001992 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001993 }
1994 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001995 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001997 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001998 * Creates the top level resources for the given package. Will return an existing
1999 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002000 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07002001 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08002002 String[] libDirs, int displayId, LoadedApk pkgInfo) {
2003 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
2004 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08002005 }
2006
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002007 final Handler getHandler() {
2008 return mH;
2009 }
2010
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002011 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
2012 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07002013 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
2014 }
2015
2016 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
2017 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002018 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07002019 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002020 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002021 if (differentUser) {
2022 // Caching not supported across users
2023 ref = null;
2024 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 ref = mPackages.get(packageName);
2026 } else {
2027 ref = mResourcePackages.get(packageName);
2028 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002029
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002030 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002031 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07002032 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
2033 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 if (packageInfo != null && (packageInfo.mResources == null
2035 || packageInfo.mResources.getAssets().isUpToDate())) {
2036 if (packageInfo.isSecurityViolation()
2037 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
2038 throw new SecurityException(
2039 "Requesting code from " + packageName
2040 + " to be run in process "
2041 + mBoundApplication.processName
2042 + "/" + mBoundApplication.appInfo.uid);
2043 }
2044 return packageInfo;
2045 }
2046 }
2047
2048 ApplicationInfo ai = null;
2049 try {
2050 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07002051 PackageManager.GET_SHARED_LIBRARY_FILES
2052 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
2053 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002055 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 }
2057
2058 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002059 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061
2062 return null;
2063 }
2064
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002065 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
2066 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
2068 boolean securityViolation = includeCode && ai.uid != 0
2069 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002070 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07002071 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002072 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002073 if ((flags&(Context.CONTEXT_INCLUDE_CODE
2074 |Context.CONTEXT_IGNORE_SECURITY))
2075 == Context.CONTEXT_INCLUDE_CODE) {
2076 if (securityViolation) {
2077 String msg = "Requesting code from " + ai.packageName
2078 + " (with uid " + ai.uid + ")";
2079 if (mBoundApplication != null) {
2080 msg = msg + " to be run in process "
2081 + mBoundApplication.processName + " (with uid "
2082 + mBoundApplication.appInfo.uid + ")";
2083 }
2084 throw new SecurityException(msg);
2085 }
2086 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002087 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
2088 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
2090
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002091 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
2092 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002093 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002096 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07002097 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002098 WeakReference<LoadedApk> ref;
2099 if (includeCode) {
2100 ref = mPackages.get(packageName);
2101 } else {
2102 ref = mResourcePackages.get(packageName);
2103 }
2104 return ref != null ? ref.get() : null;
2105 }
2106 }
2107
Romain Guy65b345f2011-07-27 18:51:50 -07002108 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002109 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
2110 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002111 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07002112 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002113 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002114 if (differentUser) {
2115 // Caching not supported across users
2116 ref = null;
2117 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 ref = mPackages.get(aInfo.packageName);
2119 } else {
2120 ref = mResourcePackages.get(aInfo.packageName);
2121 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002122
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002123 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002124 if (packageInfo == null || (packageInfo.mResources != null
2125 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002126 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 : "Loading resource-only package ") + aInfo.packageName
2128 + " (in " + (mBoundApplication != null
2129 ? mBoundApplication.processName : null)
2130 + ")");
2131 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07002132 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002134 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00002135
2136 if (mSystemThread && "android".equals(aInfo.packageName)) {
2137 packageInfo.installSystemApplicationInfo(aInfo,
2138 getSystemContext().mPackageInfo.getClassLoader());
2139 }
2140
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002141 if (differentUser) {
2142 // Caching not supported across users
2143 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002145 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 } else {
2147 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002148 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 }
2150 }
2151 return packageInfo;
2152 }
2153 }
2154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07002156 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158
2159 public ApplicationThread getApplicationThread()
2160 {
2161 return mAppThread;
2162 }
2163
2164 public Instrumentation getInstrumentation()
2165 {
2166 return mInstrumentation;
2167 }
2168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002170 return mProfiler != null && mProfiler.profileFile != null
2171 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 }
2173
2174 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002175 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177
2178 public Looper getLooper() {
2179 return mLooper;
2180 }
2181
2182 public Application getApplication() {
2183 return mInitialApplication;
2184 }
Bob Leee5408332009-09-04 18:31:17 -07002185
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002186 public String getProcessName() {
2187 return mBoundApplication.processName;
2188 }
Bob Leee5408332009-09-04 18:31:17 -07002189
Dianne Hackborn21556372010-02-04 16:34:40 -08002190 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191 synchronized (this) {
2192 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002193 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002195 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 }
2198
Adam Lesinskia82b6262017-03-21 16:56:17 -07002199 public ContextImpl getSystemUiContext() {
2200 synchronized (this) {
2201 if (mSystemUiContext == null) {
2202 mSystemUiContext = ContextImpl.createSystemUiContext(this);
2203 }
2204 return mSystemUiContext;
2205 }
2206 }
2207
Narayan Kamath29564cd2014-08-07 10:57:40 +01002208 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002209 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002210 getSystemContext().installSystemApplicationInfo(info, classLoader);
Adam Lesinskia82b6262017-03-21 16:56:17 -07002211 getSystemUiContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002212
2213 // give ourselves a default profiler
2214 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002215 }
2216 }
2217
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002218 void ensureJitEnabled() {
2219 if (!mJitEnabled) {
2220 mJitEnabled = true;
2221 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2222 }
2223 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 void scheduleGcIdler() {
2226 if (!mGcIdlerScheduled) {
2227 mGcIdlerScheduled = true;
2228 Looper.myQueue().addIdleHandler(mGcIdler);
2229 }
2230 mH.removeMessages(H.GC_WHEN_IDLE);
2231 }
2232
2233 void unscheduleGcIdler() {
2234 if (mGcIdlerScheduled) {
2235 mGcIdlerScheduled = false;
2236 Looper.myQueue().removeIdleHandler(mGcIdler);
2237 }
2238 mH.removeMessages(H.GC_WHEN_IDLE);
2239 }
2240
2241 void doGcIfNeeded() {
2242 mGcIdlerScheduled = false;
2243 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002244 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002245 // + "m now=" + now);
2246 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002247 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002248 BinderInternal.forceGc("bg");
2249 }
2250 }
2251
Dianne Hackborne77187d2013-10-25 16:32:41 -07002252 private static final String HEAP_FULL_COLUMN
2253 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2254 private static final String HEAP_COLUMN
2255 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002256 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2257 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2258 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002259
2260 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002261 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002262
2263 static void printRow(PrintWriter pw, String format, Object...objs) {
2264 pw.println(String.format(format, objs));
2265 }
2266
2267 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002268 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2269 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002270 long nativeMax, long nativeAllocated, long nativeFree,
2271 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2272
2273 // For checkin, we print one long comma-separated list of values
2274 if (checkin) {
2275 // NOTE: if you change anything significant below, also consider changing
2276 // ACTIVITY_THREAD_CHECKIN_VERSION.
2277
2278 // Header
2279 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2280 pw.print(pid); pw.print(',');
2281 pw.print(processName); pw.print(',');
2282
2283 // Heap info - max
2284 pw.print(nativeMax); pw.print(',');
2285 pw.print(dalvikMax); pw.print(',');
2286 pw.print("N/A,");
2287 pw.print(nativeMax + dalvikMax); pw.print(',');
2288
2289 // Heap info - allocated
2290 pw.print(nativeAllocated); pw.print(',');
2291 pw.print(dalvikAllocated); pw.print(',');
2292 pw.print("N/A,");
2293 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2294
2295 // Heap info - free
2296 pw.print(nativeFree); pw.print(',');
2297 pw.print(dalvikFree); pw.print(',');
2298 pw.print("N/A,");
2299 pw.print(nativeFree + dalvikFree); pw.print(',');
2300
2301 // Heap info - proportional set size
2302 pw.print(memInfo.nativePss); pw.print(',');
2303 pw.print(memInfo.dalvikPss); pw.print(',');
2304 pw.print(memInfo.otherPss); pw.print(',');
2305 pw.print(memInfo.getTotalPss()); pw.print(',');
2306
2307 // Heap info - swappable set size
2308 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2309 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2310 pw.print(memInfo.otherSwappablePss); pw.print(',');
2311 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2312
2313 // Heap info - shared dirty
2314 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2315 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2316 pw.print(memInfo.otherSharedDirty); pw.print(',');
2317 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2318
2319 // Heap info - shared clean
2320 pw.print(memInfo.nativeSharedClean); pw.print(',');
2321 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2322 pw.print(memInfo.otherSharedClean); pw.print(',');
2323 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2324
2325 // Heap info - private Dirty
2326 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2327 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2328 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2329 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2330
2331 // Heap info - private Clean
2332 pw.print(memInfo.nativePrivateClean); pw.print(',');
2333 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2334 pw.print(memInfo.otherPrivateClean); pw.print(',');
2335 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2336
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002337 // Heap info - swapped out
2338 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2339 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2340 pw.print(memInfo.otherSwappedOut); pw.print(',');
2341 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2342
2343 // Heap info - swapped out pss
2344 if (memInfo.hasSwappedOutPss) {
2345 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2346 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2347 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2348 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2349 } else {
2350 pw.print("N/A,");
2351 pw.print("N/A,");
2352 pw.print("N/A,");
2353 pw.print("N/A,");
2354 }
2355
Dianne Hackborne77187d2013-10-25 16:32:41 -07002356 // Heap info - other areas
2357 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2358 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2359 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2360 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2361 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2362 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2363 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2364 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002365 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2366 if (memInfo.hasSwappedOutPss) {
2367 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2368 } else {
2369 pw.print("N/A,");
2370 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002371 }
2372 return;
2373 }
2374
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002375 if (!dumpSummaryOnly) {
2376 if (dumpFullInfo) {
2377 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002378 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2379 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002380 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002381 "Clean", "Clean", "Dirty",
2382 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002383 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2384 "------", "------", "------", "------", "------", "------");
2385 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2386 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2387 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002388 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2389 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002390 nativeMax, nativeAllocated, nativeFree);
2391 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2392 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2393 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002394 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2395 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002396 dalvikMax, dalvikAllocated, dalvikFree);
2397 } else {
2398 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002399 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2400 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002401 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2402 "Clean", "Dirty", "Size", "Alloc", "Free");
2403 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2404 "------", "------", "------", "------", "------");
2405 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2406 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002407 memInfo.nativePrivateClean,
2408 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2409 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002410 nativeMax, nativeAllocated, nativeFree);
2411 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2412 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002413 memInfo.dalvikPrivateClean,
2414 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2415 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002416 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002417 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002418
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002419 int otherPss = memInfo.otherPss;
2420 int otherSwappablePss = memInfo.otherSwappablePss;
2421 int otherSharedDirty = memInfo.otherSharedDirty;
2422 int otherPrivateDirty = memInfo.otherPrivateDirty;
2423 int otherSharedClean = memInfo.otherSharedClean;
2424 int otherPrivateClean = memInfo.otherPrivateClean;
2425 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002426 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002427
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002428 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002429 final int myPss = memInfo.getOtherPss(i);
2430 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2431 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2432 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2433 final int mySharedClean = memInfo.getOtherSharedClean(i);
2434 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2435 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002436 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002437 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002438 || mySharedClean != 0 || myPrivateClean != 0
2439 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002440 if (dumpFullInfo) {
2441 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2442 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002443 mySharedClean, myPrivateClean,
2444 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2445 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002446 } else {
2447 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2448 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002449 myPrivateClean,
2450 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2451 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002452 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002453 otherPss -= myPss;
2454 otherSwappablePss -= mySwappablePss;
2455 otherSharedDirty -= mySharedDirty;
2456 otherPrivateDirty -= myPrivateDirty;
2457 otherSharedClean -= mySharedClean;
2458 otherPrivateClean -= myPrivateClean;
2459 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002460 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002461 }
2462 }
2463
2464 if (dumpFullInfo) {
2465 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2466 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002467 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2468 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002469 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2470 memInfo.getTotalSwappablePss(),
2471 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2472 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002473 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2474 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002475 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2476 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002477 } else {
2478 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002479 otherPrivateDirty, otherPrivateClean,
2480 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002481 "", "", "");
2482 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2483 memInfo.getTotalPrivateDirty(),
2484 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002485 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002486 memInfo.getTotalSwappedOut(),
2487 nativeMax+dalvikMax,
2488 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2489 }
2490
2491 if (dumpDalvik) {
2492 pw.println(" ");
2493 pw.println(" Dalvik Details");
2494
2495 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2496 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2497 final int myPss = memInfo.getOtherPss(i);
2498 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2499 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2500 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2501 final int mySharedClean = memInfo.getOtherSharedClean(i);
2502 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2503 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002504 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002505 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002506 || mySharedClean != 0 || myPrivateClean != 0
2507 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002508 if (dumpFullInfo) {
2509 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2510 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002511 mySharedClean, myPrivateClean,
2512 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2513 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002514 } else {
2515 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2516 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002517 myPrivateClean,
2518 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2519 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002520 }
2521 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002522 }
2523 }
2524 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002525
2526 pw.println(" ");
2527 pw.println(" App Summary");
2528 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2529 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2530 printRow(pw, ONE_COUNT_COLUMN,
2531 "Java Heap:", memInfo.getSummaryJavaHeap());
2532 printRow(pw, ONE_COUNT_COLUMN,
2533 "Native Heap:", memInfo.getSummaryNativeHeap());
2534 printRow(pw, ONE_COUNT_COLUMN,
2535 "Code:", memInfo.getSummaryCode());
2536 printRow(pw, ONE_COUNT_COLUMN,
2537 "Stack:", memInfo.getSummaryStack());
2538 printRow(pw, ONE_COUNT_COLUMN,
2539 "Graphics:", memInfo.getSummaryGraphics());
2540 printRow(pw, ONE_COUNT_COLUMN,
2541 "Private Other:", memInfo.getSummaryPrivateOther());
2542 printRow(pw, ONE_COUNT_COLUMN,
2543 "System:", memInfo.getSummarySystem());
2544 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002545 if (memInfo.hasSwappedOutPss) {
2546 printRow(pw, TWO_COUNT_COLUMNS,
2547 "TOTAL:", memInfo.getSummaryTotalPss(),
2548 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2549 } else {
2550 printRow(pw, TWO_COUNT_COLUMNS,
2551 "TOTAL:", memInfo.getSummaryTotalPss(),
2552 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2553 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002554 }
2555
Jeff Hamilton52d32032011-01-08 15:31:26 -06002556 public void registerOnActivityPausedListener(Activity activity,
2557 OnActivityPausedListener listener) {
2558 synchronized (mOnPauseListeners) {
2559 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2560 if (list == null) {
2561 list = new ArrayList<OnActivityPausedListener>();
2562 mOnPauseListeners.put(activity, list);
2563 }
2564 list.add(listener);
2565 }
2566 }
2567
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002568 public void unregisterOnActivityPausedListener(Activity activity,
2569 OnActivityPausedListener listener) {
2570 synchronized (mOnPauseListeners) {
2571 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2572 if (list != null) {
2573 list.remove(listener);
2574 }
2575 }
2576 }
2577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 public final ActivityInfo resolveActivityInfo(Intent intent) {
2579 ActivityInfo aInfo = intent.resolveActivityInfo(
2580 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2581 if (aInfo == null) {
2582 // Throw an exception.
2583 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002584 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 }
2586 return aInfo;
2587 }
Bob Leee5408332009-09-04 18:31:17 -07002588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002591 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002592 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002593 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002594 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 r.intent = intent;
2596 r.state = state;
2597 r.parent = parent;
2598 r.embeddedID = id;
2599 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002600 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601 if (localLOGV) {
2602 ComponentName compname = intent.getComponent();
2603 String name;
2604 if (compname != null) {
2605 name = compname.toShortString();
2606 } else {
2607 name = "(Intent " + intent + ").getComponent() returned null";
2608 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002609 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 + ", comp=" + name
2611 + ", token=" + token);
2612 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002613 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615
2616 public final Activity getActivity(IBinder token) {
2617 return mActivities.get(token).activity;
2618 }
2619
2620 public final void sendActivityResult(
2621 IBinder token, String id, int requestCode,
2622 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002623 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002624 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002625 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2626 list.add(new ResultInfo(id, requestCode, resultCode, data));
2627 mAppThread.scheduleSendResult(token, list);
2628 }
2629
Jeff Brown9ef09972013-10-15 20:49:59 -07002630 private void sendMessage(int what, Object obj) {
2631 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 }
2633
Jeff Brown9ef09972013-10-15 20:49:59 -07002634 private void sendMessage(int what, Object obj, int arg1) {
2635 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 }
2637
Jeff Brown9ef09972013-10-15 20:49:59 -07002638 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2639 sendMessage(what, obj, arg1, arg2, false);
2640 }
2641
2642 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2643 if (DEBUG_MESSAGES) Slog.v(
2644 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2645 + ": " + arg1 + " / " + obj);
2646 Message msg = Message.obtain();
2647 msg.what = what;
2648 msg.obj = obj;
2649 msg.arg1 = arg1;
2650 msg.arg2 = arg2;
2651 if (async) {
2652 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002654 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 }
2656
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002657 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2658 if (DEBUG_MESSAGES) Slog.v(
2659 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2660 "seq= " + seq);
2661 Message msg = Message.obtain();
2662 msg.what = what;
2663 SomeArgs args = SomeArgs.obtain();
2664 args.arg1 = obj;
2665 args.argi1 = arg1;
2666 args.argi2 = arg2;
2667 args.argi3 = seq;
2668 msg.obj = args;
2669 mH.sendMessage(msg);
2670 }
2671
Dianne Hackborn21556372010-02-04 16:34:40 -08002672 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 String what) {
2674 ContextCleanupInfo cci = new ContextCleanupInfo();
2675 cci.context = context;
2676 cci.who = who;
2677 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002678 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 }
2680
Craig Mautner233ceee2014-05-09 17:05:11 -07002681 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2683
2684 ActivityInfo aInfo = r.activityInfo;
2685 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002686 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 Context.CONTEXT_INCLUDE_CODE);
2688 }
Bob Leee5408332009-09-04 18:31:17 -07002689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 ComponentName component = r.intent.getComponent();
2691 if (component == null) {
2692 component = r.intent.resolveActivity(
2693 mInitialApplication.getPackageManager());
2694 r.intent.setComponent(component);
2695 }
2696
2697 if (r.activityInfo.targetActivity != null) {
2698 component = new ComponentName(r.activityInfo.packageName,
2699 r.activityInfo.targetActivity);
2700 }
2701
Adam Lesinski4e862812016-11-21 16:02:24 -08002702 ContextImpl appContext = createBaseContextForActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002703 Activity activity = null;
2704 try {
Adam Lesinski4e862812016-11-21 16:02:24 -08002705 java.lang.ClassLoader cl = appContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 activity = mInstrumentation.newActivity(
2707 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002708 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002710 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 if (r.state != null) {
2712 r.state.setClassLoader(cl);
2713 }
2714 } catch (Exception e) {
2715 if (!mInstrumentation.onException(activity, e)) {
2716 throw new RuntimeException(
2717 "Unable to instantiate activity " + component
2718 + ": " + e.toString(), e);
2719 }
2720 }
2721
2722 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002723 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002724
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002725 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2726 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 TAG, r + ": app=" + app
2728 + ", appName=" + app.getPackageName()
2729 + ", pkg=" + r.packageInfo.getPackageName()
2730 + ", comp=" + r.intent.getComponent().toShortString()
2731 + ", dir=" + r.packageInfo.getAppDir());
2732
2733 if (activity != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002735 Configuration config = new Configuration(mCompatConfiguration);
Andrii Kuliand0ad9382016-04-18 20:54:20 -07002736 if (r.overrideConfig != null) {
2737 config.updateFrom(r.overrideConfig);
2738 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002739 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002740 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002741 Window window = null;
2742 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2743 window = r.mPendingRemoveWindow;
2744 r.mPendingRemoveWindow = null;
2745 r.mPendingRemoveWindowManager = null;
2746 }
Adam Lesinski4e862812016-11-21 16:02:24 -08002747 appContext.setOuterContext(activity);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002748 activity.attach(appContext, this, getInstrumentation(), r.token,
2749 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002750 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002751 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002752
Christopher Tateb70f3df2009-04-07 16:07:59 -07002753 if (customIntent != null) {
2754 activity.mIntent = customIntent;
2755 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002756 r.lastNonConfigurationInstances = null;
Sudheer Shanka84a48952017-03-08 18:19:01 -08002757 checkAndBlockForNetworkAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 activity.mStartedActivity = false;
2759 int theme = r.activityInfo.getThemeResource();
2760 if (theme != 0) {
2761 activity.setTheme(theme);
2762 }
2763
2764 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002765 if (r.isPersistable()) {
2766 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2767 } else {
2768 mInstrumentation.callActivityOnCreate(activity, r.state);
2769 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 if (!activity.mCalled) {
2771 throw new SuperNotCalledException(
2772 "Activity " + r.intent.getComponent().toShortString() +
2773 " did not call through to super.onCreate()");
2774 }
2775 r.activity = activity;
2776 r.stopped = true;
2777 if (!r.activity.mFinished) {
2778 activity.performStart();
2779 r.stopped = false;
2780 }
2781 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002782 if (r.isPersistable()) {
2783 if (r.state != null || r.persistentState != null) {
2784 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2785 r.persistentState);
2786 }
2787 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2789 }
2790 }
2791 if (!r.activity.mFinished) {
2792 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002793 if (r.isPersistable()) {
2794 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2795 r.persistentState);
2796 } else {
2797 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799 if (!activity.mCalled) {
2800 throw new SuperNotCalledException(
2801 "Activity " + r.intent.getComponent().toShortString() +
2802 " did not call through to super.onPostCreate()");
2803 }
2804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 }
2806 r.paused = true;
2807
2808 mActivities.put(r.token, r);
2809
2810 } catch (SuperNotCalledException e) {
2811 throw e;
2812
2813 } catch (Exception e) {
2814 if (!mInstrumentation.onException(activity, e)) {
2815 throw new RuntimeException(
2816 "Unable to start activity " + component
2817 + ": " + e.toString(), e);
2818 }
2819 }
2820
2821 return activity;
2822 }
2823
Sudheer Shanka84a48952017-03-08 18:19:01 -08002824 /**
2825 * Checks if {@link #mNetworkBlockSeq} is {@link #INVALID_PROC_STATE_SEQ} and if so, returns
2826 * immediately. Otherwise, makes a blocking call to ActivityManagerService to wait for the
2827 * network rules to get updated.
2828 */
2829 private void checkAndBlockForNetworkAccess() {
2830 synchronized (mNetworkPolicyLock) {
2831 if (mNetworkBlockSeq != INVALID_PROC_STATE_SEQ) {
2832 try {
2833 ActivityManager.getService().waitForNetworkStateUpdate(mNetworkBlockSeq);
2834 mNetworkBlockSeq = INVALID_PROC_STATE_SEQ;
2835 } catch (RemoteException ignored) {}
2836 }
2837 }
2838 }
2839
Adam Lesinski4e862812016-11-21 16:02:24 -08002840 private ContextImpl createBaseContextForActivity(ActivityClientRecord r) {
2841 final int displayId;
Craig Mautnere0a38842013-12-16 16:14:02 -08002842 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002843 displayId = ActivityManager.getService().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002844 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002845 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002846 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002847
Wale Ogunwale7c726682015-02-06 17:34:28 -08002848 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski4e862812016-11-21 16:02:24 -08002849 this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002850
2851 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002852 // For debugging purposes, if the activity's package name contains the value of
2853 // the "debug.use-second-display" system property as a substring, then show
2854 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002855 String pkgName = SystemProperties.get("debug.second-display.pkg");
2856 if (pkgName != null && !pkgName.isEmpty()
2857 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002858 for (int id : dm.getDisplayIds()) {
2859 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002860 Display display =
Bryce Lee609bf652017-02-09 16:50:13 -08002861 dm.getCompatibleDisplay(id, appContext.getResources());
Adam Lesinski4e862812016-11-21 16:02:24 -08002862 appContext = (ContextImpl) appContext.createDisplayContext(display);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002863 break;
2864 }
2865 }
2866 }
Adam Lesinski4e862812016-11-21 16:02:24 -08002867 return appContext;
Jeff Brownefd43bd2012-09-21 17:02:35 -07002868 }
2869
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002870 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 // If we are getting ready to gc after going to the background, well
2872 // we are back active so skip it.
2873 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002874 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875
Jeff Hao1b012d32014-08-20 10:35:34 -07002876 if (r.profilerInfo != null) {
2877 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002878 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002879 }
2880
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002881 // Make sure we are running with the most recent config.
2882 handleConfigurationChanged(null, null);
2883
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002884 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002886
Chet Haase0d1c27a2014-11-03 18:35:16 +00002887 // Initialize before creating the activity
2888 WindowManagerGlobal.initialize();
2889
Craig Mautner233ceee2014-05-09 17:05:11 -07002890 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002891
2892 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002893 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002894 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002895 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002896 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002897 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898
2899 if (!r.activity.mFinished && r.startsNotResumed) {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002900 // The activity manager actually wants this one to start out paused, because it
2901 // needs to be visible but isn't in the foreground. We accomplish this by going
2902 // through the normal startup (because activities expect to go through onResume()
2903 // the first time they run, before their window is displayed), and then pausing it.
2904 // However, in this case we do -not- need to do the full pause cycle (of freezing
2905 // and such) because the activity manager assumes it can just retain the current
2906 // state it has.
2907 performPauseActivityIfNeeded(r, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002909 // We need to keep around the original state, in case we need to be created again.
2910 // But we only do this for pre-Honeycomb apps, which always save their state when
2911 // pausing, so we can not have them save their state when restarting from a paused
2912 // state. For HC and later, we want to (and can) let the state be saved as the
2913 // normal part of stopping the activity.
2914 if (r.isPreHoneycomb()) {
2915 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 }
2918 } else {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002919 // 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 -08002920 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002921 ActivityManager.getService()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002922 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2923 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002925 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 }
2927 }
2928 }
2929
Filip Gruszczynski23493322015-07-29 17:02:59 -07002930 private void reportSizeConfigurations(ActivityClientRecord r) {
2931 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2932 if (configurations == null) {
2933 return;
2934 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002935 SparseIntArray horizontal = new SparseIntArray();
2936 SparseIntArray vertical = new SparseIntArray();
2937 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002938 for (int i = configurations.length - 1; i >= 0; i--) {
2939 Configuration config = configurations[i];
2940 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002941 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002942 }
2943 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002944 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002945 }
2946 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002947 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002948 }
2949 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002950 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002951 ActivityManager.getService().reportSizeConfigurations(r.token,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002952 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002953 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002954 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002955 }
2956
2957 }
2958
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002959 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 final int N = intents.size();
2961 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002962 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002964 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002965 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2967 }
2968 }
2969
Wale Ogunwale826c7062016-09-13 08:25:54 -07002970 void performNewIntents(IBinder token, List<ReferrerIntent> intents, boolean andPause) {
2971 final ActivityClientRecord r = mActivities.get(token);
2972 if (r == null) {
2973 return;
2974 }
2975
2976 final boolean resumed = !r.paused;
2977 if (resumed) {
2978 r.activity.mTemporaryPause = true;
2979 mInstrumentation.callActivityOnPause(r.activity);
2980 }
2981 deliverNewIntents(r, intents);
2982 if (resumed) {
2983 r.activity.performResume();
2984 r.activity.mTemporaryPause = false;
2985 }
2986
2987 if (r.paused && andPause) {
2988 // In this case the activity was in the paused state when we delivered the intent,
2989 // to guarantee onResume gets called after onNewIntent we temporarily resume the
2990 // activity and pause again as the caller wanted.
2991 performResumeActivity(token, false, "performNewIntents");
2992 performPauseActivityIfNeeded(r, "performNewIntents");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 }
2994 }
Bob Leee5408332009-09-04 18:31:17 -07002995
Romain Guy65b345f2011-07-27 18:51:50 -07002996 private void handleNewIntent(NewIntentData data) {
Wale Ogunwale826c7062016-09-13 08:25:54 -07002997 performNewIntents(data.token, data.intents, data.andPause);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 }
2999
Adam Skorydfc7fd72013-08-05 19:23:41 -07003000 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Felipe Leme640f30a2017-03-06 15:44:06 -08003001 // Filling for autofill has a few differences:
Felipe Leme1ca634a2016-11-28 17:21:21 -08003002 // - it does not need an AssistContent
3003 // - it does not call onProvideAssistData()
3004 // - it needs an IAutoFillCallback
Felipe Leme640f30a2017-03-06 15:44:06 -08003005 boolean forAutofill = cmd.requestType == ActivityManager.ASSIST_CONTEXT_AUTOFILL;
Felipe Leme1ca634a2016-11-28 17:21:21 -08003006
Felipe Leme640f30a2017-03-06 15:44:06 -08003007 // TODO(b/33197203): decide if lastSessionId logic applies to autofill sessions
Amith Yamasani4f128e42016-05-10 11:44:12 -07003008 if (mLastSessionId != cmd.sessionId) {
3009 // Clear the existing structures
3010 mLastSessionId = cmd.sessionId;
3011 for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
3012 AssistStructure structure = mLastAssistStructures.get(i).get();
3013 if (structure != null) {
3014 structure.clearSendChannel();
3015 }
3016 mLastAssistStructures.remove(i);
Dianne Hackborn782d4982015-07-08 17:36:37 -07003017 }
3018 }
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003019
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003020 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07003021 AssistStructure structure = null;
Felipe Leme640f30a2017-03-06 15:44:06 -08003022 AssistContent content = forAutofill ? null : new AssistContent();
Amith Yamasani858f98d2017-02-22 12:59:53 -08003023 final long startTime = SystemClock.uptimeMillis();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003024 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07003025 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003026 if (r != null) {
Felipe Leme640f30a2017-03-06 15:44:06 -08003027 if (!forAutofill) {
Felipe Leme1ca634a2016-11-28 17:21:21 -08003028 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003029 r.activity.onProvideAssistData(data);
Felipe Leme6d553872016-12-08 17:13:25 -08003030 referrer = r.activity.onProvideReferrer();
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003031 }
Felipe Leme640f30a2017-03-06 15:44:06 -08003032 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL || forAutofill) {
3033 structure = new AssistStructure(r.activity, forAutofill);
Adam Skory4aaed142015-04-22 11:29:31 -06003034 Intent activityIntent = r.activity.getIntent();
Felipe Leme640f30a2017-03-06 15:44:06 -08003035 // TODO(b/33197203): re-evaluate conditions below for autofill. In particular,
Felipe Leme1ca634a2016-11-28 17:21:21 -08003036 // FLAG_SECURE might be allowed on AUTO_FILL but not on AUTO_FILL_SAVE)
Felipe Leme6d553872016-12-08 17:13:25 -08003037 boolean notSecure = r.window == null ||
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07003038 (r.window.getAttributes().flags
Felipe Leme6d553872016-12-08 17:13:25 -08003039 & WindowManager.LayoutParams.FLAG_SECURE) == 0;
3040 if (activityIntent != null && notSecure) {
Felipe Leme640f30a2017-03-06 15:44:06 -08003041 if (!forAutofill) {
Felipe Leme1ca634a2016-11-28 17:21:21 -08003042 Intent intent = new Intent(activityIntent);
3043 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
3044 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
3045 intent.removeUnsafeExtras();
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003046 content.setDefaultIntent(intent);
3047 }
Adam Skory4aaed142015-04-22 11:29:31 -06003048 } else {
Felipe Leme640f30a2017-03-06 15:44:06 -08003049 if (!forAutofill) {
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003050 content.setDefaultIntent(new Intent());
3051 }
Adam Skory4aaed142015-04-22 11:29:31 -06003052 }
Felipe Leme640f30a2017-03-06 15:44:06 -08003053 if (!forAutofill) {
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003054 r.activity.onProvideAssistContent(content);
3055 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07003056 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003057 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07003058 if (structure == null) {
3059 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003060 }
Felipe Leme0200d9e2017-01-24 15:10:26 -08003061
Felipe Leme640f30a2017-03-06 15:44:06 -08003062 // TODO(b/33197203): decide if lastSessionId logic applies to autofill sessions
Amith Yamasani858f98d2017-02-22 12:59:53 -08003063
3064 structure.setAcquisitionStartTime(startTime);
3065 structure.setAcquisitionEndTime(SystemClock.uptimeMillis());
3066
Amith Yamasani4f128e42016-05-10 11:44:12 -07003067 mLastAssistStructures.add(new WeakReference<>(structure));
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003068 IActivityManager mgr = ActivityManager.getService();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003069 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07003070 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003071 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003072 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003073 }
3074 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07003075
3076 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
3077 ActivityClientRecord r = mActivities.get(token);
3078 if (r != null) {
3079 r.activity.onTranslucentConversionComplete(drawComplete);
3080 }
3081 }
3082
Craig Mautnereb8abf72014-07-02 15:04:09 -07003083 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
3084 ActivityClientRecord r = mActivities.get(token);
3085 if (r != null) {
3086 r.activity.onNewActivityOptions(options);
3087 }
3088 }
3089
Jose Lima4b6c6692014-08-12 17:41:12 -07003090 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003091 ActivityClientRecord r = mActivities.get(token);
3092 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003093 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003094 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07003095 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003096 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07003097 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003098 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
3099 if (!activity.mCalled) {
3100 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07003101 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07003102 }
Jose Lima4b6c6692014-08-12 17:41:12 -07003103 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07003104 }
3105 }
3106 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003107 ActivityManager.getService().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003108 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003109 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07003110 }
3111 }
3112
Jose Lima4b6c6692014-08-12 17:41:12 -07003113 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07003114 ActivityClientRecord r = mActivities.get(token);
3115 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07003116 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07003117 }
3118 }
3119
Jeff Sharkeydd97f422013-10-08 17:01:30 -07003120 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07003121 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3122 try {
3123 installContentProviders(mInitialApplication, Lists.newArrayList(info));
3124 } finally {
3125 StrictMode.setThreadPolicy(oldPolicy);
3126 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07003127 }
3128
Craig Mautner8746a472014-07-24 15:12:54 -07003129 private void handleEnterAnimationComplete(IBinder token) {
3130 ActivityClientRecord r = mActivities.get(token);
3131 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08003132 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07003133 }
3134 }
3135
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04003136 private void handleStartBinderTracking() {
3137 Binder.enableTracing();
3138 }
3139
3140 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
3141 try {
3142 Binder.disableTracing();
3143 Binder.getTransactionTracker().writeTracesToFile(fd);
3144 } finally {
3145 IoUtils.closeQuietly(fd);
3146 Binder.getTransactionTracker().clearTraces();
3147 }
3148 }
3149
Winson Chung5af42fc2017-03-24 17:11:33 -07003150 private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode,
3151 Configuration overrideConfig) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08003152 final ActivityClientRecord r = mActivities.get(token);
3153 if (r != null) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003154 final Configuration newConfig = new Configuration(mConfiguration);
3155 if (overrideConfig != null) {
3156 newConfig.updateFrom(overrideConfig);
3157 }
3158 r.activity.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003159 }
3160 }
3161
Winson Chung5af42fc2017-03-24 17:11:33 -07003162 private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode,
3163 Configuration overrideConfig) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08003164 final ActivityClientRecord r = mActivities.get(token);
3165 if (r != null) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003166 final Configuration newConfig = new Configuration(mConfiguration);
3167 if (overrideConfig != null) {
3168 newConfig.updateFrom(overrideConfig);
3169 }
3170 r.activity.dispatchPictureInPictureModeChanged(isInPipMode, newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003171 }
3172 }
3173
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003174 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
3175 final ActivityClientRecord r = mActivities.get(token);
3176 if (r != null) {
3177 r.voiceInteractor = interactor;
3178 r.activity.setVoiceInteractor(interactor);
3179 if (interactor == null) {
3180 r.activity.onLocalVoiceInteractionStopped();
3181 } else {
3182 r.activity.onLocalVoiceInteractionStarted();
3183 }
3184 }
3185 }
3186
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07003187 static final void handleAttachAgent(String agent) {
3188 try {
3189 VMDebug.attachAgent(agent);
3190 } catch (IOException e) {
3191 Slog.e(TAG, "Attaching agent failed: " + agent);
3192 }
3193 }
3194
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003195 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
3196
3197 /**
3198 * Return the Intent that's currently being handled by a
3199 * BroadcastReceiver on this thread, or null if none.
3200 * @hide
3201 */
3202 public static Intent getIntentBeingBroadcast() {
3203 return sCurrentBroadcastIntent.get();
3204 }
3205
Romain Guy65b345f2011-07-27 18:51:50 -07003206 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 // If we are getting ready to gc after going to the background, well
3208 // we are back active so skip it.
3209 unscheduleGcIdler();
3210
3211 String component = data.intent.getComponent().getClassName();
3212
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003213 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003214 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003216 IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217
Adam Lesinski4e862812016-11-21 16:02:24 -08003218 Application app;
Romain Guy65b345f2011-07-27 18:51:50 -07003219 BroadcastReceiver receiver;
Adam Lesinski4e862812016-11-21 16:02:24 -08003220 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 try {
Adam Lesinski4e862812016-11-21 16:02:24 -08003222 app = packageInfo.makeApplication(false, mInstrumentation);
3223 context = (ContextImpl) app.getBaseContext();
3224 if (data.info.splitName != null) {
3225 context = (ContextImpl) context.createContextForSplit(data.info.splitName);
3226 }
3227 java.lang.ClassLoader cl = context.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003229 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07003230 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
3232 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003233 if (DEBUG_BROADCAST) Slog.i(TAG,
3234 "Finishing failed broadcast to " + data.intent.getComponent());
3235 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003236 throw new RuntimeException(
3237 "Unable to instantiate receiver " + component
3238 + ": " + e.toString(), e);
3239 }
3240
3241 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003242 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 TAG, "Performing receive of " + data.intent
3244 + ": app=" + app
3245 + ", appName=" + app.getPackageName()
3246 + ", pkg=" + packageInfo.getPackageName()
3247 + ", comp=" + data.intent.getComponent().toShortString()
3248 + ", dir=" + packageInfo.getAppDir());
3249
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003250 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07003251 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003252 receiver.onReceive(context.getReceiverRestrictedContext(),
3253 data.intent);
3254 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003255 if (DEBUG_BROADCAST) Slog.i(TAG,
3256 "Finishing failed broadcast to " + data.intent.getComponent());
3257 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 if (!mInstrumentation.onException(receiver, e)) {
3259 throw new RuntimeException(
3260 "Unable to start receiver " + component
3261 + ": " + e.toString(), e);
3262 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003263 } finally {
3264 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266
Dianne Hackborne829fef2010-10-26 17:44:01 -07003267 if (receiver.getPendingResult() != null) {
3268 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003269 }
3270 }
3271
Christopher Tate181fafa2009-05-14 11:12:14 -07003272 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003273 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003274 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003275
Christopher Tate346acb12012-10-15 19:20:25 -07003276 // Sanity check the requested target package's uid against ours
3277 try {
3278 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3279 data.appInfo.packageName, 0, UserHandle.myUserId());
3280 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3281 Slog.w(TAG, "Asked to instantiate non-matching package "
3282 + data.appInfo.packageName);
3283 return;
3284 }
3285 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003286 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003287 }
3288
Christopher Tate181fafa2009-05-14 11:12:14 -07003289 // no longer idle; we have backup work to do
3290 unscheduleGcIdler();
3291
3292 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003293 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003294 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003295 if (packageName == null) {
3296 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3297 return;
3298 }
3299
Christopher Tate181fafa2009-05-14 11:12:14 -07003300 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003301 // full backup operation but no app-supplied agent? use the default implementation
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003302 if (classname == null && (data.backupMode == ApplicationThreadConstants.BACKUP_MODE_FULL
3303 || data.backupMode == ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003304 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003305 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003306
Christopher Tate181fafa2009-05-14 11:12:14 -07003307 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003308 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003309 BackupAgent agent = mBackupAgents.get(packageName);
3310 if (agent != null) {
3311 // reusing the existing instance
3312 if (DEBUG_BACKUP) {
3313 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003314 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003315 binder = agent.onBind();
3316 } else {
3317 try {
3318 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3319
3320 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3321 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3322
3323 // set up the agent's context
3324 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3325 context.setOuterContext(agent);
3326 agent.attach(context);
3327
3328 agent.onCreate();
3329 binder = agent.onBind();
3330 mBackupAgents.put(packageName, agent);
3331 } catch (Exception e) {
3332 // If this is during restore, fail silently; otherwise go
3333 // ahead and let the user see the crash.
3334 Slog.e(TAG, "Agent threw during creation: " + e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003335 if (data.backupMode != ApplicationThreadConstants.BACKUP_MODE_RESTORE
3336 && data.backupMode !=
3337 ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL) {
Christopher Tate2e40d112014-07-15 12:37:38 -07003338 throw e;
3339 }
3340 // falling through with 'binder' still null
3341 }
Christopher Tated1475e02009-07-09 15:36:17 -07003342 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003343
3344 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003345 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003346 ActivityManager.getService().backupAgentCreated(packageName, binder);
Christopher Tate181fafa2009-05-14 11:12:14 -07003347 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003348 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003349 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003350 } catch (Exception e) {
3351 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003352 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003353 }
3354 }
3355
3356 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003357 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003358 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003359
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003360 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003361 String packageName = packageInfo.mPackageName;
3362 BackupAgent agent = mBackupAgents.get(packageName);
3363 if (agent != null) {
3364 try {
3365 agent.onDestroy();
3366 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003367 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003368 e.printStackTrace();
3369 }
3370 mBackupAgents.remove(packageName);
3371 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003372 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003373 }
3374 }
3375
Romain Guy65b345f2011-07-27 18:51:50 -07003376 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 // If we are getting ready to gc after going to the background, well
3378 // we are back active so skip it.
3379 unscheduleGcIdler();
3380
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003381 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003382 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003383 Service service = null;
3384 try {
3385 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3386 service = (Service) cl.loadClass(data.info.name).newInstance();
3387 } catch (Exception e) {
3388 if (!mInstrumentation.onException(service, e)) {
3389 throw new RuntimeException(
3390 "Unable to instantiate service " + data.info.name
3391 + ": " + e.toString(), e);
3392 }
3393 }
3394
3395 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003396 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397
Jeff Browndefd4a62014-03-10 21:24:37 -07003398 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3399 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003401 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 service.attach(context, this, data.info.name, data.token, app,
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003403 ActivityManager.getService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 service.onCreate();
3405 mServices.put(data.token, service);
3406 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003407 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003408 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003409 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003410 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 }
3412 } catch (Exception e) {
3413 if (!mInstrumentation.onException(service, e)) {
3414 throw new RuntimeException(
3415 "Unable to create service " + data.info.name
3416 + ": " + e.toString(), e);
3417 }
3418 }
3419 }
3420
Romain Guy65b345f2011-07-27 18:51:50 -07003421 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003423 if (DEBUG_SERVICE)
3424 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 if (s != null) {
3426 try {
3427 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003428 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 try {
3430 if (!data.rebind) {
3431 IBinder binder = s.onBind(data.intent);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003432 ActivityManager.getService().publishService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 data.token, data.intent, binder);
3434 } else {
3435 s.onRebind(data.intent);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003436 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003437 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003439 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003441 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 }
3443 } catch (Exception e) {
3444 if (!mInstrumentation.onException(s, e)) {
3445 throw new RuntimeException(
3446 "Unable to bind to service " + s
3447 + " with " + data.intent + ": " + e.toString(), e);
3448 }
3449 }
3450 }
3451 }
3452
Romain Guy65b345f2011-07-27 18:51:50 -07003453 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 Service s = mServices.get(data.token);
3455 if (s != null) {
3456 try {
3457 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003458 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459 boolean doRebind = s.onUnbind(data.intent);
3460 try {
3461 if (doRebind) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003462 ActivityManager.getService().unbindFinished(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 data.token, data.intent, doRebind);
3464 } else {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003465 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003466 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003467 }
3468 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003469 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
3471 } catch (Exception e) {
3472 if (!mInstrumentation.onException(s, e)) {
3473 throw new RuntimeException(
3474 "Unable to unbind to service " + s
3475 + " with " + data.intent + ": " + e.toString(), e);
3476 }
3477 }
3478 }
3479 }
3480
Dianne Hackborn625ac272010-09-17 18:29:22 -07003481 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003482 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3483 try {
3484 Service s = mServices.get(info.token);
3485 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003486 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3487 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003488 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3489 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003490 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003491 } finally {
3492 IoUtils.closeQuietly(info.fd);
3493 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 }
3495 }
3496
Dianne Hackborn625ac272010-09-17 18:29:22 -07003497 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003498 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3499 try {
3500 ActivityClientRecord r = mActivities.get(info.token);
3501 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003502 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3503 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003504 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3505 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003506 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003507 } finally {
3508 IoUtils.closeQuietly(info.fd);
3509 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003510 }
3511 }
3512
Marco Nelissen18cb2872011-11-15 11:19:53 -08003513 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003514 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3515 try {
3516 ProviderClientRecord r = mLocalProviders.get(info.token);
3517 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003518 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3519 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003520 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3521 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003522 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003523 } finally {
3524 IoUtils.closeQuietly(info.fd);
3525 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003526 }
3527 }
3528
Romain Guy65b345f2011-07-27 18:51:50 -07003529 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 Service s = mServices.get(data.token);
3531 if (s != null) {
3532 try {
3533 if (data.args != null) {
3534 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003535 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003536 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003537 int res;
3538 if (!data.taskRemoved) {
3539 res = s.onStartCommand(data.args, data.flags, data.startId);
3540 } else {
3541 s.onTaskRemoved(data.args);
3542 res = Service.START_TASK_REMOVED_COMPLETE;
3543 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003544
3545 QueuedWork.waitToFinish();
3546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003547 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003548 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003549 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003551 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003553 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003554 } catch (Exception e) {
3555 if (!mInstrumentation.onException(s, e)) {
3556 throw new RuntimeException(
3557 "Unable to start service " + s
3558 + " with " + data.args + ": " + e.toString(), e);
3559 }
3560 }
3561 }
3562 }
3563
Romain Guy65b345f2011-07-27 18:51:50 -07003564 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 Service s = mServices.remove(token);
3566 if (s != null) {
3567 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003568 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 s.onDestroy();
3570 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003571 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003573 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003575
3576 QueuedWork.waitToFinish();
3577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003578 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003579 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003580 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003582 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 }
3584 } catch (Exception e) {
3585 if (!mInstrumentation.onException(s, e)) {
3586 throw new RuntimeException(
3587 "Unable to stop service " + s
3588 + ": " + e.toString(), e);
3589 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003590 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003592 } else {
3593 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003595 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 }
3597
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003598 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003599 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003600 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003601 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 + " finished=" + r.activity.mFinished);
3603 if (r != null && !r.activity.mFinished) {
3604 if (clearHide) {
3605 r.hideForNow = false;
3606 r.activity.mStartedActivity = false;
3607 }
3608 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003609 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003610 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 if (r.pendingIntents != null) {
3612 deliverNewIntents(r, r.pendingIntents);
3613 r.pendingIntents = null;
3614 }
3615 if (r.pendingResults != null) {
3616 deliverResults(r, r.pendingResults);
3617 r.pendingResults = null;
3618 }
3619 r.activity.performResume();
3620
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01003621 synchronized (mResourcesManager) {
3622 // If there is a pending local relaunch that was requested when the activity was
3623 // paused, it will put the activity into paused state when it finally happens.
3624 // Since the activity resumed before being relaunched, we don't want that to
3625 // happen, so we need to clear the request to relaunch paused.
3626 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3627 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3628 if (relaunching.token == r.token
3629 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3630 relaunching.startsNotResumed = false;
3631 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003632 }
3633 }
3634
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003635 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3636 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 r.paused = false;
3639 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003641 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 } catch (Exception e) {
3643 if (!mInstrumentation.onException(r.activity, e)) {
3644 throw new RuntimeException(
3645 "Unable to resume activity "
3646 + r.intent.getComponent().toShortString()
3647 + ": " + e.toString(), e);
3648 }
3649 }
3650 }
3651 return r;
3652 }
3653
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003654 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3655 if (r.mPreserveWindow && !force) {
3656 return;
3657 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003658 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003659 r.mPendingRemoveWindowManager.removeViewImmediate(
3660 r.mPendingRemoveWindow.getDecorView());
3661 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003662 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003663 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003664 r.activity.getClass().getName(), "Activity");
3665 }
3666 }
3667 r.mPendingRemoveWindow = null;
3668 r.mPendingRemoveWindowManager = null;
3669 }
3670
Craig Mautner233ceee2014-05-09 17:05:11 -07003671 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003672 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003673 ActivityClientRecord r = mActivities.get(token);
3674 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3675 return;
3676 }
3677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 // If we are getting ready to gc after going to the background, well
3679 // we are back active so skip it.
3680 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003681 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682
Adam Powellcfbe9be2013-11-06 14:58:58 -08003683 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003684 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685
3686 if (r != null) {
3687 final Activity a = r.activity;
3688
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003689 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 TAG, "Resume " + r + " started activity: " +
3691 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3692 + ", finished: " + a.mFinished);
3693
3694 final int forwardBit = isForward ?
3695 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 // If the window hasn't yet been added to the window manager,
3698 // and this guy didn't finish itself or start another activity,
3699 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003700 boolean willBeVisible = !a.mStartedActivity;
3701 if (!willBeVisible) {
3702 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003703 willBeVisible = ActivityManager.getService().willActivityBeVisible(
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003704 a.getActivityToken());
3705 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003706 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003707 }
3708 }
3709 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 r.window = r.activity.getWindow();
3711 View decor = r.window.getDecorView();
3712 decor.setVisibility(View.INVISIBLE);
3713 ViewManager wm = a.getWindowManager();
3714 WindowManager.LayoutParams l = r.window.getAttributes();
3715 a.mDecor = decor;
3716 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3717 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003718 if (r.mPreserveWindow) {
3719 a.mWindowAdded = true;
3720 r.mPreserveWindow = false;
Robert Carrb2594852016-04-25 16:21:13 -07003721 // Normally the ViewRoot sets up callbacks with the Activity
3722 // in addView->ViewRootImpl#setView. If we are instead reusing
3723 // the decor view we have to notify the view root that the
3724 // callbacks may have changed.
3725 ViewRootImpl impl = decor.getViewRootImpl();
Robert Carr311ecba2016-04-27 12:59:45 -07003726 if (impl != null) {
3727 impl.notifyChildRebuilt();
3728 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003729 }
Bryce Lee4b51b322017-04-07 10:27:19 -07003730 if (a.mVisibleFromClient) {
3731 if (!a.mWindowAdded) {
3732 a.mWindowAdded = true;
3733 wm.addView(decor, l);
3734 } else {
3735 // The activity will get a callback for this {@link LayoutParams} change
3736 // earlier. However, at that time the decor will not be set (this is set
3737 // in this method), so no action will be taken. This call ensures the
3738 // callback occurs with the decor set.
3739 a.onWindowAttributesChanged(l);
3740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 }
3742
3743 // If the window has already been added, but during resume
3744 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003745 // window visible.
3746 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003747 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003748 TAG, "Launch " + r + " mStartedActivity set");
3749 r.hideForNow = true;
3750 }
3751
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003752 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003753 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 // The window is now visible if it has been added, we are not
3756 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003757 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003758 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 if (r.newConfig != null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08003760 performConfigurationChangedForActivity(r, r.newConfig);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003761 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003762 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 r.newConfig = null;
3764 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003765 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 + isForward);
3767 WindowManager.LayoutParams l = r.window.getAttributes();
3768 if ((l.softInputMode
3769 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3770 != forwardBit) {
3771 l.softInputMode = (l.softInputMode
3772 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3773 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003774 if (r.activity.mVisibleFromClient) {
3775 ViewManager wm = a.getWindowManager();
3776 View decor = r.window.getDecorView();
3777 wm.updateViewLayout(decor, l);
3778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 }
Bryce Lee4b51b322017-04-07 10:27:19 -07003780
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 r.activity.mVisibleFromServer = true;
3782 mNumVisibleActivities++;
3783 if (r.activity.mVisibleFromClient) {
3784 r.activity.makeVisible();
3785 }
Andrii Kulian44607962017-03-16 11:06:24 -07003786 final ViewRootImpl viewRoot = r.activity.mDecor.getViewRootImpl();
3787 if (viewRoot != null) {
3788 // TODO: Figure out the best place to set the callback.
3789 // This looks like a place where decor view is already initialized.
3790 viewRoot.setActivityConfigCallback(r.configCallback);
3791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 }
3793
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003794 if (!r.onlyLocalRequest) {
3795 r.nextIdle = mNewActivities;
3796 mNewActivities = r;
3797 if (localLOGV) Slog.v(
3798 TAG, "Scheduling idle handler for " + r);
3799 Looper.myQueue().addIdleHandler(new Idler());
3800 }
3801 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003802
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003803 // Tell the activity manager we have resumed.
3804 if (reallyResume) {
3805 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003806 ActivityManager.getService().activityResumed(token);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003807 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003808 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003809 }
3810 }
3811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 } else {
3813 // If an exception was thrown when trying to resume, then
3814 // just end this activity.
3815 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003816 ActivityManager.getService()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003817 .finishActivity(token, Activity.RESULT_CANCELED, null,
3818 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003820 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 }
3822 }
3823 }
3824
3825 private int mThumbnailWidth = -1;
3826 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003827 private Bitmap mAvailThumbnailBitmap = null;
3828 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829
Romain Guy65b345f2011-07-27 18:51:50 -07003830 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003831 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003833 if (thumbnail == null) {
3834 int w = mThumbnailWidth;
3835 int h;
3836 if (w < 0) {
3837 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003838 int wId = com.android.internal.R.dimen.thumbnail_width;
3839 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003840 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3841 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003842 } else {
3843 h = mThumbnailHeight;
3844 }
3845
3846 // On platforms where we don't want thumbnails, set dims to (0,0)
3847 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003848 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3849 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003850 thumbnail.eraseColor(0);
3851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003852 }
3853
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003854 if (thumbnail != null) {
3855 Canvas cv = mThumbnailCanvas;
3856 if (cv == null) {
3857 mThumbnailCanvas = cv = new Canvas();
3858 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003859
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003860 cv.setBitmap(thumbnail);
3861 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3862 mAvailThumbnailBitmap = thumbnail;
3863 thumbnail = null;
3864 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003865 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003866 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003867
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 } catch (Exception e) {
3869 if (!mInstrumentation.onException(r.activity, e)) {
3870 throw new RuntimeException(
3871 "Unable to create thumbnail of "
3872 + r.intent.getComponent().toShortString()
3873 + ": " + e.toString(), e);
3874 }
3875 thumbnail = null;
3876 }
3877
3878 return thumbnail;
3879 }
3880
Romain Guy65b345f2011-07-27 18:51:50 -07003881 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003882 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003883 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003884 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003885 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3886 return;
3887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003889 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 if (userLeaving) {
3891 performUserLeavingActivity(r);
3892 }
Bob Leee5408332009-09-04 18:31:17 -07003893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003895 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003897 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003898 if (r.isPreHoneycomb()) {
3899 QueuedWork.waitToFinish();
3900 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003903 if (!dontReport) {
3904 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003905 ActivityManager.getService().activityPaused(token);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003906 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003907 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003908 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003910 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003911 }
3912 }
3913
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003914 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 mInstrumentation.callActivityOnUserLeaving(r.activity);
3916 }
3917
3918 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003919 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003920 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003921 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003924 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003925 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 if (r.paused) {
3927 if (r.activity.mFinished) {
3928 // If we are finishing, we won't call onResume() in certain cases.
3929 // So here we likewise don't want to call onPause() if the activity
3930 // isn't resumed.
3931 return null;
3932 }
3933 RuntimeException e = new RuntimeException(
3934 "Performing pause of activity that is not resumed: "
3935 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003936 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003937 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003938 if (finished) {
3939 r.activity.mFinished = true;
3940 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003941
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003942 // Next have the activity save its current state and managed dialogs...
3943 if (!r.activity.mFinished && saveState) {
3944 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003946
3947 performPauseActivityIfNeeded(r, reason);
Jeff Hamilton52d32032011-01-08 15:31:26 -06003948
3949 // Notify any outstanding on paused listeners
3950 ArrayList<OnActivityPausedListener> listeners;
3951 synchronized (mOnPauseListeners) {
3952 listeners = mOnPauseListeners.remove(r.activity);
3953 }
3954 int size = (listeners != null ? listeners.size() : 0);
3955 for (int i = 0; i < size; i++) {
3956 listeners.get(i).onPaused(r.activity);
3957 }
3958
Craig Mautnera0026042014-04-23 11:45:37 -07003959 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003962 private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
3963 if (r.paused) {
3964 // You are already paused silly...
3965 return;
3966 }
3967
3968 try {
3969 r.activity.mCalled = false;
3970 mInstrumentation.callActivityOnPause(r.activity);
3971 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
3972 r.activity.getComponentName().getClassName(), reason);
3973 if (!r.activity.mCalled) {
3974 throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
3975 + " did not call through to super.onPause()");
3976 }
3977 } catch (SuperNotCalledException e) {
3978 throw e;
3979 } catch (Exception e) {
3980 if (!mInstrumentation.onException(r.activity, e)) {
3981 throw new RuntimeException("Unable to pause activity "
3982 + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
3983 }
3984 }
3985 r.paused = true;
3986 }
3987
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003988 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003989 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003990 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003991 }
3992
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003993 private static class StopInfo implements Runnable {
3994 ActivityClientRecord activity;
3995 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003996 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003998
3999 @Override public void run() {
4000 // Tell activity manager we have been stopped.
4001 try {
4002 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004003 ActivityManager.getService().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07004004 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004005 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07004006 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07004007 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07004008 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07004009 return;
4010 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004011 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004012 }
4013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004014 }
4015
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004016 private static final class ProviderRefCount {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004017 public final ContentProviderHolder holder;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004018 public final ProviderClientRecord client;
4019 public int stableCount;
4020 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004021
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004022 // When this is set, the stable and unstable ref counts are 0 and
4023 // we have a pending operation scheduled to remove the ref count
4024 // from the activity manager. On the activity manager we are still
4025 // holding an unstable ref, though it is not reflected in the counts
4026 // here.
4027 public boolean removePending;
4028
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07004029 ProviderRefCount(ContentProviderHolder inHolder,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004030 ProviderClientRecord inClient, int sCount, int uCount) {
4031 holder = inHolder;
4032 client = inClient;
4033 stableCount = sCount;
4034 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 }
4036 }
4037
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004038 /**
4039 * Core implementation of stopping an activity. Note this is a little
4040 * tricky because the server's meaning of stop is slightly different
4041 * than our client -- for the server, stop means to save state and give
4042 * it the result when it is done, but the window may still be visible.
4043 * For the client, we want to call onStop()/onStart() to indicate when
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004044 * the activity's UI visibility changes.
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004045 */
Romain Guy65b345f2011-07-27 18:51:50 -07004046 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004047 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004048 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 if (r != null) {
4050 if (!keepShown && r.stopped) {
4051 if (r.activity.mFinished) {
4052 // If we are finishing, we won't call onResume() in certain
4053 // cases. So here we likewise don't want to call onStop()
4054 // if the activity isn't resumed.
4055 return;
4056 }
4057 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07004058 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004059 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004060 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07004061 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 }
4063
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004064 // One must first be paused before stopped...
4065 performPauseActivityIfNeeded(r, reason);
4066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004067 if (info != null) {
4068 try {
4069 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004070 // For now, don't create the thumbnail here; we are
4071 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 info.description = r.activity.onCreateDescription();
4073 } catch (Exception e) {
4074 if (!mInstrumentation.onException(r.activity, e)) {
4075 throw new RuntimeException(
4076 "Unable to save state of activity "
4077 + r.intent.getComponent().toShortString()
4078 + ": " + e.toString(), e);
4079 }
4080 }
4081 }
4082
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004083 // Next have the activity save its current state and managed dialogs...
4084 if (!r.activity.mFinished && saveState) {
4085 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07004086 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004087 }
4088 }
4089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004090 if (!keepShown) {
4091 try {
4092 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004093 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 } catch (Exception e) {
4095 if (!mInstrumentation.onException(r.activity, e)) {
4096 throw new RuntimeException(
4097 "Unable to stop activity "
4098 + r.intent.getComponent().toShortString()
4099 + ": " + e.toString(), e);
4100 }
4101 }
4102 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004103 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4104 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004106 }
4107 }
4108
Romain Guy65b345f2011-07-27 18:51:50 -07004109 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 View v = r.activity.mDecor;
4111 if (v != null) {
4112 if (show) {
4113 if (!r.activity.mVisibleFromServer) {
4114 r.activity.mVisibleFromServer = true;
4115 mNumVisibleActivities++;
4116 if (r.activity.mVisibleFromClient) {
4117 r.activity.makeVisible();
4118 }
4119 }
4120 if (r.newConfig != null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004121 performConfigurationChangedForActivity(r, r.newConfig);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004122 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08004123 + r.activityInfo.name + " with new config "
4124 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 r.newConfig = null;
4126 }
4127 } else {
4128 if (r.activity.mVisibleFromServer) {
4129 r.activity.mVisibleFromServer = false;
4130 mNumVisibleActivities--;
4131 v.setVisibility(View.INVISIBLE);
4132 }
4133 }
4134 }
4135 }
4136
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004137 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004138 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004139 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
4140 return;
4141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 r.activity.mConfigChangeFlags |= configChanges;
4143
4144 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004145 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004147 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004148 TAG, "Finishing stop of " + r + ": show=" + show
4149 + " win=" + r.window);
4150
4151 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07004152
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004153 // Make sure any pending writes are now committed.
4154 if (!r.isPreHoneycomb()) {
4155 QueuedWork.waitToFinish();
4156 }
4157
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004158 // Schedule the call to tell the activity manager we have
4159 // stopped. We don't do this immediately, because we want to
4160 // have a chance for any other pending work (in particular memory
4161 // trim requests) to complete before you tell the activity
4162 // manager to proceed and allow us to go fully into the background.
4163 info.activity = r;
4164 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07004165 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004166 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004167 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 }
4169
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004170 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
4171 String action) {
4172 if (r == null) {
4173 return true;
4174 }
4175 if (seq < r.lastProcessedSeq) {
4176 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
4177 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
4178 return false;
4179 }
4180 r.lastProcessedSeq = seq;
4181 return true;
4182 }
4183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004184 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004185 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 if (r.stopped) {
4187 r.activity.performRestart();
4188 r.stopped = false;
4189 }
4190 }
4191
Romain Guy65b345f2011-07-27 18:51:50 -07004192 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004193 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07004194
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08004195 if (r == null) {
4196 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
4197 return;
4198 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004200 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004201 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 } else if (show && r.stopped) {
4203 // If we are getting ready to gc after going to the background, well
4204 // we are back active so skip it.
4205 unscheduleGcIdler();
4206
4207 r.activity.performRestart();
4208 r.stopped = false;
4209 }
4210 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07004211 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 TAG, "Handle window " + r + " visibility: " + show);
4213 updateVisibility(r, show);
4214 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004215 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 }
4217
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004218 // TODO: This method should be changed to use {@link #performStopActivityInner} to perform to
4219 // stop operation on the activity to reduce code duplication and the chance of fixing a bug in
4220 // one place and missing the other.
Romain Guy65b345f2011-07-27 18:51:50 -07004221 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004222 ActivityClientRecord r = mActivities.get(token);
4223
4224 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004225 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004226 return;
4227 }
4228
4229 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004230 if (!r.stopped && !r.isPreHoneycomb()) {
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004231 if (!r.activity.mFinished && r.state == null) {
4232 callCallActivityOnSaveInstanceState(r);
4233 }
4234
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004235 try {
4236 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004237 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004238 } catch (Exception e) {
4239 if (!mInstrumentation.onException(r.activity, e)) {
4240 throw new RuntimeException(
4241 "Unable to stop activity "
4242 + r.intent.getComponent().toShortString()
4243 + ": " + e.toString(), e);
4244 }
4245 }
4246 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004247 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4248 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004249 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004250
4251 // Make sure any pending writes are now committed.
4252 if (!r.isPreHoneycomb()) {
4253 QueuedWork.waitToFinish();
4254 }
4255
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004256 // Tell activity manager we slept.
4257 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004258 ActivityManager.getService().activitySlept(r.token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004259 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004260 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004261 }
4262 } else {
4263 if (r.stopped && r.activity.mVisibleFromServer) {
4264 r.activity.performRestart();
4265 r.stopped = false;
4266 }
4267 }
4268 }
4269
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004270 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07004271 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004272 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004273 }
Jon Miranda836c0a82014-08-11 12:32:26 -07004274 onCoreSettingsChange();
4275 }
4276
4277 private void onCoreSettingsChange() {
4278 boolean debugViewAttributes =
4279 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4280 if (debugViewAttributes != View.mDebugViewAttributes) {
4281 View.mDebugViewAttributes = debugViewAttributes;
4282
4283 // request all activities to relaunch for the changes to take place
4284 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004285 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
4286 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07004287 }
4288 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004289 }
4290
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004291 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4292 LoadedApk apk = peekPackageInfo(data.pkg, false);
4293 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004294 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004295 }
4296 apk = peekPackageInfo(data.pkg, true);
4297 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004298 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004299 }
4300 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004301 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004302 }
4303
Romain Guy65b345f2011-07-27 18:51:50 -07004304 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004305 final int N = results.size();
4306 for (int i=0; i<N; i++) {
4307 ResultInfo ri = results.get(i);
4308 try {
4309 if (ri.mData != null) {
4310 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004311 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004313 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004314 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004315 r.activity.dispatchActivityResult(ri.mResultWho,
4316 ri.mRequestCode, ri.mResultCode, ri.mData);
4317 } catch (Exception e) {
4318 if (!mInstrumentation.onException(r.activity, e)) {
4319 throw new RuntimeException(
4320 "Failure delivering result " + ri + " to activity "
4321 + r.intent.getComponent().toShortString()
4322 + ": " + e.toString(), e);
4323 }
4324 }
4325 }
4326 }
4327
Romain Guy65b345f2011-07-27 18:51:50 -07004328 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004329 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004330 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 if (r != null) {
4332 final boolean resumed = !r.paused;
4333 if (!r.activity.mFinished && r.activity.mDecor != null
4334 && r.hideForNow && resumed) {
4335 // We had hidden the activity because it started another
4336 // one... we have gotten a result back and we are not
4337 // paused, so make sure our window is visible.
4338 updateVisibility(r, true);
4339 }
4340 if (resumed) {
4341 try {
4342 // Now we are idle.
4343 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004344 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004345 mInstrumentation.callActivityOnPause(r.activity);
4346 if (!r.activity.mCalled) {
4347 throw new SuperNotCalledException(
4348 "Activity " + r.intent.getComponent().toShortString()
4349 + " did not call through to super.onPause()");
4350 }
4351 } catch (SuperNotCalledException e) {
4352 throw e;
4353 } catch (Exception e) {
4354 if (!mInstrumentation.onException(r.activity, e)) {
4355 throw new RuntimeException(
4356 "Unable to pause activity "
4357 + r.intent.getComponent().toShortString()
4358 + ": " + e.toString(), e);
4359 }
4360 }
4361 }
4362 deliverResults(r, res.results);
4363 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004364 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004365 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004366 }
4367 }
4368 }
4369
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004370 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 return performDestroyActivity(token, finishing, 0, false);
4372 }
4373
Romain Guy65b345f2011-07-27 18:51:50 -07004374 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004376 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004377 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004378 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004380 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004381 r.activity.mConfigChangeFlags |= configChanges;
4382 if (finishing) {
4383 r.activity.mFinished = true;
4384 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004385
4386 performPauseActivityIfNeeded(r, "destroy");
4387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 if (!r.stopped) {
4389 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004390 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 } catch (SuperNotCalledException e) {
4392 throw e;
4393 } catch (Exception e) {
4394 if (!mInstrumentation.onException(r.activity, e)) {
4395 throw new RuntimeException(
4396 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004397 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004398 + ": " + e.toString(), e);
4399 }
4400 }
4401 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004402 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4403 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 }
4405 if (getNonConfigInstance) {
4406 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004407 r.lastNonConfigurationInstances
4408 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004409 } catch (Exception e) {
4410 if (!mInstrumentation.onException(r.activity, e)) {
4411 throw new RuntimeException(
4412 "Unable to retain activity "
4413 + r.intent.getComponent().toShortString()
4414 + ": " + e.toString(), e);
4415 }
4416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004417 }
4418 try {
4419 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004420 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 if (!r.activity.mCalled) {
4422 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004423 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 " did not call through to super.onDestroy()");
4425 }
4426 if (r.window != null) {
4427 r.window.closeAllPanels();
4428 }
4429 } catch (SuperNotCalledException e) {
4430 throw e;
4431 } catch (Exception e) {
4432 if (!mInstrumentation.onException(r.activity, e)) {
4433 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004434 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4435 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 }
4437 }
4438 }
4439 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004440 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 return r;
4442 }
4443
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004444 private static String safeToComponentShortString(Intent intent) {
4445 ComponentName component = intent.getComponent();
4446 return component == null ? "[Unknown]" : component.toShortString();
4447 }
4448
Romain Guy65b345f2011-07-27 18:51:50 -07004449 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004451 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 configChanges, getNonConfigInstance);
4453 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004454 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 WindowManager wm = r.activity.getWindowManager();
4456 View v = r.activity.mDecor;
4457 if (v != null) {
4458 if (r.activity.mVisibleFromServer) {
4459 mNumVisibleActivities--;
4460 }
4461 IBinder wtoken = v.getWindowToken();
4462 if (r.activity.mWindowAdded) {
Robert Carr77bdfb52016-05-02 18:18:31 -07004463 if (r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004464 // Hold off on removing this until the new activity's
4465 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004466 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004467 r.mPendingRemoveWindowManager = wm;
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004468 // We can only keep the part of the view hierarchy that we control,
4469 // everything else must be removed, because it might not be able to
4470 // behave properly when activity is relaunching.
4471 r.window.clearContentView();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004472 } else {
4473 wm.removeViewImmediate(v);
4474 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004476 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004477 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 r.activity.getClass().getName(), "Activity");
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004479 } else if (r.mPendingRemoveWindow != null) {
4480 // We're preserving only one window, others should be closed so app views
4481 // will be detached before the final tear down. It should be done now because
4482 // some components (e.g. WebView) rely on detach callbacks to perform receiver
4483 // unregister and other cleanup.
4484 WindowManagerGlobal.getInstance().closeAllExceptView(token, v,
4485 r.activity.getClass().getName(), "Activity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004486 }
4487 r.activity.mDecor = null;
4488 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004489 if (r.mPendingRemoveWindow == null) {
4490 // If we are delaying the removal of the activity window, then
4491 // we can't clean up all windows here. Note that we can't do
4492 // so later either, which means any windows that aren't closed
4493 // by the app will leak. Well we try to warning them a lot
4494 // about leaking windows, because that is a bug, so if they are
4495 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004496 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004497 r.activity.getClass().getName(), "Activity");
4498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004499
4500 // Mocked out contexts won't be participating in the normal
4501 // process lifecycle, but if we're running with a proper
4502 // ApplicationContext we need to have it tear down things
4503 // cleanly.
4504 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004505 if (c instanceof ContextImpl) {
4506 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 r.activity.getClass().getName(), "Activity");
4508 }
4509 }
4510 if (finishing) {
4511 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004512 ActivityManager.getService().activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004514 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 }
4516 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004517 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 }
4519
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004520 /**
4521 * @param preserveWindow Whether the activity should try to reuse the window it created,
4522 * including the decor view after the relaunch.
4523 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004524 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004525 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004526 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004527 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004528 ActivityClientRecord target = null;
4529
Craig Mautner88c05892013-06-28 09:47:45 -07004530 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004531 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4532 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004533 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004534 if (r.token == token) {
4535 target = r;
4536 if (pendingResults != null) {
4537 if (r.pendingResults != null) {
4538 r.pendingResults.addAll(pendingResults);
4539 } else {
4540 r.pendingResults = pendingResults;
4541 }
4542 }
4543 if (pendingNewIntents != null) {
4544 if (r.pendingIntents != null) {
4545 r.pendingIntents.addAll(pendingNewIntents);
4546 } else {
4547 r.pendingIntents = pendingNewIntents;
4548 }
4549 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004550
4551 // For each relaunch request, activity manager expects an answer
4552 if (!r.onlyLocalRequest && fromServer) {
4553 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004554 ActivityManager.getService().activityRelaunched(token);
Jorim Jaggife89d122015-12-22 16:28:44 +01004555 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004556 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004557 }
4558 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004559 break;
4560 }
4561 }
4562
4563 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004564 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4565 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004566 target = new ActivityClientRecord();
4567 target.token = token;
4568 target.pendingResults = pendingResults;
4569 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004570 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004571 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004572 final ActivityClientRecord existing = mActivities.get(token);
4573 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004574 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004575 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4576 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004577 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004578 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004579 }
4580 target.onlyLocalRequest = true;
4581 }
4582 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004583 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004584 }
4585
4586 if (fromServer) {
4587 target.startsNotResumed = notResumed;
4588 target.onlyLocalRequest = false;
4589 }
4590 if (config != null) {
4591 target.createdConfig = config;
4592 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004593 if (overrideConfig != null) {
4594 target.overrideConfig = overrideConfig;
4595 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004596 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004597 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004598 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004599 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4600 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004601 }
4602
Romain Guy65b345f2011-07-27 18:51:50 -07004603 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604 // If we are getting ready to gc after going to the background, well
4605 // we are back active so skip it.
4606 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004607 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004608
4609 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004610 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 // First: make sure we have the most recent configuration and most
4613 // recent version of the activity, or skip it if some previous call
4614 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004615 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 int N = mRelaunchingActivities.size();
4617 IBinder token = tmp.token;
4618 tmp = null;
4619 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004620 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004621 if (r.token == token) {
4622 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004623 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 mRelaunchingActivities.remove(i);
4625 i--;
4626 N--;
4627 }
4628 }
Bob Leee5408332009-09-04 18:31:17 -07004629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004631 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 return;
4633 }
Bob Leee5408332009-09-04 18:31:17 -07004634
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004635 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4636 + tmp.token + " with configChanges=0x"
4637 + Integer.toHexString(configChanges));
4638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004639 if (mPendingConfiguration != null) {
4640 changedConfig = mPendingConfiguration;
4641 mPendingConfiguration = null;
4642 }
4643 }
Bob Leee5408332009-09-04 18:31:17 -07004644
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004645 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4646 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4647 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4648 } else {
4649 tmp.lastProcessedSeq = tmp.relaunchSeq;
4650 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004651 if (tmp.createdConfig != null) {
4652 // If the activity manager is passing us its current config,
4653 // assume that is really what we want regardless of what we
4654 // may have pending.
4655 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004656 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4657 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4658 if (changedConfig == null
4659 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4660 changedConfig = tmp.createdConfig;
4661 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004662 }
4663 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004664
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004665 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004666 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004667
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 // If there was a pending configuration change, execute it first.
4669 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004670 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004671 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004672 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 }
Bob Leee5408332009-09-04 18:31:17 -07004674
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004675 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004676 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004678 if (!tmp.onlyLocalRequest) {
4679 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004680 ActivityManager.getService().activityRelaunched(tmp.token);
Jorim Jaggife89d122015-12-22 16:28:44 +01004681 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004682 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004683 }
4684 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 return;
4686 }
Bob Leee5408332009-09-04 18:31:17 -07004687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004688 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004689 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004690 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004691 r.lastProcessedSeq = tmp.lastProcessedSeq;
4692 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004693 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004694
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004695 r.activity.mChangingConfigurations = true;
4696
Robert Carr23fa16b2016-01-13 13:19:58 -08004697 // If we are preserving the main window across relaunches we would also like to preserve
4698 // the children. However the client side view system does not support preserving
4699 // the child views so we notify the window manager to expect these windows to
4700 // be replaced and defer requests to destroy or hide them. This way we can achieve
4701 // visual continuity. It's important that we do this here prior to pause and destroy
4702 // as that is when we may hide or remove the child views.
Robert Carr77bdfb52016-05-02 18:18:31 -07004703 //
4704 // There is another scenario, if we have decided locally to relaunch the app from a
4705 // call to recreate, then none of the windows will be prepared for replacement or
4706 // preserved by the server, so we want to notify it that we are preparing to replace
4707 // everything
Robert Carr23fa16b2016-01-13 13:19:58 -08004708 try {
Robert Carr77bdfb52016-05-02 18:18:31 -07004709 if (r.mPreserveWindow || r.onlyLocalRequest) {
4710 WindowManagerGlobal.getWindowSession().prepareToReplaceWindows(
4711 r.token, !r.onlyLocalRequest);
Robert Carr23fa16b2016-01-13 13:19:58 -08004712 }
4713 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004714 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004715 }
4716
Dianne Hackborne2b04802010-12-09 09:24:55 -08004717 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004719 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004720 }
4721 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004722 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 }
Bob Leee5408332009-09-04 18:31:17 -07004724
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727 r.activity = null;
4728 r.window = null;
4729 r.hideForNow = false;
4730 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004731 // Merge any pending results and pending intents; don't just replace them
4732 if (tmp.pendingResults != null) {
4733 if (r.pendingResults == null) {
4734 r.pendingResults = tmp.pendingResults;
4735 } else {
4736 r.pendingResults.addAll(tmp.pendingResults);
4737 }
4738 }
4739 if (tmp.pendingIntents != null) {
4740 if (r.pendingIntents == null) {
4741 r.pendingIntents = tmp.pendingIntents;
4742 } else {
4743 r.pendingIntents.addAll(tmp.pendingIntents);
4744 }
4745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004747 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004748
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004749 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004750
4751 if (!tmp.onlyLocalRequest) {
4752 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004753 ActivityManager.getService().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004754 if (r.window != null) {
4755 r.window.reportActivityRelaunched();
4756 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004757 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004758 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004759 }
4760 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 }
4762
Craig Mautnera0026042014-04-23 11:45:37 -07004763 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4764 r.state = new Bundle();
4765 r.state.setAllowFds(false);
4766 if (r.isPersistable()) {
4767 r.persistentState = new PersistableBundle();
4768 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4769 r.persistentState);
4770 } else {
4771 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4772 }
4773 }
4774
Dianne Hackborn73c14162012-09-19 15:45:06 -07004775 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004776 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004777 ArrayList<ComponentCallbacks2> callbacks
4778 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004779
Craig Mautner88c05892013-06-28 09:47:45 -07004780 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004781 final int NAPP = mAllApplications.size();
4782 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004783 callbacks.add(mAllApplications.get(i));
4784 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004785 final int NACT = mActivities.size();
4786 for (int i=0; i<NACT; i++) {
4787 ActivityClientRecord ar = mActivities.valueAt(i);
4788 Activity a = ar.activity;
4789 if (a != null) {
4790 Configuration thisConfig = applyConfigCompatMainThread(
4791 mCurDefaultDisplayDpi, newConfig,
4792 ar.packageInfo.getCompatibilityInfo());
4793 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4794 // If the activity is currently resumed, its configuration
4795 // needs to change right now.
4796 callbacks.add(a);
4797 } else if (thisConfig != null) {
4798 // Otherwise, we will tell it about the change
4799 // the next time it is resumed or shown. Note that
4800 // the activity manager may, before then, decide the
4801 // activity needs to be destroyed to handle its new
4802 // configuration.
4803 if (DEBUG_CONFIGURATION) {
4804 Slog.v(TAG, "Setting activity "
4805 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004806 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004807 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004808 }
4809 }
4810 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004811 final int NSVC = mServices.size();
4812 for (int i=0; i<NSVC; i++) {
4813 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004814 }
4815 }
4816 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004817 final int NPRV = mLocalProviders.size();
4818 for (int i=0; i<NPRV; i++) {
4819 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 }
4821 }
Bob Leee5408332009-09-04 18:31:17 -07004822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 return callbacks;
4824 }
Bob Leee5408332009-09-04 18:31:17 -07004825
Adam Lesinski082614c2016-03-04 14:33:47 -08004826 /**
4827 * Updates the configuration for an Activity. The ActivityClientRecord's
4828 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4829 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4830 * the updated Configuration.
4831 * @param r ActivityClientRecord representing the Activity.
4832 * @param newBaseConfig The new configuration to use. This may be augmented with
4833 * {@link ActivityClientRecord#overrideConfig}.
Adam Lesinski082614c2016-03-04 14:33:47 -08004834 */
4835 private void performConfigurationChangedForActivity(ActivityClientRecord r,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004836 Configuration newBaseConfig) {
4837 performConfigurationChangedForActivity(r, newBaseConfig,
4838 r.activity.getDisplay().getDisplayId(), false /* movedToDifferentDisplay */);
4839 }
4840
4841 /**
4842 * Updates the configuration for an Activity. The ActivityClientRecord's
4843 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4844 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4845 * the updated Configuration.
4846 * @param r ActivityClientRecord representing the Activity.
4847 * @param newBaseConfig The new configuration to use. This may be augmented with
4848 * {@link ActivityClientRecord#overrideConfig}.
4849 * @param displayId The id of the display where the Activity currently resides.
4850 * @param movedToDifferentDisplay Indicates if the activity was moved to different display.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004851 * @return {@link Configuration} instance sent to client, null if not sent.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004852 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004853 private Configuration performConfigurationChangedForActivity(ActivityClientRecord r,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004854 Configuration newBaseConfig, int displayId, boolean movedToDifferentDisplay) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004855 r.tmpConfig.setTo(newBaseConfig);
4856 if (r.overrideConfig != null) {
4857 r.tmpConfig.updateFrom(r.overrideConfig);
4858 }
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004859 final Configuration reportedConfig = performActivityConfigurationChanged(r.activity,
4860 r.tmpConfig, r.overrideConfig, displayId, movedToDifferentDisplay);
Adam Lesinski082614c2016-03-04 14:33:47 -08004861 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004862 return reportedConfig;
Adam Lesinski082614c2016-03-04 14:33:47 -08004863 }
4864
4865 /**
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004866 * Creates a new Configuration only if override would modify base. Otherwise returns base.
4867 * @param base The base configuration.
4868 * @param override The update to apply to the base configuration. Can be null.
4869 * @return A Configuration representing base with override applied.
4870 */
4871 private static Configuration createNewConfigAndUpdateIfNotNull(@NonNull Configuration base,
4872 @Nullable Configuration override) {
4873 if (override == null) {
4874 return base;
4875 }
4876 Configuration newConfig = new Configuration(base);
4877 newConfig.updateFrom(override);
4878 return newConfig;
4879 }
4880
4881 /**
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004882 * Decides whether to update a component's configuration and whether to inform it.
Adam Lesinski082614c2016-03-04 14:33:47 -08004883 * @param cb The component callback to notify of configuration change.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004884 * @param newConfig The new configuration.
4885 */
4886 private void performConfigurationChanged(ComponentCallbacks2 cb, Configuration newConfig) {
4887 if (!REPORT_TO_ACTIVITY) {
4888 return;
4889 }
4890
4891 // ContextThemeWrappers may override the configuration for that context. We must check and
4892 // apply any overrides defined.
4893 Configuration contextThemeWrapperOverrideConfig = null;
4894 if (cb instanceof ContextThemeWrapper) {
4895 final ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
4896 contextThemeWrapperOverrideConfig = contextThemeWrapper.getOverrideConfiguration();
4897 }
4898
4899 // Apply the ContextThemeWrapper override if necessary.
4900 // NOTE: Make sure the configurations are not modified, as they are treated as immutable
4901 // in many places.
4902 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(
4903 newConfig, contextThemeWrapperOverrideConfig);
4904 cb.onConfigurationChanged(configToReport);
4905 }
4906
4907 /**
4908 * Decides whether to update an Activity's configuration and whether to inform it.
4909 * @param activity The activity to notify of configuration change.
Adam Lesinski082614c2016-03-04 14:33:47 -08004910 * @param newConfig The new configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004911 * @param amOverrideConfig The override config that differentiates the Activity's configuration
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004912 * from the base global configuration. This is supplied by
4913 * ActivityManager.
4914 * @param displayId Id of the display where activity currently resides.
4915 * @param movedToDifferentDisplay Indicates if the activity was moved to different display.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004916 * @return Configuration sent to client, null if no changes and not moved to different display.
Adam Lesinski082614c2016-03-04 14:33:47 -08004917 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004918 private Configuration performActivityConfigurationChanged(Activity activity,
4919 Configuration newConfig, Configuration amOverrideConfig, int displayId,
4920 boolean movedToDifferentDisplay) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004921 if (activity == null) {
4922 throw new IllegalArgumentException("No activity provided.");
4923 }
4924 final IBinder activityToken = activity.getActivityToken();
4925 if (activityToken == null) {
4926 throw new IllegalArgumentException("Activity token not set. Is the activity attached?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004927 }
Bob Leee5408332009-09-04 18:31:17 -07004928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 boolean shouldChangeConfig = false;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004930 if (activity.mCurrentConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004931 shouldChangeConfig = true;
4932 } else {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004933 // If the new config is the same as the config this Activity is already running with and
4934 // the override config also didn't change, then don't bother calling
4935 // onConfigurationChanged.
Adam Lesinski082614c2016-03-04 14:33:47 -08004936 int diff = activity.mCurrentConfig.diff(newConfig);
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004937 if (diff != 0 || !mResourcesManager.isSameResourcesOverrideConfig(activityToken,
4938 amOverrideConfig)) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004939 // Always send the task-level config changes. For system-level configuration, if
4940 // this activity doesn't handle any of the config changes, then don't bother
4941 // calling onConfigurationChanged as we're going to destroy it.
4942 if (!mUpdatingSystemConfig
4943 || (~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004944 || !REPORT_TO_ACTIVITY) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004945 shouldChangeConfig = true;
4946 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004947 }
4948 }
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004949 if (!shouldChangeConfig && !movedToDifferentDisplay) {
4950 // Nothing significant, don't proceed with updating and reporting.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004951 return null;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004952 }
4953
4954 // Propagate the configuration change to ResourcesManager and Activity.
4955
4956 // ContextThemeWrappers may override the configuration for that context. We must check and
4957 // apply any overrides defined.
4958 Configuration contextThemeWrapperOverrideConfig = activity.getOverrideConfiguration();
4959
4960 // We only update an Activity's configuration if this is not a global configuration change.
4961 // This must also be done before the callback, or else we violate the contract that the new
4962 // resources are available in ComponentCallbacks2#onConfigurationChanged(Configuration).
4963 // Also apply the ContextThemeWrapper override if necessary.
4964 // NOTE: Make sure the configurations are not modified, as they are treated as immutable in
4965 // many places.
4966 final Configuration finalOverrideConfig = createNewConfigAndUpdateIfNotNull(
4967 amOverrideConfig, contextThemeWrapperOverrideConfig);
4968 mResourcesManager.updateResourcesForActivity(activityToken, finalOverrideConfig,
4969 displayId, movedToDifferentDisplay);
4970
4971 activity.mConfigChangeFlags = 0;
4972 activity.mCurrentConfig = new Configuration(newConfig);
4973
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004974 // Apply the ContextThemeWrapper override if necessary.
4975 // NOTE: Make sure the configurations are not modified, as they are treated as immutable
4976 // in many places.
4977 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(newConfig,
4978 contextThemeWrapperOverrideConfig);
4979
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004980 if (!REPORT_TO_ACTIVITY) {
4981 // Not configured to report to activity.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004982 return configToReport;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004983 }
4984
4985 if (movedToDifferentDisplay) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004986 activity.dispatchMovedToDisplay(displayId, configToReport);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004987 }
Bob Leee5408332009-09-04 18:31:17 -07004988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004989 if (shouldChangeConfig) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004990 activity.mCalled = false;
4991 activity.onConfigurationChanged(configToReport);
4992 if (!activity.mCalled) {
4993 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
4994 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
4996 }
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004997
4998 return configToReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004999 }
5000
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07005001 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07005002 synchronized (mResourcesManager) {
5003 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07005004 }
5005 }
5006
Dianne Hackborn908aecc2012-07-31 16:37:34 -07005007 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07005008 Configuration config = mConfiguration;
5009 if (mCompatConfiguration == null) {
5010 mCompatConfiguration = new Configuration();
5011 }
5012 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08005013 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
5014 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07005015 config = mCompatConfiguration;
5016 }
5017 return config;
5018 }
5019
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005020 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005021
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005022 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005023
Craig Mautner88c05892013-06-28 09:47:45 -07005024 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005025 if (mPendingConfiguration != null) {
5026 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
5027 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07005028 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07005029 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005030 }
5031 mPendingConfiguration = null;
5032 }
5033
5034 if (config == null) {
5035 return;
5036 }
Tim Murraye1e6c662015-04-07 13:24:14 -07005037
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005038 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005039 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07005040
5041 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005042 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
5043 mResourcesManager.getConfiguration().getLocales());
Craig Mautner88c05892013-06-28 09:47:45 -07005044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 if (mConfiguration == null) {
5046 mConfiguration = new Configuration();
5047 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005048 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005049 return;
5050 }
Alan Viverettee54d2452015-05-06 10:41:43 -07005051
5052 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07005053 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04005054
5055 final Theme systemTheme = getSystemContext().getTheme();
5056 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
5057 systemTheme.rebase();
5058 }
Adam Lesinskia82b6262017-03-21 16:56:17 -07005059
5060 final Theme systemUiTheme = getSystemUiContext().getTheme();
5061 if ((systemUiTheme.getChangingConfigurations() & configDiff) != 0) {
5062 systemUiTheme.rebase();
5063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07005065
5066 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
5067
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005068 freeTextLayoutCachesIfNeeded(configDiff);
5069
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005070 if (callbacks != null) {
5071 final int N = callbacks.size();
5072 for (int i=0; i<N; i++) {
Andrii Kulian701214b2016-04-07 09:36:33 -07005073 ComponentCallbacks2 cb = callbacks.get(i);
5074 if (cb instanceof Activity) {
5075 // If callback is an Activity - call corresponding method to consider override
5076 // config and avoid onConfigurationChanged if it hasn't changed.
5077 Activity a = (Activity) cb;
5078 performConfigurationChangedForActivity(mActivities.get(a.getActivityToken()),
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005079 config);
Andrii Kulian701214b2016-04-07 09:36:33 -07005080 } else {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005081 performConfigurationChanged(cb, config);
Andrii Kulian701214b2016-04-07 09:36:33 -07005082 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005084 }
5085 }
5086
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005087 void handleApplicationInfoChanged(@NonNull final ApplicationInfo ai) {
Adam Lesinskid33ef562017-01-19 14:49:59 -08005088 // Updates triggered by package installation go through a package update
5089 // receiver. Here we try to capture ApplicationInfo changes that are
5090 // caused by other sources, such as overlays. That means we want to be as conservative
5091 // about code changes as possible. Take the diff of the old ApplicationInfo and the new
5092 // to see if anything needs to change.
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005093 synchronized (mResourcesManager) {
5094 // Update all affected loaded packages with new package information
5095 WeakReference<LoadedApk> ref = mPackages.get(ai.packageName);
5096 LoadedApk apk = ref != null ? ref.get() : null;
5097 if (apk != null) {
Adam Lesinskid33ef562017-01-19 14:49:59 -08005098 final ArrayList<String> oldPaths = new ArrayList<>();
Dimitry Ivanov638d8102017-02-22 15:39:42 -08005099 LoadedApk.makePaths(this, apk.getApplicationInfo(), oldPaths);
Adam Lesinskid33ef562017-01-19 14:49:59 -08005100 apk.updateApplicationInfo(ai, oldPaths);
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005101 }
5102
5103 ref = mResourcePackages.get(ai.packageName);
5104 apk = ref != null ? ref.get() : null;
5105 if (apk != null) {
Adam Lesinskid33ef562017-01-19 14:49:59 -08005106 final ArrayList<String> oldPaths = new ArrayList<>();
Dimitry Ivanov638d8102017-02-22 15:39:42 -08005107 LoadedApk.makePaths(this, apk.getApplicationInfo(), oldPaths);
Adam Lesinskid33ef562017-01-19 14:49:59 -08005108 apk.updateApplicationInfo(ai, oldPaths);
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005109 }
5110
5111 // Update all affected Resources objects to use new ResourcesImpl
5112 mResourcesManager.applyNewResourceDirsLocked(ai.sourceDir, ai.resourceDirs);
5113 }
5114
5115 ApplicationPackageManager.configurationChanged();
5116
5117 // Trigger a regular Configuration change event, only with a different assetsSeq number
5118 // so that we actually call through to all components.
Adam Lesinskia82b6262017-03-21 16:56:17 -07005119 // TODO(adamlesinski): Change this to make use of ActivityManager's upcoming ability to
5120 // store configurations per-process.
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005121 Configuration newConfig = new Configuration();
Adam Lesinskia82b6262017-03-21 16:56:17 -07005122 newConfig.assetsSeq = (mConfiguration != null ? mConfiguration.assetsSeq : 0) + 1;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005123 handleConfigurationChanged(newConfig, null);
5124
5125 // Schedule all activities to reload
5126 for (final Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
5127 final Activity activity = entry.getValue().activity;
5128 if (!activity.mFinished) {
5129 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
5130 false);
5131 }
5132 }
5133 }
5134
Romain Guy46bfc482013-08-16 18:38:29 -07005135 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005136 if (configDiff != 0) {
5137 // Ask text layout engine to free its caches if there is a locale change
5138 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
5139 if (hasLocaleConfigChange) {
5140 Canvas.freeTextLayoutCaches();
5141 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
5142 }
5143 }
5144 }
5145
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005146 /**
5147 * Handle new activity configuration and/or move to a different display.
5148 * @param data Configuration update data.
5149 * @param displayId Id of the display where activity was moved to, -1 if there was no move and
5150 * value didn't change.
5151 */
Andrii Kulian44607962017-03-16 11:06:24 -07005152 void handleActivityConfigurationChanged(ActivityConfigChangeData data, int displayId) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08005153 ActivityClientRecord r = mActivities.get(data.activityToken);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005154 // Check input params.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005155 if (r == null || r.activity == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005156 if (DEBUG_CONFIGURATION) Slog.w(TAG, "Not found target activity to report to: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 return;
5158 }
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005159 final boolean movedToDifferentDisplay = displayId != INVALID_DISPLAY;
5160 if (movedToDifferentDisplay) {
5161 if (r.activity.getDisplay().getDisplayId() == displayId) {
5162 throw new IllegalArgumentException("Activity is already on the target display: "
5163 + displayId);
5164 }
5165 }
Bob Leee5408332009-09-04 18:31:17 -07005166
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005167 // Perform updates.
Adam Lesinski082614c2016-03-04 14:33:47 -08005168 r.overrideConfig = data.overrideConfig;
Stephen Kiazykf903b732017-03-20 16:23:45 -07005169 final ViewRootImpl viewRoot = r.activity.mDecor != null
5170 ? r.activity.mDecor.getViewRootImpl() : null;
5171
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005172 if (movedToDifferentDisplay) {
5173 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity moved to display, activity:"
5174 + r.activityInfo.name + ", displayId=" + displayId
5175 + ", config=" + data.overrideConfig);
5176
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005177 final Configuration reportedConfig = performConfigurationChangedForActivity(r,
5178 mCompatConfiguration, displayId, true /* movedToDifferentDisplay */);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005179 if (viewRoot != null) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005180 viewRoot.onMovedToDisplay(displayId, reportedConfig);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005181 }
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005182 } else {
5183 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
5184 + r.activityInfo.name + ", config=" + data.overrideConfig);
5185 performConfigurationChangedForActivity(r, mCompatConfiguration);
5186 }
Andrii Kulian44607962017-03-16 11:06:24 -07005187 // Notify the ViewRootImpl instance about configuration changes. It may have initiated this
5188 // update to make sure that resources are updated before updating itself.
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005189 if (viewRoot != null) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005190 viewRoot.updateConfiguration(displayId);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005191 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005192 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 }
5194
Jeff Hao1b012d32014-08-20 10:35:34 -07005195 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005196 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005197 try {
Romain Guy7eabe552011-07-21 14:56:34 -07005198 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005199 default:
5200 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005201 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07005202 break;
5203 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005204 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005205 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005206 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07005207 } finally {
5208 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07005209 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07005210 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005211 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07005212 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005213 }
5214 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07005215 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07005216 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005217 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07005218 break;
Romain Guy7eabe552011-07-21 14:56:34 -07005219 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005220 }
5221 }
Bob Leee5408332009-09-04 18:31:17 -07005222
Andreas Gampe4c79fea2016-01-28 20:11:41 -08005223 /**
5224 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
5225 * so that profiler data won't be lost.
5226 *
5227 * @hide
5228 */
5229 public void stopProfiling() {
Amith Yamasanib1684432017-01-26 14:57:20 -08005230 if (mProfiler != null) {
5231 mProfiler.stopProfiling();
5232 }
Andreas Gampe4c79fea2016-01-28 20:11:41 -08005233 }
5234
Romain Guya998dff2012-03-23 18:58:36 -07005235 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07005236 if (managed) {
5237 try {
5238 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
5239 } catch (IOException e) {
5240 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
5241 + " -- can the process access this path?");
5242 } finally {
5243 try {
5244 dhd.fd.close();
5245 } catch (IOException e) {
5246 Slog.w(TAG, "Failure closing profile fd", e);
5247 }
5248 }
5249 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07005250 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07005251 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005252 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005253 ActivityManager.getService().dumpHeapFinished(dhd.path);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005254 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005255 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005256 }
Andy McFadden824c5102010-07-09 16:26:57 -07005257 }
5258
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005259 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
5260 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08005261 switch (cmd) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005262 case ApplicationThreadConstants.PACKAGE_REMOVED:
5263 case ApplicationThreadConstants.PACKAGE_REMOVED_DONT_KILL:
Todd Kennedy39bfee52016-02-24 10:28:21 -08005264 {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005265 final boolean killApp = cmd == ApplicationThreadConstants.PACKAGE_REMOVED;
Todd Kennedy39bfee52016-02-24 10:28:21 -08005266 if (packages == null) {
5267 break;
5268 }
5269 synchronized (mResourcesManager) {
5270 for (int i = packages.length - 1; i >= 0; i--) {
5271 if (!hasPkgInfo) {
5272 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
5273 if (ref != null && ref.get() != null) {
5274 hasPkgInfo = true;
5275 } else {
5276 ref = mResourcePackages.get(packages[i]);
5277 if (ref != null && ref.get() != null) {
5278 hasPkgInfo = true;
5279 }
5280 }
5281 }
5282 if (killApp) {
5283 mPackages.remove(packages[i]);
5284 mResourcePackages.remove(packages[i]);
5285 }
5286 }
5287 }
5288 break;
5289 }
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005290 case ApplicationThreadConstants.PACKAGE_REPLACED:
Todd Kennedy39bfee52016-02-24 10:28:21 -08005291 {
5292 if (packages == null) {
5293 break;
5294 }
5295 synchronized (mResourcesManager) {
5296 for (int i = packages.length - 1; i >= 0; i--) {
5297 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
5298 LoadedApk pkgInfo = ref != null ? ref.get() : null;
5299 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005300 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08005301 } else {
5302 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08005303 pkgInfo = ref != null ? ref.get() : null;
5304 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08005305 hasPkgInfo = true;
5306 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005307 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005308 // If the package is being replaced, yet it still has a valid
5309 // LoadedApk object, the package was updated with _DONT_KILL.
5310 // Adjust it's internal references to the application info and
5311 // resources.
5312 if (pkgInfo != null) {
5313 try {
5314 final String packageName = packages[i];
5315 final ApplicationInfo aInfo =
5316 sPackageManager.getApplicationInfo(
5317 packageName,
5318 0 /*flags*/,
5319 UserHandle.myUserId());
5320
5321 if (mActivities.size() > 0) {
5322 for (ActivityClientRecord ar : mActivities.values()) {
5323 if (ar.activityInfo.applicationInfo.packageName
5324 .equals(packageName)) {
5325 ar.activityInfo.applicationInfo = aInfo;
5326 ar.packageInfo = pkgInfo;
5327 }
5328 }
5329 }
5330 final List<String> oldPaths =
5331 sPackageManager.getPreviousCodePaths(packageName);
5332 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
5333 } catch (RemoteException e) {
5334 }
5335 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005336 }
5337 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005338 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005339 }
5340 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005341 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005342 }
Tim Murraye1e6c662015-04-07 13:24:14 -07005343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005344 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07005345 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07005346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 final int N = callbacks.size();
5348 for (int i=0; i<N; i++) {
5349 callbacks.get(i).onLowMemory();
5350 }
5351
Chris Tatece229052009-03-25 16:44:52 -07005352 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
5353 if (Process.myUid() != Process.SYSTEM_UID) {
5354 int sqliteReleased = SQLiteDatabase.releaseMemory();
5355 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
5356 }
Bob Leee5408332009-09-04 18:31:17 -07005357
Mike Reedcaf0df12009-04-27 14:32:05 -04005358 // Ask graphics to free up as much as possible (font/image caches)
5359 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005361 // Ask text layout engine to free also as much as possible
5362 Canvas.freeTextLayoutCaches();
5363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005364 BinderInternal.forceGc("mem");
5365 }
5366
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005367 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07005368 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005369
Dianne Hackborn73c14162012-09-19 15:45:06 -07005370 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005371
5372 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07005373 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005374 callbacks.get(i).onTrimMemory(level);
5375 }
Romain Guy19f86e82012-04-23 15:19:07 -07005376
John Reckf47a5942014-06-30 16:20:04 -07005377 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005378 }
5379
Jesse Hallb12249b2016-12-12 12:53:02 -08005380 private void setupGraphicsSupport(Context context, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005381 if (Process.isIsolated()) {
5382 // Isolated processes aren't going to do UI.
5383 return;
5384 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005385 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupGraphicsSupport");
Romain Guya9582652011-11-10 14:20:10 -08005386 try {
5387 int uid = Process.myUid();
5388 String[] packages = getPackageManager().getPackagesForUid(uid);
5389
Shuo Gao7c69a662016-03-01 13:32:03 +08005390 if (packages != null) {
John Reck51aaf902015-12-02 15:08:07 -08005391 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07005392 RenderScriptCacheDir.setupDiskCache(cacheDir);
Jesse Hallb12249b2016-12-12 12:53:02 -08005393 GraphicsEnvironment.setupGraphicsEnvironment(context);
Romain Guya9582652011-11-10 14:20:10 -08005394 }
5395 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005396 throw e.rethrowFromSystemServer();
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005397 } finally {
5398 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Romain Guya9582652011-11-10 14:20:10 -08005399 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07005400 }
5401
5402 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05005403 final int densityDpi = mCurDefaultDisplayDpi;
5404 if (!mDensityCompatMode
5405 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
5406 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
5407 DisplayMetrics.DENSITY_DEVICE = densityDpi;
5408 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005409 }
5410 }
5411
Todd Kennedye713efc2016-05-10 13:45:40 -07005412 /**
5413 * Returns the correct library directory for the current ABI.
5414 * <p>
5415 * If we're dealing with a multi-arch application that has both 32 and 64 bit shared
5416 * libraries, we might need to choose the secondary depending on what the current
5417 * runtime's instruction set is.
5418 */
5419 private String getInstrumentationLibrary(ApplicationInfo appInfo, InstrumentationInfo insInfo) {
5420 if (appInfo.primaryCpuAbi != null && appInfo.secondaryCpuAbi != null) {
5421 // Get the instruction set supported by the secondary ABI. In the presence
5422 // of a native bridge this might be different than the one secondary ABI used.
5423 String secondaryIsa =
5424 VMRuntime.getInstructionSet(appInfo.secondaryCpuAbi);
5425 final String secondaryDexCodeIsa =
5426 SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
5427 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
5428
5429 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
5430 if (runtimeIsa.equals(secondaryIsa)) {
5431 return insInfo.secondaryNativeLibraryDir;
5432 }
5433 }
5434 return insInfo.nativeLibraryDir;
5435 }
5436
Adam Lesinskib61e4052016-05-19 18:23:05 -07005437 /**
5438 * The LocaleList set for the app's resources may have been shuffled so that the preferred
5439 * Locale is at position 0. We must find the index of this preferred Locale in the
5440 * original LocaleList.
5441 */
5442 private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
5443 final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
5444 final int newLocaleListSize = newLocaleList.size();
5445 for (int i = 0; i < newLocaleListSize; i++) {
5446 if (bestLocale.equals(newLocaleList.get(i))) {
5447 LocaleList.setDefault(newLocaleList, i);
5448 return;
5449 }
5450 }
Adam Lesinski27d30162016-05-25 16:45:14 -07005451
5452 // The app may have overridden the LocaleList with its own Locale
5453 // (not present in the available list). Push the chosen Locale
5454 // to the front of the list.
5455 LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
Adam Lesinskib61e4052016-05-19 18:23:05 -07005456 }
5457
Romain Guy65b345f2011-07-27 18:51:50 -07005458 private void handleBindApplication(AppBindData data) {
Calin Juravle8f5770d2016-04-12 14:12:04 +01005459 // Register the UI Thread as a sensitive thread to the runtime.
5460 VMRuntime.registerSensitiveThread();
Man Caocfa78b22015-06-11 20:14:34 -07005461 if (data.trackAllocation) {
5462 DdmVmInternal.enableRecentAllocations(true);
5463 }
5464
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005465 // Note when this process has started.
5466 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 mBoundApplication = data;
5469 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005470 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005472 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005473 if (data.initProfilerInfo != null) {
5474 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5475 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5476 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5477 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08005478 mProfiler.streamingOutput = data.initProfilerInfo.streamingOutput;
Jeff Hao1b012d32014-08-20 10:35:34 -07005479 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005481 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005482 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005483 android.ddm.DdmHandleAppName.setAppName(data.processName,
5484 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005486 if (data.persistent) {
5487 // Persistent processes on low-memory devices do not get to
5488 // use hardware accelerated drawing, since this can add too much
5489 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005490 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005491 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005492 }
5493 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005494
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005495 if (mProfiler.profileFd != null) {
5496 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005497 }
5498
Joe Onoratod630f102011-03-17 18:42:26 -07005499 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5500 // implementation to use the pool executor. Normally, we use the
5501 // serialized executor as the default. This has to happen in the
5502 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005503 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005504 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5505 }
5506
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005507 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005509 /*
5510 * Before spawning a new process, reset the time zone to be the system time zone.
5511 * This needs to be done because the system time zone could have changed after the
5512 * the spawning of this process. Without doing this this process would have the incorrect
5513 * system time zone.
5514 */
5515 TimeZone.setDefault(null);
5516
Adam Lesinskib61e4052016-05-19 18:23:05 -07005517 /*
5518 * Set the LocaleList. This may change once we create the App Context.
5519 */
5520 LocaleList.setDefault(data.config.getLocales());
5521
5522 synchronized (mResourcesManager) {
5523 /*
5524 * Update the system configuration since its preloaded and might not
5525 * reflect configuration changes. The configuration object passed
5526 * in AppBindData can be safely assumed to be up to date
5527 */
5528 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5529 mCurDefaultDisplayDpi = data.config.densityDpi;
5530
5531 // This calls mResourcesManager so keep it within the synchronized block.
5532 applyCompatConfiguration(mCurDefaultDisplayDpi);
5533 }
5534
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005535 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536
Dianne Hackborndde331c2012-08-03 14:01:57 -07005537 /**
5538 * Switch this process to density compatibility mode if needed.
5539 */
5540 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5541 == 0) {
5542 mDensityCompatMode = true;
5543 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5544 }
5545 updateDefaultDensity();
5546
Neil Fullerc66ec402016-11-14 16:06:42 +00005547 final String use24HourSetting = mCoreSettings.getString(Settings.System.TIME_12_24);
5548 Boolean is24Hr = null;
5549 if (use24HourSetting != null) {
5550 is24Hr = "24".equals(use24HourSetting) ? Boolean.TRUE : Boolean.FALSE;
5551 }
5552 // null : use locale default for 12/24 hour formatting,
5553 // false : use 12 hour format,
5554 // true : use 24 hour format.
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005555 DateFormat.set24HourTimePref(is24Hr);
5556
Jon Miranda836c0a82014-08-11 12:32:26 -07005557 View.mDebugViewAttributes =
5558 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5559
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005560 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005561 * For system applications on userdebug/eng builds, log stack
5562 * traces of disk and network access to dropbox for analysis.
5563 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005564 if ((data.appInfo.flags &
5565 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005566 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5567 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005568 }
5569
5570 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005571 * For apps targetting Honeycomb or later, we don't allow network usage
5572 * on the main event loop / UI thread. This is what ultimately throws
5573 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005574 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005575 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005576 StrictMode.enableDeathOnNetwork();
5577 }
5578
Jeff Sharkey344744b2016-01-28 19:03:30 -07005579 /**
5580 * For apps targetting N or later, we don't allow file:// Uri exposure.
5581 * This is what ultimately throws {@link FileUriExposedException}.
5582 */
5583 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005584 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005585 }
5586
Svet Ganov37e43272016-09-09 16:01:32 -07005587 // We deprecated Build.SERIAL and only apps that target pre NMR1
5588 // SDK can see it. Since access to the serial is now behind a
5589 // permission we push down the value and here we fix it up
5590 // before any app code has been loaded.
5591 try {
5592 Field field = Build.class.getDeclaredField("SERIAL");
5593 field.setAccessible(true);
5594 field.set(Build.class, data.buildSerial);
5595 } catch (NoSuchFieldException | IllegalAccessException e) {
5596 /* ignore */
5597 }
5598
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005599 if (data.debugMode != ApplicationThreadConstants.DEBUG_OFF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 // XXX should have option to change the port.
5601 Debug.changeDebugPort(8100);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005602 if (data.debugMode == ApplicationThreadConstants.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005603 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 + " is waiting for the debugger on port 8100...");
5605
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005606 IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 try {
5608 mgr.showWaitingForDebugger(mAppThread, true);
5609 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005610 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 }
5612
5613 Debug.waitForDebugger();
5614
5615 try {
5616 mgr.showWaitingForDebugger(mAppThread, false);
5617 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005618 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 }
5620
5621 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005622 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 + " can be debugged on port 8100...");
5624 }
5625 }
5626
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005627 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005628 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5629 Trace.setAppTracingAllowed(isAppDebuggable);
5630 if (isAppDebuggable && data.enableBinderTracking) {
5631 Binder.enableTracing();
5632 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005633
Robert Greenwalt434203a2010-10-11 16:00:27 -07005634 /**
5635 * Initialize the default http proxy in this process for the reasons we set the time zone.
5636 */
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005637 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Setup proxies");
Alan Viverettebe64eae2015-09-03 14:56:04 -04005638 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005639 if (b != null) {
5640 // In pre-boot mode (doing initial launch to collect password), not
5641 // all system is up. This includes the connectivity service, so don't
5642 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005643 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005644 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005645 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005646 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005647 } catch (RemoteException e) {
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005648 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005649 throw e.rethrowFromSystemServer();
5650 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005651 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005652 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Robert Greenwalt434203a2010-10-11 16:00:27 -07005653
Alan Viverette2107d692015-09-03 14:55:27 -04005654 // Instrumentation info affects the class loader, so load it before
5655 // setting up the app context.
5656 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005657 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005659 ii = new ApplicationPackageManager(null, getPackageManager())
5660 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005663 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 }
5665
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005666 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005667 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005668 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
Todd Kennedye713efc2016-05-10 13:45:40 -07005669 mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005671 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5672 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005673 } else {
5674 ii = null;
5675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676
Alan Viverette2107d692015-09-03 14:55:27 -04005677 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005678 updateLocaleListFromAppContext(appContext,
5679 mResourcesManager.getConfiguration().getLocales());
Seigo Nonakac14dd782016-03-30 23:09:16 +09005680
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005681 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005682 // This cache location probably points at credential-encrypted
5683 // storage which may not be accessible yet; assign it anyway instead
5684 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005685 final File cacheDir = appContext.getCacheDir();
5686 if (cacheDir != null) {
5687 // Provide a usable directory for temporary files
5688 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5689 } else {
5690 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5691 + "due to missing cache directory");
5692 }
Alan Viverette346296b2015-09-01 13:08:05 -04005693
Calin Juravle2a727d72016-04-15 19:33:46 +01005694 // Setup a location to store generated/compiled graphics code.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005695 final Context deviceContext = appContext.createDeviceProtectedStorageContext();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005696 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005697 if (codeCacheDir != null) {
Jesse Hallb12249b2016-12-12 12:53:02 -08005698 setupGraphicsSupport(appContext, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005699 } else {
Calin Juravle2a727d72016-04-15 19:33:46 +01005700 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005701 }
Michael Lentine2ba303f2016-02-01 20:44:34 -06005702 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005703
Calin Juravle37dfc8e2017-03-13 19:02:32 -07005704 // If we use profiles, setup the dex reporter to notify package manager
5705 // of any relevant dex loads. The idle maintenance job will use the information
5706 // reported to optimize the loaded dex files.
5707 // Note that we only need one global reporter per app.
5708 // Make sure we do this before calling onCreate so that we can capture the
5709 // complete application startup.
5710 if (SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
Calin Juravlef5a7bfc2017-03-13 23:30:30 -07005711 BaseDexClassLoader.setReporter(DexLoadReporter.getInstance());
Calin Juravle37dfc8e2017-03-13 19:02:32 -07005712 }
5713
Chad Brubaker78d47122015-11-17 22:26:58 -08005714 // Install the Network Security Config Provider. This must happen before the application
5715 // code is loaded to prevent issues with instances of TLS objects being created before
5716 // the provider is installed.
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005717 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "NetworkSecurityConfigProvider.install");
Chad Brubaker276ee962016-06-08 12:57:46 -07005718 NetworkSecurityConfigProvider.install(appContext);
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005719 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Chad Brubaker78d47122015-11-17 22:26:58 -08005720
Alan Viverette2107d692015-09-03 14:55:27 -04005721 // Continue loading instrumentation.
5722 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005723 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005724 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005725 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005726 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005727 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005728 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729
5730 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005731 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005732 mInstrumentation = (Instrumentation)
5733 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5734 } catch (Exception e) {
5735 throw new RuntimeException(
5736 "Unable to instantiate instrumentation "
5737 + data.instrumentationName + ": " + e.toString(), e);
5738 }
5739
Alan Viverettebe64eae2015-09-03 14:56:04 -04005740 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5741 mInstrumentation.init(this, instrContext, appContext, component,
5742 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005744 if (mProfiler.profileFile != null && !ii.handleProfiling
5745 && mProfiler.profileFd == null) {
5746 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005747 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 file.getParentFile().mkdirs();
5749 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5750 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 } else {
5752 mInstrumentation = new Instrumentation();
5753 }
5754
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005755 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005756 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005757 } else {
5758 // Small heap, clamp to the current growth limit and let the heap release
5759 // pages after the growth limit to the non growth limit capacity. b/18387825
5760 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005761 }
5762
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005763 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005764 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005765 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005766 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005768 // If the app is being launched for full backup or restore, bring it up in
5769 // a restricted environment with the base application class.
5770 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5771 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005772
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005773 // don't bring up providers in restricted mode; they may depend on the
5774 // app's custom Application class
5775 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005776 if (!ArrayUtils.isEmpty(data.providers)) {
5777 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005778 // For process that contains content providers, we want to
5779 // ensure that the JIT is enabled "at some point".
5780 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5781 }
5782 }
5783
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005784 // Do this after providers, since instrumentation tests generally start their
5785 // test thread at this point, and we don't want that racing.
5786 try {
5787 mInstrumentation.onCreate(data.instrumentationArgs);
5788 }
5789 catch (Exception e) {
5790 throw new RuntimeException(
5791 "Exception thrown in onCreate() of "
5792 + data.instrumentationName + ": " + e.toString(), e);
5793 }
5794
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005795 try {
5796 mInstrumentation.callApplicationOnCreate(app);
5797 } catch (Exception e) {
5798 if (!mInstrumentation.onException(app, e)) {
5799 throw new RuntimeException(
5800 "Unable to create application " + app.getClass().getName()
5801 + ": " + e.toString(), e);
5802 }
5803 }
5804 } finally {
5805 StrictMode.setThreadPolicy(savedPolicy);
5806 }
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005807
5808 // Preload fonts resources
Clara Bayarriff221612017-03-15 17:27:31 +00005809 Typeface.setApplicationContext(appContext);
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005810 try {
5811 final ApplicationInfo info =
Amith Yamasani336d29d2017-02-14 15:14:09 -08005812 getPackageManager().getApplicationInfo(
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005813 data.appInfo.packageName,
5814 PackageManager.GET_META_DATA /*flags*/,
5815 UserHandle.myUserId());
5816 if (info.metaData != null) {
5817 final int preloadedFontsResource = info.metaData.getInt(
5818 ApplicationInfo.METADATA_PRELOADED_FONTS, 0);
5819 if (preloadedFontsResource != 0) {
5820 data.info.mResources.preloadFonts(preloadedFontsResource);
5821 }
5822 }
5823 } catch (RemoteException e) {
5824 throw e.rethrowFromSystemServer();
5825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 }
5827
5828 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005829 IActivityManager am = ActivityManager.getService();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005830 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5831 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 Debug.stopMethodTracing();
5833 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005834 //Slog.i(TAG, "am: " + ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005835 // + ", app thr: " + mAppThread);
5836 try {
5837 am.finishInstrumentation(mAppThread, resultCode, results);
5838 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005839 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005840 }
5841 }
5842
Romain Guy65b345f2011-07-27 18:51:50 -07005843 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 Context context, List<ProviderInfo> providers) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005845 final ArrayList<ContentProviderHolder> results = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005846
Romain Guya998dff2012-03-23 18:58:36 -07005847 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005848 if (DEBUG_PROVIDER) {
5849 StringBuilder buf = new StringBuilder(128);
5850 buf.append("Pub ");
5851 buf.append(cpi.authority);
5852 buf.append(": ");
5853 buf.append(cpi.name);
5854 Log.i(TAG, buf.toString());
5855 }
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005856 ContentProviderHolder cph = installProvider(context, null, cpi,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005857 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5858 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005859 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005860 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
5862 }
5863
5864 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005865 ActivityManager.getService().publishContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 getApplicationThread(), results);
5867 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005868 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 }
5870 }
5871
Jeff Sharkey6d515712012-09-20 16:06:08 -07005872 public final IContentProvider acquireProvider(
5873 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005874 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005875 if (provider != null) {
5876 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 }
5878
Wale Ogunwale1d646122015-04-24 14:45:14 -07005879 // There is a possible race here. Another thread may try to acquire
5880 // the same provider at the same time. When this happens, we want to ensure
5881 // that the first one wins.
5882 // Note that we cannot hold the lock while acquiring and installing the
5883 // provider since it might take a long time to run and it could also potentially
5884 // be re-entrant in the case where the provider is in the same process.
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005885 ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005886 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005887 holder = ActivityManager.getService().getContentProvider(
Wale Ogunwale1d646122015-04-24 14:45:14 -07005888 getApplicationThread(), auth, userId, stable);
5889 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005890 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005891 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005892 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005893 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 return null;
5895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005896
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005897 // Install provider will increment the reference count for us, and break
5898 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005899 holder = installProvider(c, holder, holder.info,
5900 true /*noisy*/, holder.noReleaseNeeded, stable);
5901 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005902 }
5903
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005904 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5905 if (stable) {
5906 prc.stableCount += 1;
5907 if (prc.stableCount == 1) {
5908 // We are acquiring a new stable reference on the provider.
5909 int unstableDelta;
5910 if (prc.removePending) {
5911 // We have a pending remove operation, which is holding the
5912 // last unstable reference. At this point we are converting
5913 // that unstable reference to our new stable reference.
5914 unstableDelta = -1;
5915 // Cancel the removal of the provider.
5916 if (DEBUG_PROVIDER) {
5917 Slog.v(TAG, "incProviderRef: stable "
5918 + "snatched provider from the jaws of death");
5919 }
5920 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005921 // There is a race! It fails to remove the message, which
5922 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005923 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5924 } else {
5925 unstableDelta = 0;
5926 }
5927 try {
5928 if (DEBUG_PROVIDER) {
5929 Slog.v(TAG, "incProviderRef Now stable - "
5930 + prc.holder.info.name + ": unstableDelta="
5931 + unstableDelta);
5932 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005933 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005934 prc.holder.connection, 1, unstableDelta);
5935 } catch (RemoteException e) {
5936 //do nothing content provider object is dead any way
5937 }
5938 }
5939 } else {
5940 prc.unstableCount += 1;
5941 if (prc.unstableCount == 1) {
5942 // We are acquiring a new unstable reference on the provider.
5943 if (prc.removePending) {
5944 // Oh look, we actually have a remove pending for the
5945 // provider, which is still holding the last unstable
5946 // reference. We just need to cancel that to take new
5947 // ownership of the reference.
5948 if (DEBUG_PROVIDER) {
5949 Slog.v(TAG, "incProviderRef: unstable "
5950 + "snatched provider from the jaws of death");
5951 }
5952 prc.removePending = false;
5953 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5954 } else {
5955 // First unstable ref, increment our count in the
5956 // activity manager.
5957 try {
5958 if (DEBUG_PROVIDER) {
5959 Slog.v(TAG, "incProviderRef: Now unstable - "
5960 + prc.holder.info.name);
5961 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005962 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005963 prc.holder.connection, 0, 1);
5964 } catch (RemoteException e) {
5965 //do nothing content provider object is dead any way
5966 }
5967 }
5968 }
5969 }
5970 }
5971
Jeff Sharkey6d515712012-09-20 16:06:08 -07005972 public final IContentProvider acquireExistingProvider(
5973 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005974 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005975 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005976 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005977 if (pr == null) {
5978 return null;
5979 }
5980
5981 IContentProvider provider = pr.mProvider;
5982 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005983 if (!jBinder.isBinderAlive()) {
5984 // The hosting process of the provider has died; we can't
5985 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005986 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005987 + ": existing object's process dead");
5988 handleUnstableProviderDiedLocked(jBinder, true);
5989 return null;
5990 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005991
5992 // Only increment the ref count if we have one. If we don't then the
5993 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005994 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005995 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005996 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005997 }
5998 return provider;
5999 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07006000 }
6001
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006002 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
6003 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006004 return false;
6005 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006007 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006008 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006009 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006010 if (prc == null) {
6011 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006012 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006013 }
6014
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006015 boolean lastRef = false;
6016 if (stable) {
6017 if (prc.stableCount == 0) {
6018 if (DEBUG_PROVIDER) Slog.v(TAG,
6019 "releaseProvider: stable ref count already 0, how?");
6020 return false;
6021 }
6022 prc.stableCount -= 1;
6023 if (prc.stableCount == 0) {
6024 // What we do at this point depends on whether there are
6025 // any unstable refs left: if there are, we just tell the
6026 // activity manager to decrement its stable count; if there
6027 // aren't, we need to enqueue this provider to be removed,
6028 // and convert to holding a single unstable ref while
6029 // doing so.
6030 lastRef = prc.unstableCount == 0;
6031 try {
6032 if (DEBUG_PROVIDER) {
6033 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
6034 + lastRef + " - " + prc.holder.info.name);
6035 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006036 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006037 prc.holder.connection, -1, lastRef ? 1 : 0);
6038 } catch (RemoteException e) {
6039 //do nothing content provider object is dead any way
6040 }
6041 }
6042 } else {
6043 if (prc.unstableCount == 0) {
6044 if (DEBUG_PROVIDER) Slog.v(TAG,
6045 "releaseProvider: unstable ref count already 0, how?");
6046 return false;
6047 }
6048 prc.unstableCount -= 1;
6049 if (prc.unstableCount == 0) {
6050 // If this is the last reference, we need to enqueue
6051 // this provider to be removed instead of telling the
6052 // activity manager to remove it at this point.
6053 lastRef = prc.stableCount == 0;
6054 if (!lastRef) {
6055 try {
6056 if (DEBUG_PROVIDER) {
6057 Slog.v(TAG, "releaseProvider: No longer unstable - "
6058 + prc.holder.info.name);
6059 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006060 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006061 prc.holder.connection, 0, -1);
6062 } catch (RemoteException e) {
6063 //do nothing content provider object is dead any way
6064 }
6065 }
6066 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006067 }
6068
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006069 if (lastRef) {
6070 if (!prc.removePending) {
6071 // Schedule the actual remove asynchronously, since we don't know the context
6072 // this will be called in.
6073 // TODO: it would be nice to post a delayed message, so
6074 // if we come back and need the same provider quickly
6075 // we will still have it available.
6076 if (DEBUG_PROVIDER) {
6077 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
6078 + prc.holder.info.name);
6079 }
6080 prc.removePending = true;
6081 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
6082 mH.sendMessage(msg);
6083 } else {
6084 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
6085 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006086 }
6087 return true;
6088 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006089 }
6090
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006091 final void completeRemoveProvider(ProviderRefCount prc) {
6092 synchronized (mProviderMap) {
6093 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006094 // There was a race! Some other client managed to acquire
6095 // the provider before the removal was completed.
6096 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006097 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006098 + "provider still in use");
6099 return;
6100 }
6101
Guobin Zhang9e3e52662013-03-21 13:57:11 +08006102 // More complicated race!! Some client managed to acquire the
6103 // provider and release it before the removal was completed.
6104 // Continue the removal, and abort the next remove message.
6105 prc.removePending = false;
6106
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006107 final IBinder jBinder = prc.holder.provider.asBinder();
6108 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
6109 if (existingPrc == prc) {
6110 mProviderRefCountMap.remove(jBinder);
6111 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006112
Dianne Hackbornadd005c2013-07-17 18:43:12 -07006113 for (int i=mProviderMap.size()-1; i>=0; i--) {
6114 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006115 IBinder myBinder = pr.mProvider.asBinder();
6116 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07006117 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006118 }
6119 }
6120 }
6121
6122 try {
6123 if (DEBUG_PROVIDER) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006124 Slog.v(TAG, "removeProvider: Invoking ActivityManagerService."
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006125 + "removeContentProvider(" + prc.holder.info.name + ")");
6126 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006127 ActivityManager.getService().removeContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006128 prc.holder.connection, false);
6129 } catch (RemoteException e) {
6130 //do nothing content provider object is dead any way
6131 }
6132 }
6133
6134 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006135 synchronized (mProviderMap) {
6136 handleUnstableProviderDiedLocked(provider, fromClient);
6137 }
6138 }
6139
6140 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
6141 ProviderRefCount prc = mProviderRefCountMap.get(provider);
6142 if (prc != null) {
6143 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
6144 + provider + " " + prc.holder.info.name);
6145 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09006146 for (int i=mProviderMap.size()-1; i>=0; i--) {
6147 ProviderClientRecord pr = mProviderMap.valueAt(i);
6148 if (pr != null && pr.mProvider.asBinder() == provider) {
6149 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
6150 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006151 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006152 }
You Kimbc74de62013-10-01 00:13:26 +09006153
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006154 if (fromClient) {
6155 // We found out about this due to execution in our client
6156 // code. Tell the activity manager about it now, to ensure
6157 // that the next time we go to do anything with the provider
6158 // it knows it is dead (so we don't race with its death
6159 // notification).
6160 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006161 ActivityManager.getService().unstableProviderDied(
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006162 prc.holder.connection);
6163 } catch (RemoteException e) {
6164 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006165 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07006166 }
6167 }
6168 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006169
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006170 final void appNotRespondingViaProvider(IBinder provider) {
6171 synchronized (mProviderMap) {
6172 ProviderRefCount prc = mProviderRefCountMap.get(provider);
6173 if (prc != null) {
6174 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006175 ActivityManager.getService()
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006176 .appNotRespondingViaProvider(prc.holder.connection);
6177 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006178 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006179 }
6180 }
6181 }
6182 }
6183
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006184 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006185 ContentProvider localProvider, ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08006186 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07006187 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
6188
Jeff Sharkey0a17db12016-11-04 11:23:46 -06006189 if (provider != null) {
6190 // If this provider is hosted by the core OS and cannot be upgraded,
6191 // then I guess we're okay doing blocking calls to it.
6192 for (String auth : auths) {
6193 switch (auth) {
6194 case ContactsContract.AUTHORITY:
6195 case CallLog.AUTHORITY:
6196 case CallLog.SHADOW_AUTHORITY:
6197 case BlockedNumberContract.AUTHORITY:
6198 case CalendarContract.AUTHORITY:
6199 case Downloads.Impl.AUTHORITY:
Jeff Sharkeycb621622016-11-11 14:04:32 -07006200 case "telephony":
Jeff Sharkey0a17db12016-11-04 11:23:46 -06006201 Binder.allowBlocking(provider.asBinder());
6202 }
6203 }
6204 }
6205
Jeff Sharkey6d515712012-09-20 16:06:08 -07006206 final ProviderClientRecord pcr = new ProviderClientRecord(
6207 auths, provider, localProvider, holder);
6208 for (String auth : auths) {
6209 final ProviderKey key = new ProviderKey(auth, userId);
6210 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006211 if (existing != null) {
6212 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07006213 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006214 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07006215 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006216 }
6217 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006218 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006219 }
6220
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006221 /**
6222 * Installs the provider.
6223 *
6224 * Providers that are local to the process or that come from the system server
6225 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
6226 * Other remote providers are reference counted. The initial reference count
6227 * for all reference counted providers is one. Providers that are not reference
6228 * counted do not have a reference count (at all).
6229 *
6230 * This method detects when a provider has already been installed. When this happens,
6231 * it increments the reference count of the existing provider (if appropriate)
6232 * and returns the existing provider. This can happen due to concurrent
6233 * attempts to acquire the same provider.
6234 */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006235 private ContentProviderHolder installProvider(Context context,
6236 ContentProviderHolder holder, ProviderInfo info,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006237 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006239 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07006240 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006241 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07006242 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 + info.name);
6244 }
6245 Context c = null;
6246 ApplicationInfo ai = info.applicationInfo;
6247 if (context.getPackageName().equals(ai.packageName)) {
6248 c = context;
6249 } else if (mInitialApplication != null &&
6250 mInitialApplication.getPackageName().equals(ai.packageName)) {
6251 c = mInitialApplication;
6252 } else {
6253 try {
6254 c = context.createPackageContext(ai.packageName,
6255 Context.CONTEXT_INCLUDE_CODE);
6256 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07006257 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006258 }
6259 }
6260 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08006261 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006262 ai.packageName +
6263 " while loading content provider " +
6264 info.name);
6265 return null;
6266 }
Adam Lesinski4e862812016-11-21 16:02:24 -08006267
6268 if (info.splitName != null) {
6269 try {
6270 c = c.createContextForSplit(info.splitName);
6271 } catch (NameNotFoundException e) {
6272 throw new RuntimeException(e);
6273 }
6274 }
6275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006276 try {
6277 final java.lang.ClassLoader cl = c.getClassLoader();
6278 localProvider = (ContentProvider)cl.
6279 loadClass(info.name).newInstance();
6280 provider = localProvider.getIContentProvider();
6281 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08006282 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006283 info.name + " from sourceDir " +
6284 info.applicationInfo.sourceDir);
6285 return null;
6286 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006287 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 TAG, "Instantiating local provider " + info.name);
6289 // XXX Need to create the correct context for this provider.
6290 localProvider.attachInfo(c, info);
6291 } catch (java.lang.Exception e) {
6292 if (!mInstrumentation.onException(null, e)) {
6293 throw new RuntimeException(
6294 "Unable to get provider " + info.name
6295 + ": " + e.toString(), e);
6296 }
6297 return null;
6298 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006299 } else {
6300 provider = holder.provider;
6301 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006302 + info.name);
6303 }
6304
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006305 ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006306
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006307 synchronized (mProviderMap) {
6308 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
6309 + " / " + info.name);
6310 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006311 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006312 ComponentName cname = new ComponentName(info.packageName, info.name);
6313 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006314 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006315 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006316 Slog.v(TAG, "installProvider: lost the race, "
6317 + "using existing local provider");
6318 }
6319 provider = pr.mProvider;
6320 } else {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006321 holder = new ContentProviderHolder(info);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006322 holder.provider = provider;
6323 holder.noReleaseNeeded = true;
6324 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006325 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006326 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006327 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006328 retHolder = pr.mHolder;
6329 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006330 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
6331 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006332 if (DEBUG_PROVIDER) {
6333 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006334 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07006335 // We need to transfer our new reference to the existing
6336 // ref count, releasing the old one... but only if
6337 // release is needed (that is, it is not running in the
6338 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006339 if (!noReleaseNeeded) {
6340 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07006341 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006342 ActivityManager.getService().removeContentProvider(
Wale Ogunwale1d646122015-04-24 14:45:14 -07006343 holder.connection, stable);
6344 } catch (RemoteException e) {
6345 //do nothing content provider object is dead any way
6346 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006347 }
6348 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006349 ProviderClientRecord client = installProviderAuthoritiesLocked(
6350 provider, localProvider, holder);
6351 if (noReleaseNeeded) {
6352 prc = new ProviderRefCount(holder, client, 1000, 1000);
6353 } else {
6354 prc = stable
6355 ? new ProviderRefCount(holder, client, 1, 0)
6356 : new ProviderRefCount(holder, client, 0, 1);
6357 }
6358 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006359 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006360 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 }
6362 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006363 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006364 }
6365
Romain Guy65b345f2011-07-27 18:51:50 -07006366 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08006367 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006370 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07006371 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08006372 public void run() {
6373 ensureJitEnabled();
6374 }
6375 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07006376 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
6377 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006378 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006379 final IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006380 try {
6381 mgr.attachApplication(mAppThread);
6382 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006383 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006384 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006385 // Watch for getting close to heap limit.
6386 BinderInternal.addGcWatcher(new Runnable() {
6387 @Override public void run() {
6388 if (!mSomeActivitiesChanged) {
6389 return;
6390 }
6391 Runtime runtime = Runtime.getRuntime();
6392 long dalvikMax = runtime.maxMemory();
6393 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
6394 if (dalvikUsed > ((3*dalvikMax)/4)) {
6395 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
6396 + " total=" + (runtime.totalMemory()/1024)
6397 + " used=" + (dalvikUsed/1024));
6398 mSomeActivitiesChanged = false;
6399 try {
6400 mgr.releaseSomeActivities(mAppThread);
6401 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006402 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006403 }
6404 }
6405 }
6406 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 } else {
6408 // Don't set application object here -- if the system crashes,
6409 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07006410 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006411 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 try {
6413 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07006414 ContextImpl context = ContextImpl.createAppContext(
6415 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07006416 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
6417 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006418 } catch (Exception e) {
6419 throw new RuntimeException(
6420 "Unable to instantiate Application():" + e.toString(), e);
6421 }
6422 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006423
6424 // add dropbox logging to libcore
6425 DropBox.setReporter(new DropBoxReporter());
6426
Andrii Kulian44607962017-03-16 11:06:24 -07006427 ViewRootImpl.ConfigChangedCallback configChangedCallback
6428 = (Configuration globalConfig) -> {
6429 synchronized (mResourcesManager) {
6430 // We need to apply this change to the resources immediately, because upon returning
6431 // the view hierarchy will be informed about it.
6432 if (mResourcesManager.applyConfigurationToResourcesLocked(globalConfig,
6433 null /* compat */)) {
6434 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
6435 mResourcesManager.getConfiguration().getLocales());
Adam Lesinskib61e4052016-05-19 18:23:05 -07006436
Andrii Kulian44607962017-03-16 11:06:24 -07006437 // This actually changed the resources! Tell everyone about it.
6438 if (mPendingConfiguration == null
6439 || mPendingConfiguration.isOtherSeqNewer(globalConfig)) {
6440 mPendingConfiguration = globalConfig;
6441 sendMessage(H.CONFIGURATION_CHANGED, globalConfig);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006442 }
6443 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006444 }
Andrii Kulian44607962017-03-16 11:06:24 -07006445 };
6446 ViewRootImpl.addConfigCallback(configChangedCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006447 }
6448
Romain Guy5e9120d2012-01-30 12:17:22 -08006449 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07006450 // The system process on low-memory devices do not get to use hardware
6451 // accelerated drawing, since this can add too much overhead to the
6452 // process.
6453 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08006454 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07006455 } else {
John Reck51aaf902015-12-02 15:08:07 -08006456 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07006457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006458 ActivityThread thread = new ActivityThread();
6459 thread.attach(true);
6460 return thread;
6461 }
6462
Jeff Brown10e89712011-07-08 18:52:57 -07006463 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07006465 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 }
6467 }
6468
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006469 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07006470 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08006471 if (mCoreSettings != null) {
6472 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006473 }
Craig Mautner88c05892013-06-28 09:47:45 -07006474 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006475 }
6476 }
6477
Geremy Condra69689a72012-09-11 16:57:17 -07006478 private static class EventLoggingReporter implements EventLogger.Reporter {
6479 @Override
6480 public void report (int code, Object... list) {
6481 EventLog.writeEvent(code, list);
6482 }
6483 }
6484
Geremy Condrab7faaf42012-09-19 18:07:42 -07006485 private class DropBoxReporter implements DropBox.Reporter {
6486
6487 private DropBoxManager dropBox;
6488
Narayan Kamath7f062242015-04-08 13:24:13 +01006489 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07006490
6491 @Override
6492 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006493 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006494 dropBox.addData(tag, data, flags);
6495 }
6496
6497 @Override
6498 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006499 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006500 dropBox.addText(tag, data);
6501 }
Narayan Kamath7f062242015-04-08 13:24:13 +01006502
6503 private synchronized void ensureInitialized() {
6504 if (dropBox == null) {
6505 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
6506 }
6507 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006508 }
6509
Romain Guy65b345f2011-07-27 18:51:50 -07006510 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006511 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07006512 SamplingProfilerIntegration.start();
6513
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08006514 // CloseGuard defaults to true and can be quite spammy. We
6515 // disable it here, but selectively enable it later (via
6516 // StrictMode) on debug builds, but using DropBox, not logs.
6517 CloseGuard.setEnabled(false);
6518
Andreas Gamped281b422016-07-08 03:50:27 +00006519 Environment.initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -07006520
Geremy Condra69689a72012-09-11 16:57:17 -07006521 // Set the reporter for event logging in libcore
6522 EventLogger.setReporter(new EventLoggingReporter());
6523
Robin Lee3d076af2014-04-25 14:57:49 +01006524 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6525 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6526 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 Process.setArgV0("<pre-initialized>");
6529
6530 Looper.prepareMainLooper();
6531
6532 ActivityThread thread = new ActivityThread();
6533 thread.attach(false);
6534
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006535 if (sMainThreadHandler == null) {
6536 sMainThreadHandler = thread.getHandler();
6537 }
6538
Dianne Hackborn287952c2010-09-22 22:34:31 -07006539 if (false) {
6540 Looper.myLooper().setMessageLogging(new
6541 LogPrinter(Log.DEBUG, "ActivityThread"));
6542 }
6543
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006544 // End of event ActivityThreadMain.
6545 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 Looper.loop();
6547
Jeff Brown10e89712011-07-08 18:52:57 -07006548 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006549 }
Chris Craikfc294242016-12-13 18:10:46 -08006550
6551 // ------------------ Regular JNI ------------------------
6552
6553 private native void nDumpGraphicsInfo(FileDescriptor fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006554}