blob: fe7b7276d96dc432d1617c0d69f48db682390b33 [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
Christopher Tate45281862010-03-05 15:46:30 -080019import android.app.backup.BackupAgent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.BroadcastReceiver;
Dianne Hackbornc68c9132011-07-29 01:25:18 -070021import android.content.ComponentCallbacks2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.ComponentName;
23import android.content.ContentProvider;
24import android.content.Context;
25import android.content.IContentProvider;
26import android.content.Intent;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070027import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.content.pm.ActivityInfo;
29import android.content.pm.ApplicationInfo;
30import android.content.pm.IPackageManager;
31import android.content.pm.InstrumentationInfo;
Christopher Tate346acb12012-10-15 19:20:25 -070032import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.pm.PackageManager;
Sen Hubde75702010-05-28 01:54:03 -070034import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.ProviderInfo;
36import android.content.pm.ServiceInfo;
37import android.content.res.AssetManager;
Mitsuru Oshimaba3ba572009-07-08 18:49:26 -070038import android.content.res.CompatibilityInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.content.res.Configuration;
40import android.content.res.Resources;
41import android.database.sqlite.SQLiteDatabase;
42import android.database.sqlite.SQLiteDebug;
Vasu Noric3849202010-03-09 10:47:25 -080043import android.database.sqlite.SQLiteDebug.DbStats;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Bitmap;
45import android.graphics.Canvas;
Jeff Brownbd6e1502012-08-28 03:27:37 -070046import android.hardware.display.DisplayManagerGlobal;
Robert Greenwalt434203a2010-10-11 16:00:27 -070047import android.net.IConnectivityManager;
48import android.net.Proxy;
Jason Monk207900c2014-04-25 15:00:09 -040049import android.net.ProxyInfo;
Jason Monk83520b92014-05-09 15:16:06 -040050import android.net.Uri;
Romain Guya9582652011-11-10 14:20:10 -080051import android.opengl.GLUtils;
Joe Onoratod630f102011-03-17 18:42:26 -070052import android.os.AsyncTask;
Amith Yamasani742a6712011-05-04 14:49:28 -070053import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Bundle;
55import android.os.Debug;
Geremy Condrab7faaf42012-09-19 18:07:42 -070056import android.os.DropBoxManager;
Jeff Sharkeyb049e212012-09-07 23:16:01 -070057import android.os.Environment;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.os.Handler;
59import android.os.IBinder;
60import android.os.Looper;
61import android.os.Message;
62import android.os.MessageQueue;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -070063import android.os.ParcelFileDescriptor;
Craig Mautnera0026042014-04-23 11:45:37 -070064import android.os.PersistableBundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.os.Process;
66import android.os.RemoteException;
67import android.os.ServiceManager;
Brad Fitzpatrick438d0592010-06-10 12:19:19 -070068import android.os.StrictMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.os.SystemClock;
Jeff Brownefd43bd2012-09-21 17:02:35 -070070import android.os.SystemProperties;
Dianne Hackborn1ded0b12012-04-26 14:14:50 -070071import android.os.Trace;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070072import android.os.UserHandle;
Narayan Kamathccb2a082013-12-19 14:49:36 +000073import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.util.AndroidRuntimeException;
Dianne Hackbornadd005c2013-07-17 18:43:12 -070075import android.util.ArrayMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.util.DisplayMetrics;
77import android.util.EventLog;
78import android.util.Log;
Dianne Hackborn287952c2010-09-22 22:34:31 -070079import android.util.LogPrinter;
Craig Mautnereb8abf72014-07-02 15:04:09 -070080import android.util.Pair;
Jeff Brown6754ba22011-12-14 20:20:01 -080081import android.util.PrintWriterPrinter;
Dianne Hackbornc9421ba2010-03-11 22:23:46 -080082import android.util.Slog;
Adam Powell14874662013-07-18 19:42:41 -070083import android.util.SuperNotCalledException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import android.view.Display;
Romain Guy52339202010-09-03 16:04:46 -070085import android.view.HardwareRenderer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086import android.view.View;
87import android.view.ViewDebug;
88import android.view.ViewManager;
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070089import android.view.ViewRootImpl;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090import android.view.Window;
91import android.view.WindowManager;
Jeff Brown98365d72012-08-19 20:30:52 -070092import android.view.WindowManagerGlobal;
Jason Samsa6f338c2012-02-24 16:22:16 -080093import android.renderscript.RenderScript;
Kenny Root8b514752013-02-04 09:35:16 -080094import android.security.AndroidKeyStoreProvider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Dianne Hackborn91097de2014-04-04 18:02:06 -070096import com.android.internal.app.IVoiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097import com.android.internal.os.BinderInternal;
98import com.android.internal.os.RuntimeInit;
Bob Leee5408332009-09-04 18:31:17 -070099import com.android.internal.os.SamplingProfilerIntegration;
Dianne Hackborn8c841092013-06-24 13:46:13 -0700100import com.android.internal.util.FastPrintWriter;
Kenny Root12e75222013-04-23 22:34:24 -0700101import com.android.org.conscrypt.OpenSSLSocketImpl;
Robin Lee3d076af2014-04-25 14:57:49 +0100102import com.android.org.conscrypt.TrustedCertificateStore;
Jeff Sharkeydd97f422013-10-08 17:01:30 -0700103import com.google.android.collect.Lists;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105import java.io.File;
106import java.io.FileDescriptor;
107import java.io.FileOutputStream;
Dianne Hackborn9c8dd552009-06-23 19:22:52 -0700108import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109import java.io.PrintWriter;
110import java.lang.ref.WeakReference;
Robert Greenwalt03595d02010-11-02 14:08:23 -0700111import java.net.InetAddress;
Kenny Root8b514752013-02-04 09:35:16 -0800112import java.security.Security;
Narayan Kamathccb2a082013-12-19 14:49:36 +0000113import java.text.DateFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114import java.util.ArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115import java.util.List;
116import java.util.Locale;
117import java.util.Map;
Kenny Roote6585b32013-12-13 12:00:26 -0800118import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119import java.util.TimeZone;
120import java.util.regex.Pattern;
121
Geremy Condrab7faaf42012-09-19 18:07:42 -0700122import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700123import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800124import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400125import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800126import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700127import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700128import dalvik.system.VMRuntime;
Bob Leee5408332009-09-04 18:31:17 -0700129
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700130final class RemoteServiceException extends AndroidRuntimeException {
131 public RemoteServiceException(String msg) {
132 super(msg);
133 }
134}
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136/**
137 * This manages the execution of the main thread in an
138 * application process, scheduling and executing activities,
139 * broadcasts, and other operations on it as the activity
140 * manager requests.
141 *
142 * {@hide}
143 */
144public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700145 /** @hide */
146 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700147 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700148 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700149 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700150 /** @hide */
151 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700152 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700153 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700154 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800155 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700156 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700157 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
159 private static final Pattern PATTERN_SEMICOLON = Pattern.compile(";");
160 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
161 private static final int LOG_ON_PAUSE_CALLED = 30021;
162 private static final int LOG_ON_RESUME_CALLED = 30022;
163
Jeff Browndefd4a62014-03-10 21:24:37 -0700164 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700165
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700166 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700168 final ApplicationThread mAppThread = new ApplicationThread();
169 final Looper mLooper = Looper.myLooper();
170 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700171 final ArrayMap<IBinder, ActivityClientRecord> mActivities
172 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700173 // List of new activities (via ActivityRecord.nextIdle) that should
174 // be reported when next we idle.
175 ActivityClientRecord mNewActivities = null;
176 // Number of activities that are currently visible on-screen.
177 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700178 final ArrayMap<IBinder, Service> mServices
179 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700180 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700181 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700182 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700183 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700185 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700186 Application mInitialApplication;
187 final ArrayList<Application> mAllApplications
188 = new ArrayList<Application>();
189 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700190 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800191 /** Reference to singleton {@link ActivityThread} */
192 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700194 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700195 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700196 String[] mInstrumentationSplitAppDirs = null;
197 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700198 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700199 String[] mInstrumentedSplitAppDirs = null;
200 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700201 boolean mSystemThread = false;
202 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700203 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700205 // These can be accessed by multiple threads; mPackages is the lock.
206 // XXX For now we keep around information about all packages we have
207 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800208 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700209 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800210 // which means this lock gets held while the activity and window managers
211 // holds their own lock. Thus you MUST NEVER call back into the activity manager
212 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700213 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
214 = new ArrayMap<String, WeakReference<LoadedApk>>();
215 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
216 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700217 final ArrayList<ActivityClientRecord> mRelaunchingActivities
218 = new ArrayList<ActivityClientRecord>();
219 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
Craig Mautner88c05892013-06-28 09:47:45 -0700221 private final ResourcesManager mResourcesManager;
222
Jeff Sharkey6d515712012-09-20 16:06:08 -0700223 private static final class ProviderKey {
224 final String authority;
225 final int userId;
226
227 public ProviderKey(String authority, int userId) {
228 this.authority = authority;
229 this.userId = userId;
230 }
231
232 @Override
233 public boolean equals(Object o) {
234 if (o instanceof ProviderKey) {
235 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800236 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700237 }
238 return false;
239 }
240
241 @Override
242 public int hashCode() {
243 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
244 }
245 }
246
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700247 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700248 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
249 = new ArrayMap<ProviderKey, ProviderClientRecord>();
250 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
251 = new ArrayMap<IBinder, ProviderRefCount>();
252 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
253 = new ArrayMap<IBinder, ProviderClientRecord>();
254 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
255 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700257 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
258 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600259
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700260 final GcIdler mGcIdler = new GcIdler();
261 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700263 static Handler sMainThreadHandler; // set once in main()
264
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800265 Bundle mCoreSettings = null;
266
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400267 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700269 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 Intent intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700271 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700273 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 Activity activity;
275 Window window;
276 Activity parent;
277 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700278 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 boolean paused;
280 boolean stopped;
281 boolean hideForNow;
282 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700283 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700284 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285
Jeff Hao1b012d32014-08-20 10:35:34 -0700286 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400289 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700290 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292 List<ResultInfo> pendingResults;
293 List<Intent> pendingIntents;
294
295 boolean startsNotResumed;
296 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800297 int pendingConfigChanges;
298 boolean onlyLocalRequest;
299
300 View mPendingRemoveWindow;
301 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700303 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 parent = null;
305 embeddedID = null;
306 paused = false;
307 stopped = false;
308 hideForNow = false;
309 nextIdle = null;
310 }
311
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800312 public boolean isPreHoneycomb() {
313 if (activity != null) {
314 return activity.getApplicationInfo().targetSdkVersion
315 < android.os.Build.VERSION_CODES.HONEYCOMB;
316 }
317 return false;
318 }
319
Craig Mautnera0026042014-04-23 11:45:37 -0700320 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700321 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700322 }
323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700325 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 return "ActivityRecord{"
327 + Integer.toHexString(System.identityHashCode(this))
328 + " token=" + token + " " + (componentName == null
329 ? "no component name" : componentName.toShortString())
330 + "}";
331 }
332 }
333
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700334 final class ProviderClientRecord {
335 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 final IContentProvider mProvider;
337 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700338 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700340 ProviderClientRecord(String[] names, IContentProvider provider,
341 ContentProvider localProvider,
342 IActivityManager.ContentProviderHolder holder) {
343 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 mProvider = provider;
345 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700346 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
348 }
349
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400350 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 List<Intent> intents;
352 IBinder token;
353 public String toString() {
354 return "NewIntentData{intents=" + intents + " token=" + token + "}";
355 }
356 }
357
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400358 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700359 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700360 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
361 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
362 token, sendingUser);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700363 this.intent = intent;
364 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700365
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 Intent intent;
367 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400368 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 public String toString() {
370 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700371 info.packageName + " resultCode=" + getResultCode()
372 + " resultData=" + getResultData() + " resultExtras="
373 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375 }
376
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700378 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400379 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700380 int backupMode;
381 public String toString() {
382 return "CreateBackupAgentData{appInfo=" + appInfo
383 + " backupAgent=" + appInfo.backupAgentName
384 + " mode=" + backupMode + "}";
385 }
386 }
Bob Leee5408332009-09-04 18:31:17 -0700387
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400388 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 IBinder token;
390 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400391 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 Intent intent;
393 public String toString() {
394 return "CreateServiceData{token=" + token + " className="
395 + info.name + " packageName=" + info.packageName
396 + " intent=" + intent + "}";
397 }
398 }
399
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400400 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 IBinder token;
402 Intent intent;
403 boolean rebind;
404 public String toString() {
405 return "BindServiceData{token=" + token + " intent=" + intent + "}";
406 }
407 }
408
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400409 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700411 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700413 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 Intent args;
415 public String toString() {
416 return "ServiceArgsData{token=" + token + " startId=" + startId
417 + " args=" + args + "}";
418 }
419 }
420
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400421 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700422 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 String processName;
424 ApplicationInfo appInfo;
425 List<ProviderInfo> providers;
426 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 Bundle instrumentationArgs;
428 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800429 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800431 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700432 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700433 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400435 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700436
437 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700438 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 public String toString() {
441 return "AppBindData{appInfo=" + appInfo + "}";
442 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700443 }
444
445 static final class Profiler {
446 String profileFile;
447 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700448 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700449 boolean autoStopProfiler;
450 boolean profiling;
451 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700452 public void setProfiler(ProfilerInfo profilerInfo) {
453 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700454 if (profiling) {
455 if (fd != null) {
456 try {
457 fd.close();
458 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700459 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700460 }
461 }
462 return;
463 }
464 if (profileFd != null) {
465 try {
466 profileFd.close();
467 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700468 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700469 }
470 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700471 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700472 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700473 samplingInterval = profilerInfo.samplingInterval;
474 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700475 }
476 public void startProfiling() {
477 if (profileFd == null || profiling) {
478 return;
479 }
480 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700481 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
482 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700483 profiling = true;
484 } catch (RuntimeException e) {
485 Slog.w(TAG, "Profiling failed on path " + profileFile);
486 try {
487 profileFd.close();
488 profileFd = null;
489 } catch (IOException e2) {
490 Slog.w(TAG, "Failure closing profile fd", e2);
491 }
492 }
493 }
494 public void stopProfiling() {
495 if (profiling) {
496 profiling = false;
497 Debug.stopMethodTracing();
498 if (profileFd != null) {
499 try {
500 profileFd.close();
501 } catch (IOException e) {
502 }
503 }
504 profileFd = null;
505 profileFile = null;
506 }
507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400510 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700511 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700512 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800513 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 }
516
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400517 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 IBinder token;
519 List<ResultInfo> results;
520 public String toString() {
521 return "ResultData{token=" + token + " results" + results + "}";
522 }
523 }
524
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400525 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800526 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 String what;
528 String who;
529 }
530
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400531 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700532 String path;
533 ParcelFileDescriptor fd;
534 }
535
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400536 static final class UpdateCompatibilityData {
537 String pkg;
538 CompatibilityInfo info;
539 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800540
Adam Skorydfc7fd72013-08-05 19:23:41 -0700541 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800542 IBinder activityToken;
543 IBinder requestToken;
544 int requestType;
545 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700546
Romain Guy65b345f2011-07-27 18:51:50 -0700547 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700548
Romain Guy65b345f2011-07-27 18:51:50 -0700549 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700550 private static final String ONE_COUNT_COLUMN = "%21s %8d";
551 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700552 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700553
Dianne Hackborna413dc02013-07-12 12:02:55 -0700554 private int mLastProcessState = -1;
555
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700556 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700557 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700558 if (mPendingConfiguration == null ||
559 mPendingConfiguration.isOtherSeqNewer(config)) {
560 mPendingConfiguration = config;
561 }
562 }
563 }
564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700566 boolean userLeaving, int configChanges, boolean dontReport) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700567 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
569 token,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700570 (userLeaving ? 1 : 0) | (dontReport ? 2 : 0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 configChanges);
572 }
573
574 public final void scheduleStopActivity(IBinder token, boolean showWindow,
575 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700576 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
578 token, 0, configChanges);
579 }
580
581 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700582 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
584 token);
585 }
586
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800587 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700588 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800589 }
590
Dianne Hackborna413dc02013-07-12 12:02:55 -0700591 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800592 boolean isForward, Bundle resumeArgs) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700593 updateProcessState(processState, false);
Craig Mautner233ceee2014-05-09 17:05:11 -0700594 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 }
596
597 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
598 ResultData res = new ResultData();
599 res.token = token;
600 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700601 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603
604 // we use token to identify this activity without having to send the
605 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700606 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700607 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Craig Mautnera0026042014-04-23 11:45:37 -0700608 IVoiceInteractor voiceInteractor, int procState, Bundle state,
609 PersistableBundle persistentState, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700610 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
Jeff Hao1b012d32014-08-20 10:35:34 -0700611 ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700612
613 updateProcessState(procState, false);
614
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700615 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616
617 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700618 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 r.intent = intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700620 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400622 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700624 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625
626 r.pendingResults = pendingResults;
627 r.pendingIntents = pendingNewIntents;
628
629 r.startsNotResumed = notResumed;
630 r.isForward = isForward;
631
Jeff Hao1b012d32014-08-20 10:35:34 -0700632 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700633
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700634 updatePendingConfiguration(curConfig);
635
Jeff Brown9ef09972013-10-15 20:49:59 -0700636 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800637 }
638
639 public final void scheduleRelaunchActivity(IBinder token,
640 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800641 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800642 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
643 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644 }
645
646 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
647 NewIntentData data = new NewIntentData();
648 data.intents = intents;
649 data.token = token;
650
Jeff Brown9ef09972013-10-15 20:49:59 -0700651 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 }
653
654 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
655 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700656 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 configChanges);
658 }
659
660 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400661 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700662 boolean sync, int sendingUser, int processState) {
663 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700664 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700665 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800666 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400667 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700668 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 }
670
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400671 public final void scheduleCreateBackupAgent(ApplicationInfo app,
672 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700673 CreateBackupAgentData d = new CreateBackupAgentData();
674 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400675 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700676 d.backupMode = backupMode;
677
Jeff Brown9ef09972013-10-15 20:49:59 -0700678 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700679 }
680
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400681 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
682 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700683 CreateBackupAgentData d = new CreateBackupAgentData();
684 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400685 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700686
Jeff Brown9ef09972013-10-15 20:49:59 -0700687 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700688 }
689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700691 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
692 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 CreateServiceData s = new CreateServiceData();
694 s.token = token;
695 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400696 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697
Jeff Brown9ef09972013-10-15 20:49:59 -0700698 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 }
700
701 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700702 boolean rebind, int processState) {
703 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 BindServiceData s = new BindServiceData();
705 s.token = token;
706 s.intent = intent;
707 s.rebind = rebind;
708
Amith Yamasani742a6712011-05-04 14:49:28 -0700709 if (DEBUG_SERVICE)
710 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
711 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700712 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 public final void scheduleUnbindService(IBinder token, Intent intent) {
716 BindServiceData s = new BindServiceData();
717 s.token = token;
718 s.intent = intent;
719
Jeff Brown9ef09972013-10-15 20:49:59 -0700720 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
722
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700723 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700724 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 ServiceArgsData s = new ServiceArgsData();
726 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700727 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700729 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 s.args = args;
731
Jeff Brown9ef09972013-10-15 20:49:59 -0700732 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734
735 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700736 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
738
Jeff Hao1b012d32014-08-20 10:35:34 -0700739 public final void bindApplication(String processName, ApplicationInfo appInfo,
740 List<ProviderInfo> providers, ComponentName instrumentationName,
741 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
742 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800743 IUiAutomationConnection instrumentationUiConnection, int debugMode,
744 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
745 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
746 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747
748 if (services != null) {
749 // Setup the service cache in the ServiceManager
750 ServiceManager.initServiceCache(services);
751 }
752
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800753 setCoreSettings(coreSettings);
754
Dave Allison0efbd9a2014-01-30 14:19:51 -0800755 /*
756 * Two possible indications that this package could be
757 * sharing its runtime with other packages:
758 *
759 * 1.) the sharedUserId attribute is set in the manifest,
760 * indicating a request to share a VM with other
761 * packages with the same sharedUserId.
762 *
763 * 2.) the application element of the manifest has an
764 * attribute specifying a non-default process name,
765 * indicating the desire to run in another packages VM.
766 *
767 * If sharing is enabled we do not have a unique application
768 * in a process and therefore cannot rely on the package
769 * name inside the runtime.
770 */
771 IPackageManager pm = getPackageManager();
772 android.content.pm.PackageInfo pi = null;
773 try {
774 pi = pm.getPackageInfo(appInfo.packageName, 0, UserHandle.myUserId());
775 } catch (RemoteException e) {
776 }
777 if (pi != null) {
778 boolean sharedUserIdSet = (pi.sharedUserId != null);
779 boolean processNameNotDefault =
780 (pi.applicationInfo != null &&
781 !appInfo.packageName.equals(pi.applicationInfo.processName));
782 boolean sharable = (sharedUserIdSet || processNameNotDefault);
783
784 // Tell the VMRuntime about the application, unless it is shared
785 // inside a process.
786 if (!sharable) {
787 VMRuntime.registerAppInfo(appInfo.packageName, appInfo.dataDir,
788 appInfo.processName);
789 }
790 }
Dave Allison24bafbc2013-11-13 17:15:50 -0800791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 AppBindData data = new AppBindData();
793 data.processName = processName;
794 data.appInfo = appInfo;
795 data.providers = providers;
796 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 data.instrumentationArgs = instrumentationArgs;
798 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800799 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800801 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700802 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700803 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400805 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700806 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700807 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
809
810 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700811 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 }
813
Christopher Tate5e1ab332009-09-01 20:32:49 -0700814 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700815 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700816 }
817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700819 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700820 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 }
822
823 public void updateTimeZone() {
824 TimeZone.setDefault(null);
825 }
826
Robert Greenwalt03595d02010-11-02 14:08:23 -0700827 public void clearDnsCache() {
828 // a non-standard API to get this to libcore
829 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400830 // Allow libcore to perform the necessary actions as it sees fit upon a network
831 // configuration change.
832 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700833 }
834
Jason Monk83520b92014-05-09 15:16:06 -0400835 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Jason Monk602b2322013-07-03 17:04:33 -0400836 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700837 }
838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 public void processInBackground() {
840 mH.removeMessages(H.GC_WHEN_IDLE);
841 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
842 }
843
844 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700845 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700846 try {
847 data.fd = ParcelFileDescriptor.dup(fd);
848 data.token = servicetoken;
849 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700850 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700851 } catch (IOException e) {
852 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 }
854 }
855
856 // This function exists to make sure all receiver dispatching is
857 // correctly ordered, since these are one-way calls and the binder driver
858 // applies transaction ordering per object for such calls.
859 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700860 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700861 boolean sticky, int sendingUser, int processState) throws RemoteException {
862 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700863 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
864 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
Bob Leee5408332009-09-04 18:31:17 -0700866
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700868 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870
871 public void scheduleActivityConfigurationChanged(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700872 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 }
874
Jeff Hao1b012d32014-08-20 10:35:34 -0700875 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
876 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800877 }
878
Andy McFadden824c5102010-07-09 16:26:57 -0700879 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
880 DumpHeapData dhd = new DumpHeapData();
881 dhd.path = path;
882 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700883 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700884 }
885
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700886 public void setSchedulingGroup(int group) {
887 // Note: do this immediately, since going into the foreground
888 // should happen regardless of what pending work we have to do
889 // and the activity manager will wait for us to report back that
890 // we are done before sending us to the background.
891 try {
892 Process.setProcessGroup(Process.myPid(), group);
893 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800894 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700895 }
896 }
Bob Leee5408332009-09-04 18:31:17 -0700897
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700898 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700899 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700900 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700901
902 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700903 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700904 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700905
Dianne Hackborn30d71892010-12-11 10:37:55 -0800906 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
907 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700908 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700909 try {
910 data.fd = ParcelFileDescriptor.dup(fd);
911 data.token = activitytoken;
912 data.prefix = prefix;
913 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700914 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700915 } catch (IOException e) {
916 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700917 }
918 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700919
Marco Nelissen18cb2872011-11-15 11:19:53 -0800920 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
921 String[] args) {
922 DumpComponentInfo data = new DumpComponentInfo();
923 try {
924 data.fd = ParcelFileDescriptor.dup(fd);
925 data.token = providertoken;
926 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700927 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800928 } catch (IOException e) {
929 Slog.w(TAG, "dumpProvider failed", e);
930 }
931 }
932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700934 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700935 boolean dumpFullInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700937 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 try {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700939 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700940 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700941 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700942 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700943 }
944
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700945 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700946 boolean dumpFullInfo, boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 long nativeMax = Debug.getNativeHeapSize() / 1024;
948 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
949 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 Runtime runtime = Runtime.getRuntime();
952
953 long dalvikMax = runtime.totalMemory() / 1024;
954 long dalvikFree = runtime.freeMemory() / 1024;
955 long dalvikAllocated = dalvikMax - dalvikFree;
956 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700957 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700958 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
959 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 int globalAssetCount = AssetManager.getGlobalAssetCount();
961 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
962 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
963 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
964 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700965 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800966 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700967
Dianne Hackborne77187d2013-10-25 16:32:41 -0700968 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, Process.myPid(),
969 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
970 nativeMax, nativeAllocated, nativeFree,
971 dalvikMax, dalvikAllocated, dalvikFree);
972
Dianne Hackbornb437e092011-08-05 17:50:29 -0700973 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 // NOTE: if you change anything significant below, also consider changing
975 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 // Object counts
978 pw.print(viewInstanceCount); pw.print(',');
979 pw.print(viewRootInstanceCount); pw.print(',');
980 pw.print(appContextInstanceCount); pw.print(',');
981 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 pw.print(globalAssetCount); pw.print(',');
984 pw.print(globalAssetManagerCount); pw.print(',');
985 pw.print(binderLocalObjectCount); pw.print(',');
986 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 pw.print(binderDeathObjectCount); pw.print(',');
989 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800992 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800993 pw.print(stats.memoryUsed / 1024); pw.print(',');
994 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700995 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800996 for (int i = 0; i < stats.dbStats.size(); i++) {
997 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700998 pw.print(','); pw.print(dbStats.dbName);
999 pw.print(','); pw.print(dbStats.pageSize);
1000 pw.print(','); pw.print(dbStats.dbSize);
1001 pw.print(','); pw.print(dbStats.lookaside);
1002 pw.print(','); pw.print(dbStats.cache);
1003 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001004 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001005 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001006
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001007 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
Bob Leee5408332009-09-04 18:31:17 -07001009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 pw.println(" ");
1011 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001012 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 viewRootInstanceCount);
1014
1015 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1016 "Activities:", activityInstanceCount);
1017
1018 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1019 "AssetManagers:", globalAssetManagerCount);
1020
1021 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1022 "Proxy Binders:", binderProxyObjectCount);
1023 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1024
1025 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 // SQLite mem info
1028 pw.println(" ");
1029 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001030 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1031 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1032 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001033 pw.println(" ");
1034 int N = stats.dbStats.size();
1035 if (N > 0) {
1036 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001037 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1038 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001039 for (int i = 0; i < N; i++) {
1040 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001041 printRow(pw, DB_INFO_FORMAT,
1042 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1043 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1044 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1045 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001046 }
1047 }
Bob Leee5408332009-09-04 18:31:17 -07001048
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001049 // Asset details.
1050 String assetAlloc = AssetManager.getAssetAllocations();
1051 if (assetAlloc != null) {
1052 pw.println(" ");
1053 pw.println(" Asset Allocations");
1054 pw.print(assetAlloc);
1055 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001056 }
1057
1058 @Override
1059 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1060 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001061 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063
Jeff Brown6754ba22011-12-14 20:20:01 -08001064 @Override
1065 public void dumpDbInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001066 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001067 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1068 SQLiteDebug.dump(printer, args);
1069 pw.flush();
1070 }
1071
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001072 @Override
1073 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001074 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001075 }
1076
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001077 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001078 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001079 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001080 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001081 cmd.activityToken = activityToken;
1082 cmd.requestToken = requestToken;
1083 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001084 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001085 }
1086
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001087 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001088 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001089 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001090
1091 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1092 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1093 ucd.pkg = pkg;
1094 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001095 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001096 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001097
1098 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001099 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001100 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001101
Craig Mautner5eda9b32013-07-02 11:58:16 -07001102 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001103 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001104 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001105
Craig Mautnereb8abf72014-07-02 15:04:09 -07001106 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1107 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1108 new Pair<IBinder, ActivityOptions>(token, options));
1109 }
1110
Dianne Hackborna413dc02013-07-12 12:02:55 -07001111 public void setProcessState(int state) {
1112 updateProcessState(state, true);
1113 }
1114
1115 public void updateProcessState(int processState, boolean fromIpc) {
1116 synchronized (this) {
1117 if (mLastProcessState != processState) {
1118 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001119 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1120 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1121 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1122 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1123 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1124 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1125 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1126 }
1127 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001128 if (false) {
1129 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1130 + (fromIpc ? " (from ipc": ""));
1131 }
1132 }
1133 }
1134 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001135
1136 @Override
1137 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001138 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001139 }
Narayan Kamathccb2a082013-12-19 14:49:36 +00001140
1141 @Override
1142 public final void updateTimePrefs(boolean is24Hour) {
1143 DateFormat.set24HourTimePref(is24Hour);
1144 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001145
1146 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001147 public void scheduleCancelVisibleBehind(IBinder token) {
1148 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001149 }
1150
1151 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001152 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1153 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001154 }
Craig Mautner8746a472014-07-24 15:12:54 -07001155
1156 public void scheduleEnterAnimationComplete(IBinder token) {
1157 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 }
1160
Romain Guy65b345f2011-07-27 18:51:50 -07001161 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 public static final int LAUNCH_ACTIVITY = 100;
1163 public static final int PAUSE_ACTIVITY = 101;
1164 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1165 public static final int STOP_ACTIVITY_SHOW = 103;
1166 public static final int STOP_ACTIVITY_HIDE = 104;
1167 public static final int SHOW_WINDOW = 105;
1168 public static final int HIDE_WINDOW = 106;
1169 public static final int RESUME_ACTIVITY = 107;
1170 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001171 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 public static final int BIND_APPLICATION = 110;
1173 public static final int EXIT_APPLICATION = 111;
1174 public static final int NEW_INTENT = 112;
1175 public static final int RECEIVER = 113;
1176 public static final int CREATE_SERVICE = 114;
1177 public static final int SERVICE_ARGS = 115;
1178 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 public static final int CONFIGURATION_CHANGED = 118;
1181 public static final int CLEAN_UP_CONTEXT = 119;
1182 public static final int GC_WHEN_IDLE = 120;
1183 public static final int BIND_SERVICE = 121;
1184 public static final int UNBIND_SERVICE = 122;
1185 public static final int DUMP_SERVICE = 123;
1186 public static final int LOW_MEMORY = 124;
1187 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1188 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001189 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001190 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001191 public static final int DESTROY_BACKUP_AGENT = 129;
1192 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001193 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001194 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001195 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001196 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001197 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001198 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001199 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001200 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001201 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001202 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001203 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001204 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001205 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001206 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001207 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001208 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001209 public static final int CANCEL_VISIBLE_BEHIND = 147;
1210 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001211 public static final int ENTER_ANIMATION_COMPLETE = 149;
Narayan Kamathccb2a082013-12-19 14:49:36 +00001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001214 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 switch (code) {
1216 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1217 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1218 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1219 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1220 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1221 case SHOW_WINDOW: return "SHOW_WINDOW";
1222 case HIDE_WINDOW: return "HIDE_WINDOW";
1223 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1224 case SEND_RESULT: return "SEND_RESULT";
1225 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1226 case BIND_APPLICATION: return "BIND_APPLICATION";
1227 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1228 case NEW_INTENT: return "NEW_INTENT";
1229 case RECEIVER: return "RECEIVER";
1230 case CREATE_SERVICE: return "CREATE_SERVICE";
1231 case SERVICE_ARGS: return "SERVICE_ARGS";
1232 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1234 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1235 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1236 case BIND_SERVICE: return "BIND_SERVICE";
1237 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1238 case DUMP_SERVICE: return "DUMP_SERVICE";
1239 case LOW_MEMORY: return "LOW_MEMORY";
1240 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1241 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001242 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001243 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1244 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001245 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001246 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001247 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001248 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001249 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001250 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001251 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001252 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001253 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001254 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001255 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001256 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001257 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001258 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001259 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001260 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001261 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001262 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1263 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001264 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 }
1266 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001267 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 }
1269 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001270 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 switch (msg.what) {
1272 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001273 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001274 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275
1276 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001277 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001278 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001279 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 } break;
1281 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001282 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001283 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001284 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001285 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 } break;
1287 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001288 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001289 handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
1290 (msg.arg1&2) != 0);
Bob Leee5408332009-09-04 18:31:17 -07001291 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001292 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 break;
1294 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001295 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001296 handlePauseActivity((IBinder)msg.obj, true, (msg.arg1&1) != 0, msg.arg2,
1297 (msg.arg1&1) != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001298 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 break;
1300 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001301 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 break;
1305 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001308 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 break;
1310 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001316 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001318 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 break;
1320 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001321 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Craig Mautner233ceee2014-05-09 17:05:11 -07001322 handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001323 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 break;
1325 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001328 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 break;
1330 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1333 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 break;
1336 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 AppBindData data = (AppBindData)msg.obj;
1339 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001340 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 break;
1342 case EXIT_APPLICATION:
1343 if (mInitialApplication != null) {
1344 mInitialApplication.onTerminate();
1345 }
1346 Looper.myLooper().quit();
1347 break;
1348 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001351 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 break;
1353 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001354 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001356 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001357 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 break;
1359 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001360 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001362 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363 break;
1364 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001365 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001367 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 break;
1369 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001370 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001372 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 break;
1374 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001375 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001377 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 break;
1379 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001380 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001382 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001385 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001386 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001387 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001388 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001389 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 break;
1391 case CLEAN_UP_CONTEXT:
1392 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1393 cci.context.performFinalCleanup(cci.who, cci.what);
1394 break;
1395 case GC_WHEN_IDLE:
1396 scheduleGcIdler();
1397 break;
1398 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001399 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001400 break;
1401 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001402 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001404 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 break;
1406 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001407 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001409 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001411 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001412 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001413 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001414 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001415 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001416 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001417 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001418 break;
1419 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001420 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001421 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001422 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001423 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001424 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001425 Process.killProcess(Process.myPid());
1426 break;
1427 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001428 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001429 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001431 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001432 case ENABLE_JIT:
1433 ensureJitEnabled();
1434 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001435 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001436 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001437 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001438 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001439 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001440 case SCHEDULE_CRASH:
1441 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001442 case DUMP_HEAP:
1443 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1444 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001445 case DUMP_ACTIVITY:
1446 handleDumpActivity((DumpComponentInfo)msg.obj);
1447 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001448 case DUMP_PROVIDER:
1449 handleDumpProvider((DumpComponentInfo)msg.obj);
1450 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001451 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001452 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001453 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001454 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001455 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001456 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001457 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001458 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001459 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001460 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001461 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1462 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001463 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001464 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001465 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001466 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001467 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001468 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001469 case UNSTABLE_PROVIDER_DIED:
1470 handleUnstableProviderDied((IBinder)msg.obj, false);
1471 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001472 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1473 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001474 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001475 case TRANSLUCENT_CONVERSION_COMPLETE:
1476 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1477 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001478 case INSTALL_PROVIDER:
1479 handleInstallProvider((ProviderInfo) msg.obj);
1480 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001481 case ON_NEW_ACTIVITY_OPTIONS:
1482 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1483 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001484 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001485 case CANCEL_VISIBLE_BEHIND:
1486 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001487 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001488 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1489 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001490 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001491 case ENTER_ANIMATION_COMPLETE:
1492 handleEnterAnimationComplete((IBinder) msg.obj);
1493 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001495 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
Bob Leee5408332009-09-04 18:31:17 -07001497
Brian Carlstromed7e0072011-03-24 13:27:57 -07001498 private void maybeSnapshot() {
1499 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001500 // convert the *private* ActivityThread.PackageInfo to *public* known
1501 // android.content.pm.PackageInfo
1502 String packageName = mBoundApplication.info.mPackageName;
1503 android.content.pm.PackageInfo packageInfo = null;
1504 try {
1505 Context context = getSystemContext();
1506 if(context == null) {
1507 Log.e(TAG, "cannot get a valid context");
1508 return;
1509 }
1510 PackageManager pm = context.getPackageManager();
1511 if(pm == null) {
1512 Log.e(TAG, "cannot get a valid PackageManager");
1513 return;
1514 }
1515 packageInfo = pm.getPackageInfo(
1516 packageName, PackageManager.GET_ACTIVITIES);
1517 } catch (NameNotFoundException e) {
1518 Log.e(TAG, "cannot get package info for " + packageName, e);
1519 }
1520 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001521 }
1522 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001523 }
1524
Romain Guy65b345f2011-07-27 18:51:50 -07001525 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001526 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001528 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001529 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001530 if (mBoundApplication != null && mProfiler.profileFd != null
1531 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001532 stopProfiling = true;
1533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 if (a != null) {
1535 mNewActivities = null;
1536 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001537 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001539 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 TAG, "Reporting idle of " + a +
1541 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001542 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001543 if (a.activity != null && !a.activity.mFinished) {
1544 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001545 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001546 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001548 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 }
1550 }
1551 prev = a;
1552 a = a.nextIdle;
1553 prev.nextIdle = null;
1554 } while (a != null);
1555 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001556 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001557 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001558 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001559 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 return false;
1561 }
1562 }
1563
1564 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001565 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 public final boolean queueIdle() {
1567 doGcIfNeeded();
1568 return false;
1569 }
1570 }
1571
Romain Guy65b345f2011-07-27 18:51:50 -07001572 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001573 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575
Romain Guy65b345f2011-07-27 18:51:50 -07001576 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001577 ActivityThread am = currentActivityThread();
1578 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001579 ? am.mBoundApplication.appInfo.packageName : null;
1580 }
1581
1582 public static String currentProcessName() {
1583 ActivityThread am = currentActivityThread();
1584 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001585 ? am.mBoundApplication.processName : null;
1586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587
Romain Guy65b345f2011-07-27 18:51:50 -07001588 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001589 ActivityThread am = currentActivityThread();
1590 return am != null ? am.mInitialApplication : null;
1591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001593 public static IPackageManager getPackageManager() {
1594 if (sPackageManager != null) {
1595 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1596 return sPackageManager;
1597 }
1598 IBinder b = ServiceManager.getService("package");
1599 //Slog.v("PackageManager", "default service binder = " + b);
1600 sPackageManager = IPackageManager.Stub.asInterface(b);
1601 //Slog.v("PackageManager", "default service = " + sPackageManager);
1602 return sPackageManager;
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604
Romain Guy65b345f2011-07-27 18:51:50 -07001605 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001606 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1607 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001608 if (config == null) {
1609 return null;
1610 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001611 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001612 mMainThreadConfig.setTo(config);
1613 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001614 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001615 }
1616 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001619 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001620 * Creates the top level resources for the given package.
1621 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001622 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1623 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001624 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001625 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
1626 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001627 }
1628
1629 final Handler getHandler() {
1630 return mH;
1631 }
1632
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001633 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1634 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001635 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1636 }
1637
1638 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1639 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001640 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001641 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1643 ref = mPackages.get(packageName);
1644 } else {
1645 ref = mResourcePackages.get(packageName);
1646 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001647 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001648 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001649 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1650 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 if (packageInfo != null && (packageInfo.mResources == null
1652 || packageInfo.mResources.getAssets().isUpToDate())) {
1653 if (packageInfo.isSecurityViolation()
1654 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1655 throw new SecurityException(
1656 "Requesting code from " + packageName
1657 + " to be run in process "
1658 + mBoundApplication.processName
1659 + "/" + mBoundApplication.appInfo.uid);
1660 }
1661 return packageInfo;
1662 }
1663 }
1664
1665 ApplicationInfo ai = null;
1666 try {
1667 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001668 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001670 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 }
1672
1673 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001674 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 }
1676
1677 return null;
1678 }
1679
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001680 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1681 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1683 boolean securityViolation = includeCode && ai.uid != 0
1684 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001685 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001686 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001687 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1689 |Context.CONTEXT_IGNORE_SECURITY))
1690 == Context.CONTEXT_INCLUDE_CODE) {
1691 if (securityViolation) {
1692 String msg = "Requesting code from " + ai.packageName
1693 + " (with uid " + ai.uid + ")";
1694 if (mBoundApplication != null) {
1695 msg = msg + " to be run in process "
1696 + mBoundApplication.processName + " (with uid "
1697 + mBoundApplication.appInfo.uid + ")";
1698 }
1699 throw new SecurityException(msg);
1700 }
1701 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001702 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1703 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001704 }
1705
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001706 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1707 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001708 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 }
1710
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001711 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001712 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001713 WeakReference<LoadedApk> ref;
1714 if (includeCode) {
1715 ref = mPackages.get(packageName);
1716 } else {
1717 ref = mResourcePackages.get(packageName);
1718 }
1719 return ref != null ? ref.get() : null;
1720 }
1721 }
1722
Romain Guy65b345f2011-07-27 18:51:50 -07001723 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001724 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1725 boolean registerPackage) {
Craig Mautner88c05892013-06-28 09:47:45 -07001726 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001727 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 if (includeCode) {
1729 ref = mPackages.get(aInfo.packageName);
1730 } else {
1731 ref = mResourcePackages.get(aInfo.packageName);
1732 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001733 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001734 if (packageInfo == null || (packageInfo.mResources != null
1735 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001736 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 : "Loading resource-only package ") + aInfo.packageName
1738 + " (in " + (mBoundApplication != null
1739 ? mBoundApplication.processName : null)
1740 + ")");
1741 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001742 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001744 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 if (includeCode) {
1746 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001747 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 } else {
1749 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001750 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 }
1752 }
1753 return packageInfo;
1754 }
1755 }
1756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001758 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760
1761 public ApplicationThread getApplicationThread()
1762 {
1763 return mAppThread;
1764 }
1765
1766 public Instrumentation getInstrumentation()
1767 {
1768 return mInstrumentation;
1769 }
1770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001772 return mProfiler != null && mProfiler.profileFile != null
1773 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775
1776 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001777 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 }
1779
1780 public Looper getLooper() {
1781 return mLooper;
1782 }
1783
1784 public Application getApplication() {
1785 return mInitialApplication;
1786 }
Bob Leee5408332009-09-04 18:31:17 -07001787
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001788 public String getProcessName() {
1789 return mBoundApplication.processName;
1790 }
Bob Leee5408332009-09-04 18:31:17 -07001791
Dianne Hackborn21556372010-02-04 16:34:40 -08001792 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 synchronized (this) {
1794 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001795 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001797 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
1800
Narayan Kamath29564cd2014-08-07 10:57:40 +01001801 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001802 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001803 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001804
Narayan Kamath3480ab42014-08-15 18:11:04 +01001805 // The code package for "android" in the system server needs
1806 // to be the system context's package.
1807 mPackages.put("android", new WeakReference<LoadedApk>(getSystemContext().mPackageInfo));
1808
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001809 // give ourselves a default profiler
1810 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001811 }
1812 }
1813
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001814 void ensureJitEnabled() {
1815 if (!mJitEnabled) {
1816 mJitEnabled = true;
1817 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1818 }
1819 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001820
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 void scheduleGcIdler() {
1822 if (!mGcIdlerScheduled) {
1823 mGcIdlerScheduled = true;
1824 Looper.myQueue().addIdleHandler(mGcIdler);
1825 }
1826 mH.removeMessages(H.GC_WHEN_IDLE);
1827 }
1828
1829 void unscheduleGcIdler() {
1830 if (mGcIdlerScheduled) {
1831 mGcIdlerScheduled = false;
1832 Looper.myQueue().removeIdleHandler(mGcIdler);
1833 }
1834 mH.removeMessages(H.GC_WHEN_IDLE);
1835 }
1836
1837 void doGcIfNeeded() {
1838 mGcIdlerScheduled = false;
1839 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001840 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 // + "m now=" + now);
1842 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001843 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 BinderInternal.forceGc("bg");
1845 }
1846 }
1847
Dianne Hackborne77187d2013-10-25 16:32:41 -07001848 private static final String HEAP_FULL_COLUMN
1849 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1850 private static final String HEAP_COLUMN
1851 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1852
1853 // Formatting for checkin service - update version if row format changes
1854 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1855
1856 static void printRow(PrintWriter pw, String format, Object...objs) {
1857 pw.println(String.format(format, objs));
1858 }
1859
1860 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1861 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1862 long nativeMax, long nativeAllocated, long nativeFree,
1863 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1864
1865 // For checkin, we print one long comma-separated list of values
1866 if (checkin) {
1867 // NOTE: if you change anything significant below, also consider changing
1868 // ACTIVITY_THREAD_CHECKIN_VERSION.
1869
1870 // Header
1871 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1872 pw.print(pid); pw.print(',');
1873 pw.print(processName); pw.print(',');
1874
1875 // Heap info - max
1876 pw.print(nativeMax); pw.print(',');
1877 pw.print(dalvikMax); pw.print(',');
1878 pw.print("N/A,");
1879 pw.print(nativeMax + dalvikMax); pw.print(',');
1880
1881 // Heap info - allocated
1882 pw.print(nativeAllocated); pw.print(',');
1883 pw.print(dalvikAllocated); pw.print(',');
1884 pw.print("N/A,");
1885 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1886
1887 // Heap info - free
1888 pw.print(nativeFree); pw.print(',');
1889 pw.print(dalvikFree); pw.print(',');
1890 pw.print("N/A,");
1891 pw.print(nativeFree + dalvikFree); pw.print(',');
1892
1893 // Heap info - proportional set size
1894 pw.print(memInfo.nativePss); pw.print(',');
1895 pw.print(memInfo.dalvikPss); pw.print(',');
1896 pw.print(memInfo.otherPss); pw.print(',');
1897 pw.print(memInfo.getTotalPss()); pw.print(',');
1898
1899 // Heap info - swappable set size
1900 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1901 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1902 pw.print(memInfo.otherSwappablePss); pw.print(',');
1903 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1904
1905 // Heap info - shared dirty
1906 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1907 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1908 pw.print(memInfo.otherSharedDirty); pw.print(',');
1909 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1910
1911 // Heap info - shared clean
1912 pw.print(memInfo.nativeSharedClean); pw.print(',');
1913 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1914 pw.print(memInfo.otherSharedClean); pw.print(',');
1915 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1916
1917 // Heap info - private Dirty
1918 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1919 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1920 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1921 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1922
1923 // Heap info - private Clean
1924 pw.print(memInfo.nativePrivateClean); pw.print(',');
1925 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1926 pw.print(memInfo.otherPrivateClean); pw.print(',');
1927 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1928
1929 // Heap info - other areas
1930 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1931 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1932 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1933 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1934 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1935 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1936 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1937 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1938 }
1939 return;
1940 }
1941
1942 // otherwise, show human-readable format
1943 if (dumpFullInfo) {
1944 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1945 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
Anwar Ghuloum629dc182014-09-04 08:05:34 -07001946 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "",
Dianne Hackborne77187d2013-10-25 16:32:41 -07001947 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1948 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1949 "------", "------", "------", "------", "------", "------");
1950 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1951 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1952 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1953 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1954 nativeMax, nativeAllocated, nativeFree);
1955 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1956 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1957 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1958 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1959 dalvikMax, dalvikAllocated, dalvikFree);
1960 } else {
1961 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1962 "Private", "Swapped", "Heap", "Heap", "Heap");
1963 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1964 "Clean", "Dirty", "Size", "Alloc", "Free");
1965 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1966 "------", "------", "------", "------", "------");
1967 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1968 memInfo.nativePrivateDirty,
1969 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1970 nativeMax, nativeAllocated, nativeFree);
1971 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1972 memInfo.dalvikPrivateDirty,
1973 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1974 dalvikMax, dalvikAllocated, dalvikFree);
1975 }
1976
1977 int otherPss = memInfo.otherPss;
1978 int otherSwappablePss = memInfo.otherSwappablePss;
1979 int otherSharedDirty = memInfo.otherSharedDirty;
1980 int otherPrivateDirty = memInfo.otherPrivateDirty;
1981 int otherSharedClean = memInfo.otherSharedClean;
1982 int otherPrivateClean = memInfo.otherPrivateClean;
1983 int otherSwappedOut = memInfo.otherSwappedOut;
1984
1985 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1986 final int myPss = memInfo.getOtherPss(i);
1987 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
1988 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
1989 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
1990 final int mySharedClean = memInfo.getOtherSharedClean(i);
1991 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
1992 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
1993 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
1994 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
1995 if (dumpFullInfo) {
1996 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
1997 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
1998 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
1999 } else {
2000 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2001 myPss, myPrivateDirty,
2002 myPrivateClean, mySwappedOut, "", "", "");
2003 }
2004 otherPss -= myPss;
2005 otherSwappablePss -= mySwappablePss;
2006 otherSharedDirty -= mySharedDirty;
2007 otherPrivateDirty -= myPrivateDirty;
2008 otherSharedClean -= mySharedClean;
2009 otherPrivateClean -= myPrivateClean;
2010 otherSwappedOut -= mySwappedOut;
2011 }
2012 }
2013
2014 if (dumpFullInfo) {
2015 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2016 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
2017 otherSwappedOut, "", "", "");
2018 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2019 memInfo.getTotalSwappablePss(),
2020 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2021 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
2022 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
2023 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2024 } else {
2025 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
2026 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
2027 "", "", "");
2028 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2029 memInfo.getTotalPrivateDirty(),
2030 memInfo.getTotalPrivateClean(),
2031 memInfo.getTotalSwappedOut(),
2032 nativeMax+dalvikMax,
2033 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2034 }
2035
2036 if (dumpDalvik) {
2037 pw.println(" ");
2038 pw.println(" Dalvik Details");
2039
2040 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2041 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2042 final int myPss = memInfo.getOtherPss(i);
2043 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2044 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2045 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2046 final int mySharedClean = memInfo.getOtherSharedClean(i);
2047 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2048 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2049 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2050 || mySharedClean != 0 || myPrivateClean != 0) {
2051 if (dumpFullInfo) {
2052 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2053 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2054 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2055 } else {
2056 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2057 myPss, myPrivateDirty,
2058 myPrivateClean, mySwappedOut, "", "", "");
2059 }
2060 }
2061 }
2062 }
2063 }
2064
Jeff Hamilton52d32032011-01-08 15:31:26 -06002065 public void registerOnActivityPausedListener(Activity activity,
2066 OnActivityPausedListener listener) {
2067 synchronized (mOnPauseListeners) {
2068 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2069 if (list == null) {
2070 list = new ArrayList<OnActivityPausedListener>();
2071 mOnPauseListeners.put(activity, list);
2072 }
2073 list.add(listener);
2074 }
2075 }
2076
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002077 public void unregisterOnActivityPausedListener(Activity activity,
2078 OnActivityPausedListener listener) {
2079 synchronized (mOnPauseListeners) {
2080 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2081 if (list != null) {
2082 list.remove(listener);
2083 }
2084 }
2085 }
2086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002087 public final ActivityInfo resolveActivityInfo(Intent intent) {
2088 ActivityInfo aInfo = intent.resolveActivityInfo(
2089 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2090 if (aInfo == null) {
2091 // Throw an exception.
2092 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002093 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 }
2095 return aInfo;
2096 }
Bob Leee5408332009-09-04 18:31:17 -07002097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002100 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002101 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002103 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 r.intent = intent;
2105 r.state = state;
2106 r.parent = parent;
2107 r.embeddedID = id;
2108 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002109 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 if (localLOGV) {
2111 ComponentName compname = intent.getComponent();
2112 String name;
2113 if (compname != null) {
2114 name = compname.toShortString();
2115 } else {
2116 name = "(Intent " + intent + ").getComponent() returned null";
2117 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002118 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 + ", comp=" + name
2120 + ", token=" + token);
2121 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002122 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124
2125 public final Activity getActivity(IBinder token) {
2126 return mActivities.get(token).activity;
2127 }
2128
2129 public final void sendActivityResult(
2130 IBinder token, String id, int requestCode,
2131 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002132 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002133 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2135 list.add(new ResultInfo(id, requestCode, resultCode, data));
2136 mAppThread.scheduleSendResult(token, list);
2137 }
2138
Jeff Brown9ef09972013-10-15 20:49:59 -07002139 private void sendMessage(int what, Object obj) {
2140 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 }
2142
Jeff Brown9ef09972013-10-15 20:49:59 -07002143 private void sendMessage(int what, Object obj, int arg1) {
2144 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002145 }
2146
Jeff Brown9ef09972013-10-15 20:49:59 -07002147 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2148 sendMessage(what, obj, arg1, arg2, false);
2149 }
2150
2151 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2152 if (DEBUG_MESSAGES) Slog.v(
2153 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2154 + ": " + arg1 + " / " + obj);
2155 Message msg = Message.obtain();
2156 msg.what = what;
2157 msg.obj = obj;
2158 msg.arg1 = arg1;
2159 msg.arg2 = arg2;
2160 if (async) {
2161 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002163 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 }
2165
Dianne Hackborn21556372010-02-04 16:34:40 -08002166 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002167 String what) {
2168 ContextCleanupInfo cci = new ContextCleanupInfo();
2169 cci.context = context;
2170 cci.who = who;
2171 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002172 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002173 }
2174
Craig Mautner233ceee2014-05-09 17:05:11 -07002175 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2177
2178 ActivityInfo aInfo = r.activityInfo;
2179 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002180 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 Context.CONTEXT_INCLUDE_CODE);
2182 }
Bob Leee5408332009-09-04 18:31:17 -07002183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002184 ComponentName component = r.intent.getComponent();
2185 if (component == null) {
2186 component = r.intent.resolveActivity(
2187 mInitialApplication.getPackageManager());
2188 r.intent.setComponent(component);
2189 }
2190
2191 if (r.activityInfo.targetActivity != null) {
2192 component = new ComponentName(r.activityInfo.packageName,
2193 r.activityInfo.targetActivity);
2194 }
2195
2196 Activity activity = null;
2197 try {
2198 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2199 activity = mInstrumentation.newActivity(
2200 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002201 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002203 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 if (r.state != null) {
2205 r.state.setClassLoader(cl);
2206 }
2207 } catch (Exception e) {
2208 if (!mInstrumentation.onException(activity, e)) {
2209 throw new RuntimeException(
2210 "Unable to instantiate activity " + component
2211 + ": " + e.toString(), e);
2212 }
2213 }
2214
2215 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002216 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002217
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002218 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2219 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 TAG, r + ": app=" + app
2221 + ", appName=" + app.getPackageName()
2222 + ", pkg=" + r.packageInfo.getPackageName()
2223 + ", comp=" + r.intent.getComponent().toShortString()
2224 + ", dir=" + r.packageInfo.getAppDir());
2225
2226 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002227 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002229 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002230 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002231 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002232 activity.attach(appContext, this, getInstrumentation(), r.token,
2233 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002234 r.embeddedID, r.lastNonConfigurationInstances, config,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002235 r.voiceInteractor);
Bob Leee5408332009-09-04 18:31:17 -07002236
Christopher Tateb70f3df2009-04-07 16:07:59 -07002237 if (customIntent != null) {
2238 activity.mIntent = customIntent;
2239 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002240 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 activity.mStartedActivity = false;
2242 int theme = r.activityInfo.getThemeResource();
2243 if (theme != 0) {
2244 activity.setTheme(theme);
2245 }
2246
2247 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002248 if (r.isPersistable()) {
2249 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2250 } else {
2251 mInstrumentation.callActivityOnCreate(activity, r.state);
2252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002253 if (!activity.mCalled) {
2254 throw new SuperNotCalledException(
2255 "Activity " + r.intent.getComponent().toShortString() +
2256 " did not call through to super.onCreate()");
2257 }
2258 r.activity = activity;
2259 r.stopped = true;
2260 if (!r.activity.mFinished) {
2261 activity.performStart();
2262 r.stopped = false;
2263 }
2264 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002265 if (r.isPersistable()) {
2266 if (r.state != null || r.persistentState != null) {
2267 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2268 r.persistentState);
2269 }
2270 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002271 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2272 }
2273 }
2274 if (!r.activity.mFinished) {
2275 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002276 if (r.isPersistable()) {
2277 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2278 r.persistentState);
2279 } else {
2280 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002282 if (!activity.mCalled) {
2283 throw new SuperNotCalledException(
2284 "Activity " + r.intent.getComponent().toShortString() +
2285 " did not call through to super.onPostCreate()");
2286 }
2287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289 r.paused = true;
2290
2291 mActivities.put(r.token, r);
2292
2293 } catch (SuperNotCalledException e) {
2294 throw e;
2295
2296 } catch (Exception e) {
2297 if (!mInstrumentation.onException(activity, e)) {
2298 throw new RuntimeException(
2299 "Unable to start activity " + component
2300 + ": " + e.toString(), e);
2301 }
2302 }
2303
2304 return activity;
2305 }
2306
Jeff Brownefd43bd2012-09-21 17:02:35 -07002307 private Context createBaseContextForActivity(ActivityClientRecord r,
2308 final Activity activity) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002309 ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002310 appContext.setOuterContext(activity);
Craig Mautnere0a38842013-12-16 16:14:02 -08002311 Context baseContext = appContext;
2312
2313 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2314 try {
2315 IActivityContainer container =
2316 ActivityManagerNative.getDefault().getEnclosingActivityContainer(r.token);
Craig Mautnerbd503a42014-01-10 10:16:43 -08002317 final int displayId =
2318 container == null ? Display.DEFAULT_DISPLAY : container.getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -08002319 if (displayId > Display.DEFAULT_DISPLAY) {
2320 Display display = dm.getRealDisplay(displayId, r.token);
2321 baseContext = appContext.createDisplayContext(display);
2322 }
2323 } catch (RemoteException e) {
2324 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002325
2326 // For debugging purposes, if the activity's package name contains the value of
2327 // the "debug.use-second-display" system property as a substring, then show
2328 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002329 String pkgName = SystemProperties.get("debug.second-display.pkg");
2330 if (pkgName != null && !pkgName.isEmpty()
2331 && r.packageInfo.mPackageName.contains(pkgName)) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002332 for (int displayId : dm.getDisplayIds()) {
2333 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002334 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002335 baseContext = appContext.createDisplayContext(display);
2336 break;
2337 }
2338 }
2339 }
2340 return baseContext;
2341 }
2342
Romain Guy65b345f2011-07-27 18:51:50 -07002343 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344 // If we are getting ready to gc after going to the background, well
2345 // we are back active so skip it.
2346 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002347 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348
Jeff Hao1b012d32014-08-20 10:35:34 -07002349 if (r.profilerInfo != null) {
2350 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002351 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002352 }
2353
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002354 // Make sure we are running with the most recent config.
2355 handleConfigurationChanged(null, null);
2356
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002357 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002359
Craig Mautner233ceee2014-05-09 17:05:11 -07002360 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361
2362 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002363 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002364 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002365 handleResumeActivity(r.token, false, r.isForward,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002366 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367
2368 if (!r.activity.mFinished && r.startsNotResumed) {
2369 // The activity manager actually wants this one to start out
2370 // paused, because it needs to be visible but isn't in the
2371 // foreground. We accomplish this by going through the
2372 // normal startup (because activities expect to go through
2373 // onResume() the first time they run, before their window
2374 // is displayed), and then pausing it. However, in this case
2375 // we do -not- need to do the full pause cycle (of freezing
2376 // and such) because the activity manager assumes it can just
2377 // retain the current state it has.
2378 try {
2379 r.activity.mCalled = false;
2380 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002381 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002382 // we need to be created again. But we only do this
2383 // for pre-Honeycomb apps, which always save their state
2384 // when pausing, so we can not have them save their state
2385 // when restarting from a paused state. For HC and later,
2386 // we want to (and can) let the state be saved as the normal
2387 // part of stopping the activity.
2388 if (r.isPreHoneycomb()) {
2389 r.state = oldState;
2390 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 if (!r.activity.mCalled) {
2392 throw new SuperNotCalledException(
2393 "Activity " + r.intent.getComponent().toShortString() +
2394 " did not call through to super.onPause()");
2395 }
2396
2397 } catch (SuperNotCalledException e) {
2398 throw e;
2399
2400 } catch (Exception e) {
2401 if (!mInstrumentation.onException(r.activity, e)) {
2402 throw new RuntimeException(
2403 "Unable to pause activity "
2404 + r.intent.getComponent().toShortString()
2405 + ": " + e.toString(), e);
2406 }
2407 }
2408 r.paused = true;
2409 }
2410 } else {
2411 // If there was an error, for any reason, tell the activity
2412 // manager to stop us.
2413 try {
2414 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07002415 .finishActivity(r.token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002416 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002417 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 }
2419 }
2420 }
2421
Romain Guy65b345f2011-07-27 18:51:50 -07002422 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 List<Intent> intents) {
2424 final int N = intents.size();
2425 for (int i=0; i<N; i++) {
2426 Intent intent = intents.get(i);
2427 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002428 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002429 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2431 }
2432 }
2433
2434 public final void performNewIntents(IBinder token,
2435 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002436 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 if (r != null) {
2438 final boolean resumed = !r.paused;
2439 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002440 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 mInstrumentation.callActivityOnPause(r.activity);
2442 }
2443 deliverNewIntents(r, intents);
2444 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002445 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002446 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002447 }
2448 }
2449 }
Bob Leee5408332009-09-04 18:31:17 -07002450
Romain Guy65b345f2011-07-27 18:51:50 -07002451 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 performNewIntents(data.token, data.intents);
2453 }
2454
Adam Skorydfc7fd72013-08-05 19:23:41 -07002455 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002456 Bundle data = new Bundle();
2457 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2458 if (r != null) {
2459 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2460 r.activity.onProvideAssistData(data);
2461 }
2462 if (data.isEmpty()) {
2463 data = null;
2464 }
2465 IActivityManager mgr = ActivityManagerNative.getDefault();
2466 try {
Adam Skory7140a252013-09-11 12:04:58 +01002467 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002468 } catch (RemoteException e) {
2469 }
2470 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002471
2472 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2473 ActivityClientRecord r = mActivities.get(token);
2474 if (r != null) {
2475 r.activity.onTranslucentConversionComplete(drawComplete);
2476 }
2477 }
2478
Craig Mautnereb8abf72014-07-02 15:04:09 -07002479 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2480 ActivityClientRecord r = mActivities.get(token);
2481 if (r != null) {
2482 r.activity.onNewActivityOptions(options);
2483 }
2484 }
2485
Jose Lima4b6c6692014-08-12 17:41:12 -07002486 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002487 ActivityClientRecord r = mActivities.get(token);
2488 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002489 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002490 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002491 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002492 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002493 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002494 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2495 if (!activity.mCalled) {
2496 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002497 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002498 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002499 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002500 }
2501 }
2502 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002503 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002504 } catch (RemoteException e) {
2505 }
2506 }
2507
Jose Lima4b6c6692014-08-12 17:41:12 -07002508 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002509 ActivityClientRecord r = mActivities.get(token);
2510 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002511 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002512 }
2513 }
2514
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002515 public void handleInstallProvider(ProviderInfo info) {
2516 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2517 }
2518
Craig Mautner8746a472014-07-24 15:12:54 -07002519 private void handleEnterAnimationComplete(IBinder token) {
2520 ActivityClientRecord r = mActivities.get(token);
2521 if (r != null) {
Filip Gruszczynski6eafa902014-11-14 14:24:37 -08002522 r.activity.dispatchEnterAnimationComplete();
Craig Mautner8746a472014-07-24 15:12:54 -07002523 }
2524 }
2525
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002526 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2527
2528 /**
2529 * Return the Intent that's currently being handled by a
2530 * BroadcastReceiver on this thread, or null if none.
2531 * @hide
2532 */
2533 public static Intent getIntentBeingBroadcast() {
2534 return sCurrentBroadcastIntent.get();
2535 }
2536
Romain Guy65b345f2011-07-27 18:51:50 -07002537 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 // If we are getting ready to gc after going to the background, well
2539 // we are back active so skip it.
2540 unscheduleGcIdler();
2541
2542 String component = data.intent.getComponent().getClassName();
2543
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002544 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002545 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546
2547 IActivityManager mgr = ActivityManagerNative.getDefault();
2548
Romain Guy65b345f2011-07-27 18:51:50 -07002549 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 try {
2551 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2552 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002553 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002554 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002555 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2556 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002557 if (DEBUG_BROADCAST) Slog.i(TAG,
2558 "Finishing failed broadcast to " + data.intent.getComponent());
2559 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 throw new RuntimeException(
2561 "Unable to instantiate receiver " + component
2562 + ": " + e.toString(), e);
2563 }
2564
2565 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002566 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002567
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002568 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 TAG, "Performing receive of " + data.intent
2570 + ": app=" + app
2571 + ", appName=" + app.getPackageName()
2572 + ", pkg=" + packageInfo.getPackageName()
2573 + ", comp=" + data.intent.getComponent().toShortString()
2574 + ", dir=" + packageInfo.getAppDir());
2575
Dianne Hackborn21556372010-02-04 16:34:40 -08002576 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002577 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002578 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 receiver.onReceive(context.getReceiverRestrictedContext(),
2580 data.intent);
2581 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002582 if (DEBUG_BROADCAST) Slog.i(TAG,
2583 "Finishing failed broadcast to " + data.intent.getComponent());
2584 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002585 if (!mInstrumentation.onException(receiver, e)) {
2586 throw new RuntimeException(
2587 "Unable to start receiver " + component
2588 + ": " + e.toString(), e);
2589 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002590 } finally {
2591 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593
Dianne Hackborne829fef2010-10-26 17:44:01 -07002594 if (receiver.getPendingResult() != null) {
2595 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 }
2597 }
2598
Christopher Tate181fafa2009-05-14 11:12:14 -07002599 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002600 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002601 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002602
Christopher Tate346acb12012-10-15 19:20:25 -07002603 // Sanity check the requested target package's uid against ours
2604 try {
2605 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2606 data.appInfo.packageName, 0, UserHandle.myUserId());
2607 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2608 Slog.w(TAG, "Asked to instantiate non-matching package "
2609 + data.appInfo.packageName);
2610 return;
2611 }
2612 } catch (RemoteException e) {
2613 Slog.e(TAG, "Can't reach package manager", e);
2614 return;
2615 }
2616
Christopher Tate181fafa2009-05-14 11:12:14 -07002617 // no longer idle; we have backup work to do
2618 unscheduleGcIdler();
2619
2620 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002621 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002622 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002623 if (packageName == null) {
2624 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2625 return;
2626 }
2627
Christopher Tate181fafa2009-05-14 11:12:14 -07002628 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07002629 // full backup operation but no app-supplied agent? use the default implementation
2630 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2631 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002632 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002633 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002634
Christopher Tate181fafa2009-05-14 11:12:14 -07002635 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002636 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07002637 BackupAgent agent = mBackupAgents.get(packageName);
2638 if (agent != null) {
2639 // reusing the existing instance
2640 if (DEBUG_BACKUP) {
2641 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07002642 }
Christopher Tate2e40d112014-07-15 12:37:38 -07002643 binder = agent.onBind();
2644 } else {
2645 try {
2646 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2647
2648 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2649 agent = (BackupAgent) cl.loadClass(classname).newInstance();
2650
2651 // set up the agent's context
2652 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2653 context.setOuterContext(agent);
2654 agent.attach(context);
2655
2656 agent.onCreate();
2657 binder = agent.onBind();
2658 mBackupAgents.put(packageName, agent);
2659 } catch (Exception e) {
2660 // If this is during restore, fail silently; otherwise go
2661 // ahead and let the user see the crash.
2662 Slog.e(TAG, "Agent threw during creation: " + e);
2663 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2664 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
2665 throw e;
2666 }
2667 // falling through with 'binder' still null
2668 }
Christopher Tated1475e02009-07-09 15:36:17 -07002669 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002670
2671 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002672 try {
2673 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2674 } catch (RemoteException e) {
2675 // nothing to do.
2676 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002677 } catch (Exception e) {
2678 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002679 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002680 }
2681 }
2682
2683 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002684 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002685 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002686
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002687 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002688 String packageName = packageInfo.mPackageName;
2689 BackupAgent agent = mBackupAgents.get(packageName);
2690 if (agent != null) {
2691 try {
2692 agent.onDestroy();
2693 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002694 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002695 e.printStackTrace();
2696 }
2697 mBackupAgents.remove(packageName);
2698 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002699 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002700 }
2701 }
2702
Romain Guy65b345f2011-07-27 18:51:50 -07002703 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 // If we are getting ready to gc after going to the background, well
2705 // we are back active so skip it.
2706 unscheduleGcIdler();
2707
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002708 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002709 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 Service service = null;
2711 try {
2712 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2713 service = (Service) cl.loadClass(data.info.name).newInstance();
2714 } catch (Exception e) {
2715 if (!mInstrumentation.onException(service, e)) {
2716 throw new RuntimeException(
2717 "Unable to instantiate service " + data.info.name
2718 + ": " + e.toString(), e);
2719 }
2720 }
2721
2722 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002723 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002724
Jeff Browndefd4a62014-03-10 21:24:37 -07002725 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2726 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002728 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 service.attach(context, this, data.info.name, data.token, app,
2730 ActivityManagerNative.getDefault());
2731 service.onCreate();
2732 mServices.put(data.token, service);
2733 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002734 ActivityManagerNative.getDefault().serviceDoneExecuting(
2735 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 } catch (RemoteException e) {
2737 // nothing to do.
2738 }
2739 } catch (Exception e) {
2740 if (!mInstrumentation.onException(service, e)) {
2741 throw new RuntimeException(
2742 "Unable to create service " + data.info.name
2743 + ": " + e.toString(), e);
2744 }
2745 }
2746 }
2747
Romain Guy65b345f2011-07-27 18:51:50 -07002748 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002750 if (DEBUG_SERVICE)
2751 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 if (s != null) {
2753 try {
2754 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002755 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 try {
2757 if (!data.rebind) {
2758 IBinder binder = s.onBind(data.intent);
2759 ActivityManagerNative.getDefault().publishService(
2760 data.token, data.intent, binder);
2761 } else {
2762 s.onRebind(data.intent);
2763 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002764 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002766 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 } catch (RemoteException ex) {
2768 }
2769 } catch (Exception e) {
2770 if (!mInstrumentation.onException(s, e)) {
2771 throw new RuntimeException(
2772 "Unable to bind to service " + s
2773 + " with " + data.intent + ": " + e.toString(), e);
2774 }
2775 }
2776 }
2777 }
2778
Romain Guy65b345f2011-07-27 18:51:50 -07002779 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 Service s = mServices.get(data.token);
2781 if (s != null) {
2782 try {
2783 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002784 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 boolean doRebind = s.onUnbind(data.intent);
2786 try {
2787 if (doRebind) {
2788 ActivityManagerNative.getDefault().unbindFinished(
2789 data.token, data.intent, doRebind);
2790 } else {
2791 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002792 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 }
2794 } catch (RemoteException ex) {
2795 }
2796 } catch (Exception e) {
2797 if (!mInstrumentation.onException(s, e)) {
2798 throw new RuntimeException(
2799 "Unable to unbind to service " + s
2800 + " with " + data.intent + ": " + e.toString(), e);
2801 }
2802 }
2803 }
2804 }
2805
Dianne Hackborn625ac272010-09-17 18:29:22 -07002806 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002807 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2808 try {
2809 Service s = mServices.get(info.token);
2810 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002811 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2812 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002813 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2814 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002816 } finally {
2817 IoUtils.closeQuietly(info.fd);
2818 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 }
2820 }
2821
Dianne Hackborn625ac272010-09-17 18:29:22 -07002822 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002823 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2824 try {
2825 ActivityClientRecord r = mActivities.get(info.token);
2826 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002827 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2828 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002829 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2830 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002831 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002832 } finally {
2833 IoUtils.closeQuietly(info.fd);
2834 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002835 }
2836 }
2837
Marco Nelissen18cb2872011-11-15 11:19:53 -08002838 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002839 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2840 try {
2841 ProviderClientRecord r = mLocalProviders.get(info.token);
2842 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002843 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2844 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002845 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2846 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002847 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002848 } finally {
2849 IoUtils.closeQuietly(info.fd);
2850 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002851 }
2852 }
2853
Romain Guy65b345f2011-07-27 18:51:50 -07002854 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002855 Service s = mServices.get(data.token);
2856 if (s != null) {
2857 try {
2858 if (data.args != null) {
2859 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002860 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002861 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002862 int res;
2863 if (!data.taskRemoved) {
2864 res = s.onStartCommand(data.args, data.flags, data.startId);
2865 } else {
2866 s.onTaskRemoved(data.args);
2867 res = Service.START_TASK_REMOVED_COMPLETE;
2868 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002869
2870 QueuedWork.waitToFinish();
2871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002873 ActivityManagerNative.getDefault().serviceDoneExecuting(
2874 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002875 } catch (RemoteException e) {
2876 // nothing to do.
2877 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002878 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879 } catch (Exception e) {
2880 if (!mInstrumentation.onException(s, e)) {
2881 throw new RuntimeException(
2882 "Unable to start service " + s
2883 + " with " + data.args + ": " + e.toString(), e);
2884 }
2885 }
2886 }
2887 }
2888
Romain Guy65b345f2011-07-27 18:51:50 -07002889 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 Service s = mServices.remove(token);
2891 if (s != null) {
2892 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002893 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 s.onDestroy();
2895 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002896 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002898 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002900
2901 QueuedWork.waitToFinish();
2902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002903 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002904 ActivityManagerNative.getDefault().serviceDoneExecuting(
2905 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 } catch (RemoteException e) {
2907 // nothing to do.
2908 }
2909 } catch (Exception e) {
2910 if (!mInstrumentation.onException(s, e)) {
2911 throw new RuntimeException(
2912 "Unable to stop service " + s
2913 + ": " + e.toString(), e);
2914 }
2915 }
2916 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002917 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 }
2919
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002920 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002921 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002922 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002923 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 + " finished=" + r.activity.mFinished);
2925 if (r != null && !r.activity.mFinished) {
2926 if (clearHide) {
2927 r.hideForNow = false;
2928 r.activity.mStartedActivity = false;
2929 }
2930 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002931 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 if (r.pendingIntents != null) {
2933 deliverNewIntents(r, r.pendingIntents);
2934 r.pendingIntents = null;
2935 }
2936 if (r.pendingResults != null) {
2937 deliverResults(r, r.pendingResults);
2938 r.pendingResults = null;
2939 }
2940 r.activity.performResume();
2941
Bob Leee5408332009-09-04 18:31:17 -07002942 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002943 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 r.paused = false;
2946 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07002948 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002949 } catch (Exception e) {
2950 if (!mInstrumentation.onException(r.activity, e)) {
2951 throw new RuntimeException(
2952 "Unable to resume activity "
2953 + r.intent.getComponent().toShortString()
2954 + ": " + e.toString(), e);
2955 }
2956 }
2957 }
2958 return r;
2959 }
2960
Romain Guya998dff2012-03-23 18:58:36 -07002961 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002962 if (r.mPendingRemoveWindow != null) {
2963 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2964 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2965 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002966 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002967 r.activity.getClass().getName(), "Activity");
2968 }
2969 }
2970 r.mPendingRemoveWindow = null;
2971 r.mPendingRemoveWindowManager = null;
2972 }
2973
Craig Mautner233ceee2014-05-09 17:05:11 -07002974 final void handleResumeActivity(IBinder token,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002975 boolean clearHide, boolean isForward, boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 // If we are getting ready to gc after going to the background, well
2977 // we are back active so skip it.
2978 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002979 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980
Adam Powellcfbe9be2013-11-06 14:58:58 -08002981 // TODO Push resumeArgs into the activity for consideration
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002982 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983
2984 if (r != null) {
2985 final Activity a = r.activity;
2986
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002987 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 TAG, "Resume " + r + " started activity: " +
2989 a.mStartedActivity + ", hideForNow: " + r.hideForNow
2990 + ", finished: " + a.mFinished);
2991
2992 final int forwardBit = isForward ?
2993 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07002994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 // If the window hasn't yet been added to the window manager,
2996 // and this guy didn't finish itself or start another activity,
2997 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08002998 boolean willBeVisible = !a.mStartedActivity;
2999 if (!willBeVisible) {
3000 try {
3001 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3002 a.getActivityToken());
3003 } catch (RemoteException e) {
3004 }
3005 }
3006 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003007 r.window = r.activity.getWindow();
3008 View decor = r.window.getDecorView();
3009 decor.setVisibility(View.INVISIBLE);
3010 ViewManager wm = a.getWindowManager();
3011 WindowManager.LayoutParams l = r.window.getAttributes();
3012 a.mDecor = decor;
3013 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3014 l.softInputMode |= forwardBit;
3015 if (a.mVisibleFromClient) {
3016 a.mWindowAdded = true;
3017 wm.addView(decor, l);
3018 }
3019
3020 // If the window has already been added, but during resume
3021 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003022 // window visible.
3023 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003024 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003025 TAG, "Launch " + r + " mStartedActivity set");
3026 r.hideForNow = true;
3027 }
3028
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003029 // Get rid of anything left hanging around.
3030 cleanUpPendingRemoveWindows(r);
3031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 // The window is now visible if it has been added, we are not
3033 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003034 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003035 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003037 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003038 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003040 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041 r.newConfig = null;
3042 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003043 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003044 + isForward);
3045 WindowManager.LayoutParams l = r.window.getAttributes();
3046 if ((l.softInputMode
3047 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3048 != forwardBit) {
3049 l.softInputMode = (l.softInputMode
3050 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3051 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003052 if (r.activity.mVisibleFromClient) {
3053 ViewManager wm = a.getWindowManager();
3054 View decor = r.window.getDecorView();
3055 wm.updateViewLayout(decor, l);
3056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 }
3058 r.activity.mVisibleFromServer = true;
3059 mNumVisibleActivities++;
3060 if (r.activity.mVisibleFromClient) {
3061 r.activity.makeVisible();
3062 }
3063 }
3064
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003065 if (!r.onlyLocalRequest) {
3066 r.nextIdle = mNewActivities;
3067 mNewActivities = r;
3068 if (localLOGV) Slog.v(
3069 TAG, "Scheduling idle handler for " + r);
3070 Looper.myQueue().addIdleHandler(new Idler());
3071 }
3072 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003074 // Tell the activity manager we have resumed.
3075 if (reallyResume) {
3076 try {
3077 ActivityManagerNative.getDefault().activityResumed(token);
3078 } catch (RemoteException ex) {
3079 }
3080 }
3081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 } else {
3083 // If an exception was thrown when trying to resume, then
3084 // just end this activity.
3085 try {
3086 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07003087 .finishActivity(token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003088 } catch (RemoteException ex) {
3089 }
3090 }
3091 }
3092
3093 private int mThumbnailWidth = -1;
3094 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003095 private Bitmap mAvailThumbnailBitmap = null;
3096 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097
Romain Guy65b345f2011-07-27 18:51:50 -07003098 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003099 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003101 if (thumbnail == null) {
3102 int w = mThumbnailWidth;
3103 int h;
3104 if (w < 0) {
3105 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003106 int wId = com.android.internal.R.dimen.thumbnail_width;
3107 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003108 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3109 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003110 } else {
3111 h = mThumbnailHeight;
3112 }
3113
3114 // On platforms where we don't want thumbnails, set dims to (0,0)
3115 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003116 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3117 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003118 thumbnail.eraseColor(0);
3119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 }
3121
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003122 if (thumbnail != null) {
3123 Canvas cv = mThumbnailCanvas;
3124 if (cv == null) {
3125 mThumbnailCanvas = cv = new Canvas();
3126 }
3127
3128 cv.setBitmap(thumbnail);
3129 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3130 mAvailThumbnailBitmap = thumbnail;
3131 thumbnail = null;
3132 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003133 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003136 } catch (Exception e) {
3137 if (!mInstrumentation.onException(r.activity, e)) {
3138 throw new RuntimeException(
3139 "Unable to create thumbnail of "
3140 + r.intent.getComponent().toShortString()
3141 + ": " + e.toString(), e);
3142 }
3143 thumbnail = null;
3144 }
3145
3146 return thumbnail;
3147 }
3148
Romain Guy65b345f2011-07-27 18:51:50 -07003149 private void handlePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003150 boolean userLeaving, int configChanges, boolean dontReport) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003151 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003153 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 if (userLeaving) {
3155 performUserLeavingActivity(r);
3156 }
Bob Leee5408332009-09-04 18:31:17 -07003157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003159 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003160
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003161 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003162 if (r.isPreHoneycomb()) {
3163 QueuedWork.waitToFinish();
3164 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003166 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003167 if (!dontReport) {
3168 try {
3169 ActivityManagerNative.getDefault().activityPaused(token);
3170 } catch (RemoteException ex) {
3171 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003172 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003173 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 }
3175 }
3176
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003177 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 mInstrumentation.callActivityOnUserLeaving(r.activity);
3179 }
3180
3181 final Bundle performPauseActivity(IBinder token, boolean finished,
3182 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003183 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 return r != null ? performPauseActivity(r, finished, saveState) : null;
3185 }
3186
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003187 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 boolean saveState) {
3189 if (r.paused) {
3190 if (r.activity.mFinished) {
3191 // If we are finishing, we won't call onResume() in certain cases.
3192 // So here we likewise don't want to call onPause() if the activity
3193 // isn't resumed.
3194 return null;
3195 }
3196 RuntimeException e = new RuntimeException(
3197 "Performing pause of activity that is not resumed: "
3198 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003199 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 if (finished) {
3202 r.activity.mFinished = true;
3203 }
3204 try {
3205 // Next have the activity save its current state and managed dialogs...
3206 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003207 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003208 }
3209 // Now we are idle.
3210 r.activity.mCalled = false;
3211 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003212 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3213 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003214 if (!r.activity.mCalled) {
3215 throw new SuperNotCalledException(
3216 "Activity " + r.intent.getComponent().toShortString() +
3217 " did not call through to super.onPause()");
3218 }
3219
3220 } catch (SuperNotCalledException e) {
3221 throw e;
3222
3223 } catch (Exception e) {
3224 if (!mInstrumentation.onException(r.activity, e)) {
3225 throw new RuntimeException(
3226 "Unable to pause activity "
3227 + r.intent.getComponent().toShortString()
3228 + ": " + e.toString(), e);
3229 }
3230 }
3231 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003232
3233 // Notify any outstanding on paused listeners
3234 ArrayList<OnActivityPausedListener> listeners;
3235 synchronized (mOnPauseListeners) {
3236 listeners = mOnPauseListeners.remove(r.activity);
3237 }
3238 int size = (listeners != null ? listeners.size() : 0);
3239 for (int i = 0; i < size; i++) {
3240 listeners.get(i).onPaused(r.activity);
3241 }
3242
Craig Mautnera0026042014-04-23 11:45:37 -07003243 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 }
3245
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003246 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003247 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003248 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 }
3250
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003251 private static class StopInfo implements Runnable {
3252 ActivityClientRecord activity;
3253 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003254 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003255 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003256
3257 @Override public void run() {
3258 // Tell activity manager we have been stopped.
3259 try {
3260 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3261 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003262 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003263 } catch (RemoteException ex) {
3264 }
3265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003266 }
3267
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003268 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003269 public final IActivityManager.ContentProviderHolder holder;
3270 public final ProviderClientRecord client;
3271 public int stableCount;
3272 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003273
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003274 // When this is set, the stable and unstable ref counts are 0 and
3275 // we have a pending operation scheduled to remove the ref count
3276 // from the activity manager. On the activity manager we are still
3277 // holding an unstable ref, though it is not reflected in the counts
3278 // here.
3279 public boolean removePending;
3280
3281 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3282 ProviderClientRecord inClient, int sCount, int uCount) {
3283 holder = inHolder;
3284 client = inClient;
3285 stableCount = sCount;
3286 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
3288 }
3289
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003290 /**
3291 * Core implementation of stopping an activity. Note this is a little
3292 * tricky because the server's meaning of stop is slightly different
3293 * than our client -- for the server, stop means to save state and give
3294 * it the result when it is done, but the window may still be visible.
3295 * For the client, we want to call onStop()/onStart() to indicate when
3296 * the activity's UI visibillity changes.
3297 */
Romain Guy65b345f2011-07-27 18:51:50 -07003298 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003299 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003300 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 if (r != null) {
3302 if (!keepShown && r.stopped) {
3303 if (r.activity.mFinished) {
3304 // If we are finishing, we won't call onResume() in certain
3305 // cases. So here we likewise don't want to call onStop()
3306 // if the activity isn't resumed.
3307 return;
3308 }
3309 RuntimeException e = new RuntimeException(
3310 "Performing stop of activity that is not resumed: "
3311 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003312 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
3314
3315 if (info != null) {
3316 try {
3317 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003318 // For now, don't create the thumbnail here; we are
3319 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003320 info.description = r.activity.onCreateDescription();
3321 } catch (Exception e) {
3322 if (!mInstrumentation.onException(r.activity, e)) {
3323 throw new RuntimeException(
3324 "Unable to save state of activity "
3325 + r.intent.getComponent().toShortString()
3326 + ": " + e.toString(), e);
3327 }
3328 }
3329 }
3330
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003331 // Next have the activity save its current state and managed dialogs...
3332 if (!r.activity.mFinished && saveState) {
3333 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003334 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003335 }
3336 }
3337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 if (!keepShown) {
3339 try {
3340 // Now we are idle.
3341 r.activity.performStop();
3342 } catch (Exception e) {
3343 if (!mInstrumentation.onException(r.activity, e)) {
3344 throw new RuntimeException(
3345 "Unable to stop activity "
3346 + r.intent.getComponent().toShortString()
3347 + ": " + e.toString(), e);
3348 }
3349 }
3350 r.stopped = true;
3351 }
3352
3353 r.paused = true;
3354 }
3355 }
3356
Romain Guy65b345f2011-07-27 18:51:50 -07003357 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003358 View v = r.activity.mDecor;
3359 if (v != null) {
3360 if (show) {
3361 if (!r.activity.mVisibleFromServer) {
3362 r.activity.mVisibleFromServer = true;
3363 mNumVisibleActivities++;
3364 if (r.activity.mVisibleFromClient) {
3365 r.activity.makeVisible();
3366 }
3367 }
3368 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003369 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003370 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003372 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 r.newConfig = null;
3374 }
3375 } else {
3376 if (r.activity.mVisibleFromServer) {
3377 r.activity.mVisibleFromServer = false;
3378 mNumVisibleActivities--;
3379 v.setVisibility(View.INVISIBLE);
3380 }
3381 }
3382 }
3383 }
3384
Romain Guy65b345f2011-07-27 18:51:50 -07003385 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003386 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003387 r.activity.mConfigChangeFlags |= configChanges;
3388
3389 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003390 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003392 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003393 TAG, "Finishing stop of " + r + ": show=" + show
3394 + " win=" + r.window);
3395
3396 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003397
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003398 // Make sure any pending writes are now committed.
3399 if (!r.isPreHoneycomb()) {
3400 QueuedWork.waitToFinish();
3401 }
3402
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003403 // Schedule the call to tell the activity manager we have
3404 // stopped. We don't do this immediately, because we want to
3405 // have a chance for any other pending work (in particular memory
3406 // trim requests) to complete before you tell the activity
3407 // manager to proceed and allow us to go fully into the background.
3408 info.activity = r;
3409 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003410 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003411 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003412 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003413 }
3414
3415 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003416 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 if (r.stopped) {
3418 r.activity.performRestart();
3419 r.stopped = false;
3420 }
3421 }
3422
Romain Guy65b345f2011-07-27 18:51:50 -07003423 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003424 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003425
3426 if (r == null) {
3427 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3428 return;
3429 }
3430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003431 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003432 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 } else if (show && r.stopped) {
3434 // If we are getting ready to gc after going to the background, well
3435 // we are back active so skip it.
3436 unscheduleGcIdler();
3437
3438 r.activity.performRestart();
3439 r.stopped = false;
3440 }
3441 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003442 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003443 TAG, "Handle window " + r + " visibility: " + show);
3444 updateVisibility(r, show);
3445 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003446 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003447 }
3448
Romain Guy65b345f2011-07-27 18:51:50 -07003449 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003450 ActivityClientRecord r = mActivities.get(token);
3451
3452 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003453 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003454 return;
3455 }
3456
3457 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003458 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003459 try {
3460 // Now we are idle.
3461 r.activity.performStop();
3462 } catch (Exception e) {
3463 if (!mInstrumentation.onException(r.activity, e)) {
3464 throw new RuntimeException(
3465 "Unable to stop activity "
3466 + r.intent.getComponent().toShortString()
3467 + ": " + e.toString(), e);
3468 }
3469 }
3470 r.stopped = true;
3471 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003472
3473 // Make sure any pending writes are now committed.
3474 if (!r.isPreHoneycomb()) {
3475 QueuedWork.waitToFinish();
3476 }
3477
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003478 // Tell activity manager we slept.
3479 try {
3480 ActivityManagerNative.getDefault().activitySlept(r.token);
3481 } catch (RemoteException ex) {
3482 }
3483 } else {
3484 if (r.stopped && r.activity.mVisibleFromServer) {
3485 r.activity.performRestart();
3486 r.stopped = false;
3487 }
3488 }
3489 }
3490
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003491 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003492 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003493 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003494 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003495 onCoreSettingsChange();
3496 }
3497
3498 private void onCoreSettingsChange() {
3499 boolean debugViewAttributes =
3500 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3501 if (debugViewAttributes != View.mDebugViewAttributes) {
3502 View.mDebugViewAttributes = debugViewAttributes;
3503
3504 // request all activities to relaunch for the changes to take place
3505 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
3506 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, false);
3507 }
3508 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003509 }
3510
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003511 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3512 LoadedApk apk = peekPackageInfo(data.pkg, false);
3513 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003514 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003515 }
3516 apk = peekPackageInfo(data.pkg, true);
3517 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003518 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003519 }
3520 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003521 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003522 }
3523
Romain Guy65b345f2011-07-27 18:51:50 -07003524 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003525 final int N = results.size();
3526 for (int i=0; i<N; i++) {
3527 ResultInfo ri = results.get(i);
3528 try {
3529 if (ri.mData != null) {
3530 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003531 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003532 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003533 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003534 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003535 r.activity.dispatchActivityResult(ri.mResultWho,
3536 ri.mRequestCode, ri.mResultCode, ri.mData);
3537 } catch (Exception e) {
3538 if (!mInstrumentation.onException(r.activity, e)) {
3539 throw new RuntimeException(
3540 "Failure delivering result " + ri + " to activity "
3541 + r.intent.getComponent().toShortString()
3542 + ": " + e.toString(), e);
3543 }
3544 }
3545 }
3546 }
3547
Romain Guy65b345f2011-07-27 18:51:50 -07003548 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003549 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003550 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551 if (r != null) {
3552 final boolean resumed = !r.paused;
3553 if (!r.activity.mFinished && r.activity.mDecor != null
3554 && r.hideForNow && resumed) {
3555 // We had hidden the activity because it started another
3556 // one... we have gotten a result back and we are not
3557 // paused, so make sure our window is visible.
3558 updateVisibility(r, true);
3559 }
3560 if (resumed) {
3561 try {
3562 // Now we are idle.
3563 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003564 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003565 mInstrumentation.callActivityOnPause(r.activity);
3566 if (!r.activity.mCalled) {
3567 throw new SuperNotCalledException(
3568 "Activity " + r.intent.getComponent().toShortString()
3569 + " did not call through to super.onPause()");
3570 }
3571 } catch (SuperNotCalledException e) {
3572 throw e;
3573 } catch (Exception e) {
3574 if (!mInstrumentation.onException(r.activity, e)) {
3575 throw new RuntimeException(
3576 "Unable to pause activity "
3577 + r.intent.getComponent().toShortString()
3578 + ": " + e.toString(), e);
3579 }
3580 }
3581 }
3582 deliverResults(r, res.results);
3583 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003584 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003585 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 }
3587 }
3588 }
3589
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003590 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 return performDestroyActivity(token, finishing, 0, false);
3592 }
3593
Romain Guy65b345f2011-07-27 18:51:50 -07003594 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003595 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003596 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003597 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003598 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003599 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003600 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 r.activity.mConfigChangeFlags |= configChanges;
3602 if (finishing) {
3603 r.activity.mFinished = true;
3604 }
3605 if (!r.paused) {
3606 try {
3607 r.activity.mCalled = false;
3608 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003609 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 r.activity.getComponentName().getClassName());
3611 if (!r.activity.mCalled) {
3612 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003613 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 + " did not call through to super.onPause()");
3615 }
3616 } catch (SuperNotCalledException e) {
3617 throw e;
3618 } catch (Exception e) {
3619 if (!mInstrumentation.onException(r.activity, e)) {
3620 throw new RuntimeException(
3621 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003622 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003623 + ": " + e.toString(), e);
3624 }
3625 }
3626 r.paused = true;
3627 }
3628 if (!r.stopped) {
3629 try {
3630 r.activity.performStop();
3631 } catch (SuperNotCalledException e) {
3632 throw e;
3633 } catch (Exception e) {
3634 if (!mInstrumentation.onException(r.activity, e)) {
3635 throw new RuntimeException(
3636 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003637 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 + ": " + e.toString(), e);
3639 }
3640 }
3641 r.stopped = true;
3642 }
3643 if (getNonConfigInstance) {
3644 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003645 r.lastNonConfigurationInstances
3646 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 } catch (Exception e) {
3648 if (!mInstrumentation.onException(r.activity, e)) {
3649 throw new RuntimeException(
3650 "Unable to retain activity "
3651 + r.intent.getComponent().toShortString()
3652 + ": " + e.toString(), e);
3653 }
3654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003655 }
3656 try {
3657 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003658 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 if (!r.activity.mCalled) {
3660 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003661 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 " did not call through to super.onDestroy()");
3663 }
3664 if (r.window != null) {
3665 r.window.closeAllPanels();
3666 }
3667 } catch (SuperNotCalledException e) {
3668 throw e;
3669 } catch (Exception e) {
3670 if (!mInstrumentation.onException(r.activity, e)) {
3671 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003672 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3673 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 }
3675 }
3676 }
3677 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003678 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003679 return r;
3680 }
3681
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003682 private static String safeToComponentShortString(Intent intent) {
3683 ComponentName component = intent.getComponent();
3684 return component == null ? "[Unknown]" : component.toShortString();
3685 }
3686
Romain Guy65b345f2011-07-27 18:51:50 -07003687 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003689 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003690 configChanges, getNonConfigInstance);
3691 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003692 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 WindowManager wm = r.activity.getWindowManager();
3694 View v = r.activity.mDecor;
3695 if (v != null) {
3696 if (r.activity.mVisibleFromServer) {
3697 mNumVisibleActivities--;
3698 }
3699 IBinder wtoken = v.getWindowToken();
3700 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003701 if (r.onlyLocalRequest) {
3702 // Hold off on removing this until the new activity's
3703 // window is being added.
3704 r.mPendingRemoveWindow = v;
3705 r.mPendingRemoveWindowManager = wm;
3706 } else {
3707 wm.removeViewImmediate(v);
3708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003710 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003711 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003712 r.activity.getClass().getName(), "Activity");
3713 }
3714 r.activity.mDecor = null;
3715 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003716 if (r.mPendingRemoveWindow == null) {
3717 // If we are delaying the removal of the activity window, then
3718 // we can't clean up all windows here. Note that we can't do
3719 // so later either, which means any windows that aren't closed
3720 // by the app will leak. Well we try to warning them a lot
3721 // about leaking windows, because that is a bug, so if they are
3722 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003723 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003724 r.activity.getClass().getName(), "Activity");
3725 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003726
3727 // Mocked out contexts won't be participating in the normal
3728 // process lifecycle, but if we're running with a proper
3729 // ApplicationContext we need to have it tear down things
3730 // cleanly.
3731 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003732 if (c instanceof ContextImpl) {
3733 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003734 r.activity.getClass().getName(), "Activity");
3735 }
3736 }
3737 if (finishing) {
3738 try {
3739 ActivityManagerNative.getDefault().activityDestroyed(token);
3740 } catch (RemoteException ex) {
3741 // If the system process has died, it's game over for everyone.
3742 }
3743 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003744 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 }
3746
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003747 public final void requestRelaunchActivity(IBinder token,
3748 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3749 int configChanges, boolean notResumed, Configuration config,
3750 boolean fromServer) {
3751 ActivityClientRecord target = null;
3752
Craig Mautner88c05892013-06-28 09:47:45 -07003753 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003754 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3755 ActivityClientRecord r = mRelaunchingActivities.get(i);
3756 if (r.token == token) {
3757 target = r;
3758 if (pendingResults != null) {
3759 if (r.pendingResults != null) {
3760 r.pendingResults.addAll(pendingResults);
3761 } else {
3762 r.pendingResults = pendingResults;
3763 }
3764 }
3765 if (pendingNewIntents != null) {
3766 if (r.pendingIntents != null) {
3767 r.pendingIntents.addAll(pendingNewIntents);
3768 } else {
3769 r.pendingIntents = pendingNewIntents;
3770 }
3771 }
3772 break;
3773 }
3774 }
3775
3776 if (target == null) {
3777 target = new ActivityClientRecord();
3778 target.token = token;
3779 target.pendingResults = pendingResults;
3780 target.pendingIntents = pendingNewIntents;
3781 if (!fromServer) {
3782 ActivityClientRecord existing = mActivities.get(token);
3783 if (existing != null) {
3784 target.startsNotResumed = existing.paused;
3785 }
3786 target.onlyLocalRequest = true;
3787 }
3788 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003789 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003790 }
3791
3792 if (fromServer) {
3793 target.startsNotResumed = notResumed;
3794 target.onlyLocalRequest = false;
3795 }
3796 if (config != null) {
3797 target.createdConfig = config;
3798 }
3799 target.pendingConfigChanges |= configChanges;
3800 }
3801 }
3802
Romain Guy65b345f2011-07-27 18:51:50 -07003803 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003804 // If we are getting ready to gc after going to the background, well
3805 // we are back active so skip it.
3806 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003807 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808
3809 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003810 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 // First: make sure we have the most recent configuration and most
3813 // recent version of the activity, or skip it if some previous call
3814 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003815 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003816 int N = mRelaunchingActivities.size();
3817 IBinder token = tmp.token;
3818 tmp = null;
3819 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003820 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003821 if (r.token == token) {
3822 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003823 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 mRelaunchingActivities.remove(i);
3825 i--;
3826 N--;
3827 }
3828 }
Bob Leee5408332009-09-04 18:31:17 -07003829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003830 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003831 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003832 return;
3833 }
Bob Leee5408332009-09-04 18:31:17 -07003834
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003835 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3836 + tmp.token + " with configChanges=0x"
3837 + Integer.toHexString(configChanges));
3838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 if (mPendingConfiguration != null) {
3840 changedConfig = mPendingConfiguration;
3841 mPendingConfiguration = null;
3842 }
3843 }
Bob Leee5408332009-09-04 18:31:17 -07003844
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003845 if (tmp.createdConfig != null) {
3846 // If the activity manager is passing us its current config,
3847 // assume that is really what we want regardless of what we
3848 // may have pending.
3849 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003850 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3851 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3852 if (changedConfig == null
3853 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3854 changedConfig = tmp.createdConfig;
3855 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003856 }
3857 }
3858
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003859 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003860 + tmp.token + ": changedConfig=" + changedConfig);
3861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 // If there was a pending configuration change, execute it first.
3863 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003864 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003865 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003866 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003867 }
Bob Leee5408332009-09-04 18:31:17 -07003868
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003869 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003870 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 if (r == null) {
3872 return;
3873 }
Bob Leee5408332009-09-04 18:31:17 -07003874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003876 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003877 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003878
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003879 r.activity.mChangingConfigurations = true;
3880
Dianne Hackborne2b04802010-12-09 09:24:55 -08003881 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003883 performPauseActivity(r.token, false, r.isPreHoneycomb());
3884 }
3885 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07003886 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 }
Bob Leee5408332009-09-04 18:31:17 -07003888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003891 r.activity = null;
3892 r.window = null;
3893 r.hideForNow = false;
3894 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003895 // Merge any pending results and pending intents; don't just replace them
3896 if (tmp.pendingResults != null) {
3897 if (r.pendingResults == null) {
3898 r.pendingResults = tmp.pendingResults;
3899 } else {
3900 r.pendingResults.addAll(tmp.pendingResults);
3901 }
3902 }
3903 if (tmp.pendingIntents != null) {
3904 if (r.pendingIntents == null) {
3905 r.pendingIntents = tmp.pendingIntents;
3906 } else {
3907 r.pendingIntents.addAll(tmp.pendingIntents);
3908 }
3909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003911
Christopher Tateb70f3df2009-04-07 16:07:59 -07003912 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 }
3914
Craig Mautnera0026042014-04-23 11:45:37 -07003915 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
3916 r.state = new Bundle();
3917 r.state.setAllowFds(false);
3918 if (r.isPersistable()) {
3919 r.persistentState = new PersistableBundle();
3920 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
3921 r.persistentState);
3922 } else {
3923 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
3924 }
3925 }
3926
Dianne Hackborn73c14162012-09-19 15:45:06 -07003927 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003928 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003929 ArrayList<ComponentCallbacks2> callbacks
3930 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003931
Craig Mautner88c05892013-06-28 09:47:45 -07003932 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003933 final int NAPP = mAllApplications.size();
3934 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003935 callbacks.add(mAllApplications.get(i));
3936 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003937 final int NACT = mActivities.size();
3938 for (int i=0; i<NACT; i++) {
3939 ActivityClientRecord ar = mActivities.valueAt(i);
3940 Activity a = ar.activity;
3941 if (a != null) {
3942 Configuration thisConfig = applyConfigCompatMainThread(
3943 mCurDefaultDisplayDpi, newConfig,
3944 ar.packageInfo.getCompatibilityInfo());
3945 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3946 // If the activity is currently resumed, its configuration
3947 // needs to change right now.
3948 callbacks.add(a);
3949 } else if (thisConfig != null) {
3950 // Otherwise, we will tell it about the change
3951 // the next time it is resumed or shown. Note that
3952 // the activity manager may, before then, decide the
3953 // activity needs to be destroyed to handle its new
3954 // configuration.
3955 if (DEBUG_CONFIGURATION) {
3956 Slog.v(TAG, "Setting activity "
3957 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003958 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003959 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003960 }
3961 }
3962 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003963 final int NSVC = mServices.size();
3964 for (int i=0; i<NSVC; i++) {
3965 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003966 }
3967 }
3968 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003969 final int NPRV = mLocalProviders.size();
3970 for (int i=0; i<NPRV; i++) {
3971 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 }
3973 }
Bob Leee5408332009-09-04 18:31:17 -07003974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003975 return callbacks;
3976 }
Bob Leee5408332009-09-04 18:31:17 -07003977
Romain Guya998dff2012-03-23 18:58:36 -07003978 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003979 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003980 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 // we check the runtime type and act accordingly.
3982 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
3983 if (activity != null) {
3984 activity.mCalled = false;
3985 }
Bob Leee5408332009-09-04 18:31:17 -07003986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 boolean shouldChangeConfig = false;
3988 if ((activity == null) || (activity.mCurrentConfig == null)) {
3989 shouldChangeConfig = true;
3990 } else {
Bob Leee5408332009-09-04 18:31:17 -07003991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003992 // If the new config is the same as the config this Activity
3993 // is already running with then don't bother calling
3994 // onConfigurationChanged
3995 int diff = activity.mCurrentConfig.diff(config);
3996 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003997 // If this activity doesn't handle any of the config changes
3998 // then don't bother calling onConfigurationChanged as we're
3999 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07004000 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 shouldChangeConfig = true;
4002 }
4003 }
4004 }
Bob Leee5408332009-09-04 18:31:17 -07004005
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004006 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004007 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 if (shouldChangeConfig) {
4009 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07004010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004011 if (activity != null) {
4012 if (!activity.mCalled) {
4013 throw new SuperNotCalledException(
4014 "Activity " + activity.getLocalClassName() +
4015 " did not call through to super.onConfigurationChanged()");
4016 }
4017 activity.mConfigChangeFlags = 0;
4018 activity.mCurrentConfig = new Configuration(config);
4019 }
4020 }
4021 }
4022
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004023 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004024 synchronized (mResourcesManager) {
4025 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004026 }
4027 }
4028
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004029 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004030 Configuration config = mConfiguration;
4031 if (mCompatConfiguration == null) {
4032 mCompatConfiguration = new Configuration();
4033 }
4034 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004035 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004036 config = mCompatConfiguration;
4037 }
4038 return config;
4039 }
4040
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004041 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004042
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004043 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004044
Craig Mautner88c05892013-06-28 09:47:45 -07004045 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004046 if (mPendingConfiguration != null) {
4047 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4048 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004049 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004050 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004051 }
4052 mPendingConfiguration = null;
4053 }
4054
4055 if (config == null) {
4056 return;
4057 }
4058
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004059 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004060 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004061
4062 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4063
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 if (mConfiguration == null) {
4065 mConfiguration = new Configuration();
4066 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004067 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004068 return;
4069 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004070 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004071 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004072 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004073 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004074
4075 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4076
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004077 freeTextLayoutCachesIfNeeded(configDiff);
4078
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004079 if (callbacks != null) {
4080 final int N = callbacks.size();
4081 for (int i=0; i<N; i++) {
4082 performConfigurationChanged(callbacks.get(i), config);
4083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 }
4085 }
4086
Romain Guy46bfc482013-08-16 18:38:29 -07004087 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004088 if (configDiff != 0) {
4089 // Ask text layout engine to free its caches if there is a locale change
4090 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4091 if (hasLocaleConfigChange) {
4092 Canvas.freeTextLayoutCaches();
4093 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4094 }
4095 }
4096 }
4097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004098 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004099 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004100 if (r == null || r.activity == null) {
4101 return;
4102 }
Bob Leee5408332009-09-04 18:31:17 -07004103
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004104 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004105 + r.activityInfo.name);
4106
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004107 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004108
4109 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004110
4111 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112 }
4113
Jeff Hao1b012d32014-08-20 10:35:34 -07004114 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004115 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004116 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004117 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004118 default:
4119 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004120 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004121 break;
4122 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004123 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004124 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004125 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004126 } finally {
4127 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004128 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004129 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004130 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004131 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004132 }
4133 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004134 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004135 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004136 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004137 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004138 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004139 }
4140 }
Bob Leee5408332009-09-04 18:31:17 -07004141
Romain Guya998dff2012-03-23 18:58:36 -07004142 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004143 if (managed) {
4144 try {
4145 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4146 } catch (IOException e) {
4147 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4148 + " -- can the process access this path?");
4149 } finally {
4150 try {
4151 dhd.fd.close();
4152 } catch (IOException e) {
4153 Slog.w(TAG, "Failure closing profile fd", e);
4154 }
4155 }
4156 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004157 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004158 }
4159 }
4160
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004161 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4162 boolean hasPkgInfo = false;
4163 if (packages != null) {
4164 for (int i=packages.length-1; i>=0; i--) {
4165 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4166 if (!hasPkgInfo) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004167 WeakReference<LoadedApk> ref;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004168 ref = mPackages.get(packages[i]);
4169 if (ref != null && ref.get() != null) {
4170 hasPkgInfo = true;
4171 } else {
4172 ref = mResourcePackages.get(packages[i]);
4173 if (ref != null && ref.get() != null) {
4174 hasPkgInfo = true;
4175 }
4176 }
4177 }
4178 mPackages.remove(packages[i]);
4179 mResourcePackages.remove(packages[i]);
4180 }
4181 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004182 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004183 hasPkgInfo);
4184 }
4185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004186 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004187 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004189 final int N = callbacks.size();
4190 for (int i=0; i<N; i++) {
4191 callbacks.get(i).onLowMemory();
4192 }
4193
Chris Tatece229052009-03-25 16:44:52 -07004194 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4195 if (Process.myUid() != Process.SYSTEM_UID) {
4196 int sqliteReleased = SQLiteDatabase.releaseMemory();
4197 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4198 }
Bob Leee5408332009-09-04 18:31:17 -07004199
Mike Reedcaf0df12009-04-27 14:32:05 -04004200 // Ask graphics to free up as much as possible (font/image caches)
4201 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004203 // Ask text layout engine to free also as much as possible
4204 Canvas.freeTextLayoutCaches();
4205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004206 BinderInternal.forceGc("mem");
4207 }
4208
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004209 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004210 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004211
Dianne Hackborn73c14162012-09-19 15:45:06 -07004212 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004213
4214 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004215 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004216 callbacks.get(i).onTrimMemory(level);
4217 }
Romain Guy19f86e82012-04-23 15:19:07 -07004218
John Reckf47a5942014-06-30 16:20:04 -07004219 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004220 }
4221
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004222 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004223 if (Process.isIsolated()) {
4224 // Isolated processes aren't going to do UI.
4225 return;
4226 }
Romain Guya9582652011-11-10 14:20:10 -08004227 try {
4228 int uid = Process.myUid();
4229 String[] packages = getPackageManager().getPackagesForUid(uid);
4230
4231 // If there are several packages in this application we won't
4232 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004233 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004234 HardwareRenderer.setupDiskCache(cacheDir);
4235 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004236 }
4237 } catch (RemoteException e) {
4238 // Ignore
4239 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004240 }
4241
4242 private void updateDefaultDensity() {
4243 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4244 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4245 && !mDensityCompatMode) {
4246 Slog.i(TAG, "Switching default density from "
4247 + DisplayMetrics.DENSITY_DEVICE + " to "
4248 + mCurDefaultDisplayDpi);
4249 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4250 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4251 }
4252 }
4253
Romain Guy65b345f2011-07-27 18:51:50 -07004254 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 mBoundApplication = data;
4256 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004257 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004258
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004259 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004260 if (data.initProfilerInfo != null) {
4261 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4262 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4263 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4264 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4265 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004268 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004269 android.ddm.DdmHandleAppName.setAppName(data.processName,
4270 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004271
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004272 if (data.persistent) {
4273 // Persistent processes on low-memory devices do not get to
4274 // use hardware accelerated drawing, since this can add too much
4275 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004276 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004277 HardwareRenderer.disable(false);
4278 }
4279 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004280
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004281 if (mProfiler.profileFd != null) {
4282 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004283 }
4284
Joe Onoratod630f102011-03-17 18:42:26 -07004285 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4286 // implementation to use the pool executor. Normally, we use the
4287 // serialized executor as the default. This has to happen in the
4288 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004289 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004290 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4291 }
4292
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004293 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004295 /*
4296 * Before spawning a new process, reset the time zone to be the system time zone.
4297 * This needs to be done because the system time zone could have changed after the
4298 * the spawning of this process. Without doing this this process would have the incorrect
4299 * system time zone.
4300 */
4301 TimeZone.setDefault(null);
4302
4303 /*
4304 * Initialize the default locale in this process for the reasons we set the time zone.
4305 */
4306 Locale.setDefault(data.config.locale);
4307
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004308 /*
4309 * Update the system configuration since its preloaded and might not
4310 * reflect configuration changes. The configuration object passed
4311 * in AppBindData can be safely assumed to be up to date
4312 */
Craig Mautner88c05892013-06-28 09:47:45 -07004313 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004314 mCurDefaultDisplayDpi = data.config.densityDpi;
4315 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004316
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004317 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318
Dianne Hackborndde331c2012-08-03 14:01:57 -07004319 /**
4320 * Switch this process to density compatibility mode if needed.
4321 */
4322 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4323 == 0) {
4324 mDensityCompatMode = true;
4325 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4326 }
4327 updateDefaultDensity();
4328
Jeff Browndefd4a62014-03-10 21:24:37 -07004329 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004330 if (!Process.isIsolated()) {
4331 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004332
Amith Yamasani92d57052012-08-23 10:07:52 -07004333 if (cacheDir != null) {
4334 // Provide a usable directory for temporary files
4335 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
4336
4337 setupGraphicsSupport(data.info, cacheDir);
4338 } else {
4339 Log.e(TAG, "Unable to setupGraphicsSupport due to missing cache directory");
4340 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004341 }
Narayan Kamathccb2a082013-12-19 14:49:36 +00004342
4343
4344 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4345 DateFormat.set24HourTimePref(is24Hr);
4346
Jon Miranda836c0a82014-08-11 12:32:26 -07004347 View.mDebugViewAttributes =
4348 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4349
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004350 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004351 * For system applications on userdebug/eng builds, log stack
4352 * traces of disk and network access to dropbox for analysis.
4353 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004354 if ((data.appInfo.flags &
4355 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004356 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4357 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004358 }
4359
4360 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004361 * For apps targetting SDK Honeycomb or later, we don't allow
4362 * network usage on the main event loop / UI thread.
4363 *
4364 * Note to those grepping: this is what ultimately throws
4365 * NetworkOnMainThreadException ...
4366 */
4367 if (data.appInfo.targetSdkVersion > 9) {
4368 StrictMode.enableDeathOnNetwork();
4369 }
4370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004371 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4372 // XXX should have option to change the port.
4373 Debug.changeDebugPort(8100);
4374 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004375 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004376 + " is waiting for the debugger on port 8100...");
4377
4378 IActivityManager mgr = ActivityManagerNative.getDefault();
4379 try {
4380 mgr.showWaitingForDebugger(mAppThread, true);
4381 } catch (RemoteException ex) {
4382 }
4383
4384 Debug.waitForDebugger();
4385
4386 try {
4387 mgr.showWaitingForDebugger(mAppThread, false);
4388 } catch (RemoteException ex) {
4389 }
4390
4391 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004392 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 + " can be debugged on port 8100...");
4394 }
4395 }
4396
Siva Velusamy92a8b222012-03-09 16:24:04 -08004397 // Enable OpenGL tracing if required
4398 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004399 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004400 }
4401
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004402 // Allow application-generated systrace messages if we're debuggable.
4403 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4404 Trace.setAppTracingAllowed(appTracingAllowed);
4405
Robert Greenwalt434203a2010-10-11 16:00:27 -07004406 /**
4407 * Initialize the default http proxy in this process for the reasons we set the time zone.
4408 */
4409 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004410 if (b != null) {
4411 // In pre-boot mode (doing initial launch to collect password), not
4412 // all system is up. This includes the connectivity service, so don't
4413 // crash if we can't get it.
4414 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4415 try {
Jason Monk207900c2014-04-25 15:00:09 -04004416 ProxyInfo proxyInfo = service.getProxy();
4417 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004418 } catch (RemoteException e) {}
4419 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004421 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 InstrumentationInfo ii = null;
4423 try {
4424 ii = appContext.getPackageManager().
4425 getInstrumentationInfo(data.instrumentationName, 0);
4426 } catch (PackageManager.NameNotFoundException e) {
4427 }
4428 if (ii == null) {
4429 throw new RuntimeException(
4430 "Unable to find instrumentation info for: "
4431 + data.instrumentationName);
4432 }
4433
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004434 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004435 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004436 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
4437 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004439 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
4440 mInstrumentedLibDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441
4442 ApplicationInfo instrApp = new ApplicationInfo();
4443 instrApp.packageName = ii.packageName;
4444 instrApp.sourceDir = ii.sourceDir;
4445 instrApp.publicSourceDir = ii.publicSourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004446 instrApp.splitSourceDirs = ii.splitSourceDirs;
4447 instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004448 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004449 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004450 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07004451 appContext.getClassLoader(), false, true, false);
Jeff Browndefd4a62014-03-10 21:24:37 -07004452 ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453
4454 try {
4455 java.lang.ClassLoader cl = instrContext.getClassLoader();
4456 mInstrumentation = (Instrumentation)
4457 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4458 } catch (Exception e) {
4459 throw new RuntimeException(
4460 "Unable to instantiate instrumentation "
4461 + data.instrumentationName + ": " + e.toString(), e);
4462 }
4463
4464 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004465 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4466 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004468 if (mProfiler.profileFile != null && !ii.handleProfiling
4469 && mProfiler.profileFd == null) {
4470 mProfiler.handlingProfiling = true;
4471 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472 file.getParentFile().mkdirs();
4473 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4474 }
4475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004476 } else {
4477 mInstrumentation = new Instrumentation();
4478 }
4479
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004480 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004481 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004482 }
4483
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004484 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004485 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004486 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004487 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004488 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004489 // If the app is being launched for full backup or restore, bring it up in
4490 // a restricted environment with the base application class.
4491 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4492 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004493
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004494 // don't bring up providers in restricted mode; they may depend on the
4495 // app's custom Application class
4496 if (!data.restrictedBackupMode) {
4497 List<ProviderInfo> providers = data.providers;
4498 if (providers != null) {
4499 installContentProviders(app, providers);
4500 // For process that contains content providers, we want to
4501 // ensure that the JIT is enabled "at some point".
4502 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4503 }
4504 }
4505
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004506 // Do this after providers, since instrumentation tests generally start their
4507 // test thread at this point, and we don't want that racing.
4508 try {
4509 mInstrumentation.onCreate(data.instrumentationArgs);
4510 }
4511 catch (Exception e) {
4512 throw new RuntimeException(
4513 "Exception thrown in onCreate() of "
4514 + data.instrumentationName + ": " + e.toString(), e);
4515 }
4516
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004517 try {
4518 mInstrumentation.callApplicationOnCreate(app);
4519 } catch (Exception e) {
4520 if (!mInstrumentation.onException(app, e)) {
4521 throw new RuntimeException(
4522 "Unable to create application " + app.getClass().getName()
4523 + ": " + e.toString(), e);
4524 }
4525 }
4526 } finally {
4527 StrictMode.setThreadPolicy(savedPolicy);
4528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004529 }
4530
4531 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4532 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004533 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4534 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004535 Debug.stopMethodTracing();
4536 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004537 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004538 // + ", app thr: " + mAppThread);
4539 try {
4540 am.finishInstrumentation(mAppThread, resultCode, results);
4541 } catch (RemoteException ex) {
4542 }
4543 }
4544
Romain Guy65b345f2011-07-27 18:51:50 -07004545 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 Context context, List<ProviderInfo> providers) {
4547 final ArrayList<IActivityManager.ContentProviderHolder> results =
4548 new ArrayList<IActivityManager.ContentProviderHolder>();
4549
Romain Guya998dff2012-03-23 18:58:36 -07004550 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004551 if (DEBUG_PROVIDER) {
4552 StringBuilder buf = new StringBuilder(128);
4553 buf.append("Pub ");
4554 buf.append(cpi.authority);
4555 buf.append(": ");
4556 buf.append(cpi.name);
4557 Log.i(TAG, buf.toString());
4558 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004559 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4560 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4561 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004562 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 }
4565 }
4566
4567 try {
4568 ActivityManagerNative.getDefault().publishContentProviders(
4569 getApplicationThread(), results);
4570 } catch (RemoteException ex) {
4571 }
4572 }
4573
Jeff Sharkey6d515712012-09-20 16:06:08 -07004574 public final IContentProvider acquireProvider(
4575 Context c, String auth, int userId, boolean stable) {
4576 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004577 if (provider != null) {
4578 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 }
4580
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004581 // There is a possible race here. Another thread may try to acquire
4582 // the same provider at the same time. When this happens, we want to ensure
4583 // that the first one wins.
4584 // Note that we cannot hold the lock while acquiring and installing the
4585 // provider since it might take a long time to run and it could also potentially
4586 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004587 IActivityManager.ContentProviderHolder holder = null;
4588 try {
4589 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004590 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004591 } catch (RemoteException ex) {
4592 }
4593 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004594 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 return null;
4596 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004598 // Install provider will increment the reference count for us, and break
4599 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004600 holder = installProvider(c, holder, holder.info,
4601 true /*noisy*/, holder.noReleaseNeeded, stable);
4602 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004603 }
4604
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004605 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4606 if (stable) {
4607 prc.stableCount += 1;
4608 if (prc.stableCount == 1) {
4609 // We are acquiring a new stable reference on the provider.
4610 int unstableDelta;
4611 if (prc.removePending) {
4612 // We have a pending remove operation, which is holding the
4613 // last unstable reference. At this point we are converting
4614 // that unstable reference to our new stable reference.
4615 unstableDelta = -1;
4616 // Cancel the removal of the provider.
4617 if (DEBUG_PROVIDER) {
4618 Slog.v(TAG, "incProviderRef: stable "
4619 + "snatched provider from the jaws of death");
4620 }
4621 prc.removePending = false;
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004622 // There is a race! It fails to remove the message, which
4623 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004624 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4625 } else {
4626 unstableDelta = 0;
4627 }
4628 try {
4629 if (DEBUG_PROVIDER) {
4630 Slog.v(TAG, "incProviderRef Now stable - "
4631 + prc.holder.info.name + ": unstableDelta="
4632 + unstableDelta);
4633 }
4634 ActivityManagerNative.getDefault().refContentProvider(
4635 prc.holder.connection, 1, unstableDelta);
4636 } catch (RemoteException e) {
4637 //do nothing content provider object is dead any way
4638 }
4639 }
4640 } else {
4641 prc.unstableCount += 1;
4642 if (prc.unstableCount == 1) {
4643 // We are acquiring a new unstable reference on the provider.
4644 if (prc.removePending) {
4645 // Oh look, we actually have a remove pending for the
4646 // provider, which is still holding the last unstable
4647 // reference. We just need to cancel that to take new
4648 // ownership of the reference.
4649 if (DEBUG_PROVIDER) {
4650 Slog.v(TAG, "incProviderRef: unstable "
4651 + "snatched provider from the jaws of death");
4652 }
4653 prc.removePending = false;
4654 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4655 } else {
4656 // First unstable ref, increment our count in the
4657 // activity manager.
4658 try {
4659 if (DEBUG_PROVIDER) {
4660 Slog.v(TAG, "incProviderRef: Now unstable - "
4661 + prc.holder.info.name);
4662 }
4663 ActivityManagerNative.getDefault().refContentProvider(
4664 prc.holder.connection, 0, 1);
4665 } catch (RemoteException e) {
4666 //do nothing content provider object is dead any way
4667 }
4668 }
4669 }
4670 }
4671 }
4672
Jeff Sharkey6d515712012-09-20 16:06:08 -07004673 public final IContentProvider acquireExistingProvider(
4674 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004675 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004676 final ProviderKey key = new ProviderKey(auth, userId);
4677 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004678 if (pr == null) {
4679 return null;
4680 }
4681
4682 IContentProvider provider = pr.mProvider;
4683 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004684 if (!jBinder.isBinderAlive()) {
4685 // The hosting process of the provider has died; we can't
4686 // use this one.
4687 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4688 + ": existing object's process dead");
4689 handleUnstableProviderDiedLocked(jBinder, true);
4690 return null;
4691 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004692
4693 // Only increment the ref count if we have one. If we don't then the
4694 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004695 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004696 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004697 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004698 }
4699 return provider;
4700 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004701 }
4702
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004703 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4704 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004705 return false;
4706 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004708 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004709 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004710 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004711 if (prc == null) {
4712 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004713 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004714 }
4715
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004716 boolean lastRef = false;
4717 if (stable) {
4718 if (prc.stableCount == 0) {
4719 if (DEBUG_PROVIDER) Slog.v(TAG,
4720 "releaseProvider: stable ref count already 0, how?");
4721 return false;
4722 }
4723 prc.stableCount -= 1;
4724 if (prc.stableCount == 0) {
4725 // What we do at this point depends on whether there are
4726 // any unstable refs left: if there are, we just tell the
4727 // activity manager to decrement its stable count; if there
4728 // aren't, we need to enqueue this provider to be removed,
4729 // and convert to holding a single unstable ref while
4730 // doing so.
4731 lastRef = prc.unstableCount == 0;
4732 try {
4733 if (DEBUG_PROVIDER) {
4734 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4735 + lastRef + " - " + prc.holder.info.name);
4736 }
4737 ActivityManagerNative.getDefault().refContentProvider(
4738 prc.holder.connection, -1, lastRef ? 1 : 0);
4739 } catch (RemoteException e) {
4740 //do nothing content provider object is dead any way
4741 }
4742 }
4743 } else {
4744 if (prc.unstableCount == 0) {
4745 if (DEBUG_PROVIDER) Slog.v(TAG,
4746 "releaseProvider: unstable ref count already 0, how?");
4747 return false;
4748 }
4749 prc.unstableCount -= 1;
4750 if (prc.unstableCount == 0) {
4751 // If this is the last reference, we need to enqueue
4752 // this provider to be removed instead of telling the
4753 // activity manager to remove it at this point.
4754 lastRef = prc.stableCount == 0;
4755 if (!lastRef) {
4756 try {
4757 if (DEBUG_PROVIDER) {
4758 Slog.v(TAG, "releaseProvider: No longer unstable - "
4759 + prc.holder.info.name);
4760 }
4761 ActivityManagerNative.getDefault().refContentProvider(
4762 prc.holder.connection, 0, -1);
4763 } catch (RemoteException e) {
4764 //do nothing content provider object is dead any way
4765 }
4766 }
4767 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004768 }
4769
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004770 if (lastRef) {
4771 if (!prc.removePending) {
4772 // Schedule the actual remove asynchronously, since we don't know the context
4773 // this will be called in.
4774 // TODO: it would be nice to post a delayed message, so
4775 // if we come back and need the same provider quickly
4776 // we will still have it available.
4777 if (DEBUG_PROVIDER) {
4778 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4779 + prc.holder.info.name);
4780 }
4781 prc.removePending = true;
4782 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4783 mH.sendMessage(msg);
4784 } else {
4785 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4786 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004787 }
4788 return true;
4789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004790 }
4791
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004792 final void completeRemoveProvider(ProviderRefCount prc) {
4793 synchronized (mProviderMap) {
4794 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004795 // There was a race! Some other client managed to acquire
4796 // the provider before the removal was completed.
4797 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004798 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004799 + "provider still in use");
4800 return;
4801 }
4802
Guobin Zhang9e3e5262013-03-21 13:57:11 +08004803 // More complicated race!! Some client managed to acquire the
4804 // provider and release it before the removal was completed.
4805 // Continue the removal, and abort the next remove message.
4806 prc.removePending = false;
4807
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004808 final IBinder jBinder = prc.holder.provider.asBinder();
4809 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4810 if (existingPrc == prc) {
4811 mProviderRefCountMap.remove(jBinder);
4812 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004813
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004814 for (int i=mProviderMap.size()-1; i>=0; i--) {
4815 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004816 IBinder myBinder = pr.mProvider.asBinder();
4817 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004818 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004819 }
4820 }
4821 }
4822
4823 try {
4824 if (DEBUG_PROVIDER) {
4825 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4826 + "removeContentProvider(" + prc.holder.info.name + ")");
4827 }
4828 ActivityManagerNative.getDefault().removeContentProvider(
4829 prc.holder.connection, false);
4830 } catch (RemoteException e) {
4831 //do nothing content provider object is dead any way
4832 }
4833 }
4834
4835 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004836 synchronized (mProviderMap) {
4837 handleUnstableProviderDiedLocked(provider, fromClient);
4838 }
4839 }
4840
4841 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4842 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4843 if (prc != null) {
4844 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4845 + provider + " " + prc.holder.info.name);
4846 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004847 for (int i=mProviderMap.size()-1; i>=0; i--) {
4848 ProviderClientRecord pr = mProviderMap.valueAt(i);
4849 if (pr != null && pr.mProvider.asBinder() == provider) {
4850 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4851 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004852 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004853 }
You Kimbc74de62013-10-01 00:13:26 +09004854
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004855 if (fromClient) {
4856 // We found out about this due to execution in our client
4857 // code. Tell the activity manager about it now, to ensure
4858 // that the next time we go to do anything with the provider
4859 // it knows it is dead (so we don't race with its death
4860 // notification).
4861 try {
4862 ActivityManagerNative.getDefault().unstableProviderDied(
4863 prc.holder.connection);
4864 } catch (RemoteException e) {
4865 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004866 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004867 }
4868 }
4869 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004870
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004871 final void appNotRespondingViaProvider(IBinder provider) {
4872 synchronized (mProviderMap) {
4873 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4874 if (prc != null) {
4875 try {
4876 ActivityManagerNative.getDefault()
4877 .appNotRespondingViaProvider(prc.holder.connection);
4878 } catch (RemoteException e) {
4879 }
4880 }
4881 }
4882 }
4883
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004884 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004885 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4886 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4887 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4888
4889 final ProviderClientRecord pcr = new ProviderClientRecord(
4890 auths, provider, localProvider, holder);
4891 for (String auth : auths) {
4892 final ProviderKey key = new ProviderKey(auth, userId);
4893 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004894 if (existing != null) {
4895 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004896 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004897 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004898 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004899 }
4900 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004901 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 }
4903
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004904 /**
4905 * Installs the provider.
4906 *
4907 * Providers that are local to the process or that come from the system server
4908 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4909 * Other remote providers are reference counted. The initial reference count
4910 * for all reference counted providers is one. Providers that are not reference
4911 * counted do not have a reference count (at all).
4912 *
4913 * This method detects when a provider has already been installed. When this happens,
4914 * it increments the reference count of the existing provider (if appropriate)
4915 * and returns the existing provider. This can happen due to concurrent
4916 * attempts to acquire the same provider.
4917 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004918 private IActivityManager.ContentProviderHolder installProvider(Context context,
4919 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4920 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004922 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004923 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004924 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004925 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004926 + info.name);
4927 }
4928 Context c = null;
4929 ApplicationInfo ai = info.applicationInfo;
4930 if (context.getPackageName().equals(ai.packageName)) {
4931 c = context;
4932 } else if (mInitialApplication != null &&
4933 mInitialApplication.getPackageName().equals(ai.packageName)) {
4934 c = mInitialApplication;
4935 } else {
4936 try {
4937 c = context.createPackageContext(ai.packageName,
4938 Context.CONTEXT_INCLUDE_CODE);
4939 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004940 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004941 }
4942 }
4943 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004944 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004945 ai.packageName +
4946 " while loading content provider " +
4947 info.name);
4948 return null;
4949 }
4950 try {
4951 final java.lang.ClassLoader cl = c.getClassLoader();
4952 localProvider = (ContentProvider)cl.
4953 loadClass(info.name).newInstance();
4954 provider = localProvider.getIContentProvider();
4955 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004956 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004957 info.name + " from sourceDir " +
4958 info.applicationInfo.sourceDir);
4959 return null;
4960 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004961 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 TAG, "Instantiating local provider " + info.name);
4963 // XXX Need to create the correct context for this provider.
4964 localProvider.attachInfo(c, info);
4965 } catch (java.lang.Exception e) {
4966 if (!mInstrumentation.onException(null, e)) {
4967 throw new RuntimeException(
4968 "Unable to get provider " + info.name
4969 + ": " + e.toString(), e);
4970 }
4971 return null;
4972 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004973 } else {
4974 provider = holder.provider;
4975 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 + info.name);
4977 }
4978
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004979 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004980
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004981 synchronized (mProviderMap) {
4982 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
4983 + " / " + info.name);
4984 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004986 ComponentName cname = new ComponentName(info.packageName, info.name);
4987 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004988 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004989 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004990 Slog.v(TAG, "installProvider: lost the race, "
4991 + "using existing local provider");
4992 }
4993 provider = pr.mProvider;
4994 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004995 holder = new IActivityManager.ContentProviderHolder(info);
4996 holder.provider = provider;
4997 holder.noReleaseNeeded = true;
4998 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004999 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005000 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005001 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005002 retHolder = pr.mHolder;
5003 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005004 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5005 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005006 if (DEBUG_PROVIDER) {
5007 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005008 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005009 // We need to transfer our new reference to the existing
5010 // ref count, releasing the old one... but only if
5011 // release is needed (that is, it is not running in the
5012 // system process).
5013 if (!noReleaseNeeded) {
5014 incProviderRefLocked(prc, stable);
5015 try {
5016 ActivityManagerNative.getDefault().removeContentProvider(
5017 holder.connection, stable);
5018 } catch (RemoteException e) {
5019 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005020 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005021 }
5022 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005023 ProviderClientRecord client = installProviderAuthoritiesLocked(
5024 provider, localProvider, holder);
5025 if (noReleaseNeeded) {
5026 prc = new ProviderRefCount(holder, client, 1000, 1000);
5027 } else {
5028 prc = stable
5029 ? new ProviderRefCount(holder, client, 1, 0)
5030 : new ProviderRefCount(holder, client, 0, 1);
5031 }
5032 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005033 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005034 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005035 }
5036 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005037
5038 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005039 }
5040
Romain Guy65b345f2011-07-27 18:51:50 -07005041 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005042 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005044 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005045 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005046 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005047 public void run() {
5048 ensureJitEnabled();
5049 }
5050 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005051 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5052 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005053 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005054 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005055 try {
5056 mgr.attachApplication(mAppThread);
5057 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005058 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005059 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005060 // Watch for getting close to heap limit.
5061 BinderInternal.addGcWatcher(new Runnable() {
5062 @Override public void run() {
5063 if (!mSomeActivitiesChanged) {
5064 return;
5065 }
5066 Runtime runtime = Runtime.getRuntime();
5067 long dalvikMax = runtime.maxMemory();
5068 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5069 if (dalvikUsed > ((3*dalvikMax)/4)) {
5070 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5071 + " total=" + (runtime.totalMemory()/1024)
5072 + " used=" + (dalvikUsed/1024));
5073 mSomeActivitiesChanged = false;
5074 try {
5075 mgr.releaseSomeActivities(mAppThread);
5076 } catch (RemoteException e) {
5077 }
5078 }
5079 }
5080 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005081 } else {
5082 // Don't set application object here -- if the system crashes,
5083 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005084 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005085 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 try {
5087 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005088 ContextImpl context = ContextImpl.createAppContext(
5089 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005090 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5091 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 } catch (Exception e) {
5093 throw new RuntimeException(
5094 "Unable to instantiate Application():" + e.toString(), e);
5095 }
5096 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005097
5098 // add dropbox logging to libcore
5099 DropBox.setReporter(new DropBoxReporter());
5100
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005101 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005102 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005103 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005104 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005105 // We need to apply this change to the resources
5106 // immediately, because upon returning the view
5107 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005108 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005109 // This actually changed the resources! Tell
5110 // everyone about it.
5111 if (mPendingConfiguration == null ||
5112 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5113 mPendingConfiguration = newConfig;
5114
Jeff Brown9ef09972013-10-15 20:49:59 -07005115 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005116 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005117 }
5118 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005119 }
Craig Mautner88c05892013-06-28 09:47:45 -07005120 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005121 public void onLowMemory() {
5122 }
Craig Mautner88c05892013-06-28 09:47:45 -07005123 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005124 public void onTrimMemory(int level) {
5125 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005126 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 }
5128
Romain Guy5e9120d2012-01-30 12:17:22 -08005129 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005130 // The system process on low-memory devices do not get to use hardware
5131 // accelerated drawing, since this can add too much overhead to the
5132 // process.
5133 if (!ActivityManager.isHighEndGfx()) {
5134 HardwareRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005135 } else {
5136 HardwareRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138 ActivityThread thread = new ActivityThread();
5139 thread.attach(true);
5140 return thread;
5141 }
5142
Jeff Brown10e89712011-07-08 18:52:57 -07005143 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005144 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005145 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005146 }
5147 }
5148
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005149 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005150 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005151 if (mCoreSettings != null) {
5152 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005153 }
Craig Mautner88c05892013-06-28 09:47:45 -07005154 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005155 }
5156 }
5157
Geremy Condra69689a72012-09-11 16:57:17 -07005158 private static class EventLoggingReporter implements EventLogger.Reporter {
5159 @Override
5160 public void report (int code, Object... list) {
5161 EventLog.writeEvent(code, list);
5162 }
5163 }
5164
Geremy Condrab7faaf42012-09-19 18:07:42 -07005165 private class DropBoxReporter implements DropBox.Reporter {
5166
5167 private DropBoxManager dropBox;
5168
5169 public DropBoxReporter() {
5170 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5171 }
5172
5173 @Override
5174 public void addData(String tag, byte[] data, int flags) {
5175 dropBox.addData(tag, data, flags);
5176 }
5177
5178 @Override
5179 public void addText(String tag, String data) {
5180 dropBox.addText(tag, data);
5181 }
5182 }
5183
Romain Guy65b345f2011-07-27 18:51:50 -07005184 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07005185 SamplingProfilerIntegration.start();
5186
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005187 // CloseGuard defaults to true and can be quite spammy. We
5188 // disable it here, but selectively enable it later (via
5189 // StrictMode) on debug builds, but using DropBox, not logs.
5190 CloseGuard.setEnabled(false);
5191
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005192 Environment.initForCurrentUser();
5193
Geremy Condra69689a72012-09-11 16:57:17 -07005194 // Set the reporter for event logging in libcore
5195 EventLogger.setReporter(new EventLoggingReporter());
5196
Kenny Root8b514752013-02-04 09:35:16 -08005197 Security.addProvider(new AndroidKeyStoreProvider());
5198
Robin Lee3d076af2014-04-25 14:57:49 +01005199 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5200 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5201 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 Process.setArgV0("<pre-initialized>");
5204
5205 Looper.prepareMainLooper();
5206
5207 ActivityThread thread = new ActivityThread();
5208 thread.attach(false);
5209
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005210 if (sMainThreadHandler == null) {
5211 sMainThreadHandler = thread.getHandler();
5212 }
5213
Romain Guy5e9120d2012-01-30 12:17:22 -08005214 AsyncTask.init();
5215
Dianne Hackborn287952c2010-09-22 22:34:31 -07005216 if (false) {
5217 Looper.myLooper().setMessageLogging(new
5218 LogPrinter(Log.DEBUG, "ActivityThread"));
5219 }
5220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 Looper.loop();
5222
Jeff Brown10e89712011-07-08 18:52:57 -07005223 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005224 }
5225}