blob: 2e4ce1886fd6ead910ee969bb8b13e0afda641f8 [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;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070032import android.content.IIntentReceiver;
Jesse Hall317fa5a2017-05-23 15:46:55 -070033import android.content.Intent;
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;
Jeff Brownbd6e1502012-08-28 03:27:37 -070054import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050055import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070056import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050057import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070058import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040059import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040060import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070061import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070062import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070063import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.os.Bundle;
65import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070066import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070067import android.os.Environment;
Jesse Hallb12249b2016-12-12 12:53:02 -080068import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Handler;
70import android.os.IBinder;
Adam Lesinskib61e4052016-05-19 18:23:05 -070071import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072import android.os.Looper;
73import android.os.Message;
74import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080075import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070076import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070077import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.os.Process;
79import android.os.RemoteException;
80import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070081import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070083import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070084import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070085import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070086import android.os.UserHandle;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060087import android.provider.BlockedNumberContract;
88import android.provider.CalendarContract;
89import android.provider.CallLog;
90import android.provider.ContactsContract;
91import android.provider.Downloads;
Seigo Nonaka54084b62017-04-24 14:46:23 -070092import android.provider.FontsContract;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000093import android.provider.Settings;
Jesse Hall317fa5a2017-05-23 15:46:55 -070094import android.renderscript.RenderScriptCacheDir;
Chad Brubakerc72875b2016-04-27 16:35:11 -070095import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080096import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070098import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.util.DisplayMetrics;
100import android.util.EventLog;
101import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700102import android.util.LogPrinter;
Jeff Sharkeya6bfeae2017-07-05 16:50:24 -0600103import android.util.LogWriter;
Craig Mautnereb8abf72014-07-02 15:04:09 -0700104import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -0800105import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800106import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -0700107import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -0700108import android.util.SuperNotCalledException;
Adam Lesinski3ad1b482016-04-01 16:41:41 -0700109import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -0800111import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import android.view.View;
113import android.view.ViewDebug;
114import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -0700115import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116import android.view.Window;
117import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700118import android.view.WindowManagerGlobal;
Richard Uhler2c036192016-09-14 09:48:31 +0100119import android.webkit.WebView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700121import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700122import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800123import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124import com.android.internal.os.BinderInternal;
125import com.android.internal.os.RuntimeInit;
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;
Jeff Sharkeya6bfeae2017-07-05 16:50:24 -0600129import com.android.internal.util.IndentingPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700130import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100131import com.android.org.conscrypt.TrustedCertificateStore;
Jesse Hall317fa5a2017-05-23 15:46:55 -0700132
133import dalvik.system.BaseDexClassLoader;
134import dalvik.system.CloseGuard;
135import dalvik.system.VMDebug;
136import dalvik.system.VMRuntime;
137
Jesse Hall317fa5a2017-05-23 15:46:55 -0700138import libcore.io.DropBox;
139import libcore.io.EventLogger;
140import libcore.io.IoUtils;
141import libcore.net.event.NetworkEventDispatcher;
142
143import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145import java.io.File;
146import java.io.FileDescriptor;
147import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700148import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149import java.io.PrintWriter;
150import java.lang.ref.WeakReference;
Svet Ganov37e43272016-09-09 16:01:32 -0700151import java.lang.reflect.Field;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700152import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000153import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154import java.util.ArrayList;
Jeff Sharkeya6bfeae2017-07-05 16:50:24 -0600155import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156import java.util.List;
Adam Lesinskib61e4052016-05-19 18:23:05 -0700157import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800159import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700162final class RemoteServiceException extends AndroidRuntimeException {
163 public RemoteServiceException(String msg) {
164 super(msg);
165 }
166}
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168/**
169 * This manages the execution of the main thread in an
170 * application process, scheduling and executing activities,
171 * broadcasts, and other operations on it as the activity
172 * manager requests.
173 *
174 * {@hide}
175 */
176public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700177 /** @hide */
178 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700179 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700180 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700181 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700182 /** @hide */
183 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700184 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700185 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700186 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800187 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700188 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700189 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700190 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800193 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
194 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800195 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Dianne Hackborn455625e2015-01-21 09:55:13 -0800197 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
198 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
199 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
200 public static final int SERVICE_DONE_EXECUTING_START = 1;
201 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
202 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
203
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700204 // Details for pausing activity.
205 private static final int USER_LEAVING = 1;
206 private static final int DONT_REPORT = 2;
207
Filip Gruszczynskica664812015-12-04 12:43:36 -0800208 // Whether to invoke an activity callback after delivering new configuration.
209 private static final boolean REPORT_TO_ACTIVITY = true;
210
Sudheer Shanka84a48952017-03-08 18:19:01 -0800211 /**
212 * Denotes an invalid sequence number corresponding to a process state change.
213 */
214 public static final long INVALID_PROC_STATE_SEQ = -1;
215
216 private final Object mNetworkPolicyLock = new Object();
217
218 /**
219 * Denotes the sequence number of the process state change for which the main thread needs
220 * to block until the network rules are updated for it.
221 *
222 * Value of {@link #INVALID_PROC_STATE_SEQ} indicates there is no need for blocking.
223 */
224 @GuardedBy("mNetworkPolicyLock")
225 private long mNetworkBlockSeq = INVALID_PROC_STATE_SEQ;
226
Jeff Browndefd4a62014-03-10 21:24:37 -0700227 private ContextImpl mSystemContext;
Adam Lesinskia82b6262017-03-21 16:56:17 -0700228 private ContextImpl mSystemUiContext;
Bob Leee5408332009-09-04 18:31:17 -0700229
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700230 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700232 final ApplicationThread mAppThread = new ApplicationThread();
233 final Looper mLooper = Looper.myLooper();
234 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700235 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700236 // List of new activities (via ActivityRecord.nextIdle) that should
237 // be reported when next we idle.
238 ActivityClientRecord mNewActivities = null;
239 // Number of activities that are currently visible on-screen.
240 int mNumVisibleActivities = 0;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700241 ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
242 private int mLastSessionId;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700243 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700244 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700245 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700246 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700247 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700249 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700250 Application mInitialApplication;
251 final ArrayList<Application> mAllApplications
252 = new ArrayList<Application>();
253 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700254 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800255 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700256 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700257 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700258 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700259 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700260 String[] mInstrumentationSplitAppDirs = null;
261 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700262 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700263 String[] mInstrumentedSplitAppDirs = null;
264 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700265 boolean mSystemThread = false;
266 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700267 boolean mSomeActivitiesChanged = false;
Chong Zhang4951f9d2016-06-23 13:15:08 -0700268 boolean mUpdatingSystemConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100270 // These can be accessed by multiple threads; mResourcesManager is the lock.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700271 // XXX For now we keep around information about all packages we have
272 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800273 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700274 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800275 // which means this lock gets held while the activity and window managers
276 // holds their own lock. Thus you MUST NEVER call back into the activity manager
277 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700278 // These LoadedApk are only valid for the userId that we're running as.
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100279 @GuardedBy("mResourcesManager")
280 final ArrayMap<String, WeakReference<LoadedApk>> mPackages = new ArrayMap<>();
281 @GuardedBy("mResourcesManager")
282 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages = new ArrayMap<>();
283 @GuardedBy("mResourcesManager")
284 final ArrayList<ActivityClientRecord> mRelaunchingActivities = new ArrayList<>();
285 @GuardedBy("mResourcesManager")
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700286 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700287 // Because we merge activity relaunch operations we can't depend on the ordering provided by
288 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
289 // us to drop certain events, if we know that they happened before relaunch we already executed.
290 // This represents the order of receiving the request from AM.
291 @GuardedBy("mResourcesManager")
292 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Craig Mautner88c05892013-06-28 09:47:45 -0700294 private final ResourcesManager mResourcesManager;
295
Jeff Sharkey6d515712012-09-20 16:06:08 -0700296 private static final class ProviderKey {
297 final String authority;
298 final int userId;
299
300 public ProviderKey(String authority, int userId) {
301 this.authority = authority;
302 this.userId = userId;
303 }
304
305 @Override
306 public boolean equals(Object o) {
307 if (o instanceof ProviderKey) {
308 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800309 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700310 }
311 return false;
312 }
313
314 @Override
315 public int hashCode() {
316 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
317 }
318 }
319
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700320 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700321 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
322 = new ArrayMap<ProviderKey, ProviderClientRecord>();
323 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
324 = new ArrayMap<IBinder, ProviderRefCount>();
325 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
326 = new ArrayMap<IBinder, ProviderClientRecord>();
327 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
328 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700330 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700331 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600332
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700333 final GcIdler mGcIdler = new GcIdler();
334 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700336 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700337
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800338 Bundle mCoreSettings = null;
339
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400340 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700342 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800344 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700345 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700347 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 Activity activity;
349 Window window;
350 Activity parent;
351 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700352 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 boolean paused;
354 boolean stopped;
355 boolean hideForNow;
356 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700357 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800358 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800359 // Used for consolidating configs before sending on to Activity.
360 private Configuration tmpConfig = new Configuration();
Andrii Kulian44607962017-03-16 11:06:24 -0700361 // Callback used for updating activity override config.
362 ViewRootImpl.ActivityConfigCallback configCallback;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700363 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Jeff Hao1b012d32014-08-20 10:35:34 -0700365 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400368 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700369 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
371 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800372 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373
374 boolean startsNotResumed;
375 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800376 int pendingConfigChanges;
377 boolean onlyLocalRequest;
378
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700379 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800380 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700381 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700383 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
384 // can be compared with other lifecycle operations.
385 int relaunchSeq = 0;
386
387 // Can only be accessed from the UI thread. This represents the latest processed message
388 // that is related to lifecycle events/
389 int lastProcessedSeq = 0;
390
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700391 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 parent = null;
393 embeddedID = null;
394 paused = false;
395 stopped = false;
396 hideForNow = false;
397 nextIdle = null;
Andrii Kulian44607962017-03-16 11:06:24 -0700398 configCallback = (Configuration overrideConfig, int newDisplayId) -> {
399 if (activity == null) {
400 throw new IllegalStateException(
401 "Received config update for non-existing activity");
402 }
403 activity.mMainThread.handleActivityConfigurationChanged(
404 new ActivityConfigChangeData(token, overrideConfig), newDisplayId);
405 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 }
407
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800408 public boolean isPreHoneycomb() {
409 if (activity != null) {
410 return activity.getApplicationInfo().targetSdkVersion
411 < android.os.Build.VERSION_CODES.HONEYCOMB;
412 }
413 return false;
414 }
415
Craig Mautnera0026042014-04-23 11:45:37 -0700416 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700417 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700418 }
419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700421 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 return "ActivityRecord{"
423 + Integer.toHexString(System.identityHashCode(this))
424 + " token=" + token + " " + (componentName == null
425 ? "no component name" : componentName.toShortString())
426 + "}";
427 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700428
429 public String getStateString() {
430 StringBuilder sb = new StringBuilder();
431 sb.append("ActivityClientRecord{");
432 sb.append("paused=").append(paused);
433 sb.append(", stopped=").append(stopped);
434 sb.append(", hideForNow=").append(hideForNow);
435 sb.append(", startsNotResumed=").append(startsNotResumed);
436 sb.append(", isForward=").append(isForward);
437 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
438 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
439 sb.append(", preserveWindow=").append(mPreserveWindow);
440 if (activity != null) {
441 sb.append(", Activity{");
442 sb.append("resumed=").append(activity.mResumed);
443 sb.append(", stopped=").append(activity.mStopped);
444 sb.append(", finished=").append(activity.isFinishing());
445 sb.append(", destroyed=").append(activity.isDestroyed());
446 sb.append(", startedActivity=").append(activity.mStartedActivity);
447 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
448 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
Andrii Kulian58178f22016-03-16 13:44:56 -0700449 sb.append("}");
450 }
451 sb.append("}");
452 return sb.toString();
453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 }
455
Wale Ogunwale1d646122015-04-24 14:45:14 -0700456 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700457 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 final IContentProvider mProvider;
459 final ContentProvider mLocalProvider;
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700460 final ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700462 ProviderClientRecord(String[] names, IContentProvider provider,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -0700463 ContentProvider localProvider, ContentProviderHolder holder) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700464 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 mProvider = provider;
466 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700467 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 }
469 }
470
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400471 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800472 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 IBinder token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700474 boolean andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 public String toString() {
Wale Ogunwale826c7062016-09-13 08:25:54 -0700476 return "NewIntentData{intents=" + intents + " token=" + token
477 + " andPause=" + andPause +"}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 }
479 }
480
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400481 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700482 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700483 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
484 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800485 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700486 this.intent = intent;
487 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 Intent intent;
490 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400491 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 public String toString() {
493 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700494 info.packageName + " resultCode=" + getResultCode()
495 + " resultData=" + getResultData() + " resultExtras="
496 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 }
498 }
499
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400500 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700501 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400502 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700503 int backupMode;
504 public String toString() {
505 return "CreateBackupAgentData{appInfo=" + appInfo
506 + " backupAgent=" + appInfo.backupAgentName
507 + " mode=" + backupMode + "}";
508 }
509 }
Bob Leee5408332009-09-04 18:31:17 -0700510
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400511 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 IBinder token;
513 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400514 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 Intent intent;
516 public String toString() {
517 return "CreateServiceData{token=" + token + " className="
518 + info.name + " packageName=" + info.packageName
519 + " intent=" + intent + "}";
520 }
521 }
522
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400523 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 IBinder token;
525 Intent intent;
526 boolean rebind;
527 public String toString() {
528 return "BindServiceData{token=" + token + " intent=" + intent + "}";
529 }
530 }
531
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400532 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700534 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700536 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 Intent args;
538 public String toString() {
539 return "ServiceArgsData{token=" + token + " startId=" + startId
540 + " args=" + args + "}";
541 }
542 }
543
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400544 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700545 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 String processName;
547 ApplicationInfo appInfo;
548 List<ProviderInfo> providers;
549 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 Bundle instrumentationArgs;
551 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800552 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400554 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700555 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700556 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700557 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400559 CompatibilityInfo compatInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700560 String buildSerial;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700561
562 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700563 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public String toString() {
566 return "AppBindData{appInfo=" + appInfo + "}";
567 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700568 }
569
570 static final class Profiler {
571 String profileFile;
572 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700573 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700574 boolean autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800575 boolean streamingOutput;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700576 boolean profiling;
577 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700578 public void setProfiler(ProfilerInfo profilerInfo) {
579 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700580 if (profiling) {
581 if (fd != null) {
582 try {
583 fd.close();
584 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700585 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700586 }
587 }
588 return;
589 }
590 if (profileFd != null) {
591 try {
592 profileFd.close();
593 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700594 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700595 }
596 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700597 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700598 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700599 samplingInterval = profilerInfo.samplingInterval;
600 autoStopProfiler = profilerInfo.autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800601 streamingOutput = profilerInfo.streamingOutput;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700602 }
603 public void startProfiling() {
604 if (profileFd == null || profiling) {
605 return;
606 }
607 try {
Andreas Gampeeff06392016-05-10 12:51:45 -0700608 int bufferSize = SystemProperties.getInt("debug.traceview-buffer-size-mb", 8);
Jeff Hao1b012d32014-08-20 10:35:34 -0700609 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
Shukang Zhou6ffd4f92017-01-25 16:07:57 -0800610 bufferSize * 1024 * 1024, 0, samplingInterval != 0, samplingInterval,
611 streamingOutput);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700612 profiling = true;
613 } catch (RuntimeException e) {
614 Slog.w(TAG, "Profiling failed on path " + profileFile);
615 try {
616 profileFd.close();
617 profileFd = null;
618 } catch (IOException e2) {
619 Slog.w(TAG, "Failure closing profile fd", e2);
620 }
621 }
622 }
623 public void stopProfiling() {
624 if (profiling) {
625 profiling = false;
626 Debug.stopMethodTracing();
627 if (profileFd != null) {
628 try {
629 profileFd.close();
630 } catch (IOException e) {
631 }
632 }
633 profileFd = null;
634 profileFile = null;
635 }
636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400639 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700640 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700641 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800642 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400646 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 IBinder token;
648 List<ResultInfo> results;
649 public String toString() {
650 return "ResultData{token=" + token + " results" + results + "}";
651 }
652 }
653
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400654 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800655 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 String what;
657 String who;
658 }
659
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400660 static final class DumpHeapData {
Christopher Ferris8d652f82017-04-11 16:29:18 -0700661 public boolean managed;
662 public boolean mallocInfo;
Makoto Onuki4556b7b2017-07-07 14:58:58 -0700663 public boolean runGc;
Andy McFadden824c5102010-07-09 16:26:57 -0700664 String path;
665 ParcelFileDescriptor fd;
666 }
667
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400668 static final class UpdateCompatibilityData {
669 String pkg;
670 CompatibilityInfo info;
671 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800672
Adam Skorydfc7fd72013-08-05 19:23:41 -0700673 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800674 IBinder activityToken;
675 IBinder requestToken;
676 int requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700677 int sessionId;
Svet Ganovfd31f852017-04-26 15:54:27 -0700678 int flags;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800679 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700680
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800681 static final class ActivityConfigChangeData {
682 final IBinder activityToken;
683 final Configuration overrideConfig;
684 public ActivityConfigChangeData(IBinder token, Configuration config) {
685 activityToken = token;
686 overrideConfig = config;
687 }
688 }
689
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700690 private class ApplicationThread extends IApplicationThread.Stub {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700691 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700692
Dianne Hackborna413dc02013-07-12 12:02:55 -0700693 private int mLastProcessState = -1;
694
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700695 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700696 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700697 if (mPendingConfiguration == null ||
698 mPendingConfiguration.isOtherSeqNewer(config)) {
699 mPendingConfiguration = config;
700 }
701 }
702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700705 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700706 int seq = getLifecycleSeq();
707 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
708 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700709 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
711 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700712 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
713 configChanges,
714 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716
717 public final void scheduleStopActivity(IBinder token, boolean showWindow,
718 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700719 int seq = getLifecycleSeq();
720 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
721 + " operation received seq: " + seq);
722 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700724 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
727 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700728 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
730 token);
731 }
732
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800733 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700734 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800735 }
736
Dianne Hackborna413dc02013-07-12 12:02:55 -0700737 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800738 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700739 int seq = getLifecycleSeq();
740 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
741 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700742 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700743 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745
746 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
747 ResultData res = new ResultData();
748 res.token = token;
749 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700750 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
753 // we use token to identify this activity without having to send the
754 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800755 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700756 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800757 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
758 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
759 int procState, Bundle state, PersistableBundle persistentState,
760 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
761 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700762
763 updateProcessState(procState, false);
764
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700765 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766
767 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700768 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800770 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700771 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400773 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700775 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776
777 r.pendingResults = pendingResults;
778 r.pendingIntents = pendingNewIntents;
779
780 r.startsNotResumed = notResumed;
781 r.isForward = isForward;
782
Jeff Hao1b012d32014-08-20 10:35:34 -0700783 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700784
Wale Ogunwale60454db2015-01-23 16:05:07 -0800785 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700786 updatePendingConfiguration(curConfig);
787
Jeff Brown9ef09972013-10-15 20:49:59 -0700788 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 }
790
Wale Ogunwale60454db2015-01-23 16:05:07 -0800791 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800793 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800794 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700795 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800796 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700797 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
Wale Ogunwale826c7062016-09-13 08:25:54 -0700800 public final void scheduleNewIntent(
801 List<ReferrerIntent> intents, IBinder token, boolean andPause) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 NewIntentData data = new NewIntentData();
803 data.intents = intents;
804 data.token = token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700805 data.andPause = andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806
Jeff Brown9ef09972013-10-15 20:49:59 -0700807 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
811 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700812 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 configChanges);
814 }
815
816 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400817 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700818 boolean sync, int sendingUser, int processState) {
819 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700820 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700821 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400823 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700824 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400827 public final void scheduleCreateBackupAgent(ApplicationInfo app,
828 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700829 CreateBackupAgentData d = new CreateBackupAgentData();
830 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400831 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700832 d.backupMode = backupMode;
833
Jeff Brown9ef09972013-10-15 20:49:59 -0700834 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700835 }
836
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400837 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
838 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700839 CreateBackupAgentData d = new CreateBackupAgentData();
840 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400841 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700842
Jeff Brown9ef09972013-10-15 20:49:59 -0700843 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700844 }
845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700847 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
848 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 CreateServiceData s = new CreateServiceData();
850 s.token = token;
851 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400852 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853
Jeff Brown9ef09972013-10-15 20:49:59 -0700854 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 }
856
857 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700858 boolean rebind, int processState) {
859 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 BindServiceData s = new BindServiceData();
861 s.token = token;
862 s.intent = intent;
863 s.rebind = rebind;
864
Amith Yamasani742a6712011-05-04 14:49:28 -0700865 if (DEBUG_SERVICE)
866 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
867 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700868 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
871 public final void scheduleUnbindService(IBinder token, Intent intent) {
872 BindServiceData s = new BindServiceData();
873 s.token = token;
874 s.intent = intent;
875
Jeff Brown9ef09972013-10-15 20:49:59 -0700876 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 }
878
Dianne Hackborn3f7c9f22017-04-04 15:36:33 -0700879 public final void scheduleServiceArgs(IBinder token, ParceledListSlice args) {
880 List<ServiceStartArgs> list = args.getList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881
Dianne Hackborn3f7c9f22017-04-04 15:36:33 -0700882 for (int i = 0; i < list.size(); i++) {
883 ServiceStartArgs ssa = list.get(i);
884 ServiceArgsData s = new ServiceArgsData();
885 s.token = token;
886 s.taskRemoved = ssa.taskRemoved;
887 s.startId = ssa.startId;
888 s.flags = ssa.flags;
889 s.args = ssa.args;
890
891 sendMessage(H.SERVICE_ARGS, s);
892 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894
895 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700896 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898
Jeff Hao1b012d32014-08-20 10:35:34 -0700899 public final void bindApplication(String processName, ApplicationInfo appInfo,
900 List<ProviderInfo> providers, ComponentName instrumentationName,
901 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
902 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800903 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700904 boolean enableBinderTracking, boolean trackAllocation,
905 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700906 CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
Svet Ganov37e43272016-09-09 16:01:32 -0700907 String buildSerial) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908
909 if (services != null) {
910 // Setup the service cache in the ServiceManager
911 ServiceManager.initServiceCache(services);
912 }
913
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800914 setCoreSettings(coreSettings);
915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 AppBindData data = new AppBindData();
917 data.processName = processName;
918 data.appInfo = appInfo;
919 data.providers = providers;
920 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 data.instrumentationArgs = instrumentationArgs;
922 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800923 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400925 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700926 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700927 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700928 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400930 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700931 data.initProfilerInfo = profilerInfo;
Svet Ganov37e43272016-09-09 16:01:32 -0700932 data.buildSerial = buildSerial;
Jeff Brown9ef09972013-10-15 20:49:59 -0700933 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
935
936 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700937 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 }
939
Christopher Tate5e1ab332009-09-01 20:32:49 -0700940 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700941 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700942 }
943
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700945 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700946 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 }
948
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +0100949 public void scheduleApplicationInfoChanged(ApplicationInfo ai) {
950 sendMessage(H.APPLICATION_INFO_CHANGED, ai);
951 }
952
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800953 public void updateTimeZone() {
954 TimeZone.setDefault(null);
955 }
956
Robert Greenwalt03595d02010-11-02 14:08:23 -0700957 public void clearDnsCache() {
958 // a non-standard API to get this to libcore
959 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400960 // Allow libcore to perform the necessary actions as it sees fit upon a network
961 // configuration change.
962 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700963 }
964
Jason Monk83520b92014-05-09 15:16:06 -0400965 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400966 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
967 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500968 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400969 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500970 } else {
971 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
972 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700973 }
974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 public void processInBackground() {
976 mH.removeMessages(H.GC_WHEN_IDLE);
977 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
978 }
979
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700980 public void dumpService(ParcelFileDescriptor pfd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700981 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700982 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700983 data.fd = pfd.dup();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700984 data.token = servicetoken;
985 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700986 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700987 } catch (IOException e) {
988 Slog.w(TAG, "dumpService failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -0700989 } finally {
990 IoUtils.closeQuietly(pfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 }
992 }
993
994 // This function exists to make sure all receiver dispatching is
995 // correctly ordered, since these are one-way calls and the binder driver
996 // applies transaction ordering per object for such calls.
997 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700998 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700999 boolean sticky, int sendingUser, int processState) throws RemoteException {
1000 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -07001001 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
1002 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
Bob Leee5408332009-09-04 18:31:17 -07001004
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001005 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -07001007 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
Wale Ogunwale60454db2015-01-23 16:05:07 -08001010 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001011 public void scheduleActivityConfigurationChanged(
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001012 IBinder token, Configuration overrideConfig) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001013 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001014 new ActivityConfigChangeData(token, overrideConfig));
1015 }
1016
1017 @Override
1018 public void scheduleActivityMovedToDisplay(IBinder token, int displayId,
1019 Configuration overrideConfig) {
1020 sendMessage(H.ACTIVITY_MOVED_TO_DISPLAY,
1021 new ActivityConfigChangeData(token, overrideConfig), displayId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 }
1023
Wale Ogunwalec2607b42015-02-07 16:16:59 -08001024 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -07001025 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
1026 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001027 }
1028
Makoto Onuki4556b7b2017-07-07 14:58:58 -07001029 @Override
Christopher Ferris8d652f82017-04-11 16:29:18 -07001030 public void dumpHeap(boolean managed, boolean mallocInfo, boolean runGc, String path,
1031 ParcelFileDescriptor fd) {
Andy McFadden824c5102010-07-09 16:26:57 -07001032 DumpHeapData dhd = new DumpHeapData();
Christopher Ferris8d652f82017-04-11 16:29:18 -07001033 dhd.managed = managed;
1034 dhd.mallocInfo = mallocInfo;
Makoto Onuki4556b7b2017-07-07 14:58:58 -07001035 dhd.runGc = runGc;
Andy McFadden824c5102010-07-09 16:26:57 -07001036 dhd.path = path;
1037 dhd.fd = fd;
Christopher Ferris8d652f82017-04-11 16:29:18 -07001038 sendMessage(H.DUMP_HEAP, dhd, 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -07001039 }
1040
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001041 public void attachAgent(String agent) {
1042 sendMessage(H.ATTACH_AGENT, agent);
1043 }
1044
Dianne Hackborn06de2ea2009-05-21 12:56:43 -07001045 public void setSchedulingGroup(int group) {
1046 // Note: do this immediately, since going into the foreground
1047 // should happen regardless of what pending work we have to do
1048 // and the activity manager will wait for us to report back that
1049 // we are done before sending us to the background.
1050 try {
1051 Process.setProcessGroup(Process.myPid(), group);
1052 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08001053 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -07001054 }
1055 }
Bob Leee5408332009-09-04 18:31:17 -07001056
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001057 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001058 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001059 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001060
1061 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001062 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001063 }
Dianne Hackborn625ac272010-09-17 18:29:22 -07001064
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001065 public void dumpActivity(ParcelFileDescriptor pfd, IBinder activitytoken,
Dianne Hackborn30d71892010-12-11 10:37:55 -08001066 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -07001067 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001068 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001069 data.fd = pfd.dup();
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001070 data.token = activitytoken;
1071 data.prefix = prefix;
1072 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001073 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001074 } catch (IOException e) {
1075 Slog.w(TAG, "dumpActivity failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001076 } finally {
1077 IoUtils.closeQuietly(pfd);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001078 }
1079 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07001080
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001081 public void dumpProvider(ParcelFileDescriptor pfd, IBinder providertoken,
Marco Nelissen18cb2872011-11-15 11:19:53 -08001082 String[] args) {
1083 DumpComponentInfo data = new DumpComponentInfo();
1084 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001085 data.fd = pfd.dup();
Marco Nelissen18cb2872011-11-15 11:19:53 -08001086 data.token = providertoken;
1087 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001088 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001089 } catch (IOException e) {
1090 Slog.w(TAG, "dumpProvider failed", e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001091 } finally {
1092 IoUtils.closeQuietly(pfd);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001093 }
1094 }
1095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001097 public void dumpMemInfo(ParcelFileDescriptor pfd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001098 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1099 boolean dumpUnreachable, String[] args) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001100 FileOutputStream fout = new FileOutputStream(pfd.getFileDescriptor());
Dianne Hackborn8c841092013-06-24 13:46:13 -07001101 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001102 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001103 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001104 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001105 pw.flush();
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001106 IoUtils.closeQuietly(pfd);
Chet Haase9c1e23b2011-03-24 10:51:31 -07001107 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001108 }
1109
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001110 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001111 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 long nativeMax = Debug.getNativeHeapSize() / 1024;
1113 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1114 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001117 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 long dalvikMax = runtime.totalMemory() / 1024;
1119 long dalvikFree = runtime.freeMemory() / 1024;
1120 long dalvikAllocated = dalvikMax - dalvikFree;
Richard Uhler2c036192016-09-14 09:48:31 +01001121
1122 Class[] classesToCount = new Class[] {
1123 ContextImpl.class,
1124 Activity.class,
1125 WebView.class,
1126 OpenSSLSocketImpl.class
1127 };
1128 long[] instanceCounts = VMDebug.countInstancesOfClasses(classesToCount, true);
1129 long appContextInstanceCount = instanceCounts[0];
1130 long activityInstanceCount = instanceCounts[1];
1131 long webviewInstanceCount = instanceCounts[2];
1132 long openSslSocketCount = instanceCounts[3];
1133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001135 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 int globalAssetCount = AssetManager.getGlobalAssetCount();
1137 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1138 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1139 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1140 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001141 long parcelSize = Parcel.getGlobalAllocSize();
1142 long parcelCount = Parcel.getGlobalAllocCount();
Vasu Noric3849202010-03-09 10:47:25 -08001143 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001144
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001145 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1146 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001147 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1148 nativeMax, nativeAllocated, nativeFree,
1149 dalvikMax, dalvikAllocated, dalvikFree);
1150
Dianne Hackbornb437e092011-08-05 17:50:29 -07001151 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 // NOTE: if you change anything significant below, also consider changing
1153 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 // Object counts
1156 pw.print(viewInstanceCount); pw.print(',');
1157 pw.print(viewRootInstanceCount); pw.print(',');
1158 pw.print(appContextInstanceCount); pw.print(',');
1159 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 pw.print(globalAssetCount); pw.print(',');
1162 pw.print(globalAssetManagerCount); pw.print(',');
1163 pw.print(binderLocalObjectCount); pw.print(',');
1164 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 pw.print(binderDeathObjectCount); pw.print(',');
1167 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001170 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001171 pw.print(stats.memoryUsed / 1024); pw.print(',');
1172 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001173 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001174 for (int i = 0; i < stats.dbStats.size(); i++) {
1175 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001176 pw.print(','); pw.print(dbStats.dbName);
1177 pw.print(','); pw.print(dbStats.pageSize);
1178 pw.print(','); pw.print(dbStats.dbSize);
1179 pw.print(','); pw.print(dbStats.lookaside);
1180 pw.print(','); pw.print(dbStats.cache);
1181 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001182 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001183 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001184
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001185 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 }
Bob Leee5408332009-09-04 18:31:17 -07001187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 pw.println(" ");
1189 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001190 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 viewRootInstanceCount);
1192
1193 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1194 "Activities:", activityInstanceCount);
1195
1196 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1197 "AssetManagers:", globalAssetManagerCount);
1198
1199 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1200 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001201 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1202 "Parcel count:", parcelCount);
1203 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1204 "OpenSSL Sockets:", openSslSocketCount);
Richard Uhler2c036192016-09-14 09:48:31 +01001205 printRow(pw, ONE_COUNT_COLUMN, "WebViews:", webviewInstanceCount);
Bob Leee5408332009-09-04 18:31:17 -07001206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 // SQLite mem info
1208 pw.println(" ");
1209 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001210 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1211 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1212 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001213 pw.println(" ");
1214 int N = stats.dbStats.size();
1215 if (N > 0) {
1216 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001217 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1218 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001219 for (int i = 0; i < N; i++) {
1220 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001221 printRow(pw, DB_INFO_FORMAT,
1222 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1223 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1224 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1225 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001226 }
1227 }
Bob Leee5408332009-09-04 18:31:17 -07001228
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001229 // Asset details.
1230 String assetAlloc = AssetManager.getAssetAllocations();
1231 if (assetAlloc != null) {
1232 pw.println(" ");
1233 pw.println(" Asset Allocations");
1234 pw.print(assetAlloc);
1235 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001236
1237 // Unreachable native memory
1238 if (dumpUnreachable) {
1239 boolean showContents = ((mBoundApplication != null)
1240 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1241 || android.os.Build.IS_DEBUGGABLE;
1242 pw.println(" ");
1243 pw.println(" Unreachable memory");
1244 pw.print(Debug.getUnreachableMemory(100, showContents));
1245 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001246 }
1247
1248 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001249 public void dumpGfxInfo(ParcelFileDescriptor pfd, String[] args) {
Chris Craikfc294242016-12-13 18:10:46 -08001250 nDumpGraphicsInfo(pfd.getFileDescriptor());
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001251 WindowManagerGlobal.getInstance().dumpGfxInfo(pfd.getFileDescriptor(), args);
1252 IoUtils.closeQuietly(pfd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001255 private void dumpDatabaseInfo(ParcelFileDescriptor pfd, String[] args) {
1256 PrintWriter pw = new FastPrintWriter(
1257 new FileOutputStream(pfd.getFileDescriptor()));
Jeff Brown6754ba22011-12-14 20:20:01 -08001258 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1259 SQLiteDebug.dump(printer, args);
1260 pw.flush();
1261 }
1262
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001263 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001264 public void dumpDbInfo(final ParcelFileDescriptor pfd, final String[] args) {
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001265 if (mSystemThread) {
Felipe Leme29de4922016-06-07 16:14:07 -07001266 // Ensure this invocation is asynchronous to prevent writer waiting if buffer cannot
1267 // be consumed. But it must duplicate the file descriptor first, since caller might
1268 // be closing it.
1269 final ParcelFileDescriptor dup;
1270 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001271 dup = pfd.dup();
Felipe Leme29de4922016-06-07 16:14:07 -07001272 } catch (IOException e) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001273 Log.w(TAG, "Could not dup FD " + pfd.getFileDescriptor().getInt$());
Felipe Leme29de4922016-06-07 16:14:07 -07001274 return;
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001275 } finally {
1276 IoUtils.closeQuietly(pfd);
Felipe Leme29de4922016-06-07 16:14:07 -07001277 }
1278
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001279 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1280 @Override
1281 public void run() {
Felipe Lemec74972f2016-06-08 09:12:37 -07001282 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001283 dumpDatabaseInfo(dup, args);
Felipe Lemec74972f2016-06-08 09:12:37 -07001284 } finally {
1285 IoUtils.closeQuietly(dup);
1286 }
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001287 }
1288 });
1289 } else {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001290 dumpDatabaseInfo(pfd, args);
1291 IoUtils.closeQuietly(pfd);
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001292 }
1293 }
1294
1295 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001296 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001297 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001298 }
1299
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001300 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001301 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Svet Ganovfd31f852017-04-26 15:54:27 -07001302 int requestType, int sessionId, int flags) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001303 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001304 cmd.activityToken = activityToken;
1305 cmd.requestToken = requestToken;
1306 cmd.requestType = requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -07001307 cmd.sessionId = sessionId;
Svet Ganovfd31f852017-04-26 15:54:27 -07001308 cmd.flags = flags;
Jeff Brown9ef09972013-10-15 20:49:59 -07001309 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001310 }
1311
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001312 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001313 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001314 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001315
1316 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1317 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1318 ucd.pkg = pkg;
1319 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001320 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001321 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001322
1323 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001324 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001325 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001326
Craig Mautner5eda9b32013-07-02 11:58:16 -07001327 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001328 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001329 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001330
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001331 public void scheduleOnNewActivityOptions(IBinder token, Bundle options) {
Craig Mautnereb8abf72014-07-02 15:04:09 -07001332 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001333 new Pair<IBinder, ActivityOptions>(token, ActivityOptions.fromBundle(options)));
Craig Mautnereb8abf72014-07-02 15:04:09 -07001334 }
1335
Dianne Hackborna413dc02013-07-12 12:02:55 -07001336 public void setProcessState(int state) {
1337 updateProcessState(state, true);
1338 }
1339
1340 public void updateProcessState(int processState, boolean fromIpc) {
1341 synchronized (this) {
1342 if (mLastProcessState != processState) {
1343 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001344 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1345 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1346 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1347 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1348 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1349 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1350 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1351 }
1352 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001353 if (false) {
1354 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1355 + (fromIpc ? " (from ipc": ""));
1356 }
1357 }
1358 }
1359 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001360
Sudheer Shanka84a48952017-03-08 18:19:01 -08001361 /**
1362 * Updates {@link #mNetworkBlockSeq}. This is used by ActivityManagerService to inform
1363 * the main thread that it needs to wait for the network rules to get updated before
1364 * launching an activity.
1365 */
1366 @Override
1367 public void setNetworkBlockSeq(long procStateSeq) {
1368 synchronized (mNetworkPolicyLock) {
1369 mNetworkBlockSeq = procStateSeq;
1370 }
1371 }
1372
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001373 @Override
1374 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001375 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001376 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001377
1378 @Override
Neil Fullerb7146fe2016-11-15 16:52:15 +00001379 public final void updateTimePrefs(int timeFormatPreference) {
1380 final Boolean timeFormatPreferenceBool;
1381 // For convenience we are using the Intent extra values.
1382 if (timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_12_HOUR) {
1383 timeFormatPreferenceBool = Boolean.FALSE;
1384 } else if (timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_24_HOUR) {
1385 timeFormatPreferenceBool = Boolean.TRUE;
1386 } else {
1387 // timeFormatPreference == Intent.EXTRA_TIME_PREF_VALUE_USE_LOCALE_DEFAULT
1388 // (or unknown).
1389 timeFormatPreferenceBool = null;
1390 }
1391 DateFormat.set24HourTimePref(timeFormatPreferenceBool);
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001392 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001393
1394 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001395 public void scheduleEnterAnimationComplete(IBinder token) {
1396 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1397 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001398
1399 @Override
1400 public void notifyCleartextNetwork(byte[] firstPacket) {
1401 if (StrictMode.vmCleartextNetworkEnabled()) {
1402 StrictMode.onCleartextNetworkDetected(firstPacket);
1403 }
1404 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001405
1406 @Override
1407 public void startBinderTracking() {
1408 sendMessage(H.START_BINDER_TRACKING, null);
1409 }
1410
1411 @Override
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001412 public void stopBinderTrackingAndDump(ParcelFileDescriptor pfd) {
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001413 try {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001414 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, pfd.dup());
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001415 } catch (IOException e) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07001416 } finally {
1417 IoUtils.closeQuietly(pfd);
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001418 }
1419 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001420
1421 @Override
Winson Chung5af42fc2017-03-24 17:11:33 -07001422 public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode,
1423 Configuration overrideConfig) throws RemoteException {
1424 SomeArgs args = SomeArgs.obtain();
1425 args.arg1 = token;
1426 args.arg2 = overrideConfig;
1427 args.argi1 = isInMultiWindowMode ? 1 : 0;
1428 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, args);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001429 }
1430
1431 @Override
Winson Chung5af42fc2017-03-24 17:11:33 -07001432 public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode,
1433 Configuration overrideConfig) throws RemoteException {
1434 SomeArgs args = SomeArgs.obtain();
1435 args.arg1 = token;
1436 args.arg2 = overrideConfig;
1437 args.argi1 = isInPipMode ? 1 : 0;
1438 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, args);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001439 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001440
1441 @Override
1442 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1443 IVoiceInteractor voiceInteractor) throws RemoteException {
1444 SomeArgs args = SomeArgs.obtain();
1445 args.arg1 = token;
1446 args.arg2 = voiceInteractor;
1447 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1448 }
Chad Brubakerc72875b2016-04-27 16:35:11 -07001449
1450 @Override
1451 public void handleTrustStorageUpdate() {
1452 NetworkSecurityPolicy.getInstance().handleTrustStorageUpdate();
1453 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454 }
1455
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001456 private int getLifecycleSeq() {
1457 synchronized (mResourcesManager) {
1458 return mLifecycleSeq++;
1459 }
1460 }
1461
Romain Guy65b345f2011-07-27 18:51:50 -07001462 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 public static final int LAUNCH_ACTIVITY = 100;
1464 public static final int PAUSE_ACTIVITY = 101;
1465 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1466 public static final int STOP_ACTIVITY_SHOW = 103;
1467 public static final int STOP_ACTIVITY_HIDE = 104;
1468 public static final int SHOW_WINDOW = 105;
1469 public static final int HIDE_WINDOW = 106;
1470 public static final int RESUME_ACTIVITY = 107;
1471 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001472 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 public static final int BIND_APPLICATION = 110;
1474 public static final int EXIT_APPLICATION = 111;
1475 public static final int NEW_INTENT = 112;
1476 public static final int RECEIVER = 113;
1477 public static final int CREATE_SERVICE = 114;
1478 public static final int SERVICE_ARGS = 115;
1479 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 public static final int CONFIGURATION_CHANGED = 118;
1482 public static final int CLEAN_UP_CONTEXT = 119;
1483 public static final int GC_WHEN_IDLE = 120;
1484 public static final int BIND_SERVICE = 121;
1485 public static final int UNBIND_SERVICE = 122;
1486 public static final int DUMP_SERVICE = 123;
1487 public static final int LOW_MEMORY = 124;
1488 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1489 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001490 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001491 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001492 public static final int DESTROY_BACKUP_AGENT = 129;
1493 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001494 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001495 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001496 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001497 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001498 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001499 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001500 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001501 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001502 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001503 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001504 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001505 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001506 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001507 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001508 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001509 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
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";
Craig Mautner8746a472014-07-24 15:12:54 -07001570 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Andrii Kulian933076d2016-03-29 17:04:42 -07001571 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1572 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001573 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001574 case ATTACH_AGENT: return "ATTACH_AGENT";
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001575 case APPLICATION_INFO_CHANGED: return "APPLICATION_INFO_CHANGED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001576 }
1577 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001578 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 }
1580 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001581 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 switch (msg.what) {
1583 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001584 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001585 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586
1587 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001588 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001589 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001590 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 } break;
1592 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001593 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001594 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001595 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001596 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001598 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001599 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001600 SomeArgs args = (SomeArgs) msg.obj;
1601 handlePauseActivity((IBinder) args.arg1, false,
1602 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1603 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001604 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001605 } break;
1606 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001607 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001608 SomeArgs args = (SomeArgs) msg.obj;
1609 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1610 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001611 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001612 } break;
1613 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001614 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001615 SomeArgs args = (SomeArgs) msg.obj;
1616 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001617 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001618 } break;
1619 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001620 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001621 SomeArgs args = (SomeArgs) msg.obj;
1622 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001623 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001624 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001626 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001628 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 break;
1630 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001631 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001633 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 break;
1635 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001636 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001637 SomeArgs args = (SomeArgs) msg.obj;
1638 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001639 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001640 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 break;
1642 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001643 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001645 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 break;
1647 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001648 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1650 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001651 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 break;
1653 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001654 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 AppBindData data = (AppBindData)msg.obj;
1656 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001657 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 break;
1659 case EXIT_APPLICATION:
1660 if (mInitialApplication != null) {
1661 mInitialApplication.onTerminate();
1662 }
1663 Looper.myLooper().quit();
1664 break;
1665 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001666 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001668 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 break;
1670 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001671 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 handleReceiver((ReceiverData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001673 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 break;
1675 case CREATE_SERVICE:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001676 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001678 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 break;
1680 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001681 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001683 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 break;
1685 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001686 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001688 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 break;
1690 case SERVICE_ARGS:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001691 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceStart: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001693 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 break;
1695 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001696 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697 handleStopService((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001698 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001701 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001702 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Chong Zhang4951f9d2016-06-23 13:15:08 -07001703 mUpdatingSystemConfig = true;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001704 try {
1705 handleConfigurationChanged((Configuration) msg.obj, null);
1706 } finally {
1707 mUpdatingSystemConfig = false;
1708 }
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001709 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001710 break;
1711 case CLEAN_UP_CONTEXT:
1712 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1713 cci.context.performFinalCleanup(cci.who, cci.what);
1714 break;
1715 case GC_WHEN_IDLE:
1716 scheduleGcIdler();
1717 break;
1718 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001719 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 break;
1721 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001722 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001724 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 break;
1726 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001727 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001728 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08001729 INVALID_DISPLAY);
1730 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
1731 break;
1732 case ACTIVITY_MOVED_TO_DISPLAY:
1733 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityMovedToDisplay");
1734 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1735 msg.arg1 /* displayId */);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001736 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001738 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001739 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001740 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001741 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001742 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001743 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001744 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001745 break;
1746 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001747 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001748 handleDestroyBackupAgent((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;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001751 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001752 Process.killProcess(Process.myPid());
1753 break;
1754 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001755 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001756 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001757 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001758 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001759 case ENABLE_JIT:
1760 ensureJitEnabled();
1761 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001762 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001763 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001764 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001765 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001766 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001767 case SCHEDULE_CRASH:
1768 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001769 case DUMP_HEAP:
Christopher Ferris8d652f82017-04-11 16:29:18 -07001770 handleDumpHeap((DumpHeapData) msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001771 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001772 case DUMP_ACTIVITY:
1773 handleDumpActivity((DumpComponentInfo)msg.obj);
1774 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001775 case DUMP_PROVIDER:
1776 handleDumpProvider((DumpComponentInfo)msg.obj);
1777 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001778 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001779 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001780 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001781 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001782 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001783 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001784 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001785 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001786 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001787 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001788 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1789 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001790 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001791 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001792 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001793 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001794 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001795 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001796 case UNSTABLE_PROVIDER_DIED:
1797 handleUnstableProviderDied((IBinder)msg.obj, false);
1798 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001799 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1800 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001801 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001802 case TRANSLUCENT_CONVERSION_COMPLETE:
1803 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1804 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001805 case INSTALL_PROVIDER:
1806 handleInstallProvider((ProviderInfo) msg.obj);
1807 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001808 case ON_NEW_ACTIVITY_OPTIONS:
1809 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1810 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001811 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001812 case ENTER_ANIMATION_COMPLETE:
1813 handleEnterAnimationComplete((IBinder) msg.obj);
1814 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001815 case START_BINDER_TRACKING:
1816 handleStartBinderTracking();
1817 break;
1818 case STOP_BINDER_TRACKING_AND_DUMP:
1819 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1820 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001821 case MULTI_WINDOW_MODE_CHANGED:
Winson Chung5af42fc2017-03-24 17:11:33 -07001822 handleMultiWindowModeChanged((IBinder) ((SomeArgs) msg.obj).arg1,
1823 ((SomeArgs) msg.obj).argi1 == 1,
1824 (Configuration) ((SomeArgs) msg.obj).arg2);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001825 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001826 case PICTURE_IN_PICTURE_MODE_CHANGED:
Winson Chung5af42fc2017-03-24 17:11:33 -07001827 handlePictureInPictureModeChanged((IBinder) ((SomeArgs) msg.obj).arg1,
1828 ((SomeArgs) msg.obj).argi1 == 1,
1829 (Configuration) ((SomeArgs) msg.obj).arg2);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001830 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001831 case LOCAL_VOICE_INTERACTION_STARTED:
1832 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1833 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
Amith Yamasani61019112017-01-10 15:05:00 -08001834 break;
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07001835 case ATTACH_AGENT:
1836 handleAttachAgent((String) msg.obj);
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001837 break;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01001838 case APPLICATION_INFO_CHANGED:
1839 mUpdatingSystemConfig = true;
1840 try {
1841 handleApplicationInfoChanged((ApplicationInfo) msg.obj);
1842 } finally {
1843 mUpdatingSystemConfig = false;
1844 }
1845 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001847 Object obj = msg.obj;
1848 if (obj instanceof SomeArgs) {
1849 ((SomeArgs) obj).recycle();
1850 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001851 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853 }
1854
Romain Guy65b345f2011-07-27 18:51:50 -07001855 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001856 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001858 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001859 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001860 if (mBoundApplication != null && mProfiler.profileFd != null
1861 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001862 stopProfiling = true;
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 if (a != null) {
1865 mNewActivities = null;
Sudheer Shankadc589ac2016-11-10 15:30:17 -08001866 IActivityManager am = ActivityManager.getService();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001867 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001869 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 TAG, "Reporting idle of " + a +
1871 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001872 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 if (a.activity != null && !a.activity.mFinished) {
1874 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001875 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001876 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001878 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 }
1880 }
1881 prev = a;
1882 a = a.nextIdle;
1883 prev.nextIdle = null;
1884 } while (a != null);
1885 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001886 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001887 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001888 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001889 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 return false;
1891 }
1892 }
1893
1894 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001895 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001896 public final boolean queueIdle() {
1897 doGcIfNeeded();
1898 return false;
1899 }
1900 }
1901
Romain Guy65b345f2011-07-27 18:51:50 -07001902 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001903 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001906 public static boolean isSystem() {
1907 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1908 }
1909
Svetoslavfbf0eca2015-05-01 16:52:41 -07001910 public static String currentOpPackageName() {
1911 ActivityThread am = currentActivityThread();
1912 return (am != null && am.getApplication() != null)
1913 ? am.getApplication().getOpPackageName() : null;
1914 }
1915
Romain Guy65b345f2011-07-27 18:51:50 -07001916 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001917 ActivityThread am = currentActivityThread();
1918 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001919 ? am.mBoundApplication.appInfo.packageName : null;
1920 }
1921
1922 public static String currentProcessName() {
1923 ActivityThread am = currentActivityThread();
1924 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001925 ? am.mBoundApplication.processName : null;
1926 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927
Romain Guy65b345f2011-07-27 18:51:50 -07001928 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001929 ActivityThread am = currentActivityThread();
1930 return am != null ? am.mInitialApplication : null;
1931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001933 public static IPackageManager getPackageManager() {
1934 if (sPackageManager != null) {
1935 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1936 return sPackageManager;
1937 }
1938 IBinder b = ServiceManager.getService("package");
1939 //Slog.v("PackageManager", "default service binder = " + b);
1940 sPackageManager = IPackageManager.Stub.asInterface(b);
1941 //Slog.v("PackageManager", "default service = " + sPackageManager);
1942 return sPackageManager;
1943 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944
Romain Guy65b345f2011-07-27 18:51:50 -07001945 private Configuration mMainThreadConfig = new Configuration();
Amith Yamasani4f128e42016-05-10 11:44:12 -07001946
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001947 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1948 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001949 if (config == null) {
1950 return null;
1951 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001952 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001953 mMainThreadConfig.setTo(config);
1954 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001955 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001956 }
1957 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001960 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001961 * Creates the top level resources for the given package. Will return an existing
1962 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001963 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001964 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001965 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1966 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1967 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001968 }
1969
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001970 final Handler getHandler() {
1971 return mH;
1972 }
1973
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001974 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1975 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001976 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1977 }
1978
1979 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1980 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001981 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001982 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001983 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001984 if (differentUser) {
1985 // Caching not supported across users
1986 ref = null;
1987 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001988 ref = mPackages.get(packageName);
1989 } else {
1990 ref = mResourcePackages.get(packageName);
1991 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001992
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001993 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001994 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001995 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1996 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 if (packageInfo != null && (packageInfo.mResources == null
1998 || packageInfo.mResources.getAssets().isUpToDate())) {
1999 if (packageInfo.isSecurityViolation()
2000 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
2001 throw new SecurityException(
2002 "Requesting code from " + packageName
2003 + " to be run in process "
2004 + mBoundApplication.processName
2005 + "/" + mBoundApplication.appInfo.uid);
2006 }
2007 return packageInfo;
2008 }
2009 }
2010
2011 ApplicationInfo ai = null;
2012 try {
2013 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07002014 PackageManager.GET_SHARED_LIBRARY_FILES
2015 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
2016 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002018 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020
2021 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002022 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
2024
2025 return null;
2026 }
2027
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002028 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
2029 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
2031 boolean securityViolation = includeCode && ai.uid != 0
2032 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002033 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07002034 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002035 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 if ((flags&(Context.CONTEXT_INCLUDE_CODE
2037 |Context.CONTEXT_IGNORE_SECURITY))
2038 == Context.CONTEXT_INCLUDE_CODE) {
2039 if (securityViolation) {
2040 String msg = "Requesting code from " + ai.packageName
2041 + " (with uid " + ai.uid + ")";
2042 if (mBoundApplication != null) {
2043 msg = msg + " to be run in process "
2044 + mBoundApplication.processName + " (with uid "
2045 + mBoundApplication.appInfo.uid + ")";
2046 }
2047 throw new SecurityException(msg);
2048 }
2049 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002050 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
2051 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002054 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
2055 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002056 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 }
2058
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002059 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07002060 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002061 WeakReference<LoadedApk> ref;
2062 if (includeCode) {
2063 ref = mPackages.get(packageName);
2064 } else {
2065 ref = mResourcePackages.get(packageName);
2066 }
2067 return ref != null ? ref.get() : null;
2068 }
2069 }
2070
Romain Guy65b345f2011-07-27 18:51:50 -07002071 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002072 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
2073 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002074 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07002075 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002076 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002077 if (differentUser) {
2078 // Caching not supported across users
2079 ref = null;
2080 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081 ref = mPackages.get(aInfo.packageName);
2082 } else {
2083 ref = mResourcePackages.get(aInfo.packageName);
2084 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002085
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002086 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 if (packageInfo == null || (packageInfo.mResources != null
2088 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002089 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 : "Loading resource-only package ") + aInfo.packageName
2091 + " (in " + (mBoundApplication != null
2092 ? mBoundApplication.processName : null)
2093 + ")");
2094 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07002095 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07002097 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00002098
2099 if (mSystemThread && "android".equals(aInfo.packageName)) {
2100 packageInfo.installSystemApplicationInfo(aInfo,
2101 getSystemContext().mPackageInfo.getClassLoader());
2102 }
2103
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002104 if (differentUser) {
2105 // Caching not supported across users
2106 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002108 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002109 } else {
2110 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002111 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 }
2113 }
2114 return packageInfo;
2115 }
2116 }
2117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07002119 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 }
2121
2122 public ApplicationThread getApplicationThread()
2123 {
2124 return mAppThread;
2125 }
2126
2127 public Instrumentation getInstrumentation()
2128 {
2129 return mInstrumentation;
2130 }
2131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002133 return mProfiler != null && mProfiler.profileFile != null
2134 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 }
2136
2137 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002138 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002139 }
2140
2141 public Looper getLooper() {
2142 return mLooper;
2143 }
2144
2145 public Application getApplication() {
2146 return mInitialApplication;
2147 }
Bob Leee5408332009-09-04 18:31:17 -07002148
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002149 public String getProcessName() {
2150 return mBoundApplication.processName;
2151 }
Bob Leee5408332009-09-04 18:31:17 -07002152
Dianne Hackborn21556372010-02-04 16:34:40 -08002153 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 synchronized (this) {
2155 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002156 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002158 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 }
2161
Adam Lesinskia82b6262017-03-21 16:56:17 -07002162 public ContextImpl getSystemUiContext() {
2163 synchronized (this) {
2164 if (mSystemUiContext == null) {
Adam Lesinski6f1a9172017-04-10 16:35:19 -07002165 mSystemUiContext = ContextImpl.createSystemUiContext(getSystemContext());
Adam Lesinskia82b6262017-03-21 16:56:17 -07002166 }
2167 return mSystemUiContext;
2168 }
2169 }
2170
Narayan Kamath29564cd2014-08-07 10:57:40 +01002171 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002172 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002173 getSystemContext().installSystemApplicationInfo(info, classLoader);
Adam Lesinskia82b6262017-03-21 16:56:17 -07002174 getSystemUiContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002175
2176 // give ourselves a default profiler
2177 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002178 }
2179 }
2180
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002181 void ensureJitEnabled() {
2182 if (!mJitEnabled) {
2183 mJitEnabled = true;
2184 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2185 }
2186 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 void scheduleGcIdler() {
2189 if (!mGcIdlerScheduled) {
2190 mGcIdlerScheduled = true;
2191 Looper.myQueue().addIdleHandler(mGcIdler);
2192 }
2193 mH.removeMessages(H.GC_WHEN_IDLE);
2194 }
2195
2196 void unscheduleGcIdler() {
2197 if (mGcIdlerScheduled) {
2198 mGcIdlerScheduled = false;
2199 Looper.myQueue().removeIdleHandler(mGcIdler);
2200 }
2201 mH.removeMessages(H.GC_WHEN_IDLE);
2202 }
2203
2204 void doGcIfNeeded() {
2205 mGcIdlerScheduled = false;
2206 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002207 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 // + "m now=" + now);
2209 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002210 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002211 BinderInternal.forceGc("bg");
2212 }
2213 }
2214
Dianne Hackborne77187d2013-10-25 16:32:41 -07002215 private static final String HEAP_FULL_COLUMN
2216 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2217 private static final String HEAP_COLUMN
2218 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002219 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2220 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2221 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002222
2223 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002224 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002225
2226 static void printRow(PrintWriter pw, String format, Object...objs) {
2227 pw.println(String.format(format, objs));
2228 }
2229
2230 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002231 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2232 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002233 long nativeMax, long nativeAllocated, long nativeFree,
2234 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2235
2236 // For checkin, we print one long comma-separated list of values
2237 if (checkin) {
2238 // NOTE: if you change anything significant below, also consider changing
2239 // ACTIVITY_THREAD_CHECKIN_VERSION.
2240
2241 // Header
2242 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2243 pw.print(pid); pw.print(',');
2244 pw.print(processName); pw.print(',');
2245
2246 // Heap info - max
2247 pw.print(nativeMax); pw.print(',');
2248 pw.print(dalvikMax); pw.print(',');
2249 pw.print("N/A,");
2250 pw.print(nativeMax + dalvikMax); pw.print(',');
2251
2252 // Heap info - allocated
2253 pw.print(nativeAllocated); pw.print(',');
2254 pw.print(dalvikAllocated); pw.print(',');
2255 pw.print("N/A,");
2256 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2257
2258 // Heap info - free
2259 pw.print(nativeFree); pw.print(',');
2260 pw.print(dalvikFree); pw.print(',');
2261 pw.print("N/A,");
2262 pw.print(nativeFree + dalvikFree); pw.print(',');
2263
2264 // Heap info - proportional set size
2265 pw.print(memInfo.nativePss); pw.print(',');
2266 pw.print(memInfo.dalvikPss); pw.print(',');
2267 pw.print(memInfo.otherPss); pw.print(',');
2268 pw.print(memInfo.getTotalPss()); pw.print(',');
2269
2270 // Heap info - swappable set size
2271 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2272 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2273 pw.print(memInfo.otherSwappablePss); pw.print(',');
2274 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2275
2276 // Heap info - shared dirty
2277 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2278 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2279 pw.print(memInfo.otherSharedDirty); pw.print(',');
2280 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2281
2282 // Heap info - shared clean
2283 pw.print(memInfo.nativeSharedClean); pw.print(',');
2284 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2285 pw.print(memInfo.otherSharedClean); pw.print(',');
2286 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2287
2288 // Heap info - private Dirty
2289 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2290 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2291 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2292 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2293
2294 // Heap info - private Clean
2295 pw.print(memInfo.nativePrivateClean); pw.print(',');
2296 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2297 pw.print(memInfo.otherPrivateClean); pw.print(',');
2298 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2299
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002300 // Heap info - swapped out
2301 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2302 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2303 pw.print(memInfo.otherSwappedOut); pw.print(',');
2304 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2305
2306 // Heap info - swapped out pss
2307 if (memInfo.hasSwappedOutPss) {
2308 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2309 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2310 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2311 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2312 } else {
2313 pw.print("N/A,");
2314 pw.print("N/A,");
2315 pw.print("N/A,");
2316 pw.print("N/A,");
2317 }
2318
Dianne Hackborne77187d2013-10-25 16:32:41 -07002319 // Heap info - other areas
2320 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2321 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2322 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2323 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2324 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2325 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2326 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2327 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002328 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2329 if (memInfo.hasSwappedOutPss) {
2330 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2331 } else {
2332 pw.print("N/A,");
2333 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002334 }
2335 return;
2336 }
2337
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002338 if (!dumpSummaryOnly) {
2339 if (dumpFullInfo) {
2340 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002341 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2342 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002343 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002344 "Clean", "Clean", "Dirty",
2345 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002346 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2347 "------", "------", "------", "------", "------", "------");
2348 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2349 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2350 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002351 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
Richard Uhler91702eb32017-06-23 16:54:25 +01002352 memInfo.nativeSwappedOutPss : memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002353 nativeMax, nativeAllocated, nativeFree);
2354 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2355 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2356 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002357 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
Richard Uhler91702eb32017-06-23 16:54:25 +01002358 memInfo.dalvikSwappedOutPss : memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002359 dalvikMax, dalvikAllocated, dalvikFree);
2360 } else {
2361 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002362 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2363 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002364 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2365 "Clean", "Dirty", "Size", "Alloc", "Free");
2366 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2367 "------", "------", "------", "------", "------");
2368 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2369 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002370 memInfo.nativePrivateClean,
2371 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2372 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002373 nativeMax, nativeAllocated, nativeFree);
2374 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2375 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002376 memInfo.dalvikPrivateClean,
2377 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2378 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002379 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002380 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002381
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002382 int otherPss = memInfo.otherPss;
2383 int otherSwappablePss = memInfo.otherSwappablePss;
2384 int otherSharedDirty = memInfo.otherSharedDirty;
2385 int otherPrivateDirty = memInfo.otherPrivateDirty;
2386 int otherSharedClean = memInfo.otherSharedClean;
2387 int otherPrivateClean = memInfo.otherPrivateClean;
2388 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002389 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002390
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002391 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002392 final int myPss = memInfo.getOtherPss(i);
2393 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2394 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2395 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2396 final int mySharedClean = memInfo.getOtherSharedClean(i);
2397 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2398 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002399 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002400 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002401 || mySharedClean != 0 || myPrivateClean != 0
2402 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002403 if (dumpFullInfo) {
2404 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2405 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002406 mySharedClean, myPrivateClean,
2407 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2408 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002409 } else {
2410 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2411 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002412 myPrivateClean,
2413 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2414 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002415 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002416 otherPss -= myPss;
2417 otherSwappablePss -= mySwappablePss;
2418 otherSharedDirty -= mySharedDirty;
2419 otherPrivateDirty -= myPrivateDirty;
2420 otherSharedClean -= mySharedClean;
2421 otherPrivateClean -= myPrivateClean;
2422 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002423 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002424 }
2425 }
2426
2427 if (dumpFullInfo) {
2428 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2429 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002430 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2431 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002432 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2433 memInfo.getTotalSwappablePss(),
2434 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2435 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002436 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2437 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002438 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2439 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002440 } else {
2441 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002442 otherPrivateDirty, otherPrivateClean,
2443 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002444 "", "", "");
2445 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2446 memInfo.getTotalPrivateDirty(),
2447 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002448 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002449 memInfo.getTotalSwappedOut(),
2450 nativeMax+dalvikMax,
2451 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2452 }
2453
2454 if (dumpDalvik) {
2455 pw.println(" ");
2456 pw.println(" Dalvik Details");
2457
2458 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2459 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2460 final int myPss = memInfo.getOtherPss(i);
2461 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2462 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2463 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2464 final int mySharedClean = memInfo.getOtherSharedClean(i);
2465 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2466 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002467 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002468 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002469 || mySharedClean != 0 || myPrivateClean != 0
2470 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002471 if (dumpFullInfo) {
2472 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2473 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002474 mySharedClean, myPrivateClean,
2475 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2476 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002477 } else {
2478 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2479 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002480 myPrivateClean,
2481 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2482 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002483 }
2484 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002485 }
2486 }
2487 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002488
2489 pw.println(" ");
2490 pw.println(" App Summary");
2491 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2492 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2493 printRow(pw, ONE_COUNT_COLUMN,
2494 "Java Heap:", memInfo.getSummaryJavaHeap());
2495 printRow(pw, ONE_COUNT_COLUMN,
2496 "Native Heap:", memInfo.getSummaryNativeHeap());
2497 printRow(pw, ONE_COUNT_COLUMN,
2498 "Code:", memInfo.getSummaryCode());
2499 printRow(pw, ONE_COUNT_COLUMN,
2500 "Stack:", memInfo.getSummaryStack());
2501 printRow(pw, ONE_COUNT_COLUMN,
2502 "Graphics:", memInfo.getSummaryGraphics());
2503 printRow(pw, ONE_COUNT_COLUMN,
2504 "Private Other:", memInfo.getSummaryPrivateOther());
2505 printRow(pw, ONE_COUNT_COLUMN,
2506 "System:", memInfo.getSummarySystem());
2507 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002508 if (memInfo.hasSwappedOutPss) {
2509 printRow(pw, TWO_COUNT_COLUMNS,
2510 "TOTAL:", memInfo.getSummaryTotalPss(),
2511 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2512 } else {
2513 printRow(pw, TWO_COUNT_COLUMNS,
2514 "TOTAL:", memInfo.getSummaryTotalPss(),
2515 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2516 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002517 }
2518
Jeff Hamilton52d32032011-01-08 15:31:26 -06002519 public void registerOnActivityPausedListener(Activity activity,
2520 OnActivityPausedListener listener) {
2521 synchronized (mOnPauseListeners) {
2522 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2523 if (list == null) {
2524 list = new ArrayList<OnActivityPausedListener>();
2525 mOnPauseListeners.put(activity, list);
2526 }
2527 list.add(listener);
2528 }
2529 }
2530
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002531 public void unregisterOnActivityPausedListener(Activity activity,
2532 OnActivityPausedListener listener) {
2533 synchronized (mOnPauseListeners) {
2534 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2535 if (list != null) {
2536 list.remove(listener);
2537 }
2538 }
2539 }
2540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002541 public final ActivityInfo resolveActivityInfo(Intent intent) {
2542 ActivityInfo aInfo = intent.resolveActivityInfo(
2543 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2544 if (aInfo == null) {
2545 // Throw an exception.
2546 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002547 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 }
2549 return aInfo;
2550 }
Bob Leee5408332009-09-04 18:31:17 -07002551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002552 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002553 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002554 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002555 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002557 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 r.intent = intent;
2559 r.state = state;
2560 r.parent = parent;
2561 r.embeddedID = id;
2562 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002563 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 if (localLOGV) {
2565 ComponentName compname = intent.getComponent();
2566 String name;
2567 if (compname != null) {
2568 name = compname.toShortString();
2569 } else {
2570 name = "(Intent " + intent + ").getComponent() returned null";
2571 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002572 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 + ", comp=" + name
2574 + ", token=" + token);
2575 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002576 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002577 }
2578
2579 public final Activity getActivity(IBinder token) {
2580 return mActivities.get(token).activity;
2581 }
2582
2583 public final void sendActivityResult(
2584 IBinder token, String id, int requestCode,
2585 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002586 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002587 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002588 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2589 list.add(new ResultInfo(id, requestCode, resultCode, data));
2590 mAppThread.scheduleSendResult(token, list);
2591 }
2592
Jeff Brown9ef09972013-10-15 20:49:59 -07002593 private void sendMessage(int what, Object obj) {
2594 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 }
2596
Jeff Brown9ef09972013-10-15 20:49:59 -07002597 private void sendMessage(int what, Object obj, int arg1) {
2598 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 }
2600
Jeff Brown9ef09972013-10-15 20:49:59 -07002601 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2602 sendMessage(what, obj, arg1, arg2, false);
2603 }
2604
2605 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2606 if (DEBUG_MESSAGES) Slog.v(
2607 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2608 + ": " + arg1 + " / " + obj);
2609 Message msg = Message.obtain();
2610 msg.what = what;
2611 msg.obj = obj;
2612 msg.arg1 = arg1;
2613 msg.arg2 = arg2;
2614 if (async) {
2615 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002617 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618 }
2619
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002620 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2621 if (DEBUG_MESSAGES) Slog.v(
2622 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2623 "seq= " + seq);
2624 Message msg = Message.obtain();
2625 msg.what = what;
2626 SomeArgs args = SomeArgs.obtain();
2627 args.arg1 = obj;
2628 args.argi1 = arg1;
2629 args.argi2 = arg2;
2630 args.argi3 = seq;
2631 msg.obj = args;
2632 mH.sendMessage(msg);
2633 }
2634
Dianne Hackborn21556372010-02-04 16:34:40 -08002635 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 String what) {
2637 ContextCleanupInfo cci = new ContextCleanupInfo();
2638 cci.context = context;
2639 cci.who = who;
2640 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002641 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 }
2643
Craig Mautner233ceee2014-05-09 17:05:11 -07002644 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2646
2647 ActivityInfo aInfo = r.activityInfo;
2648 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002649 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 Context.CONTEXT_INCLUDE_CODE);
2651 }
Bob Leee5408332009-09-04 18:31:17 -07002652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002653 ComponentName component = r.intent.getComponent();
2654 if (component == null) {
2655 component = r.intent.resolveActivity(
2656 mInitialApplication.getPackageManager());
2657 r.intent.setComponent(component);
2658 }
2659
2660 if (r.activityInfo.targetActivity != null) {
2661 component = new ComponentName(r.activityInfo.packageName,
2662 r.activityInfo.targetActivity);
2663 }
2664
Adam Lesinski4e862812016-11-21 16:02:24 -08002665 ContextImpl appContext = createBaseContextForActivity(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002666 Activity activity = null;
2667 try {
Adam Lesinski4e862812016-11-21 16:02:24 -08002668 java.lang.ClassLoader cl = appContext.getClassLoader();
Dan Sandler07fe63e2017-08-15 16:41:11 -04002669 activity = mInstrumentation.newActivity(
2670 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002671 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002673 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002674 if (r.state != null) {
2675 r.state.setClassLoader(cl);
2676 }
2677 } catch (Exception e) {
2678 if (!mInstrumentation.onException(activity, e)) {
2679 throw new RuntimeException(
2680 "Unable to instantiate activity " + component
2681 + ": " + e.toString(), e);
2682 }
2683 }
2684
2685 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002686 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002687
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002688 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2689 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 TAG, r + ": app=" + app
2691 + ", appName=" + app.getPackageName()
2692 + ", pkg=" + r.packageInfo.getPackageName()
2693 + ", comp=" + r.intent.getComponent().toShortString()
2694 + ", dir=" + r.packageInfo.getAppDir());
2695
2696 if (activity != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002697 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002698 Configuration config = new Configuration(mCompatConfiguration);
Andrii Kuliand0ad9382016-04-18 20:54:20 -07002699 if (r.overrideConfig != null) {
2700 config.updateFrom(r.overrideConfig);
2701 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002702 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002703 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002704 Window window = null;
2705 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2706 window = r.mPendingRemoveWindow;
2707 r.mPendingRemoveWindow = null;
2708 r.mPendingRemoveWindowManager = null;
2709 }
Adam Lesinski4e862812016-11-21 16:02:24 -08002710 appContext.setOuterContext(activity);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002711 activity.attach(appContext, this, getInstrumentation(), r.token,
2712 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002713 r.embeddedID, r.lastNonConfigurationInstances, config,
Andrii Kulian51c1b672017-04-07 18:39:32 -07002714 r.referrer, r.voiceInteractor, window, r.configCallback);
Bob Leee5408332009-09-04 18:31:17 -07002715
Christopher Tateb70f3df2009-04-07 16:07:59 -07002716 if (customIntent != null) {
2717 activity.mIntent = customIntent;
2718 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002719 r.lastNonConfigurationInstances = null;
Sudheer Shanka84a48952017-03-08 18:19:01 -08002720 checkAndBlockForNetworkAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 activity.mStartedActivity = false;
2722 int theme = r.activityInfo.getThemeResource();
2723 if (theme != 0) {
2724 activity.setTheme(theme);
2725 }
2726
2727 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002728 if (r.isPersistable()) {
2729 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2730 } else {
2731 mInstrumentation.callActivityOnCreate(activity, r.state);
2732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 if (!activity.mCalled) {
2734 throw new SuperNotCalledException(
2735 "Activity " + r.intent.getComponent().toShortString() +
2736 " did not call through to super.onCreate()");
2737 }
2738 r.activity = activity;
2739 r.stopped = true;
2740 if (!r.activity.mFinished) {
2741 activity.performStart();
2742 r.stopped = false;
2743 }
2744 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002745 if (r.isPersistable()) {
2746 if (r.state != null || r.persistentState != null) {
2747 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2748 r.persistentState);
2749 }
2750 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2752 }
2753 }
2754 if (!r.activity.mFinished) {
2755 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002756 if (r.isPersistable()) {
2757 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2758 r.persistentState);
2759 } else {
2760 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 if (!activity.mCalled) {
2763 throw new SuperNotCalledException(
2764 "Activity " + r.intent.getComponent().toShortString() +
2765 " did not call through to super.onPostCreate()");
2766 }
2767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002768 }
2769 r.paused = true;
2770
2771 mActivities.put(r.token, r);
2772
2773 } catch (SuperNotCalledException e) {
2774 throw e;
2775
2776 } catch (Exception e) {
2777 if (!mInstrumentation.onException(activity, e)) {
2778 throw new RuntimeException(
2779 "Unable to start activity " + component
2780 + ": " + e.toString(), e);
2781 }
2782 }
2783
2784 return activity;
2785 }
2786
Sudheer Shanka84a48952017-03-08 18:19:01 -08002787 /**
2788 * Checks if {@link #mNetworkBlockSeq} is {@link #INVALID_PROC_STATE_SEQ} and if so, returns
2789 * immediately. Otherwise, makes a blocking call to ActivityManagerService to wait for the
2790 * network rules to get updated.
2791 */
2792 private void checkAndBlockForNetworkAccess() {
2793 synchronized (mNetworkPolicyLock) {
2794 if (mNetworkBlockSeq != INVALID_PROC_STATE_SEQ) {
2795 try {
2796 ActivityManager.getService().waitForNetworkStateUpdate(mNetworkBlockSeq);
2797 mNetworkBlockSeq = INVALID_PROC_STATE_SEQ;
2798 } catch (RemoteException ignored) {}
2799 }
2800 }
2801 }
2802
Adam Lesinski4e862812016-11-21 16:02:24 -08002803 private ContextImpl createBaseContextForActivity(ActivityClientRecord r) {
2804 final int displayId;
Craig Mautnere0a38842013-12-16 16:14:02 -08002805 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002806 displayId = ActivityManager.getService().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002807 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002808 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002809 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002810
Wale Ogunwale7c726682015-02-06 17:34:28 -08002811 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski4e862812016-11-21 16:02:24 -08002812 this, r.packageInfo, r.activityInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002813
2814 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002815 // For debugging purposes, if the activity's package name contains the value of
2816 // the "debug.use-second-display" system property as a substring, then show
2817 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002818 String pkgName = SystemProperties.get("debug.second-display.pkg");
2819 if (pkgName != null && !pkgName.isEmpty()
2820 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002821 for (int id : dm.getDisplayIds()) {
2822 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002823 Display display =
Bryce Lee609bf652017-02-09 16:50:13 -08002824 dm.getCompatibleDisplay(id, appContext.getResources());
Adam Lesinski4e862812016-11-21 16:02:24 -08002825 appContext = (ContextImpl) appContext.createDisplayContext(display);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002826 break;
2827 }
2828 }
2829 }
Adam Lesinski4e862812016-11-21 16:02:24 -08002830 return appContext;
Jeff Brownefd43bd2012-09-21 17:02:35 -07002831 }
2832
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002833 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 // If we are getting ready to gc after going to the background, well
2835 // we are back active so skip it.
2836 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002837 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838
Jeff Hao1b012d32014-08-20 10:35:34 -07002839 if (r.profilerInfo != null) {
2840 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002841 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002842 }
2843
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002844 // Make sure we are running with the most recent config.
2845 handleConfigurationChanged(null, null);
2846
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002847 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002849
Chet Haase0d1c27a2014-11-03 18:35:16 +00002850 // Initialize before creating the activity
Jesse Hallc37984f2017-05-23 16:55:08 -07002851 if (!ThreadedRenderer.sRendererDisabled) {
2852 GraphicsEnvironment.earlyInitEGL();
2853 }
Chet Haase0d1c27a2014-11-03 18:35:16 +00002854 WindowManagerGlobal.initialize();
2855
Craig Mautner233ceee2014-05-09 17:05:11 -07002856 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002857
2858 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002859 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002860 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002861 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002862 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002863 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002864
2865 if (!r.activity.mFinished && r.startsNotResumed) {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002866 // The activity manager actually wants this one to start out paused, because it
2867 // needs to be visible but isn't in the foreground. We accomplish this by going
2868 // through the normal startup (because activities expect to go through onResume()
2869 // the first time they run, before their window is displayed), and then pausing it.
2870 // However, in this case we do -not- need to do the full pause cycle (of freezing
2871 // and such) because the activity manager assumes it can just retain the current
2872 // state it has.
2873 performPauseActivityIfNeeded(r, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002875 // We need to keep around the original state, in case we need to be created again.
2876 // But we only do this for pre-Honeycomb apps, which always save their state when
2877 // pausing, so we can not have them save their state when restarting from a paused
2878 // state. For HC and later, we want to (and can) let the state be saved as the
2879 // normal part of stopping the activity.
2880 if (r.isPreHoneycomb()) {
2881 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 }
2884 } else {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002885 // 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 -08002886 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002887 ActivityManager.getService()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002888 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2889 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002891 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
2893 }
2894 }
2895
Filip Gruszczynski23493322015-07-29 17:02:59 -07002896 private void reportSizeConfigurations(ActivityClientRecord r) {
2897 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2898 if (configurations == null) {
2899 return;
2900 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002901 SparseIntArray horizontal = new SparseIntArray();
2902 SparseIntArray vertical = new SparseIntArray();
2903 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002904 for (int i = configurations.length - 1; i >= 0; i--) {
2905 Configuration config = configurations[i];
2906 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002907 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002908 }
2909 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002910 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002911 }
2912 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002913 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002914 }
2915 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002916 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08002917 ActivityManager.getService().reportSizeConfigurations(r.token,
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002918 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002919 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002920 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002921 }
2922
2923 }
2924
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002925 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 final int N = intents.size();
2927 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002928 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002930 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002931 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2933 }
2934 }
2935
Wale Ogunwale826c7062016-09-13 08:25:54 -07002936 void performNewIntents(IBinder token, List<ReferrerIntent> intents, boolean andPause) {
2937 final ActivityClientRecord r = mActivities.get(token);
2938 if (r == null) {
2939 return;
2940 }
2941
2942 final boolean resumed = !r.paused;
2943 if (resumed) {
2944 r.activity.mTemporaryPause = true;
2945 mInstrumentation.callActivityOnPause(r.activity);
2946 }
Sudheer Shanka43b5ea42017-06-21 11:24:53 -07002947 checkAndBlockForNetworkAccess();
Wale Ogunwale826c7062016-09-13 08:25:54 -07002948 deliverNewIntents(r, intents);
2949 if (resumed) {
2950 r.activity.performResume();
2951 r.activity.mTemporaryPause = false;
2952 }
2953
2954 if (r.paused && andPause) {
2955 // In this case the activity was in the paused state when we delivered the intent,
2956 // to guarantee onResume gets called after onNewIntent we temporarily resume the
2957 // activity and pause again as the caller wanted.
2958 performResumeActivity(token, false, "performNewIntents");
2959 performPauseActivityIfNeeded(r, "performNewIntents");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002960 }
2961 }
Bob Leee5408332009-09-04 18:31:17 -07002962
Romain Guy65b345f2011-07-27 18:51:50 -07002963 private void handleNewIntent(NewIntentData data) {
Wale Ogunwale826c7062016-09-13 08:25:54 -07002964 performNewIntents(data.token, data.intents, data.andPause);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 }
2966
Adam Skorydfc7fd72013-08-05 19:23:41 -07002967 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002968 // Filling for autofill has a few differences:
Felipe Leme1ca634a2016-11-28 17:21:21 -08002969 // - it does not need an AssistContent
2970 // - it does not call onProvideAssistData()
2971 // - it needs an IAutoFillCallback
Felipe Leme640f30a2017-03-06 15:44:06 -08002972 boolean forAutofill = cmd.requestType == ActivityManager.ASSIST_CONTEXT_AUTOFILL;
Felipe Leme1ca634a2016-11-28 17:21:21 -08002973
Felipe Leme85d1c2d2017-04-21 08:56:04 -07002974 // TODO: decide if lastSessionId logic applies to autofill sessions
Amith Yamasani4f128e42016-05-10 11:44:12 -07002975 if (mLastSessionId != cmd.sessionId) {
2976 // Clear the existing structures
2977 mLastSessionId = cmd.sessionId;
2978 for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
2979 AssistStructure structure = mLastAssistStructures.get(i).get();
2980 if (structure != null) {
2981 structure.clearSendChannel();
2982 }
2983 mLastAssistStructures.remove(i);
Dianne Hackborn782d4982015-07-08 17:36:37 -07002984 }
2985 }
Felipe Leme29a5b0d2016-10-25 14:57:11 -07002986
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002987 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002988 AssistStructure structure = null;
Felipe Leme640f30a2017-03-06 15:44:06 -08002989 AssistContent content = forAutofill ? null : new AssistContent();
Amith Yamasani858f98d2017-02-22 12:59:53 -08002990 final long startTime = SystemClock.uptimeMillis();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002991 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002992 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002993 if (r != null) {
Felipe Leme640f30a2017-03-06 15:44:06 -08002994 if (!forAutofill) {
Felipe Leme1ca634a2016-11-28 17:21:21 -08002995 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
Felipe Leme29a5b0d2016-10-25 14:57:11 -07002996 r.activity.onProvideAssistData(data);
Felipe Leme6d553872016-12-08 17:13:25 -08002997 referrer = r.activity.onProvideReferrer();
Felipe Leme29a5b0d2016-10-25 14:57:11 -07002998 }
Felipe Leme640f30a2017-03-06 15:44:06 -08002999 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL || forAutofill) {
Svet Ganovfd31f852017-04-26 15:54:27 -07003000 structure = new AssistStructure(r.activity, forAutofill, cmd.flags);
Adam Skory4aaed142015-04-22 11:29:31 -06003001 Intent activityIntent = r.activity.getIntent();
Felipe Leme6d553872016-12-08 17:13:25 -08003002 boolean notSecure = r.window == null ||
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07003003 (r.window.getAttributes().flags
Felipe Leme6d553872016-12-08 17:13:25 -08003004 & WindowManager.LayoutParams.FLAG_SECURE) == 0;
3005 if (activityIntent != null && notSecure) {
Felipe Leme640f30a2017-03-06 15:44:06 -08003006 if (!forAutofill) {
Felipe Leme1ca634a2016-11-28 17:21:21 -08003007 Intent intent = new Intent(activityIntent);
3008 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
3009 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
3010 intent.removeUnsafeExtras();
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003011 content.setDefaultIntent(intent);
3012 }
Adam Skory4aaed142015-04-22 11:29:31 -06003013 } else {
Felipe Leme640f30a2017-03-06 15:44:06 -08003014 if (!forAutofill) {
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003015 content.setDefaultIntent(new Intent());
3016 }
Adam Skory4aaed142015-04-22 11:29:31 -06003017 }
Felipe Leme640f30a2017-03-06 15:44:06 -08003018 if (!forAutofill) {
Felipe Leme29a5b0d2016-10-25 14:57:11 -07003019 r.activity.onProvideAssistContent(content);
3020 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07003021 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003022 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07003023 if (structure == null) {
3024 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003025 }
Felipe Leme0200d9e2017-01-24 15:10:26 -08003026
Felipe Leme85d1c2d2017-04-21 08:56:04 -07003027 // TODO: decide if lastSessionId logic applies to autofill sessions
Amith Yamasani858f98d2017-02-22 12:59:53 -08003028
3029 structure.setAcquisitionStartTime(startTime);
3030 structure.setAcquisitionEndTime(SystemClock.uptimeMillis());
3031
Amith Yamasani4f128e42016-05-10 11:44:12 -07003032 mLastAssistStructures.add(new WeakReference<>(structure));
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003033 IActivityManager mgr = ActivityManager.getService();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003034 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07003035 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003036 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003037 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08003038 }
3039 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07003040
3041 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
3042 ActivityClientRecord r = mActivities.get(token);
3043 if (r != null) {
3044 r.activity.onTranslucentConversionComplete(drawComplete);
3045 }
3046 }
3047
Craig Mautnereb8abf72014-07-02 15:04:09 -07003048 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
3049 ActivityClientRecord r = mActivities.get(token);
3050 if (r != null) {
3051 r.activity.onNewActivityOptions(options);
3052 }
3053 }
3054
Jeff Sharkeydd97f422013-10-08 17:01:30 -07003055 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07003056 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3057 try {
Jeff Sharkeya6bfeae2017-07-05 16:50:24 -06003058 installContentProviders(mInitialApplication, Arrays.asList(info));
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07003059 } finally {
3060 StrictMode.setThreadPolicy(oldPolicy);
3061 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07003062 }
3063
Craig Mautner8746a472014-07-24 15:12:54 -07003064 private void handleEnterAnimationComplete(IBinder token) {
3065 ActivityClientRecord r = mActivities.get(token);
3066 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08003067 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07003068 }
3069 }
3070
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04003071 private void handleStartBinderTracking() {
3072 Binder.enableTracing();
3073 }
3074
3075 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
3076 try {
3077 Binder.disableTracing();
3078 Binder.getTransactionTracker().writeTracesToFile(fd);
3079 } finally {
3080 IoUtils.closeQuietly(fd);
3081 Binder.getTransactionTracker().clearTraces();
3082 }
3083 }
3084
Winson Chung5af42fc2017-03-24 17:11:33 -07003085 private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode,
3086 Configuration overrideConfig) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08003087 final ActivityClientRecord r = mActivities.get(token);
3088 if (r != null) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003089 final Configuration newConfig = new Configuration(mConfiguration);
3090 if (overrideConfig != null) {
3091 newConfig.updateFrom(overrideConfig);
3092 }
3093 r.activity.dispatchMultiWindowModeChanged(isInMultiWindowMode, newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003094 }
3095 }
3096
Winson Chung5af42fc2017-03-24 17:11:33 -07003097 private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode,
3098 Configuration overrideConfig) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08003099 final ActivityClientRecord r = mActivities.get(token);
3100 if (r != null) {
Winson Chung5af42fc2017-03-24 17:11:33 -07003101 final Configuration newConfig = new Configuration(mConfiguration);
3102 if (overrideConfig != null) {
3103 newConfig.updateFrom(overrideConfig);
3104 }
3105 r.activity.dispatchPictureInPictureModeChanged(isInPipMode, newConfig);
Wale Ogunwale5f986092015-12-04 15:35:38 -08003106 }
3107 }
3108
Amith Yamasani0af6fa72016-01-17 15:36:19 -08003109 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
3110 final ActivityClientRecord r = mActivities.get(token);
3111 if (r != null) {
3112 r.voiceInteractor = interactor;
3113 r.activity.setVoiceInteractor(interactor);
3114 if (interactor == null) {
3115 r.activity.onLocalVoiceInteractionStopped();
3116 } else {
3117 r.activity.onLocalVoiceInteractionStarted();
3118 }
3119 }
3120 }
3121
Leonard Mosescuf3409ce2016-10-06 17:32:05 -07003122 static final void handleAttachAgent(String agent) {
3123 try {
3124 VMDebug.attachAgent(agent);
3125 } catch (IOException e) {
3126 Slog.e(TAG, "Attaching agent failed: " + agent);
3127 }
3128 }
3129
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003130 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
3131
3132 /**
3133 * Return the Intent that's currently being handled by a
3134 * BroadcastReceiver on this thread, or null if none.
3135 * @hide
3136 */
3137 public static Intent getIntentBeingBroadcast() {
3138 return sCurrentBroadcastIntent.get();
3139 }
3140
Romain Guy65b345f2011-07-27 18:51:50 -07003141 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 // If we are getting ready to gc after going to the background, well
3143 // we are back active so skip it.
3144 unscheduleGcIdler();
3145
3146 String component = data.intent.getComponent().getClassName();
3147
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003148 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003149 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003151 IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152
Adam Lesinski4e862812016-11-21 16:02:24 -08003153 Application app;
Romain Guy65b345f2011-07-27 18:51:50 -07003154 BroadcastReceiver receiver;
Adam Lesinski4e862812016-11-21 16:02:24 -08003155 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 try {
Adam Lesinski4e862812016-11-21 16:02:24 -08003157 app = packageInfo.makeApplication(false, mInstrumentation);
3158 context = (ContextImpl) app.getBaseContext();
3159 if (data.info.splitName != null) {
3160 context = (ContextImpl) context.createContextForSplit(data.info.splitName);
3161 }
3162 java.lang.ClassLoader cl = context.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003164 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07003165 data.setExtrasClassLoader(cl);
Dan Sandler07fe63e2017-08-15 16:41:11 -04003166 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003168 if (DEBUG_BROADCAST) Slog.i(TAG,
3169 "Finishing failed broadcast to " + data.intent.getComponent());
3170 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 throw new RuntimeException(
3172 "Unable to instantiate receiver " + component
3173 + ": " + e.toString(), e);
3174 }
3175
3176 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003177 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 TAG, "Performing receive of " + data.intent
3179 + ": app=" + app
3180 + ", appName=" + app.getPackageName()
3181 + ", pkg=" + packageInfo.getPackageName()
3182 + ", comp=" + data.intent.getComponent().toShortString()
3183 + ", dir=" + packageInfo.getAppDir());
3184
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003185 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07003186 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 receiver.onReceive(context.getReceiverRestrictedContext(),
3188 data.intent);
3189 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003190 if (DEBUG_BROADCAST) Slog.i(TAG,
3191 "Finishing failed broadcast to " + data.intent.getComponent());
3192 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 if (!mInstrumentation.onException(receiver, e)) {
3194 throw new RuntimeException(
3195 "Unable to start receiver " + component
3196 + ": " + e.toString(), e);
3197 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003198 } finally {
3199 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
3201
Dianne Hackborne829fef2010-10-26 17:44:01 -07003202 if (receiver.getPendingResult() != null) {
3203 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 }
3205 }
3206
Christopher Tate181fafa2009-05-14 11:12:14 -07003207 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003208 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003209 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003210
Christopher Tate346acb12012-10-15 19:20:25 -07003211 // Sanity check the requested target package's uid against ours
3212 try {
3213 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3214 data.appInfo.packageName, 0, UserHandle.myUserId());
3215 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3216 Slog.w(TAG, "Asked to instantiate non-matching package "
3217 + data.appInfo.packageName);
3218 return;
3219 }
3220 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003221 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003222 }
3223
Christopher Tate181fafa2009-05-14 11:12:14 -07003224 // no longer idle; we have backup work to do
3225 unscheduleGcIdler();
3226
3227 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003228 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003229 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003230 if (packageName == null) {
3231 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3232 return;
3233 }
3234
Christopher Tate181fafa2009-05-14 11:12:14 -07003235 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003236 // full backup operation but no app-supplied agent? use the default implementation
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003237 if (classname == null && (data.backupMode == ApplicationThreadConstants.BACKUP_MODE_FULL
3238 || data.backupMode == ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003239 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003240 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003241
Christopher Tate181fafa2009-05-14 11:12:14 -07003242 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003243 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003244 BackupAgent agent = mBackupAgents.get(packageName);
3245 if (agent != null) {
3246 // reusing the existing instance
3247 if (DEBUG_BACKUP) {
3248 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003249 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003250 binder = agent.onBind();
3251 } else {
3252 try {
3253 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3254
3255 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Dan Sandler07fe63e2017-08-15 16:41:11 -04003256 agent = (BackupAgent) cl.loadClass(classname).newInstance();
Christopher Tate2e40d112014-07-15 12:37:38 -07003257
3258 // set up the agent's context
Dan Sandler07fe63e2017-08-15 16:41:11 -04003259 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
Christopher Tate2e40d112014-07-15 12:37:38 -07003260 context.setOuterContext(agent);
3261 agent.attach(context);
3262
3263 agent.onCreate();
3264 binder = agent.onBind();
3265 mBackupAgents.put(packageName, agent);
3266 } catch (Exception e) {
3267 // If this is during restore, fail silently; otherwise go
3268 // ahead and let the user see the crash.
3269 Slog.e(TAG, "Agent threw during creation: " + e);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07003270 if (data.backupMode != ApplicationThreadConstants.BACKUP_MODE_RESTORE
3271 && data.backupMode !=
3272 ApplicationThreadConstants.BACKUP_MODE_RESTORE_FULL) {
Christopher Tate2e40d112014-07-15 12:37:38 -07003273 throw e;
3274 }
3275 // falling through with 'binder' still null
3276 }
Christopher Tated1475e02009-07-09 15:36:17 -07003277 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003278
3279 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003280 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003281 ActivityManager.getService().backupAgentCreated(packageName, binder);
Christopher Tate181fafa2009-05-14 11:12:14 -07003282 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003283 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003284 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003285 } catch (Exception e) {
3286 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003287 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003288 }
3289 }
3290
3291 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003292 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003293 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003294
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003295 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003296 String packageName = packageInfo.mPackageName;
3297 BackupAgent agent = mBackupAgents.get(packageName);
3298 if (agent != null) {
3299 try {
3300 agent.onDestroy();
3301 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003302 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003303 e.printStackTrace();
3304 }
3305 mBackupAgents.remove(packageName);
3306 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003307 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003308 }
3309 }
3310
Romain Guy65b345f2011-07-27 18:51:50 -07003311 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003312 // If we are getting ready to gc after going to the background, well
3313 // we are back active so skip it.
3314 unscheduleGcIdler();
3315
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003316 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003317 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 Service service = null;
3319 try {
3320 java.lang.ClassLoader cl = packageInfo.getClassLoader();
Dan Sandler07fe63e2017-08-15 16:41:11 -04003321 service = (Service) cl.loadClass(data.info.name).newInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 } catch (Exception e) {
3323 if (!mInstrumentation.onException(service, e)) {
3324 throw new RuntimeException(
3325 "Unable to instantiate service " + data.info.name
3326 + ": " + e.toString(), e);
3327 }
3328 }
3329
3330 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003331 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332
Jeff Browndefd4a62014-03-10 21:24:37 -07003333 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3334 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335
Dan Sandler07fe63e2017-08-15 16:41:11 -04003336 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 service.attach(context, this, data.info.name, data.token, app,
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003338 ActivityManager.getService());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 service.onCreate();
3340 mServices.put(data.token, service);
3341 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003342 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003343 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003344 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003345 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347 } catch (Exception e) {
3348 if (!mInstrumentation.onException(service, e)) {
3349 throw new RuntimeException(
3350 "Unable to create service " + data.info.name
3351 + ": " + e.toString(), e);
3352 }
3353 }
3354 }
3355
Romain Guy65b345f2011-07-27 18:51:50 -07003356 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003358 if (DEBUG_SERVICE)
3359 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 if (s != null) {
3361 try {
3362 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003363 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 try {
3365 if (!data.rebind) {
3366 IBinder binder = s.onBind(data.intent);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003367 ActivityManager.getService().publishService(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003368 data.token, data.intent, binder);
3369 } else {
3370 s.onRebind(data.intent);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003371 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003372 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003374 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003376 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 }
3378 } catch (Exception e) {
3379 if (!mInstrumentation.onException(s, e)) {
3380 throw new RuntimeException(
3381 "Unable to bind to service " + s
3382 + " with " + data.intent + ": " + e.toString(), e);
3383 }
3384 }
3385 }
3386 }
3387
Romain Guy65b345f2011-07-27 18:51:50 -07003388 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 Service s = mServices.get(data.token);
3390 if (s != null) {
3391 try {
3392 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003393 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 boolean doRebind = s.onUnbind(data.intent);
3395 try {
3396 if (doRebind) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003397 ActivityManager.getService().unbindFinished(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 data.token, data.intent, doRebind);
3399 } else {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003400 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003401 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 }
3403 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003404 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 }
3406 } catch (Exception e) {
3407 if (!mInstrumentation.onException(s, e)) {
3408 throw new RuntimeException(
3409 "Unable to unbind to service " + s
3410 + " with " + data.intent + ": " + e.toString(), e);
3411 }
3412 }
3413 }
3414 }
3415
Dianne Hackborn625ac272010-09-17 18:29:22 -07003416 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003417 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3418 try {
3419 Service s = mServices.get(info.token);
3420 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003421 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3422 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003423 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3424 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003426 } finally {
3427 IoUtils.closeQuietly(info.fd);
3428 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 }
3430 }
3431
Dianne Hackborn625ac272010-09-17 18:29:22 -07003432 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003433 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3434 try {
3435 ActivityClientRecord r = mActivities.get(info.token);
3436 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003437 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3438 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003439 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3440 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003441 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003442 } finally {
3443 IoUtils.closeQuietly(info.fd);
3444 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003445 }
3446 }
3447
Marco Nelissen18cb2872011-11-15 11:19:53 -08003448 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003449 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3450 try {
3451 ProviderClientRecord r = mLocalProviders.get(info.token);
3452 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003453 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3454 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003455 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3456 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003457 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003458 } finally {
3459 IoUtils.closeQuietly(info.fd);
3460 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003461 }
3462 }
3463
Romain Guy65b345f2011-07-27 18:51:50 -07003464 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 Service s = mServices.get(data.token);
3466 if (s != null) {
3467 try {
3468 if (data.args != null) {
3469 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003470 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003472 int res;
3473 if (!data.taskRemoved) {
3474 res = s.onStartCommand(data.args, data.flags, data.startId);
3475 } else {
3476 s.onTaskRemoved(data.args);
3477 res = Service.START_TASK_REMOVED_COMPLETE;
3478 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003479
3480 QueuedWork.waitToFinish();
3481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003483 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003484 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003485 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003486 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003487 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003488 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003489 } catch (Exception e) {
3490 if (!mInstrumentation.onException(s, e)) {
3491 throw new RuntimeException(
3492 "Unable to start service " + s
3493 + " with " + data.args + ": " + e.toString(), e);
3494 }
3495 }
3496 }
3497 }
3498
Romain Guy65b345f2011-07-27 18:51:50 -07003499 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 Service s = mServices.remove(token);
3501 if (s != null) {
3502 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003503 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003504 s.onDestroy();
Amith Yamasani75928252017-05-22 15:10:47 -07003505 s.detachAndCleanUp();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003506 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003507 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003508 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003509 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003511
3512 QueuedWork.waitToFinish();
3513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003515 ActivityManager.getService().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003516 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003518 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 }
3520 } catch (Exception e) {
3521 if (!mInstrumentation.onException(s, e)) {
3522 throw new RuntimeException(
3523 "Unable to stop service " + s
3524 + ": " + e.toString(), e);
3525 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003526 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003528 } else {
3529 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003530 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003531 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 }
3533
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003534 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003535 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003536 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003537 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003538 + " finished=" + r.activity.mFinished);
3539 if (r != null && !r.activity.mFinished) {
3540 if (clearHide) {
3541 r.hideForNow = false;
3542 r.activity.mStartedActivity = false;
3543 }
3544 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003545 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003546 r.activity.mFragments.noteStateNotSaved();
Sudheer Shanka43b5ea42017-06-21 11:24:53 -07003547 checkAndBlockForNetworkAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 if (r.pendingIntents != null) {
3549 deliverNewIntents(r, r.pendingIntents);
3550 r.pendingIntents = null;
3551 }
3552 if (r.pendingResults != null) {
3553 deliverResults(r, r.pendingResults);
3554 r.pendingResults = null;
3555 }
3556 r.activity.performResume();
3557
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01003558 synchronized (mResourcesManager) {
3559 // If there is a pending local relaunch that was requested when the activity was
3560 // paused, it will put the activity into paused state when it finally happens.
3561 // Since the activity resumed before being relaunched, we don't want that to
3562 // happen, so we need to clear the request to relaunch paused.
3563 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3564 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3565 if (relaunching.token == r.token
3566 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3567 relaunching.startsNotResumed = false;
3568 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003569 }
3570 }
3571
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003572 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3573 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003574
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 r.paused = false;
3576 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003577 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003578 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003579 } catch (Exception e) {
3580 if (!mInstrumentation.onException(r.activity, e)) {
3581 throw new RuntimeException(
3582 "Unable to resume activity "
3583 + r.intent.getComponent().toShortString()
3584 + ": " + e.toString(), e);
3585 }
3586 }
3587 }
3588 return r;
3589 }
3590
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003591 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3592 if (r.mPreserveWindow && !force) {
3593 return;
3594 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003595 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003596 r.mPendingRemoveWindowManager.removeViewImmediate(
3597 r.mPendingRemoveWindow.getDecorView());
3598 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003599 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003600 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003601 r.activity.getClass().getName(), "Activity");
3602 }
3603 }
3604 r.mPendingRemoveWindow = null;
3605 r.mPendingRemoveWindowManager = null;
3606 }
3607
Craig Mautner233ceee2014-05-09 17:05:11 -07003608 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003609 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003610 ActivityClientRecord r = mActivities.get(token);
3611 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3612 return;
3613 }
3614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 // If we are getting ready to gc after going to the background, well
3616 // we are back active so skip it.
3617 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003618 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619
Adam Powellcfbe9be2013-11-06 14:58:58 -08003620 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003621 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622
3623 if (r != null) {
3624 final Activity a = r.activity;
3625
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003626 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003627 TAG, "Resume " + r + " started activity: " +
3628 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3629 + ", finished: " + a.mFinished);
3630
3631 final int forwardBit = isForward ?
3632 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 // If the window hasn't yet been added to the window manager,
3635 // and this guy didn't finish itself or start another activity,
3636 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003637 boolean willBeVisible = !a.mStartedActivity;
3638 if (!willBeVisible) {
3639 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003640 willBeVisible = ActivityManager.getService().willActivityBeVisible(
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003641 a.getActivityToken());
3642 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003643 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003644 }
3645 }
3646 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 r.window = r.activity.getWindow();
3648 View decor = r.window.getDecorView();
3649 decor.setVisibility(View.INVISIBLE);
3650 ViewManager wm = a.getWindowManager();
3651 WindowManager.LayoutParams l = r.window.getAttributes();
3652 a.mDecor = decor;
3653 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3654 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003655 if (r.mPreserveWindow) {
3656 a.mWindowAdded = true;
3657 r.mPreserveWindow = false;
Robert Carrb2594852016-04-25 16:21:13 -07003658 // Normally the ViewRoot sets up callbacks with the Activity
3659 // in addView->ViewRootImpl#setView. If we are instead reusing
3660 // the decor view we have to notify the view root that the
3661 // callbacks may have changed.
3662 ViewRootImpl impl = decor.getViewRootImpl();
Robert Carr311ecba2016-04-27 12:59:45 -07003663 if (impl != null) {
3664 impl.notifyChildRebuilt();
3665 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003666 }
Bryce Lee4b51b322017-04-07 10:27:19 -07003667 if (a.mVisibleFromClient) {
3668 if (!a.mWindowAdded) {
3669 a.mWindowAdded = true;
3670 wm.addView(decor, l);
3671 } else {
3672 // The activity will get a callback for this {@link LayoutParams} change
3673 // earlier. However, at that time the decor will not be set (this is set
3674 // in this method), so no action will be taken. This call ensures the
3675 // callback occurs with the decor set.
3676 a.onWindowAttributesChanged(l);
3677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
3679
3680 // If the window has already been added, but during resume
3681 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003682 // window visible.
3683 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003684 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 TAG, "Launch " + r + " mStartedActivity set");
3686 r.hideForNow = true;
3687 }
3688
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003689 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003690 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003692 // The window is now visible if it has been added, we are not
3693 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003694 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003695 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003696 if (r.newConfig != null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08003697 performConfigurationChangedForActivity(r, r.newConfig);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003698 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003699 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 r.newConfig = null;
3701 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003702 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 + isForward);
3704 WindowManager.LayoutParams l = r.window.getAttributes();
3705 if ((l.softInputMode
3706 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3707 != forwardBit) {
3708 l.softInputMode = (l.softInputMode
3709 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3710 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003711 if (r.activity.mVisibleFromClient) {
3712 ViewManager wm = a.getWindowManager();
3713 View decor = r.window.getDecorView();
3714 wm.updateViewLayout(decor, l);
3715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
Bryce Lee4b51b322017-04-07 10:27:19 -07003717
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 r.activity.mVisibleFromServer = true;
3719 mNumVisibleActivities++;
3720 if (r.activity.mVisibleFromClient) {
3721 r.activity.makeVisible();
3722 }
3723 }
3724
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003725 if (!r.onlyLocalRequest) {
3726 r.nextIdle = mNewActivities;
3727 mNewActivities = r;
3728 if (localLOGV) Slog.v(
3729 TAG, "Scheduling idle handler for " + r);
3730 Looper.myQueue().addIdleHandler(new Idler());
3731 }
3732 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003734 // Tell the activity manager we have resumed.
3735 if (reallyResume) {
3736 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003737 ActivityManager.getService().activityResumed(token);
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003738 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003739 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003740 }
3741 }
3742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003743 } else {
3744 // If an exception was thrown when trying to resume, then
3745 // just end this activity.
3746 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003747 ActivityManager.getService()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003748 .finishActivity(token, Activity.RESULT_CANCELED, null,
3749 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003751 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
3753 }
3754 }
3755
3756 private int mThumbnailWidth = -1;
3757 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003758 private Bitmap mAvailThumbnailBitmap = null;
3759 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760
Romain Guy65b345f2011-07-27 18:51:50 -07003761 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003762 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003763 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003764 if (thumbnail == null) {
3765 int w = mThumbnailWidth;
3766 int h;
3767 if (w < 0) {
3768 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003769 int wId = com.android.internal.R.dimen.thumbnail_width;
3770 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003771 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3772 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003773 } else {
3774 h = mThumbnailHeight;
3775 }
3776
3777 // On platforms where we don't want thumbnails, set dims to (0,0)
3778 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003779 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3780 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003781 thumbnail.eraseColor(0);
3782 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 }
3784
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003785 if (thumbnail != null) {
3786 Canvas cv = mThumbnailCanvas;
3787 if (cv == null) {
3788 mThumbnailCanvas = cv = new Canvas();
3789 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003790
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003791 cv.setBitmap(thumbnail);
3792 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3793 mAvailThumbnailBitmap = thumbnail;
3794 thumbnail = null;
3795 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003796 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003797 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003799 } catch (Exception e) {
3800 if (!mInstrumentation.onException(r.activity, e)) {
3801 throw new RuntimeException(
3802 "Unable to create thumbnail of "
3803 + r.intent.getComponent().toShortString()
3804 + ": " + e.toString(), e);
3805 }
3806 thumbnail = null;
3807 }
3808
3809 return thumbnail;
3810 }
3811
Romain Guy65b345f2011-07-27 18:51:50 -07003812 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003813 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003814 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003815 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003816 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3817 return;
3818 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003819 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003820 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 if (userLeaving) {
3822 performUserLeavingActivity(r);
3823 }
Bob Leee5408332009-09-04 18:31:17 -07003824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003826 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003828 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003829 if (r.isPreHoneycomb()) {
3830 QueuedWork.waitToFinish();
3831 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003834 if (!dontReport) {
3835 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003836 ActivityManager.getService().activityPaused(token);
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003837 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003838 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003840 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003841 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 }
3843 }
3844
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003845 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003846 mInstrumentation.callActivityOnUserLeaving(r.activity);
3847 }
3848
3849 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003850 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003851 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003852 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 }
3854
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003855 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003856 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003857 if (r.paused) {
3858 if (r.activity.mFinished) {
3859 // If we are finishing, we won't call onResume() in certain cases.
3860 // So here we likewise don't want to call onPause() if the activity
3861 // isn't resumed.
3862 return null;
3863 }
3864 RuntimeException e = new RuntimeException(
3865 "Performing pause of activity that is not resumed: "
3866 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003867 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869 if (finished) {
3870 r.activity.mFinished = true;
3871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003872
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003873 // Next have the activity save its current state and managed dialogs...
3874 if (!r.activity.mFinished && saveState) {
3875 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003877
3878 performPauseActivityIfNeeded(r, reason);
Jeff Hamilton52d32032011-01-08 15:31:26 -06003879
3880 // Notify any outstanding on paused listeners
3881 ArrayList<OnActivityPausedListener> listeners;
3882 synchronized (mOnPauseListeners) {
3883 listeners = mOnPauseListeners.remove(r.activity);
3884 }
3885 int size = (listeners != null ? listeners.size() : 0);
3886 for (int i = 0; i < size; i++) {
3887 listeners.get(i).onPaused(r.activity);
3888 }
3889
Craig Mautnera0026042014-04-23 11:45:37 -07003890 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 }
3892
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003893 private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
3894 if (r.paused) {
3895 // You are already paused silly...
3896 return;
3897 }
3898
3899 try {
3900 r.activity.mCalled = false;
3901 mInstrumentation.callActivityOnPause(r.activity);
3902 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
3903 r.activity.getComponentName().getClassName(), reason);
3904 if (!r.activity.mCalled) {
3905 throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
3906 + " did not call through to super.onPause()");
3907 }
3908 } catch (SuperNotCalledException e) {
3909 throw e;
3910 } catch (Exception e) {
3911 if (!mInstrumentation.onException(r.activity, e)) {
3912 throw new RuntimeException("Unable to pause activity "
3913 + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
3914 }
3915 }
3916 r.paused = true;
3917 }
3918
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003919 final void performStopActivity(IBinder token, 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 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003922 }
3923
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003924 private static class StopInfo implements Runnable {
3925 ActivityClientRecord activity;
3926 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003927 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003929
3930 @Override public void run() {
3931 // Tell activity manager we have been stopped.
3932 try {
3933 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
Sudheer Shankadc589ac2016-11-10 15:30:17 -08003934 ActivityManager.getService().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003935 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003936 } catch (RemoteException ex) {
Jeff Sharkeya6bfeae2017-07-05 16:50:24 -06003937 // Dump statistics about bundle to help developers debug
3938 final LogWriter writer = new LogWriter(Log.WARN, TAG);
3939 final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
3940 pw.println("Bundle stats:");
3941 Bundle.dumpStats(pw, state);
3942 pw.println("PersistableBundle stats:");
3943 Bundle.dumpStats(pw, persistentState);
3944
Jeff Sharkey369f5092016-02-29 11:16:21 -07003945 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003946 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003947 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003948 return;
3949 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003950 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003951 }
3952 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003953 }
3954
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003955 private static final class ProviderRefCount {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07003956 public final ContentProviderHolder holder;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003957 public final ProviderClientRecord client;
3958 public int stableCount;
3959 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003960
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003961 // When this is set, the stable and unstable ref counts are 0 and
3962 // we have a pending operation scheduled to remove the ref count
3963 // from the activity manager. On the activity manager we are still
3964 // holding an unstable ref, though it is not reflected in the counts
3965 // here.
3966 public boolean removePending;
3967
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07003968 ProviderRefCount(ContentProviderHolder inHolder,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003969 ProviderClientRecord inClient, int sCount, int uCount) {
3970 holder = inHolder;
3971 client = inClient;
3972 stableCount = sCount;
3973 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003974 }
3975 }
3976
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003977 /**
3978 * Core implementation of stopping an activity. Note this is a little
3979 * tricky because the server's meaning of stop is slightly different
3980 * than our client -- for the server, stop means to save state and give
3981 * it the result when it is done, but the window may still be visible.
3982 * For the client, we want to call onStop()/onStart() to indicate when
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07003983 * the activity's UI visibility changes.
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003984 */
Romain Guy65b345f2011-07-27 18:51:50 -07003985 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003986 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003987 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003988 if (r != null) {
3989 if (!keepShown && r.stopped) {
3990 if (r.activity.mFinished) {
3991 // If we are finishing, we won't call onResume() in certain
3992 // cases. So here we likewise don't want to call onStop()
3993 // if the activity isn't resumed.
3994 return;
3995 }
3996 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07003997 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003999 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07004000 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 }
4002
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004003 // One must first be paused before stopped...
4004 performPauseActivityIfNeeded(r, reason);
4005
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004006 if (info != null) {
4007 try {
4008 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07004009 // For now, don't create the thumbnail here; we are
4010 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 info.description = r.activity.onCreateDescription();
4012 } catch (Exception e) {
4013 if (!mInstrumentation.onException(r.activity, e)) {
4014 throw new RuntimeException(
4015 "Unable to save state of activity "
4016 + r.intent.getComponent().toShortString()
4017 + ": " + e.toString(), e);
4018 }
4019 }
4020 }
4021
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004022 // Next have the activity save its current state and managed dialogs...
4023 if (!r.activity.mFinished && saveState) {
4024 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07004025 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004026 }
4027 }
4028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 if (!keepShown) {
4030 try {
4031 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004032 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 } catch (Exception e) {
4034 if (!mInstrumentation.onException(r.activity, e)) {
4035 throw new RuntimeException(
4036 "Unable to stop activity "
4037 + r.intent.getComponent().toShortString()
4038 + ": " + e.toString(), e);
4039 }
4040 }
4041 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004042 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4043 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004044 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004045 }
4046 }
4047
Romain Guy65b345f2011-07-27 18:51:50 -07004048 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004049 View v = r.activity.mDecor;
4050 if (v != null) {
4051 if (show) {
4052 if (!r.activity.mVisibleFromServer) {
4053 r.activity.mVisibleFromServer = true;
4054 mNumVisibleActivities++;
4055 if (r.activity.mVisibleFromClient) {
4056 r.activity.makeVisible();
4057 }
4058 }
4059 if (r.newConfig != null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004060 performConfigurationChangedForActivity(r, r.newConfig);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004061 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08004062 + r.activityInfo.name + " with new config "
4063 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 r.newConfig = null;
4065 }
4066 } else {
4067 if (r.activity.mVisibleFromServer) {
4068 r.activity.mVisibleFromServer = false;
4069 mNumVisibleActivities--;
4070 v.setVisibility(View.INVISIBLE);
4071 }
4072 }
4073 }
4074 }
4075
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004076 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004077 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004078 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
4079 return;
4080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004081 r.activity.mConfigChangeFlags |= configChanges;
4082
4083 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004084 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004086 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 TAG, "Finishing stop of " + r + ": show=" + show
4088 + " win=" + r.window);
4089
4090 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07004091
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004092 // Make sure any pending writes are now committed.
4093 if (!r.isPreHoneycomb()) {
4094 QueuedWork.waitToFinish();
4095 }
4096
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004097 // Schedule the call to tell the activity manager we have
4098 // stopped. We don't do this immediately, because we want to
4099 // have a chance for any other pending work (in particular memory
4100 // trim requests) to complete before you tell the activity
4101 // manager to proceed and allow us to go fully into the background.
4102 info.activity = r;
4103 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07004104 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004105 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004106 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
4108
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004109 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
4110 String action) {
4111 if (r == null) {
4112 return true;
4113 }
4114 if (seq < r.lastProcessedSeq) {
4115 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
4116 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
4117 return false;
4118 }
4119 r.lastProcessedSeq = seq;
4120 return true;
4121 }
4122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004124 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004125 if (r.stopped) {
4126 r.activity.performRestart();
4127 r.stopped = false;
4128 }
4129 }
4130
Romain Guy65b345f2011-07-27 18:51:50 -07004131 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004132 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07004133
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08004134 if (r == null) {
4135 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
4136 return;
4137 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004139 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004140 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004141 } else if (show && r.stopped) {
4142 // If we are getting ready to gc after going to the background, well
4143 // we are back active so skip it.
4144 unscheduleGcIdler();
4145
4146 r.activity.performRestart();
4147 r.stopped = false;
4148 }
4149 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07004150 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 TAG, "Handle window " + r + " visibility: " + show);
4152 updateVisibility(r, show);
4153 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004154 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004155 }
4156
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004157 // TODO: This method should be changed to use {@link #performStopActivityInner} to perform to
4158 // stop operation on the activity to reduce code duplication and the chance of fixing a bug in
4159 // one place and missing the other.
Romain Guy65b345f2011-07-27 18:51:50 -07004160 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004161 ActivityClientRecord r = mActivities.get(token);
4162
4163 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004164 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004165 return;
4166 }
4167
4168 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004169 if (!r.stopped && !r.isPreHoneycomb()) {
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004170 if (!r.activity.mFinished && r.state == null) {
4171 callCallActivityOnSaveInstanceState(r);
4172 }
4173
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004174 try {
4175 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004176 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004177 } catch (Exception e) {
4178 if (!mInstrumentation.onException(r.activity, e)) {
4179 throw new RuntimeException(
4180 "Unable to stop activity "
4181 + r.intent.getComponent().toShortString()
4182 + ": " + e.toString(), e);
4183 }
4184 }
4185 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004186 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4187 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004188 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004189
4190 // Make sure any pending writes are now committed.
4191 if (!r.isPreHoneycomb()) {
4192 QueuedWork.waitToFinish();
4193 }
4194
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004195 // Tell activity manager we slept.
4196 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004197 ActivityManager.getService().activitySlept(r.token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004198 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004199 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004200 }
4201 } else {
4202 if (r.stopped && r.activity.mVisibleFromServer) {
4203 r.activity.performRestart();
4204 r.stopped = false;
4205 }
4206 }
4207 }
4208
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004209 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07004210 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004211 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004212 }
Jon Miranda836c0a82014-08-11 12:32:26 -07004213 onCoreSettingsChange();
4214 }
4215
4216 private void onCoreSettingsChange() {
4217 boolean debugViewAttributes =
4218 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4219 if (debugViewAttributes != View.mDebugViewAttributes) {
4220 View.mDebugViewAttributes = debugViewAttributes;
4221
4222 // request all activities to relaunch for the changes to take place
Fyodor Kupolovcb93d6f2017-05-04 17:27:16 -07004223 requestRelaunchAllActivities();
4224 }
4225 }
4226
4227 private void requestRelaunchAllActivities() {
4228 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
4229 final Activity activity = entry.getValue().activity;
4230 if (!activity.mFinished) {
4231 try {
4232 ActivityManager.getService().requestActivityRelaunch(entry.getKey());
4233 } catch (RemoteException e) {
4234 throw e.rethrowFromSystemServer();
4235 }
Jon Miranda836c0a82014-08-11 12:32:26 -07004236 }
4237 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004238 }
4239
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004240 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4241 LoadedApk apk = peekPackageInfo(data.pkg, false);
4242 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004243 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004244 }
4245 apk = peekPackageInfo(data.pkg, true);
4246 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004247 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004248 }
4249 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004250 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004251 }
4252
Romain Guy65b345f2011-07-27 18:51:50 -07004253 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254 final int N = results.size();
4255 for (int i=0; i<N; i++) {
4256 ResultInfo ri = results.get(i);
4257 try {
4258 if (ri.mData != null) {
4259 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004260 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004261 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004262 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004263 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004264 r.activity.dispatchActivityResult(ri.mResultWho,
4265 ri.mRequestCode, ri.mResultCode, ri.mData);
4266 } catch (Exception e) {
4267 if (!mInstrumentation.onException(r.activity, e)) {
4268 throw new RuntimeException(
4269 "Failure delivering result " + ri + " to activity "
4270 + r.intent.getComponent().toShortString()
4271 + ": " + e.toString(), e);
4272 }
4273 }
4274 }
4275 }
4276
Romain Guy65b345f2011-07-27 18:51:50 -07004277 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004278 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004279 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 if (r != null) {
4281 final boolean resumed = !r.paused;
4282 if (!r.activity.mFinished && r.activity.mDecor != null
4283 && r.hideForNow && resumed) {
4284 // We had hidden the activity because it started another
4285 // one... we have gotten a result back and we are not
4286 // paused, so make sure our window is visible.
4287 updateVisibility(r, true);
4288 }
4289 if (resumed) {
4290 try {
4291 // Now we are idle.
4292 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004293 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294 mInstrumentation.callActivityOnPause(r.activity);
4295 if (!r.activity.mCalled) {
4296 throw new SuperNotCalledException(
4297 "Activity " + r.intent.getComponent().toShortString()
4298 + " did not call through to super.onPause()");
4299 }
4300 } catch (SuperNotCalledException e) {
4301 throw e;
4302 } catch (Exception e) {
4303 if (!mInstrumentation.onException(r.activity, e)) {
4304 throw new RuntimeException(
4305 "Unable to pause activity "
4306 + r.intent.getComponent().toShortString()
4307 + ": " + e.toString(), e);
4308 }
4309 }
4310 }
Sudheer Shanka43b5ea42017-06-21 11:24:53 -07004311 checkAndBlockForNetworkAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312 deliverResults(r, res.results);
4313 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004314 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004315 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004316 }
4317 }
4318 }
4319
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004320 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 return performDestroyActivity(token, finishing, 0, false);
4322 }
4323
Romain Guy65b345f2011-07-27 18:51:50 -07004324 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004325 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004326 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004327 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004328 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004330 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004331 r.activity.mConfigChangeFlags |= configChanges;
4332 if (finishing) {
4333 r.activity.mFinished = true;
4334 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004335
4336 performPauseActivityIfNeeded(r, "destroy");
4337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004338 if (!r.stopped) {
4339 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004340 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341 } catch (SuperNotCalledException e) {
4342 throw e;
4343 } catch (Exception e) {
4344 if (!mInstrumentation.onException(r.activity, e)) {
4345 throw new RuntimeException(
4346 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004347 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348 + ": " + e.toString(), e);
4349 }
4350 }
4351 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004352 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4353 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004354 }
4355 if (getNonConfigInstance) {
4356 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004357 r.lastNonConfigurationInstances
4358 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 } catch (Exception e) {
4360 if (!mInstrumentation.onException(r.activity, e)) {
4361 throw new RuntimeException(
4362 "Unable to retain activity "
4363 + r.intent.getComponent().toShortString()
4364 + ": " + e.toString(), e);
4365 }
4366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004367 }
4368 try {
4369 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004370 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 if (!r.activity.mCalled) {
4372 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004373 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004374 " did not call through to super.onDestroy()");
4375 }
4376 if (r.window != null) {
4377 r.window.closeAllPanels();
4378 }
4379 } catch (SuperNotCalledException e) {
4380 throw e;
4381 } catch (Exception e) {
4382 if (!mInstrumentation.onException(r.activity, e)) {
4383 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004384 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4385 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004386 }
4387 }
4388 }
4389 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004390 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004391 return r;
4392 }
4393
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004394 private static String safeToComponentShortString(Intent intent) {
4395 ComponentName component = intent.getComponent();
4396 return component == null ? "[Unknown]" : component.toShortString();
4397 }
4398
Romain Guy65b345f2011-07-27 18:51:50 -07004399 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004401 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 configChanges, getNonConfigInstance);
4403 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004404 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 WindowManager wm = r.activity.getWindowManager();
4406 View v = r.activity.mDecor;
4407 if (v != null) {
4408 if (r.activity.mVisibleFromServer) {
4409 mNumVisibleActivities--;
4410 }
4411 IBinder wtoken = v.getWindowToken();
4412 if (r.activity.mWindowAdded) {
Robert Carr77bdfb52016-05-02 18:18:31 -07004413 if (r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004414 // Hold off on removing this until the new activity's
4415 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004416 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004417 r.mPendingRemoveWindowManager = wm;
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004418 // We can only keep the part of the view hierarchy that we control,
4419 // everything else must be removed, because it might not be able to
4420 // behave properly when activity is relaunching.
4421 r.window.clearContentView();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004422 } else {
4423 wm.removeViewImmediate(v);
4424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004425 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004426 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004427 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004428 r.activity.getClass().getName(), "Activity");
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004429 } else if (r.mPendingRemoveWindow != null) {
4430 // We're preserving only one window, others should be closed so app views
4431 // will be detached before the final tear down. It should be done now because
4432 // some components (e.g. WebView) rely on detach callbacks to perform receiver
4433 // unregister and other cleanup.
4434 WindowManagerGlobal.getInstance().closeAllExceptView(token, v,
4435 r.activity.getClass().getName(), "Activity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 }
4437 r.activity.mDecor = null;
4438 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004439 if (r.mPendingRemoveWindow == null) {
4440 // If we are delaying the removal of the activity window, then
4441 // we can't clean up all windows here. Note that we can't do
4442 // so later either, which means any windows that aren't closed
4443 // by the app will leak. Well we try to warning them a lot
4444 // about leaking windows, because that is a bug, so if they are
4445 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004446 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004447 r.activity.getClass().getName(), "Activity");
4448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449
4450 // Mocked out contexts won't be participating in the normal
4451 // process lifecycle, but if we're running with a proper
4452 // ApplicationContext we need to have it tear down things
4453 // cleanly.
4454 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004455 if (c instanceof ContextImpl) {
4456 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004457 r.activity.getClass().getName(), "Activity");
4458 }
4459 }
4460 if (finishing) {
4461 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004462 ActivityManager.getService().activityDestroyed(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004463 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004464 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
4466 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004467 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 }
4469
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004470 /**
4471 * @param preserveWindow Whether the activity should try to reuse the window it created,
4472 * including the decor view after the relaunch.
4473 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004474 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004475 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004476 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004477 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004478 ActivityClientRecord target = null;
4479
Craig Mautner88c05892013-06-28 09:47:45 -07004480 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004481 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4482 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004483 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004484 if (r.token == token) {
4485 target = r;
4486 if (pendingResults != null) {
4487 if (r.pendingResults != null) {
4488 r.pendingResults.addAll(pendingResults);
4489 } else {
4490 r.pendingResults = pendingResults;
4491 }
4492 }
4493 if (pendingNewIntents != null) {
4494 if (r.pendingIntents != null) {
4495 r.pendingIntents.addAll(pendingNewIntents);
4496 } else {
4497 r.pendingIntents = pendingNewIntents;
4498 }
4499 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004500
4501 // For each relaunch request, activity manager expects an answer
4502 if (!r.onlyLocalRequest && fromServer) {
4503 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004504 ActivityManager.getService().activityRelaunched(token);
Jorim Jaggife89d122015-12-22 16:28:44 +01004505 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004506 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004507 }
4508 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004509 break;
4510 }
4511 }
4512
4513 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004514 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4515 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004516 target = new ActivityClientRecord();
4517 target.token = token;
4518 target.pendingResults = pendingResults;
4519 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004520 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004521 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004522 final ActivityClientRecord existing = mActivities.get(token);
4523 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004524 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004525 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4526 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004527 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004528 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004529 }
4530 target.onlyLocalRequest = true;
4531 }
4532 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004533 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004534 }
4535
4536 if (fromServer) {
4537 target.startsNotResumed = notResumed;
4538 target.onlyLocalRequest = false;
4539 }
4540 if (config != null) {
4541 target.createdConfig = config;
4542 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004543 if (overrideConfig != null) {
4544 target.overrideConfig = overrideConfig;
4545 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004546 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004547 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004548 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004549 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4550 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004551 }
4552
Romain Guy65b345f2011-07-27 18:51:50 -07004553 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 // If we are getting ready to gc after going to the background, well
4555 // we are back active so skip it.
4556 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004557 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004558
4559 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004560 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 // First: make sure we have the most recent configuration and most
4563 // recent version of the activity, or skip it if some previous call
4564 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004565 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566 int N = mRelaunchingActivities.size();
4567 IBinder token = tmp.token;
4568 tmp = null;
4569 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004570 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 if (r.token == token) {
4572 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004573 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 mRelaunchingActivities.remove(i);
4575 i--;
4576 N--;
4577 }
4578 }
Bob Leee5408332009-09-04 18:31:17 -07004579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004581 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 return;
4583 }
Bob Leee5408332009-09-04 18:31:17 -07004584
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004585 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4586 + tmp.token + " with configChanges=0x"
4587 + Integer.toHexString(configChanges));
4588
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004589 if (mPendingConfiguration != null) {
4590 changedConfig = mPendingConfiguration;
4591 mPendingConfiguration = null;
4592 }
4593 }
Bob Leee5408332009-09-04 18:31:17 -07004594
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004595 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4596 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4597 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4598 } else {
4599 tmp.lastProcessedSeq = tmp.relaunchSeq;
4600 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004601 if (tmp.createdConfig != null) {
4602 // If the activity manager is passing us its current config,
4603 // assume that is really what we want regardless of what we
4604 // may have pending.
4605 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004606 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4607 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4608 if (changedConfig == null
4609 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4610 changedConfig = tmp.createdConfig;
4611 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004612 }
4613 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004614
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004615 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004616 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004618 // If there was a pending configuration change, execute it first.
4619 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004620 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004621 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004622 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 }
Bob Leee5408332009-09-04 18:31:17 -07004624
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004625 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004626 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004628 if (!tmp.onlyLocalRequest) {
4629 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004630 ActivityManager.getService().activityRelaunched(tmp.token);
Jorim Jaggife89d122015-12-22 16:28:44 +01004631 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004632 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004633 }
4634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004635 return;
4636 }
Bob Leee5408332009-09-04 18:31:17 -07004637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004639 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004640 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004641 r.lastProcessedSeq = tmp.lastProcessedSeq;
4642 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004643 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004644
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004645 r.activity.mChangingConfigurations = true;
4646
Robert Carr23fa16b2016-01-13 13:19:58 -08004647 // If we are preserving the main window across relaunches we would also like to preserve
4648 // the children. However the client side view system does not support preserving
4649 // the child views so we notify the window manager to expect these windows to
4650 // be replaced and defer requests to destroy or hide them. This way we can achieve
4651 // visual continuity. It's important that we do this here prior to pause and destroy
4652 // as that is when we may hide or remove the child views.
Robert Carr77bdfb52016-05-02 18:18:31 -07004653 //
4654 // There is another scenario, if we have decided locally to relaunch the app from a
4655 // call to recreate, then none of the windows will be prepared for replacement or
4656 // preserved by the server, so we want to notify it that we are preparing to replace
4657 // everything
Robert Carr23fa16b2016-01-13 13:19:58 -08004658 try {
Robert Carr77bdfb52016-05-02 18:18:31 -07004659 if (r.mPreserveWindow || r.onlyLocalRequest) {
4660 WindowManagerGlobal.getWindowSession().prepareToReplaceWindows(
4661 r.token, !r.onlyLocalRequest);
Robert Carr23fa16b2016-01-13 13:19:58 -08004662 }
4663 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004664 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004665 }
4666
Dianne Hackborne2b04802010-12-09 09:24:55 -08004667 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004668 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004669 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004670 }
4671 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004672 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004673 }
Bob Leee5408332009-09-04 18:31:17 -07004674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004675 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 r.activity = null;
4678 r.window = null;
4679 r.hideForNow = false;
4680 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004681 // Merge any pending results and pending intents; don't just replace them
4682 if (tmp.pendingResults != null) {
4683 if (r.pendingResults == null) {
4684 r.pendingResults = tmp.pendingResults;
4685 } else {
4686 r.pendingResults.addAll(tmp.pendingResults);
4687 }
4688 }
4689 if (tmp.pendingIntents != null) {
4690 if (r.pendingIntents == null) {
4691 r.pendingIntents = tmp.pendingIntents;
4692 } else {
4693 r.pendingIntents.addAll(tmp.pendingIntents);
4694 }
4695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004697 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004698
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004699 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004700
4701 if (!tmp.onlyLocalRequest) {
4702 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08004703 ActivityManager.getService().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004704 if (r.window != null) {
4705 r.window.reportActivityRelaunched();
4706 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004707 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004708 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004709 }
4710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 }
4712
Craig Mautnera0026042014-04-23 11:45:37 -07004713 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4714 r.state = new Bundle();
4715 r.state.setAllowFds(false);
4716 if (r.isPersistable()) {
4717 r.persistentState = new PersistableBundle();
4718 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4719 r.persistentState);
4720 } else {
4721 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4722 }
4723 }
4724
Dianne Hackborn73c14162012-09-19 15:45:06 -07004725 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004727 ArrayList<ComponentCallbacks2> callbacks
4728 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004729
Craig Mautner88c05892013-06-28 09:47:45 -07004730 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004731 final int NAPP = mAllApplications.size();
4732 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004733 callbacks.add(mAllApplications.get(i));
4734 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004735 final int NACT = mActivities.size();
4736 for (int i=0; i<NACT; i++) {
4737 ActivityClientRecord ar = mActivities.valueAt(i);
4738 Activity a = ar.activity;
4739 if (a != null) {
4740 Configuration thisConfig = applyConfigCompatMainThread(
4741 mCurDefaultDisplayDpi, newConfig,
4742 ar.packageInfo.getCompatibilityInfo());
4743 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4744 // If the activity is currently resumed, its configuration
4745 // needs to change right now.
4746 callbacks.add(a);
4747 } else if (thisConfig != null) {
4748 // Otherwise, we will tell it about the change
4749 // the next time it is resumed or shown. Note that
4750 // the activity manager may, before then, decide the
4751 // activity needs to be destroyed to handle its new
4752 // configuration.
4753 if (DEBUG_CONFIGURATION) {
4754 Slog.v(TAG, "Setting activity "
4755 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004756 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004757 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004758 }
4759 }
4760 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004761 final int NSVC = mServices.size();
4762 for (int i=0; i<NSVC; i++) {
4763 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004764 }
4765 }
4766 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004767 final int NPRV = mLocalProviders.size();
4768 for (int i=0; i<NPRV; i++) {
4769 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 }
4771 }
Bob Leee5408332009-09-04 18:31:17 -07004772
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004773 return callbacks;
4774 }
Bob Leee5408332009-09-04 18:31:17 -07004775
Adam Lesinski082614c2016-03-04 14:33:47 -08004776 /**
4777 * Updates the configuration for an Activity. The ActivityClientRecord's
4778 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4779 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4780 * the updated Configuration.
4781 * @param r ActivityClientRecord representing the Activity.
4782 * @param newBaseConfig The new configuration to use. This may be augmented with
4783 * {@link ActivityClientRecord#overrideConfig}.
Adam Lesinski082614c2016-03-04 14:33:47 -08004784 */
4785 private void performConfigurationChangedForActivity(ActivityClientRecord r,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004786 Configuration newBaseConfig) {
4787 performConfigurationChangedForActivity(r, newBaseConfig,
4788 r.activity.getDisplay().getDisplayId(), false /* movedToDifferentDisplay */);
4789 }
4790
4791 /**
4792 * Updates the configuration for an Activity. The ActivityClientRecord's
4793 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4794 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4795 * the updated Configuration.
4796 * @param r ActivityClientRecord representing the Activity.
4797 * @param newBaseConfig The new configuration to use. This may be augmented with
4798 * {@link ActivityClientRecord#overrideConfig}.
4799 * @param displayId The id of the display where the Activity currently resides.
4800 * @param movedToDifferentDisplay Indicates if the activity was moved to different display.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004801 * @return {@link Configuration} instance sent to client, null if not sent.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004802 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004803 private Configuration performConfigurationChangedForActivity(ActivityClientRecord r,
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004804 Configuration newBaseConfig, int displayId, boolean movedToDifferentDisplay) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004805 r.tmpConfig.setTo(newBaseConfig);
4806 if (r.overrideConfig != null) {
4807 r.tmpConfig.updateFrom(r.overrideConfig);
4808 }
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004809 final Configuration reportedConfig = performActivityConfigurationChanged(r.activity,
4810 r.tmpConfig, r.overrideConfig, displayId, movedToDifferentDisplay);
Adam Lesinski082614c2016-03-04 14:33:47 -08004811 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004812 return reportedConfig;
Adam Lesinski082614c2016-03-04 14:33:47 -08004813 }
4814
4815 /**
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004816 * Creates a new Configuration only if override would modify base. Otherwise returns base.
4817 * @param base The base configuration.
4818 * @param override The update to apply to the base configuration. Can be null.
4819 * @return A Configuration representing base with override applied.
4820 */
4821 private static Configuration createNewConfigAndUpdateIfNotNull(@NonNull Configuration base,
4822 @Nullable Configuration override) {
4823 if (override == null) {
4824 return base;
4825 }
4826 Configuration newConfig = new Configuration(base);
4827 newConfig.updateFrom(override);
4828 return newConfig;
4829 }
4830
4831 /**
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004832 * Decides whether to update a component's configuration and whether to inform it.
Adam Lesinski082614c2016-03-04 14:33:47 -08004833 * @param cb The component callback to notify of configuration change.
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004834 * @param newConfig The new configuration.
4835 */
4836 private void performConfigurationChanged(ComponentCallbacks2 cb, Configuration newConfig) {
4837 if (!REPORT_TO_ACTIVITY) {
4838 return;
4839 }
4840
4841 // ContextThemeWrappers may override the configuration for that context. We must check and
4842 // apply any overrides defined.
4843 Configuration contextThemeWrapperOverrideConfig = null;
4844 if (cb instanceof ContextThemeWrapper) {
4845 final ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
4846 contextThemeWrapperOverrideConfig = contextThemeWrapper.getOverrideConfiguration();
4847 }
4848
4849 // Apply the ContextThemeWrapper override if necessary.
4850 // NOTE: Make sure the configurations are not modified, as they are treated as immutable
4851 // in many places.
4852 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(
4853 newConfig, contextThemeWrapperOverrideConfig);
4854 cb.onConfigurationChanged(configToReport);
4855 }
4856
4857 /**
4858 * Decides whether to update an Activity's configuration and whether to inform it.
4859 * @param activity The activity to notify of configuration change.
Adam Lesinski082614c2016-03-04 14:33:47 -08004860 * @param newConfig The new configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004861 * @param amOverrideConfig The override config that differentiates the Activity's configuration
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004862 * from the base global configuration. This is supplied by
4863 * ActivityManager.
4864 * @param displayId Id of the display where activity currently resides.
4865 * @param movedToDifferentDisplay Indicates if the activity was moved to different display.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004866 * @return Configuration sent to client, null if no changes and not moved to different display.
Adam Lesinski082614c2016-03-04 14:33:47 -08004867 */
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004868 private Configuration performActivityConfigurationChanged(Activity activity,
4869 Configuration newConfig, Configuration amOverrideConfig, int displayId,
4870 boolean movedToDifferentDisplay) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004871 if (activity == null) {
4872 throw new IllegalArgumentException("No activity provided.");
4873 }
4874 final IBinder activityToken = activity.getActivityToken();
4875 if (activityToken == null) {
4876 throw new IllegalArgumentException("Activity token not set. Is the activity attached?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004877 }
Bob Leee5408332009-09-04 18:31:17 -07004878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004879 boolean shouldChangeConfig = false;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004880 if (activity.mCurrentConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881 shouldChangeConfig = true;
4882 } else {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004883 // If the new config is the same as the config this Activity is already running with and
4884 // the override config also didn't change, then don't bother calling
4885 // onConfigurationChanged.
Bryce Lee658d9842017-07-28 08:33:36 -07004886 final int diff = activity.mCurrentConfig.diffPublicOnly(newConfig);
4887
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004888 if (diff != 0 || !mResourcesManager.isSameResourcesOverrideConfig(activityToken,
4889 amOverrideConfig)) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004890 // Always send the task-level config changes. For system-level configuration, if
4891 // this activity doesn't handle any of the config changes, then don't bother
4892 // calling onConfigurationChanged as we're going to destroy it.
4893 if (!mUpdatingSystemConfig
4894 || (~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004895 || !REPORT_TO_ACTIVITY) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004896 shouldChangeConfig = true;
4897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004898 }
4899 }
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004900 if (!shouldChangeConfig && !movedToDifferentDisplay) {
4901 // Nothing significant, don't proceed with updating and reporting.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004902 return null;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004903 }
4904
4905 // Propagate the configuration change to ResourcesManager and Activity.
4906
4907 // ContextThemeWrappers may override the configuration for that context. We must check and
4908 // apply any overrides defined.
4909 Configuration contextThemeWrapperOverrideConfig = activity.getOverrideConfiguration();
4910
4911 // We only update an Activity's configuration if this is not a global configuration change.
4912 // This must also be done before the callback, or else we violate the contract that the new
4913 // resources are available in ComponentCallbacks2#onConfigurationChanged(Configuration).
4914 // Also apply the ContextThemeWrapper override if necessary.
4915 // NOTE: Make sure the configurations are not modified, as they are treated as immutable in
4916 // many places.
4917 final Configuration finalOverrideConfig = createNewConfigAndUpdateIfNotNull(
4918 amOverrideConfig, contextThemeWrapperOverrideConfig);
4919 mResourcesManager.updateResourcesForActivity(activityToken, finalOverrideConfig,
4920 displayId, movedToDifferentDisplay);
4921
4922 activity.mConfigChangeFlags = 0;
4923 activity.mCurrentConfig = new Configuration(newConfig);
4924
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004925 // Apply the ContextThemeWrapper override if necessary.
4926 // NOTE: Make sure the configurations are not modified, as they are treated as immutable
4927 // in many places.
4928 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(newConfig,
4929 contextThemeWrapperOverrideConfig);
4930
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004931 if (!REPORT_TO_ACTIVITY) {
4932 // Not configured to report to activity.
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004933 return configToReport;
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004934 }
4935
4936 if (movedToDifferentDisplay) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004937 activity.dispatchMovedToDisplay(displayId, configToReport);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004938 }
Bob Leee5408332009-09-04 18:31:17 -07004939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940 if (shouldChangeConfig) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08004941 activity.mCalled = false;
4942 activity.onConfigurationChanged(configToReport);
4943 if (!activity.mCalled) {
4944 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
4945 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004946 }
4947 }
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07004948
4949 return configToReport;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004950 }
4951
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004952 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004953 synchronized (mResourcesManager) {
4954 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004955 }
4956 }
4957
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004958 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004959 Configuration config = mConfiguration;
4960 if (mCompatConfiguration == null) {
4961 mCompatConfiguration = new Configuration();
4962 }
4963 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004964 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4965 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004966 config = mCompatConfiguration;
4967 }
4968 return config;
4969 }
4970
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004971 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004972
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004973 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004974
Bryce Lee658d9842017-07-28 08:33:36 -07004975 // This flag tracks whether the new configuration is fundamentally equivalent to the
4976 // existing configuration. This is necessary to determine whether non-activity
4977 // callbacks should receive notice when the only changes are related to non-public fields.
4978 // We do not gate calling {@link #performActivityConfigurationChanged} based on this flag
4979 // as that method uses the same check on the activity config override as well.
4980 final boolean equivalent = config != null && mConfiguration != null
4981 && (0 == mConfiguration.diffPublicOnly(config));
4982
Craig Mautner88c05892013-06-28 09:47:45 -07004983 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004984 if (mPendingConfiguration != null) {
4985 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4986 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004987 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004988 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004989 }
4990 mPendingConfiguration = null;
4991 }
4992
4993 if (config == null) {
4994 return;
4995 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004996
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004997 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004998 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004999
5000 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005001 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
5002 mResourcesManager.getConfiguration().getLocales());
Craig Mautner88c05892013-06-28 09:47:45 -07005003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 if (mConfiguration == null) {
5005 mConfiguration = new Configuration();
5006 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005007 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005008 return;
5009 }
Alan Viverettee54d2452015-05-06 10:41:43 -07005010
5011 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07005012 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04005013
5014 final Theme systemTheme = getSystemContext().getTheme();
5015 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
5016 systemTheme.rebase();
5017 }
Adam Lesinskia82b6262017-03-21 16:56:17 -07005018
5019 final Theme systemUiTheme = getSystemUiContext().getTheme();
5020 if ((systemUiTheme.getChangingConfigurations() & configDiff) != 0) {
5021 systemUiTheme.rebase();
5022 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07005024
5025 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
5026
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005027 freeTextLayoutCachesIfNeeded(configDiff);
5028
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005029 if (callbacks != null) {
5030 final int N = callbacks.size();
5031 for (int i=0; i<N; i++) {
Andrii Kulian701214b2016-04-07 09:36:33 -07005032 ComponentCallbacks2 cb = callbacks.get(i);
5033 if (cb instanceof Activity) {
5034 // If callback is an Activity - call corresponding method to consider override
5035 // config and avoid onConfigurationChanged if it hasn't changed.
5036 Activity a = (Activity) cb;
5037 performConfigurationChangedForActivity(mActivities.get(a.getActivityToken()),
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005038 config);
Bryce Lee658d9842017-07-28 08:33:36 -07005039 } else if (!equivalent) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005040 performConfigurationChanged(cb, config);
Andrii Kulian701214b2016-04-07 09:36:33 -07005041 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005042 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 }
5044 }
5045
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005046 void handleApplicationInfoChanged(@NonNull final ApplicationInfo ai) {
Adam Lesinskid33ef562017-01-19 14:49:59 -08005047 // Updates triggered by package installation go through a package update
5048 // receiver. Here we try to capture ApplicationInfo changes that are
5049 // caused by other sources, such as overlays. That means we want to be as conservative
5050 // about code changes as possible. Take the diff of the old ApplicationInfo and the new
5051 // to see if anything needs to change.
Fyodor Kupolovb7865ce2017-05-05 15:08:03 -07005052 LoadedApk apk;
5053 LoadedApk resApk;
5054 // Update all affected loaded packages with new package information
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005055 synchronized (mResourcesManager) {
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005056 WeakReference<LoadedApk> ref = mPackages.get(ai.packageName);
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005057 apk = ref != null ? ref.get() : null;
Fyodor Kupolovb7865ce2017-05-05 15:08:03 -07005058 ref = mResourcePackages.get(ai.packageName);
5059 resApk = ref != null ? ref.get() : null;
5060 }
5061 if (apk != null) {
5062 final ArrayList<String> oldPaths = new ArrayList<>();
5063 LoadedApk.makePaths(this, apk.getApplicationInfo(), oldPaths);
5064 apk.updateApplicationInfo(ai, oldPaths);
5065 }
5066 if (resApk != null) {
5067 final ArrayList<String> oldPaths = new ArrayList<>();
5068 LoadedApk.makePaths(this, resApk.getApplicationInfo(), oldPaths);
5069 resApk.updateApplicationInfo(ai, oldPaths);
5070 }
5071 synchronized (mResourcesManager) {
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005072 // Update all affected Resources objects to use new ResourcesImpl
5073 mResourcesManager.applyNewResourceDirsLocked(ai.sourceDir, ai.resourceDirs);
5074 }
5075
5076 ApplicationPackageManager.configurationChanged();
5077
5078 // Trigger a regular Configuration change event, only with a different assetsSeq number
5079 // so that we actually call through to all components.
Adam Lesinskia82b6262017-03-21 16:56:17 -07005080 // TODO(adamlesinski): Change this to make use of ActivityManager's upcoming ability to
5081 // store configurations per-process.
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005082 Configuration newConfig = new Configuration();
Adam Lesinskia82b6262017-03-21 16:56:17 -07005083 newConfig.assetsSeq = (mConfiguration != null ? mConfiguration.assetsSeq : 0) + 1;
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005084 handleConfigurationChanged(newConfig, null);
5085
Fyodor Kupolovcb93d6f2017-05-04 17:27:16 -07005086 requestRelaunchAllActivities();
MÃ¥rten Kongstad49a4a1d2017-01-12 08:36:37 +01005087 }
5088
Romain Guy46bfc482013-08-16 18:38:29 -07005089 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005090 if (configDiff != 0) {
5091 // Ask text layout engine to free its caches if there is a locale change
5092 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
5093 if (hasLocaleConfigChange) {
5094 Canvas.freeTextLayoutCaches();
5095 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
5096 }
5097 }
5098 }
5099
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005100 /**
5101 * Handle new activity configuration and/or move to a different display.
5102 * @param data Configuration update data.
5103 * @param displayId Id of the display where activity was moved to, -1 if there was no move and
5104 * value didn't change.
5105 */
Andrii Kulian44607962017-03-16 11:06:24 -07005106 void handleActivityConfigurationChanged(ActivityConfigChangeData data, int displayId) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08005107 ActivityClientRecord r = mActivities.get(data.activityToken);
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005108 // Check input params.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005109 if (r == null || r.activity == null) {
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005110 if (DEBUG_CONFIGURATION) Slog.w(TAG, "Not found target activity to report to: " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005111 return;
5112 }
Andrii Kulian51c1b672017-04-07 18:39:32 -07005113 final boolean movedToDifferentDisplay = displayId != INVALID_DISPLAY
5114 && displayId != r.activity.getDisplay().getDisplayId();
Bob Leee5408332009-09-04 18:31:17 -07005115
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005116 // Perform updates.
Adam Lesinski082614c2016-03-04 14:33:47 -08005117 r.overrideConfig = data.overrideConfig;
Stephen Kiazykf903b732017-03-20 16:23:45 -07005118 final ViewRootImpl viewRoot = r.activity.mDecor != null
5119 ? r.activity.mDecor.getViewRootImpl() : null;
5120
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005121 if (movedToDifferentDisplay) {
5122 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity moved to display, activity:"
5123 + r.activityInfo.name + ", displayId=" + displayId
5124 + ", config=" + data.overrideConfig);
5125
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005126 final Configuration reportedConfig = performConfigurationChangedForActivity(r,
5127 mCompatConfiguration, displayId, true /* movedToDifferentDisplay */);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005128 if (viewRoot != null) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005129 viewRoot.onMovedToDisplay(displayId, reportedConfig);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005130 }
Andrii Kulianb047b8b2017-02-08 18:38:26 -08005131 } else {
5132 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
5133 + r.activityInfo.name + ", config=" + data.overrideConfig);
5134 performConfigurationChangedForActivity(r, mCompatConfiguration);
5135 }
Andrii Kulian44607962017-03-16 11:06:24 -07005136 // Notify the ViewRootImpl instance about configuration changes. It may have initiated this
5137 // update to make sure that resources are updated before updating itself.
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005138 if (viewRoot != null) {
Andrii Kuliane5c58ee2017-03-27 19:25:24 -07005139 viewRoot.updateConfiguration(displayId);
Andrii Kuliance1ecf12017-03-17 14:58:36 -07005140 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005141 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005142 }
5143
Jeff Hao1b012d32014-08-20 10:35:34 -07005144 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005145 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005146 try {
Romain Guy7eabe552011-07-21 14:56:34 -07005147 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005148 default:
5149 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005150 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07005151 break;
5152 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005153 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07005154 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005155 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07005156 } finally {
Andreas Gampe2b073a02017-06-22 17:28:57 -07005157 profilerInfo.closeFd();
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005158 }
5159 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07005160 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07005161 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005162 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07005163 break;
Romain Guy7eabe552011-07-21 14:56:34 -07005164 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08005165 }
5166 }
Bob Leee5408332009-09-04 18:31:17 -07005167
Andreas Gampe4c79fea2016-01-28 20:11:41 -08005168 /**
5169 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
5170 * so that profiler data won't be lost.
5171 *
5172 * @hide
5173 */
5174 public void stopProfiling() {
Amith Yamasanib1684432017-01-26 14:57:20 -08005175 if (mProfiler != null) {
5176 mProfiler.stopProfiling();
5177 }
Andreas Gampe4c79fea2016-01-28 20:11:41 -08005178 }
5179
Christopher Ferris8d652f82017-04-11 16:29:18 -07005180 static void handleDumpHeap(DumpHeapData dhd) {
Makoto Onuki4556b7b2017-07-07 14:58:58 -07005181 if (dhd.runGc) {
5182 System.gc();
5183 System.runFinalization();
5184 System.gc();
5185 }
Christopher Ferris8d652f82017-04-11 16:29:18 -07005186 if (dhd.managed) {
Andy McFadden824c5102010-07-09 16:26:57 -07005187 try {
5188 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
5189 } catch (IOException e) {
5190 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
5191 + " -- can the process access this path?");
5192 } finally {
5193 try {
5194 dhd.fd.close();
5195 } catch (IOException e) {
5196 Slog.w(TAG, "Failure closing profile fd", e);
5197 }
5198 }
Christopher Ferris8d652f82017-04-11 16:29:18 -07005199 } else if (dhd.mallocInfo) {
5200 Debug.dumpNativeMallocInfo(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07005201 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07005202 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07005203 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005204 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005205 ActivityManager.getService().dumpHeapFinished(dhd.path);
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005206 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005207 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08005208 }
Andy McFadden824c5102010-07-09 16:26:57 -07005209 }
5210
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005211 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
5212 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08005213 switch (cmd) {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005214 case ApplicationThreadConstants.PACKAGE_REMOVED:
5215 case ApplicationThreadConstants.PACKAGE_REMOVED_DONT_KILL:
Todd Kennedy39bfee52016-02-24 10:28:21 -08005216 {
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005217 final boolean killApp = cmd == ApplicationThreadConstants.PACKAGE_REMOVED;
Todd Kennedy39bfee52016-02-24 10:28:21 -08005218 if (packages == null) {
5219 break;
5220 }
5221 synchronized (mResourcesManager) {
5222 for (int i = packages.length - 1; i >= 0; i--) {
5223 if (!hasPkgInfo) {
5224 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
5225 if (ref != null && ref.get() != null) {
5226 hasPkgInfo = true;
5227 } else {
5228 ref = mResourcePackages.get(packages[i]);
5229 if (ref != null && ref.get() != null) {
5230 hasPkgInfo = true;
5231 }
5232 }
5233 }
5234 if (killApp) {
5235 mPackages.remove(packages[i]);
5236 mResourcePackages.remove(packages[i]);
5237 }
5238 }
5239 }
5240 break;
5241 }
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005242 case ApplicationThreadConstants.PACKAGE_REPLACED:
Todd Kennedy39bfee52016-02-24 10:28:21 -08005243 {
5244 if (packages == null) {
5245 break;
5246 }
5247 synchronized (mResourcesManager) {
5248 for (int i = packages.length - 1; i >= 0; i--) {
5249 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
5250 LoadedApk pkgInfo = ref != null ? ref.get() : null;
5251 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005252 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08005253 } else {
5254 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08005255 pkgInfo = ref != null ? ref.get() : null;
5256 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08005257 hasPkgInfo = true;
5258 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005259 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005260 // If the package is being replaced, yet it still has a valid
5261 // LoadedApk object, the package was updated with _DONT_KILL.
5262 // Adjust it's internal references to the application info and
5263 // resources.
5264 if (pkgInfo != null) {
5265 try {
5266 final String packageName = packages[i];
5267 final ApplicationInfo aInfo =
5268 sPackageManager.getApplicationInfo(
5269 packageName,
5270 0 /*flags*/,
5271 UserHandle.myUserId());
5272
5273 if (mActivities.size() > 0) {
5274 for (ActivityClientRecord ar : mActivities.values()) {
5275 if (ar.activityInfo.applicationInfo.packageName
5276 .equals(packageName)) {
5277 ar.activityInfo.applicationInfo = aInfo;
5278 ar.packageInfo = pkgInfo;
5279 }
5280 }
5281 }
5282 final List<String> oldPaths =
5283 sPackageManager.getPreviousCodePaths(packageName);
5284 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
5285 } catch (RemoteException e) {
5286 }
5287 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005288 }
5289 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005290 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005291 }
5292 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08005293 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07005294 }
Tim Murraye1e6c662015-04-07 13:24:14 -07005295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07005297 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07005298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 final int N = callbacks.size();
5300 for (int i=0; i<N; i++) {
5301 callbacks.get(i).onLowMemory();
5302 }
5303
Chris Tatece229052009-03-25 16:44:52 -07005304 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
5305 if (Process.myUid() != Process.SYSTEM_UID) {
5306 int sqliteReleased = SQLiteDatabase.releaseMemory();
5307 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
5308 }
Bob Leee5408332009-09-04 18:31:17 -07005309
Mike Reedcaf0df12009-04-27 14:32:05 -04005310 // Ask graphics to free up as much as possible (font/image caches)
5311 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005312
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07005313 // Ask text layout engine to free also as much as possible
5314 Canvas.freeTextLayoutCaches();
5315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005316 BinderInternal.forceGc("mem");
5317 }
5318
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005319 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07005320 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005321
Dianne Hackborn73c14162012-09-19 15:45:06 -07005322 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005323
5324 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07005325 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005326 callbacks.get(i).onTrimMemory(level);
5327 }
Romain Guy19f86e82012-04-23 15:19:07 -07005328
John Reckf47a5942014-06-30 16:20:04 -07005329 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005330 }
5331
Jesse Hall317fa5a2017-05-23 15:46:55 -07005332 private void setupGraphicsSupport(Context context) {
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005333 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupGraphicsSupport");
Romain Guya9582652011-11-10 14:20:10 -08005334
Jesse Hall317fa5a2017-05-23 15:46:55 -07005335 // The system package doesn't have real data directories, so don't set up cache paths.
5336 if (!"android".equals(context.getPackageName())) {
5337 // This cache location probably points at credential-encrypted
5338 // storage which may not be accessible yet; assign it anyway instead
5339 // of pointing at device-encrypted storage.
5340 final File cacheDir = context.getCacheDir();
5341 if (cacheDir != null) {
5342 // Provide a usable directory for temporary files
5343 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5344 } else {
5345 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5346 + "due to missing cache directory");
Romain Guya9582652011-11-10 14:20:10 -08005347 }
Jesse Hall317fa5a2017-05-23 15:46:55 -07005348
5349 // Setup a location to store generated/compiled graphics code.
5350 final Context deviceContext = context.createDeviceProtectedStorageContext();
5351 final File codeCacheDir = deviceContext.getCodeCacheDir();
5352 if (codeCacheDir != null) {
5353 try {
5354 int uid = Process.myUid();
5355 String[] packages = getPackageManager().getPackagesForUid(uid);
5356 if (packages != null) {
Romain Guycecbe072017-07-18 15:42:06 -07005357 ThreadedRenderer.setupDiskCache(codeCacheDir);
5358 RenderScriptCacheDir.setupDiskCache(codeCacheDir);
Jesse Hall317fa5a2017-05-23 15:46:55 -07005359 }
5360 } catch (RemoteException e) {
5361 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
5362 throw e.rethrowFromSystemServer();
5363 }
5364 } else {
5365 Log.w(TAG, "Unable to use shader/script cache: missing code-cache directory");
5366 }
Romain Guya9582652011-11-10 14:20:10 -08005367 }
Jesse Hall317fa5a2017-05-23 15:46:55 -07005368
Jesse Hallc37984f2017-05-23 16:55:08 -07005369 GraphicsEnvironment.chooseDriver(context);
Jesse Hall317fa5a2017-05-23 15:46:55 -07005370 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005371 }
5372
5373 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05005374 final int densityDpi = mCurDefaultDisplayDpi;
5375 if (!mDensityCompatMode
5376 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
5377 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
5378 DisplayMetrics.DENSITY_DEVICE = densityDpi;
5379 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005380 }
5381 }
5382
Todd Kennedye713efc2016-05-10 13:45:40 -07005383 /**
5384 * Returns the correct library directory for the current ABI.
5385 * <p>
5386 * If we're dealing with a multi-arch application that has both 32 and 64 bit shared
5387 * libraries, we might need to choose the secondary depending on what the current
5388 * runtime's instruction set is.
5389 */
5390 private String getInstrumentationLibrary(ApplicationInfo appInfo, InstrumentationInfo insInfo) {
5391 if (appInfo.primaryCpuAbi != null && appInfo.secondaryCpuAbi != null) {
5392 // Get the instruction set supported by the secondary ABI. In the presence
5393 // of a native bridge this might be different than the one secondary ABI used.
5394 String secondaryIsa =
5395 VMRuntime.getInstructionSet(appInfo.secondaryCpuAbi);
5396 final String secondaryDexCodeIsa =
5397 SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
5398 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
5399
5400 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
5401 if (runtimeIsa.equals(secondaryIsa)) {
5402 return insInfo.secondaryNativeLibraryDir;
5403 }
5404 }
5405 return insInfo.nativeLibraryDir;
5406 }
5407
Adam Lesinskib61e4052016-05-19 18:23:05 -07005408 /**
5409 * The LocaleList set for the app's resources may have been shuffled so that the preferred
5410 * Locale is at position 0. We must find the index of this preferred Locale in the
5411 * original LocaleList.
5412 */
5413 private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
5414 final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
5415 final int newLocaleListSize = newLocaleList.size();
5416 for (int i = 0; i < newLocaleListSize; i++) {
5417 if (bestLocale.equals(newLocaleList.get(i))) {
5418 LocaleList.setDefault(newLocaleList, i);
5419 return;
5420 }
5421 }
Adam Lesinski27d30162016-05-25 16:45:14 -07005422
5423 // The app may have overridden the LocaleList with its own Locale
5424 // (not present in the available list). Push the chosen Locale
5425 // to the front of the list.
5426 LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
Adam Lesinskib61e4052016-05-19 18:23:05 -07005427 }
5428
Romain Guy65b345f2011-07-27 18:51:50 -07005429 private void handleBindApplication(AppBindData data) {
Calin Juravle8f5770d2016-04-12 14:12:04 +01005430 // Register the UI Thread as a sensitive thread to the runtime.
5431 VMRuntime.registerSensitiveThread();
Man Caocfa78b22015-06-11 20:14:34 -07005432 if (data.trackAllocation) {
5433 DdmVmInternal.enableRecentAllocations(true);
5434 }
5435
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005436 // Note when this process has started.
5437 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 mBoundApplication = data;
5440 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005441 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005442
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005443 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005444 if (data.initProfilerInfo != null) {
5445 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5446 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5447 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5448 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
Shukang Zhou6ffd4f92017-01-25 16:07:57 -08005449 mProfiler.streamingOutput = data.initProfilerInfo.streamingOutput;
Jeff Hao1b012d32014-08-20 10:35:34 -07005450 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005453 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005454 android.ddm.DdmHandleAppName.setAppName(data.processName,
5455 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005456
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005457 if (mProfiler.profileFd != null) {
5458 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005459 }
5460
Joe Onoratod630f102011-03-17 18:42:26 -07005461 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5462 // implementation to use the pool executor. Normally, we use the
5463 // serialized executor as the default. This has to happen in the
5464 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005465 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005466 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5467 }
5468
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005469 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471 /*
5472 * Before spawning a new process, reset the time zone to be the system time zone.
5473 * This needs to be done because the system time zone could have changed after the
5474 * the spawning of this process. Without doing this this process would have the incorrect
5475 * system time zone.
5476 */
5477 TimeZone.setDefault(null);
5478
Adam Lesinskib61e4052016-05-19 18:23:05 -07005479 /*
5480 * Set the LocaleList. This may change once we create the App Context.
5481 */
5482 LocaleList.setDefault(data.config.getLocales());
5483
5484 synchronized (mResourcesManager) {
5485 /*
5486 * Update the system configuration since its preloaded and might not
5487 * reflect configuration changes. The configuration object passed
5488 * in AppBindData can be safely assumed to be up to date
5489 */
5490 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5491 mCurDefaultDisplayDpi = data.config.densityDpi;
5492
5493 // This calls mResourcesManager so keep it within the synchronized block.
5494 applyCompatConfiguration(mCurDefaultDisplayDpi);
5495 }
5496
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005497 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498
Dianne Hackborndde331c2012-08-03 14:01:57 -07005499 /**
5500 * Switch this process to density compatibility mode if needed.
5501 */
5502 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5503 == 0) {
5504 mDensityCompatMode = true;
5505 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5506 }
5507 updateDefaultDensity();
5508
Neil Fullerc66ec402016-11-14 16:06:42 +00005509 final String use24HourSetting = mCoreSettings.getString(Settings.System.TIME_12_24);
5510 Boolean is24Hr = null;
5511 if (use24HourSetting != null) {
5512 is24Hr = "24".equals(use24HourSetting) ? Boolean.TRUE : Boolean.FALSE;
5513 }
5514 // null : use locale default for 12/24 hour formatting,
5515 // false : use 12 hour format,
5516 // true : use 24 hour format.
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005517 DateFormat.set24HourTimePref(is24Hr);
5518
Jon Miranda836c0a82014-08-11 12:32:26 -07005519 View.mDebugViewAttributes =
5520 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5521
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005522 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005523 * For system applications on userdebug/eng builds, log stack
5524 * traces of disk and network access to dropbox for analysis.
5525 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005526 if ((data.appInfo.flags &
5527 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005528 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5529 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005530 }
5531
5532 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005533 * For apps targetting Honeycomb or later, we don't allow network usage
5534 * on the main event loop / UI thread. This is what ultimately throws
5535 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005536 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005537 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005538 StrictMode.enableDeathOnNetwork();
5539 }
5540
Jeff Sharkey344744b2016-01-28 19:03:30 -07005541 /**
5542 * For apps targetting N or later, we don't allow file:// Uri exposure.
5543 * This is what ultimately throws {@link FileUriExposedException}.
5544 */
5545 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005546 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005547 }
5548
Svet Ganov37e43272016-09-09 16:01:32 -07005549 // We deprecated Build.SERIAL and only apps that target pre NMR1
5550 // SDK can see it. Since access to the serial is now behind a
5551 // permission we push down the value and here we fix it up
5552 // before any app code has been loaded.
5553 try {
5554 Field field = Build.class.getDeclaredField("SERIAL");
5555 field.setAccessible(true);
5556 field.set(Build.class, data.buildSerial);
5557 } catch (NoSuchFieldException | IllegalAccessException e) {
5558 /* ignore */
5559 }
5560
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005561 if (data.debugMode != ApplicationThreadConstants.DEBUG_OFF) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005562 // XXX should have option to change the port.
5563 Debug.changeDebugPort(8100);
Sudheer Shankacc6418f2016-10-13 12:03:44 -07005564 if (data.debugMode == ApplicationThreadConstants.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005565 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005566 + " is waiting for the debugger on port 8100...");
5567
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005568 IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 try {
5570 mgr.showWaitingForDebugger(mAppThread, true);
5571 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005572 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005573 }
5574
5575 Debug.waitForDebugger();
5576
5577 try {
5578 mgr.showWaitingForDebugger(mAppThread, false);
5579 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005580 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 }
5582
5583 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005584 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005585 + " can be debugged on port 8100...");
5586 }
5587 }
5588
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005589 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005590 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5591 Trace.setAppTracingAllowed(isAppDebuggable);
5592 if (isAppDebuggable && data.enableBinderTracking) {
5593 Binder.enableTracing();
5594 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005595
Robert Greenwalt434203a2010-10-11 16:00:27 -07005596 /**
5597 * Initialize the default http proxy in this process for the reasons we set the time zone.
5598 */
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005599 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Setup proxies");
Alan Viverettebe64eae2015-09-03 14:56:04 -04005600 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005601 if (b != null) {
5602 // In pre-boot mode (doing initial launch to collect password), not
5603 // all system is up. This includes the connectivity service, so don't
5604 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005605 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005606 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005607 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005608 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005609 } catch (RemoteException e) {
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005610 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005611 throw e.rethrowFromSystemServer();
5612 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005613 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005614 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Robert Greenwalt434203a2010-10-11 16:00:27 -07005615
Alan Viverette2107d692015-09-03 14:55:27 -04005616 // Instrumentation info affects the class loader, so load it before
5617 // setting up the app context.
5618 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005619 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005620 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005621 ii = new ApplicationPackageManager(null, getPackageManager())
5622 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005623 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005624 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005625 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005626 }
5627
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005628 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005629 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005630 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
Todd Kennedye713efc2016-05-10 13:45:40 -07005631 mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005633 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5634 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005635 } else {
5636 ii = null;
5637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005638
Alan Viverette2107d692015-09-03 14:55:27 -04005639 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005640 updateLocaleListFromAppContext(appContext,
5641 mResourcesManager.getConfiguration().getLocales());
Seigo Nonakac14dd782016-03-30 23:09:16 +09005642
Jesse Hall317fa5a2017-05-23 15:46:55 -07005643 if (!Process.isIsolated()) {
5644 setupGraphicsSupport(appContext);
Michael Lentine2ba303f2016-02-01 20:44:34 -06005645 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005646
Calin Juravle37dfc8e2017-03-13 19:02:32 -07005647 // If we use profiles, setup the dex reporter to notify package manager
5648 // of any relevant dex loads. The idle maintenance job will use the information
5649 // reported to optimize the loaded dex files.
5650 // Note that we only need one global reporter per app.
5651 // Make sure we do this before calling onCreate so that we can capture the
5652 // complete application startup.
5653 if (SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
Calin Juravlef5a7bfc2017-03-13 23:30:30 -07005654 BaseDexClassLoader.setReporter(DexLoadReporter.getInstance());
Calin Juravle37dfc8e2017-03-13 19:02:32 -07005655 }
5656
Chad Brubaker78d47122015-11-17 22:26:58 -08005657 // Install the Network Security Config Provider. This must happen before the application
5658 // code is loaded to prevent issues with instances of TLS objects being created before
5659 // the provider is installed.
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005660 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "NetworkSecurityConfigProvider.install");
Chad Brubaker276ee962016-06-08 12:57:46 -07005661 NetworkSecurityConfigProvider.install(appContext);
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005662 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Chad Brubaker78d47122015-11-17 22:26:58 -08005663
Alan Viverette2107d692015-09-03 14:55:27 -04005664 // Continue loading instrumentation.
5665 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005666 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005667 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005668 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005669 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005670 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005671 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672
5673 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005674 final ClassLoader cl = instrContext.getClassLoader();
Dan Sandler07fe63e2017-08-15 16:41:11 -04005675 mInstrumentation = (Instrumentation)
5676 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005677 } catch (Exception e) {
5678 throw new RuntimeException(
5679 "Unable to instantiate instrumentation "
5680 + data.instrumentationName + ": " + e.toString(), e);
5681 }
5682
Alan Viverettebe64eae2015-09-03 14:56:04 -04005683 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5684 mInstrumentation.init(this, instrContext, appContext, component,
5685 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005686
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005687 if (mProfiler.profileFile != null && !ii.handleProfiling
5688 && mProfiler.profileFd == null) {
5689 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005690 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691 file.getParentFile().mkdirs();
5692 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005694 } else {
5695 mInstrumentation = new Instrumentation();
5696 }
5697
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005698 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005699 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005700 } else {
5701 // Small heap, clamp to the current growth limit and let the heap release
5702 // pages after the growth limit to the non growth limit capacity. b/18387825
5703 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005704 }
5705
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005706 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005707 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005708 // probably end up doing the same disk access.
Kurt Nelson5e154362017-06-29 17:20:56 -07005709 Application app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005710 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
Kurt Nelson571c3f62017-07-10 11:09:04 -07005711 final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005712 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005713 // If the app is being launched for full backup or restore, bring it up in
5714 // a restricted environment with the base application class.
Kurt Nelson5e154362017-06-29 17:20:56 -07005715 app = data.info.makeApplication(data.restrictedBackupMode, null);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005716 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005717
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005718 // don't bring up providers in restricted mode; they may depend on the
5719 // app's custom Application class
5720 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005721 if (!ArrayUtils.isEmpty(data.providers)) {
5722 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005723 // For process that contains content providers, we want to
5724 // ensure that the JIT is enabled "at some point".
5725 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5726 }
5727 }
5728
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005729 // Do this after providers, since instrumentation tests generally start their
5730 // test thread at this point, and we don't want that racing.
5731 try {
5732 mInstrumentation.onCreate(data.instrumentationArgs);
5733 }
5734 catch (Exception e) {
5735 throw new RuntimeException(
5736 "Exception thrown in onCreate() of "
5737 + data.instrumentationName + ": " + e.toString(), e);
5738 }
Kurt Nelson571c3f62017-07-10 11:09:04 -07005739 try {
5740 mInstrumentation.callApplicationOnCreate(app);
5741 } catch (Exception e) {
5742 if (!mInstrumentation.onException(app, e)) {
5743 throw new RuntimeException(
5744 "Unable to create application " + app.getClass().getName()
5745 + ": " + e.toString(), e);
5746 }
5747 }
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005748 } finally {
Kurt Nelson571c3f62017-07-10 11:09:04 -07005749 // If the app targets < O-MR1, or doesn't change the thread policy
5750 // during startup, clobber the policy to maintain behavior of b/36951662
5751 if (data.appInfo.targetSdkVersion <= Build.VERSION_CODES.O
5752 || StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) {
5753 StrictMode.setThreadPolicy(savedPolicy);
Kurt Nelson5e154362017-06-29 17:20:56 -07005754 }
5755 }
5756
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005757 // Preload fonts resources
Seigo Nonaka54084b62017-04-24 14:46:23 -07005758 FontsContract.setApplicationContextForResources(appContext);
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005759 try {
5760 final ApplicationInfo info =
Amith Yamasani336d29d2017-02-14 15:14:09 -08005761 getPackageManager().getApplicationInfo(
Clara Bayarri4b5a4d22017-01-27 20:15:45 +00005762 data.appInfo.packageName,
5763 PackageManager.GET_META_DATA /*flags*/,
5764 UserHandle.myUserId());
5765 if (info.metaData != null) {
5766 final int preloadedFontsResource = info.metaData.getInt(
5767 ApplicationInfo.METADATA_PRELOADED_FONTS, 0);
5768 if (preloadedFontsResource != 0) {
5769 data.info.mResources.preloadFonts(preloadedFontsResource);
5770 }
5771 }
5772 } catch (RemoteException e) {
5773 throw e.rethrowFromSystemServer();
5774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005775 }
5776
5777 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005778 IActivityManager am = ActivityManager.getService();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005779 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5780 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 Debug.stopMethodTracing();
5782 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005783 //Slog.i(TAG, "am: " + ActivityManager.getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 // + ", app thr: " + mAppThread);
5785 try {
5786 am.finishInstrumentation(mAppThread, resultCode, results);
5787 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005788 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005789 }
5790 }
5791
Romain Guy65b345f2011-07-27 18:51:50 -07005792 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 Context context, List<ProviderInfo> providers) {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005794 final ArrayList<ContentProviderHolder> results = new ArrayList<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005795
Romain Guya998dff2012-03-23 18:58:36 -07005796 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005797 if (DEBUG_PROVIDER) {
5798 StringBuilder buf = new StringBuilder(128);
5799 buf.append("Pub ");
5800 buf.append(cpi.authority);
5801 buf.append(": ");
5802 buf.append(cpi.name);
5803 Log.i(TAG, buf.toString());
5804 }
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005805 ContentProviderHolder cph = installProvider(context, null, cpi,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005806 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5807 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005808 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005809 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005810 }
5811 }
5812
5813 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005814 ActivityManager.getService().publishContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815 getApplicationThread(), results);
5816 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005817 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005818 }
5819 }
5820
Jeff Sharkey6d515712012-09-20 16:06:08 -07005821 public final IContentProvider acquireProvider(
5822 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005823 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005824 if (provider != null) {
5825 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005826 }
5827
Wale Ogunwale1d646122015-04-24 14:45:14 -07005828 // There is a possible race here. Another thread may try to acquire
5829 // the same provider at the same time. When this happens, we want to ensure
5830 // that the first one wins.
5831 // Note that we cannot hold the lock while acquiring and installing the
5832 // provider since it might take a long time to run and it could also potentially
5833 // be re-entrant in the case where the provider is in the same process.
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07005834 ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005835 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005836 holder = ActivityManager.getService().getContentProvider(
Wale Ogunwale1d646122015-04-24 14:45:14 -07005837 getApplicationThread(), auth, userId, stable);
5838 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005839 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005840 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005841 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005842 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005843 return null;
5844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005845
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005846 // Install provider will increment the reference count for us, and break
5847 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005848 holder = installProvider(c, holder, holder.info,
5849 true /*noisy*/, holder.noReleaseNeeded, stable);
5850 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005851 }
5852
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005853 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5854 if (stable) {
5855 prc.stableCount += 1;
5856 if (prc.stableCount == 1) {
5857 // We are acquiring a new stable reference on the provider.
5858 int unstableDelta;
5859 if (prc.removePending) {
5860 // We have a pending remove operation, which is holding the
5861 // last unstable reference. At this point we are converting
5862 // that unstable reference to our new stable reference.
5863 unstableDelta = -1;
5864 // Cancel the removal of the provider.
5865 if (DEBUG_PROVIDER) {
5866 Slog.v(TAG, "incProviderRef: stable "
5867 + "snatched provider from the jaws of death");
5868 }
5869 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005870 // There is a race! It fails to remove the message, which
5871 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005872 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5873 } else {
5874 unstableDelta = 0;
5875 }
5876 try {
5877 if (DEBUG_PROVIDER) {
5878 Slog.v(TAG, "incProviderRef Now stable - "
5879 + prc.holder.info.name + ": unstableDelta="
5880 + unstableDelta);
5881 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005882 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005883 prc.holder.connection, 1, unstableDelta);
5884 } catch (RemoteException e) {
5885 //do nothing content provider object is dead any way
5886 }
5887 }
5888 } else {
5889 prc.unstableCount += 1;
5890 if (prc.unstableCount == 1) {
5891 // We are acquiring a new unstable reference on the provider.
5892 if (prc.removePending) {
5893 // Oh look, we actually have a remove pending for the
5894 // provider, which is still holding the last unstable
5895 // reference. We just need to cancel that to take new
5896 // ownership of the reference.
5897 if (DEBUG_PROVIDER) {
5898 Slog.v(TAG, "incProviderRef: unstable "
5899 + "snatched provider from the jaws of death");
5900 }
5901 prc.removePending = false;
5902 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5903 } else {
5904 // First unstable ref, increment our count in the
5905 // activity manager.
5906 try {
5907 if (DEBUG_PROVIDER) {
5908 Slog.v(TAG, "incProviderRef: Now unstable - "
5909 + prc.holder.info.name);
5910 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005911 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005912 prc.holder.connection, 0, 1);
5913 } catch (RemoteException e) {
5914 //do nothing content provider object is dead any way
5915 }
5916 }
5917 }
5918 }
5919 }
5920
Jeff Sharkey6d515712012-09-20 16:06:08 -07005921 public final IContentProvider acquireExistingProvider(
5922 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005923 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005924 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005925 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005926 if (pr == null) {
5927 return null;
5928 }
5929
5930 IContentProvider provider = pr.mProvider;
5931 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005932 if (!jBinder.isBinderAlive()) {
5933 // The hosting process of the provider has died; we can't
5934 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005935 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005936 + ": existing object's process dead");
5937 handleUnstableProviderDiedLocked(jBinder, true);
5938 return null;
5939 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005940
5941 // Only increment the ref count if we have one. If we don't then the
5942 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005943 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005944 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005945 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005946 }
5947 return provider;
5948 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005949 }
5950
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005951 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5952 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005953 return false;
5954 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005956 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005957 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005958 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005959 if (prc == null) {
5960 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005962 }
5963
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005964 boolean lastRef = false;
5965 if (stable) {
5966 if (prc.stableCount == 0) {
5967 if (DEBUG_PROVIDER) Slog.v(TAG,
5968 "releaseProvider: stable ref count already 0, how?");
5969 return false;
5970 }
5971 prc.stableCount -= 1;
5972 if (prc.stableCount == 0) {
5973 // What we do at this point depends on whether there are
5974 // any unstable refs left: if there are, we just tell the
5975 // activity manager to decrement its stable count; if there
5976 // aren't, we need to enqueue this provider to be removed,
5977 // and convert to holding a single unstable ref while
5978 // doing so.
5979 lastRef = prc.unstableCount == 0;
5980 try {
5981 if (DEBUG_PROVIDER) {
5982 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5983 + lastRef + " - " + prc.holder.info.name);
5984 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08005985 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005986 prc.holder.connection, -1, lastRef ? 1 : 0);
5987 } catch (RemoteException e) {
5988 //do nothing content provider object is dead any way
5989 }
5990 }
5991 } else {
5992 if (prc.unstableCount == 0) {
5993 if (DEBUG_PROVIDER) Slog.v(TAG,
5994 "releaseProvider: unstable ref count already 0, how?");
5995 return false;
5996 }
5997 prc.unstableCount -= 1;
5998 if (prc.unstableCount == 0) {
5999 // If this is the last reference, we need to enqueue
6000 // this provider to be removed instead of telling the
6001 // activity manager to remove it at this point.
6002 lastRef = prc.stableCount == 0;
6003 if (!lastRef) {
6004 try {
6005 if (DEBUG_PROVIDER) {
6006 Slog.v(TAG, "releaseProvider: No longer unstable - "
6007 + prc.holder.info.name);
6008 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006009 ActivityManager.getService().refContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006010 prc.holder.connection, 0, -1);
6011 } catch (RemoteException e) {
6012 //do nothing content provider object is dead any way
6013 }
6014 }
6015 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006016 }
6017
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006018 if (lastRef) {
6019 if (!prc.removePending) {
6020 // Schedule the actual remove asynchronously, since we don't know the context
6021 // this will be called in.
6022 // TODO: it would be nice to post a delayed message, so
6023 // if we come back and need the same provider quickly
6024 // we will still have it available.
6025 if (DEBUG_PROVIDER) {
6026 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
6027 + prc.holder.info.name);
6028 }
6029 prc.removePending = true;
6030 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
6031 mH.sendMessage(msg);
6032 } else {
6033 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
6034 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006035 }
6036 return true;
6037 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006038 }
6039
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006040 final void completeRemoveProvider(ProviderRefCount prc) {
6041 synchronized (mProviderMap) {
6042 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006043 // There was a race! Some other client managed to acquire
6044 // the provider before the removal was completed.
6045 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006046 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006047 + "provider still in use");
6048 return;
6049 }
6050
Guobin Zhang9e3e52662013-03-21 13:57:11 +08006051 // More complicated race!! Some client managed to acquire the
6052 // provider and release it before the removal was completed.
6053 // Continue the removal, and abort the next remove message.
6054 prc.removePending = false;
6055
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006056 final IBinder jBinder = prc.holder.provider.asBinder();
6057 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
6058 if (existingPrc == prc) {
6059 mProviderRefCountMap.remove(jBinder);
6060 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006061
Dianne Hackbornadd005c2013-07-17 18:43:12 -07006062 for (int i=mProviderMap.size()-1; i>=0; i--) {
6063 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006064 IBinder myBinder = pr.mProvider.asBinder();
6065 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07006066 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006067 }
6068 }
6069 }
6070
6071 try {
6072 if (DEBUG_PROVIDER) {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006073 Slog.v(TAG, "removeProvider: Invoking ActivityManagerService."
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006074 + "removeContentProvider(" + prc.holder.info.name + ")");
6075 }
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006076 ActivityManager.getService().removeContentProvider(
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006077 prc.holder.connection, false);
6078 } catch (RemoteException e) {
6079 //do nothing content provider object is dead any way
6080 }
6081 }
6082
6083 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006084 synchronized (mProviderMap) {
6085 handleUnstableProviderDiedLocked(provider, fromClient);
6086 }
6087 }
6088
6089 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
6090 ProviderRefCount prc = mProviderRefCountMap.get(provider);
6091 if (prc != null) {
6092 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
6093 + provider + " " + prc.holder.info.name);
6094 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09006095 for (int i=mProviderMap.size()-1; i>=0; i--) {
6096 ProviderClientRecord pr = mProviderMap.valueAt(i);
6097 if (pr != null && pr.mProvider.asBinder() == provider) {
6098 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
6099 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006100 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006101 }
You Kimbc74de62013-10-01 00:13:26 +09006102
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006103 if (fromClient) {
6104 // We found out about this due to execution in our client
6105 // code. Tell the activity manager about it now, to ensure
6106 // that the next time we go to do anything with the provider
6107 // it knows it is dead (so we don't race with its death
6108 // notification).
6109 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006110 ActivityManager.getService().unstableProviderDied(
Dianne Hackbornc428aae2012-10-03 16:38:22 -07006111 prc.holder.connection);
6112 } catch (RemoteException e) {
6113 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006114 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07006115 }
6116 }
6117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006119 final void appNotRespondingViaProvider(IBinder provider) {
6120 synchronized (mProviderMap) {
6121 ProviderRefCount prc = mProviderRefCountMap.get(provider);
6122 if (prc != null) {
6123 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006124 ActivityManager.getService()
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006125 .appNotRespondingViaProvider(prc.holder.connection);
6126 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006127 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07006128 }
6129 }
6130 }
6131 }
6132
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006133 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006134 ContentProvider localProvider, ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08006135 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07006136 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
6137
Jeff Sharkey0a17db12016-11-04 11:23:46 -06006138 if (provider != null) {
6139 // If this provider is hosted by the core OS and cannot be upgraded,
6140 // then I guess we're okay doing blocking calls to it.
6141 for (String auth : auths) {
6142 switch (auth) {
6143 case ContactsContract.AUTHORITY:
6144 case CallLog.AUTHORITY:
6145 case CallLog.SHADOW_AUTHORITY:
6146 case BlockedNumberContract.AUTHORITY:
6147 case CalendarContract.AUTHORITY:
6148 case Downloads.Impl.AUTHORITY:
Jeff Sharkeycb621622016-11-11 14:04:32 -07006149 case "telephony":
Jeff Sharkey0a17db12016-11-04 11:23:46 -06006150 Binder.allowBlocking(provider.asBinder());
6151 }
6152 }
6153 }
6154
Jeff Sharkey6d515712012-09-20 16:06:08 -07006155 final ProviderClientRecord pcr = new ProviderClientRecord(
6156 auths, provider, localProvider, holder);
6157 for (String auth : auths) {
6158 final ProviderKey key = new ProviderKey(auth, userId);
6159 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006160 if (existing != null) {
6161 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07006162 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006163 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07006164 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006165 }
6166 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006167 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006168 }
6169
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006170 /**
6171 * Installs the provider.
6172 *
6173 * Providers that are local to the process or that come from the system server
6174 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
6175 * Other remote providers are reference counted. The initial reference count
6176 * for all reference counted providers is one. Providers that are not reference
6177 * counted do not have a reference count (at all).
6178 *
6179 * This method detects when a provider has already been installed. When this happens,
6180 * it increments the reference count of the existing provider (if appropriate)
6181 * and returns the existing provider. This can happen due to concurrent
6182 * attempts to acquire the same provider.
6183 */
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006184 private ContentProviderHolder installProvider(Context context,
6185 ContentProviderHolder holder, ProviderInfo info,
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006186 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006187 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006188 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07006189 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006190 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07006191 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006192 + info.name);
6193 }
6194 Context c = null;
6195 ApplicationInfo ai = info.applicationInfo;
6196 if (context.getPackageName().equals(ai.packageName)) {
6197 c = context;
6198 } else if (mInitialApplication != null &&
6199 mInitialApplication.getPackageName().equals(ai.packageName)) {
6200 c = mInitialApplication;
6201 } else {
6202 try {
6203 c = context.createPackageContext(ai.packageName,
6204 Context.CONTEXT_INCLUDE_CODE);
6205 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07006206 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 }
6208 }
6209 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08006210 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 ai.packageName +
6212 " while loading content provider " +
6213 info.name);
6214 return null;
6215 }
Adam Lesinski4e862812016-11-21 16:02:24 -08006216
6217 if (info.splitName != null) {
6218 try {
6219 c = c.createContextForSplit(info.splitName);
6220 } catch (NameNotFoundException e) {
6221 throw new RuntimeException(e);
6222 }
6223 }
6224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006225 try {
6226 final java.lang.ClassLoader cl = c.getClassLoader();
Dan Sandler07fe63e2017-08-15 16:41:11 -04006227 localProvider = (ContentProvider)cl.
6228 loadClass(info.name).newInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006229 provider = localProvider.getIContentProvider();
6230 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08006231 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 info.name + " from sourceDir " +
6233 info.applicationInfo.sourceDir);
6234 return null;
6235 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006236 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 TAG, "Instantiating local provider " + info.name);
6238 // XXX Need to create the correct context for this provider.
6239 localProvider.attachInfo(c, info);
6240 } catch (java.lang.Exception e) {
6241 if (!mInstrumentation.onException(null, e)) {
6242 throw new RuntimeException(
6243 "Unable to get provider " + info.name
6244 + ": " + e.toString(), e);
6245 }
6246 return null;
6247 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006248 } else {
6249 provider = holder.provider;
6250 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006251 + info.name);
6252 }
6253
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006254 ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006255
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006256 synchronized (mProviderMap) {
6257 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
6258 + " / " + info.name);
6259 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006260 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006261 ComponentName cname = new ComponentName(info.packageName, info.name);
6262 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006263 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006264 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006265 Slog.v(TAG, "installProvider: lost the race, "
6266 + "using existing local provider");
6267 }
6268 provider = pr.mProvider;
6269 } else {
Sudheer Shankafc46e9b2016-10-21 17:55:27 -07006270 holder = new ContentProviderHolder(info);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006271 holder.provider = provider;
6272 holder.noReleaseNeeded = true;
6273 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006274 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006275 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006276 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006277 retHolder = pr.mHolder;
6278 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006279 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
6280 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006281 if (DEBUG_PROVIDER) {
6282 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006283 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07006284 // We need to transfer our new reference to the existing
6285 // ref count, releasing the old one... but only if
6286 // release is needed (that is, it is not running in the
6287 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006288 if (!noReleaseNeeded) {
6289 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07006290 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006291 ActivityManager.getService().removeContentProvider(
Wale Ogunwale1d646122015-04-24 14:45:14 -07006292 holder.connection, stable);
6293 } catch (RemoteException e) {
6294 //do nothing content provider object is dead any way
6295 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006296 }
6297 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006298 ProviderClientRecord client = installProviderAuthoritiesLocked(
6299 provider, localProvider, holder);
6300 if (noReleaseNeeded) {
6301 prc = new ProviderRefCount(holder, client, 1000, 1000);
6302 } else {
6303 prc = stable
6304 ? new ProviderRefCount(holder, client, 1, 0)
6305 : new ProviderRefCount(holder, client, 0, 1);
6306 }
6307 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08006308 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006309 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006310 }
6311 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07006312 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006313 }
6314
Romain Guy65b345f2011-07-27 18:51:50 -07006315 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08006316 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006317 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006318 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07006319 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07006320 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08006321 public void run() {
6322 ensureJitEnabled();
6323 }
6324 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07006325 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
6326 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006327 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Sudheer Shankadc589ac2016-11-10 15:30:17 -08006328 final IActivityManager mgr = ActivityManager.getService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006329 try {
6330 mgr.attachApplication(mAppThread);
6331 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006332 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006333 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006334 // Watch for getting close to heap limit.
6335 BinderInternal.addGcWatcher(new Runnable() {
6336 @Override public void run() {
6337 if (!mSomeActivitiesChanged) {
6338 return;
6339 }
6340 Runtime runtime = Runtime.getRuntime();
6341 long dalvikMax = runtime.maxMemory();
6342 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
6343 if (dalvikUsed > ((3*dalvikMax)/4)) {
6344 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
6345 + " total=" + (runtime.totalMemory()/1024)
6346 + " used=" + (dalvikUsed/1024));
6347 mSomeActivitiesChanged = false;
6348 try {
6349 mgr.releaseSomeActivities(mAppThread);
6350 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07006351 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006352 }
6353 }
6354 }
6355 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006356 } else {
6357 // Don't set application object here -- if the system crashes,
6358 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07006359 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07006360 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006361 try {
6362 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07006363 ContextImpl context = ContextImpl.createAppContext(
6364 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07006365 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
6366 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 } catch (Exception e) {
6368 throw new RuntimeException(
6369 "Unable to instantiate Application():" + e.toString(), e);
6370 }
6371 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006372
6373 // add dropbox logging to libcore
6374 DropBox.setReporter(new DropBoxReporter());
6375
Andrii Kulian44607962017-03-16 11:06:24 -07006376 ViewRootImpl.ConfigChangedCallback configChangedCallback
6377 = (Configuration globalConfig) -> {
6378 synchronized (mResourcesManager) {
6379 // We need to apply this change to the resources immediately, because upon returning
6380 // the view hierarchy will be informed about it.
6381 if (mResourcesManager.applyConfigurationToResourcesLocked(globalConfig,
6382 null /* compat */)) {
6383 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
6384 mResourcesManager.getConfiguration().getLocales());
Adam Lesinskib61e4052016-05-19 18:23:05 -07006385
Andrii Kulian44607962017-03-16 11:06:24 -07006386 // This actually changed the resources! Tell everyone about it.
6387 if (mPendingConfiguration == null
6388 || mPendingConfiguration.isOtherSeqNewer(globalConfig)) {
6389 mPendingConfiguration = globalConfig;
6390 sendMessage(H.CONFIGURATION_CHANGED, globalConfig);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006391 }
6392 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006393 }
Andrii Kulian44607962017-03-16 11:06:24 -07006394 };
6395 ViewRootImpl.addConfigCallback(configChangedCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006396 }
6397
Romain Guy5e9120d2012-01-30 12:17:22 -08006398 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07006399 // The system process on low-memory devices do not get to use hardware
6400 // accelerated drawing, since this can add too much overhead to the
6401 // process.
6402 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08006403 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07006404 } else {
John Reck51aaf902015-12-02 15:08:07 -08006405 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07006406 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 ActivityThread thread = new ActivityThread();
6408 thread.attach(true);
6409 return thread;
6410 }
6411
Jeff Brown10e89712011-07-08 18:52:57 -07006412 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006413 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07006414 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 }
6416 }
6417
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006418 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07006419 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08006420 if (mCoreSettings != null) {
6421 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006422 }
Craig Mautner88c05892013-06-28 09:47:45 -07006423 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006424 }
6425 }
6426
Geremy Condra69689a72012-09-11 16:57:17 -07006427 private static class EventLoggingReporter implements EventLogger.Reporter {
6428 @Override
6429 public void report (int code, Object... list) {
6430 EventLog.writeEvent(code, list);
6431 }
6432 }
6433
Geremy Condrab7faaf42012-09-19 18:07:42 -07006434 private class DropBoxReporter implements DropBox.Reporter {
6435
6436 private DropBoxManager dropBox;
6437
Narayan Kamath7f062242015-04-08 13:24:13 +01006438 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07006439
6440 @Override
6441 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006442 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006443 dropBox.addData(tag, data, flags);
6444 }
6445
6446 @Override
6447 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006448 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006449 dropBox.addText(tag, data);
6450 }
Narayan Kamath7f062242015-04-08 13:24:13 +01006451
6452 private synchronized void ensureInitialized() {
6453 if (dropBox == null) {
6454 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
6455 }
6456 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006457 }
6458
Romain Guy65b345f2011-07-27 18:51:50 -07006459 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006460 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07006461
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08006462 // CloseGuard defaults to true and can be quite spammy. We
6463 // disable it here, but selectively enable it later (via
6464 // StrictMode) on debug builds, but using DropBox, not logs.
6465 CloseGuard.setEnabled(false);
6466
Andreas Gamped281b422016-07-08 03:50:27 +00006467 Environment.initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -07006468
Geremy Condra69689a72012-09-11 16:57:17 -07006469 // Set the reporter for event logging in libcore
6470 EventLogger.setReporter(new EventLoggingReporter());
6471
Robin Lee3d076af2014-04-25 14:57:49 +01006472 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6473 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6474 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006476 Process.setArgV0("<pre-initialized>");
6477
6478 Looper.prepareMainLooper();
6479
6480 ActivityThread thread = new ActivityThread();
6481 thread.attach(false);
6482
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006483 if (sMainThreadHandler == null) {
6484 sMainThreadHandler = thread.getHandler();
6485 }
6486
Dianne Hackborn287952c2010-09-22 22:34:31 -07006487 if (false) {
6488 Looper.myLooper().setMessageLogging(new
6489 LogPrinter(Log.DEBUG, "ActivityThread"));
6490 }
6491
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006492 // End of event ActivityThreadMain.
6493 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 Looper.loop();
6495
Jeff Brown10e89712011-07-08 18:52:57 -07006496 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006497 }
Chris Craikfc294242016-12-13 18:10:46 -08006498
6499 // ------------------ Regular JNI ------------------------
6500
6501 private native void nDumpGraphicsInfo(FileDescriptor fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006502}