blob: 5cfcac33050e07c7b8dbbf750c3bf92db99c1166 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Dianne Hackborn69c6adc2015-06-02 10:52:59 -070021import android.app.assist.AssistContent;
22import android.app.assist.AssistStructure;
Christopher Tate45281862010-03-05 15:46:30 -080023import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070025import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.ComponentName;
27import android.content.ContentProvider;
28import android.content.Context;
29import android.content.IContentProvider;
30import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070031import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.content.pm.ActivityInfo;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.IPackageManager;
35import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070036import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070038import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.pm.ProviderInfo;
40import android.content.pm.ServiceInfo;
41import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070042import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.content.res.Configuration;
44import android.content.res.Resources;
Alan Viverettee54d2452015-05-06 10:41:43 -070045import android.content.res.Resources.Theme;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.database.sqlite.SQLiteDatabase;
47import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080048import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.graphics.Bitmap;
50import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070051import android.hardware.display.DisplayManagerGlobal;
Paul Jensene0bef712014-12-10 15:12:18 -050052import android.net.ConnectivityManager;
Robert Greenwalt434203a2010-10-11 16:00:27 -070053import android.net.IConnectivityManager;
Paul Jensene0bef712014-12-10 15:12:18 -050054import android.net.Network;
Robert Greenwalt434203a2010-10-11 16:00:27 -070055import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040056import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040057import android.net.Uri;
Joe Onoratod630f102011-03-17 18:42:26 -070058import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070059import android.os.Binder;
Jeff Sharkey344744b2016-01-28 19:03:30 -070060import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Bundle;
62import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070063import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070064import android.os.Environment;
Jesse Hall79bf3922016-12-12 12:53:02 -080065import android.os.GraphicsEnvironment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.os.Handler;
67import android.os.IBinder;
Adam Lesinskib61e4052016-05-19 18:23:05 -070068import android.os.LocaleList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.Looper;
70import android.os.Message;
71import android.os.MessageQueue;
Dianne Hackbornfabb70b2014-11-11 12:22:36 -080072import android.os.Parcel;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070073import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070074import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.os.Process;
76import android.os.RemoteException;
77import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070078import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070080import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070081import android.os.Trace;
Jeff Sharkey369f5092016-02-29 11:16:21 -070082import android.os.TransactionTooLargeException;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070083import android.os.UserHandle;
Narayan Kamathccb2a0862013-12-19 14:49:36 +000084import android.provider.Settings;
Alex Klyubinf9034cc2015-02-12 11:43:09 -080085import android.security.NetworkSecurityPolicy;
Chad Brubaker78d47122015-11-17 22:26:58 -080086import android.security.net.config.NetworkSecurityConfigProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070088import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089import android.util.DisplayMetrics;
90import android.util.EventLog;
91import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070092import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070093import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080094import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080095import android.util.Slog;
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -070096import android.util.SparseIntArray;
Adam Powell14874662013-07-18 19:42:41 -070097import android.util.SuperNotCalledException;
Adam Lesinski3ad1b482016-04-01 16:41:41 -070098import android.view.ContextThemeWrapper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099import android.view.Display;
John Reck51aaf902015-12-02 15:08:07 -0800100import android.view.ThreadedRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import android.view.View;
102import android.view.ViewDebug;
103import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -0700104import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import android.view.Window;
106import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -0700107import android.view.WindowManagerGlobal;
Tim Murraye1e6c662015-04-07 13:24:14 -0700108import android.renderscript.RenderScriptCacheDir;
Calin Juravle69052392015-12-17 17:06:49 +0200109import android.system.Os;
110import android.system.OsConstants;
111import android.system.ErrnoException;
Richard Uhler2c036192016-09-14 09:48:31 +0100112import android.webkit.WebView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700114import com.android.internal.annotations.GuardedBy;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700115import com.android.internal.app.IVoiceInteractor;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800116import com.android.internal.content.ReferrerIntent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117import com.android.internal.os.BinderInternal;
118import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -0700119import com.android.internal.os.SamplingProfilerIntegration;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700120import com.android.internal.os.SomeArgs;
Jeff Sharkey3e195892016-03-05 19:48:59 -0700121import com.android.internal.util.ArrayUtils;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700122import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700123import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100124import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700125import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127import java.io.File;
128import java.io.FileDescriptor;
129import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700130import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131import java.io.PrintWriter;
132import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700133import java.net.InetAddress;
Narayan Kamathccb2a0862013-12-19 14:49:36 +0000134import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135import java.util.ArrayList;
Calin Juravlef19c9632015-12-10 17:32:15 +0000136import java.util.Collections;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137import java.util.List;
Adam Lesinskib61e4052016-05-19 18:23:05 -0700138import java.util.Locale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800140import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141import java.util.TimeZone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
Geremy Condrab7faaf42012-09-19 18:07:42 -0700143import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700144import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800145import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400146import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800147import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700148import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700149import dalvik.system.VMRuntime;
Man Caocfa78b22015-06-11 20:14:34 -0700150import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
Bob Leee5408332009-09-04 18:31:17 -0700151
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700152final class RemoteServiceException extends AndroidRuntimeException {
153 public RemoteServiceException(String msg) {
154 super(msg);
155 }
156}
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158/**
159 * This manages the execution of the main thread in an
160 * application process, scheduling and executing activities,
161 * broadcasts, and other operations on it as the activity
162 * manager requests.
163 *
164 * {@hide}
165 */
166public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700167 /** @hide */
168 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700169 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700170 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700171 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700172 /** @hide */
173 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700174 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700175 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700176 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800177 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700178 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700179 private static final boolean DEBUG_PROVIDER = false;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700180 private static final boolean DEBUG_ORDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
Craig Mautnere3119b72015-01-20 15:02:36 -0800183 private static final int LOG_AM_ON_PAUSE_CALLED = 30021;
184 private static final int LOG_AM_ON_RESUME_CALLED = 30022;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -0800185 private static final int LOG_AM_ON_STOP_CALLED = 30049;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Dianne Hackborn455625e2015-01-21 09:55:13 -0800187 /** Type for IActivityManager.serviceDoneExecuting: anonymous operation */
188 public static final int SERVICE_DONE_EXECUTING_ANON = 0;
189 /** Type for IActivityManager.serviceDoneExecuting: done with an onStart call */
190 public static final int SERVICE_DONE_EXECUTING_START = 1;
191 /** Type for IActivityManager.serviceDoneExecuting: done stopping (destroying) service */
192 public static final int SERVICE_DONE_EXECUTING_STOP = 2;
193
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700194 // Details for pausing activity.
195 private static final int USER_LEAVING = 1;
196 private static final int DONT_REPORT = 2;
197
Filip Gruszczynskica664812015-12-04 12:43:36 -0800198 // Whether to invoke an activity callback after delivering new configuration.
199 private static final boolean REPORT_TO_ACTIVITY = true;
200
Jeff Browndefd4a62014-03-10 21:24:37 -0700201 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700202
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700203 static volatile IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 final ApplicationThread mAppThread = new ApplicationThread();
206 final Looper mLooper = Looper.myLooper();
207 final H mH = new H();
Dianne Hackborn782d4982015-07-08 17:36:37 -0700208 final ArrayMap<IBinder, ActivityClientRecord> mActivities = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700209 // List of new activities (via ActivityRecord.nextIdle) that should
210 // be reported when next we idle.
211 ActivityClientRecord mNewActivities = null;
212 // Number of activities that are currently visible on-screen.
213 int mNumVisibleActivities = 0;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700214 ArrayList<WeakReference<AssistStructure>> mLastAssistStructures = new ArrayList<>();
215 private int mLastSessionId;
Dianne Hackborn782d4982015-07-08 17:36:37 -0700216 final ArrayMap<IBinder, Service> mServices = new ArrayMap<>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700217 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700218 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700219 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700220 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700221 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700222 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700223 Application mInitialApplication;
224 final ArrayList<Application> mAllApplications
225 = new ArrayList<Application>();
226 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700227 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800228 /** Reference to singleton {@link ActivityThread} */
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700229 private static volatile ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700230 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700231 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700232 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700233 String[] mInstrumentationSplitAppDirs = null;
234 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700235 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700236 String[] mInstrumentedSplitAppDirs = null;
237 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700238 boolean mSystemThread = false;
239 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700240 boolean mSomeActivitiesChanged = false;
Chong Zhang4951f9d2016-06-23 13:15:08 -0700241 boolean mUpdatingSystemConfig = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700243 // These can be accessed by multiple threads; mPackages is the lock.
244 // XXX For now we keep around information about all packages we have
245 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800246 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700247 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800248 // which means this lock gets held while the activity and window managers
249 // holds their own lock. Thus you MUST NEVER call back into the activity manager
250 // or window manager or anything that depends on them while holding this lock.
Jeff Sharkeyb9f36742015-04-08 21:02:14 -0700251 // These LoadedApk are only valid for the userId that we're running as.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700252 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
253 = new ArrayMap<String, WeakReference<LoadedApk>>();
254 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
255 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700256 final ArrayList<ActivityClientRecord> mRelaunchingActivities
257 = new ArrayList<ActivityClientRecord>();
258 Configuration mPendingConfiguration = null;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700259 // Because we merge activity relaunch operations we can't depend on the ordering provided by
260 // the handler messages. We need to introduce secondary ordering mechanism, which will allow
261 // us to drop certain events, if we know that they happened before relaunch we already executed.
262 // This represents the order of receiving the request from AM.
263 @GuardedBy("mResourcesManager")
264 int mLifecycleSeq = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265
Craig Mautner88c05892013-06-28 09:47:45 -0700266 private final ResourcesManager mResourcesManager;
267
Jeff Sharkey6d515712012-09-20 16:06:08 -0700268 private static final class ProviderKey {
269 final String authority;
270 final int userId;
271
272 public ProviderKey(String authority, int userId) {
273 this.authority = authority;
274 this.userId = userId;
275 }
276
277 @Override
278 public boolean equals(Object o) {
279 if (o instanceof ProviderKey) {
280 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800281 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700282 }
283 return false;
284 }
285
286 @Override
287 public int hashCode() {
288 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
289 }
290 }
291
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700292 // The lock of mProviderMap protects the following variables.
Wale Ogunwale1d646122015-04-24 14:45:14 -0700293 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
294 = new ArrayMap<ProviderKey, ProviderClientRecord>();
295 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
296 = new ArrayMap<IBinder, ProviderRefCount>();
297 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
298 = new ArrayMap<IBinder, ProviderClientRecord>();
299 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
300 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700302 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
Wale Ogunwale1d646122015-04-24 14:45:14 -0700303 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600304
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700305 final GcIdler mGcIdler = new GcIdler();
306 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
Jeff Sharkeyd5896632016-03-04 16:16:00 -0700308 static volatile Handler sMainThreadHandler; // set once in main()
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700309
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800310 Bundle mCoreSettings = null;
311
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400312 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700314 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 Intent intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800316 String referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700317 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700319 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 Activity activity;
321 Window window;
322 Activity parent;
323 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700324 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 boolean paused;
326 boolean stopped;
327 boolean hideForNow;
328 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700329 Configuration createdConfig;
Wale Ogunwale60454db2015-01-23 16:05:07 -0800330 Configuration overrideConfig;
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800331 // Used for consolidating configs before sending on to Activity.
332 private Configuration tmpConfig = new Configuration();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700333 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
Jeff Hao1b012d32014-08-20 10:35:34 -0700335 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400338 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700339 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340
341 List<ResultInfo> pendingResults;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800342 List<ReferrerIntent> pendingIntents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343
344 boolean startsNotResumed;
345 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800346 int pendingConfigChanges;
347 boolean onlyLocalRequest;
348
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700349 Window mPendingRemoveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800350 WindowManager mPendingRemoveWindowManager;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700351 boolean mPreserveWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700353 // Set for relaunch requests, indicates the order number of the relaunch operation, so it
354 // can be compared with other lifecycle operations.
355 int relaunchSeq = 0;
356
357 // Can only be accessed from the UI thread. This represents the latest processed message
358 // that is related to lifecycle events/
359 int lastProcessedSeq = 0;
360
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700361 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 parent = null;
363 embeddedID = null;
364 paused = false;
365 stopped = false;
366 hideForNow = false;
367 nextIdle = null;
368 }
369
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800370 public boolean isPreHoneycomb() {
371 if (activity != null) {
372 return activity.getApplicationInfo().targetSdkVersion
373 < android.os.Build.VERSION_CODES.HONEYCOMB;
374 }
375 return false;
376 }
377
Craig Mautnera0026042014-04-23 11:45:37 -0700378 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700379 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700380 }
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700383 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 return "ActivityRecord{"
385 + Integer.toHexString(System.identityHashCode(this))
386 + " token=" + token + " " + (componentName == null
387 ? "no component name" : componentName.toShortString())
388 + "}";
389 }
Andrii Kulian58178f22016-03-16 13:44:56 -0700390
391 public String getStateString() {
392 StringBuilder sb = new StringBuilder();
393 sb.append("ActivityClientRecord{");
394 sb.append("paused=").append(paused);
395 sb.append(", stopped=").append(stopped);
396 sb.append(", hideForNow=").append(hideForNow);
397 sb.append(", startsNotResumed=").append(startsNotResumed);
398 sb.append(", isForward=").append(isForward);
399 sb.append(", pendingConfigChanges=").append(pendingConfigChanges);
400 sb.append(", onlyLocalRequest=").append(onlyLocalRequest);
401 sb.append(", preserveWindow=").append(mPreserveWindow);
402 if (activity != null) {
403 sb.append(", Activity{");
404 sb.append("resumed=").append(activity.mResumed);
405 sb.append(", stopped=").append(activity.mStopped);
406 sb.append(", finished=").append(activity.isFinishing());
407 sb.append(", destroyed=").append(activity.isDestroyed());
408 sb.append(", startedActivity=").append(activity.mStartedActivity);
409 sb.append(", temporaryPause=").append(activity.mTemporaryPause);
410 sb.append(", changingConfigurations=").append(activity.mChangingConfigurations);
411 sb.append(", visibleBehind=").append(activity.mVisibleBehind);
412 sb.append("}");
413 }
414 sb.append("}");
415 return sb.toString();
416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
418
Wale Ogunwale1d646122015-04-24 14:45:14 -0700419 final class ProviderClientRecord {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700420 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 final IContentProvider mProvider;
422 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700423 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700425 ProviderClientRecord(String[] names, IContentProvider provider,
426 ContentProvider localProvider,
427 IActivityManager.ContentProviderHolder holder) {
428 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 mProvider = provider;
430 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700431 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 }
433 }
434
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435 static final class NewIntentData {
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800436 List<ReferrerIntent> intents;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 IBinder token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700438 boolean andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 public String toString() {
Wale Ogunwale826c7062016-09-13 08:25:54 -0700440 return "NewIntentData{intents=" + intents + " token=" + token
441 + " andPause=" + andPause +"}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443 }
444
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400445 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700446 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700447 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
448 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800449 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700450 this.intent = intent;
451 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 Intent intent;
454 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400455 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public String toString() {
457 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700458 info.packageName + " resultCode=" + getResultCode()
459 + " resultData=" + getResultData() + " resultExtras="
460 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462 }
463
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400464 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700465 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400466 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700467 int backupMode;
468 public String toString() {
469 return "CreateBackupAgentData{appInfo=" + appInfo
470 + " backupAgent=" + appInfo.backupAgentName
471 + " mode=" + backupMode + "}";
472 }
473 }
Bob Leee5408332009-09-04 18:31:17 -0700474
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400475 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 IBinder token;
477 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400478 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 Intent intent;
480 public String toString() {
481 return "CreateServiceData{token=" + token + " className="
482 + info.name + " packageName=" + info.packageName
483 + " intent=" + intent + "}";
484 }
485 }
486
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400487 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 IBinder token;
489 Intent intent;
490 boolean rebind;
491 public String toString() {
492 return "BindServiceData{token=" + token + " intent=" + intent + "}";
493 }
494 }
495
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400496 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700498 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700500 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 Intent args;
502 public String toString() {
503 return "ServiceArgsData{token=" + token + " startId=" + startId
504 + " args=" + args + "}";
505 }
506 }
507
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400508 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700509 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 String processName;
511 ApplicationInfo appInfo;
512 List<ProviderInfo> providers;
513 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 Bundle instrumentationArgs;
515 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800516 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 int debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400518 boolean enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700519 boolean trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700520 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700521 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400523 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700524
525 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700526 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 public String toString() {
529 return "AppBindData{appInfo=" + appInfo + "}";
530 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700531 }
532
533 static final class Profiler {
534 String profileFile;
535 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700536 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700537 boolean autoStopProfiler;
538 boolean profiling;
539 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700540 public void setProfiler(ProfilerInfo profilerInfo) {
541 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700542 if (profiling) {
543 if (fd != null) {
544 try {
545 fd.close();
546 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700547 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700548 }
549 }
550 return;
551 }
552 if (profileFd != null) {
553 try {
554 profileFd.close();
555 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700556 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700557 }
558 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700559 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700560 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700561 samplingInterval = profilerInfo.samplingInterval;
562 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700563 }
564 public void startProfiling() {
565 if (profileFd == null || profiling) {
566 return;
567 }
568 try {
Andreas Gampeeff06392016-05-10 12:51:45 -0700569 int bufferSize = SystemProperties.getInt("debug.traceview-buffer-size-mb", 8);
Jeff Hao1b012d32014-08-20 10:35:34 -0700570 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
Andreas Gampe0f03a012016-05-09 14:26:07 -0700571 bufferSize * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700572 profiling = true;
573 } catch (RuntimeException e) {
574 Slog.w(TAG, "Profiling failed on path " + profileFile);
575 try {
576 profileFd.close();
577 profileFd = null;
578 } catch (IOException e2) {
579 Slog.w(TAG, "Failure closing profile fd", e2);
580 }
581 }
582 }
583 public void stopProfiling() {
584 if (profiling) {
585 profiling = false;
586 Debug.stopMethodTracing();
587 if (profileFd != null) {
588 try {
589 profileFd.close();
590 } catch (IOException e) {
591 }
592 }
593 profileFd = null;
594 profileFile = null;
595 }
596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 }
598
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400599 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700600 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700601 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800602 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400606 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 IBinder token;
608 List<ResultInfo> results;
609 public String toString() {
610 return "ResultData{token=" + token + " results" + results + "}";
611 }
612 }
613
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400614 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800615 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 String what;
617 String who;
618 }
619
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400620 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700621 String path;
622 ParcelFileDescriptor fd;
623 }
624
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400625 static final class UpdateCompatibilityData {
626 String pkg;
627 CompatibilityInfo info;
628 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800629
Adam Skorydfc7fd72013-08-05 19:23:41 -0700630 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800631 IBinder activityToken;
632 IBinder requestToken;
633 int requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -0700634 int sessionId;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800635 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700636
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800637 static final class ActivityConfigChangeData {
638 final IBinder activityToken;
639 final Configuration overrideConfig;
640 public ActivityConfigChangeData(IBinder token, Configuration config) {
641 activityToken = token;
642 overrideConfig = config;
643 }
644 }
645
Romain Guy65b345f2011-07-27 18:51:50 -0700646 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700647
Romain Guy65b345f2011-07-27 18:51:50 -0700648 private class ApplicationThread extends ApplicationThreadNative {
Vasu Nori3c7131f2010-09-21 14:36:57 -0700649 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700650
Dianne Hackborna413dc02013-07-12 12:02:55 -0700651 private int mLastProcessState = -1;
652
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700653 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700654 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700655 if (mPendingConfiguration == null ||
656 mPendingConfiguration.isOtherSeqNewer(config)) {
657 mPendingConfiguration = config;
658 }
659 }
660 }
661
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700663 boolean userLeaving, int configChanges, boolean dontReport) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700664 int seq = getLifecycleSeq();
665 if (DEBUG_ORDER) Slog.d(TAG, "pauseActivity " + ActivityThread.this
666 + " operation received seq: " + seq);
Jeff Brown9ef09972013-10-15 20:49:59 -0700667 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
669 token,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700670 (userLeaving ? USER_LEAVING : 0) | (dontReport ? DONT_REPORT : 0),
671 configChanges,
672 seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 }
674
675 public final void scheduleStopActivity(IBinder token, boolean showWindow,
676 int configChanges) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700677 int seq = getLifecycleSeq();
678 if (DEBUG_ORDER) Slog.d(TAG, "stopActivity " + ActivityThread.this
679 + " operation received seq: " + seq);
680 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700682 token, 0, configChanges, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 }
684
685 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700686 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
688 token);
689 }
690
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800691 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700692 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800693 }
694
Dianne Hackborna413dc02013-07-12 12:02:55 -0700695 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800696 boolean isForward, Bundle resumeArgs) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700697 int seq = getLifecycleSeq();
698 if (DEBUG_ORDER) Slog.d(TAG, "resumeActivity " + ActivityThread.this
699 + " operation received seq: " + seq);
Dianne Hackborna413dc02013-07-12 12:02:55 -0700700 updateProcessState(processState, false);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -0700701 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0, 0, seq);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 }
703
704 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
705 ResultData res = new ResultData();
706 res.token = token;
707 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700708 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
711 // we use token to identify this activity without having to send the
712 // activity itself back to the activity manager. (matters more with ipc)
Wale Ogunwale60454db2015-01-23 16:05:07 -0800713 @Override
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700714 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800715 ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
716 CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
717 int procState, Bundle state, PersistableBundle persistentState,
718 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
719 boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700720
721 updateProcessState(procState, false);
722
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700723 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724
725 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700726 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 r.intent = intent;
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800728 r.referrer = referrer;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700729 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400731 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700733 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
735 r.pendingResults = pendingResults;
736 r.pendingIntents = pendingNewIntents;
737
738 r.startsNotResumed = notResumed;
739 r.isForward = isForward;
740
Jeff Hao1b012d32014-08-20 10:35:34 -0700741 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700742
Wale Ogunwale60454db2015-01-23 16:05:07 -0800743 r.overrideConfig = overrideConfig;
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700744 updatePendingConfiguration(curConfig);
745
Jeff Brown9ef09972013-10-15 20:49:59 -0700746 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748
Wale Ogunwale60454db2015-01-23 16:05:07 -0800749 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 public final void scheduleRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -0800751 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Wale Ogunwale60454db2015-01-23 16:05:07 -0800752 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700753 Configuration overrideConfig, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800754 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -0700755 configChanges, notResumed, config, overrideConfig, true, preserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 }
757
Wale Ogunwale826c7062016-09-13 08:25:54 -0700758 public final void scheduleNewIntent(
759 List<ReferrerIntent> intents, IBinder token, boolean andPause) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 NewIntentData data = new NewIntentData();
761 data.intents = intents;
762 data.token = token;
Wale Ogunwale826c7062016-09-13 08:25:54 -0700763 data.andPause = andPause;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764
Jeff Brown9ef09972013-10-15 20:49:59 -0700765 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 }
767
768 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
769 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700770 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 configChanges);
772 }
773
774 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400775 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700776 boolean sync, int sendingUser, int processState) {
777 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700778 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700779 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400781 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700782 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400785 public final void scheduleCreateBackupAgent(ApplicationInfo app,
786 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700787 CreateBackupAgentData d = new CreateBackupAgentData();
788 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400789 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700790 d.backupMode = backupMode;
791
Jeff Brown9ef09972013-10-15 20:49:59 -0700792 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700793 }
794
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400795 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
796 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700797 CreateBackupAgentData d = new CreateBackupAgentData();
798 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400799 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700800
Jeff Brown9ef09972013-10-15 20:49:59 -0700801 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700802 }
803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700805 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
806 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 CreateServiceData s = new CreateServiceData();
808 s.token = token;
809 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400810 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811
Jeff Brown9ef09972013-10-15 20:49:59 -0700812 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813 }
814
815 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700816 boolean rebind, int processState) {
817 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 BindServiceData s = new BindServiceData();
819 s.token = token;
820 s.intent = intent;
821 s.rebind = rebind;
822
Amith Yamasani742a6712011-05-04 14:49:28 -0700823 if (DEBUG_SERVICE)
824 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
825 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700826 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 }
828
829 public final void scheduleUnbindService(IBinder token, Intent intent) {
830 BindServiceData s = new BindServiceData();
831 s.token = token;
832 s.intent = intent;
833
Jeff Brown9ef09972013-10-15 20:49:59 -0700834 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 }
836
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700837 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700838 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 ServiceArgsData s = new ServiceArgsData();
840 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700841 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700843 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 s.args = args;
845
Jeff Brown9ef09972013-10-15 20:49:59 -0700846 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 }
848
849 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700850 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852
Jeff Hao1b012d32014-08-20 10:35:34 -0700853 public final void bindApplication(String processName, ApplicationInfo appInfo,
854 List<ProviderInfo> providers, ComponentName instrumentationName,
855 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
856 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800857 IUiAutomationConnection instrumentationUiConnection, int debugMode,
Pablo Ceballosa4d4e822015-10-05 10:27:52 -0700858 boolean enableBinderTracking, boolean trackAllocation,
859 boolean isRestrictedBackupMode, boolean persistent, Configuration config,
860 CompatibilityInfo compatInfo, Map<String, IBinder> services, Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861
862 if (services != null) {
863 // Setup the service cache in the ServiceManager
864 ServiceManager.initServiceCache(services);
865 }
866
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800867 setCoreSettings(coreSettings);
868
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 AppBindData data = new AppBindData();
870 data.processName = processName;
871 data.appInfo = appInfo;
872 data.providers = providers;
873 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 data.instrumentationArgs = instrumentationArgs;
875 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800876 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 data.debugMode = debugMode;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -0400878 data.enableBinderTracking = enableBinderTracking;
Man Caocfa78b22015-06-11 20:14:34 -0700879 data.trackAllocation = trackAllocation;
Christopher Tate181fafa2009-05-14 11:12:14 -0700880 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700881 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400883 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700884 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700885 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887
888 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700889 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 }
891
Christopher Tate5e1ab332009-09-01 20:32:49 -0700892 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700893 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700894 }
895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700897 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700898 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 }
900
901 public void updateTimeZone() {
902 TimeZone.setDefault(null);
903 }
904
Robert Greenwalt03595d02010-11-02 14:08:23 -0700905 public void clearDnsCache() {
906 // a non-standard API to get this to libcore
907 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400908 // Allow libcore to perform the necessary actions as it sees fit upon a network
909 // configuration change.
910 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700911 }
912
Jason Monk83520b92014-05-09 15:16:06 -0400913 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400914 final ConnectivityManager cm = ConnectivityManager.from(getSystemContext());
915 final Network network = cm.getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -0500916 if (network != null) {
Paul Jensen72db88e2015-03-10 10:54:12 -0400917 Proxy.setHttpProxySystemProperty(cm.getDefaultProxy());
Paul Jensene0bef712014-12-10 15:12:18 -0500918 } else {
919 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
920 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700921 }
922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 public void processInBackground() {
924 mH.removeMessages(H.GC_WHEN_IDLE);
925 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
926 }
927
928 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700929 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700930 try {
931 data.fd = ParcelFileDescriptor.dup(fd);
932 data.token = servicetoken;
933 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700934 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700935 } catch (IOException e) {
936 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 }
938 }
939
940 // This function exists to make sure all receiver dispatching is
941 // correctly ordered, since these are one-way calls and the binder driver
942 // applies transaction ordering per object for such calls.
943 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700944 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700945 boolean sticky, int sendingUser, int processState) throws RemoteException {
946 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700947 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
948 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 }
Bob Leee5408332009-09-04 18:31:17 -0700950
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800951 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700953 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955
Wale Ogunwale60454db2015-01-23 16:05:07 -0800956 @Override
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800957 public void scheduleActivityConfigurationChanged(
Filip Gruszczynskica664812015-12-04 12:43:36 -0800958 IBinder token, Configuration overrideConfig, boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800959 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED,
Filip Gruszczynskica664812015-12-04 12:43:36 -0800960 new ActivityConfigChangeData(token, overrideConfig), reportToActivity ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 }
962
Wale Ogunwalec2607b42015-02-07 16:16:59 -0800963 @Override
Jeff Hao1b012d32014-08-20 10:35:34 -0700964 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
965 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800966 }
967
Andy McFadden824c5102010-07-09 16:26:57 -0700968 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
969 DumpHeapData dhd = new DumpHeapData();
970 dhd.path = path;
971 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700972 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700973 }
974
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700975 public void setSchedulingGroup(int group) {
976 // Note: do this immediately, since going into the foreground
977 // should happen regardless of what pending work we have to do
978 // and the activity manager will wait for us to report back that
979 // we are done before sending us to the background.
980 try {
981 Process.setProcessGroup(Process.myPid(), group);
982 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800983 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700984 }
985 }
Bob Leee5408332009-09-04 18:31:17 -0700986
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700987 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700988 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700989 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700990
991 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700992 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700993 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700994
Dianne Hackborn30d71892010-12-11 10:37:55 -0800995 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
996 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700997 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700998 try {
999 data.fd = ParcelFileDescriptor.dup(fd);
1000 data.token = activitytoken;
1001 data.prefix = prefix;
1002 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001003 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -07001004 } catch (IOException e) {
1005 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -07001006 }
1007 }
Chet Haase9c1e23b2011-03-24 10:51:31 -07001008
Marco Nelissen18cb2872011-11-15 11:19:53 -08001009 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
1010 String[] args) {
1011 DumpComponentInfo data = new DumpComponentInfo();
1012 try {
1013 data.fd = ParcelFileDescriptor.dup(fd);
1014 data.token = providertoken;
1015 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -07001016 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -08001017 } catch (IOException e) {
1018 Slog.w(TAG, "dumpProvider failed", e);
1019 }
1020 }
1021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001023 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001024 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
1025 boolean dumpUnreachable, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001026 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -07001027 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001028 try {
Colin Crossc4fb5f92016-02-02 16:51:15 -08001029 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly, dumpUnreachable);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001030 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -07001031 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -07001032 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001033 }
1034
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001035 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Colin Crossc4fb5f92016-02-02 16:51:15 -08001036 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly, boolean dumpUnreachable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 long nativeMax = Debug.getNativeHeapSize() / 1024;
1038 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
1039 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
1040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 Runtime runtime = Runtime.getRuntime();
Mathieu Chartierd288a262015-07-10 13:44:42 -07001042 runtime.gc(); // Do GC since countInstancesOfClass counts unreachable objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 long dalvikMax = runtime.totalMemory() / 1024;
1044 long dalvikFree = runtime.freeMemory() / 1024;
1045 long dalvikAllocated = dalvikMax - dalvikFree;
Richard Uhler2c036192016-09-14 09:48:31 +01001046
1047 Class[] classesToCount = new Class[] {
1048 ContextImpl.class,
1049 Activity.class,
1050 WebView.class,
1051 OpenSSLSocketImpl.class
1052 };
1053 long[] instanceCounts = VMDebug.countInstancesOfClasses(classesToCount, true);
1054 long appContextInstanceCount = instanceCounts[0];
1055 long activityInstanceCount = instanceCounts[1];
1056 long webviewInstanceCount = instanceCounts[2];
1057 long openSslSocketCount = instanceCounts[3];
1058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -07001060 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 int globalAssetCount = AssetManager.getGlobalAssetCount();
1062 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
1063 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
1064 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
1065 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001066 long parcelSize = Parcel.getGlobalAllocSize();
1067 long parcelCount = Parcel.getGlobalAllocCount();
Vasu Noric3849202010-03-09 10:47:25 -08001068 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -07001069
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07001070 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
1071 Process.myPid(),
Dianne Hackborne77187d2013-10-25 16:32:41 -07001072 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
1073 nativeMax, nativeAllocated, nativeFree,
1074 dalvikMax, dalvikAllocated, dalvikFree);
1075
Dianne Hackbornb437e092011-08-05 17:50:29 -07001076 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 // NOTE: if you change anything significant below, also consider changing
1078 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -07001079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 // Object counts
1081 pw.print(viewInstanceCount); pw.print(',');
1082 pw.print(viewRootInstanceCount); pw.print(',');
1083 pw.print(appContextInstanceCount); pw.print(',');
1084 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001086 pw.print(globalAssetCount); pw.print(',');
1087 pw.print(globalAssetManagerCount); pw.print(',');
1088 pw.print(binderLocalObjectCount); pw.print(',');
1089 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001090
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 pw.print(binderDeathObjectCount); pw.print(',');
1092 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -07001093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 // SQL
Vasu Noric3849202010-03-09 10:47:25 -08001095 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -08001096 pw.print(stats.memoryUsed / 1024); pw.print(',');
1097 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -07001098 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001099 for (int i = 0; i < stats.dbStats.size(); i++) {
1100 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -07001101 pw.print(','); pw.print(dbStats.dbName);
1102 pw.print(','); pw.print(dbStats.pageSize);
1103 pw.print(','); pw.print(dbStats.dbSize);
1104 pw.print(','); pw.print(dbStats.lookaside);
1105 pw.print(','); pw.print(dbStats.cache);
1106 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001107 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001108 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001109
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001110 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Bob Leee5408332009-09-04 18:31:17 -07001112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 pw.println(" ");
1114 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001115 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 viewRootInstanceCount);
1117
1118 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1119 "Activities:", activityInstanceCount);
1120
1121 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1122 "AssetManagers:", globalAssetManagerCount);
1123
1124 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1125 "Proxy Binders:", binderProxyObjectCount);
Dianne Hackbornfabb70b2014-11-11 12:22:36 -08001126 printRow(pw, TWO_COUNT_COLUMNS, "Parcel memory:", parcelSize/1024,
1127 "Parcel count:", parcelCount);
1128 printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
1129 "OpenSSL Sockets:", openSslSocketCount);
Richard Uhler2c036192016-09-14 09:48:31 +01001130 printRow(pw, ONE_COUNT_COLUMN, "WebViews:", webviewInstanceCount);
Bob Leee5408332009-09-04 18:31:17 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 // SQLite mem info
1133 pw.println(" ");
1134 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001135 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1136 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1137 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001138 pw.println(" ");
1139 int N = stats.dbStats.size();
1140 if (N > 0) {
1141 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001142 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1143 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001144 for (int i = 0; i < N; i++) {
1145 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001146 printRow(pw, DB_INFO_FORMAT,
1147 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1148 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1149 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1150 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001151 }
1152 }
Bob Leee5408332009-09-04 18:31:17 -07001153
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001154 // Asset details.
1155 String assetAlloc = AssetManager.getAssetAllocations();
1156 if (assetAlloc != null) {
1157 pw.println(" ");
1158 pw.println(" Asset Allocations");
1159 pw.print(assetAlloc);
1160 }
Colin Crossc4fb5f92016-02-02 16:51:15 -08001161
1162 // Unreachable native memory
1163 if (dumpUnreachable) {
1164 boolean showContents = ((mBoundApplication != null)
1165 && ((mBoundApplication.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0))
1166 || android.os.Build.IS_DEBUGGABLE;
1167 pw.println(" ");
1168 pw.println(" Unreachable memory");
1169 pw.print(Debug.getUnreachableMemory(100, showContents));
1170 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001171 }
1172
1173 @Override
1174 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1175 dumpGraphicsInfo(fd);
John Reckba6adf62015-02-19 14:36:50 -08001176 WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 }
1178
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001179 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001180 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001181 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1182 SQLiteDebug.dump(printer, args);
1183 pw.flush();
1184 }
1185
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001186 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001187 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1188 if (mSystemThread) {
Felipe Leme29de4922016-06-07 16:14:07 -07001189 // Ensure this invocation is asynchronous to prevent writer waiting if buffer cannot
1190 // be consumed. But it must duplicate the file descriptor first, since caller might
1191 // be closing it.
1192 final ParcelFileDescriptor dup;
1193 try {
1194 dup = ParcelFileDescriptor.dup(fd);
1195 } catch (IOException e) {
1196 Log.w(TAG, "Could not dup FD " + fd.getInt$());
1197 return;
1198 }
1199
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001200 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1201 @Override
1202 public void run() {
Felipe Lemec74972f2016-06-08 09:12:37 -07001203 try {
1204 dumpDatabaseInfo(dup.getFileDescriptor(), args);
1205 } finally {
1206 IoUtils.closeQuietly(dup);
1207 }
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001208 }
1209 });
1210 } else {
1211 dumpDatabaseInfo(fd, args);
1212 }
1213 }
1214
1215 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001216 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001217 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001218 }
1219
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001220 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001221 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Amith Yamasani4f128e42016-05-10 11:44:12 -07001222 int requestType, int sessionId) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001223 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001224 cmd.activityToken = activityToken;
1225 cmd.requestToken = requestToken;
1226 cmd.requestType = requestType;
Amith Yamasani4f128e42016-05-10 11:44:12 -07001227 cmd.sessionId = sessionId;
Jeff Brown9ef09972013-10-15 20:49:59 -07001228 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001229 }
1230
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001231 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001232 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001233 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001234
1235 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1236 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1237 ucd.pkg = pkg;
1238 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001239 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001240 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001241
1242 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001243 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001244 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001245
Craig Mautner5eda9b32013-07-02 11:58:16 -07001246 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001247 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001248 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001249
Craig Mautnereb8abf72014-07-02 15:04:09 -07001250 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1251 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1252 new Pair<IBinder, ActivityOptions>(token, options));
1253 }
1254
Dianne Hackborna413dc02013-07-12 12:02:55 -07001255 public void setProcessState(int state) {
1256 updateProcessState(state, true);
1257 }
1258
1259 public void updateProcessState(int processState, boolean fromIpc) {
1260 synchronized (this) {
1261 if (mLastProcessState != processState) {
1262 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001263 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1264 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1265 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1266 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1267 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1268 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1269 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1270 }
1271 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001272 if (false) {
1273 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1274 + (fromIpc ? " (from ipc": ""));
1275 }
1276 }
1277 }
1278 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001279
1280 @Override
1281 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001282 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001283 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001284
1285 @Override
1286 public final void updateTimePrefs(boolean is24Hour) {
1287 DateFormat.set24HourTimePref(is24Hour);
1288 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001289
1290 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001291 public void scheduleCancelVisibleBehind(IBinder token) {
1292 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001293 }
1294
1295 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001296 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1297 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001298 }
Craig Mautner8746a472014-07-24 15:12:54 -07001299
Jeff Sharkey605eb792014-11-04 13:34:06 -08001300 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001301 public void scheduleEnterAnimationComplete(IBinder token) {
1302 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1303 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001304
1305 @Override
1306 public void notifyCleartextNetwork(byte[] firstPacket) {
1307 if (StrictMode.vmCleartextNetworkEnabled()) {
1308 StrictMode.onCleartextNetworkDetected(firstPacket);
1309 }
1310 }
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001311
1312 @Override
1313 public void startBinderTracking() {
1314 sendMessage(H.START_BINDER_TRACKING, null);
1315 }
1316
1317 @Override
1318 public void stopBinderTrackingAndDump(FileDescriptor fd) {
1319 try {
1320 sendMessage(H.STOP_BINDER_TRACKING_AND_DUMP, ParcelFileDescriptor.dup(fd));
1321 } catch (IOException e) {
1322 }
1323 }
Wale Ogunwale5f986092015-12-04 15:35:38 -08001324
1325 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001326 public void scheduleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001327 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001328 sendMessage(H.MULTI_WINDOW_MODE_CHANGED, token, isInMultiWindowMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001329 }
1330
1331 @Override
Andrii Kulian933076d2016-03-29 17:04:42 -07001332 public void schedulePictureInPictureModeChanged(IBinder token, boolean isInPipMode)
Wale Ogunwale5f986092015-12-04 15:35:38 -08001333 throws RemoteException {
Andrii Kulian933076d2016-03-29 17:04:42 -07001334 sendMessage(H.PICTURE_IN_PICTURE_MODE_CHANGED, token, isInPipMode ? 1 : 0);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001335 }
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001336
1337 @Override
1338 public void scheduleLocalVoiceInteractionStarted(IBinder token,
1339 IVoiceInteractor voiceInteractor) throws RemoteException {
1340 SomeArgs args = SomeArgs.obtain();
1341 args.arg1 = token;
1342 args.arg2 = voiceInteractor;
1343 sendMessage(H.LOCAL_VOICE_INTERACTION_STARTED, args);
1344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001347 private int getLifecycleSeq() {
1348 synchronized (mResourcesManager) {
1349 return mLifecycleSeq++;
1350 }
1351 }
1352
Romain Guy65b345f2011-07-27 18:51:50 -07001353 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 public static final int LAUNCH_ACTIVITY = 100;
1355 public static final int PAUSE_ACTIVITY = 101;
1356 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1357 public static final int STOP_ACTIVITY_SHOW = 103;
1358 public static final int STOP_ACTIVITY_HIDE = 104;
1359 public static final int SHOW_WINDOW = 105;
1360 public static final int HIDE_WINDOW = 106;
1361 public static final int RESUME_ACTIVITY = 107;
1362 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001363 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 public static final int BIND_APPLICATION = 110;
1365 public static final int EXIT_APPLICATION = 111;
1366 public static final int NEW_INTENT = 112;
1367 public static final int RECEIVER = 113;
1368 public static final int CREATE_SERVICE = 114;
1369 public static final int SERVICE_ARGS = 115;
1370 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 public static final int CONFIGURATION_CHANGED = 118;
1373 public static final int CLEAN_UP_CONTEXT = 119;
1374 public static final int GC_WHEN_IDLE = 120;
1375 public static final int BIND_SERVICE = 121;
1376 public static final int UNBIND_SERVICE = 122;
1377 public static final int DUMP_SERVICE = 123;
1378 public static final int LOW_MEMORY = 124;
1379 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1380 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001381 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001382 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001383 public static final int DESTROY_BACKUP_AGENT = 129;
1384 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001385 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001386 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001387 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001388 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001389 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001390 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001391 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001392 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001393 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001394 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001395 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001396 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001397 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001398 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001399 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001400 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001401 public static final int CANCEL_VISIBLE_BEHIND = 147;
1402 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001403 public static final int ENTER_ANIMATION_COMPLETE = 149;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001404 public static final int START_BINDER_TRACKING = 150;
1405 public static final int STOP_BINDER_TRACKING_AND_DUMP = 151;
Andrii Kulian933076d2016-03-29 17:04:42 -07001406 public static final int MULTI_WINDOW_MODE_CHANGED = 152;
1407 public static final int PICTURE_IN_PICTURE_MODE_CHANGED = 153;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001408 public static final int LOCAL_VOICE_INTERACTION_STARTED = 154;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001411 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 switch (code) {
1413 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1414 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1415 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1416 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1417 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1418 case SHOW_WINDOW: return "SHOW_WINDOW";
1419 case HIDE_WINDOW: return "HIDE_WINDOW";
1420 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1421 case SEND_RESULT: return "SEND_RESULT";
1422 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1423 case BIND_APPLICATION: return "BIND_APPLICATION";
1424 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1425 case NEW_INTENT: return "NEW_INTENT";
1426 case RECEIVER: return "RECEIVER";
1427 case CREATE_SERVICE: return "CREATE_SERVICE";
1428 case SERVICE_ARGS: return "SERVICE_ARGS";
1429 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1431 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1432 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1433 case BIND_SERVICE: return "BIND_SERVICE";
1434 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1435 case DUMP_SERVICE: return "DUMP_SERVICE";
1436 case LOW_MEMORY: return "LOW_MEMORY";
1437 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1438 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001439 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001440 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1441 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001442 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001443 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001444 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001445 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001446 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001447 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001448 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001449 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001450 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001451 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001452 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001453 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001454 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001455 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001456 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001457 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001458 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001459 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1460 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001461 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
Andrii Kulian933076d2016-03-29 17:04:42 -07001462 case MULTI_WINDOW_MODE_CHANGED: return "MULTI_WINDOW_MODE_CHANGED";
1463 case PICTURE_IN_PICTURE_MODE_CHANGED: return "PICTURE_IN_PICTURE_MODE_CHANGED";
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001464 case LOCAL_VOICE_INTERACTION_STARTED: return "LOCAL_VOICE_INTERACTION_STARTED";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 }
1466 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001467 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
1469 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001470 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 switch (msg.what) {
1472 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001473 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001474 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475
1476 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001477 r.activityInfo.applicationInfo, r.compatInfo);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001478 handleLaunchActivity(r, null, "LAUNCH_ACTIVITY");
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 RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001483 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001484 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001485 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 } break;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001487 case PAUSE_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001488 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001489 SomeArgs args = (SomeArgs) msg.obj;
1490 handlePauseActivity((IBinder) args.arg1, false,
1491 (args.argi1 & USER_LEAVING) != 0, args.argi2,
1492 (args.argi1 & DONT_REPORT) != 0, args.argi3);
Bob Leee5408332009-09-04 18:31:17 -07001493 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001494 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001495 } break;
1496 case PAUSE_ACTIVITY_FINISHING: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001497 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001498 SomeArgs args = (SomeArgs) msg.obj;
1499 handlePauseActivity((IBinder) args.arg1, true, (args.argi1 & USER_LEAVING) != 0,
1500 args.argi2, (args.argi1 & DONT_REPORT) != 0, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001501 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001502 } break;
1503 case STOP_ACTIVITY_SHOW: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001504 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001505 SomeArgs args = (SomeArgs) msg.obj;
1506 handleStopActivity((IBinder) args.arg1, true, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001507 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001508 } break;
1509 case STOP_ACTIVITY_HIDE: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001510 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001511 SomeArgs args = (SomeArgs) msg.obj;
1512 handleStopActivity((IBinder) args.arg1, false, args.argi2, args.argi3);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001513 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001514 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001516 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001518 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 break;
1520 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001521 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001523 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 break;
1525 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001526 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001527 SomeArgs args = (SomeArgs) msg.obj;
1528 handleResumeActivity((IBinder) args.arg1, true, args.argi1 != 0, true,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08001529 args.argi3, "RESUME_ACTIVITY");
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 SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001533 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 handleSendResult((ResultData)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 DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001538 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1540 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001541 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 break;
1543 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001544 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001545 AppBindData data = (AppBindData)msg.obj;
1546 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001547 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 break;
1549 case EXIT_APPLICATION:
1550 if (mInitialApplication != null) {
1551 mInitialApplication.onTerminate();
1552 }
1553 Looper.myLooper().quit();
1554 break;
1555 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001556 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001558 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 break;
1560 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001561 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001563 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001564 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 break;
1566 case CREATE_SERVICE:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001567 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceCreate: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001569 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 break;
1571 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001572 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001574 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 break;
1576 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001577 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001578 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001579 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 break;
1581 case SERVICE_ARGS:
Tim Murrayb6f5a422016-04-07 15:25:22 -07001582 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, ("serviceStart: " + String.valueOf(msg.obj)));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001584 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 break;
1586 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001587 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001589 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001590 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001593 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001594 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Chong Zhang4951f9d2016-06-23 13:15:08 -07001595 mUpdatingSystemConfig = true;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001596 handleConfigurationChanged((Configuration)msg.obj, null);
Chong Zhang4951f9d2016-06-23 13:15:08 -07001597 mUpdatingSystemConfig = false;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001598 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 break;
1600 case CLEAN_UP_CONTEXT:
1601 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1602 cci.context.performFinalCleanup(cci.who, cci.what);
1603 break;
1604 case GC_WHEN_IDLE:
1605 scheduleGcIdler();
1606 break;
1607 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001608 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 break;
1610 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001611 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001613 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614 break;
1615 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001616 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
Filip Gruszczynskica664812015-12-04 12:43:36 -08001617 handleActivityConfigurationChanged((ActivityConfigChangeData) msg.obj,
1618 msg.arg1 == 1 ? REPORT_TO_ACTIVITY : !REPORT_TO_ACTIVITY);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001619 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001621 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001622 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001623 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001624 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001625 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001626 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001627 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001628 break;
1629 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001630 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001631 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001632 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001633 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001634 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001635 Process.killProcess(Process.myPid());
1636 break;
1637 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001638 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001639 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001640 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001641 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001642 case ENABLE_JIT:
1643 ensureJitEnabled();
1644 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001645 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001646 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001647 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001648 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001649 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001650 case SCHEDULE_CRASH:
1651 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001652 case DUMP_HEAP:
1653 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1654 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001655 case DUMP_ACTIVITY:
1656 handleDumpActivity((DumpComponentInfo)msg.obj);
1657 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001658 case DUMP_PROVIDER:
1659 handleDumpProvider((DumpComponentInfo)msg.obj);
1660 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001661 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001662 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001663 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001664 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001665 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001666 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001667 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001668 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001669 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001670 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001671 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1672 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001673 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001674 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001675 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001676 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001677 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001678 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001679 case UNSTABLE_PROVIDER_DIED:
1680 handleUnstableProviderDied((IBinder)msg.obj, false);
1681 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001682 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1683 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001684 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001685 case TRANSLUCENT_CONVERSION_COMPLETE:
1686 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1687 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001688 case INSTALL_PROVIDER:
1689 handleInstallProvider((ProviderInfo) msg.obj);
1690 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001691 case ON_NEW_ACTIVITY_OPTIONS:
1692 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1693 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001694 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001695 case CANCEL_VISIBLE_BEHIND:
1696 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001697 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001698 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1699 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001700 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001701 case ENTER_ANIMATION_COMPLETE:
1702 handleEnterAnimationComplete((IBinder) msg.obj);
1703 break;
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04001704 case START_BINDER_TRACKING:
1705 handleStartBinderTracking();
1706 break;
1707 case STOP_BINDER_TRACKING_AND_DUMP:
1708 handleStopBinderTrackingAndDump((ParcelFileDescriptor) msg.obj);
1709 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001710 case MULTI_WINDOW_MODE_CHANGED:
1711 handleMultiWindowModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001712 break;
Andrii Kulian933076d2016-03-29 17:04:42 -07001713 case PICTURE_IN_PICTURE_MODE_CHANGED:
1714 handlePictureInPictureModeChanged((IBinder) msg.obj, msg.arg1 == 1);
Wale Ogunwale5f986092015-12-04 15:35:38 -08001715 break;
Amith Yamasani0af6fa72016-01-17 15:36:19 -08001716 case LOCAL_VOICE_INTERACTION_STARTED:
1717 handleLocalVoiceInteractionStarted((IBinder) ((SomeArgs) msg.obj).arg1,
1718 (IVoiceInteractor) ((SomeArgs) msg.obj).arg2);
1719 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001720 }
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07001721 Object obj = msg.obj;
1722 if (obj instanceof SomeArgs) {
1723 ((SomeArgs) obj).recycle();
1724 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001725 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001726 }
Bob Leee5408332009-09-04 18:31:17 -07001727
Brian Carlstromed7e0072011-03-24 13:27:57 -07001728 private void maybeSnapshot() {
1729 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001730 // convert the *private* ActivityThread.PackageInfo to *public* known
1731 // android.content.pm.PackageInfo
1732 String packageName = mBoundApplication.info.mPackageName;
1733 android.content.pm.PackageInfo packageInfo = null;
1734 try {
1735 Context context = getSystemContext();
1736 if(context == null) {
1737 Log.e(TAG, "cannot get a valid context");
1738 return;
1739 }
1740 PackageManager pm = context.getPackageManager();
1741 if(pm == null) {
1742 Log.e(TAG, "cannot get a valid PackageManager");
1743 return;
1744 }
1745 packageInfo = pm.getPackageInfo(
1746 packageName, PackageManager.GET_ACTIVITIES);
1747 } catch (NameNotFoundException e) {
1748 Log.e(TAG, "cannot get package info for " + packageName, e);
1749 }
1750 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001751 }
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
Romain Guy65b345f2011-07-27 18:51:50 -07001755 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001756 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001758 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001759 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001760 if (mBoundApplication != null && mProfiler.profileFd != null
1761 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001762 stopProfiling = true;
1763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 if (a != null) {
1765 mNewActivities = null;
1766 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001767 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001769 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 TAG, "Reporting idle of " + a +
1771 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001772 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 if (a.activity != null && !a.activity.mFinished) {
1774 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001775 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001776 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001778 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 }
1780 }
1781 prev = a;
1782 a = a.nextIdle;
1783 prev.nextIdle = null;
1784 } while (a != null);
1785 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001786 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001787 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001788 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001789 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 return false;
1791 }
1792 }
1793
1794 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001795 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 public final boolean queueIdle() {
1797 doGcIfNeeded();
1798 return false;
1799 }
1800 }
1801
Romain Guy65b345f2011-07-27 18:51:50 -07001802 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001803 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
Wale Ogunwale9a6ef1e2015-06-02 13:41:00 -07001806 public static boolean isSystem() {
1807 return (sCurrentActivityThread != null) ? sCurrentActivityThread.mSystemThread : false;
1808 }
1809
Svetoslavfbf0eca2015-05-01 16:52:41 -07001810 public static String currentOpPackageName() {
1811 ActivityThread am = currentActivityThread();
1812 return (am != null && am.getApplication() != null)
1813 ? am.getApplication().getOpPackageName() : null;
1814 }
1815
Romain Guy65b345f2011-07-27 18:51:50 -07001816 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001817 ActivityThread am = currentActivityThread();
1818 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001819 ? am.mBoundApplication.appInfo.packageName : null;
1820 }
1821
1822 public static String currentProcessName() {
1823 ActivityThread am = currentActivityThread();
1824 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001825 ? am.mBoundApplication.processName : null;
1826 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827
Romain Guy65b345f2011-07-27 18:51:50 -07001828 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001829 ActivityThread am = currentActivityThread();
1830 return am != null ? am.mInitialApplication : null;
1831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001833 public static IPackageManager getPackageManager() {
1834 if (sPackageManager != null) {
1835 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1836 return sPackageManager;
1837 }
1838 IBinder b = ServiceManager.getService("package");
1839 //Slog.v("PackageManager", "default service binder = " + b);
1840 sPackageManager = IPackageManager.Stub.asInterface(b);
1841 //Slog.v("PackageManager", "default service = " + sPackageManager);
1842 return sPackageManager;
1843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844
Romain Guy65b345f2011-07-27 18:51:50 -07001845 private Configuration mMainThreadConfig = new Configuration();
Amith Yamasani4f128e42016-05-10 11:44:12 -07001846
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001847 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1848 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001849 if (config == null) {
1850 return null;
1851 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001852 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001853 mMainThreadConfig.setTo(config);
1854 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001855 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001856 }
1857 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001860 /**
Todd Kennedy39bfee52016-02-24 10:28:21 -08001861 * Creates the top level resources for the given package. Will return an existing
1862 * Resources if one has already been created.
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001863 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001864 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
Adam Lesinski082614c2016-03-04 14:33:47 -08001865 String[] libDirs, int displayId, LoadedApk pkgInfo) {
1866 return mResourcesManager.getResources(null, resDir, splitResDirs, overlayDirs, libDirs,
1867 displayId, null, pkgInfo.getCompatibilityInfo(), pkgInfo.getClassLoader());
Todd Kennedy39bfee52016-02-24 10:28:21 -08001868 }
1869
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001870 final Handler getHandler() {
1871 return mH;
1872 }
1873
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001874 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1875 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001876 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1877 }
1878
1879 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1880 int flags, int userId) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001881 final boolean differentUser = (UserHandle.myUserId() != userId);
Craig Mautner88c05892013-06-28 09:47:45 -07001882 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001883 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001884 if (differentUser) {
1885 // Caching not supported across users
1886 ref = null;
1887 } else if ((flags & Context.CONTEXT_INCLUDE_CODE) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 ref = mPackages.get(packageName);
1889 } else {
1890 ref = mResourcePackages.get(packageName);
1891 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001892
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001893 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001894 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001895 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1896 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 if (packageInfo != null && (packageInfo.mResources == null
1898 || packageInfo.mResources.getAssets().isUpToDate())) {
1899 if (packageInfo.isSecurityViolation()
1900 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1901 throw new SecurityException(
1902 "Requesting code from " + packageName
1903 + " to be run in process "
1904 + mBoundApplication.processName
1905 + "/" + mBoundApplication.appInfo.uid);
1906 }
1907 return packageInfo;
1908 }
1909 }
1910
1911 ApplicationInfo ai = null;
1912 try {
1913 ai = getPackageManager().getApplicationInfo(packageName,
Jeff Sharkeyc5967e92016-01-07 18:50:29 -07001914 PackageManager.GET_SHARED_LIBRARY_FILES
1915 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING,
1916 userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07001918 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 }
1920
1921 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001922 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 }
1924
1925 return null;
1926 }
1927
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001928 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1929 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1931 boolean securityViolation = includeCode && ai.uid != 0
1932 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001933 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001934 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001935 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1937 |Context.CONTEXT_IGNORE_SECURITY))
1938 == Context.CONTEXT_INCLUDE_CODE) {
1939 if (securityViolation) {
1940 String msg = "Requesting code from " + ai.packageName
1941 + " (with uid " + ai.uid + ")";
1942 if (mBoundApplication != null) {
1943 msg = msg + " to be run in process "
1944 + mBoundApplication.processName + " (with uid "
1945 + mBoundApplication.appInfo.uid + ")";
1946 }
1947 throw new SecurityException(msg);
1948 }
1949 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001950 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1951 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 }
1953
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001954 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1955 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001956 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957 }
1958
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001959 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001960 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001961 WeakReference<LoadedApk> ref;
1962 if (includeCode) {
1963 ref = mPackages.get(packageName);
1964 } else {
1965 ref = mResourcePackages.get(packageName);
1966 }
1967 return ref != null ? ref.get() : null;
1968 }
1969 }
1970
Romain Guy65b345f2011-07-27 18:51:50 -07001971 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001972 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1973 boolean registerPackage) {
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001974 final boolean differentUser = (UserHandle.myUserId() != UserHandle.getUserId(aInfo.uid));
Craig Mautner88c05892013-06-28 09:47:45 -07001975 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001976 WeakReference<LoadedApk> ref;
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001977 if (differentUser) {
1978 // Caching not supported across users
1979 ref = null;
1980 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 ref = mPackages.get(aInfo.packageName);
1982 } else {
1983 ref = mResourcePackages.get(aInfo.packageName);
1984 }
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07001985
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001986 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 if (packageInfo == null || (packageInfo.mResources != null
1988 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001989 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 : "Loading resource-only package ") + aInfo.packageName
1991 + " (in " + (mBoundApplication != null
1992 ? mBoundApplication.processName : null)
1993 + ")");
1994 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001995 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001997 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
Narayan Kamathcb383182014-10-29 17:56:42 +00001998
1999 if (mSystemThread && "android".equals(aInfo.packageName)) {
2000 packageInfo.installSystemApplicationInfo(aInfo,
2001 getSystemContext().mPackageInfo.getClassLoader());
2002 }
2003
Jeff Sharkeyb9f36742015-04-08 21:02:14 -07002004 if (differentUser) {
2005 // Caching not supported across users
2006 } else if (includeCode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002008 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 } else {
2010 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002011 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 }
2013 }
2014 return packageInfo;
2015 }
2016 }
2017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002018 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07002019 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 }
2021
2022 public ApplicationThread getApplicationThread()
2023 {
2024 return mAppThread;
2025 }
2026
2027 public Instrumentation getInstrumentation()
2028 {
2029 return mInstrumentation;
2030 }
2031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002033 return mProfiler != null && mProfiler.profileFile != null
2034 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036
2037 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002038 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
2040
2041 public Looper getLooper() {
2042 return mLooper;
2043 }
2044
2045 public Application getApplication() {
2046 return mInitialApplication;
2047 }
Bob Leee5408332009-09-04 18:31:17 -07002048
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07002049 public String getProcessName() {
2050 return mBoundApplication.processName;
2051 }
Bob Leee5408332009-09-04 18:31:17 -07002052
Dianne Hackborn21556372010-02-04 16:34:40 -08002053 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 synchronized (this) {
2055 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002056 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 }
Jeff Browndefd4a62014-03-10 21:24:37 -07002058 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061
Narayan Kamath29564cd2014-08-07 10:57:40 +01002062 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07002063 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01002064 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002065
2066 // give ourselves a default profiler
2067 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07002068 }
2069 }
2070
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002071 void ensureJitEnabled() {
2072 if (!mJitEnabled) {
2073 mJitEnabled = true;
2074 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
2075 }
2076 }
Craig Mautner48d0d182013-06-11 07:53:06 -07002077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 void scheduleGcIdler() {
2079 if (!mGcIdlerScheduled) {
2080 mGcIdlerScheduled = true;
2081 Looper.myQueue().addIdleHandler(mGcIdler);
2082 }
2083 mH.removeMessages(H.GC_WHEN_IDLE);
2084 }
2085
2086 void unscheduleGcIdler() {
2087 if (mGcIdlerScheduled) {
2088 mGcIdlerScheduled = false;
2089 Looper.myQueue().removeIdleHandler(mGcIdler);
2090 }
2091 mH.removeMessages(H.GC_WHEN_IDLE);
2092 }
2093
2094 void doGcIfNeeded() {
2095 mGcIdlerScheduled = false;
2096 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002097 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 // + "m now=" + now);
2099 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002100 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002101 BinderInternal.forceGc("bg");
2102 }
2103 }
2104
Dianne Hackborne77187d2013-10-25 16:32:41 -07002105 private static final String HEAP_FULL_COLUMN
2106 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
2107 private static final String HEAP_COLUMN
2108 = "%13s %8s %8s %8s %8s %8s %8s %8s";
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002109 private static final String ONE_COUNT_COLUMN = "%21s %8d";
2110 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
2111 private static final String ONE_COUNT_COLUMN_HEADER = "%21s %8s";
Dianne Hackborne77187d2013-10-25 16:32:41 -07002112
2113 // Formatting for checkin service - update version if row format changes
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002114 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 4;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002115
2116 static void printRow(PrintWriter pw, String format, Object...objs) {
2117 pw.println(String.format(format, objs));
2118 }
2119
2120 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002121 boolean dumpFullInfo, boolean dumpDalvik, boolean dumpSummaryOnly,
2122 int pid, String processName,
Dianne Hackborne77187d2013-10-25 16:32:41 -07002123 long nativeMax, long nativeAllocated, long nativeFree,
2124 long dalvikMax, long dalvikAllocated, long dalvikFree) {
2125
2126 // For checkin, we print one long comma-separated list of values
2127 if (checkin) {
2128 // NOTE: if you change anything significant below, also consider changing
2129 // ACTIVITY_THREAD_CHECKIN_VERSION.
2130
2131 // Header
2132 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
2133 pw.print(pid); pw.print(',');
2134 pw.print(processName); pw.print(',');
2135
2136 // Heap info - max
2137 pw.print(nativeMax); pw.print(',');
2138 pw.print(dalvikMax); pw.print(',');
2139 pw.print("N/A,");
2140 pw.print(nativeMax + dalvikMax); pw.print(',');
2141
2142 // Heap info - allocated
2143 pw.print(nativeAllocated); pw.print(',');
2144 pw.print(dalvikAllocated); pw.print(',');
2145 pw.print("N/A,");
2146 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
2147
2148 // Heap info - free
2149 pw.print(nativeFree); pw.print(',');
2150 pw.print(dalvikFree); pw.print(',');
2151 pw.print("N/A,");
2152 pw.print(nativeFree + dalvikFree); pw.print(',');
2153
2154 // Heap info - proportional set size
2155 pw.print(memInfo.nativePss); pw.print(',');
2156 pw.print(memInfo.dalvikPss); pw.print(',');
2157 pw.print(memInfo.otherPss); pw.print(',');
2158 pw.print(memInfo.getTotalPss()); pw.print(',');
2159
2160 // Heap info - swappable set size
2161 pw.print(memInfo.nativeSwappablePss); pw.print(',');
2162 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
2163 pw.print(memInfo.otherSwappablePss); pw.print(',');
2164 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
2165
2166 // Heap info - shared dirty
2167 pw.print(memInfo.nativeSharedDirty); pw.print(',');
2168 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
2169 pw.print(memInfo.otherSharedDirty); pw.print(',');
2170 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
2171
2172 // Heap info - shared clean
2173 pw.print(memInfo.nativeSharedClean); pw.print(',');
2174 pw.print(memInfo.dalvikSharedClean); pw.print(',');
2175 pw.print(memInfo.otherSharedClean); pw.print(',');
2176 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
2177
2178 // Heap info - private Dirty
2179 pw.print(memInfo.nativePrivateDirty); pw.print(',');
2180 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
2181 pw.print(memInfo.otherPrivateDirty); pw.print(',');
2182 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
2183
2184 // Heap info - private Clean
2185 pw.print(memInfo.nativePrivateClean); pw.print(',');
2186 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
2187 pw.print(memInfo.otherPrivateClean); pw.print(',');
2188 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
2189
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002190 // Heap info - swapped out
2191 pw.print(memInfo.nativeSwappedOut); pw.print(',');
2192 pw.print(memInfo.dalvikSwappedOut); pw.print(',');
2193 pw.print(memInfo.otherSwappedOut); pw.print(',');
2194 pw.print(memInfo.getTotalSwappedOut()); pw.print(',');
2195
2196 // Heap info - swapped out pss
2197 if (memInfo.hasSwappedOutPss) {
2198 pw.print(memInfo.nativeSwappedOutPss); pw.print(',');
2199 pw.print(memInfo.dalvikSwappedOutPss); pw.print(',');
2200 pw.print(memInfo.otherSwappedOutPss); pw.print(',');
2201 pw.print(memInfo.getTotalSwappedOutPss()); pw.print(',');
2202 } else {
2203 pw.print("N/A,");
2204 pw.print("N/A,");
2205 pw.print("N/A,");
2206 pw.print("N/A,");
2207 }
2208
Dianne Hackborne77187d2013-10-25 16:32:41 -07002209 // Heap info - other areas
2210 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2211 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
2212 pw.print(memInfo.getOtherPss(i)); pw.print(',');
2213 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
2214 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
2215 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
2216 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
2217 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
Martijn Coenen41f94ed2015-12-14 15:28:51 +01002218 pw.print(memInfo.getOtherSwappedOut(i)); pw.print(',');
2219 if (memInfo.hasSwappedOutPss) {
2220 pw.print(memInfo.getOtherSwappedOutPss(i)); pw.print(',');
2221 } else {
2222 pw.print("N/A,");
2223 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002224 }
2225 return;
2226 }
2227
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002228 if (!dumpSummaryOnly) {
2229 if (dumpFullInfo) {
2230 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002231 "Shared", "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2232 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002233 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
Martijn Coenene0764852016-01-07 17:04:22 -08002234 "Clean", "Clean", "Dirty",
2235 "Size", "Alloc", "Free");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002236 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
2237 "------", "------", "------", "------", "------", "------");
2238 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
2239 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
2240 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002241 memInfo.nativePrivateClean, memInfo.hasSwappedOutPss ?
2242 memInfo.nativeSwappedOut : memInfo.nativeSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002243 nativeMax, nativeAllocated, nativeFree);
2244 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2245 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
2246 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002247 memInfo.dalvikPrivateClean, memInfo.hasSwappedOutPss ?
2248 memInfo.dalvikSwappedOut : memInfo.dalvikSwappedOutPss,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002249 dalvikMax, dalvikAllocated, dalvikFree);
2250 } else {
2251 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
Martijn Coenene0764852016-01-07 17:04:22 -08002252 "Private", memInfo.hasSwappedOutPss ? "SwapPss" : "Swap",
2253 "Heap", "Heap", "Heap");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002254 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
2255 "Clean", "Dirty", "Size", "Alloc", "Free");
2256 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
2257 "------", "------", "------", "------", "------");
2258 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
2259 memInfo.nativePrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002260 memInfo.nativePrivateClean,
2261 memInfo.hasSwappedOutPss ? memInfo.nativeSwappedOutPss :
2262 memInfo.nativeSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002263 nativeMax, nativeAllocated, nativeFree);
2264 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
2265 memInfo.dalvikPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002266 memInfo.dalvikPrivateClean,
2267 memInfo.hasSwappedOutPss ? memInfo.dalvikSwappedOutPss :
2268 memInfo.dalvikSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002269 dalvikMax, dalvikAllocated, dalvikFree);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002270 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002271
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002272 int otherPss = memInfo.otherPss;
2273 int otherSwappablePss = memInfo.otherSwappablePss;
2274 int otherSharedDirty = memInfo.otherSharedDirty;
2275 int otherPrivateDirty = memInfo.otherPrivateDirty;
2276 int otherSharedClean = memInfo.otherSharedClean;
2277 int otherPrivateClean = memInfo.otherPrivateClean;
2278 int otherSwappedOut = memInfo.otherSwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002279 int otherSwappedOutPss = memInfo.otherSwappedOutPss;
Dianne Hackborne77187d2013-10-25 16:32:41 -07002280
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002281 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002282 final int myPss = memInfo.getOtherPss(i);
2283 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2284 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2285 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2286 final int mySharedClean = memInfo.getOtherSharedClean(i);
2287 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2288 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002289 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Dianne Hackborne77187d2013-10-25 16:32:41 -07002290 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002291 || mySharedClean != 0 || myPrivateClean != 0
2292 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Dianne Hackborne77187d2013-10-25 16:32:41 -07002293 if (dumpFullInfo) {
2294 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2295 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002296 mySharedClean, myPrivateClean,
2297 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2298 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002299 } else {
2300 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2301 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002302 myPrivateClean,
2303 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2304 "", "", "");
Dianne Hackborne77187d2013-10-25 16:32:41 -07002305 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002306 otherPss -= myPss;
2307 otherSwappablePss -= mySwappablePss;
2308 otherSharedDirty -= mySharedDirty;
2309 otherPrivateDirty -= myPrivateDirty;
2310 otherSharedClean -= mySharedClean;
2311 otherPrivateClean -= myPrivateClean;
2312 otherSwappedOut -= mySwappedOut;
Martijn Coenene0764852016-01-07 17:04:22 -08002313 otherSwappedOutPss -= mySwappedOutPss;
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002314 }
2315 }
2316
2317 if (dumpFullInfo) {
2318 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2319 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
Martijn Coenene0764852016-01-07 17:04:22 -08002320 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
2321 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002322 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2323 memInfo.getTotalSwappablePss(),
2324 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2325 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
Thierry Strudel9b511602016-02-25 17:46:38 -08002326 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
2327 memInfo.getTotalSwappedOut(),
Martijn Coenene0764852016-01-07 17:04:22 -08002328 nativeMax+dalvikMax, nativeAllocated+dalvikAllocated,
2329 nativeFree+dalvikFree);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002330 } else {
2331 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
Martijn Coenene0764852016-01-07 17:04:22 -08002332 otherPrivateDirty, otherPrivateClean,
2333 memInfo.hasSwappedOutPss ? otherSwappedOutPss : otherSwappedOut,
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002334 "", "", "");
2335 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2336 memInfo.getTotalPrivateDirty(),
2337 memInfo.getTotalPrivateClean(),
Martijn Coenene0764852016-01-07 17:04:22 -08002338 memInfo.hasSwappedOutPss ? memInfo.getTotalSwappedOutPss() :
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002339 memInfo.getTotalSwappedOut(),
2340 nativeMax+dalvikMax,
2341 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2342 }
2343
2344 if (dumpDalvik) {
2345 pw.println(" ");
2346 pw.println(" Dalvik Details");
2347
2348 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2349 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2350 final int myPss = memInfo.getOtherPss(i);
2351 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2352 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2353 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2354 final int mySharedClean = memInfo.getOtherSharedClean(i);
2355 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2356 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
Martijn Coenene0764852016-01-07 17:04:22 -08002357 final int mySwappedOutPss = memInfo.getOtherSwappedOutPss(i);
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002358 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
Martijn Coenene0764852016-01-07 17:04:22 -08002359 || mySharedClean != 0 || myPrivateClean != 0
2360 || (memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut) != 0) {
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002361 if (dumpFullInfo) {
2362 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2363 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002364 mySharedClean, myPrivateClean,
2365 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2366 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002367 } else {
2368 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2369 myPss, myPrivateDirty,
Martijn Coenene0764852016-01-07 17:04:22 -08002370 myPrivateClean,
2371 memInfo.hasSwappedOutPss ? mySwappedOutPss : mySwappedOut,
2372 "", "", "");
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002373 }
2374 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002375 }
2376 }
2377 }
Richard Uhlerc14b9cf2015-03-13 12:38:38 -07002378
2379 pw.println(" ");
2380 pw.println(" App Summary");
2381 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "Pss(KB)");
2382 printRow(pw, ONE_COUNT_COLUMN_HEADER, "", "------");
2383 printRow(pw, ONE_COUNT_COLUMN,
2384 "Java Heap:", memInfo.getSummaryJavaHeap());
2385 printRow(pw, ONE_COUNT_COLUMN,
2386 "Native Heap:", memInfo.getSummaryNativeHeap());
2387 printRow(pw, ONE_COUNT_COLUMN,
2388 "Code:", memInfo.getSummaryCode());
2389 printRow(pw, ONE_COUNT_COLUMN,
2390 "Stack:", memInfo.getSummaryStack());
2391 printRow(pw, ONE_COUNT_COLUMN,
2392 "Graphics:", memInfo.getSummaryGraphics());
2393 printRow(pw, ONE_COUNT_COLUMN,
2394 "Private Other:", memInfo.getSummaryPrivateOther());
2395 printRow(pw, ONE_COUNT_COLUMN,
2396 "System:", memInfo.getSummarySystem());
2397 pw.println(" ");
Martijn Coenene0764852016-01-07 17:04:22 -08002398 if (memInfo.hasSwappedOutPss) {
2399 printRow(pw, TWO_COUNT_COLUMNS,
2400 "TOTAL:", memInfo.getSummaryTotalPss(),
2401 "TOTAL SWAP PSS:", memInfo.getSummaryTotalSwapPss());
2402 } else {
2403 printRow(pw, TWO_COUNT_COLUMNS,
2404 "TOTAL:", memInfo.getSummaryTotalPss(),
2405 "TOTAL SWAP (KB):", memInfo.getSummaryTotalSwap());
2406 }
Dianne Hackborne77187d2013-10-25 16:32:41 -07002407 }
2408
Jeff Hamilton52d32032011-01-08 15:31:26 -06002409 public void registerOnActivityPausedListener(Activity activity,
2410 OnActivityPausedListener listener) {
2411 synchronized (mOnPauseListeners) {
2412 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2413 if (list == null) {
2414 list = new ArrayList<OnActivityPausedListener>();
2415 mOnPauseListeners.put(activity, list);
2416 }
2417 list.add(listener);
2418 }
2419 }
2420
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002421 public void unregisterOnActivityPausedListener(Activity activity,
2422 OnActivityPausedListener listener) {
2423 synchronized (mOnPauseListeners) {
2424 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2425 if (list != null) {
2426 list.remove(listener);
2427 }
2428 }
2429 }
2430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 public final ActivityInfo resolveActivityInfo(Intent intent) {
2432 ActivityInfo aInfo = intent.resolveActivityInfo(
2433 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2434 if (aInfo == null) {
2435 // Throw an exception.
2436 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002437 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002438 }
2439 return aInfo;
2440 }
Bob Leee5408332009-09-04 18:31:17 -07002441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002444 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002445 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002447 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 r.intent = intent;
2449 r.state = state;
2450 r.parent = parent;
2451 r.embeddedID = id;
2452 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002453 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 if (localLOGV) {
2455 ComponentName compname = intent.getComponent();
2456 String name;
2457 if (compname != null) {
2458 name = compname.toShortString();
2459 } else {
2460 name = "(Intent " + intent + ").getComponent() returned null";
2461 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002462 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002463 + ", comp=" + name
2464 + ", token=" + token);
2465 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002466 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468
2469 public final Activity getActivity(IBinder token) {
2470 return mActivities.get(token).activity;
2471 }
2472
2473 public final void sendActivityResult(
2474 IBinder token, String id, int requestCode,
2475 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002476 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002477 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2479 list.add(new ResultInfo(id, requestCode, resultCode, data));
2480 mAppThread.scheduleSendResult(token, list);
2481 }
2482
Jeff Brown9ef09972013-10-15 20:49:59 -07002483 private void sendMessage(int what, Object obj) {
2484 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 }
2486
Jeff Brown9ef09972013-10-15 20:49:59 -07002487 private void sendMessage(int what, Object obj, int arg1) {
2488 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002489 }
2490
Jeff Brown9ef09972013-10-15 20:49:59 -07002491 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2492 sendMessage(what, obj, arg1, arg2, false);
2493 }
2494
2495 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2496 if (DEBUG_MESSAGES) Slog.v(
2497 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2498 + ": " + arg1 + " / " + obj);
2499 Message msg = Message.obtain();
2500 msg.what = what;
2501 msg.obj = obj;
2502 msg.arg1 = arg1;
2503 msg.arg2 = arg2;
2504 if (async) {
2505 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002507 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002508 }
2509
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07002510 private void sendMessage(int what, Object obj, int arg1, int arg2, int seq) {
2511 if (DEBUG_MESSAGES) Slog.v(
2512 TAG, "SCHEDULE " + mH.codeToString(what) + " arg1=" + arg1 + " arg2=" + arg2 +
2513 "seq= " + seq);
2514 Message msg = Message.obtain();
2515 msg.what = what;
2516 SomeArgs args = SomeArgs.obtain();
2517 args.arg1 = obj;
2518 args.argi1 = arg1;
2519 args.argi2 = arg2;
2520 args.argi3 = seq;
2521 msg.obj = args;
2522 mH.sendMessage(msg);
2523 }
2524
Dianne Hackborn21556372010-02-04 16:34:40 -08002525 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002526 String what) {
2527 ContextCleanupInfo cci = new ContextCleanupInfo();
2528 cci.context = context;
2529 cci.who = who;
2530 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002531 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 }
2533
Craig Mautner233ceee2014-05-09 17:05:11 -07002534 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2536
2537 ActivityInfo aInfo = r.activityInfo;
2538 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002539 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002540 Context.CONTEXT_INCLUDE_CODE);
2541 }
Bob Leee5408332009-09-04 18:31:17 -07002542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002543 ComponentName component = r.intent.getComponent();
2544 if (component == null) {
2545 component = r.intent.resolveActivity(
2546 mInitialApplication.getPackageManager());
2547 r.intent.setComponent(component);
2548 }
2549
2550 if (r.activityInfo.targetActivity != null) {
2551 component = new ComponentName(r.activityInfo.packageName,
2552 r.activityInfo.targetActivity);
2553 }
2554
2555 Activity activity = null;
2556 try {
2557 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2558 activity = mInstrumentation.newActivity(
2559 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002560 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002562 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 if (r.state != null) {
2564 r.state.setClassLoader(cl);
2565 }
2566 } catch (Exception e) {
2567 if (!mInstrumentation.onException(activity, e)) {
2568 throw new RuntimeException(
2569 "Unable to instantiate activity " + component
2570 + ": " + e.toString(), e);
2571 }
2572 }
2573
2574 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002575 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002576
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002577 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2578 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 TAG, r + ": app=" + app
2580 + ", appName=" + app.getPackageName()
2581 + ", pkg=" + r.packageInfo.getPackageName()
2582 + ", comp=" + r.intent.getComponent().toShortString()
2583 + ", dir=" + r.packageInfo.getAppDir());
2584
2585 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002586 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002588 Configuration config = new Configuration(mCompatConfiguration);
Andrii Kuliand0ad9382016-04-18 20:54:20 -07002589 if (r.overrideConfig != null) {
2590 config.updateFrom(r.overrideConfig);
2591 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002592 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002593 + r.activityInfo.name + " with config " + config);
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002594 Window window = null;
2595 if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {
2596 window = r.mPendingRemoveWindow;
2597 r.mPendingRemoveWindow = null;
2598 r.mPendingRemoveWindowManager = null;
2599 }
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002600 activity.attach(appContext, this, getInstrumentation(), r.token,
2601 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002602 r.embeddedID, r.lastNonConfigurationInstances, config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07002603 r.referrer, r.voiceInteractor, window);
Bob Leee5408332009-09-04 18:31:17 -07002604
Christopher Tateb70f3df2009-04-07 16:07:59 -07002605 if (customIntent != null) {
2606 activity.mIntent = customIntent;
2607 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002608 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 activity.mStartedActivity = false;
2610 int theme = r.activityInfo.getThemeResource();
2611 if (theme != 0) {
2612 activity.setTheme(theme);
2613 }
2614
2615 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002616 if (r.isPersistable()) {
2617 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2618 } else {
2619 mInstrumentation.callActivityOnCreate(activity, r.state);
2620 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621 if (!activity.mCalled) {
2622 throw new SuperNotCalledException(
2623 "Activity " + r.intent.getComponent().toShortString() +
2624 " did not call through to super.onCreate()");
2625 }
2626 r.activity = activity;
2627 r.stopped = true;
2628 if (!r.activity.mFinished) {
2629 activity.performStart();
2630 r.stopped = false;
2631 }
2632 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002633 if (r.isPersistable()) {
2634 if (r.state != null || r.persistentState != null) {
2635 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2636 r.persistentState);
2637 }
2638 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2640 }
2641 }
2642 if (!r.activity.mFinished) {
2643 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002644 if (r.isPersistable()) {
2645 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2646 r.persistentState);
2647 } else {
2648 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650 if (!activity.mCalled) {
2651 throw new SuperNotCalledException(
2652 "Activity " + r.intent.getComponent().toShortString() +
2653 " did not call through to super.onPostCreate()");
2654 }
2655 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
2657 r.paused = true;
2658
2659 mActivities.put(r.token, r);
2660
2661 } catch (SuperNotCalledException e) {
2662 throw e;
2663
2664 } catch (Exception e) {
2665 if (!mInstrumentation.onException(activity, e)) {
2666 throw new RuntimeException(
2667 "Unable to start activity " + component
2668 + ": " + e.toString(), e);
2669 }
2670 }
2671
2672 return activity;
2673 }
2674
Wale Ogunwale7c726682015-02-06 17:34:28 -08002675 private Context createBaseContextForActivity(ActivityClientRecord r, final Activity activity) {
2676 int displayId = Display.DEFAULT_DISPLAY;
Craig Mautnere0a38842013-12-16 16:14:02 -08002677 try {
Craig Mautneraa7e3ed2015-02-17 10:17:21 -08002678 displayId = ActivityManagerNative.getDefault().getActivityDisplayId(r.token);
Craig Mautnere0a38842013-12-16 16:14:02 -08002679 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002680 throw e.rethrowFromSystemServer();
Craig Mautnere0a38842013-12-16 16:14:02 -08002681 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002682
Wale Ogunwale7c726682015-02-06 17:34:28 -08002683 ContextImpl appContext = ContextImpl.createActivityContext(
Adam Lesinski082614c2016-03-04 14:33:47 -08002684 this, r.packageInfo, r.token, displayId, r.overrideConfig);
Wale Ogunwale7c726682015-02-06 17:34:28 -08002685 appContext.setOuterContext(activity);
2686 Context baseContext = appContext;
2687
2688 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
Jeff Brownefd43bd2012-09-21 17:02:35 -07002689 // For debugging purposes, if the activity's package name contains the value of
2690 // the "debug.use-second-display" system property as a substring, then show
2691 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002692 String pkgName = SystemProperties.get("debug.second-display.pkg");
2693 if (pkgName != null && !pkgName.isEmpty()
2694 && r.packageInfo.mPackageName.contains(pkgName)) {
Wale Ogunwale7c726682015-02-06 17:34:28 -08002695 for (int id : dm.getDisplayIds()) {
2696 if (id != Display.DEFAULT_DISPLAY) {
Wale Ogunwale26698512015-06-05 16:55:33 -07002697 Display display =
2698 dm.getCompatibleDisplay(id, appContext.getDisplayAdjustments(id));
Jeff Brownefd43bd2012-09-21 17:02:35 -07002699 baseContext = appContext.createDisplayContext(display);
2700 break;
2701 }
2702 }
2703 }
2704 return baseContext;
2705 }
2706
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002707 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 // If we are getting ready to gc after going to the background, well
2709 // we are back active so skip it.
2710 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002711 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712
Jeff Hao1b012d32014-08-20 10:35:34 -07002713 if (r.profilerInfo != null) {
2714 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002715 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002716 }
2717
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002718 // Make sure we are running with the most recent config.
2719 handleConfigurationChanged(null, null);
2720
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002721 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002723
Chet Haase0d1c27a2014-11-03 18:35:16 +00002724 // Initialize before creating the activity
2725 WindowManagerGlobal.initialize();
2726
Craig Mautner233ceee2014-05-09 17:05:11 -07002727 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728
2729 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002730 r.createdConfig = new Configuration(mConfiguration);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002731 reportSizeConfigurations(r);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002732 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002733 handleResumeActivity(r.token, false, r.isForward,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08002734 !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735
2736 if (!r.activity.mFinished && r.startsNotResumed) {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002737 // The activity manager actually wants this one to start out paused, because it
2738 // needs to be visible but isn't in the foreground. We accomplish this by going
2739 // through the normal startup (because activities expect to go through onResume()
2740 // the first time they run, before their window is displayed), and then pausing it.
2741 // However, in this case we do -not- need to do the full pause cycle (of freezing
2742 // and such) because the activity manager assumes it can just retain the current
2743 // state it has.
2744 performPauseActivityIfNeeded(r, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002746 // We need to keep around the original state, in case we need to be created again.
2747 // But we only do this for pre-Honeycomb apps, which always save their state when
2748 // pausing, so we can not have them save their state when restarting from a paused
2749 // state. For HC and later, we want to (and can) let the state be saved as the
2750 // normal part of stopping the activity.
2751 if (r.isPreHoneycomb()) {
2752 r.state = oldState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 }
2755 } else {
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07002756 // If there was an error, for any reason, tell the activity manager to stop us.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 try {
2758 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07002759 .finishActivity(r.token, Activity.RESULT_CANCELED, null,
2760 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002761 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002762 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 }
2764 }
2765 }
2766
Filip Gruszczynski23493322015-07-29 17:02:59 -07002767 private void reportSizeConfigurations(ActivityClientRecord r) {
2768 Configuration[] configurations = r.activity.getResources().getSizeConfigurations();
2769 if (configurations == null) {
2770 return;
2771 }
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002772 SparseIntArray horizontal = new SparseIntArray();
2773 SparseIntArray vertical = new SparseIntArray();
2774 SparseIntArray smallest = new SparseIntArray();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002775 for (int i = configurations.length - 1; i >= 0; i--) {
2776 Configuration config = configurations[i];
2777 if (config.screenHeightDp != Configuration.SCREEN_HEIGHT_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002778 vertical.put(config.screenHeightDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002779 }
2780 if (config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002781 horizontal.put(config.screenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002782 }
2783 if (config.smallestScreenWidthDp != Configuration.SMALLEST_SCREEN_WIDTH_DP_UNDEFINED) {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002784 smallest.put(config.smallestScreenWidthDp, 0);
Filip Gruszczynski23493322015-07-29 17:02:59 -07002785 }
2786 }
Filip Gruszczynski23493322015-07-29 17:02:59 -07002787 try {
Filip Gruszczynski20aa0ae2015-10-30 10:08:27 -07002788 ActivityManagerNative.getDefault().reportSizeConfigurations(r.token,
2789 horizontal.copyKeys(), vertical.copyKeys(), smallest.copyKeys());
Filip Gruszczynski23493322015-07-29 17:02:59 -07002790 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002791 throw ex.rethrowFromSystemServer();
Filip Gruszczynski23493322015-07-29 17:02:59 -07002792 }
2793
2794 }
2795
Dianne Hackborn85d558c2014-11-04 10:31:54 -08002796 private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 final int N = intents.size();
2798 for (int i=0; i<N; i++) {
Dianne Hackborna01a0fa2014-12-02 10:33:14 -08002799 ReferrerIntent intent = intents.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002801 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002802 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2804 }
2805 }
2806
Wale Ogunwale826c7062016-09-13 08:25:54 -07002807 void performNewIntents(IBinder token, List<ReferrerIntent> intents, boolean andPause) {
2808 final ActivityClientRecord r = mActivities.get(token);
2809 if (r == null) {
2810 return;
2811 }
2812
2813 final boolean resumed = !r.paused;
2814 if (resumed) {
2815 r.activity.mTemporaryPause = true;
2816 mInstrumentation.callActivityOnPause(r.activity);
2817 }
2818 deliverNewIntents(r, intents);
2819 if (resumed) {
2820 r.activity.performResume();
2821 r.activity.mTemporaryPause = false;
2822 }
2823
2824 if (r.paused && andPause) {
2825 // In this case the activity was in the paused state when we delivered the intent,
2826 // to guarantee onResume gets called after onNewIntent we temporarily resume the
2827 // activity and pause again as the caller wanted.
2828 performResumeActivity(token, false, "performNewIntents");
2829 performPauseActivityIfNeeded(r, "performNewIntents");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 }
2831 }
Bob Leee5408332009-09-04 18:31:17 -07002832
Romain Guy65b345f2011-07-27 18:51:50 -07002833 private void handleNewIntent(NewIntentData data) {
Wale Ogunwale826c7062016-09-13 08:25:54 -07002834 performNewIntents(data.token, data.intents, data.andPause);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002835 }
2836
Adam Skorydfc7fd72013-08-05 19:23:41 -07002837 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Amith Yamasani4f128e42016-05-10 11:44:12 -07002838 if (mLastSessionId != cmd.sessionId) {
2839 // Clear the existing structures
2840 mLastSessionId = cmd.sessionId;
2841 for (int i = mLastAssistStructures.size() - 1; i >= 0; i--) {
2842 AssistStructure structure = mLastAssistStructures.get(i).get();
2843 if (structure != null) {
2844 structure.clearSendChannel();
2845 }
2846 mLastAssistStructures.remove(i);
Dianne Hackborn782d4982015-07-08 17:36:37 -07002847 }
2848 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002849 Bundle data = new Bundle();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002850 AssistStructure structure = null;
2851 AssistContent content = new AssistContent();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002852 ActivityClientRecord r = mActivities.get(cmd.activityToken);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002853 Uri referrer = null;
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002854 if (r != null) {
2855 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2856 r.activity.onProvideAssistData(data);
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002857 referrer = r.activity.onProvideReferrer();
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002858 if (cmd.requestType == ActivityManager.ASSIST_CONTEXT_FULL) {
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002859 structure = new AssistStructure(r.activity);
Adam Skory4aaed142015-04-22 11:29:31 -06002860 Intent activityIntent = r.activity.getIntent();
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002861 if (activityIntent != null && (r.window == null ||
2862 (r.window.getAttributes().flags
2863 & WindowManager.LayoutParams.FLAG_SECURE) == 0)) {
Adam Skory4aaed142015-04-22 11:29:31 -06002864 Intent intent = new Intent(activityIntent);
2865 intent.setFlags(intent.getFlags() & ~(Intent.FLAG_GRANT_WRITE_URI_PERMISSION
2866 | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION));
2867 intent.removeUnsafeExtras();
Adam Skorycd70c642015-06-05 11:41:55 -06002868 content.setDefaultIntent(intent);
Adam Skory4aaed142015-04-22 11:29:31 -06002869 } else {
Adam Skorycd70c642015-06-05 11:41:55 -06002870 content.setDefaultIntent(new Intent());
Adam Skory4aaed142015-04-22 11:29:31 -06002871 }
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002872 r.activity.onProvideAssistContent(content);
Dianne Hackborna83ce1d2015-03-11 15:16:13 -07002873 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002874 }
Dianne Hackborn09d57fe2015-05-27 18:05:52 -07002875 if (structure == null) {
2876 structure = new AssistStructure();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002877 }
Amith Yamasani4f128e42016-05-10 11:44:12 -07002878 mLastAssistStructures.add(new WeakReference<>(structure));
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002879 IActivityManager mgr = ActivityManagerNative.getDefault();
2880 try {
Dianne Hackborna3acdb32015-06-08 17:07:40 -07002881 mgr.reportAssistContextExtras(cmd.requestToken, data, structure, content, referrer);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002882 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002883 throw e.rethrowFromSystemServer();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002884 }
2885 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002886
2887 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2888 ActivityClientRecord r = mActivities.get(token);
2889 if (r != null) {
2890 r.activity.onTranslucentConversionComplete(drawComplete);
2891 }
2892 }
2893
Craig Mautnereb8abf72014-07-02 15:04:09 -07002894 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2895 ActivityClientRecord r = mActivities.get(token);
2896 if (r != null) {
2897 r.activity.onNewActivityOptions(options);
2898 }
2899 }
2900
Jose Lima4b6c6692014-08-12 17:41:12 -07002901 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002902 ActivityClientRecord r = mActivities.get(token);
2903 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002904 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002905 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002906 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002907 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002908 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002909 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2910 if (!activity.mCalled) {
2911 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002912 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002913 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002914 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002915 }
2916 }
2917 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002918 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002919 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07002920 throw e.rethrowFromSystemServer();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002921 }
2922 }
2923
Jose Lima4b6c6692014-08-12 17:41:12 -07002924 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002925 ActivityClientRecord r = mActivities.get(token);
2926 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002927 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002928 }
2929 }
2930
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002931 public void handleInstallProvider(ProviderInfo info) {
Jeff Sharkeybb2e2ca2014-10-23 11:42:31 -07002932 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2933 try {
2934 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2935 } finally {
2936 StrictMode.setThreadPolicy(oldPolicy);
2937 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002938 }
2939
Craig Mautner8746a472014-07-24 15:12:54 -07002940 private void handleEnterAnimationComplete(IBinder token) {
2941 ActivityClientRecord r = mActivities.get(token);
2942 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002943 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002944 }
2945 }
2946
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04002947 private void handleStartBinderTracking() {
2948 Binder.enableTracing();
2949 }
2950
2951 private void handleStopBinderTrackingAndDump(ParcelFileDescriptor fd) {
2952 try {
2953 Binder.disableTracing();
2954 Binder.getTransactionTracker().writeTracesToFile(fd);
2955 } finally {
2956 IoUtils.closeQuietly(fd);
2957 Binder.getTransactionTracker().clearTraces();
2958 }
2959 }
2960
Andrii Kulian933076d2016-03-29 17:04:42 -07002961 private void handleMultiWindowModeChanged(IBinder token, boolean isInMultiWindowMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002962 final ActivityClientRecord r = mActivities.get(token);
2963 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002964 r.activity.dispatchMultiWindowModeChanged(isInMultiWindowMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002965 }
2966 }
2967
Andrii Kulian933076d2016-03-29 17:04:42 -07002968 private void handlePictureInPictureModeChanged(IBinder token, boolean isInPipMode) {
Wale Ogunwale5f986092015-12-04 15:35:38 -08002969 final ActivityClientRecord r = mActivities.get(token);
2970 if (r != null) {
Adam Powell858cf032016-05-09 15:45:37 -07002971 r.activity.dispatchPictureInPictureModeChanged(isInPipMode);
Wale Ogunwale5f986092015-12-04 15:35:38 -08002972 }
2973 }
2974
Amith Yamasani0af6fa72016-01-17 15:36:19 -08002975 private void handleLocalVoiceInteractionStarted(IBinder token, IVoiceInteractor interactor) {
2976 final ActivityClientRecord r = mActivities.get(token);
2977 if (r != null) {
2978 r.voiceInteractor = interactor;
2979 r.activity.setVoiceInteractor(interactor);
2980 if (interactor == null) {
2981 r.activity.onLocalVoiceInteractionStopped();
2982 } else {
2983 r.activity.onLocalVoiceInteractionStarted();
2984 }
2985 }
2986 }
2987
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002988 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2989
2990 /**
2991 * Return the Intent that's currently being handled by a
2992 * BroadcastReceiver on this thread, or null if none.
2993 * @hide
2994 */
2995 public static Intent getIntentBeingBroadcast() {
2996 return sCurrentBroadcastIntent.get();
2997 }
2998
Romain Guy65b345f2011-07-27 18:51:50 -07002999 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003000 // If we are getting ready to gc after going to the background, well
3001 // we are back active so skip it.
3002 unscheduleGcIdler();
3003
3004 String component = data.intent.getComponent().getClassName();
3005
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003006 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003007 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003008
3009 IActivityManager mgr = ActivityManagerNative.getDefault();
3010
Romain Guy65b345f2011-07-27 18:51:50 -07003011 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 try {
3013 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3014 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003015 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07003016 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
3018 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003019 if (DEBUG_BROADCAST) Slog.i(TAG,
3020 "Finishing failed broadcast to " + data.intent.getComponent());
3021 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003022 throw new RuntimeException(
3023 "Unable to instantiate receiver " + component
3024 + ": " + e.toString(), e);
3025 }
3026
3027 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003028 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07003029
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003030 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 TAG, "Performing receive of " + data.intent
3032 + ": app=" + app
3033 + ", appName=" + app.getPackageName()
3034 + ", pkg=" + packageInfo.getPackageName()
3035 + ", comp=" + data.intent.getComponent().toShortString()
3036 + ", dir=" + packageInfo.getAppDir());
3037
Dianne Hackborn21556372010-02-04 16:34:40 -08003038 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003039 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07003040 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 receiver.onReceive(context.getReceiverRestrictedContext(),
3042 data.intent);
3043 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07003044 if (DEBUG_BROADCAST) Slog.i(TAG,
3045 "Finishing failed broadcast to " + data.intent.getComponent());
3046 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 if (!mInstrumentation.onException(receiver, e)) {
3048 throw new RuntimeException(
3049 "Unable to start receiver " + component
3050 + ": " + e.toString(), e);
3051 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07003052 } finally {
3053 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 }
3055
Dianne Hackborne829fef2010-10-26 17:44:01 -07003056 if (receiver.getPendingResult() != null) {
3057 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 }
3059 }
3060
Christopher Tate181fafa2009-05-14 11:12:14 -07003061 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07003062 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003063 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003064
Christopher Tate346acb12012-10-15 19:20:25 -07003065 // Sanity check the requested target package's uid against ours
3066 try {
3067 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
3068 data.appInfo.packageName, 0, UserHandle.myUserId());
3069 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
3070 Slog.w(TAG, "Asked to instantiate non-matching package "
3071 + data.appInfo.packageName);
3072 return;
3073 }
3074 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003075 throw e.rethrowFromSystemServer();
Christopher Tate346acb12012-10-15 19:20:25 -07003076 }
3077
Christopher Tate181fafa2009-05-14 11:12:14 -07003078 // no longer idle; we have backup work to do
3079 unscheduleGcIdler();
3080
3081 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003082 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003083 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07003084 if (packageName == null) {
3085 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
3086 return;
3087 }
3088
Christopher Tate181fafa2009-05-14 11:12:14 -07003089 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07003090 // full backup operation but no app-supplied agent? use the default implementation
3091 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
3092 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07003093 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07003094 }
Christopher Tate4a627c72011-04-01 14:43:32 -07003095
Christopher Tate181fafa2009-05-14 11:12:14 -07003096 try {
Christopher Tated1475e02009-07-09 15:36:17 -07003097 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07003098 BackupAgent agent = mBackupAgents.get(packageName);
3099 if (agent != null) {
3100 // reusing the existing instance
3101 if (DEBUG_BACKUP) {
3102 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07003103 }
Christopher Tate2e40d112014-07-15 12:37:38 -07003104 binder = agent.onBind();
3105 } else {
3106 try {
3107 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
3108
3109 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3110 agent = (BackupAgent) cl.loadClass(classname).newInstance();
3111
3112 // set up the agent's context
3113 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3114 context.setOuterContext(agent);
3115 agent.attach(context);
3116
3117 agent.onCreate();
3118 binder = agent.onBind();
3119 mBackupAgents.put(packageName, agent);
3120 } catch (Exception e) {
3121 // If this is during restore, fail silently; otherwise go
3122 // ahead and let the user see the crash.
3123 Slog.e(TAG, "Agent threw during creation: " + e);
3124 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
3125 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
3126 throw e;
3127 }
3128 // falling through with 'binder' still null
3129 }
Christopher Tated1475e02009-07-09 15:36:17 -07003130 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003131
3132 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07003133 try {
3134 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
3135 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003136 throw e.rethrowFromSystemServer();
Christopher Tate181fafa2009-05-14 11:12:14 -07003137 }
Christopher Tate181fafa2009-05-14 11:12:14 -07003138 } catch (Exception e) {
3139 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07003140 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07003141 }
3142 }
3143
3144 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07003145 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003146 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07003147
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003148 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003149 String packageName = packageInfo.mPackageName;
3150 BackupAgent agent = mBackupAgents.get(packageName);
3151 if (agent != null) {
3152 try {
3153 agent.onDestroy();
3154 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003155 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07003156 e.printStackTrace();
3157 }
3158 mBackupAgents.remove(packageName);
3159 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003160 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07003161 }
3162 }
3163
Romain Guy65b345f2011-07-27 18:51:50 -07003164 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003165 // If we are getting ready to gc after going to the background, well
3166 // we are back active so skip it.
3167 unscheduleGcIdler();
3168
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003169 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003170 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003171 Service service = null;
3172 try {
3173 java.lang.ClassLoader cl = packageInfo.getClassLoader();
3174 service = (Service) cl.loadClass(data.info.name).newInstance();
3175 } catch (Exception e) {
3176 if (!mInstrumentation.onException(service, e)) {
3177 throw new RuntimeException(
3178 "Unable to instantiate service " + data.info.name
3179 + ": " + e.toString(), e);
3180 }
3181 }
3182
3183 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003184 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003185
Jeff Browndefd4a62014-03-10 21:24:37 -07003186 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
3187 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188
Dianne Hackborn0be1f782009-11-09 12:30:12 -08003189 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003190 service.attach(context, this, data.info.name, data.token, app,
3191 ActivityManagerNative.getDefault());
3192 service.onCreate();
3193 mServices.put(data.token, service);
3194 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003195 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003196 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003198 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 }
3200 } catch (Exception e) {
3201 if (!mInstrumentation.onException(service, e)) {
3202 throw new RuntimeException(
3203 "Unable to create service " + data.info.name
3204 + ": " + e.toString(), e);
3205 }
3206 }
3207 }
3208
Romain Guy65b345f2011-07-27 18:51:50 -07003209 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07003211 if (DEBUG_SERVICE)
3212 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003213 if (s != null) {
3214 try {
3215 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003216 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 try {
3218 if (!data.rebind) {
3219 IBinder binder = s.onBind(data.intent);
3220 ActivityManagerNative.getDefault().publishService(
3221 data.token, data.intent, binder);
3222 } else {
3223 s.onRebind(data.intent);
3224 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003225 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003226 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003227 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003229 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003230 }
3231 } catch (Exception e) {
3232 if (!mInstrumentation.onException(s, e)) {
3233 throw new RuntimeException(
3234 "Unable to bind to service " + s
3235 + " with " + data.intent + ": " + e.toString(), e);
3236 }
3237 }
3238 }
3239 }
3240
Romain Guy65b345f2011-07-27 18:51:50 -07003241 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003242 Service s = mServices.get(data.token);
3243 if (s != null) {
3244 try {
3245 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003246 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003247 boolean doRebind = s.onUnbind(data.intent);
3248 try {
3249 if (doRebind) {
3250 ActivityManagerNative.getDefault().unbindFinished(
3251 data.token, data.intent, doRebind);
3252 } else {
3253 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003254 data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 }
3256 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003257 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 }
3259 } catch (Exception e) {
3260 if (!mInstrumentation.onException(s, e)) {
3261 throw new RuntimeException(
3262 "Unable to unbind to service " + s
3263 + " with " + data.intent + ": " + e.toString(), e);
3264 }
3265 }
3266 }
3267 }
3268
Dianne Hackborn625ac272010-09-17 18:29:22 -07003269 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003270 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3271 try {
3272 Service s = mServices.get(info.token);
3273 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003274 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3275 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003276 s.dump(info.fd.getFileDescriptor(), pw, info.args);
3277 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003279 } finally {
3280 IoUtils.closeQuietly(info.fd);
3281 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 }
3283 }
3284
Dianne Hackborn625ac272010-09-17 18:29:22 -07003285 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003286 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3287 try {
3288 ActivityClientRecord r = mActivities.get(info.token);
3289 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003290 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3291 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003292 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
3293 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07003294 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003295 } finally {
3296 IoUtils.closeQuietly(info.fd);
3297 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07003298 }
3299 }
3300
Marco Nelissen18cb2872011-11-15 11:19:53 -08003301 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08003302 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
3303 try {
3304 ProviderClientRecord r = mLocalProviders.get(info.token);
3305 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07003306 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
3307 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08003308 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
3309 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08003310 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08003311 } finally {
3312 IoUtils.closeQuietly(info.fd);
3313 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08003314 }
3315 }
3316
Romain Guy65b345f2011-07-27 18:51:50 -07003317 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 Service s = mServices.get(data.token);
3319 if (s != null) {
3320 try {
3321 if (data.args != null) {
3322 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003323 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07003325 int res;
3326 if (!data.taskRemoved) {
3327 res = s.onStartCommand(data.args, data.flags, data.startId);
3328 } else {
3329 s.onTaskRemoved(data.args);
3330 res = Service.START_TASK_REMOVED_COMPLETE;
3331 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003332
3333 QueuedWork.waitToFinish();
3334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003336 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003337 data.token, SERVICE_DONE_EXECUTING_START, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003339 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08003341 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003342 } catch (Exception e) {
3343 if (!mInstrumentation.onException(s, e)) {
3344 throw new RuntimeException(
3345 "Unable to start service " + s
3346 + " with " + data.args + ": " + e.toString(), e);
3347 }
3348 }
3349 }
3350 }
3351
Romain Guy65b345f2011-07-27 18:51:50 -07003352 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 Service s = mServices.remove(token);
3354 if (s != null) {
3355 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003356 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003357 s.onDestroy();
3358 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003359 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08003361 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07003363
3364 QueuedWork.waitToFinish();
3365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003366 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07003367 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackborn455625e2015-01-21 09:55:13 -08003368 token, SERVICE_DONE_EXECUTING_STOP, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003369 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003370 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
3372 } catch (Exception e) {
3373 if (!mInstrumentation.onException(s, e)) {
3374 throw new RuntimeException(
3375 "Unable to stop service " + s
3376 + ": " + e.toString(), e);
3377 }
Craig Mautner66c4a822015-01-16 12:48:16 -08003378 Slog.i(TAG, "handleStopService: exception for " + token, e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 }
Craig Mautner9776ad42015-01-15 14:38:39 -08003380 } else {
3381 Slog.i(TAG, "handleStopService: token=" + token + " not found.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003383 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
3385
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003386 public final ActivityClientRecord performResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003387 boolean clearHide, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003388 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003389 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003390 + " finished=" + r.activity.mFinished);
3391 if (r != null && !r.activity.mFinished) {
3392 if (clearHide) {
3393 r.hideForNow = false;
3394 r.activity.mStartedActivity = false;
3395 }
3396 try {
Dianne Hackborn6bdd3a12015-08-05 15:01:28 -07003397 r.activity.onStateNotSaved();
Dianne Hackborn689586d2012-10-01 18:23:04 -07003398 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003399 if (r.pendingIntents != null) {
3400 deliverNewIntents(r, r.pendingIntents);
3401 r.pendingIntents = null;
3402 }
3403 if (r.pendingResults != null) {
3404 deliverResults(r, r.pendingResults);
3405 r.pendingResults = null;
3406 }
3407 r.activity.performResume();
3408
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003409 // If there is a pending local relaunch that was requested when the activity was
3410 // paused, it will put the activity into paused state when it finally happens.
3411 // Since the activity resumed before being relaunched, we don't want that to happen,
3412 // so we need to clear the request to relaunch paused.
3413 for (int i = mRelaunchingActivities.size() - 1; i >= 0; i--) {
3414 final ActivityClientRecord relaunching = mRelaunchingActivities.get(i);
3415 if (relaunching.token == r.token
3416 && relaunching.onlyLocalRequest && relaunching.startsNotResumed) {
3417 relaunching.startsNotResumed = false;
3418 }
3419 }
3420
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003421 EventLog.writeEvent(LOG_AM_ON_RESUME_CALLED, UserHandle.myUserId(),
3422 r.activity.getComponentName().getClassName(), reason);
Bob Leee5408332009-09-04 18:31:17 -07003423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003424 r.paused = false;
3425 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003426 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07003427 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 } catch (Exception e) {
3429 if (!mInstrumentation.onException(r.activity, e)) {
3430 throw new RuntimeException(
3431 "Unable to resume activity "
3432 + r.intent.getComponent().toShortString()
3433 + ": " + e.toString(), e);
3434 }
3435 }
3436 }
3437 return r;
3438 }
3439
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003440 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r, boolean force) {
3441 if (r.mPreserveWindow && !force) {
3442 return;
3443 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003444 if (r.mPendingRemoveWindow != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003445 r.mPendingRemoveWindowManager.removeViewImmediate(
3446 r.mPendingRemoveWindow.getDecorView());
3447 IBinder wtoken = r.mPendingRemoveWindow.getDecorView().getWindowToken();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003448 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003449 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003450 r.activity.getClass().getName(), "Activity");
3451 }
3452 }
3453 r.mPendingRemoveWindow = null;
3454 r.mPendingRemoveWindowManager = null;
3455 }
3456
Craig Mautner233ceee2014-05-09 17:05:11 -07003457 final void handleResumeActivity(IBinder token,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003458 boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003459 ActivityClientRecord r = mActivities.get(token);
3460 if (!checkAndUpdateLifecycleSeq(seq, r, "resumeActivity")) {
3461 return;
3462 }
3463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 // If we are getting ready to gc after going to the background, well
3465 // we are back active so skip it.
3466 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003467 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468
Adam Powellcfbe9be2013-11-06 14:58:58 -08003469 // TODO Push resumeArgs into the activity for consideration
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003470 r = performResumeActivity(token, clearHide, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471
3472 if (r != null) {
3473 final Activity a = r.activity;
3474
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003475 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003476 TAG, "Resume " + r + " started activity: " +
3477 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3478 + ", finished: " + a.mFinished);
3479
3480 final int forwardBit = isForward ?
3481 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003483 // If the window hasn't yet been added to the window manager,
3484 // and this guy didn't finish itself or start another activity,
3485 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003486 boolean willBeVisible = !a.mStartedActivity;
3487 if (!willBeVisible) {
3488 try {
3489 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3490 a.getActivityToken());
3491 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003492 throw e.rethrowFromSystemServer();
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003493 }
3494 }
3495 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 r.window = r.activity.getWindow();
3497 View decor = r.window.getDecorView();
3498 decor.setVisibility(View.INVISIBLE);
3499 ViewManager wm = a.getWindowManager();
3500 WindowManager.LayoutParams l = r.window.getAttributes();
3501 a.mDecor = decor;
3502 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3503 l.softInputMode |= forwardBit;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003504 if (r.mPreserveWindow) {
3505 a.mWindowAdded = true;
3506 r.mPreserveWindow = false;
Robert Carrb2594852016-04-25 16:21:13 -07003507 // Normally the ViewRoot sets up callbacks with the Activity
3508 // in addView->ViewRootImpl#setView. If we are instead reusing
3509 // the decor view we have to notify the view root that the
3510 // callbacks may have changed.
3511 ViewRootImpl impl = decor.getViewRootImpl();
Robert Carr311ecba2016-04-27 12:59:45 -07003512 if (impl != null) {
3513 impl.notifyChildRebuilt();
3514 }
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003515 }
3516 if (a.mVisibleFromClient && !a.mWindowAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003517 a.mWindowAdded = true;
3518 wm.addView(decor, l);
3519 }
3520
3521 // If the window has already been added, but during resume
3522 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003523 // window visible.
3524 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003525 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003526 TAG, "Launch " + r + " mStartedActivity set");
3527 r.hideForNow = true;
3528 }
3529
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003530 // Get rid of anything left hanging around.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07003531 cleanUpPendingRemoveWindows(r, false /* force */);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003533 // The window is now visible if it has been added, we are not
3534 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003535 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003536 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003538 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003539 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Adam Lesinski082614c2016-03-04 14:33:47 -08003540 + r.activityInfo.name + " with newConfig " + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003541 r.newConfig = null;
3542 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003543 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003544 + isForward);
3545 WindowManager.LayoutParams l = r.window.getAttributes();
3546 if ((l.softInputMode
3547 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3548 != forwardBit) {
3549 l.softInputMode = (l.softInputMode
3550 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3551 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003552 if (r.activity.mVisibleFromClient) {
3553 ViewManager wm = a.getWindowManager();
3554 View decor = r.window.getDecorView();
3555 wm.updateViewLayout(decor, l);
3556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 }
3558 r.activity.mVisibleFromServer = true;
3559 mNumVisibleActivities++;
3560 if (r.activity.mVisibleFromClient) {
3561 r.activity.makeVisible();
3562 }
3563 }
3564
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003565 if (!r.onlyLocalRequest) {
3566 r.nextIdle = mNewActivities;
3567 mNewActivities = r;
3568 if (localLOGV) Slog.v(
3569 TAG, "Scheduling idle handler for " + r);
3570 Looper.myQueue().addIdleHandler(new Idler());
3571 }
3572 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003574 // Tell the activity manager we have resumed.
3575 if (reallyResume) {
3576 try {
3577 ActivityManagerNative.getDefault().activityResumed(token);
3578 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003579 throw ex.rethrowFromSystemServer();
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003580 }
3581 }
3582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 } else {
3584 // If an exception was thrown when trying to resume, then
3585 // just end this activity.
3586 try {
3587 ActivityManagerNative.getDefault()
Wale Ogunwaleba7881c2015-08-01 19:28:29 -07003588 .finishActivity(token, Activity.RESULT_CANCELED, null,
3589 Activity.DONT_FINISH_TASK_WITH_ACTIVITY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003591 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003592 }
3593 }
3594 }
3595
3596 private int mThumbnailWidth = -1;
3597 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003598 private Bitmap mAvailThumbnailBitmap = null;
3599 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600
Romain Guy65b345f2011-07-27 18:51:50 -07003601 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003602 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003603 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003604 if (thumbnail == null) {
3605 int w = mThumbnailWidth;
3606 int h;
3607 if (w < 0) {
3608 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003609 int wId = com.android.internal.R.dimen.thumbnail_width;
3610 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003611 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3612 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003613 } else {
3614 h = mThumbnailHeight;
3615 }
3616
3617 // On platforms where we don't want thumbnails, set dims to (0,0)
3618 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003619 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3620 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003621 thumbnail.eraseColor(0);
3622 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 }
3624
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003625 if (thumbnail != null) {
3626 Canvas cv = mThumbnailCanvas;
3627 if (cv == null) {
3628 mThumbnailCanvas = cv = new Canvas();
3629 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003630
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003631 cv.setBitmap(thumbnail);
3632 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3633 mAvailThumbnailBitmap = thumbnail;
3634 thumbnail = null;
3635 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003636 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003639 } catch (Exception e) {
3640 if (!mInstrumentation.onException(r.activity, e)) {
3641 throw new RuntimeException(
3642 "Unable to create thumbnail of "
3643 + r.intent.getComponent().toShortString()
3644 + ": " + e.toString(), e);
3645 }
3646 thumbnail = null;
3647 }
3648
3649 return thumbnail;
3650 }
3651
Romain Guy65b345f2011-07-27 18:51:50 -07003652 private void handlePauseActivity(IBinder token, boolean finished,
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003653 boolean userLeaving, int configChanges, boolean dontReport, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003654 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08003655 if (DEBUG_ORDER) Slog.d(TAG, "handlePauseActivity " + r + ", seq: " + seq);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003656 if (!checkAndUpdateLifecycleSeq(seq, r, "pauseActivity")) {
3657 return;
3658 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003660 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 if (userLeaving) {
3662 performUserLeavingActivity(r);
3663 }
Bob Leee5408332009-09-04 18:31:17 -07003664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 r.activity.mConfigChangeFlags |= configChanges;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003666 performPauseActivity(token, finished, r.isPreHoneycomb(), "handlePauseActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003668 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003669 if (r.isPreHoneycomb()) {
3670 QueuedWork.waitToFinish();
3671 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003674 if (!dontReport) {
3675 try {
3676 ActivityManagerNative.getDefault().activityPaused(token);
3677 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003678 throw ex.rethrowFromSystemServer();
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003681 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 }
3683 }
3684
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003685 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 mInstrumentation.callActivityOnUserLeaving(r.activity);
3687 }
3688
3689 final Bundle performPauseActivity(IBinder token, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003690 boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003691 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003692 return r != null ? performPauseActivity(r, finished, saveState, reason) : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 }
3694
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003695 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003696 boolean saveState, String reason) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 if (r.paused) {
3698 if (r.activity.mFinished) {
3699 // If we are finishing, we won't call onResume() in certain cases.
3700 // So here we likewise don't want to call onPause() if the activity
3701 // isn't resumed.
3702 return null;
3703 }
3704 RuntimeException e = new RuntimeException(
3705 "Performing pause of activity that is not resumed: "
3706 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003707 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 if (finished) {
3710 r.activity.mFinished = true;
3711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003713 // Next have the activity save its current state and managed dialogs...
3714 if (!r.activity.mFinished && saveState) {
3715 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003717
3718 performPauseActivityIfNeeded(r, reason);
Jeff Hamilton52d32032011-01-08 15:31:26 -06003719
3720 // Notify any outstanding on paused listeners
3721 ArrayList<OnActivityPausedListener> listeners;
3722 synchronized (mOnPauseListeners) {
3723 listeners = mOnPauseListeners.remove(r.activity);
3724 }
3725 int size = (listeners != null ? listeners.size() : 0);
3726 for (int i = 0; i < size; i++) {
3727 listeners.get(i).onPaused(r.activity);
3728 }
3729
Craig Mautnera0026042014-04-23 11:45:37 -07003730 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 }
3732
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003733 private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
3734 if (r.paused) {
3735 // You are already paused silly...
3736 return;
3737 }
3738
3739 try {
3740 r.activity.mCalled = false;
3741 mInstrumentation.callActivityOnPause(r.activity);
3742 EventLog.writeEvent(LOG_AM_ON_PAUSE_CALLED, UserHandle.myUserId(),
3743 r.activity.getComponentName().getClassName(), reason);
3744 if (!r.activity.mCalled) {
3745 throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
3746 + " did not call through to super.onPause()");
3747 }
3748 } catch (SuperNotCalledException e) {
3749 throw e;
3750 } catch (Exception e) {
3751 if (!mInstrumentation.onException(r.activity, e)) {
3752 throw new RuntimeException("Unable to pause activity "
3753 + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
3754 }
3755 }
3756 r.paused = true;
3757 }
3758
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003759 final void performStopActivity(IBinder token, boolean saveState, String reason) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003760 ActivityClientRecord r = mActivities.get(token);
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003761 performStopActivityInner(r, null, false, saveState, reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003762 }
3763
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003764 private static class StopInfo implements Runnable {
3765 ActivityClientRecord activity;
3766 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003767 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003769
3770 @Override public void run() {
3771 // Tell activity manager we have been stopped.
3772 try {
3773 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3774 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003775 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003776 } catch (RemoteException ex) {
Jeff Sharkey369f5092016-02-29 11:16:21 -07003777 if (ex instanceof TransactionTooLargeException
Jeff Sharkey9ad86132016-03-01 11:22:59 -07003778 && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -07003779 Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
Jeff Sharkey369f5092016-02-29 11:16:21 -07003780 return;
3781 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -07003782 throw ex.rethrowFromSystemServer();
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003783 }
3784 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003785 }
3786
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003787 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003788 public final IActivityManager.ContentProviderHolder holder;
3789 public final ProviderClientRecord client;
3790 public int stableCount;
3791 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003792
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003793 // When this is set, the stable and unstable ref counts are 0 and
3794 // we have a pending operation scheduled to remove the ref count
3795 // from the activity manager. On the activity manager we are still
3796 // holding an unstable ref, though it is not reflected in the counts
3797 // here.
3798 public boolean removePending;
3799
3800 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3801 ProviderClientRecord inClient, int sCount, int uCount) {
3802 holder = inHolder;
3803 client = inClient;
3804 stableCount = sCount;
3805 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807 }
3808
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003809 /**
3810 * Core implementation of stopping an activity. Note this is a little
3811 * tricky because the server's meaning of stop is slightly different
3812 * than our client -- for the server, stop means to save state and give
3813 * it the result when it is done, but the window may still be visible.
3814 * For the client, we want to call onStop()/onStart() to indicate when
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07003815 * the activity's UI visibility changes.
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003816 */
Romain Guy65b345f2011-07-27 18:51:50 -07003817 private void performStopActivityInner(ActivityClientRecord r,
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003818 StopInfo info, boolean keepShown, boolean saveState, String reason) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003819 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003820 if (r != null) {
3821 if (!keepShown && r.stopped) {
3822 if (r.activity.mFinished) {
3823 // If we are finishing, we won't call onResume() in certain
3824 // cases. So here we likewise don't want to call onStop()
3825 // if the activity isn't resumed.
3826 return;
3827 }
3828 RuntimeException e = new RuntimeException(
Andrii Kulian58178f22016-03-16 13:44:56 -07003829 "Performing stop of activity that is already stopped: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003831 Slog.e(TAG, e.getMessage(), e);
Andrii Kulian58178f22016-03-16 13:44:56 -07003832 Slog.e(TAG, r.getStateString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 }
3834
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07003835 // One must first be paused before stopped...
3836 performPauseActivityIfNeeded(r, reason);
3837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 if (info != null) {
3839 try {
3840 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003841 // For now, don't create the thumbnail here; we are
3842 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003843 info.description = r.activity.onCreateDescription();
3844 } catch (Exception e) {
3845 if (!mInstrumentation.onException(r.activity, e)) {
3846 throw new RuntimeException(
3847 "Unable to save state of activity "
3848 + r.intent.getComponent().toShortString()
3849 + ": " + e.toString(), e);
3850 }
3851 }
3852 }
3853
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003854 // Next have the activity save its current state and managed dialogs...
3855 if (!r.activity.mFinished && saveState) {
3856 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003857 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003858 }
3859 }
3860
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003861 if (!keepShown) {
3862 try {
3863 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08003864 r.activity.performStop(false /*preserveWindow*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865 } catch (Exception e) {
3866 if (!mInstrumentation.onException(r.activity, e)) {
3867 throw new RuntimeException(
3868 "Unable to stop activity "
3869 + r.intent.getComponent().toShortString()
3870 + ": " + e.toString(), e);
3871 }
3872 }
3873 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003874 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
3875 r.activity.getComponentName().getClassName(), reason);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003876 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 }
3878 }
3879
Romain Guy65b345f2011-07-27 18:51:50 -07003880 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003881 View v = r.activity.mDecor;
3882 if (v != null) {
3883 if (show) {
3884 if (!r.activity.mVisibleFromServer) {
3885 r.activity.mVisibleFromServer = true;
3886 mNumVisibleActivities++;
3887 if (r.activity.mVisibleFromClient) {
3888 r.activity.makeVisible();
3889 }
3890 }
3891 if (r.newConfig != null) {
Adam Lesinski082614c2016-03-04 14:33:47 -08003892 performConfigurationChangedForActivity(r, r.newConfig, REPORT_TO_ACTIVITY);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003893 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Adam Lesinski082614c2016-03-04 14:33:47 -08003894 + r.activityInfo.name + " with new config "
3895 + r.activity.mCurrentConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 r.newConfig = null;
3897 }
3898 } else {
3899 if (r.activity.mVisibleFromServer) {
3900 r.activity.mVisibleFromServer = false;
3901 mNumVisibleActivities--;
3902 v.setVisibility(View.INVISIBLE);
3903 }
3904 }
3905 }
3906 }
3907
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003908 private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003909 ActivityClientRecord r = mActivities.get(token);
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003910 if (!checkAndUpdateLifecycleSeq(seq, r, "stopActivity")) {
3911 return;
3912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 r.activity.mConfigChangeFlags |= configChanges;
3914
3915 StopInfo info = new StopInfo();
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003916 performStopActivityInner(r, info, show, true, "handleStopActivity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003917
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003918 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003919 TAG, "Finishing stop of " + r + ": show=" + show
3920 + " win=" + r.window);
3921
3922 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003923
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003924 // Make sure any pending writes are now committed.
3925 if (!r.isPreHoneycomb()) {
3926 QueuedWork.waitToFinish();
3927 }
3928
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003929 // Schedule the call to tell the activity manager we have
3930 // stopped. We don't do this immediately, because we want to
3931 // have a chance for any other pending work (in particular memory
3932 // trim requests) to complete before you tell the activity
3933 // manager to proceed and allow us to go fully into the background.
3934 info.activity = r;
3935 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003936 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003937 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003938 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003939 }
3940
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07003941 private static boolean checkAndUpdateLifecycleSeq(int seq, ActivityClientRecord r,
3942 String action) {
3943 if (r == null) {
3944 return true;
3945 }
3946 if (seq < r.lastProcessedSeq) {
3947 if (DEBUG_ORDER) Slog.d(TAG, action + " for " + r + " ignored, because seq=" + seq
3948 + " < mCurrentLifecycleSeq=" + r.lastProcessedSeq);
3949 return false;
3950 }
3951 r.lastProcessedSeq = seq;
3952 return true;
3953 }
3954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003955 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003956 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003957 if (r.stopped) {
3958 r.activity.performRestart();
3959 r.stopped = false;
3960 }
3961 }
3962
Romain Guy65b345f2011-07-27 18:51:50 -07003963 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003964 ActivityClientRecord r = mActivities.get(token);
Tim Murraye1e6c662015-04-07 13:24:14 -07003965
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003966 if (r == null) {
3967 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3968 return;
3969 }
Tim Murraye1e6c662015-04-07 13:24:14 -07003970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003971 if (!show && !r.stopped) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08003972 performStopActivityInner(r, null, show, false, "handleWindowVisibility");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003973 } else if (show && r.stopped) {
3974 // If we are getting ready to gc after going to the background, well
3975 // we are back active so skip it.
3976 unscheduleGcIdler();
3977
3978 r.activity.performRestart();
3979 r.stopped = false;
3980 }
3981 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003982 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 TAG, "Handle window " + r + " visibility: " + show);
3984 updateVisibility(r, show);
3985 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003986 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 }
3988
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07003989 // TODO: This method should be changed to use {@link #performStopActivityInner} to perform to
3990 // stop operation on the activity to reduce code duplication and the chance of fixing a bug in
3991 // one place and missing the other.
Romain Guy65b345f2011-07-27 18:51:50 -07003992 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003993 ActivityClientRecord r = mActivities.get(token);
3994
3995 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003996 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003997 return;
3998 }
3999
4000 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08004001 if (!r.stopped && !r.isPreHoneycomb()) {
Wale Ogunwaleb5066fe2016-09-21 07:33:43 -07004002 if (!r.activity.mFinished && r.state == null) {
4003 callCallActivityOnSaveInstanceState(r);
4004 }
4005
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004006 try {
4007 // Now we are idle.
Chong Zhang7687f252016-02-26 12:03:33 -08004008 r.activity.performStop(false /*preserveWindow*/);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004009 } catch (Exception e) {
4010 if (!mInstrumentation.onException(r.activity, e)) {
4011 throw new RuntimeException(
4012 "Unable to stop activity "
4013 + r.intent.getComponent().toShortString()
4014 + ": " + e.toString(), e);
4015 }
4016 }
4017 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004018 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4019 r.activity.getComponentName().getClassName(), "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004020 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08004021
4022 // Make sure any pending writes are now committed.
4023 if (!r.isPreHoneycomb()) {
4024 QueuedWork.waitToFinish();
4025 }
4026
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004027 // Tell activity manager we slept.
4028 try {
4029 ActivityManagerNative.getDefault().activitySlept(r.token);
4030 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004031 throw ex.rethrowFromSystemServer();
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08004032 }
4033 } else {
4034 if (r.stopped && r.activity.mVisibleFromServer) {
4035 r.activity.performRestart();
4036 r.stopped = false;
4037 }
4038 }
4039 }
4040
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004041 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07004042 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08004043 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004044 }
Jon Miranda836c0a82014-08-11 12:32:26 -07004045 onCoreSettingsChange();
4046 }
4047
4048 private void onCoreSettingsChange() {
4049 boolean debugViewAttributes =
4050 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4051 if (debugViewAttributes != View.mDebugViewAttributes) {
4052 View.mDebugViewAttributes = debugViewAttributes;
4053
4054 // request all activities to relaunch for the changes to take place
4055 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004056 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, null, false,
4057 false /* preserveWindow */);
Jon Miranda836c0a82014-08-11 12:32:26 -07004058 }
4059 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08004060 }
4061
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004062 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
4063 LoadedApk apk = peekPackageInfo(data.pkg, false);
4064 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004065 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004066 }
4067 apk = peekPackageInfo(data.pkg, true);
4068 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07004069 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004070 }
4071 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07004072 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004073 }
4074
Romain Guy65b345f2011-07-27 18:51:50 -07004075 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076 final int N = results.size();
4077 for (int i=0; i<N; i++) {
4078 ResultInfo ri = results.get(i);
4079 try {
4080 if (ri.mData != null) {
4081 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01004082 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004083 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004084 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07004085 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004086 r.activity.dispatchActivityResult(ri.mResultWho,
4087 ri.mRequestCode, ri.mResultCode, ri.mData);
4088 } catch (Exception e) {
4089 if (!mInstrumentation.onException(r.activity, e)) {
4090 throw new RuntimeException(
4091 "Failure delivering result " + ri + " to activity "
4092 + r.intent.getComponent().toShortString()
4093 + ": " + e.toString(), e);
4094 }
4095 }
4096 }
4097 }
4098
Romain Guy65b345f2011-07-27 18:51:50 -07004099 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004100 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004101 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004102 if (r != null) {
4103 final boolean resumed = !r.paused;
4104 if (!r.activity.mFinished && r.activity.mDecor != null
4105 && r.hideForNow && resumed) {
4106 // We had hidden the activity because it started another
4107 // one... we have gotten a result back and we are not
4108 // paused, so make sure our window is visible.
4109 updateVisibility(r, true);
4110 }
4111 if (resumed) {
4112 try {
4113 // Now we are idle.
4114 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004115 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 mInstrumentation.callActivityOnPause(r.activity);
4117 if (!r.activity.mCalled) {
4118 throw new SuperNotCalledException(
4119 "Activity " + r.intent.getComponent().toShortString()
4120 + " did not call through to super.onPause()");
4121 }
4122 } catch (SuperNotCalledException e) {
4123 throw e;
4124 } catch (Exception e) {
4125 if (!mInstrumentation.onException(r.activity, e)) {
4126 throw new RuntimeException(
4127 "Unable to pause activity "
4128 + r.intent.getComponent().toShortString()
4129 + ": " + e.toString(), e);
4130 }
4131 }
4132 }
4133 deliverResults(r, res.results);
4134 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07004135 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07004136 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004137 }
4138 }
4139 }
4140
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004141 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004142 return performDestroyActivity(token, finishing, 0, false);
4143 }
4144
Romain Guy65b345f2011-07-27 18:51:50 -07004145 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004147 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07004148 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004149 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004150 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004151 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 r.activity.mConfigChangeFlags |= configChanges;
4153 if (finishing) {
4154 r.activity.mFinished = true;
4155 }
Wale Ogunwale5dc6d652016-05-04 10:24:33 -07004156
4157 performPauseActivityIfNeeded(r, "destroy");
4158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004159 if (!r.stopped) {
4160 try {
Chong Zhang7687f252016-02-26 12:03:33 -08004161 r.activity.performStop(r.mPreserveWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004162 } catch (SuperNotCalledException e) {
4163 throw e;
4164 } catch (Exception e) {
4165 if (!mInstrumentation.onException(r.activity, e)) {
4166 throw new RuntimeException(
4167 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004168 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 + ": " + e.toString(), e);
4170 }
4171 }
4172 r.stopped = true;
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004173 EventLog.writeEvent(LOG_AM_ON_STOP_CALLED, UserHandle.myUserId(),
4174 r.activity.getComponentName().getClassName(), "destroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004175 }
4176 if (getNonConfigInstance) {
4177 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004178 r.lastNonConfigurationInstances
4179 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004180 } catch (Exception e) {
4181 if (!mInstrumentation.onException(r.activity, e)) {
4182 throw new RuntimeException(
4183 "Unable to retain activity "
4184 + r.intent.getComponent().toShortString()
4185 + ": " + e.toString(), e);
4186 }
4187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188 }
4189 try {
4190 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07004191 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004192 if (!r.activity.mCalled) {
4193 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004194 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004195 " did not call through to super.onDestroy()");
4196 }
4197 if (r.window != null) {
4198 r.window.closeAllPanels();
4199 }
4200 } catch (SuperNotCalledException e) {
4201 throw e;
4202 } catch (Exception e) {
4203 if (!mInstrumentation.onException(r.activity, e)) {
4204 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004205 "Unable to destroy activity " + safeToComponentShortString(r.intent)
4206 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 }
4208 }
4209 }
4210 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08004211 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 return r;
4213 }
4214
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07004215 private static String safeToComponentShortString(Intent intent) {
4216 ComponentName component = intent.getComponent();
4217 return component == null ? "[Unknown]" : component.toShortString();
4218 }
4219
Romain Guy65b345f2011-07-27 18:51:50 -07004220 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004221 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004222 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004223 configChanges, getNonConfigInstance);
4224 if (r != null) {
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004225 cleanUpPendingRemoveWindows(r, finishing);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004226 WindowManager wm = r.activity.getWindowManager();
4227 View v = r.activity.mDecor;
4228 if (v != null) {
4229 if (r.activity.mVisibleFromServer) {
4230 mNumVisibleActivities--;
4231 }
4232 IBinder wtoken = v.getWindowToken();
4233 if (r.activity.mWindowAdded) {
Robert Carr77bdfb52016-05-02 18:18:31 -07004234 if (r.mPreserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004235 // Hold off on removing this until the new activity's
4236 // window is being added.
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004237 r.mPendingRemoveWindow = r.window;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004238 r.mPendingRemoveWindowManager = wm;
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004239 // We can only keep the part of the view hierarchy that we control,
4240 // everything else must be removed, because it might not be able to
4241 // behave properly when activity is relaunching.
4242 r.window.clearContentView();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004243 } else {
4244 wm.removeViewImmediate(v);
4245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004247 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07004248 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004249 r.activity.getClass().getName(), "Activity");
Andrii Kulianeac0ea52016-05-11 15:50:24 -07004250 } else if (r.mPendingRemoveWindow != null) {
4251 // We're preserving only one window, others should be closed so app views
4252 // will be detached before the final tear down. It should be done now because
4253 // some components (e.g. WebView) rely on detach callbacks to perform receiver
4254 // unregister and other cleanup.
4255 WindowManagerGlobal.getInstance().closeAllExceptView(token, v,
4256 r.activity.getClass().getName(), "Activity");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 }
4258 r.activity.mDecor = null;
4259 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004260 if (r.mPendingRemoveWindow == null) {
4261 // If we are delaying the removal of the activity window, then
4262 // we can't clean up all windows here. Note that we can't do
4263 // so later either, which means any windows that aren't closed
4264 // by the app will leak. Well we try to warning them a lot
4265 // about leaking windows, because that is a bug, so if they are
4266 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07004267 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004268 r.activity.getClass().getName(), "Activity");
4269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270
4271 // Mocked out contexts won't be participating in the normal
4272 // process lifecycle, but if we're running with a proper
4273 // ApplicationContext we need to have it tear down things
4274 // cleanly.
4275 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08004276 if (c instanceof ContextImpl) {
4277 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 r.activity.getClass().getName(), "Activity");
4279 }
4280 }
4281 if (finishing) {
4282 try {
4283 ActivityManagerNative.getDefault().activityDestroyed(token);
4284 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004285 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 }
4287 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004288 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004289 }
4290
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004291 /**
4292 * @param preserveWindow Whether the activity should try to reuse the window it created,
4293 * including the decor view after the relaunch.
4294 */
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004295 public final void requestRelaunchActivity(IBinder token,
Dianne Hackborn85d558c2014-11-04 10:31:54 -08004296 List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004297 int configChanges, boolean notResumed, Configuration config,
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004298 Configuration overrideConfig, boolean fromServer, boolean preserveWindow) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004299 ActivityClientRecord target = null;
4300
Craig Mautner88c05892013-06-28 09:47:45 -07004301 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004302 for (int i=0; i<mRelaunchingActivities.size(); i++) {
4303 ActivityClientRecord r = mRelaunchingActivities.get(i);
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004304 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + this + ", trying: " + r);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004305 if (r.token == token) {
4306 target = r;
4307 if (pendingResults != null) {
4308 if (r.pendingResults != null) {
4309 r.pendingResults.addAll(pendingResults);
4310 } else {
4311 r.pendingResults = pendingResults;
4312 }
4313 }
4314 if (pendingNewIntents != null) {
4315 if (r.pendingIntents != null) {
4316 r.pendingIntents.addAll(pendingNewIntents);
4317 } else {
4318 r.pendingIntents = pendingNewIntents;
4319 }
4320 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004321
4322 // For each relaunch request, activity manager expects an answer
4323 if (!r.onlyLocalRequest && fromServer) {
4324 try {
4325 ActivityManagerNative.getDefault().activityRelaunched(token);
4326 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004327 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004328 }
4329 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004330 break;
4331 }
4332 }
4333
4334 if (target == null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004335 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: target is null, fromServer:"
4336 + fromServer);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004337 target = new ActivityClientRecord();
4338 target.token = token;
4339 target.pendingResults = pendingResults;
4340 target.pendingIntents = pendingNewIntents;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004341 target.mPreserveWindow = preserveWindow;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004342 if (!fromServer) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004343 final ActivityClientRecord existing = mActivities.get(token);
4344 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: " + existing);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004345 if (existing != null) {
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004346 if (DEBUG_ORDER) Slog.d(TAG, "requestRelaunchActivity: paused= "
4347 + existing.paused);;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004348 target.startsNotResumed = existing.paused;
Wale Ogunwale93f543c2015-02-17 16:55:03 -08004349 target.overrideConfig = existing.overrideConfig;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004350 }
4351 target.onlyLocalRequest = true;
4352 }
4353 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07004354 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004355 }
4356
4357 if (fromServer) {
4358 target.startsNotResumed = notResumed;
4359 target.onlyLocalRequest = false;
4360 }
4361 if (config != null) {
4362 target.createdConfig = config;
4363 }
Wale Ogunwale60454db2015-01-23 16:05:07 -08004364 if (overrideConfig != null) {
4365 target.overrideConfig = overrideConfig;
4366 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004367 target.pendingConfigChanges |= configChanges;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004368 target.relaunchSeq = getLifecycleSeq();
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004369 }
Filip Gruszczynskic3b61792016-01-22 16:43:48 -08004370 if (DEBUG_ORDER) Slog.d(TAG, "relaunchActivity " + ActivityThread.this + ", target "
4371 + target + " operation received seq: " + target.relaunchSeq);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004372 }
4373
Romain Guy65b345f2011-07-27 18:51:50 -07004374 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004375 // If we are getting ready to gc after going to the background, well
4376 // we are back active so skip it.
4377 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004378 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379
4380 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004381 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07004382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004383 // First: make sure we have the most recent configuration and most
4384 // recent version of the activity, or skip it if some previous call
4385 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07004386 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004387 int N = mRelaunchingActivities.size();
4388 IBinder token = tmp.token;
4389 tmp = null;
4390 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004391 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 if (r.token == token) {
4393 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004394 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004395 mRelaunchingActivities.remove(i);
4396 i--;
4397 N--;
4398 }
4399 }
Bob Leee5408332009-09-04 18:31:17 -07004400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004401 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004402 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004403 return;
4404 }
Bob Leee5408332009-09-04 18:31:17 -07004405
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004406 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
4407 + tmp.token + " with configChanges=0x"
4408 + Integer.toHexString(configChanges));
4409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004410 if (mPendingConfiguration != null) {
4411 changedConfig = mPendingConfiguration;
4412 mPendingConfiguration = null;
4413 }
4414 }
Bob Leee5408332009-09-04 18:31:17 -07004415
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004416 if (tmp.lastProcessedSeq > tmp.relaunchSeq) {
4417 Slog.wtf(TAG, "For some reason target: " + tmp + " has lower sequence: "
4418 + tmp.relaunchSeq + " than current sequence: " + tmp.lastProcessedSeq);
4419 } else {
4420 tmp.lastProcessedSeq = tmp.relaunchSeq;
4421 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004422 if (tmp.createdConfig != null) {
4423 // If the activity manager is passing us its current config,
4424 // assume that is really what we want regardless of what we
4425 // may have pending.
4426 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004427 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
4428 && mConfiguration.diff(tmp.createdConfig) != 0)) {
4429 if (changedConfig == null
4430 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
4431 changedConfig = tmp.createdConfig;
4432 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08004433 }
4434 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004435
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004436 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004437 + tmp.token + ": changedConfig=" + changedConfig);
Tim Murraye1e6c662015-04-07 13:24:14 -07004438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 // If there was a pending configuration change, execute it first.
4440 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004441 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004442 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004443 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 }
Bob Leee5408332009-09-04 18:31:17 -07004445
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004446 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004447 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 if (r == null) {
Jorim Jaggife89d122015-12-22 16:28:44 +01004449 if (!tmp.onlyLocalRequest) {
4450 try {
4451 ActivityManagerNative.getDefault().activityRelaunched(tmp.token);
4452 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004453 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004454 }
4455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 return;
4457 }
Bob Leee5408332009-09-04 18:31:17 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08004460 r.onlyLocalRequest = tmp.onlyLocalRequest;
Filip Gruszczynskia59ac9c2015-09-10 18:28:48 -07004461 r.mPreserveWindow = tmp.mPreserveWindow;
Filip Gruszczynskidb5dec22015-10-11 15:17:48 -07004462 r.lastProcessedSeq = tmp.lastProcessedSeq;
4463 r.relaunchSeq = tmp.relaunchSeq;
Christopher Tateb70f3df2009-04-07 16:07:59 -07004464 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07004465
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08004466 r.activity.mChangingConfigurations = true;
4467
Robert Carr23fa16b2016-01-13 13:19:58 -08004468 // If we are preserving the main window across relaunches we would also like to preserve
4469 // the children. However the client side view system does not support preserving
4470 // the child views so we notify the window manager to expect these windows to
4471 // be replaced and defer requests to destroy or hide them. This way we can achieve
4472 // visual continuity. It's important that we do this here prior to pause and destroy
4473 // as that is when we may hide or remove the child views.
Robert Carr77bdfb52016-05-02 18:18:31 -07004474 //
4475 // There is another scenario, if we have decided locally to relaunch the app from a
4476 // call to recreate, then none of the windows will be prepared for replacement or
4477 // preserved by the server, so we want to notify it that we are preparing to replace
4478 // everything
Robert Carr23fa16b2016-01-13 13:19:58 -08004479 try {
Robert Carr77bdfb52016-05-02 18:18:31 -07004480 if (r.mPreserveWindow || r.onlyLocalRequest) {
4481 WindowManagerGlobal.getWindowSession().prepareToReplaceWindows(
4482 r.token, !r.onlyLocalRequest);
Robert Carr23fa16b2016-01-13 13:19:58 -08004483 }
4484 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004485 throw e.rethrowFromSystemServer();
Robert Carr23fa16b2016-01-13 13:19:58 -08004486 }
4487
Dianne Hackborne2b04802010-12-09 09:24:55 -08004488 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 if (!r.paused) {
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004490 performPauseActivity(r.token, false, r.isPreHoneycomb(), "handleRelaunchActivity");
Dianne Hackborne2b04802010-12-09 09:24:55 -08004491 }
4492 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07004493 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
Bob Leee5408332009-09-04 18:31:17 -07004495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07004497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 r.activity = null;
4499 r.window = null;
4500 r.hideForNow = false;
4501 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07004502 // Merge any pending results and pending intents; don't just replace them
4503 if (tmp.pendingResults != null) {
4504 if (r.pendingResults == null) {
4505 r.pendingResults = tmp.pendingResults;
4506 } else {
4507 r.pendingResults.addAll(tmp.pendingResults);
4508 }
4509 }
4510 if (tmp.pendingIntents != null) {
4511 if (r.pendingIntents == null) {
4512 r.pendingIntents = tmp.pendingIntents;
4513 } else {
4514 r.pendingIntents.addAll(tmp.pendingIntents);
4515 }
4516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004517 r.startsNotResumed = tmp.startsNotResumed;
Wale Ogunwale60454db2015-01-23 16:05:07 -08004518 r.overrideConfig = tmp.overrideConfig;
Bob Leee5408332009-09-04 18:31:17 -07004519
Wale Ogunwalecd7043e2016-02-27 17:37:46 -08004520 handleLaunchActivity(r, currentIntent, "handleRelaunchActivity");
Jorim Jaggife89d122015-12-22 16:28:44 +01004521
4522 if (!tmp.onlyLocalRequest) {
4523 try {
4524 ActivityManagerNative.getDefault().activityRelaunched(r.token);
Jorim Jaggi4846ee32016-01-07 17:39:12 +01004525 if (r.window != null) {
4526 r.window.reportActivityRelaunched();
4527 }
Jorim Jaggife89d122015-12-22 16:28:44 +01004528 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004529 throw e.rethrowFromSystemServer();
Jorim Jaggife89d122015-12-22 16:28:44 +01004530 }
4531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004532 }
4533
Craig Mautnera0026042014-04-23 11:45:37 -07004534 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
4535 r.state = new Bundle();
4536 r.state.setAllowFds(false);
4537 if (r.isPersistable()) {
4538 r.persistentState = new PersistableBundle();
4539 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
4540 r.persistentState);
4541 } else {
4542 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
4543 }
4544 }
4545
Dianne Hackborn73c14162012-09-19 15:45:06 -07004546 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004548 ArrayList<ComponentCallbacks2> callbacks
4549 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07004550
Craig Mautner88c05892013-06-28 09:47:45 -07004551 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004552 final int NAPP = mAllApplications.size();
4553 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004554 callbacks.add(mAllApplications.get(i));
4555 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004556 final int NACT = mActivities.size();
4557 for (int i=0; i<NACT; i++) {
4558 ActivityClientRecord ar = mActivities.valueAt(i);
4559 Activity a = ar.activity;
4560 if (a != null) {
4561 Configuration thisConfig = applyConfigCompatMainThread(
4562 mCurDefaultDisplayDpi, newConfig,
4563 ar.packageInfo.getCompatibilityInfo());
4564 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
4565 // If the activity is currently resumed, its configuration
4566 // needs to change right now.
4567 callbacks.add(a);
4568 } else if (thisConfig != null) {
4569 // Otherwise, we will tell it about the change
4570 // the next time it is resumed or shown. Note that
4571 // the activity manager may, before then, decide the
4572 // activity needs to be destroyed to handle its new
4573 // configuration.
4574 if (DEBUG_CONFIGURATION) {
4575 Slog.v(TAG, "Setting activity "
4576 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07004577 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004578 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
4580 }
4581 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004582 final int NSVC = mServices.size();
4583 for (int i=0; i<NSVC; i++) {
4584 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 }
4586 }
4587 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004588 final int NPRV = mLocalProviders.size();
4589 for (int i=0; i<NPRV; i++) {
4590 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 }
4592 }
Bob Leee5408332009-09-04 18:31:17 -07004593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 return callbacks;
4595 }
Bob Leee5408332009-09-04 18:31:17 -07004596
Adam Lesinski082614c2016-03-04 14:33:47 -08004597 /**
4598 * Updates the configuration for an Activity. The ActivityClientRecord's
4599 * {@link ActivityClientRecord#overrideConfig} is used to compute the final Configuration for
4600 * that Activity. {@link ActivityClientRecord#tmpConfig} is used as a temporary for delivering
4601 * the updated Configuration.
4602 * @param r ActivityClientRecord representing the Activity.
4603 * @param newBaseConfig The new configuration to use. This may be augmented with
4604 * {@link ActivityClientRecord#overrideConfig}.
4605 * @param reportToActivity true if the change should be reported to the Activity's callback.
4606 */
4607 private void performConfigurationChangedForActivity(ActivityClientRecord r,
4608 Configuration newBaseConfig,
4609 boolean reportToActivity) {
4610 r.tmpConfig.setTo(newBaseConfig);
4611 if (r.overrideConfig != null) {
4612 r.tmpConfig.updateFrom(r.overrideConfig);
4613 }
4614 performConfigurationChanged(r.activity, r.token, r.tmpConfig, r.overrideConfig,
4615 reportToActivity);
4616 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.tmpConfig));
4617 }
4618
4619 /**
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004620 * Creates a new Configuration only if override would modify base. Otherwise returns base.
4621 * @param base The base configuration.
4622 * @param override The update to apply to the base configuration. Can be null.
4623 * @return A Configuration representing base with override applied.
4624 */
4625 private static Configuration createNewConfigAndUpdateIfNotNull(@NonNull Configuration base,
4626 @Nullable Configuration override) {
4627 if (override == null) {
4628 return base;
4629 }
4630 Configuration newConfig = new Configuration(base);
4631 newConfig.updateFrom(override);
4632 return newConfig;
4633 }
4634
4635 /**
Adam Lesinski082614c2016-03-04 14:33:47 -08004636 * Decides whether to update an Activity's configuration and whether to tell the
4637 * Activity/Component about it.
4638 * @param cb The component callback to notify of configuration change.
4639 * @param activityToken The Activity binder token for which this configuration change happened.
4640 * If the change is global, this is null.
4641 * @param newConfig The new configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004642 * @param amOverrideConfig The override config that differentiates the Activity's configuration
Adam Lesinski082614c2016-03-04 14:33:47 -08004643 * from the base global configuration.
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004644 * This is supplied by ActivityManager.
Adam Lesinski082614c2016-03-04 14:33:47 -08004645 * @param reportToActivity Notify the Activity of the change.
4646 */
4647 private void performConfigurationChanged(ComponentCallbacks2 cb,
4648 IBinder activityToken,
4649 Configuration newConfig,
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004650 Configuration amOverrideConfig,
Adam Lesinski082614c2016-03-04 14:33:47 -08004651 boolean reportToActivity) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004652 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004653 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004654 // we check the runtime type and act accordingly.
4655 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4656 if (activity != null) {
4657 activity.mCalled = false;
4658 }
Bob Leee5408332009-09-04 18:31:17 -07004659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 boolean shouldChangeConfig = false;
4661 if ((activity == null) || (activity.mCurrentConfig == null)) {
4662 shouldChangeConfig = true;
4663 } else {
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004664 // If the new config is the same as the config this Activity is already
4665 // running with and the override config also didn't change, then don't
4666 // bother calling onConfigurationChanged.
Adam Lesinski082614c2016-03-04 14:33:47 -08004667 int diff = activity.mCurrentConfig.diff(newConfig);
Andrii Kulian3b3c9142016-07-18 19:15:56 -07004668 if (diff != 0 || !mResourcesManager.isSameResourcesOverrideConfig(activityToken,
4669 amOverrideConfig)) {
Chong Zhang4951f9d2016-06-23 13:15:08 -07004670 // Always send the task-level config changes. For system-level configuration, if
4671 // this activity doesn't handle any of the config changes, then don't bother
4672 // calling onConfigurationChanged as we're going to destroy it.
4673 if (!mUpdatingSystemConfig
4674 || (~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
4675 || !reportToActivity) {
4676 shouldChangeConfig = true;
4677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004678 }
4679 }
Bob Leee5408332009-09-04 18:31:17 -07004680
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004681 if (shouldChangeConfig) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004682 // Propagate the configuration change to the Activity and ResourcesManager.
4683
4684 // ContextThemeWrappers may override the configuration for that context.
4685 // We must check and apply any overrides defined.
4686 Configuration contextThemeWrapperOverrideConfig = null;
4687 if (cb instanceof ContextThemeWrapper) {
4688 final ContextThemeWrapper contextThemeWrapper = (ContextThemeWrapper) cb;
4689 contextThemeWrapperOverrideConfig = contextThemeWrapper.getOverrideConfiguration();
4690 }
4691
4692 // We only update an Activity's configuration if this is not a global
4693 // configuration change. This must also be done before the callback,
4694 // or else we violate the contract that the new resources are available
4695 // in {@link ComponentCallbacks2#onConfigurationChanged(Configuration)}.
Adam Lesinski082614c2016-03-04 14:33:47 -08004696 if (activityToken != null) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004697 // Apply the ContextThemeWrapper override if necessary.
4698 // NOTE: Make sure the configurations are not modified, as they are treated
4699 // as immutable in many places.
4700 final Configuration finalOverrideConfig = createNewConfigAndUpdateIfNotNull(
4701 amOverrideConfig, contextThemeWrapperOverrideConfig);
4702 mResourcesManager.updateResourcesForActivity(activityToken, finalOverrideConfig);
Adam Lesinski082614c2016-03-04 14:33:47 -08004703 }
4704
Filip Gruszczynskica664812015-12-04 12:43:36 -08004705 if (reportToActivity) {
Adam Lesinskic1b0ebf2016-06-09 11:17:10 -07004706 // Apply the ContextThemeWrapper override if necessary.
4707 // NOTE: Make sure the configurations are not modified, as they are treated
4708 // as immutable in many places.
4709 final Configuration configToReport = createNewConfigAndUpdateIfNotNull(
4710 newConfig, contextThemeWrapperOverrideConfig);
Adam Lesinski3ad1b482016-04-01 16:41:41 -07004711 cb.onConfigurationChanged(configToReport);
Filip Gruszczynskica664812015-12-04 12:43:36 -08004712 }
Bob Leee5408332009-09-04 18:31:17 -07004713
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004714 if (activity != null) {
Filip Gruszczynskica664812015-12-04 12:43:36 -08004715 if (reportToActivity && !activity.mCalled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 throw new SuperNotCalledException(
4717 "Activity " + activity.getLocalClassName() +
Adam Lesinski082614c2016-03-04 14:33:47 -08004718 " did not call through to super.onConfigurationChanged()");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004719 }
4720 activity.mConfigChangeFlags = 0;
Adam Lesinski082614c2016-03-04 14:33:47 -08004721 activity.mCurrentConfig = new Configuration(newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004722 }
4723 }
4724 }
4725
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004726 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004727 synchronized (mResourcesManager) {
4728 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004729 }
4730 }
4731
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004732 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004733 Configuration config = mConfiguration;
4734 if (mCompatConfiguration == null) {
4735 mCompatConfiguration = new Configuration();
4736 }
4737 mCompatConfiguration.setTo(mConfiguration);
Adam Lesinski082614c2016-03-04 14:33:47 -08004738 if (mResourcesManager.applyCompatConfigurationLocked(displayDensity,
4739 mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004740 config = mCompatConfiguration;
4741 }
4742 return config;
4743 }
4744
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004745 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004746
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004747 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004748
Craig Mautner88c05892013-06-28 09:47:45 -07004749 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004750 if (mPendingConfiguration != null) {
4751 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4752 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004753 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004754 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004755 }
4756 mPendingConfiguration = null;
4757 }
4758
4759 if (config == null) {
4760 return;
4761 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004762
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004763 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004764 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004765
4766 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
Adam Lesinskib61e4052016-05-19 18:23:05 -07004767 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
4768 mResourcesManager.getConfiguration().getLocales());
Craig Mautner88c05892013-06-28 09:47:45 -07004769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004770 if (mConfiguration == null) {
4771 mConfiguration = new Configuration();
4772 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004773 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004774 return;
4775 }
Alan Viverettee54d2452015-05-06 10:41:43 -07004776
4777 configDiff = mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004778 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
Alan Viverette395cd012015-08-11 17:27:04 -04004779
4780 final Theme systemTheme = getSystemContext().getTheme();
4781 if ((systemTheme.getChangingConfigurations() & configDiff) != 0) {
4782 systemTheme.rebase();
4783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004784 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004785
4786 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4787
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004788 freeTextLayoutCachesIfNeeded(configDiff);
4789
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004790 if (callbacks != null) {
4791 final int N = callbacks.size();
4792 for (int i=0; i<N; i++) {
Andrii Kulian701214b2016-04-07 09:36:33 -07004793 ComponentCallbacks2 cb = callbacks.get(i);
4794 if (cb instanceof Activity) {
4795 // If callback is an Activity - call corresponding method to consider override
4796 // config and avoid onConfigurationChanged if it hasn't changed.
4797 Activity a = (Activity) cb;
4798 performConfigurationChangedForActivity(mActivities.get(a.getActivityToken()),
4799 config, REPORT_TO_ACTIVITY);
4800 } else {
4801 performConfigurationChanged(cb, null, config, null, REPORT_TO_ACTIVITY);
4802 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004804 }
4805 }
4806
Romain Guy46bfc482013-08-16 18:38:29 -07004807 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004808 if (configDiff != 0) {
4809 // Ask text layout engine to free its caches if there is a locale change
4810 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4811 if (hasLocaleConfigChange) {
4812 Canvas.freeTextLayoutCaches();
4813 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4814 }
4815 }
4816 }
4817
Filip Gruszczynskica664812015-12-04 12:43:36 -08004818 final void handleActivityConfigurationChanged(ActivityConfigChangeData data,
4819 boolean reportToActivity) {
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004820 ActivityClientRecord r = mActivities.get(data.activityToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 if (r == null || r.activity == null) {
4822 return;
4823 }
Bob Leee5408332009-09-04 18:31:17 -07004824
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004825 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Filip Gruszczynskica664812015-12-04 12:43:36 -08004826 + r.activityInfo.name + ", with callback=" + reportToActivity);
Wale Ogunwalec2607b42015-02-07 16:16:59 -08004827
Adam Lesinski082614c2016-03-04 14:33:47 -08004828 r.overrideConfig = data.overrideConfig;
4829 performConfigurationChangedForActivity(r, mCompatConfiguration, reportToActivity);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004830 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004831 }
4832
Jeff Hao1b012d32014-08-20 10:35:34 -07004833 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004834 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004835 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004836 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004837 default:
4838 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004839 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004840 break;
4841 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004842 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004843 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004844 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004845 } finally {
4846 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004847 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004848 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004849 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004850 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004851 }
4852 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004853 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004854 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004855 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004856 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004857 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004858 }
4859 }
Bob Leee5408332009-09-04 18:31:17 -07004860
Andreas Gampe4c79fea2016-01-28 20:11:41 -08004861 /**
4862 * Public entrypoint to stop profiling. This is required to end profiling when the app crashes,
4863 * so that profiler data won't be lost.
4864 *
4865 * @hide
4866 */
4867 public void stopProfiling() {
4868 mProfiler.stopProfiling();
4869 }
4870
Romain Guya998dff2012-03-23 18:58:36 -07004871 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004872 if (managed) {
4873 try {
4874 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4875 } catch (IOException e) {
4876 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4877 + " -- can the process access this path?");
4878 } finally {
4879 try {
4880 dhd.fd.close();
4881 } catch (IOException e) {
4882 Slog.w(TAG, "Failure closing profile fd", e);
4883 }
4884 }
4885 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004886 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004887 }
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004888 try {
4889 ActivityManagerNative.getDefault().dumpHeapFinished(dhd.path);
4890 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07004891 throw e.rethrowFromSystemServer();
Dianne Hackbornb9a5e4a2015-03-03 17:04:12 -08004892 }
Andy McFadden824c5102010-07-09 16:26:57 -07004893 }
4894
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004895 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4896 boolean hasPkgInfo = false;
Todd Kennedy39bfee52016-02-24 10:28:21 -08004897 switch (cmd) {
4898 case IApplicationThread.PACKAGE_REMOVED:
4899 case IApplicationThread.PACKAGE_REMOVED_DONT_KILL:
4900 {
4901 final boolean killApp = cmd == IApplicationThread.PACKAGE_REMOVED;
4902 if (packages == null) {
4903 break;
4904 }
4905 synchronized (mResourcesManager) {
4906 for (int i = packages.length - 1; i >= 0; i--) {
4907 if (!hasPkgInfo) {
4908 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4909 if (ref != null && ref.get() != null) {
4910 hasPkgInfo = true;
4911 } else {
4912 ref = mResourcePackages.get(packages[i]);
4913 if (ref != null && ref.get() != null) {
4914 hasPkgInfo = true;
4915 }
4916 }
4917 }
4918 if (killApp) {
4919 mPackages.remove(packages[i]);
4920 mResourcePackages.remove(packages[i]);
4921 }
4922 }
4923 }
4924 break;
4925 }
4926 case IApplicationThread.PACKAGE_REPLACED:
4927 {
4928 if (packages == null) {
4929 break;
4930 }
4931 synchronized (mResourcesManager) {
4932 for (int i = packages.length - 1; i >= 0; i--) {
4933 WeakReference<LoadedApk> ref = mPackages.get(packages[i]);
4934 LoadedApk pkgInfo = ref != null ? ref.get() : null;
4935 if (pkgInfo != null) {
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004936 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004937 } else {
4938 ref = mResourcePackages.get(packages[i]);
Todd Kennedy39bfee52016-02-24 10:28:21 -08004939 pkgInfo = ref != null ? ref.get() : null;
4940 if (pkgInfo != null) {
mark_chen89764e32014-12-12 15:38:48 +08004941 hasPkgInfo = true;
4942 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004943 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004944 // If the package is being replaced, yet it still has a valid
4945 // LoadedApk object, the package was updated with _DONT_KILL.
4946 // Adjust it's internal references to the application info and
4947 // resources.
4948 if (pkgInfo != null) {
4949 try {
4950 final String packageName = packages[i];
4951 final ApplicationInfo aInfo =
4952 sPackageManager.getApplicationInfo(
4953 packageName,
4954 0 /*flags*/,
4955 UserHandle.myUserId());
4956
4957 if (mActivities.size() > 0) {
4958 for (ActivityClientRecord ar : mActivities.values()) {
4959 if (ar.activityInfo.applicationInfo.packageName
4960 .equals(packageName)) {
4961 ar.activityInfo.applicationInfo = aInfo;
4962 ar.packageInfo = pkgInfo;
4963 }
4964 }
4965 }
4966 final List<String> oldPaths =
4967 sPackageManager.getPreviousCodePaths(packageName);
4968 pkgInfo.updateApplicationInfo(aInfo, oldPaths);
4969 } catch (RemoteException e) {
4970 }
4971 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004972 }
4973 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004974 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004975 }
4976 }
Todd Kennedy39bfee52016-02-24 10:28:21 -08004977 ApplicationPackageManager.handlePackageBroadcast(cmd, packages, hasPkgInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004978 }
Tim Murraye1e6c662015-04-07 13:24:14 -07004979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004981 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 final int N = callbacks.size();
4984 for (int i=0; i<N; i++) {
4985 callbacks.get(i).onLowMemory();
4986 }
4987
Chris Tatece229052009-03-25 16:44:52 -07004988 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4989 if (Process.myUid() != Process.SYSTEM_UID) {
4990 int sqliteReleased = SQLiteDatabase.releaseMemory();
4991 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4992 }
Bob Leee5408332009-09-04 18:31:17 -07004993
Mike Reedcaf0df12009-04-27 14:32:05 -04004994 // Ask graphics to free up as much as possible (font/image caches)
4995 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004997 // Ask text layout engine to free also as much as possible
4998 Canvas.freeTextLayoutCaches();
4999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 BinderInternal.forceGc("mem");
5001 }
5002
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005003 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07005004 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005005
Dianne Hackborn73c14162012-09-19 15:45:06 -07005006 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005007
5008 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07005009 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005010 callbacks.get(i).onTrimMemory(level);
5011 }
Romain Guy19f86e82012-04-23 15:19:07 -07005012
John Reckf47a5942014-06-30 16:20:04 -07005013 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07005014 }
5015
Jesse Hall79bf3922016-12-12 12:53:02 -08005016 private void setupGraphicsSupport(Context context, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08005017 if (Process.isIsolated()) {
5018 // Isolated processes aren't going to do UI.
5019 return;
5020 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005021 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setupGraphicsSupport");
Romain Guya9582652011-11-10 14:20:10 -08005022 try {
5023 int uid = Process.myUid();
5024 String[] packages = getPackageManager().getPackagesForUid(uid);
5025
Shuo Gao1e6d77e2016-03-01 13:32:03 +08005026 if (packages != null) {
John Reck51aaf902015-12-02 15:08:07 -08005027 ThreadedRenderer.setupDiskCache(cacheDir);
Tim Murraye1e6c662015-04-07 13:24:14 -07005028 RenderScriptCacheDir.setupDiskCache(cacheDir);
Jesse Hall79bf3922016-12-12 12:53:02 -08005029 GraphicsEnvironment.setupGraphicsEnvironment(context);
Romain Guya9582652011-11-10 14:20:10 -08005030 }
5031 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005032 throw e.rethrowFromSystemServer();
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005033 } finally {
5034 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Romain Guya9582652011-11-10 14:20:10 -08005035 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07005036 }
5037
5038 private void updateDefaultDensity() {
Alan Viverette2ac46f12016-02-04 16:58:14 -05005039 final int densityDpi = mCurDefaultDisplayDpi;
5040 if (!mDensityCompatMode
5041 && densityDpi != Configuration.DENSITY_DPI_UNDEFINED
5042 && densityDpi != DisplayMetrics.DENSITY_DEVICE) {
5043 DisplayMetrics.DENSITY_DEVICE = densityDpi;
5044 Bitmap.setDefaultDensity(densityDpi);
Dianne Hackborndde331c2012-08-03 14:01:57 -07005045 }
5046 }
5047
Todd Kennedye713efc2016-05-10 13:45:40 -07005048 /**
5049 * Returns the correct library directory for the current ABI.
5050 * <p>
5051 * If we're dealing with a multi-arch application that has both 32 and 64 bit shared
5052 * libraries, we might need to choose the secondary depending on what the current
5053 * runtime's instruction set is.
5054 */
5055 private String getInstrumentationLibrary(ApplicationInfo appInfo, InstrumentationInfo insInfo) {
5056 if (appInfo.primaryCpuAbi != null && appInfo.secondaryCpuAbi != null) {
5057 // Get the instruction set supported by the secondary ABI. In the presence
5058 // of a native bridge this might be different than the one secondary ABI used.
5059 String secondaryIsa =
5060 VMRuntime.getInstructionSet(appInfo.secondaryCpuAbi);
5061 final String secondaryDexCodeIsa =
5062 SystemProperties.get("ro.dalvik.vm.isa." + secondaryIsa);
5063 secondaryIsa = secondaryDexCodeIsa.isEmpty() ? secondaryIsa : secondaryDexCodeIsa;
5064
5065 final String runtimeIsa = VMRuntime.getRuntime().vmInstructionSet();
5066 if (runtimeIsa.equals(secondaryIsa)) {
5067 return insInfo.secondaryNativeLibraryDir;
5068 }
5069 }
5070 return insInfo.nativeLibraryDir;
5071 }
5072
Adam Lesinskib61e4052016-05-19 18:23:05 -07005073 /**
5074 * The LocaleList set for the app's resources may have been shuffled so that the preferred
5075 * Locale is at position 0. We must find the index of this preferred Locale in the
5076 * original LocaleList.
5077 */
5078 private void updateLocaleListFromAppContext(Context context, LocaleList newLocaleList) {
5079 final Locale bestLocale = context.getResources().getConfiguration().getLocales().get(0);
5080 final int newLocaleListSize = newLocaleList.size();
5081 for (int i = 0; i < newLocaleListSize; i++) {
5082 if (bestLocale.equals(newLocaleList.get(i))) {
5083 LocaleList.setDefault(newLocaleList, i);
5084 return;
5085 }
5086 }
Adam Lesinski27d30162016-05-25 16:45:14 -07005087
5088 // The app may have overridden the LocaleList with its own Locale
5089 // (not present in the available list). Push the chosen Locale
5090 // to the front of the list.
5091 LocaleList.setDefault(new LocaleList(bestLocale, newLocaleList));
Adam Lesinskib61e4052016-05-19 18:23:05 -07005092 }
5093
Romain Guy65b345f2011-07-27 18:51:50 -07005094 private void handleBindApplication(AppBindData data) {
Calin Juravle8f5770d2016-04-12 14:12:04 +01005095 // Register the UI Thread as a sensitive thread to the runtime.
5096 VMRuntime.registerSensitiveThread();
Man Caocfa78b22015-06-11 20:14:34 -07005097 if (data.trackAllocation) {
5098 DdmVmInternal.enableRecentAllocations(true);
5099 }
5100
Dianne Hackbornd98885c2016-03-01 17:13:03 -08005101 // Note when this process has started.
5102 Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
5103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 mBoundApplication = data;
5105 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07005106 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005107
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005108 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07005109 if (data.initProfilerInfo != null) {
5110 mProfiler.profileFile = data.initProfilerInfo.profileFile;
5111 mProfiler.profileFd = data.initProfilerInfo.profileFd;
5112 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
5113 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
5114 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08005117 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005118 android.ddm.DdmHandleAppName.setAppName(data.processName,
5119 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005121 if (data.persistent) {
5122 // Persistent processes on low-memory devices do not get to
5123 // use hardware accelerated drawing, since this can add too much
5124 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07005125 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08005126 ThreadedRenderer.disable(false);
Dianne Hackborn5d927c22011-09-02 12:22:18 -07005127 }
5128 }
Jeff Hao1b012d32014-08-20 10:35:34 -07005129
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005130 if (mProfiler.profileFd != null) {
5131 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07005132 }
5133
Joe Onoratod630f102011-03-17 18:42:26 -07005134 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
5135 // implementation to use the pool executor. Normally, we use the
5136 // serialized executor as the default. This has to happen in the
5137 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07005138 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07005139 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
5140 }
5141
Dianne Hackborn7895bc22014-09-05 15:09:03 -07005142 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
5143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 /*
5145 * Before spawning a new process, reset the time zone to be the system time zone.
5146 * This needs to be done because the system time zone could have changed after the
5147 * the spawning of this process. Without doing this this process would have the incorrect
5148 * system time zone.
5149 */
5150 TimeZone.setDefault(null);
5151
Adam Lesinskib61e4052016-05-19 18:23:05 -07005152 /*
5153 * Set the LocaleList. This may change once we create the App Context.
5154 */
5155 LocaleList.setDefault(data.config.getLocales());
5156
5157 synchronized (mResourcesManager) {
5158 /*
5159 * Update the system configuration since its preloaded and might not
5160 * reflect configuration changes. The configuration object passed
5161 * in AppBindData can be safely assumed to be up to date
5162 */
5163 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
5164 mCurDefaultDisplayDpi = data.config.densityDpi;
5165
5166 // This calls mResourcesManager so keep it within the synchronized block.
5167 applyCompatConfiguration(mCurDefaultDisplayDpi);
5168 }
5169
Dianne Hackborne2515ee2011-04-27 18:52:56 -04005170 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171
Dianne Hackborndde331c2012-08-03 14:01:57 -07005172 /**
5173 * Switch this process to density compatibility mode if needed.
5174 */
5175 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
5176 == 0) {
5177 mDensityCompatMode = true;
5178 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
5179 }
5180 updateDefaultDensity();
5181
Narayan Kamathccb2a0862013-12-19 14:49:36 +00005182 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
5183 DateFormat.set24HourTimePref(is24Hr);
5184
Jon Miranda836c0a82014-08-11 12:32:26 -07005185 View.mDebugViewAttributes =
5186 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
5187
Dianne Hackborn96e240f2009-07-26 17:42:30 -07005188 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005189 * For system applications on userdebug/eng builds, log stack
5190 * traces of disk and network access to dropbox for analysis.
5191 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07005192 if ((data.appInfo.flags &
5193 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07005194 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
5195 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07005196 }
5197
5198 /**
Jeff Sharkey344744b2016-01-28 19:03:30 -07005199 * For apps targetting Honeycomb or later, we don't allow network usage
5200 * on the main event loop / UI thread. This is what ultimately throws
5201 * {@link NetworkOnMainThreadException}.
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005202 */
Jeff Sharkey344744b2016-01-28 19:03:30 -07005203 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07005204 StrictMode.enableDeathOnNetwork();
5205 }
5206
Jeff Sharkey344744b2016-01-28 19:03:30 -07005207 /**
5208 * For apps targetting N or later, we don't allow file:// Uri exposure.
5209 * This is what ultimately throws {@link FileUriExposedException}.
5210 */
5211 if (data.appInfo.targetSdkVersion >= Build.VERSION_CODES.N) {
Thierry Strudele60b28d2016-02-25 02:14:16 +00005212 StrictMode.enableDeathOnFileUriExposure();
Jeff Sharkey344744b2016-01-28 19:03:30 -07005213 }
5214
Alex Klyubinf9034cc2015-02-12 11:43:09 -08005215 NetworkSecurityPolicy.getInstance().setCleartextTrafficPermitted(
5216 (data.appInfo.flags & ApplicationInfo.FLAG_USES_CLEARTEXT_TRAFFIC) != 0);
5217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005218 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
5219 // XXX should have option to change the port.
5220 Debug.changeDebugPort(8100);
5221 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005222 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005223 + " is waiting for the debugger on port 8100...");
5224
5225 IActivityManager mgr = ActivityManagerNative.getDefault();
5226 try {
5227 mgr.showWaitingForDebugger(mAppThread, true);
5228 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005229 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005230 }
5231
5232 Debug.waitForDebugger();
5233
5234 try {
5235 mgr.showWaitingForDebugger(mAppThread, false);
5236 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005237 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 }
5239
5240 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005241 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 + " can be debugged on port 8100...");
5243 }
5244 }
5245
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005246 // Allow application-generated systrace messages if we're debuggable.
Rahul Chaturvedi52613f92015-06-17 23:54:08 -04005247 boolean isAppDebuggable = (data.appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
5248 Trace.setAppTracingAllowed(isAppDebuggable);
5249 if (isAppDebuggable && data.enableBinderTracking) {
5250 Binder.enableTracing();
5251 }
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07005252
Robert Greenwalt434203a2010-10-11 16:00:27 -07005253 /**
5254 * Initialize the default http proxy in this process for the reasons we set the time zone.
5255 */
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005256 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Setup proxies");
Alan Viverettebe64eae2015-09-03 14:56:04 -04005257 final IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005258 if (b != null) {
5259 // In pre-boot mode (doing initial launch to collect password), not
5260 // all system is up. This includes the connectivity service, so don't
5261 // crash if we can't get it.
Alan Viverettebe64eae2015-09-03 14:56:04 -04005262 final IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005263 try {
Paul Jensencee9b512015-05-06 07:32:40 -04005264 final ProxyInfo proxyInfo = service.getProxyForNetwork(null);
Jason Monk207900c2014-04-25 15:00:09 -04005265 Proxy.setHttpProxySystemProperty(proxyInfo);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005266 } catch (RemoteException e) {
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005267 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005268 throw e.rethrowFromSystemServer();
5269 }
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08005270 }
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005271 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Robert Greenwalt434203a2010-10-11 16:00:27 -07005272
Alan Viverette2107d692015-09-03 14:55:27 -04005273 // Instrumentation info affects the class loader, so load it before
5274 // setting up the app context.
5275 final InstrumentationInfo ii;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 try {
Alan Viverette2107d692015-09-03 14:55:27 -04005278 ii = new ApplicationPackageManager(null, getPackageManager())
5279 .getInstrumentationInfo(data.instrumentationName, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005280 } catch (PackageManager.NameNotFoundException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 throw new RuntimeException(
Alan Viverette2107d692015-09-03 14:55:27 -04005282 "Unable to find instrumentation info for: " + data.instrumentationName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005283 }
5284
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005285 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005287 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
Todd Kennedye713efc2016-05-10 13:45:40 -07005288 mInstrumentationLibDir = getInstrumentationLibrary(data.appInfo, ii);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005289 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07005290 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
5291 mInstrumentedLibDir = data.info.getLibDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005292 } else {
5293 ii = null;
5294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005295
Alan Viverette2107d692015-09-03 14:55:27 -04005296 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Adam Lesinskib61e4052016-05-19 18:23:05 -07005297 updateLocaleListFromAppContext(appContext,
5298 mResourcesManager.getConfiguration().getLocales());
Seigo Nonakac14dd782016-03-30 23:09:16 +09005299
Jeff Sharkeye6cb0972016-02-01 09:59:12 -07005300 if (!Process.isIsolated() && !"android".equals(appContext.getPackageName())) {
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005301 // This cache location probably points at credential-encrypted
5302 // storage which may not be accessible yet; assign it anyway instead
5303 // of pointing at device-encrypted storage.
Alan Viverette2107d692015-09-03 14:55:27 -04005304 final File cacheDir = appContext.getCacheDir();
5305 if (cacheDir != null) {
5306 // Provide a usable directory for temporary files
5307 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
5308 } else {
5309 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property "
5310 + "due to missing cache directory");
5311 }
Alan Viverette346296b2015-09-01 13:08:05 -04005312
Calin Juravle2a727d72016-04-15 19:33:46 +01005313 // Setup a location to store generated/compiled graphics code.
Jeff Sharkey8a372a02016-03-16 16:25:45 -06005314 final Context deviceContext = appContext.createDeviceProtectedStorageContext();
Jeff Sharkeye84bdd32016-02-08 12:16:00 -07005315 final File codeCacheDir = deviceContext.getCodeCacheDir();
Alan Viverette2107d692015-09-03 14:55:27 -04005316 if (codeCacheDir != null) {
Jesse Hall79bf3922016-12-12 12:53:02 -08005317 setupGraphicsSupport(appContext, codeCacheDir);
Alan Viverette2107d692015-09-03 14:55:27 -04005318 } else {
Calin Juravle2a727d72016-04-15 19:33:46 +01005319 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Alan Viverette2107d692015-09-03 14:55:27 -04005320 }
Michael Lentine2ba303f2016-02-01 20:44:34 -06005321 }
Michael Lentine03d8f7682016-01-31 15:37:11 -06005322
Chad Brubaker78d47122015-11-17 22:26:58 -08005323 // Install the Network Security Config Provider. This must happen before the application
5324 // code is loaded to prevent issues with instances of TLS objects being created before
5325 // the provider is installed.
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005326 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "NetworkSecurityConfigProvider.install");
Chad Brubakerc3a6c782016-06-08 12:57:46 -07005327 NetworkSecurityConfigProvider.install(appContext);
Andreas Gampe4c8e5422016-05-18 11:58:47 -07005328 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Chad Brubaker78d47122015-11-17 22:26:58 -08005329
Alan Viverette2107d692015-09-03 14:55:27 -04005330 // Continue loading instrumentation.
5331 if (ii != null) {
Alan Viverette346296b2015-09-01 13:08:05 -04005332 final ApplicationInfo instrApp = new ApplicationInfo();
Jeff Sharkey15447792015-11-05 16:18:51 -08005333 ii.copyTo(instrApp);
Fyodor Kupolovaf38b8e2015-12-02 16:16:07 -08005334 instrApp.initForUser(UserHandle.myUserId());
Alan Viverettebe64eae2015-09-03 14:56:04 -04005335 final LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07005336 appContext.getClassLoader(), false, true, false);
Alan Viverettebe64eae2015-09-03 14:56:04 -04005337 final ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338
5339 try {
Alan Viverettebe64eae2015-09-03 14:56:04 -04005340 final ClassLoader cl = instrContext.getClassLoader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005341 mInstrumentation = (Instrumentation)
5342 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
5343 } catch (Exception e) {
5344 throw new RuntimeException(
5345 "Unable to instantiate instrumentation "
5346 + data.instrumentationName + ": " + e.toString(), e);
5347 }
5348
Alan Viverettebe64eae2015-09-03 14:56:04 -04005349 final ComponentName component = new ComponentName(ii.packageName, ii.name);
5350 mInstrumentation.init(this, instrContext, appContext, component,
5351 data.instrumentationWatcher, data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005352
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005353 if (mProfiler.profileFile != null && !ii.handleProfiling
5354 && mProfiler.profileFd == null) {
5355 mProfiler.handlingProfiling = true;
Alan Viverettebe64eae2015-09-03 14:56:04 -04005356 final File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005357 file.getParentFile().mkdirs();
5358 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
5359 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005360 } else {
5361 mInstrumentation = new Instrumentation();
5362 }
5363
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005364 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08005365 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08005366 } else {
5367 // Small heap, clamp to the current growth limit and let the heap release
5368 // pages after the growth limit to the non growth limit capacity. b/18387825
5369 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08005370 }
5371
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005372 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08005373 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005374 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08005375 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005376 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005377 // If the app is being launched for full backup or restore, bring it up in
5378 // a restricted environment with the base application class.
5379 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
5380 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08005381
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005382 // don't bring up providers in restricted mode; they may depend on the
5383 // app's custom Application class
5384 if (!data.restrictedBackupMode) {
Jeff Sharkey3e195892016-03-05 19:48:59 -07005385 if (!ArrayUtils.isEmpty(data.providers)) {
5386 installContentProviders(app, data.providers);
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005387 // For process that contains content providers, we want to
5388 // ensure that the JIT is enabled "at some point".
5389 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
5390 }
5391 }
5392
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005393 // Do this after providers, since instrumentation tests generally start their
5394 // test thread at this point, and we don't want that racing.
5395 try {
5396 mInstrumentation.onCreate(data.instrumentationArgs);
5397 }
5398 catch (Exception e) {
5399 throw new RuntimeException(
5400 "Exception thrown in onCreate() of "
5401 + data.instrumentationName + ": " + e.toString(), e);
5402 }
5403
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08005404 try {
5405 mInstrumentation.callApplicationOnCreate(app);
5406 } catch (Exception e) {
5407 if (!mInstrumentation.onException(app, e)) {
5408 throw new RuntimeException(
5409 "Unable to create application " + app.getClass().getName()
5410 + ": " + e.toString(), e);
5411 }
5412 }
5413 } finally {
5414 StrictMode.setThreadPolicy(savedPolicy);
5415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 }
5417
5418 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
5419 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07005420 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
5421 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005422 Debug.stopMethodTracing();
5423 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005424 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005425 // + ", app thr: " + mAppThread);
5426 try {
5427 am.finishInstrumentation(mAppThread, resultCode, results);
5428 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005429 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005430 }
5431 }
5432
Romain Guy65b345f2011-07-27 18:51:50 -07005433 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 Context context, List<ProviderInfo> providers) {
5435 final ArrayList<IActivityManager.ContentProviderHolder> results =
5436 new ArrayList<IActivityManager.ContentProviderHolder>();
5437
Romain Guya998dff2012-03-23 18:58:36 -07005438 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08005439 if (DEBUG_PROVIDER) {
5440 StringBuilder buf = new StringBuilder(128);
5441 buf.append("Pub ");
5442 buf.append(cpi.authority);
5443 buf.append(": ");
5444 buf.append(cpi.name);
5445 Log.i(TAG, buf.toString());
5446 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005447 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
5448 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
5449 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005450 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005451 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 }
5453 }
5454
5455 try {
5456 ActivityManagerNative.getDefault().publishContentProviders(
5457 getApplicationThread(), results);
5458 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005459 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 }
5461 }
5462
Jeff Sharkey6d515712012-09-20 16:06:08 -07005463 public final IContentProvider acquireProvider(
5464 Context c, String auth, int userId, boolean stable) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005465 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005466 if (provider != null) {
5467 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 }
5469
Wale Ogunwale1d646122015-04-24 14:45:14 -07005470 // There is a possible race here. Another thread may try to acquire
5471 // the same provider at the same time. When this happens, we want to ensure
5472 // that the first one wins.
5473 // Note that we cannot hold the lock while acquiring and installing the
5474 // provider since it might take a long time to run and it could also potentially
5475 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005476 IActivityManager.ContentProviderHolder holder = null;
Wale Ogunwale1d646122015-04-24 14:45:14 -07005477 try {
5478 holder = ActivityManagerNative.getDefault().getContentProvider(
5479 getApplicationThread(), auth, userId, stable);
5480 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005481 throw ex.rethrowFromSystemServer();
Wale Ogunwale67fe0a42015-04-24 14:44:54 -07005482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005484 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005485 return null;
5486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005487
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005488 // Install provider will increment the reference count for us, and break
5489 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005490 holder = installProvider(c, holder, holder.info,
5491 true /*noisy*/, holder.noReleaseNeeded, stable);
5492 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005493 }
5494
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005495 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
5496 if (stable) {
5497 prc.stableCount += 1;
5498 if (prc.stableCount == 1) {
5499 // We are acquiring a new stable reference on the provider.
5500 int unstableDelta;
5501 if (prc.removePending) {
5502 // We have a pending remove operation, which is holding the
5503 // last unstable reference. At this point we are converting
5504 // that unstable reference to our new stable reference.
5505 unstableDelta = -1;
5506 // Cancel the removal of the provider.
5507 if (DEBUG_PROVIDER) {
5508 Slog.v(TAG, "incProviderRef: stable "
5509 + "snatched provider from the jaws of death");
5510 }
5511 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005512 // There is a race! It fails to remove the message, which
5513 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005514 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5515 } else {
5516 unstableDelta = 0;
5517 }
5518 try {
5519 if (DEBUG_PROVIDER) {
5520 Slog.v(TAG, "incProviderRef Now stable - "
5521 + prc.holder.info.name + ": unstableDelta="
5522 + unstableDelta);
5523 }
5524 ActivityManagerNative.getDefault().refContentProvider(
5525 prc.holder.connection, 1, unstableDelta);
5526 } catch (RemoteException e) {
5527 //do nothing content provider object is dead any way
5528 }
5529 }
5530 } else {
5531 prc.unstableCount += 1;
5532 if (prc.unstableCount == 1) {
5533 // We are acquiring a new unstable reference on the provider.
5534 if (prc.removePending) {
5535 // Oh look, we actually have a remove pending for the
5536 // provider, which is still holding the last unstable
5537 // reference. We just need to cancel that to take new
5538 // ownership of the reference.
5539 if (DEBUG_PROVIDER) {
5540 Slog.v(TAG, "incProviderRef: unstable "
5541 + "snatched provider from the jaws of death");
5542 }
5543 prc.removePending = false;
5544 mH.removeMessages(H.REMOVE_PROVIDER, prc);
5545 } else {
5546 // First unstable ref, increment our count in the
5547 // activity manager.
5548 try {
5549 if (DEBUG_PROVIDER) {
5550 Slog.v(TAG, "incProviderRef: Now unstable - "
5551 + prc.holder.info.name);
5552 }
5553 ActivityManagerNative.getDefault().refContentProvider(
5554 prc.holder.connection, 0, 1);
5555 } catch (RemoteException e) {
5556 //do nothing content provider object is dead any way
5557 }
5558 }
5559 }
5560 }
5561 }
5562
Jeff Sharkey6d515712012-09-20 16:06:08 -07005563 public final IContentProvider acquireExistingProvider(
5564 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005565 synchronized (mProviderMap) {
Wale Ogunwale1d646122015-04-24 14:45:14 -07005566 final ProviderKey key = new ProviderKey(auth, userId);
Jeff Sharkey6d515712012-09-20 16:06:08 -07005567 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005568 if (pr == null) {
5569 return null;
5570 }
5571
5572 IContentProvider provider = pr.mProvider;
5573 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005574 if (!jBinder.isBinderAlive()) {
5575 // The hosting process of the provider has died; we can't
5576 // use this one.
Wale Ogunwale1d646122015-04-24 14:45:14 -07005577 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005578 + ": existing object's process dead");
5579 handleUnstableProviderDiedLocked(jBinder, true);
5580 return null;
5581 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005582
5583 // Only increment the ref count if we have one. If we don't then the
5584 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005585 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005586 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005587 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005588 }
5589 return provider;
5590 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07005591 }
5592
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005593 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
5594 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005595 return false;
5596 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005599 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005601 if (prc == null) {
5602 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005603 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005604 }
5605
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005606 boolean lastRef = false;
5607 if (stable) {
5608 if (prc.stableCount == 0) {
5609 if (DEBUG_PROVIDER) Slog.v(TAG,
5610 "releaseProvider: stable ref count already 0, how?");
5611 return false;
5612 }
5613 prc.stableCount -= 1;
5614 if (prc.stableCount == 0) {
5615 // What we do at this point depends on whether there are
5616 // any unstable refs left: if there are, we just tell the
5617 // activity manager to decrement its stable count; if there
5618 // aren't, we need to enqueue this provider to be removed,
5619 // and convert to holding a single unstable ref while
5620 // doing so.
5621 lastRef = prc.unstableCount == 0;
5622 try {
5623 if (DEBUG_PROVIDER) {
5624 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
5625 + lastRef + " - " + prc.holder.info.name);
5626 }
5627 ActivityManagerNative.getDefault().refContentProvider(
5628 prc.holder.connection, -1, lastRef ? 1 : 0);
5629 } catch (RemoteException e) {
5630 //do nothing content provider object is dead any way
5631 }
5632 }
5633 } else {
5634 if (prc.unstableCount == 0) {
5635 if (DEBUG_PROVIDER) Slog.v(TAG,
5636 "releaseProvider: unstable ref count already 0, how?");
5637 return false;
5638 }
5639 prc.unstableCount -= 1;
5640 if (prc.unstableCount == 0) {
5641 // If this is the last reference, we need to enqueue
5642 // this provider to be removed instead of telling the
5643 // activity manager to remove it at this point.
5644 lastRef = prc.stableCount == 0;
5645 if (!lastRef) {
5646 try {
5647 if (DEBUG_PROVIDER) {
5648 Slog.v(TAG, "releaseProvider: No longer unstable - "
5649 + prc.holder.info.name);
5650 }
5651 ActivityManagerNative.getDefault().refContentProvider(
5652 prc.holder.connection, 0, -1);
5653 } catch (RemoteException e) {
5654 //do nothing content provider object is dead any way
5655 }
5656 }
5657 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005658 }
5659
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005660 if (lastRef) {
5661 if (!prc.removePending) {
5662 // Schedule the actual remove asynchronously, since we don't know the context
5663 // this will be called in.
5664 // TODO: it would be nice to post a delayed message, so
5665 // if we come back and need the same provider quickly
5666 // we will still have it available.
5667 if (DEBUG_PROVIDER) {
5668 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
5669 + prc.holder.info.name);
5670 }
5671 prc.removePending = true;
5672 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
5673 mH.sendMessage(msg);
5674 } else {
5675 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
5676 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005677 }
5678 return true;
5679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 }
5681
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005682 final void completeRemoveProvider(ProviderRefCount prc) {
5683 synchronized (mProviderMap) {
5684 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005685 // There was a race! Some other client managed to acquire
5686 // the provider before the removal was completed.
5687 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005688 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005689 + "provider still in use");
5690 return;
5691 }
5692
Guobin Zhang9e3e52662013-03-21 13:57:11 +08005693 // More complicated race!! Some client managed to acquire the
5694 // provider and release it before the removal was completed.
5695 // Continue the removal, and abort the next remove message.
5696 prc.removePending = false;
5697
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005698 final IBinder jBinder = prc.holder.provider.asBinder();
5699 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
5700 if (existingPrc == prc) {
5701 mProviderRefCountMap.remove(jBinder);
5702 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005703
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005704 for (int i=mProviderMap.size()-1; i>=0; i--) {
5705 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005706 IBinder myBinder = pr.mProvider.asBinder();
5707 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07005708 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005709 }
5710 }
5711 }
5712
5713 try {
5714 if (DEBUG_PROVIDER) {
5715 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
5716 + "removeContentProvider(" + prc.holder.info.name + ")");
5717 }
5718 ActivityManagerNative.getDefault().removeContentProvider(
5719 prc.holder.connection, false);
5720 } catch (RemoteException e) {
5721 //do nothing content provider object is dead any way
5722 }
5723 }
5724
5725 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005726 synchronized (mProviderMap) {
5727 handleUnstableProviderDiedLocked(provider, fromClient);
5728 }
5729 }
5730
5731 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
5732 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5733 if (prc != null) {
5734 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
5735 + provider + " " + prc.holder.info.name);
5736 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09005737 for (int i=mProviderMap.size()-1; i>=0; i--) {
5738 ProviderClientRecord pr = mProviderMap.valueAt(i);
5739 if (pr != null && pr.mProvider.asBinder() == provider) {
5740 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
5741 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005742 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005743 }
You Kimbc74de62013-10-01 00:13:26 +09005744
Dianne Hackbornc428aae2012-10-03 16:38:22 -07005745 if (fromClient) {
5746 // We found out about this due to execution in our client
5747 // code. Tell the activity manager about it now, to ensure
5748 // that the next time we go to do anything with the provider
5749 // it knows it is dead (so we don't race with its death
5750 // notification).
5751 try {
5752 ActivityManagerNative.getDefault().unstableProviderDied(
5753 prc.holder.connection);
5754 } catch (RemoteException e) {
5755 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005756 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07005757 }
5758 }
5759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005760
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005761 final void appNotRespondingViaProvider(IBinder provider) {
5762 synchronized (mProviderMap) {
5763 ProviderRefCount prc = mProviderRefCountMap.get(provider);
5764 if (prc != null) {
5765 try {
5766 ActivityManagerNative.getDefault()
5767 .appNotRespondingViaProvider(prc.holder.connection);
5768 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005769 throw e.rethrowFromSystemServer();
Jeff Sharkey7aa76012013-09-30 14:26:27 -07005770 }
5771 }
5772 }
5773 }
5774
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005775 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07005776 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08005777 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07005778 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
5779
5780 final ProviderClientRecord pcr = new ProviderClientRecord(
5781 auths, provider, localProvider, holder);
5782 for (String auth : auths) {
5783 final ProviderKey key = new ProviderKey(auth, userId);
5784 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005785 if (existing != null) {
5786 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07005787 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005788 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07005789 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005790 }
5791 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005792 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 }
5794
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005795 /**
5796 * Installs the provider.
5797 *
5798 * Providers that are local to the process or that come from the system server
5799 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
5800 * Other remote providers are reference counted. The initial reference count
5801 * for all reference counted providers is one. Providers that are not reference
5802 * counted do not have a reference count (at all).
5803 *
5804 * This method detects when a provider has already been installed. When this happens,
5805 * it increments the reference count of the existing provider (if appropriate)
5806 * and returns the existing provider. This can happen due to concurrent
5807 * attempts to acquire the same provider.
5808 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005809 private IActivityManager.ContentProviderHolder installProvider(Context context,
5810 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
5811 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005812 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005813 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07005814 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005815 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07005816 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 + info.name);
5818 }
5819 Context c = null;
5820 ApplicationInfo ai = info.applicationInfo;
5821 if (context.getPackageName().equals(ai.packageName)) {
5822 c = context;
5823 } else if (mInitialApplication != null &&
5824 mInitialApplication.getPackageName().equals(ai.packageName)) {
5825 c = mInitialApplication;
5826 } else {
5827 try {
5828 c = context.createPackageContext(ai.packageName,
5829 Context.CONTEXT_INCLUDE_CODE);
5830 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07005831 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 }
5833 }
5834 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005835 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 ai.packageName +
5837 " while loading content provider " +
5838 info.name);
5839 return null;
5840 }
5841 try {
5842 final java.lang.ClassLoader cl = c.getClassLoader();
5843 localProvider = (ContentProvider)cl.
5844 loadClass(info.name).newInstance();
5845 provider = localProvider.getIContentProvider();
5846 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08005847 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 info.name + " from sourceDir " +
5849 info.applicationInfo.sourceDir);
5850 return null;
5851 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005852 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005853 TAG, "Instantiating local provider " + info.name);
5854 // XXX Need to create the correct context for this provider.
5855 localProvider.attachInfo(c, info);
5856 } catch (java.lang.Exception e) {
5857 if (!mInstrumentation.onException(null, e)) {
5858 throw new RuntimeException(
5859 "Unable to get provider " + info.name
5860 + ": " + e.toString(), e);
5861 }
5862 return null;
5863 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005864 } else {
5865 provider = holder.provider;
5866 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005867 + info.name);
5868 }
5869
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005870 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005871
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005872 synchronized (mProviderMap) {
5873 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5874 + " / " + info.name);
5875 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005876 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005877 ComponentName cname = new ComponentName(info.packageName, info.name);
5878 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005879 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005880 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005881 Slog.v(TAG, "installProvider: lost the race, "
5882 + "using existing local provider");
5883 }
5884 provider = pr.mProvider;
5885 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005886 holder = new IActivityManager.ContentProviderHolder(info);
5887 holder.provider = provider;
5888 holder.noReleaseNeeded = true;
5889 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005890 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005891 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005892 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005893 retHolder = pr.mHolder;
5894 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005895 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5896 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005897 if (DEBUG_PROVIDER) {
5898 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005899 }
Wale Ogunwale1d646122015-04-24 14:45:14 -07005900 // We need to transfer our new reference to the existing
5901 // ref count, releasing the old one... but only if
5902 // release is needed (that is, it is not running in the
5903 // system process).
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005904 if (!noReleaseNeeded) {
5905 incProviderRefLocked(prc, stable);
Wale Ogunwale1d646122015-04-24 14:45:14 -07005906 try {
5907 ActivityManagerNative.getDefault().removeContentProvider(
5908 holder.connection, stable);
5909 } catch (RemoteException e) {
5910 //do nothing content provider object is dead any way
5911 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005912 }
5913 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005914 ProviderClientRecord client = installProviderAuthoritiesLocked(
5915 provider, localProvider, holder);
5916 if (noReleaseNeeded) {
5917 prc = new ProviderRefCount(holder, client, 1000, 1000);
5918 } else {
5919 prc = stable
5920 ? new ProviderRefCount(holder, client, 1, 0)
5921 : new ProviderRefCount(holder, client, 0, 1);
5922 }
5923 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005924 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005925 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005926 }
5927 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005928
5929 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 }
5931
Romain Guy65b345f2011-07-27 18:51:50 -07005932 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005933 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005934 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005936 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005937 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005938 public void run() {
5939 ensureJitEnabled();
5940 }
5941 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005942 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5943 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005945 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 try {
5947 mgr.attachApplication(mAppThread);
5948 } catch (RemoteException ex) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005949 throw ex.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005950 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005951 // Watch for getting close to heap limit.
5952 BinderInternal.addGcWatcher(new Runnable() {
5953 @Override public void run() {
5954 if (!mSomeActivitiesChanged) {
5955 return;
5956 }
5957 Runtime runtime = Runtime.getRuntime();
5958 long dalvikMax = runtime.maxMemory();
5959 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5960 if (dalvikUsed > ((3*dalvikMax)/4)) {
5961 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5962 + " total=" + (runtime.totalMemory()/1024)
5963 + " used=" + (dalvikUsed/1024));
5964 mSomeActivitiesChanged = false;
5965 try {
5966 mgr.releaseSomeActivities(mAppThread);
5967 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -07005968 throw e.rethrowFromSystemServer();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005969 }
5970 }
5971 }
5972 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005973 } else {
5974 // Don't set application object here -- if the system crashes,
5975 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005976 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005977 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005978 try {
5979 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005980 ContextImpl context = ContextImpl.createAppContext(
5981 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005982 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5983 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005984 } catch (Exception e) {
5985 throw new RuntimeException(
5986 "Unable to instantiate Application():" + e.toString(), e);
5987 }
5988 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005989
5990 // add dropbox logging to libcore
5991 DropBox.setReporter(new DropBoxReporter());
5992
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005993 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005994 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005995 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005996 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005997 // We need to apply this change to the resources
5998 // immediately, because upon returning the view
5999 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07006000 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Adam Lesinskib61e4052016-05-19 18:23:05 -07006001 updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
6002 mResourcesManager.getConfiguration().getLocales());
6003
Dianne Hackbornae078162010-03-18 11:29:37 -07006004 // This actually changed the resources! Tell
6005 // everyone about it.
6006 if (mPendingConfiguration == null ||
6007 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
6008 mPendingConfiguration = newConfig;
Tim Murraye1e6c662015-04-07 13:24:14 -07006009
Jeff Brown9ef09972013-10-15 20:49:59 -07006010 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07006011 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006012 }
6013 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006014 }
Craig Mautner88c05892013-06-28 09:47:45 -07006015 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006016 public void onLowMemory() {
6017 }
Craig Mautner88c05892013-06-28 09:47:45 -07006018 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07006019 public void onTrimMemory(int level) {
6020 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08006021 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006022 }
6023
Romain Guy5e9120d2012-01-30 12:17:22 -08006024 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07006025 // The system process on low-memory devices do not get to use hardware
6026 // accelerated drawing, since this can add too much overhead to the
6027 // process.
6028 if (!ActivityManager.isHighEndGfx()) {
John Reck51aaf902015-12-02 15:08:07 -08006029 ThreadedRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07006030 } else {
John Reck51aaf902015-12-02 15:08:07 -08006031 ThreadedRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07006032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 ActivityThread thread = new ActivityThread();
6034 thread.attach(true);
6035 return thread;
6036 }
6037
Jeff Brown10e89712011-07-08 18:52:57 -07006038 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07006040 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006041 }
6042 }
6043
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006044 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07006045 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08006046 if (mCoreSettings != null) {
6047 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006048 }
Craig Mautner88c05892013-06-28 09:47:45 -07006049 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08006050 }
6051 }
6052
Geremy Condra69689a72012-09-11 16:57:17 -07006053 private static class EventLoggingReporter implements EventLogger.Reporter {
6054 @Override
6055 public void report (int code, Object... list) {
6056 EventLog.writeEvent(code, list);
6057 }
6058 }
6059
Geremy Condrab7faaf42012-09-19 18:07:42 -07006060 private class DropBoxReporter implements DropBox.Reporter {
6061
6062 private DropBoxManager dropBox;
6063
Narayan Kamath7f062242015-04-08 13:24:13 +01006064 public DropBoxReporter() {}
Geremy Condrab7faaf42012-09-19 18:07:42 -07006065
6066 @Override
6067 public void addData(String tag, byte[] data, int flags) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006068 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006069 dropBox.addData(tag, data, flags);
6070 }
6071
6072 @Override
6073 public void addText(String tag, String data) {
Narayan Kamath7f062242015-04-08 13:24:13 +01006074 ensureInitialized();
Geremy Condrab7faaf42012-09-19 18:07:42 -07006075 dropBox.addText(tag, data);
6076 }
Narayan Kamath7f062242015-04-08 13:24:13 +01006077
6078 private synchronized void ensureInitialized() {
6079 if (dropBox == null) {
6080 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
6081 }
6082 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07006083 }
6084
Romain Guy65b345f2011-07-27 18:51:50 -07006085 public static void main(String[] args) {
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006086 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
Bob Leee5408332009-09-04 18:31:17 -07006087 SamplingProfilerIntegration.start();
6088
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08006089 // CloseGuard defaults to true and can be quite spammy. We
6090 // disable it here, but selectively enable it later (via
6091 // StrictMode) on debug builds, but using DropBox, not logs.
6092 CloseGuard.setEnabled(false);
6093
Andreas Gamped281b422016-07-08 03:50:27 +00006094 Environment.initForCurrentUser();
Jeff Sharkeyb049e212012-09-07 23:16:01 -07006095
Geremy Condra69689a72012-09-11 16:57:17 -07006096 // Set the reporter for event logging in libcore
6097 EventLogger.setReporter(new EventLoggingReporter());
6098
Robin Lee3d076af2014-04-25 14:57:49 +01006099 // Make sure TrustedCertificateStore looks in the right place for CA certificates
6100 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
6101 TrustedCertificateStore.setDefaultUserDirectory(configDir);
6102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006103 Process.setArgV0("<pre-initialized>");
6104
6105 Looper.prepareMainLooper();
6106
6107 ActivityThread thread = new ActivityThread();
6108 thread.attach(false);
6109
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07006110 if (sMainThreadHandler == null) {
6111 sMainThreadHandler = thread.getHandler();
6112 }
6113
Dianne Hackborn287952c2010-09-22 22:34:31 -07006114 if (false) {
6115 Looper.myLooper().setMessageLogging(new
6116 LogPrinter(Log.DEBUG, "ActivityThread"));
6117 }
6118
Narayan Kamathfbb32f62015-06-12 15:34:35 +01006119 // End of event ActivityThreadMain.
6120 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006121 Looper.loop();
6122
Jeff Brown10e89712011-07-08 18:52:57 -07006123 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006124 }
6125}