blob: dbc6c841b7d591083d45ceed74926a192cb0187b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070019import android.app.assist.AssistContent;
20import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080021import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070023import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.ComponentName;
25import android.content.ContentProvider;
26import android.content.Context;
27import android.content.IContentProvider;
28import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070029import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.pm.ActivityInfo;
31import android.content.pm.ApplicationInfo;
32import android.content.pm.IPackageManager;
33import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070034import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070036import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.ProviderInfo;
38import android.content.pm.ServiceInfo;
39import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070040import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.content.res.Configuration;
42import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070043import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.database.sqlite.SQLiteDatabase;
45import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080046import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.graphics.Bitmap;
48import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070049import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050050import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070051import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050052import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070053import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040054import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040055import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070056import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070057import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070058import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.os.Bundle;
60import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070061import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070062import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.os.Handler;
64import android.os.IBinder;
65import android.os.Looper;
66import android.os.Message;
67import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080068import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070069import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070070import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.os.Process;
72import android.os.RemoteException;
73import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070074import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070076import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070077import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070078import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070079import android.os.UserHandle;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000080import android.provider.Settings;
Alex Klyubinf9034cc2015-02-12 11:43:09 -080081import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080082import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070084import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import android.util.DisplayMetrics;
86import android.util.EventLog;
87import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070088import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070089import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080090import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080091import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -070092import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -070093import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -080095import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096import android.view.View;
97import android.view.ViewDebug;
98import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070099import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import android.view.Window;
101import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700102import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700103import android.renderscript.RenderScriptCacheDir;
Alex Klyubindcdaf872015-05-13 15:57:09 -0700104import android.security.keystore.AndroidKeyStoreProvider;
Calin Juravle69052392015-12-17 17:06:49 +0200105import android.system.Os;
106import android.system.OsConstants;
107import android.system.ErrnoException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700109import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700110import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800111import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112import com.android.internal.os.BinderInternal;
113import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700114import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700115import com.android.internal.os.SomeArgs;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700116import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700117import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700118import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100119import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700120import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122import java.io.File;
123import java.io.FileDescriptor;
124import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700125import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126import java.io.PrintWriter;
127import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700128import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000129import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000131import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132import java.util.List;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800134import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
Geremy Condrab7faaf42012-09-19 18:07:42 -0700137import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700138import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800139import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400140import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800141import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700142import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700143import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700144import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700145
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700146final class RemoteServiceException extends AndroidRuntimeException {
147 public RemoteServiceException(String msg) {
148 super(msg);
149 }
150}
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152/**
153 * This manages the execution of the main thread in an
154 * application process, scheduling and executing activities,
155 * broadcasts, and other operations on it as the activity
156 * manager requests.
157 *
158 * {@hide}
159 */
160public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700161 /** @hide */
162 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700163 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700164 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700165 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700166 /** @hide */
167 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700168 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700169 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700170 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800171 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700172 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700173 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700174 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800177 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
178 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800179 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
Dianne Hackborn455625e2015-01-21 09:55:13 -0800181 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
182 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
183 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
184 public static final int SERVICE_DONE_EXECUTING_START = 1;
185 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
186 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
187
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700188 // Details for pausing activity.
189 private static final int USER_LEAVING = 1;
190 private static final int DONT_REPORT = 2;
191
Filip Gruszczynskica664812015-12-04 12:43:36 -0800192 // Whether to invoke an activity callback after delivering new configuration.
193 private static final boolean REPORT_TO_ACTIVITY = true;
194
Jeff Browndefd4a62014-03-10 21:24:37 -0700195 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700196
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700197 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 final ApplicationThread mAppThread = new ApplicationThread();
200 final Looper mLooper = Looper.myLooper();
201 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700202 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700203 // List of new activities (via ActivityRecord.nextIdle) that should
204 // be reported when next we idle.
205 ActivityClientRecord mNewActivities = null;
206 // Number of activities that are currently visible on-screen.
207 int mNumVisibleActivities = 0;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700208 WeakReference<AssistStructure> mLastAssistStructure;
209 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700210 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700211 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700212 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700213 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700214 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700215 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700216 Application mInitialApplication;
217 final ArrayList<Application> mAllApplications
218 = new ArrayList<Application>();
219 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700220 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800221 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700222 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700224 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700225 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700226 String[] mInstrumentationSplitAppDirs = null;
227 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700228 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700229 String[] mInstrumentedSplitAppDirs = null;
230 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700231 boolean mSystemThread = false;
232 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700233 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700235 // These can be accessed by multiple threads; mPackages is the lock.
236 // XXX For now we keep around information about all packages we have
237 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800238 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700239 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800240 // which means this lock gets held while the activity and window managers
241 // holds their own lock. Thus you MUST NEVER call back into the activity manager
242 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700243 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700244 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
245 = new ArrayMap<String, WeakReference<LoadedApk>>();
246 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
247 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700248 final ArrayList<ActivityClientRecord> mRelaunchingActivities
249 = new ArrayList<ActivityClientRecord>();
250 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700251 // Because we merge activity relaunch operations we can't depend on the ordering provided by
252 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
253 // us to drop certain events, if we know that they happened before relaunch we already executed.
254 // This represents the order of receiving the request from AM.
255 @GuardedBy("mResourcesManager")
256 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Craig Mautner88c05892013-06-28 09:47:45 -0700258 private final ResourcesManager mResourcesManager;
259
Jeff Sharkey6d515712012-09-20 16:06:08 -0700260 private static final class ProviderKey {
261 final String authority;
262 final int userId;
263
264 public ProviderKey(String authority, int userId) {
265 this.authority = authority;
266 this.userId = userId;
267 }
268
269 @Override
270 public boolean equals(Object o) {
271 if (o instanceof ProviderKey) {
272 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800273 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700274 }
275 return false;
276 }
277
278 @Override
279 public int hashCode() {
280 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
281 }
282 }
283
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700284 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700285 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
286 = new ArrayMap<ProviderKey, ProviderClientRecord>();
287 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
288 = new ArrayMap<IBinder, ProviderRefCount>();
289 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
290 = new ArrayMap<IBinder, ProviderClientRecord>();
291 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
292 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700294 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700295 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600296
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700297 final GcIdler mGcIdler = new GcIdler();
298 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700300 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700301
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800302 Bundle mCoreSettings = null;
303
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400304 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700306 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800308 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700309 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700311 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 Activity activity;
313 Window window;
314 Activity parent;
315 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700316 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 boolean paused;
318 boolean stopped;
319 boolean hideForNow;
320 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700321 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800322 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800323 // Used for consolidating configs before sending on to Activity.
324 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700325 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Jeff Hao1b012d32014-08-20 10:35:34 -0700327 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400330 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700331 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800334 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
336 boolean startsNotResumed;
337 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800338 int pendingConfigChanges;
339 boolean onlyLocalRequest;
340
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700341 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800342 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700343 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700345 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
346 // can be compared with other lifecycle operations.
347 int relaunchSeq = 0;
348
349 // Can only be accessed from the UI thread. This represents the latest processed message
350 // that is related to lifecycle events/
351 int lastProcessedSeq = 0;
352
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700353 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 parent = null;
355 embeddedID = null;
356 paused = false;
357 stopped = false;
358 hideForNow = false;
359 nextIdle = null;
360 }
361
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800362 public boolean isPreHoneycomb() {
363 if (activity != null) {
364 return activity.getApplicationInfo().targetSdkVersion
365 < android.os.Build.VERSION_CODES.HONEYCOMB;
366 }
367 return false;
368 }
369
Craig Mautnera0026042014-04-23 11:45:37 -0700370 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700371 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700372 }
373
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700375 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 return "ActivityRecord{"
377 + Integer.toHexString(System.identityHashCode(this))
378 + " token=" + token + " " + (componentName == null
379 ? "no component name" : componentName.toShortString())
380 + "}";
381 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700382
383 public String getStateString() {
384 StringBuilder sb = new StringBuilder();
385 sb.append("ActivityClientRecord{");
386 sb.append("paused=").append(paused);
387 sb.append(", stopped=").append(stopped);
388 sb.append(", hideForNow=").append(hideForNow);
389 sb.append(", startsNotResumed=").append(startsNotResumed);
390 sb.append(", isForward=").append(isForward);
391 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
392 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
393 sb.append(", preserveWindow=").append(mPreserveWindow);
394 if (activity != null) {
395 sb.append(", Activity{");
396 sb.append("resumed=").append(activity.mResumed);
397 sb.append(", stopped=").append(activity.mStopped);
398 sb.append(", finished=").append(activity.isFinishing());
399 sb.append(", destroyed=").append(activity.isDestroyed());
400 sb.append(", startedActivity=").append(activity.mStartedActivity);
401 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
402 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
403 sb.append(", visibleBehind=").append(activity.mVisibleBehind);
404 sb.append("}");
405 }
406 sb.append("}");
407 return sb.toString();
408 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 }
410
Wale Ogunwale1d646122015-04-24 14:45:14 -0700411 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700412 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 final IContentProvider mProvider;
414 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700415 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700417 ProviderClientRecord(String[] names, IContentProvider provider,
418 ContentProvider localProvider,
419 IActivityManager.ContentProviderHolder holder) {
420 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 mProvider = provider;
422 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700423 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 }
425 }
426
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400427 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800428 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 IBinder token;
430 public String toString() {
431 return "NewIntentData{intents=" + intents + " token=" + token + "}";
432 }
433 }
434
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700436 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700437 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
438 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800439 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700440 this.intent = intent;
441 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 Intent intent;
444 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400445 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 public String toString() {
447 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700448 info.packageName + " resultCode=" + getResultCode()
449 + " resultData=" + getResultData() + " resultExtras="
450 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452 }
453
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400454 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700455 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400456 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700457 int backupMode;
458 public String toString() {
459 return "CreateBackupAgentData{appInfo=" + appInfo
460 + " backupAgent=" + appInfo.backupAgentName
461 + " mode=" + backupMode + "}";
462 }
463 }
Bob Leee5408332009-09-04 18:31:17 -0700464
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400465 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 IBinder token;
467 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400468 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 Intent intent;
470 public String toString() {
471 return "CreateServiceData{token=" + token + " className="
472 + info.name + " packageName=" + info.packageName
473 + " intent=" + intent + "}";
474 }
475 }
476
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400477 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 IBinder token;
479 Intent intent;
480 boolean rebind;
481 public String toString() {
482 return "BindServiceData{token=" + token + " intent=" + intent + "}";
483 }
484 }
485
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400486 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800487 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700488 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700490 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 Intent args;
492 public String toString() {
493 return "ServiceArgsData{token=" + token + " startId=" + startId
494 + " args=" + args + "}";
495 }
496 }
497
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400498 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700499 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 String processName;
501 ApplicationInfo appInfo;
502 List<ProviderInfo> providers;
503 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 Bundle instrumentationArgs;
505 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800506 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400508 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700509 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700510 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700511 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400513 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700514
515 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700516 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public String toString() {
519 return "AppBindData{appInfo=" + appInfo + "}";
520 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700521 }
522
523 static final class Profiler {
524 String profileFile;
525 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700526 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700527 boolean autoStopProfiler;
528 boolean profiling;
529 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700530 public void setProfiler(ProfilerInfo profilerInfo) {
531 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700532 if (profiling) {
533 if (fd != null) {
534 try {
535 fd.close();
536 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700537 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700538 }
539 }
540 return;
541 }
542 if (profileFd != null) {
543 try {
544 profileFd.close();
545 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700546 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700547 }
548 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700549 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700550 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700551 samplingInterval = profilerInfo.samplingInterval;
552 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700553 }
554 public void startProfiling() {
555 if (profileFd == null || profiling) {
556 return;
557 }
558 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700559 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
560 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700561 profiling = true;
562 } catch (RuntimeException e) {
563 Slog.w(TAG, "Profiling failed on path " + profileFile);
564 try {
565 profileFd.close();
566 profileFd = null;
567 } catch (IOException e2) {
568 Slog.w(TAG, "Failure closing profile fd", e2);
569 }
570 }
571 }
572 public void stopProfiling() {
573 if (profiling) {
574 profiling = false;
575 Debug.stopMethodTracing();
576 if (profileFd != null) {
577 try {
578 profileFd.close();
579 } catch (IOException e) {
580 }
581 }
582 profileFd = null;
583 profileFile = null;
584 }
585 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 }
587
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400588 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700589 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700590 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800591 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400595 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 IBinder token;
597 List<ResultInfo> results;
598 public String toString() {
599 return "ResultData{token=" + token + " results" + results + "}";
600 }
601 }
602
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400603 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800604 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 String what;
606 String who;
607 }
608
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400609 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700610 String path;
611 ParcelFileDescriptor fd;
612 }
613
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400614 static final class UpdateCompatibilityData {
615 String pkg;
616 CompatibilityInfo info;
617 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800618
Adam Skorydfc7fd72013-08-05 19:23:41 -0700619 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800620 IBinder activityToken;
621 IBinder requestToken;
622 int requestType;
623 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700624
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800625 static final class ActivityConfigChangeData {
626 final IBinder activityToken;
627 final Configuration overrideConfig;
628 public ActivityConfigChangeData(IBinder token, Configuration config) {
629 activityToken = token;
630 overrideConfig = config;
631 }
632 }
633
Romain Guy65b345f2011-07-27 18:51:50 -0700634 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700635
Romain Guy65b345f2011-07-27 18:51:50 -0700636 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700637 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700638
Dianne Hackborna413dc02013-07-12 12:02:55 -0700639 private int mLastProcessState = -1;
640
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700641 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700642 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700643 if (mPendingConfiguration == null ||
644 mPendingConfiguration.isOtherSeqNewer(config)) {
645 mPendingConfiguration = config;
646 }
647 }
648 }
649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700651 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700652 int seq = getLifecycleSeq();
653 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
654 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700655 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
657 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700658 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
659 configChanges,
660 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661 }
662
663 public final void scheduleStopActivity(IBinder token, boolean showWindow,
664 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700665 int seq = getLifecycleSeq();
666 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
667 + " operation received seq: " + seq);
668 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700670 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 }
672
673 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700674 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
676 token);
677 }
678
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800679 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700680 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800681 }
682
Dianne Hackborna413dc02013-07-12 12:02:55 -0700683 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800684 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700685 int seq = getLifecycleSeq();
686 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
687 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700688 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700689 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 }
691
692 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
693 ResultData res = new ResultData();
694 res.token = token;
695 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700696 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698
699 // we use token to identify this activity without having to send the
700 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800701 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700702 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800703 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
704 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
705 int procState, Bundle state, PersistableBundle persistentState,
706 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
707 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700708
709 updateProcessState(procState, false);
710
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700711 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712
713 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700714 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800716 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700717 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400719 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700721 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722
723 r.pendingResults = pendingResults;
724 r.pendingIntents = pendingNewIntents;
725
726 r.startsNotResumed = notResumed;
727 r.isForward = isForward;
728
Jeff Hao1b012d32014-08-20 10:35:34 -0700729 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700730
Wale Ogunwale60454db2015-01-23 16:05:07 -0800731 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700732 updatePendingConfiguration(curConfig);
733
Jeff Brown9ef09972013-10-15 20:49:59 -0700734 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
Wale Ogunwale60454db2015-01-23 16:05:07 -0800737 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800739 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800740 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700741 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800742 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700743 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 }
745
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800746 public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 NewIntentData data = new NewIntentData();
748 data.intents = intents;
749 data.token = token;
750
Jeff Brown9ef09972013-10-15 20:49:59 -0700751 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
754 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
755 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700756 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 configChanges);
758 }
759
760 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400761 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700762 boolean sync, int sendingUser, int processState) {
763 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700764 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700765 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400767 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700768 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 }
770
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400771 public final void scheduleCreateBackupAgent(ApplicationInfo app,
772 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700773 CreateBackupAgentData d = new CreateBackupAgentData();
774 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400775 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700776 d.backupMode = backupMode;
777
Jeff Brown9ef09972013-10-15 20:49:59 -0700778 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700779 }
780
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400781 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
782 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700783 CreateBackupAgentData d = new CreateBackupAgentData();
784 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400785 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700786
Jeff Brown9ef09972013-10-15 20:49:59 -0700787 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700788 }
789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700791 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
792 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 CreateServiceData s = new CreateServiceData();
794 s.token = token;
795 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400796 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797
Jeff Brown9ef09972013-10-15 20:49:59 -0700798 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800
801 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700802 boolean rebind, int processState) {
803 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 BindServiceData s = new BindServiceData();
805 s.token = token;
806 s.intent = intent;
807 s.rebind = rebind;
808
Amith Yamasani742a6712011-05-04 14:49:28 -0700809 if (DEBUG_SERVICE)
810 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
811 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700812 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814
815 public final void scheduleUnbindService(IBinder token, Intent intent) {
816 BindServiceData s = new BindServiceData();
817 s.token = token;
818 s.intent = intent;
819
Jeff Brown9ef09972013-10-15 20:49:59 -0700820 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700823 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700824 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 ServiceArgsData s = new ServiceArgsData();
826 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700827 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700829 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 s.args = args;
831
Jeff Brown9ef09972013-10-15 20:49:59 -0700832 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834
835 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700836 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 }
838
Jeff Hao1b012d32014-08-20 10:35:34 -0700839 public final void bindApplication(String processName, ApplicationInfo appInfo,
840 List<ProviderInfo> providers, ComponentName instrumentationName,
841 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
842 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800843 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700844 boolean enableBinderTracking, boolean trackAllocation,
845 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
846 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847
848 if (services != null) {
849 // Setup the service cache in the ServiceManager
850 ServiceManager.initServiceCache(services);
851 }
852
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800853 setCoreSettings(coreSettings);
854
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800855 AppBindData data = new AppBindData();
856 data.processName = processName;
857 data.appInfo = appInfo;
858 data.providers = providers;
859 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 data.instrumentationArgs = instrumentationArgs;
861 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800862 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400864 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700865 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700866 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700867 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400869 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700870 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700871 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872 }
873
874 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700875 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
Christopher Tate5e1ab332009-09-01 20:32:49 -0700878 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700879 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700880 }
881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700883 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700884 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
886
887 public void updateTimeZone() {
888 TimeZone.setDefault(null);
889 }
890
Robert Greenwalt03595d02010-11-02 14:08:23 -0700891 public void clearDnsCache() {
892 // a non-standard API to get this to libcore
893 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400894 // Allow libcore to perform the necessary actions as it sees fit upon a network
895 // configuration change.
896 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700897 }
898
Jason Monk83520b92014-05-09 15:16:06 -0400899 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400900 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
901 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500902 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400903 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500904 } else {
905 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
906 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700907 }
908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 public void processInBackground() {
910 mH.removeMessages(H.GC_WHEN_IDLE);
911 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
912 }
913
914 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700915 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700916 try {
917 data.fd = ParcelFileDescriptor.dup(fd);
918 data.token = servicetoken;
919 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700920 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700921 } catch (IOException e) {
922 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924 }
925
926 // This function exists to make sure all receiver dispatching is
927 // correctly ordered, since these are one-way calls and the binder driver
928 // applies transaction ordering per object for such calls.
929 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700930 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700931 boolean sticky, int sendingUser, int processState) throws RemoteException {
932 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700933 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
934 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
Bob Leee5408332009-09-04 18:31:17 -0700936
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800937 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700939 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941
Wale Ogunwale60454db2015-01-23 16:05:07 -0800942 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800943 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800944 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800945 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800946 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 }
948
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800949 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700950 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
951 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800952 }
953
Andy McFadden824c5102010-07-09 16:26:57 -0700954 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
955 DumpHeapData dhd = new DumpHeapData();
956 dhd.path = path;
957 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700958 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700959 }
960
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700961 public void setSchedulingGroup(int group) {
962 // Note: do this immediately, since going into the foreground
963 // should happen regardless of what pending work we have to do
964 // and the activity manager will wait for us to report back that
965 // we are done before sending us to the background.
966 try {
967 Process.setProcessGroup(Process.myPid(), group);
968 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800969 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700970 }
971 }
Bob Leee5408332009-09-04 18:31:17 -0700972
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700973 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700974 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700975 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700976
977 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700978 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700979 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700980
Dianne Hackborn30d71892010-12-11 10:37:55 -0800981 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
982 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700983 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700984 try {
985 data.fd = ParcelFileDescriptor.dup(fd);
986 data.token = activitytoken;
987 data.prefix = prefix;
988 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700989 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700990 } catch (IOException e) {
991 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700992 }
993 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700994
Marco Nelissen18cb2872011-11-15 11:19:53 -0800995 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
996 String[] args) {
997 DumpComponentInfo data = new DumpComponentInfo();
998 try {
999 data.fd = ParcelFileDescriptor.dup(fd);
1000 data.token = providertoken;
1001 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001002 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001003 } catch (IOException e) {
1004 Slog.w(TAG, "dumpProvider failed", e);
1005 }
1006 }
1007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001009 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001010 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1011 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001012 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001013 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001014 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001015 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001016 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001017 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07001018 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001019 }
1020
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001021 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001022 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 long nativeMax = Debug.getNativeHeapSize() / 1024;
1024 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1025 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001028 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 long dalvikMax = runtime.totalMemory() / 1024;
1030 long dalvikFree = runtime.freeMemory() / 1024;
1031 long dalvikAllocated = dalvikMax - dalvikFree;
1032 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001033 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -07001034 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
1035 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 int globalAssetCount = AssetManager.getGlobalAssetCount();
1037 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1038 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1039 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1040 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001041 long parcelSize = Parcel.getGlobalAllocSize();
1042 long parcelCount = Parcel.getGlobalAllocCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -07001043 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -08001044 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001045
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001046 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1047 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001048 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1049 nativeMax, nativeAllocated, nativeFree,
1050 dalvikMax, dalvikAllocated, dalvikFree);
1051
Dianne Hackbornb437e092011-08-05 17:50:29 -07001052 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 // NOTE: if you change anything significant below, also consider changing
1054 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 // Object counts
1057 pw.print(viewInstanceCount); pw.print(',');
1058 pw.print(viewRootInstanceCount); pw.print(',');
1059 pw.print(appContextInstanceCount); pw.print(',');
1060 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 pw.print(globalAssetCount); pw.print(',');
1063 pw.print(globalAssetManagerCount); pw.print(',');
1064 pw.print(binderLocalObjectCount); pw.print(',');
1065 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 pw.print(binderDeathObjectCount); pw.print(',');
1068 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001071 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001072 pw.print(stats.memoryUsed / 1024); pw.print(',');
1073 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001074 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001075 for (int i = 0; i < stats.dbStats.size(); i++) {
1076 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001077 pw.print(','); pw.print(dbStats.dbName);
1078 pw.print(','); pw.print(dbStats.pageSize);
1079 pw.print(','); pw.print(dbStats.dbSize);
1080 pw.print(','); pw.print(dbStats.lookaside);
1081 pw.print(','); pw.print(dbStats.cache);
1082 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001083 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001084 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001085
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001086 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
Bob Leee5408332009-09-04 18:31:17 -07001088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 pw.println(" ");
1090 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001091 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 viewRootInstanceCount);
1093
1094 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1095 "Activities:", activityInstanceCount);
1096
1097 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1098 "AssetManagers:", globalAssetManagerCount);
1099
1100 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1101 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001102 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1103 "Parcel count:", parcelCount);
1104 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1105 "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 // SQLite mem info
1108 pw.println(" ");
1109 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001110 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1111 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1112 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001113 pw.println(" ");
1114 int N = stats.dbStats.size();
1115 if (N > 0) {
1116 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001117 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1118 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001119 for (int i = 0; i < N; i++) {
1120 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001121 printRow(pw, DB_INFO_FORMAT,
1122 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1123 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1124 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1125 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001126 }
1127 }
Bob Leee5408332009-09-04 18:31:17 -07001128
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001129 // Asset details.
1130 String assetAlloc = AssetManager.getAssetAllocations();
1131 if (assetAlloc != null) {
1132 pw.println(" ");
1133 pw.println(" Asset Allocations");
1134 pw.print(assetAlloc);
1135 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001136
1137 // Unreachable native memory
1138 if (dumpUnreachable) {
1139 boolean showContents = ((mBoundApplication != null)
1140 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1141 || android.os.Build.IS_DEBUGGABLE;
1142 pw.println(" ");
1143 pw.println(" Unreachable memory");
1144 pw.print(Debug.getUnreachableMemory(100, showContents));
1145 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001146 }
1147
1148 @Override
1149 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1150 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001151 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001152 }
1153
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001154 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001155 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001156 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1157 SQLiteDebug.dump(printer, args);
1158 pw.flush();
1159 }
1160
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001161 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001162 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1163 if (mSystemThread) {
1164 // Ensure this invocation is asynchronous to prevent
1165 // writer waiting due to buffer cannot be consumed.
1166 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1167 @Override
1168 public void run() {
1169 dumpDatabaseInfo(fd, args);
1170 }
1171 });
1172 } else {
1173 dumpDatabaseInfo(fd, args);
1174 }
1175 }
1176
1177 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001178 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001179 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001180 }
1181
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001182 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001183 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001184 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001185 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001186 cmd.activityToken = activityToken;
1187 cmd.requestToken = requestToken;
1188 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001189 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001190 }
1191
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001192 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001193 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001194 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001195
1196 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1197 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1198 ucd.pkg = pkg;
1199 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001200 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001201 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001202
1203 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001204 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001205 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001206
Craig Mautner5eda9b32013-07-02 11:58:16 -07001207 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001208 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001209 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001210
Craig Mautnereb8abf72014-07-02 15:04:09 -07001211 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1212 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1213 new Pair<IBinder, ActivityOptions>(token, options));
1214 }
1215
Dianne Hackborna413dc02013-07-12 12:02:55 -07001216 public void setProcessState(int state) {
1217 updateProcessState(state, true);
1218 }
1219
1220 public void updateProcessState(int processState, boolean fromIpc) {
1221 synchronized (this) {
1222 if (mLastProcessState != processState) {
1223 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001224 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1225 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1226 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1227 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1228 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1229 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1230 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1231 }
1232 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001233 if (false) {
1234 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1235 + (fromIpc ? " (from ipc": ""));
1236 }
1237 }
1238 }
1239 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001240
1241 @Override
1242 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001243 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001244 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001245
1246 @Override
1247 public final void updateTimePrefs(boolean is24Hour) {
1248 DateFormat.set24HourTimePref(is24Hour);
1249 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001250
1251 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001252 public void scheduleCancelVisibleBehind(IBinder token) {
1253 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001254 }
1255
1256 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001257 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1258 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001259 }
Craig Mautner8746a472014-07-24 15:12:54 -07001260
Jeff Sharkey605eb792014-11-04 13:34:06 -08001261 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001262 public void scheduleEnterAnimationComplete(IBinder token) {
1263 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1264 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001265
1266 @Override
1267 public void notifyCleartextNetwork(byte[] firstPacket) {
1268 if (StrictMode.vmCleartextNetworkEnabled()) {
1269 StrictMode.onCleartextNetworkDetected(firstPacket);
1270 }
1271 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001272
1273 @Override
1274 public void startBinderTracking() {
1275 sendMessage(H.START_BINDER_TRACKING, null);
1276 }
1277
1278 @Override
1279 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1280 try {
1281 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1282 } catch (IOException e) {
1283 }
1284 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001285
1286 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001287 public void scheduleMultiWindowChanged(IBinder token, boolean inMultiWindow)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001288 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001289 sendMessage(H.MULTI_WINDOW_CHANGED, token, inMultiWindow ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001290 }
1291
1292 @Override
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001293 public void schedulePictureInPictureChanged(IBinder token, boolean inPip)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001294 throws RemoteException {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001295 sendMessage(H.PICTURE_IN_PICTURE_CHANGED, token, inPip ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001296 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001297
1298 @Override
1299 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1300 IVoiceInteractor voiceInteractor) throws RemoteException {
1301 SomeArgs args = SomeArgs.obtain();
1302 args.arg1 = token;
1303 args.arg2 = voiceInteractor;
1304 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 }
1307
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001308 private int getLifecycleSeq() {
1309 synchronized (mResourcesManager) {
1310 return mLifecycleSeq++;
1311 }
1312 }
1313
Romain Guy65b345f2011-07-27 18:51:50 -07001314 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 public static final int LAUNCH_ACTIVITY = 100;
1316 public static final int PAUSE_ACTIVITY = 101;
1317 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1318 public static final int STOP_ACTIVITY_SHOW = 103;
1319 public static final int STOP_ACTIVITY_HIDE = 104;
1320 public static final int SHOW_WINDOW = 105;
1321 public static final int HIDE_WINDOW = 106;
1322 public static final int RESUME_ACTIVITY = 107;
1323 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001324 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 public static final int BIND_APPLICATION = 110;
1326 public static final int EXIT_APPLICATION = 111;
1327 public static final int NEW_INTENT = 112;
1328 public static final int RECEIVER = 113;
1329 public static final int CREATE_SERVICE = 114;
1330 public static final int SERVICE_ARGS = 115;
1331 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 public static final int CONFIGURATION_CHANGED = 118;
1334 public static final int CLEAN_UP_CONTEXT = 119;
1335 public static final int GC_WHEN_IDLE = 120;
1336 public static final int BIND_SERVICE = 121;
1337 public static final int UNBIND_SERVICE = 122;
1338 public static final int DUMP_SERVICE = 123;
1339 public static final int LOW_MEMORY = 124;
1340 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1341 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001342 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001343 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001344 public static final int DESTROY_BACKUP_AGENT = 129;
1345 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001346 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001347 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001348 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001349 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001350 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001351 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001352 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001353 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001354 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001355 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001356 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001357 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001358 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001359 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001360 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001361 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001362 public static final int CANCEL_VISIBLE_BEHIND = 147;
1363 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001364 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001365 public static final int START_BINDER_TRACKING = 150;
1366 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001367 public static final int MULTI_WINDOW_CHANGED = 152;
1368 public static final int PICTURE_IN_PICTURE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001369 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001372 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 switch (code) {
1374 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1375 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1376 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1377 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1378 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1379 case SHOW_WINDOW: return "SHOW_WINDOW";
1380 case HIDE_WINDOW: return "HIDE_WINDOW";
1381 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1382 case SEND_RESULT: return "SEND_RESULT";
1383 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1384 case BIND_APPLICATION: return "BIND_APPLICATION";
1385 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1386 case NEW_INTENT: return "NEW_INTENT";
1387 case RECEIVER: return "RECEIVER";
1388 case CREATE_SERVICE: return "CREATE_SERVICE";
1389 case SERVICE_ARGS: return "SERVICE_ARGS";
1390 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1392 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1393 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1394 case BIND_SERVICE: return "BIND_SERVICE";
1395 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1396 case DUMP_SERVICE: return "DUMP_SERVICE";
1397 case LOW_MEMORY: return "LOW_MEMORY";
1398 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1399 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001400 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001401 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1402 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001403 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001404 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001405 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001406 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001407 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001408 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001409 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001410 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001411 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001412 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001413 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001414 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001415 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001416 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001417 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001418 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001419 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001420 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1421 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001422 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001423 case MULTI_WINDOW_CHANGED: return "MULTI_WINDOW_CHANGED";
1424 case PICTURE_IN_PICTURE_CHANGED: return "PICTURE_IN_PICTURE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001425 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001428 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 }
1430 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001431 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 switch (msg.what) {
1433 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001434 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001435 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001436
1437 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001438 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001439 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001440 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 } break;
1442 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001444 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001445 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001446 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001448 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001449 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001450 SomeArgs args = (SomeArgs) msg.obj;
1451 handlePauseActivity((IBinder) args.arg1, false,
1452 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1453 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001454 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001455 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001456 } break;
1457 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001458 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001459 SomeArgs args = (SomeArgs) msg.obj;
1460 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1461 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001462 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001463 } break;
1464 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001465 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001466 SomeArgs args = (SomeArgs) msg.obj;
1467 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001468 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001469 } break;
1470 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001471 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001472 SomeArgs args = (SomeArgs) msg.obj;
1473 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001474 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001475 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001477 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001479 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 break;
1481 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001484 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 break;
1486 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001487 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001488 SomeArgs args = (SomeArgs) msg.obj;
1489 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001490 args.argi3, "RESUME_ACTIVITY");
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001491 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 break;
1493 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001494 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001496 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 break;
1498 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001499 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1501 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001502 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 break;
1504 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001505 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 AppBindData data = (AppBindData)msg.obj;
1507 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001508 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 break;
1510 case EXIT_APPLICATION:
1511 if (mInitialApplication != null) {
1512 mInitialApplication.onTerminate();
1513 }
1514 Looper.myLooper().quit();
1515 break;
1516 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001517 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001519 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 break;
1521 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001522 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001524 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001525 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 break;
1527 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001528 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001529 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001530 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 break;
1532 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001533 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001535 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001536 break;
1537 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001538 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001540 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 break;
1542 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001543 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001545 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 break;
1547 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001548 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001550 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001551 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001554 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001555 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001556 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001557 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 break;
1559 case CLEAN_UP_CONTEXT:
1560 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1561 cci.context.performFinalCleanup(cci.who, cci.what);
1562 break;
1563 case GC_WHEN_IDLE:
1564 scheduleGcIdler();
1565 break;
1566 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001567 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 break;
1569 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001570 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001572 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 break;
1574 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001575 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001576 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1577 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001578 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001580 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001581 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001582 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001583 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001584 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001585 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001586 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001587 break;
1588 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001589 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001590 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001591 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001592 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001593 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001594 Process.killProcess(Process.myPid());
1595 break;
1596 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001597 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001598 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001599 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001600 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001601 case ENABLE_JIT:
1602 ensureJitEnabled();
1603 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001604 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001605 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001606 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001607 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001608 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001609 case SCHEDULE_CRASH:
1610 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001611 case DUMP_HEAP:
1612 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1613 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001614 case DUMP_ACTIVITY:
1615 handleDumpActivity((DumpComponentInfo)msg.obj);
1616 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001617 case DUMP_PROVIDER:
1618 handleDumpProvider((DumpComponentInfo)msg.obj);
1619 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001620 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001621 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001622 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001623 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001624 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001625 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001626 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001627 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001628 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001629 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001630 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1631 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001632 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001633 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001634 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001635 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001636 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001637 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001638 case UNSTABLE_PROVIDER_DIED:
1639 handleUnstableProviderDied((IBinder)msg.obj, false);
1640 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001641 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1642 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001643 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001644 case TRANSLUCENT_CONVERSION_COMPLETE:
1645 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1646 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001647 case INSTALL_PROVIDER:
1648 handleInstallProvider((ProviderInfo) msg.obj);
1649 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001650 case ON_NEW_ACTIVITY_OPTIONS:
1651 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1652 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001653 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001654 case CANCEL_VISIBLE_BEHIND:
1655 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001656 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001657 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1658 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001659 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001660 case ENTER_ANIMATION_COMPLETE:
1661 handleEnterAnimationComplete((IBinder) msg.obj);
1662 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001663 case START_BINDER_TRACKING:
1664 handleStartBinderTracking();
1665 break;
1666 case STOP_BINDER_TRACKING_AND_DUMP:
1667 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1668 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001669 case MULTI_WINDOW_CHANGED:
1670 handleMultiWindowChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001671 break;
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08001672 case PICTURE_IN_PICTURE_CHANGED:
1673 handlePictureInPictureChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001674 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001675 case LOCAL_VOICE_INTERACTION_STARTED:
1676 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1677 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1678 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001680 Object obj = msg.obj;
1681 if (obj instanceof SomeArgs) {
1682 ((SomeArgs) obj).recycle();
1683 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001684 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Bob Leee5408332009-09-04 18:31:17 -07001686
Brian Carlstromed7e0072011-03-24 13:27:57 -07001687 private void maybeSnapshot() {
1688 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001689 // convert the *private* ActivityThread.PackageInfo to *public* known
1690 // android.content.pm.PackageInfo
1691 String packageName = mBoundApplication.info.mPackageName;
1692 android.content.pm.PackageInfo packageInfo = null;
1693 try {
1694 Context context = getSystemContext();
1695 if(context == null) {
1696 Log.e(TAG, "cannot get a valid context");
1697 return;
1698 }
1699 PackageManager pm = context.getPackageManager();
1700 if(pm == null) {
1701 Log.e(TAG, "cannot get a valid PackageManager");
1702 return;
1703 }
1704 packageInfo = pm.getPackageInfo(
1705 packageName, PackageManager.GET_ACTIVITIES);
1706 } catch (NameNotFoundException e) {
1707 Log.e(TAG, "cannot get package info for " + packageName, e);
1708 }
1709 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001710 }
1711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001712 }
1713
Romain Guy65b345f2011-07-27 18:51:50 -07001714 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001715 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001717 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001718 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001719 if (mBoundApplication != null && mProfiler.profileFd != null
1720 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001721 stopProfiling = true;
1722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001723 if (a != null) {
1724 mNewActivities = null;
1725 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001726 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001728 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 TAG, "Reporting idle of " + a +
1730 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001731 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 if (a.activity != null && !a.activity.mFinished) {
1733 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001734 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001735 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001737 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
1739 }
1740 prev = a;
1741 a = a.nextIdle;
1742 prev.nextIdle = null;
1743 } while (a != null);
1744 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001745 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001746 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001747 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001748 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 return false;
1750 }
1751 }
1752
1753 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001754 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 public final boolean queueIdle() {
1756 doGcIfNeeded();
1757 return false;
1758 }
1759 }
1760
Romain Guy65b345f2011-07-27 18:51:50 -07001761 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001762 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001765 public static boolean isSystem() {
1766 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1767 }
1768
Svetoslavfbf0eca2015-05-01 16:52:41 -07001769 public static String currentOpPackageName() {
1770 ActivityThread am = currentActivityThread();
1771 return (am != null && am.getApplication() != null)
1772 ? am.getApplication().getOpPackageName() : null;
1773 }
1774
Romain Guy65b345f2011-07-27 18:51:50 -07001775 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001776 ActivityThread am = currentActivityThread();
1777 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001778 ? am.mBoundApplication.appInfo.packageName : null;
1779 }
1780
1781 public static String currentProcessName() {
1782 ActivityThread am = currentActivityThread();
1783 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001784 ? am.mBoundApplication.processName : null;
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786
Romain Guy65b345f2011-07-27 18:51:50 -07001787 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001788 ActivityThread am = currentActivityThread();
1789 return am != null ? am.mInitialApplication : null;
1790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001792 public static IPackageManager getPackageManager() {
1793 if (sPackageManager != null) {
1794 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1795 return sPackageManager;
1796 }
1797 IBinder b = ServiceManager.getService("package");
1798 //Slog.v("PackageManager", "default service binder = " + b);
1799 sPackageManager = IPackageManager.Stub.asInterface(b);
1800 //Slog.v("PackageManager", "default service = " + sPackageManager);
1801 return sPackageManager;
1802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803
Romain Guy65b345f2011-07-27 18:51:50 -07001804 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001805 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1806 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001807 if (config == null) {
1808 return null;
1809 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001810 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001811 mMainThreadConfig.setTo(config);
1812 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001813 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001814 }
1815 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001816 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001818 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001819 * Creates the top level resources for the given package. Will return an existing
1820 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001821 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001822 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001823 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1824 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1825 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001826 }
1827
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001828 final Handler getHandler() {
1829 return mH;
1830 }
1831
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001832 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1833 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001834 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1835 }
1836
1837 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1838 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001839 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001840 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001841 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001842 if (differentUser) {
1843 // Caching not supported across users
1844 ref = null;
1845 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 ref = mPackages.get(packageName);
1847 } else {
1848 ref = mResourcePackages.get(packageName);
1849 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001850
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001851 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001852 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001853 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1854 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001855 if (packageInfo != null && (packageInfo.mResources == null
1856 || packageInfo.mResources.getAssets().isUpToDate())) {
1857 if (packageInfo.isSecurityViolation()
1858 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1859 throw new SecurityException(
1860 "Requesting code from " + packageName
1861 + " to be run in process "
1862 + mBoundApplication.processName
1863 + "/" + mBoundApplication.appInfo.uid);
1864 }
1865 return packageInfo;
1866 }
1867 }
1868
1869 ApplicationInfo ai = null;
1870 try {
1871 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001872 PackageManager.GET_SHARED_LIBRARY_FILES
1873 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1874 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001876 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 }
1878
1879 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001880 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882
1883 return null;
1884 }
1885
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001886 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1887 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1889 boolean securityViolation = includeCode && ai.uid != 0
1890 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001891 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001892 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001893 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1895 |Context.CONTEXT_IGNORE_SECURITY))
1896 == Context.CONTEXT_INCLUDE_CODE) {
1897 if (securityViolation) {
1898 String msg = "Requesting code from " + ai.packageName
1899 + " (with uid " + ai.uid + ")";
1900 if (mBoundApplication != null) {
1901 msg = msg + " to be run in process "
1902 + mBoundApplication.processName + " (with uid "
1903 + mBoundApplication.appInfo.uid + ")";
1904 }
1905 throw new SecurityException(msg);
1906 }
1907 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001908 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1909 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 }
1911
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001912 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1913 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001914 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 }
1916
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001917 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001918 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001919 WeakReference<LoadedApk> ref;
1920 if (includeCode) {
1921 ref = mPackages.get(packageName);
1922 } else {
1923 ref = mResourcePackages.get(packageName);
1924 }
1925 return ref != null ? ref.get() : null;
1926 }
1927 }
1928
Romain Guy65b345f2011-07-27 18:51:50 -07001929 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001930 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1931 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001932 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001933 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001934 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001935 if (differentUser) {
1936 // Caching not supported across users
1937 ref = null;
1938 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 ref = mPackages.get(aInfo.packageName);
1940 } else {
1941 ref = mResourcePackages.get(aInfo.packageName);
1942 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001943
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001944 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (packageInfo == null || (packageInfo.mResources != null
1946 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001947 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 : "Loading resource-only package ") + aInfo.packageName
1949 + " (in " + (mBoundApplication != null
1950 ? mBoundApplication.processName : null)
1951 + ")");
1952 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001953 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001955 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001956
1957 if (mSystemThread && "android".equals(aInfo.packageName)) {
1958 packageInfo.installSystemApplicationInfo(aInfo,
1959 getSystemContext().mPackageInfo.getClassLoader());
1960 }
1961
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001962 if (differentUser) {
1963 // Caching not supported across users
1964 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001966 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 } else {
1968 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001969 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 }
1971 }
1972 return packageInfo;
1973 }
1974 }
1975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001977 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979
1980 public ApplicationThread getApplicationThread()
1981 {
1982 return mAppThread;
1983 }
1984
1985 public Instrumentation getInstrumentation()
1986 {
1987 return mInstrumentation;
1988 }
1989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001991 return mProfiler != null && mProfiler.profileFile != null
1992 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993 }
1994
1995 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001996 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
1998
1999 public Looper getLooper() {
2000 return mLooper;
2001 }
2002
2003 public Application getApplication() {
2004 return mInitialApplication;
2005 }
Bob Leee5408332009-09-04 18:31:17 -07002006
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002007 public String getProcessName() {
2008 return mBoundApplication.processName;
2009 }
Bob Leee5408332009-09-04 18:31:17 -07002010
Dianne Hackborn21556372010-02-04 16:34:40 -08002011 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 synchronized (this) {
2013 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002014 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002016 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 }
2019
Narayan Kamath29564cd2014-08-07 10:57:40 +01002020 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002021 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002022 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002023
2024 // give ourselves a default profiler
2025 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002026 }
2027 }
2028
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002029 void ensureJitEnabled() {
2030 if (!mJitEnabled) {
2031 mJitEnabled = true;
2032 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2033 }
2034 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002036 void scheduleGcIdler() {
2037 if (!mGcIdlerScheduled) {
2038 mGcIdlerScheduled = true;
2039 Looper.myQueue().addIdleHandler(mGcIdler);
2040 }
2041 mH.removeMessages(H.GC_WHEN_IDLE);
2042 }
2043
2044 void unscheduleGcIdler() {
2045 if (mGcIdlerScheduled) {
2046 mGcIdlerScheduled = false;
2047 Looper.myQueue().removeIdleHandler(mGcIdler);
2048 }
2049 mH.removeMessages(H.GC_WHEN_IDLE);
2050 }
2051
2052 void doGcIfNeeded() {
2053 mGcIdlerScheduled = false;
2054 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002055 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 // + "m now=" + now);
2057 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002058 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 BinderInternal.forceGc("bg");
2060 }
2061 }
2062
Dianne Hackborne77187d2013-10-25 16:32:41 -07002063 private static final String HEAP_FULL_COLUMN
2064 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2065 private static final String HEAP_COLUMN
2066 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002067 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2068 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2069 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002070
2071 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002072 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002073
2074 static void printRow(PrintWriter pw, String format, Object...objs) {
2075 pw.println(String.format(format, objs));
2076 }
2077
2078 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002079 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2080 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002081 long nativeMax, long nativeAllocated, long nativeFree,
2082 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2083
2084 // For checkin, we print one long comma-separated list of values
2085 if (checkin) {
2086 // NOTE: if you change anything significant below, also consider changing
2087 // ACTIVITY_THREAD_CHECKIN_VERSION.
2088
2089 // Header
2090 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2091 pw.print(pid); pw.print(',');
2092 pw.print(processName); pw.print(',');
2093
2094 // Heap info - max
2095 pw.print(nativeMax); pw.print(',');
2096 pw.print(dalvikMax); pw.print(',');
2097 pw.print("N/A,");
2098 pw.print(nativeMax + dalvikMax); pw.print(',');
2099
2100 // Heap info - allocated
2101 pw.print(nativeAllocated); pw.print(',');
2102 pw.print(dalvikAllocated); pw.print(',');
2103 pw.print("N/A,");
2104 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2105
2106 // Heap info - free
2107 pw.print(nativeFree); pw.print(',');
2108 pw.print(dalvikFree); pw.print(',');
2109 pw.print("N/A,");
2110 pw.print(nativeFree + dalvikFree); pw.print(',');
2111
2112 // Heap info - proportional set size
2113 pw.print(memInfo.nativePss); pw.print(',');
2114 pw.print(memInfo.dalvikPss); pw.print(',');
2115 pw.print(memInfo.otherPss); pw.print(',');
2116 pw.print(memInfo.getTotalPss()); pw.print(',');
2117
2118 // Heap info - swappable set size
2119 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2120 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2121 pw.print(memInfo.otherSwappablePss); pw.print(',');
2122 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2123
2124 // Heap info - shared dirty
2125 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2126 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2127 pw.print(memInfo.otherSharedDirty); pw.print(',');
2128 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2129
2130 // Heap info - shared clean
2131 pw.print(memInfo.nativeSharedClean); pw.print(',');
2132 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2133 pw.print(memInfo.otherSharedClean); pw.print(',');
2134 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2135
2136 // Heap info - private Dirty
2137 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2138 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2139 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2140 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2141
2142 // Heap info - private Clean
2143 pw.print(memInfo.nativePrivateClean); pw.print(',');
2144 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2145 pw.print(memInfo.otherPrivateClean); pw.print(',');
2146 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2147
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002148 // Heap info - swapped out
2149 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2150 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2151 pw.print(memInfo.otherSwappedOut); pw.print(',');
2152 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2153
2154 // Heap info - swapped out pss
2155 if (memInfo.hasSwappedOutPss) {
2156 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2157 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2158 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2159 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2160 } else {
2161 pw.print("N/A,");
2162 pw.print("N/A,");
2163 pw.print("N/A,");
2164 pw.print("N/A,");
2165 }
2166
Dianne Hackborne77187d2013-10-25 16:32:41 -07002167 // Heap info - other areas
2168 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2169 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2170 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2171 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2172 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2173 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2174 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2175 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002176 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2177 if (memInfo.hasSwappedOutPss) {
2178 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2179 } else {
2180 pw.print("N/A,");
2181 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002182 }
2183 return;
2184 }
2185
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002186 if (!dumpSummaryOnly) {
2187 if (dumpFullInfo) {
2188 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002189 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2190 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002191 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002192 "Clean", "Clean", "Dirty",
2193 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002194 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2195 "------", "------", "------", "------", "------", "------");
2196 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2197 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2198 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002199 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2200 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002201 nativeMax, nativeAllocated, nativeFree);
2202 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2203 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2204 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002205 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2206 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002207 dalvikMax, dalvikAllocated, dalvikFree);
2208 } else {
2209 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002210 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2211 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002212 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2213 "Clean", "Dirty", "Size", "Alloc", "Free");
2214 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2215 "------", "------", "------", "------", "------");
2216 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2217 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002218 memInfo.nativePrivateClean,
2219 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2220 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002221 nativeMax, nativeAllocated, nativeFree);
2222 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2223 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002224 memInfo.dalvikPrivateClean,
2225 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2226 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002227 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002228 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002229
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002230 int otherPss = memInfo.otherPss;
2231 int otherSwappablePss = memInfo.otherSwappablePss;
2232 int otherSharedDirty = memInfo.otherSharedDirty;
2233 int otherPrivateDirty = memInfo.otherPrivateDirty;
2234 int otherSharedClean = memInfo.otherSharedClean;
2235 int otherPrivateClean = memInfo.otherPrivateClean;
2236 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002237 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002238
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002239 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002240 final int myPss = memInfo.getOtherPss(i);
2241 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2242 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2243 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2244 final int mySharedClean = memInfo.getOtherSharedClean(i);
2245 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2246 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002247 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002248 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002249 || mySharedClean != 0 || myPrivateClean != 0
2250 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002251 if (dumpFullInfo) {
2252 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2253 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002254 mySharedClean, myPrivateClean,
2255 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2256 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002257 } else {
2258 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2259 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002260 myPrivateClean,
2261 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2262 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002263 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002264 otherPss -= myPss;
2265 otherSwappablePss -= mySwappablePss;
2266 otherSharedDirty -= mySharedDirty;
2267 otherPrivateDirty -= myPrivateDirty;
2268 otherSharedClean -= mySharedClean;
2269 otherPrivateClean -= myPrivateClean;
2270 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002271 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002272 }
2273 }
2274
2275 if (dumpFullInfo) {
2276 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2277 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002278 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2279 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002280 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2281 memInfo.getTotalSwappablePss(),
2282 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2283 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002284 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2285 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002286 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2287 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002288 } else {
2289 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002290 otherPrivateDirty, otherPrivateClean,
2291 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002292 "", "", "");
2293 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2294 memInfo.getTotalPrivateDirty(),
2295 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002296 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002297 memInfo.getTotalSwappedOut(),
2298 nativeMax+dalvikMax,
2299 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2300 }
2301
2302 if (dumpDalvik) {
2303 pw.println(" ");
2304 pw.println(" Dalvik Details");
2305
2306 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2307 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2308 final int myPss = memInfo.getOtherPss(i);
2309 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2310 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2311 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2312 final int mySharedClean = memInfo.getOtherSharedClean(i);
2313 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2314 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002315 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002316 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002317 || mySharedClean != 0 || myPrivateClean != 0
2318 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002319 if (dumpFullInfo) {
2320 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2321 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002322 mySharedClean, myPrivateClean,
2323 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2324 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002325 } else {
2326 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2327 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002328 myPrivateClean,
2329 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2330 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002331 }
2332 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002333 }
2334 }
2335 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002336
2337 pw.println(" ");
2338 pw.println(" App Summary");
2339 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2340 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2341 printRow(pw, ONE_COUNT_COLUMN,
2342 "Java Heap:", memInfo.getSummaryJavaHeap());
2343 printRow(pw, ONE_COUNT_COLUMN,
2344 "Native Heap:", memInfo.getSummaryNativeHeap());
2345 printRow(pw, ONE_COUNT_COLUMN,
2346 "Code:", memInfo.getSummaryCode());
2347 printRow(pw, ONE_COUNT_COLUMN,
2348 "Stack:", memInfo.getSummaryStack());
2349 printRow(pw, ONE_COUNT_COLUMN,
2350 "Graphics:", memInfo.getSummaryGraphics());
2351 printRow(pw, ONE_COUNT_COLUMN,
2352 "Private Other:", memInfo.getSummaryPrivateOther());
2353 printRow(pw, ONE_COUNT_COLUMN,
2354 "System:", memInfo.getSummarySystem());
2355 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002356 if (memInfo.hasSwappedOutPss) {
2357 printRow(pw, TWO_COUNT_COLUMNS,
2358 "TOTAL:", memInfo.getSummaryTotalPss(),
2359 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2360 } else {
2361 printRow(pw, TWO_COUNT_COLUMNS,
2362 "TOTAL:", memInfo.getSummaryTotalPss(),
2363 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2364 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002365 }
2366
Jeff Hamilton52d32032011-01-08 15:31:26 -06002367 public void registerOnActivityPausedListener(Activity activity,
2368 OnActivityPausedListener listener) {
2369 synchronized (mOnPauseListeners) {
2370 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2371 if (list == null) {
2372 list = new ArrayList<OnActivityPausedListener>();
2373 mOnPauseListeners.put(activity, list);
2374 }
2375 list.add(listener);
2376 }
2377 }
2378
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002379 public void unregisterOnActivityPausedListener(Activity activity,
2380 OnActivityPausedListener listener) {
2381 synchronized (mOnPauseListeners) {
2382 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2383 if (list != null) {
2384 list.remove(listener);
2385 }
2386 }
2387 }
2388
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002389 public final ActivityInfo resolveActivityInfo(Intent intent) {
2390 ActivityInfo aInfo = intent.resolveActivityInfo(
2391 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2392 if (aInfo == null) {
2393 // Throw an exception.
2394 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002395 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 }
2397 return aInfo;
2398 }
Bob Leee5408332009-09-04 18:31:17 -07002399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002400 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002402 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002403 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002405 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 r.intent = intent;
2407 r.state = state;
2408 r.parent = parent;
2409 r.embeddedID = id;
2410 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002411 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (localLOGV) {
2413 ComponentName compname = intent.getComponent();
2414 String name;
2415 if (compname != null) {
2416 name = compname.toShortString();
2417 } else {
2418 name = "(Intent " + intent + ").getComponent() returned null";
2419 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002420 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 + ", comp=" + name
2422 + ", token=" + token);
2423 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002424 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 }
2426
2427 public final Activity getActivity(IBinder token) {
2428 return mActivities.get(token).activity;
2429 }
2430
2431 public final void sendActivityResult(
2432 IBinder token, String id, int requestCode,
2433 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002434 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002435 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2437 list.add(new ResultInfo(id, requestCode, resultCode, data));
2438 mAppThread.scheduleSendResult(token, list);
2439 }
2440
Jeff Brown9ef09972013-10-15 20:49:59 -07002441 private void sendMessage(int what, Object obj) {
2442 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
Jeff Brown9ef09972013-10-15 20:49:59 -07002445 private void sendMessage(int what, Object obj, int arg1) {
2446 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448
Jeff Brown9ef09972013-10-15 20:49:59 -07002449 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2450 sendMessage(what, obj, arg1, arg2, false);
2451 }
2452
2453 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2454 if (DEBUG_MESSAGES) Slog.v(
2455 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2456 + ": " + arg1 + " / " + obj);
2457 Message msg = Message.obtain();
2458 msg.what = what;
2459 msg.obj = obj;
2460 msg.arg1 = arg1;
2461 msg.arg2 = arg2;
2462 if (async) {
2463 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002465 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
2467
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002468 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2469 if (DEBUG_MESSAGES) Slog.v(
2470 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2471 "seq= " + seq);
2472 Message msg = Message.obtain();
2473 msg.what = what;
2474 SomeArgs args = SomeArgs.obtain();
2475 args.arg1 = obj;
2476 args.argi1 = arg1;
2477 args.argi2 = arg2;
2478 args.argi3 = seq;
2479 msg.obj = args;
2480 mH.sendMessage(msg);
2481 }
2482
Dianne Hackborn21556372010-02-04 16:34:40 -08002483 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 String what) {
2485 ContextCleanupInfo cci = new ContextCleanupInfo();
2486 cci.context = context;
2487 cci.who = who;
2488 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002489 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 }
2491
Craig Mautner233ceee2014-05-09 17:05:11 -07002492 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2494
2495 ActivityInfo aInfo = r.activityInfo;
2496 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002497 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 Context.CONTEXT_INCLUDE_CODE);
2499 }
Bob Leee5408332009-09-04 18:31:17 -07002500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 ComponentName component = r.intent.getComponent();
2502 if (component == null) {
2503 component = r.intent.resolveActivity(
2504 mInitialApplication.getPackageManager());
2505 r.intent.setComponent(component);
2506 }
2507
2508 if (r.activityInfo.targetActivity != null) {
2509 component = new ComponentName(r.activityInfo.packageName,
2510 r.activityInfo.targetActivity);
2511 }
2512
2513 Activity activity = null;
2514 try {
2515 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2516 activity = mInstrumentation.newActivity(
2517 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002518 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002519 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002520 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 if (r.state != null) {
2522 r.state.setClassLoader(cl);
2523 }
2524 } catch (Exception e) {
2525 if (!mInstrumentation.onException(activity, e)) {
2526 throw new RuntimeException(
2527 "Unable to instantiate activity " + component
2528 + ": " + e.toString(), e);
2529 }
2530 }
2531
2532 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002533 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002534
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002535 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2536 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002537 TAG, r + ": app=" + app
2538 + ", appName=" + app.getPackageName()
2539 + ", pkg=" + r.packageInfo.getPackageName()
2540 + ", comp=" + r.intent.getComponent().toShortString()
2541 + ", dir=" + r.packageInfo.getAppDir());
2542
2543 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002544 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002546 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002547 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002548 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002549 Window window = null;
2550 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2551 window = r.mPendingRemoveWindow;
2552 r.mPendingRemoveWindow = null;
2553 r.mPendingRemoveWindowManager = null;
2554 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002555 activity.attach(appContext, this, getInstrumentation(), r.token,
2556 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002557 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002558 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002559
Christopher Tateb70f3df2009-04-07 16:07:59 -07002560 if (customIntent != null) {
2561 activity.mIntent = customIntent;
2562 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002563 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 activity.mStartedActivity = false;
2565 int theme = r.activityInfo.getThemeResource();
2566 if (theme != 0) {
2567 activity.setTheme(theme);
2568 }
2569
2570 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002571 if (r.isPersistable()) {
2572 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2573 } else {
2574 mInstrumentation.callActivityOnCreate(activity, r.state);
2575 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 if (!activity.mCalled) {
2577 throw new SuperNotCalledException(
2578 "Activity " + r.intent.getComponent().toShortString() +
2579 " did not call through to super.onCreate()");
2580 }
2581 r.activity = activity;
2582 r.stopped = true;
2583 if (!r.activity.mFinished) {
2584 activity.performStart();
2585 r.stopped = false;
2586 }
2587 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002588 if (r.isPersistable()) {
2589 if (r.state != null || r.persistentState != null) {
2590 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2591 r.persistentState);
2592 }
2593 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2595 }
2596 }
2597 if (!r.activity.mFinished) {
2598 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002599 if (r.isPersistable()) {
2600 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2601 r.persistentState);
2602 } else {
2603 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 if (!activity.mCalled) {
2606 throw new SuperNotCalledException(
2607 "Activity " + r.intent.getComponent().toShortString() +
2608 " did not call through to super.onPostCreate()");
2609 }
2610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 }
2612 r.paused = true;
2613
2614 mActivities.put(r.token, r);
2615
2616 } catch (SuperNotCalledException e) {
2617 throw e;
2618
2619 } catch (Exception e) {
2620 if (!mInstrumentation.onException(activity, e)) {
2621 throw new RuntimeException(
2622 "Unable to start activity " + component
2623 + ": " + e.toString(), e);
2624 }
2625 }
2626
2627 return activity;
2628 }
2629
Wale Ogunwale7c726682015-02-06 17:34:28 -08002630 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2631 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002632 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002633 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002634 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002635 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002636 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002637
Wale Ogunwale7c726682015-02-06 17:34:28 -08002638 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski082614c2016-03-04 14:33:47 -08002639 this, r.packageInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002640 appContext.setOuterContext(activity);
2641 Context baseContext = appContext;
2642
2643 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002644 // For debugging purposes, if the activity's package name contains the value of
2645 // the "debug.use-second-display" system property as a substring, then show
2646 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002647 String pkgName = SystemProperties.get("debug.second-display.pkg");
2648 if (pkgName != null && !pkgName.isEmpty()
2649 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002650 for (int id : dm.getDisplayIds()) {
2651 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002652 Display display =
2653 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002654 baseContext = appContext.createDisplayContext(display);
2655 break;
2656 }
2657 }
2658 }
2659 return baseContext;
2660 }
2661
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002662 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002663 // If we are getting ready to gc after going to the background, well
2664 // we are back active so skip it.
2665 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002666 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667
Jeff Hao1b012d32014-08-20 10:35:34 -07002668 if (r.profilerInfo != null) {
2669 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002670 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002671 }
2672
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002673 // Make sure we are running with the most recent config.
2674 handleConfigurationChanged(null, null);
2675
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002676 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002678
Chet Haase0d1c27a2014-11-03 18:35:16 +00002679 // Initialize before creating the activity
2680 WindowManagerGlobal.initialize();
2681
Craig Mautner233ceee2014-05-09 17:05:11 -07002682 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683
2684 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002685 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002686 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002687 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002688 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002689 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690
2691 if (!r.activity.mFinished && r.startsNotResumed) {
2692 // The activity manager actually wants this one to start out
2693 // paused, because it needs to be visible but isn't in the
2694 // foreground. We accomplish this by going through the
2695 // normal startup (because activities expect to go through
2696 // onResume() the first time they run, before their window
2697 // is displayed), and then pausing it. However, in this case
2698 // we do -not- need to do the full pause cycle (of freezing
2699 // and such) because the activity manager assumes it can just
2700 // retain the current state it has.
2701 try {
2702 r.activity.mCalled = false;
2703 mInstrumentation.callActivityOnPause(r.activity);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002704 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
2705 r.activity.getComponentName().getClassName(), reason);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002706 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002707 // we need to be created again. But we only do this
2708 // for pre-Honeycomb apps, which always save their state
2709 // when pausing, so we can not have them save their state
2710 // when restarting from a paused state. For HC and later,
2711 // we want to (and can) let the state be saved as the normal
2712 // part of stopping the activity.
2713 if (r.isPreHoneycomb()) {
2714 r.state = oldState;
2715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 if (!r.activity.mCalled) {
2717 throw new SuperNotCalledException(
2718 "Activity " + r.intent.getComponent().toShortString() +
2719 " did not call through to super.onPause()");
2720 }
2721
2722 } catch (SuperNotCalledException e) {
2723 throw e;
2724
2725 } catch (Exception e) {
2726 if (!mInstrumentation.onException(r.activity, e)) {
2727 throw new RuntimeException(
2728 "Unable to pause activity "
2729 + r.intent.getComponent().toShortString()
2730 + ": " + e.toString(), e);
2731 }
2732 }
2733 r.paused = true;
2734 }
2735 } else {
2736 // If there was an error, for any reason, tell the activity
2737 // manager to stop us.
2738 try {
2739 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002740 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2741 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002743 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002744 }
2745 }
2746 }
2747
Filip Gruszczynski23493322015-07-29 17:02:59 -07002748 private void reportSizeConfigurations(ActivityClientRecord r) {
2749 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2750 if (configurations == null) {
2751 return;
2752 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002753 SparseIntArray horizontal = new SparseIntArray();
2754 SparseIntArray vertical = new SparseIntArray();
2755 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002756 for (int i = configurations.length - 1; i >= 0; i--) {
2757 Configuration config = configurations[i];
2758 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002759 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002760 }
2761 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002762 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002763 }
2764 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002765 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002766 }
2767 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002768 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002769 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2770 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002771 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002772 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002773 }
2774
2775 }
2776
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002777 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002778 final int N = intents.size();
2779 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002780 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002781 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002782 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002783 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2785 }
2786 }
2787
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002788 public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002789 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 if (r != null) {
2791 final boolean resumed = !r.paused;
2792 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002793 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 mInstrumentation.callActivityOnPause(r.activity);
2795 }
2796 deliverNewIntents(r, intents);
2797 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002798 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002799 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 }
2801 }
2802 }
Bob Leee5408332009-09-04 18:31:17 -07002803
Romain Guy65b345f2011-07-27 18:51:50 -07002804 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002805 performNewIntents(data.token, data.intents);
2806 }
2807
Adam Skorydfc7fd72013-08-05 19:23:41 -07002808 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackborn782d4982015-07-08 17:36:37 -07002809 if (mLastAssistStructure != null) {
2810 AssistStructure structure = mLastAssistStructure.get();
2811 if (structure != null) {
2812 structure.clearSendChannel();
2813 }
2814 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002815 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002816 AssistStructure structure = null;
2817 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002818 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002819 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002820 if (r != null) {
2821 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2822 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002823 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002824 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002825 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002826 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002827 if (activityIntent != null && (r.window == null ||
2828 (r.window.getAttributes().flags
2829 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002830 Intent intent = new Intent(activityIntent);
2831 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2832 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2833 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002834 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002835 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002836 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002837 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002838 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002839 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002840 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002841 if (structure == null) {
2842 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002843 }
Dianne Hackborn782d4982015-07-08 17:36:37 -07002844 mLastAssistStructure = new WeakReference<>(structure);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002845 IActivityManager mgr = ActivityManagerNative.getDefault();
2846 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002847 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002848 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002849 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002850 }
2851 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002852
2853 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2854 ActivityClientRecord r = mActivities.get(token);
2855 if (r != null) {
2856 r.activity.onTranslucentConversionComplete(drawComplete);
2857 }
2858 }
2859
Craig Mautnereb8abf72014-07-02 15:04:09 -07002860 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2861 ActivityClientRecord r = mActivities.get(token);
2862 if (r != null) {
2863 r.activity.onNewActivityOptions(options);
2864 }
2865 }
2866
Jose Lima4b6c6692014-08-12 17:41:12 -07002867 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002868 ActivityClientRecord r = mActivities.get(token);
2869 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002870 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002871 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002872 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002873 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002874 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002875 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2876 if (!activity.mCalled) {
2877 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002878 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002879 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002880 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002881 }
2882 }
2883 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002884 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002885 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002886 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002887 }
2888 }
2889
Jose Lima4b6c6692014-08-12 17:41:12 -07002890 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002891 ActivityClientRecord r = mActivities.get(token);
2892 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002893 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002894 }
2895 }
2896
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002897 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002898 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2899 try {
2900 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2901 } finally {
2902 StrictMode.setThreadPolicy(oldPolicy);
2903 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002904 }
2905
Craig Mautner8746a472014-07-24 15:12:54 -07002906 private void handleEnterAnimationComplete(IBinder token) {
2907 ActivityClientRecord r = mActivities.get(token);
2908 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002909 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002910 }
2911 }
2912
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002913 private void handleStartBinderTracking() {
2914 Binder.enableTracing();
2915 }
2916
2917 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2918 try {
2919 Binder.disableTracing();
2920 Binder.getTransactionTracker().writeTracesToFile(fd);
2921 } finally {
2922 IoUtils.closeQuietly(fd);
2923 Binder.getTransactionTracker().clearTraces();
2924 }
2925 }
2926
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002927 private void handleMultiWindowChanged(IBinder token, boolean inMultiWindow) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002928 final ActivityClientRecord r = mActivities.get(token);
2929 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002930 r.activity.onMultiWindowChanged(inMultiWindow);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002931 }
2932 }
2933
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002934 private void handlePictureInPictureChanged(IBinder token, boolean inPip) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002935 final ActivityClientRecord r = mActivities.get(token);
2936 if (r != null) {
Wale Ogunwale3b93a4d2016-01-29 17:46:53 -08002937 r.activity.onPictureInPictureChanged(inPip);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002938 }
2939 }
2940
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002941 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2942 final ActivityClientRecord r = mActivities.get(token);
2943 if (r != null) {
2944 r.voiceInteractor = interactor;
2945 r.activity.setVoiceInteractor(interactor);
2946 if (interactor == null) {
2947 r.activity.onLocalVoiceInteractionStopped();
2948 } else {
2949 r.activity.onLocalVoiceInteractionStarted();
2950 }
2951 }
2952 }
2953
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002954 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2955
2956 /**
2957 * Return the Intent that's currently being handled by a
2958 * BroadcastReceiver on this thread, or null if none.
2959 * @hide
2960 */
2961 public static Intent getIntentBeingBroadcast() {
2962 return sCurrentBroadcastIntent.get();
2963 }
2964
Romain Guy65b345f2011-07-27 18:51:50 -07002965 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 // If we are getting ready to gc after going to the background, well
2967 // we are back active so skip it.
2968 unscheduleGcIdler();
2969
2970 String component = data.intent.getComponent().getClassName();
2971
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002972 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002973 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974
2975 IActivityManager mgr = ActivityManagerNative.getDefault();
2976
Romain Guy65b345f2011-07-27 18:51:50 -07002977 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978 try {
2979 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2980 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002981 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002982 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2984 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002985 if (DEBUG_BROADCAST) Slog.i(TAG,
2986 "Finishing failed broadcast to " + data.intent.getComponent());
2987 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 throw new RuntimeException(
2989 "Unable to instantiate receiver " + component
2990 + ": " + e.toString(), e);
2991 }
2992
2993 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002994 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002995
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002996 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 TAG, "Performing receive of " + data.intent
2998 + ": app=" + app
2999 + ", appName=" + app.getPackageName()
3000 + ", pkg=" + packageInfo.getPackageName()
3001 + ", comp=" + data.intent.getComponent().toShortString()
3002 + ", dir=" + packageInfo.getAppDir());
3003
Dianne Hackborn21556372010-02-04 16:34:40 -08003004 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003005 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07003006 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 receiver.onReceive(context.getReceiverRestrictedContext(),
3008 data.intent);
3009 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003010 if (DEBUG_BROADCAST) Slog.i(TAG,
3011 "Finishing failed broadcast to " + data.intent.getComponent());
3012 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003013 if (!mInstrumentation.onException(receiver, e)) {
3014 throw new RuntimeException(
3015 "Unable to start receiver " + component
3016 + ": " + e.toString(), e);
3017 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003018 } finally {
3019 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003020 }
3021
Dianne Hackborne829fef2010-10-26 17:44:01 -07003022 if (receiver.getPendingResult() != null) {
3023 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 }
3025 }
3026
Christopher Tate181fafa2009-05-14 11:12:14 -07003027 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003028 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003029 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003030
Christopher Tate346acb12012-10-15 19:20:25 -07003031 // Sanity check the requested target package's uid against ours
3032 try {
3033 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3034 data.appInfo.packageName, 0, UserHandle.myUserId());
3035 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3036 Slog.w(TAG, "Asked to instantiate non-matching package "
3037 + data.appInfo.packageName);
3038 return;
3039 }
3040 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003041 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003042 }
3043
Christopher Tate181fafa2009-05-14 11:12:14 -07003044 // no longer idle; we have backup work to do
3045 unscheduleGcIdler();
3046
3047 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003048 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003049 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003050 if (packageName == null) {
3051 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3052 return;
3053 }
3054
Christopher Tate181fafa2009-05-14 11:12:14 -07003055 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003056 // full backup operation but no app-supplied agent? use the default implementation
3057 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3058 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003059 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003060 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003061
Christopher Tate181fafa2009-05-14 11:12:14 -07003062 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003063 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003064 BackupAgent agent = mBackupAgents.get(packageName);
3065 if (agent != null) {
3066 // reusing the existing instance
3067 if (DEBUG_BACKUP) {
3068 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003069 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003070 binder = agent.onBind();
3071 } else {
3072 try {
3073 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3074
3075 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3076 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3077
3078 // set up the agent's context
3079 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3080 context.setOuterContext(agent);
3081 agent.attach(context);
3082
3083 agent.onCreate();
3084 binder = agent.onBind();
3085 mBackupAgents.put(packageName, agent);
3086 } catch (Exception e) {
3087 // If this is during restore, fail silently; otherwise go
3088 // ahead and let the user see the crash.
3089 Slog.e(TAG, "Agent threw during creation: " + e);
3090 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3091 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3092 throw e;
3093 }
3094 // falling through with 'binder' still null
3095 }
Christopher Tated1475e02009-07-09 15:36:17 -07003096 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003097
3098 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003099 try {
3100 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3101 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003102 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003103 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003104 } catch (Exception e) {
3105 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003106 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003107 }
3108 }
3109
3110 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003111 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003112 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003113
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003114 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003115 String packageName = packageInfo.mPackageName;
3116 BackupAgent agent = mBackupAgents.get(packageName);
3117 if (agent != null) {
3118 try {
3119 agent.onDestroy();
3120 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003121 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003122 e.printStackTrace();
3123 }
3124 mBackupAgents.remove(packageName);
3125 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003126 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003127 }
3128 }
3129
Romain Guy65b345f2011-07-27 18:51:50 -07003130 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003131 // If we are getting ready to gc after going to the background, well
3132 // we are back active so skip it.
3133 unscheduleGcIdler();
3134
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003135 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003136 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 Service service = null;
3138 try {
3139 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3140 service = (Service) cl.loadClass(data.info.name).newInstance();
3141 } catch (Exception e) {
3142 if (!mInstrumentation.onException(service, e)) {
3143 throw new RuntimeException(
3144 "Unable to instantiate service " + data.info.name
3145 + ": " + e.toString(), e);
3146 }
3147 }
3148
3149 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003150 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003151
Jeff Browndefd4a62014-03-10 21:24:37 -07003152 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3153 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003155 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 service.attach(context, this, data.info.name, data.token, app,
3157 ActivityManagerNative.getDefault());
3158 service.onCreate();
3159 mServices.put(data.token, service);
3160 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003161 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003162 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003163 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003164 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 }
3166 } catch (Exception e) {
3167 if (!mInstrumentation.onException(service, e)) {
3168 throw new RuntimeException(
3169 "Unable to create service " + data.info.name
3170 + ": " + e.toString(), e);
3171 }
3172 }
3173 }
3174
Romain Guy65b345f2011-07-27 18:51:50 -07003175 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003177 if (DEBUG_SERVICE)
3178 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 if (s != null) {
3180 try {
3181 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003182 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 try {
3184 if (!data.rebind) {
3185 IBinder binder = s.onBind(data.intent);
3186 ActivityManagerNative.getDefault().publishService(
3187 data.token, data.intent, binder);
3188 } else {
3189 s.onRebind(data.intent);
3190 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003191 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003192 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003193 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003194 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003195 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 }
3197 } catch (Exception e) {
3198 if (!mInstrumentation.onException(s, e)) {
3199 throw new RuntimeException(
3200 "Unable to bind to service " + s
3201 + " with " + data.intent + ": " + e.toString(), e);
3202 }
3203 }
3204 }
3205 }
3206
Romain Guy65b345f2011-07-27 18:51:50 -07003207 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 Service s = mServices.get(data.token);
3209 if (s != null) {
3210 try {
3211 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003212 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 boolean doRebind = s.onUnbind(data.intent);
3214 try {
3215 if (doRebind) {
3216 ActivityManagerNative.getDefault().unbindFinished(
3217 data.token, data.intent, doRebind);
3218 } else {
3219 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003220 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 }
3222 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003223 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 }
3225 } catch (Exception e) {
3226 if (!mInstrumentation.onException(s, e)) {
3227 throw new RuntimeException(
3228 "Unable to unbind to service " + s
3229 + " with " + data.intent + ": " + e.toString(), e);
3230 }
3231 }
3232 }
3233 }
3234
Dianne Hackborn625ac272010-09-17 18:29:22 -07003235 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003236 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3237 try {
3238 Service s = mServices.get(info.token);
3239 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003240 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3241 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003242 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3243 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003245 } finally {
3246 IoUtils.closeQuietly(info.fd);
3247 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 }
3249 }
3250
Dianne Hackborn625ac272010-09-17 18:29:22 -07003251 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003252 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3253 try {
3254 ActivityClientRecord r = mActivities.get(info.token);
3255 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003256 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3257 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003258 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3259 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003260 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003261 } finally {
3262 IoUtils.closeQuietly(info.fd);
3263 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003264 }
3265 }
3266
Marco Nelissen18cb2872011-11-15 11:19:53 -08003267 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003268 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3269 try {
3270 ProviderClientRecord r = mLocalProviders.get(info.token);
3271 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003272 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3273 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003274 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3275 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003276 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003277 } finally {
3278 IoUtils.closeQuietly(info.fd);
3279 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003280 }
3281 }
3282
Romain Guy65b345f2011-07-27 18:51:50 -07003283 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 Service s = mServices.get(data.token);
3285 if (s != null) {
3286 try {
3287 if (data.args != null) {
3288 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003289 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003291 int res;
3292 if (!data.taskRemoved) {
3293 res = s.onStartCommand(data.args, data.flags, data.startId);
3294 } else {
3295 s.onTaskRemoved(data.args);
3296 res = Service.START_TASK_REMOVED_COMPLETE;
3297 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003298
3299 QueuedWork.waitToFinish();
3300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003302 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003303 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003305 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003306 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003307 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 } catch (Exception e) {
3309 if (!mInstrumentation.onException(s, e)) {
3310 throw new RuntimeException(
3311 "Unable to start service " + s
3312 + " with " + data.args + ": " + e.toString(), e);
3313 }
3314 }
3315 }
3316 }
3317
Romain Guy65b345f2011-07-27 18:51:50 -07003318 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 Service s = mServices.remove(token);
3320 if (s != null) {
3321 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003322 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 s.onDestroy();
3324 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003325 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003326 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003327 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003328 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003329
3330 QueuedWork.waitToFinish();
3331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003333 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003334 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003336 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 }
3338 } catch (Exception e) {
3339 if (!mInstrumentation.onException(s, e)) {
3340 throw new RuntimeException(
3341 "Unable to stop service " + s
3342 + ": " + e.toString(), e);
3343 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003344 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003346 } else {
3347 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003349 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003350 }
3351
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003352 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003353 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003354 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003355 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 + " finished=" + r.activity.mFinished);
3357 if (r != null && !r.activity.mFinished) {
3358 if (clearHide) {
3359 r.hideForNow = false;
3360 r.activity.mStartedActivity = false;
3361 }
3362 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003363 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003364 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 if (r.pendingIntents != null) {
3366 deliverNewIntents(r, r.pendingIntents);
3367 r.pendingIntents = null;
3368 }
3369 if (r.pendingResults != null) {
3370 deliverResults(r, r.pendingResults);
3371 r.pendingResults = null;
3372 }
3373 r.activity.performResume();
3374
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003375 // If there is a pending local relaunch that was requested when the activity was
3376 // paused, it will put the activity into paused state when it finally happens.
3377 // Since the activity resumed before being relaunched, we don't want that to happen,
3378 // so we need to clear the request to relaunch paused.
3379 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3380 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3381 if (relaunching.token == r.token
3382 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3383 relaunching.startsNotResumed = false;
3384 }
3385 }
3386
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003387 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3388 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 r.paused = false;
3391 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003393 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 } catch (Exception e) {
3395 if (!mInstrumentation.onException(r.activity, e)) {
3396 throw new RuntimeException(
3397 "Unable to resume activity "
3398 + r.intent.getComponent().toShortString()
3399 + ": " + e.toString(), e);
3400 }
3401 }
3402 }
3403 return r;
3404 }
3405
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003406 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3407 if (r.mPreserveWindow && !force) {
3408 return;
3409 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003410 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003411 r.mPendingRemoveWindowManager.removeViewImmediate(
3412 r.mPendingRemoveWindow.getDecorView());
3413 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003414 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003415 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003416 r.activity.getClass().getName(), "Activity");
3417 }
3418 }
3419 r.mPendingRemoveWindow = null;
3420 r.mPendingRemoveWindowManager = null;
3421 }
3422
Craig Mautner233ceee2014-05-09 17:05:11 -07003423 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003424 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003425 ActivityClientRecord r = mActivities.get(token);
3426 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3427 return;
3428 }
3429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 // If we are getting ready to gc after going to the background, well
3431 // we are back active so skip it.
3432 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003433 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434
Adam Powellcfbe9be2013-11-06 14:58:58 -08003435 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003436 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003437
3438 if (r != null) {
3439 final Activity a = r.activity;
3440
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003441 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 TAG, "Resume " + r + " started activity: " +
3443 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3444 + ", finished: " + a.mFinished);
3445
3446 final int forwardBit = isForward ?
3447 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 // If the window hasn't yet been added to the window manager,
3450 // and this guy didn't finish itself or start another activity,
3451 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003452 boolean willBeVisible = !a.mStartedActivity;
3453 if (!willBeVisible) {
3454 try {
3455 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3456 a.getActivityToken());
3457 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003458 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003459 }
3460 }
3461 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003462 r.window = r.activity.getWindow();
3463 View decor = r.window.getDecorView();
3464 decor.setVisibility(View.INVISIBLE);
3465 ViewManager wm = a.getWindowManager();
3466 WindowManager.LayoutParams l = r.window.getAttributes();
3467 a.mDecor = decor;
3468 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3469 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003470 if (r.mPreserveWindow) {
3471 a.mWindowAdded = true;
3472 r.mPreserveWindow = false;
3473 }
3474 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003475 a.mWindowAdded = true;
3476 wm.addView(decor, l);
3477 }
3478
3479 // If the window has already been added, but during resume
3480 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003481 // window visible.
3482 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003483 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 TAG, "Launch " + r + " mStartedActivity set");
3485 r.hideForNow = true;
3486 }
3487
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003488 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003489 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003491 // The window is now visible if it has been added, we are not
3492 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003493 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003494 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003496 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003497 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003498 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 r.newConfig = null;
3500 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003501 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003502 + isForward);
3503 WindowManager.LayoutParams l = r.window.getAttributes();
3504 if ((l.softInputMode
3505 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3506 != forwardBit) {
3507 l.softInputMode = (l.softInputMode
3508 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3509 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003510 if (r.activity.mVisibleFromClient) {
3511 ViewManager wm = a.getWindowManager();
3512 View decor = r.window.getDecorView();
3513 wm.updateViewLayout(decor, l);
3514 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 }
3516 r.activity.mVisibleFromServer = true;
3517 mNumVisibleActivities++;
3518 if (r.activity.mVisibleFromClient) {
3519 r.activity.makeVisible();
3520 }
3521 }
3522
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003523 if (!r.onlyLocalRequest) {
3524 r.nextIdle = mNewActivities;
3525 mNewActivities = r;
3526 if (localLOGV) Slog.v(
3527 TAG, "Scheduling idle handler for " + r);
3528 Looper.myQueue().addIdleHandler(new Idler());
3529 }
3530 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003532 // Tell the activity manager we have resumed.
3533 if (reallyResume) {
3534 try {
3535 ActivityManagerNative.getDefault().activityResumed(token);
3536 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003537 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003538 }
3539 }
3540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 } else {
3542 // If an exception was thrown when trying to resume, then
3543 // just end this activity.
3544 try {
3545 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003546 .finishActivity(token, Activity.RESULT_CANCELED, null,
3547 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003549 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 }
3551 }
3552 }
3553
3554 private int mThumbnailWidth = -1;
3555 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003556 private Bitmap mAvailThumbnailBitmap = null;
3557 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558
Romain Guy65b345f2011-07-27 18:51:50 -07003559 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003560 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003561 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003562 if (thumbnail == null) {
3563 int w = mThumbnailWidth;
3564 int h;
3565 if (w < 0) {
3566 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003567 int wId = com.android.internal.R.dimen.thumbnail_width;
3568 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003569 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3570 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003571 } else {
3572 h = mThumbnailHeight;
3573 }
3574
3575 // On platforms where we don't want thumbnails, set dims to (0,0)
3576 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003577 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3578 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003579 thumbnail.eraseColor(0);
3580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003581 }
3582
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003583 if (thumbnail != null) {
3584 Canvas cv = mThumbnailCanvas;
3585 if (cv == null) {
3586 mThumbnailCanvas = cv = new Canvas();
3587 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003588
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003589 cv.setBitmap(thumbnail);
3590 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3591 mAvailThumbnailBitmap = thumbnail;
3592 thumbnail = null;
3593 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003594 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003597 } catch (Exception e) {
3598 if (!mInstrumentation.onException(r.activity, e)) {
3599 throw new RuntimeException(
3600 "Unable to create thumbnail of "
3601 + r.intent.getComponent().toShortString()
3602 + ": " + e.toString(), e);
3603 }
3604 thumbnail = null;
3605 }
3606
3607 return thumbnail;
3608 }
3609
Romain Guy65b345f2011-07-27 18:51:50 -07003610 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003611 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003612 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003613 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003614 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3615 return;
3616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003618 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 if (userLeaving) {
3620 performUserLeavingActivity(r);
3621 }
Bob Leee5408332009-09-04 18:31:17 -07003622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003624 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003626 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003627 if (r.isPreHoneycomb()) {
3628 QueuedWork.waitToFinish();
3629 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003630
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003632 if (!dontReport) {
3633 try {
3634 ActivityManagerNative.getDefault().activityPaused(token);
3635 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003636 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003639 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003640 }
3641 }
3642
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003643 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 mInstrumentation.callActivityOnUserLeaving(r.activity);
3645 }
3646
3647 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003648 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003649 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003650 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651 }
3652
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003653 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003654 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 if (r.paused) {
3656 if (r.activity.mFinished) {
3657 // If we are finishing, we won't call onResume() in certain cases.
3658 // So here we likewise don't want to call onPause() if the activity
3659 // isn't resumed.
3660 return null;
3661 }
3662 RuntimeException e = new RuntimeException(
3663 "Performing pause of activity that is not resumed: "
3664 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003665 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 if (finished) {
3668 r.activity.mFinished = true;
3669 }
3670 try {
3671 // Next have the activity save its current state and managed dialogs...
3672 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003673 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675 // Now we are idle.
3676 r.activity.mCalled = false;
3677 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08003678 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003679 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 if (!r.activity.mCalled) {
3681 throw new SuperNotCalledException(
3682 "Activity " + r.intent.getComponent().toShortString() +
3683 " did not call through to super.onPause()");
3684 }
3685
3686 } catch (SuperNotCalledException e) {
3687 throw e;
3688
3689 } catch (Exception e) {
3690 if (!mInstrumentation.onException(r.activity, e)) {
3691 throw new RuntimeException(
3692 "Unable to pause activity "
3693 + r.intent.getComponent().toShortString()
3694 + ": " + e.toString(), e);
3695 }
3696 }
3697 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003698
3699 // Notify any outstanding on paused listeners
3700 ArrayList<OnActivityPausedListener> listeners;
3701 synchronized (mOnPauseListeners) {
3702 listeners = mOnPauseListeners.remove(r.activity);
3703 }
3704 int size = (listeners != null ? listeners.size() : 0);
3705 for (int i = 0; i < size; i++) {
3706 listeners.get(i).onPaused(r.activity);
3707 }
3708
Craig Mautnera0026042014-04-23 11:45:37 -07003709 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003710 }
3711
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003712 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003713 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003714 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 }
3716
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003717 private static class StopInfo implements Runnable {
3718 ActivityClientRecord activity;
3719 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003720 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003722
3723 @Override public void run() {
3724 // Tell activity manager we have been stopped.
3725 try {
3726 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3727 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003728 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003729 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003730 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003731 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003732 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003733 return;
3734 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003735 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003736 }
3737 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003738 }
3739
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003740 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003741 public final IActivityManager.ContentProviderHolder holder;
3742 public final ProviderClientRecord client;
3743 public int stableCount;
3744 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003745
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003746 // When this is set, the stable and unstable ref counts are 0 and
3747 // we have a pending operation scheduled to remove the ref count
3748 // from the activity manager. On the activity manager we are still
3749 // holding an unstable ref, though it is not reflected in the counts
3750 // here.
3751 public boolean removePending;
3752
3753 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3754 ProviderClientRecord inClient, int sCount, int uCount) {
3755 holder = inHolder;
3756 client = inClient;
3757 stableCount = sCount;
3758 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 }
3760 }
3761
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003762 /**
3763 * Core implementation of stopping an activity. Note this is a little
3764 * tricky because the server's meaning of stop is slightly different
3765 * than our client -- for the server, stop means to save state and give
3766 * it the result when it is done, but the window may still be visible.
3767 * For the client, we want to call onStop()/onStart() to indicate when
3768 * the activity's UI visibillity changes.
3769 */
Romain Guy65b345f2011-07-27 18:51:50 -07003770 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003771 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003772 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 if (r != null) {
3774 if (!keepShown && r.stopped) {
3775 if (r.activity.mFinished) {
3776 // If we are finishing, we won't call onResume() in certain
3777 // cases. So here we likewise don't want to call onStop()
3778 // if the activity isn't resumed.
3779 return;
3780 }
3781 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07003782 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003783 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003784 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07003785 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003786 }
3787
3788 if (info != null) {
3789 try {
3790 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003791 // For now, don't create the thumbnail here; we are
3792 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 info.description = r.activity.onCreateDescription();
3794 } catch (Exception e) {
3795 if (!mInstrumentation.onException(r.activity, e)) {
3796 throw new RuntimeException(
3797 "Unable to save state of activity "
3798 + r.intent.getComponent().toShortString()
3799 + ": " + e.toString(), e);
3800 }
3801 }
3802 }
3803
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003804 // Next have the activity save its current state and managed dialogs...
3805 if (!r.activity.mFinished && saveState) {
3806 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003807 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003808 }
3809 }
3810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 if (!keepShown) {
3812 try {
3813 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003814 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 } catch (Exception e) {
3816 if (!mInstrumentation.onException(r.activity, e)) {
3817 throw new RuntimeException(
3818 "Unable to stop activity "
3819 + r.intent.getComponent().toShortString()
3820 + ": " + e.toString(), e);
3821 }
3822 }
3823 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003824 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3825 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827
3828 r.paused = true;
3829 }
3830 }
3831
Romain Guy65b345f2011-07-27 18:51:50 -07003832 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 View v = r.activity.mDecor;
3834 if (v != null) {
3835 if (show) {
3836 if (!r.activity.mVisibleFromServer) {
3837 r.activity.mVisibleFromServer = true;
3838 mNumVisibleActivities++;
3839 if (r.activity.mVisibleFromClient) {
3840 r.activity.makeVisible();
3841 }
3842 }
3843 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003844 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003845 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08003846 + r.activityInfo.name + " with new config "
3847 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 r.newConfig = null;
3849 }
3850 } else {
3851 if (r.activity.mVisibleFromServer) {
3852 r.activity.mVisibleFromServer = false;
3853 mNumVisibleActivities--;
3854 v.setVisibility(View.INVISIBLE);
3855 }
3856 }
3857 }
3858 }
3859
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003860 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003861 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003862 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3863 return;
3864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 r.activity.mConfigChangeFlags |= configChanges;
3866
3867 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003868 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003869
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003870 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 TAG, "Finishing stop of " + r + ": show=" + show
3872 + " win=" + r.window);
3873
3874 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003875
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003876 // Make sure any pending writes are now committed.
3877 if (!r.isPreHoneycomb()) {
3878 QueuedWork.waitToFinish();
3879 }
3880
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003881 // Schedule the call to tell the activity manager we have
3882 // stopped. We don't do this immediately, because we want to
3883 // have a chance for any other pending work (in particular memory
3884 // trim requests) to complete before you tell the activity
3885 // manager to proceed and allow us to go fully into the background.
3886 info.activity = r;
3887 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003888 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003889 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003890 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 }
3892
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003893 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3894 String action) {
3895 if (r == null) {
3896 return true;
3897 }
3898 if (seq < r.lastProcessedSeq) {
3899 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3900 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3901 return false;
3902 }
3903 r.lastProcessedSeq = seq;
3904 return true;
3905 }
3906
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003907 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003908 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003909 if (r.stopped) {
3910 r.activity.performRestart();
3911 r.stopped = false;
3912 }
3913 }
3914
Romain Guy65b345f2011-07-27 18:51:50 -07003915 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003916 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003917
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003918 if (r == null) {
3919 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3920 return;
3921 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003923 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003924 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 } else if (show && r.stopped) {
3926 // If we are getting ready to gc after going to the background, well
3927 // we are back active so skip it.
3928 unscheduleGcIdler();
3929
3930 r.activity.performRestart();
3931 r.stopped = false;
3932 }
3933 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003934 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003935 TAG, "Handle window " + r + " visibility: " + show);
3936 updateVisibility(r, show);
3937 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003938 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940
Romain Guy65b345f2011-07-27 18:51:50 -07003941 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003942 ActivityClientRecord r = mActivities.get(token);
3943
3944 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003945 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003946 return;
3947 }
3948
3949 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003950 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003951 try {
3952 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003953 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003954 } catch (Exception e) {
3955 if (!mInstrumentation.onException(r.activity, e)) {
3956 throw new RuntimeException(
3957 "Unable to stop activity "
3958 + r.intent.getComponent().toShortString()
3959 + ": " + e.toString(), e);
3960 }
3961 }
3962 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003963 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3964 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003965 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003966
3967 // Make sure any pending writes are now committed.
3968 if (!r.isPreHoneycomb()) {
3969 QueuedWork.waitToFinish();
3970 }
3971
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003972 // Tell activity manager we slept.
3973 try {
3974 ActivityManagerNative.getDefault().activitySlept(r.token);
3975 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003976 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003977 }
3978 } else {
3979 if (r.stopped && r.activity.mVisibleFromServer) {
3980 r.activity.performRestart();
3981 r.stopped = false;
3982 }
3983 }
3984 }
3985
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003986 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003987 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003988 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003989 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003990 onCoreSettingsChange();
3991 }
3992
3993 private void onCoreSettingsChange() {
3994 boolean debugViewAttributes =
3995 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3996 if (debugViewAttributes != View.mDebugViewAttributes) {
3997 View.mDebugViewAttributes = debugViewAttributes;
3998
3999 // request all activities to relaunch for the changes to take place
4000 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004001 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
4002 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07004003 }
4004 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004005 }
4006
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004007 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4008 LoadedApk apk = peekPackageInfo(data.pkg, false);
4009 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004010 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004011 }
4012 apk = peekPackageInfo(data.pkg, true);
4013 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004014 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004015 }
4016 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004017 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004018 }
4019
Romain Guy65b345f2011-07-27 18:51:50 -07004020 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004021 final int N = results.size();
4022 for (int i=0; i<N; i++) {
4023 ResultInfo ri = results.get(i);
4024 try {
4025 if (ri.mData != null) {
4026 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004027 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004029 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004030 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 r.activity.dispatchActivityResult(ri.mResultWho,
4032 ri.mRequestCode, ri.mResultCode, ri.mData);
4033 } catch (Exception e) {
4034 if (!mInstrumentation.onException(r.activity, e)) {
4035 throw new RuntimeException(
4036 "Failure delivering result " + ri + " to activity "
4037 + r.intent.getComponent().toShortString()
4038 + ": " + e.toString(), e);
4039 }
4040 }
4041 }
4042 }
4043
Romain Guy65b345f2011-07-27 18:51:50 -07004044 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004045 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004046 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 if (r != null) {
4048 final boolean resumed = !r.paused;
4049 if (!r.activity.mFinished && r.activity.mDecor != null
4050 && r.hideForNow && resumed) {
4051 // We had hidden the activity because it started another
4052 // one... we have gotten a result back and we are not
4053 // paused, so make sure our window is visible.
4054 updateVisibility(r, true);
4055 }
4056 if (resumed) {
4057 try {
4058 // Now we are idle.
4059 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004060 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004061 mInstrumentation.callActivityOnPause(r.activity);
4062 if (!r.activity.mCalled) {
4063 throw new SuperNotCalledException(
4064 "Activity " + r.intent.getComponent().toShortString()
4065 + " did not call through to super.onPause()");
4066 }
4067 } catch (SuperNotCalledException e) {
4068 throw e;
4069 } catch (Exception e) {
4070 if (!mInstrumentation.onException(r.activity, e)) {
4071 throw new RuntimeException(
4072 "Unable to pause activity "
4073 + r.intent.getComponent().toShortString()
4074 + ": " + e.toString(), e);
4075 }
4076 }
4077 }
4078 deliverResults(r, res.results);
4079 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004080 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004081 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 }
4083 }
4084 }
4085
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004086 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 return performDestroyActivity(token, finishing, 0, false);
4088 }
4089
Romain Guy65b345f2011-07-27 18:51:50 -07004090 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004091 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004092 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004093 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004094 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004095 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004096 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004097 r.activity.mConfigChangeFlags |= configChanges;
4098 if (finishing) {
4099 r.activity.mFinished = true;
4100 }
4101 if (!r.paused) {
4102 try {
4103 r.activity.mCalled = false;
4104 mInstrumentation.callActivityOnPause(r.activity);
Craig Mautnere3119b72015-01-20 15:02:36 -08004105 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004106 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 if (!r.activity.mCalled) {
4108 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004109 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 + " did not call through to super.onPause()");
4111 }
4112 } catch (SuperNotCalledException e) {
4113 throw e;
4114 } catch (Exception e) {
4115 if (!mInstrumentation.onException(r.activity, e)) {
4116 throw new RuntimeException(
4117 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004118 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 + ": " + e.toString(), e);
4120 }
4121 }
4122 r.paused = true;
4123 }
4124 if (!r.stopped) {
4125 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004126 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004127 } catch (SuperNotCalledException e) {
4128 throw e;
4129 } catch (Exception e) {
4130 if (!mInstrumentation.onException(r.activity, e)) {
4131 throw new RuntimeException(
4132 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004133 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 + ": " + e.toString(), e);
4135 }
4136 }
4137 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004138 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4139 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004140 }
4141 if (getNonConfigInstance) {
4142 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004143 r.lastNonConfigurationInstances
4144 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 } catch (Exception e) {
4146 if (!mInstrumentation.onException(r.activity, e)) {
4147 throw new RuntimeException(
4148 "Unable to retain activity "
4149 + r.intent.getComponent().toShortString()
4150 + ": " + e.toString(), e);
4151 }
4152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 }
4154 try {
4155 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004156 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004157 if (!r.activity.mCalled) {
4158 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004159 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004160 " did not call through to super.onDestroy()");
4161 }
4162 if (r.window != null) {
4163 r.window.closeAllPanels();
4164 }
4165 } catch (SuperNotCalledException e) {
4166 throw e;
4167 } catch (Exception e) {
4168 if (!mInstrumentation.onException(r.activity, e)) {
4169 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004170 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4171 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 }
4173 }
4174 }
4175 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004176 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004177 return r;
4178 }
4179
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004180 private static String safeToComponentShortString(Intent intent) {
4181 ComponentName component = intent.getComponent();
4182 return component == null ? "[Unknown]" : component.toShortString();
4183 }
4184
Romain Guy65b345f2011-07-27 18:51:50 -07004185 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004187 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 configChanges, getNonConfigInstance);
4189 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004190 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004191 WindowManager wm = r.activity.getWindowManager();
4192 View v = r.activity.mDecor;
4193 if (v != null) {
4194 if (r.activity.mVisibleFromServer) {
4195 mNumVisibleActivities--;
4196 }
4197 IBinder wtoken = v.getWindowToken();
4198 if (r.activity.mWindowAdded) {
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004199 if (r.onlyLocalRequest || r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004200 // Hold off on removing this until the new activity's
4201 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004202 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004203 r.mPendingRemoveWindowManager = wm;
Filip Gruszczynski14418da2015-10-04 16:43:48 -07004204 if (r.mPreserveWindow) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004205 // We can only keep the part of the view hierarchy that we control,
4206 // everything else must be removed, because it might not be able to
4207 // behave properly when activity is relaunching.
4208 r.window.clearContentView();
4209 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004210 } else {
4211 wm.removeViewImmediate(v);
4212 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004213 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004214 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004215 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004216 r.activity.getClass().getName(), "Activity");
4217 }
4218 r.activity.mDecor = null;
4219 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004220 if (r.mPendingRemoveWindow == null) {
4221 // If we are delaying the removal of the activity window, then
4222 // we can't clean up all windows here. Note that we can't do
4223 // so later either, which means any windows that aren't closed
4224 // by the app will leak. Well we try to warning them a lot
4225 // about leaking windows, because that is a bug, so if they are
4226 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004227 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004228 r.activity.getClass().getName(), "Activity");
4229 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004230
4231 // Mocked out contexts won't be participating in the normal
4232 // process lifecycle, but if we're running with a proper
4233 // ApplicationContext we need to have it tear down things
4234 // cleanly.
4235 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004236 if (c instanceof ContextImpl) {
4237 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004238 r.activity.getClass().getName(), "Activity");
4239 }
4240 }
4241 if (finishing) {
4242 try {
4243 ActivityManagerNative.getDefault().activityDestroyed(token);
4244 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004245 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
4247 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004248 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 }
4250
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004251 /**
4252 * @param preserveWindow Whether the activity should try to reuse the window it created,
4253 * including the decor view after the relaunch.
4254 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004255 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004256 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004257 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004258 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004259 ActivityClientRecord target = null;
4260
Craig Mautner88c05892013-06-28 09:47:45 -07004261 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004262 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4263 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004264 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004265 if (r.token == token) {
4266 target = r;
4267 if (pendingResults != null) {
4268 if (r.pendingResults != null) {
4269 r.pendingResults.addAll(pendingResults);
4270 } else {
4271 r.pendingResults = pendingResults;
4272 }
4273 }
4274 if (pendingNewIntents != null) {
4275 if (r.pendingIntents != null) {
4276 r.pendingIntents.addAll(pendingNewIntents);
4277 } else {
4278 r.pendingIntents = pendingNewIntents;
4279 }
4280 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004281
4282 // For each relaunch request, activity manager expects an answer
4283 if (!r.onlyLocalRequest && fromServer) {
4284 try {
4285 ActivityManagerNative.getDefault().activityRelaunched(token);
4286 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004287 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004288 }
4289 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004290 break;
4291 }
4292 }
4293
4294 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004295 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4296 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004297 target = new ActivityClientRecord();
4298 target.token = token;
4299 target.pendingResults = pendingResults;
4300 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004301 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004302 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004303 final ActivityClientRecord existing = mActivities.get(token);
4304 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004305 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004306 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4307 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004308 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004309 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004310 }
4311 target.onlyLocalRequest = true;
4312 }
4313 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004314 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004315 }
4316
4317 if (fromServer) {
4318 target.startsNotResumed = notResumed;
4319 target.onlyLocalRequest = false;
4320 }
4321 if (config != null) {
4322 target.createdConfig = config;
4323 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004324 if (overrideConfig != null) {
4325 target.overrideConfig = overrideConfig;
4326 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004327 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004328 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004329 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004330 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4331 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004332 }
4333
Romain Guy65b345f2011-07-27 18:51:50 -07004334 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 // If we are getting ready to gc after going to the background, well
4336 // we are back active so skip it.
4337 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004338 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004339
4340 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004341 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343 // First: make sure we have the most recent configuration and most
4344 // recent version of the activity, or skip it if some previous call
4345 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004346 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004347 int N = mRelaunchingActivities.size();
4348 IBinder token = tmp.token;
4349 tmp = null;
4350 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004351 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004352 if (r.token == token) {
4353 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004354 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004355 mRelaunchingActivities.remove(i);
4356 i--;
4357 N--;
4358 }
4359 }
Bob Leee5408332009-09-04 18:31:17 -07004360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004362 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004363 return;
4364 }
Bob Leee5408332009-09-04 18:31:17 -07004365
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004366 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4367 + tmp.token + " with configChanges=0x"
4368 + Integer.toHexString(configChanges));
4369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 if (mPendingConfiguration != null) {
4371 changedConfig = mPendingConfiguration;
4372 mPendingConfiguration = null;
4373 }
4374 }
Bob Leee5408332009-09-04 18:31:17 -07004375
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004376 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4377 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4378 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4379 } else {
4380 tmp.lastProcessedSeq = tmp.relaunchSeq;
4381 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004382 if (tmp.createdConfig != null) {
4383 // If the activity manager is passing us its current config,
4384 // assume that is really what we want regardless of what we
4385 // may have pending.
4386 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004387 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4388 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4389 if (changedConfig == null
4390 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4391 changedConfig = tmp.createdConfig;
4392 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004393 }
4394 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004395
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004396 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004397 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399 // If there was a pending configuration change, execute it first.
4400 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004401 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004402 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004403 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004404 }
Bob Leee5408332009-09-04 18:31:17 -07004405
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004406 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004407 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004409 if (!tmp.onlyLocalRequest) {
4410 try {
4411 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4412 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004413 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004414 }
4415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004416 return;
4417 }
Bob Leee5408332009-09-04 18:31:17 -07004418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004420 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004421 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004422 r.lastProcessedSeq = tmp.lastProcessedSeq;
4423 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004424 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004425
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004426 r.activity.mChangingConfigurations = true;
4427
Robert Carr23fa16b2016-01-13 13:19:58 -08004428 // If we are preserving the main window across relaunches we would also like to preserve
4429 // the children. However the client side view system does not support preserving
4430 // the child views so we notify the window manager to expect these windows to
4431 // be replaced and defer requests to destroy or hide them. This way we can achieve
4432 // visual continuity. It's important that we do this here prior to pause and destroy
4433 // as that is when we may hide or remove the child views.
4434 try {
4435 if (r.mPreserveWindow) {
4436 WindowManagerGlobal.getWindowSession().prepareToReplaceChildren(r.token);
4437 }
4438 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004439 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004440 }
4441
4442
Dianne Hackborne2b04802010-12-09 09:24:55 -08004443 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004445 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004446 }
4447 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004448 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 }
Bob Leee5408332009-09-04 18:31:17 -07004450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 r.activity = null;
4454 r.window = null;
4455 r.hideForNow = false;
4456 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004457 // Merge any pending results and pending intents; don't just replace them
4458 if (tmp.pendingResults != null) {
4459 if (r.pendingResults == null) {
4460 r.pendingResults = tmp.pendingResults;
4461 } else {
4462 r.pendingResults.addAll(tmp.pendingResults);
4463 }
4464 }
4465 if (tmp.pendingIntents != null) {
4466 if (r.pendingIntents == null) {
4467 r.pendingIntents = tmp.pendingIntents;
4468 } else {
4469 r.pendingIntents.addAll(tmp.pendingIntents);
4470 }
4471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004473 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004474
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004475 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004476
4477 if (!tmp.onlyLocalRequest) {
4478 try {
4479 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004480 if (r.window != null) {
4481 r.window.reportActivityRelaunched();
4482 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004483 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004484 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004485 }
4486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004487 }
4488
Craig Mautnera0026042014-04-23 11:45:37 -07004489 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4490 r.state = new Bundle();
4491 r.state.setAllowFds(false);
4492 if (r.isPersistable()) {
4493 r.persistentState = new PersistableBundle();
4494 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4495 r.persistentState);
4496 } else {
4497 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4498 }
4499 }
4500
Dianne Hackborn73c14162012-09-19 15:45:06 -07004501 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004502 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004503 ArrayList<ComponentCallbacks2> callbacks
4504 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004505
Craig Mautner88c05892013-06-28 09:47:45 -07004506 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004507 final int NAPP = mAllApplications.size();
4508 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004509 callbacks.add(mAllApplications.get(i));
4510 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004511 final int NACT = mActivities.size();
4512 for (int i=0; i<NACT; i++) {
4513 ActivityClientRecord ar = mActivities.valueAt(i);
4514 Activity a = ar.activity;
4515 if (a != null) {
4516 Configuration thisConfig = applyConfigCompatMainThread(
4517 mCurDefaultDisplayDpi, newConfig,
4518 ar.packageInfo.getCompatibilityInfo());
4519 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4520 // If the activity is currently resumed, its configuration
4521 // needs to change right now.
4522 callbacks.add(a);
4523 } else if (thisConfig != null) {
4524 // Otherwise, we will tell it about the change
4525 // the next time it is resumed or shown. Note that
4526 // the activity manager may, before then, decide the
4527 // activity needs to be destroyed to handle its new
4528 // configuration.
4529 if (DEBUG_CONFIGURATION) {
4530 Slog.v(TAG, "Setting activity "
4531 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004532 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004533 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 }
4535 }
4536 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004537 final int NSVC = mServices.size();
4538 for (int i=0; i<NSVC; i++) {
4539 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 }
4541 }
4542 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004543 final int NPRV = mLocalProviders.size();
4544 for (int i=0; i<NPRV; i++) {
4545 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 }
4547 }
Bob Leee5408332009-09-04 18:31:17 -07004548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004549 return callbacks;
4550 }
Bob Leee5408332009-09-04 18:31:17 -07004551
Adam Lesinski082614c2016-03-04 14:33:47 -08004552 /**
4553 * Updates the configuration for an Activity. The ActivityClientRecord's
4554 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4555 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4556 * the updated Configuration.
4557 * @param r ActivityClientRecord representing the Activity.
4558 * @param newBaseConfig The new configuration to use. This may be augmented with
4559 * {@link ActivityClientRecord#overrideConfig}.
4560 * @param reportToActivity true if the change should be reported to the Activity's callback.
4561 */
4562 private void performConfigurationChangedForActivity(ActivityClientRecord r,
4563 Configuration newBaseConfig,
4564 boolean reportToActivity) {
4565 r.tmpConfig.setTo(newBaseConfig);
4566 if (r.overrideConfig != null) {
4567 r.tmpConfig.updateFrom(r.overrideConfig);
4568 }
4569 performConfigurationChanged(r.activity, r.token, r.tmpConfig, r.overrideConfig,
4570 reportToActivity);
4571 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
4572 }
4573
4574 /**
4575 * Decides whether to update an Activity's configuration and whether to tell the
4576 * Activity/Component about it.
4577 * @param cb The component callback to notify of configuration change.
4578 * @param activityToken The Activity binder token for which this configuration change happened.
4579 * If the change is global, this is null.
4580 * @param newConfig The new configuration.
4581 * @param overrideConfig The override config that differentiates the Activity's configuration
4582 * from the base global configuration.
4583 * @param reportToActivity Notify the Activity of the change.
4584 */
4585 private void performConfigurationChanged(ComponentCallbacks2 cb,
4586 IBinder activityToken,
4587 Configuration newConfig,
4588 Configuration overrideConfig,
4589 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004590 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004591 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004592 // we check the runtime type and act accordingly.
4593 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4594 if (activity != null) {
4595 activity.mCalled = false;
4596 }
Bob Leee5408332009-09-04 18:31:17 -07004597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 boolean shouldChangeConfig = false;
4599 if ((activity == null) || (activity.mCurrentConfig == null)) {
4600 shouldChangeConfig = true;
4601 } else {
Bob Leee5408332009-09-04 18:31:17 -07004602
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 // If the new config is the same as the config this Activity
4604 // is already running with then don't bother calling
4605 // onConfigurationChanged
Adam Lesinski082614c2016-03-04 14:33:47 -08004606 int diff = activity.mCurrentConfig.diff(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004607 if (diff != 0) {
Wale Ogunwale14e811f2015-12-14 15:04:22 -08004608 // If this activity doesn't handle any of the config changes then don't bother
4609 // calling onConfigurationChanged as we're going to destroy it.
4610 // Except in the case where the configuration changed on the activity manager side,
4611 // but wasn't big enough to cause a resource change so the activity wasn't destroyed.
4612 // In this case we still want to change the configuration of the activity but not
4613 // report it to the app.
4614 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4615 || !reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004616 shouldChangeConfig = true;
4617 }
4618 }
4619 }
Bob Leee5408332009-09-04 18:31:17 -07004620
Adam Lesinski082614c2016-03-04 14:33:47 -08004621 if (DEBUG_CONFIGURATION) {
4622 Slog.v(TAG, "Config callback " + cb + ": shouldChangeConfig=" + shouldChangeConfig);
4623 }
4624
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 if (shouldChangeConfig) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004626 if (activityToken != null) {
4627 // We only update an Activity's configuration if this is not a global
4628 // configuration change. This must also be done before the callback,
4629 // or else we violate the contract that the new resources are available
4630 // in {@link ComponentCallbacks2#onConfigurationChanged(Configuration)}.
4631 mResourcesManager.updateResourcesForActivity(activityToken, overrideConfig);
4632 }
4633
Filip Gruszczynskica664812015-12-04 12:43:36 -08004634 if (reportToActivity) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004635 cb.onConfigurationChanged(newConfig);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004636 }
Bob Leee5408332009-09-04 18:31:17 -07004637
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004639 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004640 throw new SuperNotCalledException(
4641 "Activity " + activity.getLocalClassName() +
Adam Lesinski082614c2016-03-04 14:33:47 -08004642 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004643 }
4644 activity.mConfigChangeFlags = 0;
Adam Lesinski082614c2016-03-04 14:33:47 -08004645 activity.mCurrentConfig = new Configuration(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
4647 }
4648 }
4649
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004650 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004651 synchronized (mResourcesManager) {
4652 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004653 }
4654 }
4655
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004656 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004657 Configuration config = mConfiguration;
4658 if (mCompatConfiguration == null) {
4659 mCompatConfiguration = new Configuration();
4660 }
4661 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004662 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4663 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004664 config = mCompatConfiguration;
4665 }
4666 return config;
4667 }
4668
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004669 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004670
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004671 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004672
Craig Mautner88c05892013-06-28 09:47:45 -07004673 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004674 if (mPendingConfiguration != null) {
4675 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4676 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004677 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004678 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004679 }
4680 mPendingConfiguration = null;
4681 }
4682
4683 if (config == null) {
4684 return;
4685 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004686
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004687 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004688 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004689
4690 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004692 if (mConfiguration == null) {
4693 mConfiguration = new Configuration();
4694 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004695 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004696 return;
4697 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004698
4699 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004700 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004701
4702 final Theme systemTheme = getSystemContext().getTheme();
4703 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4704 systemTheme.rebase();
4705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004707
4708 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4709
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004710 freeTextLayoutCachesIfNeeded(configDiff);
4711
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004712 if (callbacks != null) {
4713 final int N = callbacks.size();
4714 for (int i=0; i<N; i++) {
Adam Lesinski082614c2016-03-04 14:33:47 -08004715 performConfigurationChanged(callbacks.get(i), null, config, null,
4716 REPORT_TO_ACTIVITY);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004718 }
4719 }
4720
Romain Guy46bfc482013-08-16 18:38:29 -07004721 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004722 if (configDiff != 0) {
4723 // Ask text layout engine to free its caches if there is a locale change
4724 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4725 if (hasLocaleConfigChange) {
4726 Canvas.freeTextLayoutCaches();
4727 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4728 }
4729 }
4730 }
4731
Filip Gruszczynskica664812015-12-04 12:43:36 -08004732 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4733 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004734 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 if (r == null || r.activity == null) {
4736 return;
4737 }
Bob Leee5408332009-09-04 18:31:17 -07004738
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004739 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004740 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004741
Adam Lesinski082614c2016-03-04 14:33:47 -08004742 r.overrideConfig = data.overrideConfig;
4743 performConfigurationChangedForActivity(r, mCompatConfiguration, reportToActivity);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004744 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 }
4746
Jeff Hao1b012d32014-08-20 10:35:34 -07004747 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004748 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004749 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004750 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004751 default:
4752 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004753 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004754 break;
4755 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004756 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004757 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004758 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004759 } finally {
4760 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004761 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004762 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004763 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004764 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004765 }
4766 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004767 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004768 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004769 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004770 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004771 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004772 }
4773 }
Bob Leee5408332009-09-04 18:31:17 -07004774
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004775 /**
4776 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4777 * so that profiler data won't be lost.
4778 *
4779 * @hide
4780 */
4781 public void stopProfiling() {
4782 mProfiler.stopProfiling();
4783 }
4784
Romain Guya998dff2012-03-23 18:58:36 -07004785 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004786 if (managed) {
4787 try {
4788 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4789 } catch (IOException e) {
4790 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4791 + " -- can the process access this path?");
4792 } finally {
4793 try {
4794 dhd.fd.close();
4795 } catch (IOException e) {
4796 Slog.w(TAG, "Failure closing profile fd", e);
4797 }
4798 }
4799 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004800 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004801 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004802 try {
4803 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4804 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004805 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004806 }
Andy McFadden824c5102010-07-09 16:26:57 -07004807 }
4808
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004809 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4810 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08004811 switch (cmd) {
4812 case IApplicationThread.PACKAGE_REMOVED:
4813 case IApplicationThread.PACKAGE_REMOVED_DONT_KILL:
4814 {
4815 final boolean killApp = cmd == IApplicationThread.PACKAGE_REMOVED;
4816 if (packages == null) {
4817 break;
4818 }
4819 synchronized (mResourcesManager) {
4820 for (int i = packages.length - 1; i >= 0; i--) {
4821 if (!hasPkgInfo) {
4822 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4823 if (ref != null && ref.get() != null) {
4824 hasPkgInfo = true;
4825 } else {
4826 ref = mResourcePackages.get(packages[i]);
4827 if (ref != null && ref.get() != null) {
4828 hasPkgInfo = true;
4829 }
4830 }
4831 }
4832 if (killApp) {
4833 mPackages.remove(packages[i]);
4834 mResourcePackages.remove(packages[i]);
4835 }
4836 }
4837 }
4838 break;
4839 }
4840 case IApplicationThread.PACKAGE_REPLACED:
4841 {
4842 if (packages == null) {
4843 break;
4844 }
4845 synchronized (mResourcesManager) {
4846 for (int i = packages.length - 1; i >= 0; i--) {
4847 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4848 LoadedApk pkgInfo = ref != null ? ref.get() : null;
4849 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004850 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004851 } else {
4852 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08004853 pkgInfo = ref != null ? ref.get() : null;
4854 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08004855 hasPkgInfo = true;
4856 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004857 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004858 // If the package is being replaced, yet it still has a valid
4859 // LoadedApk object, the package was updated with _DONT_KILL.
4860 // Adjust it's internal references to the application info and
4861 // resources.
4862 if (pkgInfo != null) {
4863 try {
4864 final String packageName = packages[i];
4865 final ApplicationInfo aInfo =
4866 sPackageManager.getApplicationInfo(
4867 packageName,
4868 0 /*flags*/,
4869 UserHandle.myUserId());
4870
4871 if (mActivities.size() > 0) {
4872 for (ActivityClientRecord ar : mActivities.values()) {
4873 if (ar.activityInfo.applicationInfo.packageName
4874 .equals(packageName)) {
4875 ar.activityInfo.applicationInfo = aInfo;
4876 ar.packageInfo = pkgInfo;
4877 }
4878 }
4879 }
4880 final List<String> oldPaths =
4881 sPackageManager.getPreviousCodePaths(packageName);
4882 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
4883 } catch (RemoteException e) {
4884 }
4885 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004886 }
4887 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004888 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004889 }
4890 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004891 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004892 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004894 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004895 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 final int N = callbacks.size();
4898 for (int i=0; i<N; i++) {
4899 callbacks.get(i).onLowMemory();
4900 }
4901
Chris Tatece229052009-03-25 16:44:52 -07004902 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4903 if (Process.myUid() != Process.SYSTEM_UID) {
4904 int sqliteReleased = SQLiteDatabase.releaseMemory();
4905 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4906 }
Bob Leee5408332009-09-04 18:31:17 -07004907
Mike Reedcaf0df12009-04-27 14:32:05 -04004908 // Ask graphics to free up as much as possible (font/image caches)
4909 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004911 // Ask text layout engine to free also as much as possible
4912 Canvas.freeTextLayoutCaches();
4913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004914 BinderInternal.forceGc("mem");
4915 }
4916
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004917 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004918 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004919
Dianne Hackborn73c14162012-09-19 15:45:06 -07004920 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004921
4922 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004923 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004924 callbacks.get(i).onTrimMemory(level);
4925 }
Romain Guy19f86e82012-04-23 15:19:07 -07004926
John Reckf47a5942014-06-30 16:20:04 -07004927 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004928 }
4929
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004930 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004931 if (Process.isIsolated()) {
4932 // Isolated processes aren't going to do UI.
4933 return;
4934 }
Romain Guya9582652011-11-10 14:20:10 -08004935 try {
4936 int uid = Process.myUid();
4937 String[] packages = getPackageManager().getPackagesForUid(uid);
4938
4939 // If there are several packages in this application we won't
Tim Murraye1e6c662015-04-07 13:24:14 -07004940 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004941 if (packages != null && packages.length == 1) {
John Reck51aaf902015-12-02 15:08:07 -08004942 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07004943 RenderScriptCacheDir.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004944 }
4945 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004946 throw e.rethrowFromSystemServer();
Romain Guya9582652011-11-10 14:20:10 -08004947 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004948 }
4949
Calin Juravled96d2dd2016-02-02 23:28:39 +00004950 // Keep in sync with installd (frameworks/native/cmds/installd/commands.cpp).
4951 private static File getPrimaryProfileFile(String packageName) {
Calin Juravled479b522016-02-24 16:22:03 +00004952 File profileDir = Environment.getDataProfilesDePackageDirectory(
4953 UserHandle.myUserId(), packageName);
4954 return new File(profileDir, "primary.prof");
Calin Juravled96d2dd2016-02-02 23:28:39 +00004955 }
4956
Calin Juravle69052392015-12-17 17:06:49 +02004957 private static void setupJitProfileSupport(LoadedApk loadedApk, File cacheDir) {
Calin Juravledb4a79a2015-12-23 18:55:08 +02004958 if (!SystemProperties.getBoolean("dalvik.vm.usejitprofiles", false)) {
4959 return;
4960 }
Calin Juravle69052392015-12-17 17:06:49 +02004961 final ApplicationInfo appInfo = loadedApk.getApplicationInfo();
Calin Juravle69052392015-12-17 17:06:49 +02004962 final List<String> codePaths = new ArrayList<>();
4963 if ((appInfo.flags & ApplicationInfo.FLAG_HAS_CODE) != 0) {
4964 codePaths.add(appInfo.sourceDir);
4965 }
4966 if (appInfo.splitSourceDirs != null) {
4967 Collections.addAll(codePaths, appInfo.splitSourceDirs);
4968 }
4969
4970 if (codePaths.isEmpty()) {
4971 // If there are no code paths there's no need to setup a profile file and register with
4972 // the runtime,
4973 return;
4974 }
4975
Calin Juravled96d2dd2016-02-02 23:28:39 +00004976 final File profileFile = getPrimaryProfileFile(loadedApk.mPackageName);
Calin Juravle69052392015-12-17 17:06:49 +02004977 if (!profileFile.exists()) {
4978 FileDescriptor fd = null;
4979 try {
4980 final int permissions = 0600; // read-write for user.
4981 fd = Os.open(profileFile.getAbsolutePath(), OsConstants.O_CREAT, permissions);
4982 Os.fchmod(fd, permissions);
4983 Os.fchown(fd, appInfo.uid, appInfo.uid);
4984 } catch (ErrnoException e) {
Calin Juravle6bd212b2016-02-29 16:38:28 +00004985 Log.v(TAG, "Unable to create jit profile file "
4986 + profileFile + ": " + e.getMessage());
Calin Juravle69052392015-12-17 17:06:49 +02004987 try {
4988 Os.unlink(profileFile.getAbsolutePath());
4989 } catch (ErrnoException unlinkErr) {
Calin Juravle6bd212b2016-02-29 16:38:28 +00004990 if (unlinkErr.errno != OsConstants.ENOENT) {
4991 Log.v(TAG, "Unable to unlink jit profile file "
4992 + profileFile + ": " + unlinkErr.getMessage());
4993 }
Calin Juravle69052392015-12-17 17:06:49 +02004994 }
4995 return;
4996 } finally {
4997 IoUtils.closeQuietly(fd);
4998 }
4999 }
5000
Calin Juravled479b522016-02-24 16:22:03 +00005001 final File foreignDexProfilesFile =
5002 Environment.getDataProfilesDeForeignDexDirectory(UserHandle.myUserId());
5003 String foreignDexProfilesPath = null;
5004 if (!foreignDexProfilesFile.exists()) {
5005 Log.v(TAG, "ForeignDexProfilesPath does not exists:" +
5006 foreignDexProfilesFile.getPath());
5007 } else {
5008 foreignDexProfilesPath = foreignDexProfilesFile.getAbsolutePath();
5009 }
Calin Juravle69052392015-12-17 17:06:49 +02005010 VMRuntime.registerAppInfo(profileFile.getAbsolutePath(), appInfo.dataDir,
Calin Juravled479b522016-02-24 16:22:03 +00005011 codePaths.toArray(new String[codePaths.size()]), foreignDexProfilesPath);
Calin Juravle69052392015-12-17 17:06:49 +02005012 }
5013
Dianne Hackborndde331c2012-08-03 14:01:57 -07005014 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05005015 final int densityDpi = mCurDefaultDisplayDpi;
5016 if (!mDensityCompatMode
5017 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
5018 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
5019 DisplayMetrics.DENSITY_DEVICE = densityDpi;
5020 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005021 }
5022 }
5023
Romain Guy65b345f2011-07-27 18:51:50 -07005024 private void handleBindApplication(AppBindData data) {
Man Caocfa78b22015-06-11 20:14:34 -07005025 if (data.trackAllocation) {
5026 DdmVmInternal.enableRecentAllocations(true);
5027 }
5028
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005029 // Note when this process has started.
5030 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005032 mBoundApplication = data;
5033 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005034 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005036 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005037 if (data.initProfilerInfo != null) {
5038 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5039 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5040 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5041 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
5042 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005045 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005046 android.ddm.DdmHandleAppName.setAppName(data.processName,
5047 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005049 if (data.persistent) {
5050 // Persistent processes on low-memory devices do not get to
5051 // use hardware accelerated drawing, since this can add too much
5052 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005053 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005054 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005055 }
5056 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005057
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005058 if (mProfiler.profileFd != null) {
5059 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005060 }
5061
Joe Onoratod630f102011-03-17 18:42:26 -07005062 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5063 // implementation to use the pool executor. Normally, we use the
5064 // serialized executor as the default. This has to happen in the
5065 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005066 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005067 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5068 }
5069
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005070 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 /*
5073 * Before spawning a new process, reset the time zone to be the system time zone.
5074 * This needs to be done because the system time zone could have changed after the
5075 * the spawning of this process. Without doing this this process would have the incorrect
5076 * system time zone.
5077 */
5078 TimeZone.setDefault(null);
5079
Adam Lesinski082614c2016-03-04 14:33:47 -08005080 synchronized (mResourcesManager) {
5081 /*
5082 * Initialize the default locales in this process for the reasons we set the time zone.
5083 *
5084 * We do this through ResourcesManager, since we need to do locale negotiation.
5085 */
5086 mResourcesManager.setDefaultLocalesLocked(data.config.getLocales());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005087
Adam Lesinski082614c2016-03-04 14:33:47 -08005088 /*
5089 * Update the system configuration since its preloaded and might not
5090 * reflect configuration changes. The configuration object passed
5091 * in AppBindData can be safely assumed to be up to date
5092 */
5093 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5094 mCurDefaultDisplayDpi = data.config.densityDpi;
5095
5096 // This calls mResourcesManager so keep it within the synchronized block.
5097 applyCompatConfiguration(mCurDefaultDisplayDpi);
5098 }
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07005099
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005100 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101
Dianne Hackborndde331c2012-08-03 14:01:57 -07005102 /**
5103 * Switch this process to density compatibility mode if needed.
5104 */
5105 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5106 == 0) {
5107 mDensityCompatMode = true;
5108 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5109 }
5110 updateDefaultDensity();
5111
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005112 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
5113 DateFormat.set24HourTimePref(is24Hr);
5114
Jon Miranda836c0a82014-08-11 12:32:26 -07005115 View.mDebugViewAttributes =
5116 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5117
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005118 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005119 * For system applications on userdebug/eng builds, log stack
5120 * traces of disk and network access to dropbox for analysis.
5121 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005122 if ((data.appInfo.flags &
5123 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005124 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5125 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005126 }
5127
5128 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005129 * For apps targetting Honeycomb or later, we don't allow network usage
5130 * on the main event loop / UI thread. This is what ultimately throws
5131 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005132 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005133 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005134 StrictMode.enableDeathOnNetwork();
5135 }
5136
Jeff Sharkey344744b2016-01-28 19:03:30 -07005137 /**
5138 * For apps targetting N or later, we don't allow file:// Uri exposure.
5139 * This is what ultimately throws {@link FileUriExposedException}.
5140 */
5141 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005142 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005143 }
5144
Alex Klyubinf9034cc2015-02-12 11:43:09 -08005145 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
5146 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
5147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5149 // XXX should have option to change the port.
5150 Debug.changeDebugPort(8100);
5151 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005152 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005153 + " is waiting for the debugger on port 8100...");
5154
5155 IActivityManager mgr = ActivityManagerNative.getDefault();
5156 try {
5157 mgr.showWaitingForDebugger(mAppThread, true);
5158 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005159 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
5161
5162 Debug.waitForDebugger();
5163
5164 try {
5165 mgr.showWaitingForDebugger(mAppThread, false);
5166 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005167 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005168 }
5169
5170 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005171 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005172 + " can be debugged on port 8100...");
5173 }
5174 }
5175
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005176 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005177 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5178 Trace.setAppTracingAllowed(isAppDebuggable);
5179 if (isAppDebuggable && data.enableBinderTracking) {
5180 Binder.enableTracing();
5181 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005182
Robert Greenwalt434203a2010-10-11 16:00:27 -07005183 /**
5184 * Initialize the default http proxy in this process for the reasons we set the time zone.
5185 */
Alan Viverettebe64eae2015-09-03 14:56:04 -04005186 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005187 if (b != null) {
5188 // In pre-boot mode (doing initial launch to collect password), not
5189 // all system is up. This includes the connectivity service, so don't
5190 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005191 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005192 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005193 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005194 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005195 } catch (RemoteException e) {
5196 throw e.rethrowFromSystemServer();
5197 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005198 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07005199
Alan Viverette2107d692015-09-03 14:55:27 -04005200 // Instrumentation info affects the class loader, so load it before
5201 // setting up the app context.
5202 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005205 ii = new ApplicationPackageManager(null, getPackageManager())
5206 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005207 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005208 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005209 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005210 }
5211
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005212 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005214 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
5215 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005216 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005217 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5218 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005219 } else {
5220 ii = null;
5221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005222
Alan Viverette2107d692015-09-03 14:55:27 -04005223 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005224 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005225 // This cache location probably points at credential-encrypted
5226 // storage which may not be accessible yet; assign it anyway instead
5227 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005228 final File cacheDir = appContext.getCacheDir();
5229 if (cacheDir != null) {
5230 // Provide a usable directory for temporary files
5231 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5232 } else {
5233 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5234 + "due to missing cache directory");
5235 }
Alan Viverette346296b2015-09-01 13:08:05 -04005236
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005237 // Setup a location to store generated/compiled graphics code and
5238 // JIT profiling data. Note that this data is stored in a
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005239 // device-protected storage area, so these caches must never contain
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005240 // user sensitive user data.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005241 final Context deviceContext = appContext.createDeviceProtectedStorageContext();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005242 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005243 if (codeCacheDir != null) {
5244 setupGraphicsSupport(data.info, codeCacheDir);
Calin Juravlec74d3382016-01-07 12:15:36 +00005245 setupJitProfileSupport(data.info, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005246 } else {
Calin Juravlec74d3382016-01-07 12:15:36 +00005247 Log.e(TAG, "Unable to setupGraphicsSupport and setupJitProfileSupport " +
5248 "due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005249 }
Alan Viverette2107d692015-09-03 14:55:27 -04005250
Michael Lentine2ba303f2016-02-01 20:44:34 -06005251 // Add the lib dir path to hardware renderer so that vulkan layers
5252 // can be searched for within that directory.
5253 ThreadedRenderer.setLibDir(data.info.getLibDir());
5254 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005255
Chad Brubaker78d47122015-11-17 22:26:58 -08005256 // Install the Network Security Config Provider. This must happen before the application
5257 // code is loaded to prevent issues with instances of TLS objects being created before
5258 // the provider is installed.
5259 NetworkSecurityConfigProvider.install(appContext);
5260
Alan Viverette2107d692015-09-03 14:55:27 -04005261 // Continue loading instrumentation.
5262 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005263 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005264 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005265 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005266 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005267 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005268 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269
5270 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005271 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005272 mInstrumentation = (Instrumentation)
5273 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5274 } catch (Exception e) {
5275 throw new RuntimeException(
5276 "Unable to instantiate instrumentation "
5277 + data.instrumentationName + ": " + e.toString(), e);
5278 }
5279
Alan Viverettebe64eae2015-09-03 14:56:04 -04005280 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5281 mInstrumentation.init(this, instrContext, appContext, component,
5282 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005284 if (mProfiler.profileFile != null && !ii.handleProfiling
5285 && mProfiler.profileFd == null) {
5286 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005287 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005288 file.getParentFile().mkdirs();
5289 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005291 } else {
5292 mInstrumentation = new Instrumentation();
5293 }
5294
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005295 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005296 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005297 } else {
5298 // Small heap, clamp to the current growth limit and let the heap release
5299 // pages after the growth limit to the non growth limit capacity. b/18387825
5300 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005301 }
5302
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005303 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005304 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005305 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005306 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005308 // If the app is being launched for full backup or restore, bring it up in
5309 // a restricted environment with the base application class.
5310 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5311 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005312
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005313 // don't bring up providers in restricted mode; they may depend on the
5314 // app's custom Application class
5315 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005316 if (!ArrayUtils.isEmpty(data.providers)) {
5317 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005318 // For process that contains content providers, we want to
5319 // ensure that the JIT is enabled "at some point".
5320 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5321 }
5322 }
5323
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005324 // Do this after providers, since instrumentation tests generally start their
5325 // test thread at this point, and we don't want that racing.
5326 try {
5327 mInstrumentation.onCreate(data.instrumentationArgs);
5328 }
5329 catch (Exception e) {
5330 throw new RuntimeException(
5331 "Exception thrown in onCreate() of "
5332 + data.instrumentationName + ": " + e.toString(), e);
5333 }
5334
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005335 try {
5336 mInstrumentation.callApplicationOnCreate(app);
5337 } catch (Exception e) {
5338 if (!mInstrumentation.onException(app, e)) {
5339 throw new RuntimeException(
5340 "Unable to create application " + app.getClass().getName()
5341 + ": " + e.toString(), e);
5342 }
5343 }
5344 } finally {
5345 StrictMode.setThreadPolicy(savedPolicy);
5346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005347 }
5348
5349 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5350 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005351 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5352 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005353 Debug.stopMethodTracing();
5354 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005355 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005356 // + ", app thr: " + mAppThread);
5357 try {
5358 am.finishInstrumentation(mAppThread, resultCode, results);
5359 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005360 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 }
5362 }
5363
Romain Guy65b345f2011-07-27 18:51:50 -07005364 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005365 Context context, List<ProviderInfo> providers) {
5366 final ArrayList<IActivityManager.ContentProviderHolder> results =
5367 new ArrayList<IActivityManager.ContentProviderHolder>();
5368
Romain Guya998dff2012-03-23 18:58:36 -07005369 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005370 if (DEBUG_PROVIDER) {
5371 StringBuilder buf = new StringBuilder(128);
5372 buf.append("Pub ");
5373 buf.append(cpi.authority);
5374 buf.append(": ");
5375 buf.append(cpi.name);
5376 Log.i(TAG, buf.toString());
5377 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005378 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5379 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5380 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005381 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005382 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005383 }
5384 }
5385
5386 try {
5387 ActivityManagerNative.getDefault().publishContentProviders(
5388 getApplicationThread(), results);
5389 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005390 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005391 }
5392 }
5393
Jeff Sharkey6d515712012-09-20 16:06:08 -07005394 public final IContentProvider acquireProvider(
5395 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005396 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005397 if (provider != null) {
5398 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005399 }
5400
Wale Ogunwale1d646122015-04-24 14:45:14 -07005401 // There is a possible race here. Another thread may try to acquire
5402 // the same provider at the same time. When this happens, we want to ensure
5403 // that the first one wins.
5404 // Note that we cannot hold the lock while acquiring and installing the
5405 // provider since it might take a long time to run and it could also potentially
5406 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005408 try {
5409 holder = ActivityManagerNative.getDefault().getContentProvider(
5410 getApplicationThread(), auth, userId, stable);
5411 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005412 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005415 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 return null;
5417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005418
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005419 // Install provider will increment the reference count for us, and break
5420 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005421 holder = installProvider(c, holder, holder.info,
5422 true /*noisy*/, holder.noReleaseNeeded, stable);
5423 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005424 }
5425
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005426 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5427 if (stable) {
5428 prc.stableCount += 1;
5429 if (prc.stableCount == 1) {
5430 // We are acquiring a new stable reference on the provider.
5431 int unstableDelta;
5432 if (prc.removePending) {
5433 // We have a pending remove operation, which is holding the
5434 // last unstable reference. At this point we are converting
5435 // that unstable reference to our new stable reference.
5436 unstableDelta = -1;
5437 // Cancel the removal of the provider.
5438 if (DEBUG_PROVIDER) {
5439 Slog.v(TAG, "incProviderRef: stable "
5440 + "snatched provider from the jaws of death");
5441 }
5442 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005443 // There is a race! It fails to remove the message, which
5444 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005445 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5446 } else {
5447 unstableDelta = 0;
5448 }
5449 try {
5450 if (DEBUG_PROVIDER) {
5451 Slog.v(TAG, "incProviderRef Now stable - "
5452 + prc.holder.info.name + ": unstableDelta="
5453 + unstableDelta);
5454 }
5455 ActivityManagerNative.getDefault().refContentProvider(
5456 prc.holder.connection, 1, unstableDelta);
5457 } catch (RemoteException e) {
5458 //do nothing content provider object is dead any way
5459 }
5460 }
5461 } else {
5462 prc.unstableCount += 1;
5463 if (prc.unstableCount == 1) {
5464 // We are acquiring a new unstable reference on the provider.
5465 if (prc.removePending) {
5466 // Oh look, we actually have a remove pending for the
5467 // provider, which is still holding the last unstable
5468 // reference. We just need to cancel that to take new
5469 // ownership of the reference.
5470 if (DEBUG_PROVIDER) {
5471 Slog.v(TAG, "incProviderRef: unstable "
5472 + "snatched provider from the jaws of death");
5473 }
5474 prc.removePending = false;
5475 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5476 } else {
5477 // First unstable ref, increment our count in the
5478 // activity manager.
5479 try {
5480 if (DEBUG_PROVIDER) {
5481 Slog.v(TAG, "incProviderRef: Now unstable - "
5482 + prc.holder.info.name);
5483 }
5484 ActivityManagerNative.getDefault().refContentProvider(
5485 prc.holder.connection, 0, 1);
5486 } catch (RemoteException e) {
5487 //do nothing content provider object is dead any way
5488 }
5489 }
5490 }
5491 }
5492 }
5493
Jeff Sharkey6d515712012-09-20 16:06:08 -07005494 public final IContentProvider acquireExistingProvider(
5495 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005496 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005497 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005498 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005499 if (pr == null) {
5500 return null;
5501 }
5502
5503 IContentProvider provider = pr.mProvider;
5504 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005505 if (!jBinder.isBinderAlive()) {
5506 // The hosting process of the provider has died; we can't
5507 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005508 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005509 + ": existing object's process dead");
5510 handleUnstableProviderDiedLocked(jBinder, true);
5511 return null;
5512 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005513
5514 // Only increment the ref count if we have one. If we don't then the
5515 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005516 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005517 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005518 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005519 }
5520 return provider;
5521 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005522 }
5523
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005524 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5525 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005526 return false;
5527 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005529 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005530 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005531 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005532 if (prc == null) {
5533 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005535 }
5536
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005537 boolean lastRef = false;
5538 if (stable) {
5539 if (prc.stableCount == 0) {
5540 if (DEBUG_PROVIDER) Slog.v(TAG,
5541 "releaseProvider: stable ref count already 0, how?");
5542 return false;
5543 }
5544 prc.stableCount -= 1;
5545 if (prc.stableCount == 0) {
5546 // What we do at this point depends on whether there are
5547 // any unstable refs left: if there are, we just tell the
5548 // activity manager to decrement its stable count; if there
5549 // aren't, we need to enqueue this provider to be removed,
5550 // and convert to holding a single unstable ref while
5551 // doing so.
5552 lastRef = prc.unstableCount == 0;
5553 try {
5554 if (DEBUG_PROVIDER) {
5555 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5556 + lastRef + " - " + prc.holder.info.name);
5557 }
5558 ActivityManagerNative.getDefault().refContentProvider(
5559 prc.holder.connection, -1, lastRef ? 1 : 0);
5560 } catch (RemoteException e) {
5561 //do nothing content provider object is dead any way
5562 }
5563 }
5564 } else {
5565 if (prc.unstableCount == 0) {
5566 if (DEBUG_PROVIDER) Slog.v(TAG,
5567 "releaseProvider: unstable ref count already 0, how?");
5568 return false;
5569 }
5570 prc.unstableCount -= 1;
5571 if (prc.unstableCount == 0) {
5572 // If this is the last reference, we need to enqueue
5573 // this provider to be removed instead of telling the
5574 // activity manager to remove it at this point.
5575 lastRef = prc.stableCount == 0;
5576 if (!lastRef) {
5577 try {
5578 if (DEBUG_PROVIDER) {
5579 Slog.v(TAG, "releaseProvider: No longer unstable - "
5580 + prc.holder.info.name);
5581 }
5582 ActivityManagerNative.getDefault().refContentProvider(
5583 prc.holder.connection, 0, -1);
5584 } catch (RemoteException e) {
5585 //do nothing content provider object is dead any way
5586 }
5587 }
5588 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005589 }
5590
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005591 if (lastRef) {
5592 if (!prc.removePending) {
5593 // Schedule the actual remove asynchronously, since we don't know the context
5594 // this will be called in.
5595 // TODO: it would be nice to post a delayed message, so
5596 // if we come back and need the same provider quickly
5597 // we will still have it available.
5598 if (DEBUG_PROVIDER) {
5599 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5600 + prc.holder.info.name);
5601 }
5602 prc.removePending = true;
5603 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5604 mH.sendMessage(msg);
5605 } else {
5606 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5607 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005608 }
5609 return true;
5610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 }
5612
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005613 final void completeRemoveProvider(ProviderRefCount prc) {
5614 synchronized (mProviderMap) {
5615 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005616 // There was a race! Some other client managed to acquire
5617 // the provider before the removal was completed.
5618 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005619 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005620 + "provider still in use");
5621 return;
5622 }
5623
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005624 // More complicated race!! Some client managed to acquire the
5625 // provider and release it before the removal was completed.
5626 // Continue the removal, and abort the next remove message.
5627 prc.removePending = false;
5628
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005629 final IBinder jBinder = prc.holder.provider.asBinder();
5630 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5631 if (existingPrc == prc) {
5632 mProviderRefCountMap.remove(jBinder);
5633 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005634
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005635 for (int i=mProviderMap.size()-1; i>=0; i--) {
5636 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005637 IBinder myBinder = pr.mProvider.asBinder();
5638 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005639 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005640 }
5641 }
5642 }
5643
5644 try {
5645 if (DEBUG_PROVIDER) {
5646 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5647 + "removeContentProvider(" + prc.holder.info.name + ")");
5648 }
5649 ActivityManagerNative.getDefault().removeContentProvider(
5650 prc.holder.connection, false);
5651 } catch (RemoteException e) {
5652 //do nothing content provider object is dead any way
5653 }
5654 }
5655
5656 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005657 synchronized (mProviderMap) {
5658 handleUnstableProviderDiedLocked(provider, fromClient);
5659 }
5660 }
5661
5662 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5663 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5664 if (prc != null) {
5665 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5666 + provider + " " + prc.holder.info.name);
5667 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005668 for (int i=mProviderMap.size()-1; i>=0; i--) {
5669 ProviderClientRecord pr = mProviderMap.valueAt(i);
5670 if (pr != null && pr.mProvider.asBinder() == provider) {
5671 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5672 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005673 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005674 }
You Kimbc74de62013-10-01 00:13:26 +09005675
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005676 if (fromClient) {
5677 // We found out about this due to execution in our client
5678 // code. Tell the activity manager about it now, to ensure
5679 // that the next time we go to do anything with the provider
5680 // it knows it is dead (so we don't race with its death
5681 // notification).
5682 try {
5683 ActivityManagerNative.getDefault().unstableProviderDied(
5684 prc.holder.connection);
5685 } catch (RemoteException e) {
5686 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005687 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005688 }
5689 }
5690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005691
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005692 final void appNotRespondingViaProvider(IBinder provider) {
5693 synchronized (mProviderMap) {
5694 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5695 if (prc != null) {
5696 try {
5697 ActivityManagerNative.getDefault()
5698 .appNotRespondingViaProvider(prc.holder.connection);
5699 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005700 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005701 }
5702 }
5703 }
5704 }
5705
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005706 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005707 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005708 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005709 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5710
5711 final ProviderClientRecord pcr = new ProviderClientRecord(
5712 auths, provider, localProvider, holder);
5713 for (String auth : auths) {
5714 final ProviderKey key = new ProviderKey(auth, userId);
5715 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005716 if (existing != null) {
5717 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005718 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005719 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005720 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005721 }
5722 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005723 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005724 }
5725
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005726 /**
5727 * Installs the provider.
5728 *
5729 * Providers that are local to the process or that come from the system server
5730 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5731 * Other remote providers are reference counted. The initial reference count
5732 * for all reference counted providers is one. Providers that are not reference
5733 * counted do not have a reference count (at all).
5734 *
5735 * This method detects when a provider has already been installed. When this happens,
5736 * it increments the reference count of the existing provider (if appropriate)
5737 * and returns the existing provider. This can happen due to concurrent
5738 * attempts to acquire the same provider.
5739 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005740 private IActivityManager.ContentProviderHolder installProvider(Context context,
5741 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5742 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005743 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005744 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005745 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005746 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005747 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005748 + info.name);
5749 }
5750 Context c = null;
5751 ApplicationInfo ai = info.applicationInfo;
5752 if (context.getPackageName().equals(ai.packageName)) {
5753 c = context;
5754 } else if (mInitialApplication != null &&
5755 mInitialApplication.getPackageName().equals(ai.packageName)) {
5756 c = mInitialApplication;
5757 } else {
5758 try {
5759 c = context.createPackageContext(ai.packageName,
5760 Context.CONTEXT_INCLUDE_CODE);
5761 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005762 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005763 }
5764 }
5765 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005766 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005767 ai.packageName +
5768 " while loading content provider " +
5769 info.name);
5770 return null;
5771 }
5772 try {
5773 final java.lang.ClassLoader cl = c.getClassLoader();
5774 localProvider = (ContentProvider)cl.
5775 loadClass(info.name).newInstance();
5776 provider = localProvider.getIContentProvider();
5777 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005778 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005779 info.name + " from sourceDir " +
5780 info.applicationInfo.sourceDir);
5781 return null;
5782 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005783 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005784 TAG, "Instantiating local provider " + info.name);
5785 // XXX Need to create the correct context for this provider.
5786 localProvider.attachInfo(c, info);
5787 } catch (java.lang.Exception e) {
5788 if (!mInstrumentation.onException(null, e)) {
5789 throw new RuntimeException(
5790 "Unable to get provider " + info.name
5791 + ": " + e.toString(), e);
5792 }
5793 return null;
5794 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005795 } else {
5796 provider = holder.provider;
5797 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005798 + info.name);
5799 }
5800
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005801 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005802
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005803 synchronized (mProviderMap) {
5804 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5805 + " / " + info.name);
5806 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005807 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005808 ComponentName cname = new ComponentName(info.packageName, info.name);
5809 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005810 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005811 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005812 Slog.v(TAG, "installProvider: lost the race, "
5813 + "using existing local provider");
5814 }
5815 provider = pr.mProvider;
5816 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005817 holder = new IActivityManager.ContentProviderHolder(info);
5818 holder.provider = provider;
5819 holder.noReleaseNeeded = true;
5820 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005821 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005822 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005823 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005824 retHolder = pr.mHolder;
5825 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005826 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5827 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005828 if (DEBUG_PROVIDER) {
5829 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005830 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005831 // We need to transfer our new reference to the existing
5832 // ref count, releasing the old one... but only if
5833 // release is needed (that is, it is not running in the
5834 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005835 if (!noReleaseNeeded) {
5836 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005837 try {
5838 ActivityManagerNative.getDefault().removeContentProvider(
5839 holder.connection, stable);
5840 } catch (RemoteException e) {
5841 //do nothing content provider object is dead any way
5842 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005843 }
5844 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005845 ProviderClientRecord client = installProviderAuthoritiesLocked(
5846 provider, localProvider, holder);
5847 if (noReleaseNeeded) {
5848 prc = new ProviderRefCount(holder, client, 1000, 1000);
5849 } else {
5850 prc = stable
5851 ? new ProviderRefCount(holder, client, 1, 0)
5852 : new ProviderRefCount(holder, client, 0, 1);
5853 }
5854 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005855 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005856 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 }
5858 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005859
5860 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005861 }
5862
Romain Guy65b345f2011-07-27 18:51:50 -07005863 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005864 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005865 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005866 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005867 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005868 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005869 public void run() {
5870 ensureJitEnabled();
5871 }
5872 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005873 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5874 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005876 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005877 try {
5878 mgr.attachApplication(mAppThread);
5879 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005880 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005882 // Watch for getting close to heap limit.
5883 BinderInternal.addGcWatcher(new Runnable() {
5884 @Override public void run() {
5885 if (!mSomeActivitiesChanged) {
5886 return;
5887 }
5888 Runtime runtime = Runtime.getRuntime();
5889 long dalvikMax = runtime.maxMemory();
5890 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5891 if (dalvikUsed > ((3*dalvikMax)/4)) {
5892 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5893 + " total=" + (runtime.totalMemory()/1024)
5894 + " used=" + (dalvikUsed/1024));
5895 mSomeActivitiesChanged = false;
5896 try {
5897 mgr.releaseSomeActivities(mAppThread);
5898 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005899 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005900 }
5901 }
5902 }
5903 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005904 } else {
5905 // Don't set application object here -- if the system crashes,
5906 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005907 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005908 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005909 try {
5910 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005911 ContextImpl context = ContextImpl.createAppContext(
5912 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005913 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5914 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005915 } catch (Exception e) {
5916 throw new RuntimeException(
5917 "Unable to instantiate Application():" + e.toString(), e);
5918 }
5919 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005920
5921 // add dropbox logging to libcore
5922 DropBox.setReporter(new DropBoxReporter());
5923
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005924 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005925 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005926 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005927 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005928 // We need to apply this change to the resources
5929 // immediately, because upon returning the view
5930 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005931 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005932 // This actually changed the resources! Tell
5933 // everyone about it.
5934 if (mPendingConfiguration == null ||
5935 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5936 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07005937
Jeff Brown9ef09972013-10-15 20:49:59 -07005938 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005939 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005940 }
5941 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005942 }
Craig Mautner88c05892013-06-28 09:47:45 -07005943 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005944 public void onLowMemory() {
5945 }
Craig Mautner88c05892013-06-28 09:47:45 -07005946 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005947 public void onTrimMemory(int level) {
5948 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005949 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
5951
Romain Guy5e9120d2012-01-30 12:17:22 -08005952 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005953 // The system process on low-memory devices do not get to use hardware
5954 // accelerated drawing, since this can add too much overhead to the
5955 // process.
5956 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005957 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005958 } else {
John Reck51aaf902015-12-02 15:08:07 -08005959 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 ActivityThread thread = new ActivityThread();
5962 thread.attach(true);
5963 return thread;
5964 }
5965
Jeff Brown10e89712011-07-08 18:52:57 -07005966 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005967 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005968 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005969 }
5970 }
5971
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005972 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005973 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005974 if (mCoreSettings != null) {
5975 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005976 }
Craig Mautner88c05892013-06-28 09:47:45 -07005977 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005978 }
5979 }
5980
Geremy Condra69689a72012-09-11 16:57:17 -07005981 private static class EventLoggingReporter implements EventLogger.Reporter {
5982 @Override
5983 public void report (int code, Object... list) {
5984 EventLog.writeEvent(code, list);
5985 }
5986 }
5987
Geremy Condrab7faaf42012-09-19 18:07:42 -07005988 private class DropBoxReporter implements DropBox.Reporter {
5989
5990 private DropBoxManager dropBox;
5991
Narayan Kamath7f062242015-04-08 13:24:13 +01005992 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07005993
5994 @Override
5995 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01005996 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07005997 dropBox.addData(tag, data, flags);
5998 }
5999
6000 @Override
6001 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006002 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006003 dropBox.addText(tag, data);
6004 }
Narayan Kamath7f062242015-04-08 13:24:13 +01006005
6006 private synchronized void ensureInitialized() {
6007 if (dropBox == null) {
6008 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
6009 }
6010 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006011 }
6012
Romain Guy65b345f2011-07-27 18:51:50 -07006013 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006014 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07006015 SamplingProfilerIntegration.start();
6016
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08006017 // CloseGuard defaults to true and can be quite spammy. We
6018 // disable it here, but selectively enable it later (via
6019 // StrictMode) on debug builds, but using DropBox, not logs.
6020 CloseGuard.setEnabled(false);
6021
Jeff Sharkeyb049e212012-09-07 23:16:01 -07006022 Environment.initForCurrentUser();
6023
Geremy Condra69689a72012-09-11 16:57:17 -07006024 // Set the reporter for event logging in libcore
6025 EventLogger.setReporter(new EventLoggingReporter());
6026
Alex Klyubin48125632015-04-29 13:16:30 -07006027 AndroidKeyStoreProvider.install();
Kenny Root8b514752013-02-04 09:35:16 -08006028
Robin Lee3d076af2014-04-25 14:57:49 +01006029 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6030 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6031 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 Process.setArgV0("<pre-initialized>");
6034
6035 Looper.prepareMainLooper();
6036
6037 ActivityThread thread = new ActivityThread();
6038 thread.attach(false);
6039
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006040 if (sMainThreadHandler == null) {
6041 sMainThreadHandler = thread.getHandler();
6042 }
6043
Dianne Hackborn287952c2010-09-22 22:34:31 -07006044 if (false) {
6045 Looper.myLooper().setMessageLogging(new
6046 LogPrinter(Log.DEBUG, "ActivityThread"));
6047 }
6048
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006049 // End of event ActivityThreadMain.
6050 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006051 Looper.loop();
6052
Jeff Brown10e89712011-07-08 18:52:57 -07006053 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006054 }
6055}