blob: bffe395c31dd6623933870553d9f6553280b524a [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 Kamathccb2a0862013-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 Kamathccb2a0862013-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,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800362 token, sendingUser, intent.getFlags());
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
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001064 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001065 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001066 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1067 SQLiteDebug.dump(printer, args);
1068 pw.flush();
1069 }
1070
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001071 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001072 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1073 if (mSystemThread) {
1074 // Ensure this invocation is asynchronous to prevent
1075 // writer waiting due to buffer cannot be consumed.
1076 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1077 @Override
1078 public void run() {
1079 dumpDatabaseInfo(fd, args);
1080 }
1081 });
1082 } else {
1083 dumpDatabaseInfo(fd, args);
1084 }
1085 }
1086
1087 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001088 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001089 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001090 }
1091
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001092 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001093 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001094 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001095 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001096 cmd.activityToken = activityToken;
1097 cmd.requestToken = requestToken;
1098 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001099 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001100 }
1101
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001102 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001103 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001104 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001105
1106 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1107 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1108 ucd.pkg = pkg;
1109 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001110 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001111 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001112
1113 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001114 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001115 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001116
Craig Mautner5eda9b32013-07-02 11:58:16 -07001117 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001118 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001119 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001120
Craig Mautnereb8abf72014-07-02 15:04:09 -07001121 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1122 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1123 new Pair<IBinder, ActivityOptions>(token, options));
1124 }
1125
Dianne Hackborna413dc02013-07-12 12:02:55 -07001126 public void setProcessState(int state) {
1127 updateProcessState(state, true);
1128 }
1129
1130 public void updateProcessState(int processState, boolean fromIpc) {
1131 synchronized (this) {
1132 if (mLastProcessState != processState) {
1133 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001134 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1135 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1136 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1137 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1138 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1139 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1140 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1141 }
1142 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001143 if (false) {
1144 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1145 + (fromIpc ? " (from ipc": ""));
1146 }
1147 }
1148 }
1149 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001150
1151 @Override
1152 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001153 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001154 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001155
1156 @Override
1157 public final void updateTimePrefs(boolean is24Hour) {
1158 DateFormat.set24HourTimePref(is24Hour);
1159 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001160
1161 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001162 public void scheduleCancelVisibleBehind(IBinder token) {
1163 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001164 }
1165
1166 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001167 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1168 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001169 }
Craig Mautner8746a472014-07-24 15:12:54 -07001170
Jeff Sharkey605eb792014-11-04 13:34:06 -08001171 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001172 public void scheduleEnterAnimationComplete(IBinder token) {
1173 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1174 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001175
1176 @Override
1177 public void notifyCleartextNetwork(byte[] firstPacket) {
1178 if (StrictMode.vmCleartextNetworkEnabled()) {
1179 StrictMode.onCleartextNetworkDetected(firstPacket);
1180 }
1181 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 }
1183
Romain Guy65b345f2011-07-27 18:51:50 -07001184 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 public static final int LAUNCH_ACTIVITY = 100;
1186 public static final int PAUSE_ACTIVITY = 101;
1187 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1188 public static final int STOP_ACTIVITY_SHOW = 103;
1189 public static final int STOP_ACTIVITY_HIDE = 104;
1190 public static final int SHOW_WINDOW = 105;
1191 public static final int HIDE_WINDOW = 106;
1192 public static final int RESUME_ACTIVITY = 107;
1193 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001194 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 public static final int BIND_APPLICATION = 110;
1196 public static final int EXIT_APPLICATION = 111;
1197 public static final int NEW_INTENT = 112;
1198 public static final int RECEIVER = 113;
1199 public static final int CREATE_SERVICE = 114;
1200 public static final int SERVICE_ARGS = 115;
1201 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 public static final int CONFIGURATION_CHANGED = 118;
1204 public static final int CLEAN_UP_CONTEXT = 119;
1205 public static final int GC_WHEN_IDLE = 120;
1206 public static final int BIND_SERVICE = 121;
1207 public static final int UNBIND_SERVICE = 122;
1208 public static final int DUMP_SERVICE = 123;
1209 public static final int LOW_MEMORY = 124;
1210 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1211 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001212 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001213 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001214 public static final int DESTROY_BACKUP_AGENT = 129;
1215 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001216 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001217 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001218 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001219 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001220 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001221 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001222 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001223 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001224 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001225 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001226 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001227 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001228 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001229 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001230 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001231 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001232 public static final int CANCEL_VISIBLE_BEHIND = 147;
1233 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001234 public static final int ENTER_ANIMATION_COMPLETE = 149;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001237 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 switch (code) {
1239 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1240 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1241 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1242 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1243 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1244 case SHOW_WINDOW: return "SHOW_WINDOW";
1245 case HIDE_WINDOW: return "HIDE_WINDOW";
1246 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1247 case SEND_RESULT: return "SEND_RESULT";
1248 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1249 case BIND_APPLICATION: return "BIND_APPLICATION";
1250 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1251 case NEW_INTENT: return "NEW_INTENT";
1252 case RECEIVER: return "RECEIVER";
1253 case CREATE_SERVICE: return "CREATE_SERVICE";
1254 case SERVICE_ARGS: return "SERVICE_ARGS";
1255 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1257 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1258 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1259 case BIND_SERVICE: return "BIND_SERVICE";
1260 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1261 case DUMP_SERVICE: return "DUMP_SERVICE";
1262 case LOW_MEMORY: return "LOW_MEMORY";
1263 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1264 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001265 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001266 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1267 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001268 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001269 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001270 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001271 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001272 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001273 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001274 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001275 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001276 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001277 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001278 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001279 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001280 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001281 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001282 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001283 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001284 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001285 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1286 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001287 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 }
1289 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001290 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 }
1292 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001293 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 switch (msg.what) {
1295 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001296 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001297 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298
1299 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001300 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001301 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001302 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 } break;
1304 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001305 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001306 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001307 handleRelaunchActivity(r);
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 PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001311 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001312 handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
1313 (msg.arg1&2) != 0);
Bob Leee5408332009-09-04 18:31:17 -07001314 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001315 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 break;
1317 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001318 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001319 handlePauseActivity((IBinder)msg.obj, true, (msg.arg1&1) != 0, msg.arg2,
1320 (msg.arg1&1) != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001321 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 break;
1323 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001324 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001326 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 break;
1328 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001331 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 break;
1333 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001336 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 break;
1338 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001341 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 break;
1343 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001344 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Craig Mautner233ceee2014-05-09 17:05:11 -07001345 handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001346 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 break;
1348 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 handleSendResult((ResultData)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 DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001354 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1356 msg.arg2, false);
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 BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001360 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 AppBindData data = (AppBindData)msg.obj;
1362 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001363 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 break;
1365 case EXIT_APPLICATION:
1366 if (mInitialApplication != null) {
1367 mInitialApplication.onTerminate();
1368 }
1369 Looper.myLooper().quit();
1370 break;
1371 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001372 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001374 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 break;
1376 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001377 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001379 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001380 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001381 break;
1382 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001385 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 break;
1387 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001388 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001390 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 break;
1392 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001393 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001395 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 break;
1397 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001398 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001400 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 break;
1402 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001403 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001404 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001405 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001406 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001409 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001410 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001411 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001412 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 break;
1414 case CLEAN_UP_CONTEXT:
1415 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1416 cci.context.performFinalCleanup(cci.who, cci.what);
1417 break;
1418 case GC_WHEN_IDLE:
1419 scheduleGcIdler();
1420 break;
1421 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001422 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 break;
1424 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001425 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001427 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001428 break;
1429 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001432 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001434 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001435 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001436 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001437 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001438 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001439 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001440 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001441 break;
1442 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001444 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001445 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001446 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001447 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001448 Process.killProcess(Process.myPid());
1449 break;
1450 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001451 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001452 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001453 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001454 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001455 case ENABLE_JIT:
1456 ensureJitEnabled();
1457 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001458 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001459 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001460 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001461 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001462 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001463 case SCHEDULE_CRASH:
1464 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001465 case DUMP_HEAP:
1466 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1467 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001468 case DUMP_ACTIVITY:
1469 handleDumpActivity((DumpComponentInfo)msg.obj);
1470 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001471 case DUMP_PROVIDER:
1472 handleDumpProvider((DumpComponentInfo)msg.obj);
1473 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001474 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001475 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001476 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001477 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001478 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001479 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001480 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001481 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001482 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001483 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001484 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1485 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001486 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001487 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001488 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001489 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001490 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001491 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001492 case UNSTABLE_PROVIDER_DIED:
1493 handleUnstableProviderDied((IBinder)msg.obj, false);
1494 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001495 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1496 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001497 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001498 case TRANSLUCENT_CONVERSION_COMPLETE:
1499 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1500 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001501 case INSTALL_PROVIDER:
1502 handleInstallProvider((ProviderInfo) msg.obj);
1503 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001504 case ON_NEW_ACTIVITY_OPTIONS:
1505 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1506 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001507 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001508 case CANCEL_VISIBLE_BEHIND:
1509 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001510 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001511 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1512 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001513 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001514 case ENTER_ANIMATION_COMPLETE:
1515 handleEnterAnimationComplete((IBinder) msg.obj);
1516 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001518 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
Bob Leee5408332009-09-04 18:31:17 -07001520
Brian Carlstromed7e0072011-03-24 13:27:57 -07001521 private void maybeSnapshot() {
1522 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001523 // convert the *private* ActivityThread.PackageInfo to *public* known
1524 // android.content.pm.PackageInfo
1525 String packageName = mBoundApplication.info.mPackageName;
1526 android.content.pm.PackageInfo packageInfo = null;
1527 try {
1528 Context context = getSystemContext();
1529 if(context == null) {
1530 Log.e(TAG, "cannot get a valid context");
1531 return;
1532 }
1533 PackageManager pm = context.getPackageManager();
1534 if(pm == null) {
1535 Log.e(TAG, "cannot get a valid PackageManager");
1536 return;
1537 }
1538 packageInfo = pm.getPackageInfo(
1539 packageName, PackageManager.GET_ACTIVITIES);
1540 } catch (NameNotFoundException e) {
1541 Log.e(TAG, "cannot get package info for " + packageName, e);
1542 }
1543 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001544 }
1545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 }
1547
Romain Guy65b345f2011-07-27 18:51:50 -07001548 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001549 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001551 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001552 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001553 if (mBoundApplication != null && mProfiler.profileFd != null
1554 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001555 stopProfiling = true;
1556 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 if (a != null) {
1558 mNewActivities = null;
1559 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001560 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001562 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 TAG, "Reporting idle of " + a +
1564 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001565 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 if (a.activity != null && !a.activity.mFinished) {
1567 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001568 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001569 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001571 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573 }
1574 prev = a;
1575 a = a.nextIdle;
1576 prev.nextIdle = null;
1577 } while (a != null);
1578 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001579 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001580 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001581 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001582 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 return false;
1584 }
1585 }
1586
1587 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001588 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 public final boolean queueIdle() {
1590 doGcIfNeeded();
1591 return false;
1592 }
1593 }
1594
Romain Guy65b345f2011-07-27 18:51:50 -07001595 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001596 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
Romain Guy65b345f2011-07-27 18:51:50 -07001599 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001600 ActivityThread am = currentActivityThread();
1601 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001602 ? am.mBoundApplication.appInfo.packageName : null;
1603 }
1604
1605 public static String currentProcessName() {
1606 ActivityThread am = currentActivityThread();
1607 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001608 ? am.mBoundApplication.processName : null;
1609 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610
Romain Guy65b345f2011-07-27 18:51:50 -07001611 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001612 ActivityThread am = currentActivityThread();
1613 return am != null ? am.mInitialApplication : null;
1614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001616 public static IPackageManager getPackageManager() {
1617 if (sPackageManager != null) {
1618 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1619 return sPackageManager;
1620 }
1621 IBinder b = ServiceManager.getService("package");
1622 //Slog.v("PackageManager", "default service binder = " + b);
1623 sPackageManager = IPackageManager.Stub.asInterface(b);
1624 //Slog.v("PackageManager", "default service = " + sPackageManager);
1625 return sPackageManager;
1626 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627
Romain Guy65b345f2011-07-27 18:51:50 -07001628 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001629 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1630 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001631 if (config == null) {
1632 return null;
1633 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001634 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001635 mMainThreadConfig.setTo(config);
1636 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001637 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001638 }
1639 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001640 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001642 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001643 * Creates the top level resources for the given package.
1644 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001645 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1646 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001647 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001648 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
1649 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001650 }
1651
1652 final Handler getHandler() {
1653 return mH;
1654 }
1655
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001656 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1657 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001658 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1659 }
1660
1661 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1662 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001663 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001664 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1666 ref = mPackages.get(packageName);
1667 } else {
1668 ref = mResourcePackages.get(packageName);
1669 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001670 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001671 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001672 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1673 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 if (packageInfo != null && (packageInfo.mResources == null
1675 || packageInfo.mResources.getAssets().isUpToDate())) {
1676 if (packageInfo.isSecurityViolation()
1677 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1678 throw new SecurityException(
1679 "Requesting code from " + packageName
1680 + " to be run in process "
1681 + mBoundApplication.processName
1682 + "/" + mBoundApplication.appInfo.uid);
1683 }
1684 return packageInfo;
1685 }
1686 }
1687
1688 ApplicationInfo ai = null;
1689 try {
1690 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001691 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001693 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001694 }
1695
1696 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001697 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001698 }
1699
1700 return null;
1701 }
1702
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001703 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1704 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1706 boolean securityViolation = includeCode && ai.uid != 0
1707 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001708 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001709 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001710 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1712 |Context.CONTEXT_IGNORE_SECURITY))
1713 == Context.CONTEXT_INCLUDE_CODE) {
1714 if (securityViolation) {
1715 String msg = "Requesting code from " + ai.packageName
1716 + " (with uid " + ai.uid + ")";
1717 if (mBoundApplication != null) {
1718 msg = msg + " to be run in process "
1719 + mBoundApplication.processName + " (with uid "
1720 + mBoundApplication.appInfo.uid + ")";
1721 }
1722 throw new SecurityException(msg);
1723 }
1724 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001725 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1726 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 }
1728
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001729 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1730 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001731 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 }
1733
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001734 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001735 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001736 WeakReference<LoadedApk> ref;
1737 if (includeCode) {
1738 ref = mPackages.get(packageName);
1739 } else {
1740 ref = mResourcePackages.get(packageName);
1741 }
1742 return ref != null ? ref.get() : null;
1743 }
1744 }
1745
Romain Guy65b345f2011-07-27 18:51:50 -07001746 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001747 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1748 boolean registerPackage) {
Craig Mautner88c05892013-06-28 09:47:45 -07001749 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001750 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 if (includeCode) {
1752 ref = mPackages.get(aInfo.packageName);
1753 } else {
1754 ref = mResourcePackages.get(aInfo.packageName);
1755 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001756 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 if (packageInfo == null || (packageInfo.mResources != null
1758 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001759 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 : "Loading resource-only package ") + aInfo.packageName
1761 + " (in " + (mBoundApplication != null
1762 ? mBoundApplication.processName : null)
1763 + ")");
1764 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001765 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001767 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 if (includeCode) {
1769 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001770 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 } else {
1772 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001773 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 }
1775 }
1776 return packageInfo;
1777 }
1778 }
1779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001781 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001782 }
1783
1784 public ApplicationThread getApplicationThread()
1785 {
1786 return mAppThread;
1787 }
1788
1789 public Instrumentation getInstrumentation()
1790 {
1791 return mInstrumentation;
1792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001795 return mProfiler != null && mProfiler.profileFile != null
1796 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798
1799 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001800 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802
1803 public Looper getLooper() {
1804 return mLooper;
1805 }
1806
1807 public Application getApplication() {
1808 return mInitialApplication;
1809 }
Bob Leee5408332009-09-04 18:31:17 -07001810
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001811 public String getProcessName() {
1812 return mBoundApplication.processName;
1813 }
Bob Leee5408332009-09-04 18:31:17 -07001814
Dianne Hackborn21556372010-02-04 16:34:40 -08001815 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 synchronized (this) {
1817 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001818 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001820 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 }
1823
Narayan Kamath29564cd2014-08-07 10:57:40 +01001824 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001825 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001826 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001827
Narayan Kamath3480ab42014-08-15 18:11:04 +01001828 // The code package for "android" in the system server needs
1829 // to be the system context's package.
1830 mPackages.put("android", new WeakReference<LoadedApk>(getSystemContext().mPackageInfo));
1831
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001832 // give ourselves a default profiler
1833 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001834 }
1835 }
1836
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001837 void ensureJitEnabled() {
1838 if (!mJitEnabled) {
1839 mJitEnabled = true;
1840 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1841 }
1842 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 void scheduleGcIdler() {
1845 if (!mGcIdlerScheduled) {
1846 mGcIdlerScheduled = true;
1847 Looper.myQueue().addIdleHandler(mGcIdler);
1848 }
1849 mH.removeMessages(H.GC_WHEN_IDLE);
1850 }
1851
1852 void unscheduleGcIdler() {
1853 if (mGcIdlerScheduled) {
1854 mGcIdlerScheduled = false;
1855 Looper.myQueue().removeIdleHandler(mGcIdler);
1856 }
1857 mH.removeMessages(H.GC_WHEN_IDLE);
1858 }
1859
1860 void doGcIfNeeded() {
1861 mGcIdlerScheduled = false;
1862 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001863 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 // + "m now=" + now);
1865 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001866 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 BinderInternal.forceGc("bg");
1868 }
1869 }
1870
Dianne Hackborne77187d2013-10-25 16:32:41 -07001871 private static final String HEAP_FULL_COLUMN
1872 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1873 private static final String HEAP_COLUMN
1874 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1875
1876 // Formatting for checkin service - update version if row format changes
1877 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1878
1879 static void printRow(PrintWriter pw, String format, Object...objs) {
1880 pw.println(String.format(format, objs));
1881 }
1882
1883 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1884 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1885 long nativeMax, long nativeAllocated, long nativeFree,
1886 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1887
1888 // For checkin, we print one long comma-separated list of values
1889 if (checkin) {
1890 // NOTE: if you change anything significant below, also consider changing
1891 // ACTIVITY_THREAD_CHECKIN_VERSION.
1892
1893 // Header
1894 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1895 pw.print(pid); pw.print(',');
1896 pw.print(processName); pw.print(',');
1897
1898 // Heap info - max
1899 pw.print(nativeMax); pw.print(',');
1900 pw.print(dalvikMax); pw.print(',');
1901 pw.print("N/A,");
1902 pw.print(nativeMax + dalvikMax); pw.print(',');
1903
1904 // Heap info - allocated
1905 pw.print(nativeAllocated); pw.print(',');
1906 pw.print(dalvikAllocated); pw.print(',');
1907 pw.print("N/A,");
1908 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1909
1910 // Heap info - free
1911 pw.print(nativeFree); pw.print(',');
1912 pw.print(dalvikFree); pw.print(',');
1913 pw.print("N/A,");
1914 pw.print(nativeFree + dalvikFree); pw.print(',');
1915
1916 // Heap info - proportional set size
1917 pw.print(memInfo.nativePss); pw.print(',');
1918 pw.print(memInfo.dalvikPss); pw.print(',');
1919 pw.print(memInfo.otherPss); pw.print(',');
1920 pw.print(memInfo.getTotalPss()); pw.print(',');
1921
1922 // Heap info - swappable set size
1923 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1924 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1925 pw.print(memInfo.otherSwappablePss); pw.print(',');
1926 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1927
1928 // Heap info - shared dirty
1929 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1930 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1931 pw.print(memInfo.otherSharedDirty); pw.print(',');
1932 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1933
1934 // Heap info - shared clean
1935 pw.print(memInfo.nativeSharedClean); pw.print(',');
1936 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1937 pw.print(memInfo.otherSharedClean); pw.print(',');
1938 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1939
1940 // Heap info - private Dirty
1941 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1942 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1943 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1944 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1945
1946 // Heap info - private Clean
1947 pw.print(memInfo.nativePrivateClean); pw.print(',');
1948 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1949 pw.print(memInfo.otherPrivateClean); pw.print(',');
1950 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1951
1952 // Heap info - other areas
1953 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1954 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1955 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1956 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1957 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1958 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1959 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1960 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1961 }
1962 return;
1963 }
1964
1965 // otherwise, show human-readable format
1966 if (dumpFullInfo) {
1967 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1968 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
1969 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
1970 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1971 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1972 "------", "------", "------", "------", "------", "------");
1973 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1974 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1975 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1976 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1977 nativeMax, nativeAllocated, nativeFree);
1978 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1979 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1980 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1981 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1982 dalvikMax, dalvikAllocated, dalvikFree);
1983 } else {
1984 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1985 "Private", "Swapped", "Heap", "Heap", "Heap");
1986 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1987 "Clean", "Dirty", "Size", "Alloc", "Free");
1988 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1989 "------", "------", "------", "------", "------");
1990 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1991 memInfo.nativePrivateDirty,
1992 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1993 nativeMax, nativeAllocated, nativeFree);
1994 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1995 memInfo.dalvikPrivateDirty,
1996 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1997 dalvikMax, dalvikAllocated, dalvikFree);
1998 }
1999
2000 int otherPss = memInfo.otherPss;
2001 int otherSwappablePss = memInfo.otherSwappablePss;
2002 int otherSharedDirty = memInfo.otherSharedDirty;
2003 int otherPrivateDirty = memInfo.otherPrivateDirty;
2004 int otherSharedClean = memInfo.otherSharedClean;
2005 int otherPrivateClean = memInfo.otherPrivateClean;
2006 int otherSwappedOut = memInfo.otherSwappedOut;
2007
2008 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2009 final int myPss = memInfo.getOtherPss(i);
2010 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2011 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2012 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2013 final int mySharedClean = memInfo.getOtherSharedClean(i);
2014 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2015 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2016 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2017 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
2018 if (dumpFullInfo) {
2019 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2020 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2021 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2022 } else {
2023 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2024 myPss, myPrivateDirty,
2025 myPrivateClean, mySwappedOut, "", "", "");
2026 }
2027 otherPss -= myPss;
2028 otherSwappablePss -= mySwappablePss;
2029 otherSharedDirty -= mySharedDirty;
2030 otherPrivateDirty -= myPrivateDirty;
2031 otherSharedClean -= mySharedClean;
2032 otherPrivateClean -= myPrivateClean;
2033 otherSwappedOut -= mySwappedOut;
2034 }
2035 }
2036
2037 if (dumpFullInfo) {
2038 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2039 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
2040 otherSwappedOut, "", "", "");
2041 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2042 memInfo.getTotalSwappablePss(),
2043 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2044 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
2045 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
2046 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2047 } else {
2048 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
2049 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
2050 "", "", "");
2051 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2052 memInfo.getTotalPrivateDirty(),
2053 memInfo.getTotalPrivateClean(),
2054 memInfo.getTotalSwappedOut(),
2055 nativeMax+dalvikMax,
2056 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2057 }
2058
2059 if (dumpDalvik) {
2060 pw.println(" ");
2061 pw.println(" Dalvik Details");
2062
2063 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2064 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2065 final int myPss = memInfo.getOtherPss(i);
2066 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2067 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2068 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2069 final int mySharedClean = memInfo.getOtherSharedClean(i);
2070 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2071 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2072 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2073 || mySharedClean != 0 || myPrivateClean != 0) {
2074 if (dumpFullInfo) {
2075 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2076 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2077 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2078 } else {
2079 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2080 myPss, myPrivateDirty,
2081 myPrivateClean, mySwappedOut, "", "", "");
2082 }
2083 }
2084 }
2085 }
2086 }
2087
Jeff Hamilton52d32032011-01-08 15:31:26 -06002088 public void registerOnActivityPausedListener(Activity activity,
2089 OnActivityPausedListener listener) {
2090 synchronized (mOnPauseListeners) {
2091 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2092 if (list == null) {
2093 list = new ArrayList<OnActivityPausedListener>();
2094 mOnPauseListeners.put(activity, list);
2095 }
2096 list.add(listener);
2097 }
2098 }
2099
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002100 public void unregisterOnActivityPausedListener(Activity activity,
2101 OnActivityPausedListener listener) {
2102 synchronized (mOnPauseListeners) {
2103 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2104 if (list != null) {
2105 list.remove(listener);
2106 }
2107 }
2108 }
2109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 public final ActivityInfo resolveActivityInfo(Intent intent) {
2111 ActivityInfo aInfo = intent.resolveActivityInfo(
2112 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2113 if (aInfo == null) {
2114 // Throw an exception.
2115 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002116 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 }
2118 return aInfo;
2119 }
Bob Leee5408332009-09-04 18:31:17 -07002120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002121 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002123 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002124 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002126 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 r.intent = intent;
2128 r.state = state;
2129 r.parent = parent;
2130 r.embeddedID = id;
2131 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002132 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 if (localLOGV) {
2134 ComponentName compname = intent.getComponent();
2135 String name;
2136 if (compname != null) {
2137 name = compname.toShortString();
2138 } else {
2139 name = "(Intent " + intent + ").getComponent() returned null";
2140 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002141 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 + ", comp=" + name
2143 + ", token=" + token);
2144 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002145 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 }
2147
2148 public final Activity getActivity(IBinder token) {
2149 return mActivities.get(token).activity;
2150 }
2151
2152 public final void sendActivityResult(
2153 IBinder token, String id, int requestCode,
2154 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002155 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002156 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2158 list.add(new ResultInfo(id, requestCode, resultCode, data));
2159 mAppThread.scheduleSendResult(token, list);
2160 }
2161
Jeff Brown9ef09972013-10-15 20:49:59 -07002162 private void sendMessage(int what, Object obj) {
2163 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164 }
2165
Jeff Brown9ef09972013-10-15 20:49:59 -07002166 private void sendMessage(int what, Object obj, int arg1) {
2167 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
2169
Jeff Brown9ef09972013-10-15 20:49:59 -07002170 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2171 sendMessage(what, obj, arg1, arg2, false);
2172 }
2173
2174 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2175 if (DEBUG_MESSAGES) Slog.v(
2176 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2177 + ": " + arg1 + " / " + obj);
2178 Message msg = Message.obtain();
2179 msg.what = what;
2180 msg.obj = obj;
2181 msg.arg1 = arg1;
2182 msg.arg2 = arg2;
2183 if (async) {
2184 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002186 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188
Dianne Hackborn21556372010-02-04 16:34:40 -08002189 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 String what) {
2191 ContextCleanupInfo cci = new ContextCleanupInfo();
2192 cci.context = context;
2193 cci.who = who;
2194 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002195 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
2197
Craig Mautner233ceee2014-05-09 17:05:11 -07002198 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2200
2201 ActivityInfo aInfo = r.activityInfo;
2202 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002203 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 Context.CONTEXT_INCLUDE_CODE);
2205 }
Bob Leee5408332009-09-04 18:31:17 -07002206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 ComponentName component = r.intent.getComponent();
2208 if (component == null) {
2209 component = r.intent.resolveActivity(
2210 mInitialApplication.getPackageManager());
2211 r.intent.setComponent(component);
2212 }
2213
2214 if (r.activityInfo.targetActivity != null) {
2215 component = new ComponentName(r.activityInfo.packageName,
2216 r.activityInfo.targetActivity);
2217 }
2218
2219 Activity activity = null;
2220 try {
2221 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2222 activity = mInstrumentation.newActivity(
2223 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002224 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002226 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002227 if (r.state != null) {
2228 r.state.setClassLoader(cl);
2229 }
2230 } catch (Exception e) {
2231 if (!mInstrumentation.onException(activity, e)) {
2232 throw new RuntimeException(
2233 "Unable to instantiate activity " + component
2234 + ": " + e.toString(), e);
2235 }
2236 }
2237
2238 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002239 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002240
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002241 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2242 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 TAG, r + ": app=" + app
2244 + ", appName=" + app.getPackageName()
2245 + ", pkg=" + r.packageInfo.getPackageName()
2246 + ", comp=" + r.intent.getComponent().toShortString()
2247 + ", dir=" + r.packageInfo.getAppDir());
2248
2249 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002250 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002251 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002252 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002253 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002254 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002255 activity.attach(appContext, this, getInstrumentation(), r.token,
2256 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002257 r.embeddedID, r.lastNonConfigurationInstances, config,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002258 r.voiceInteractor);
Bob Leee5408332009-09-04 18:31:17 -07002259
Christopher Tateb70f3df2009-04-07 16:07:59 -07002260 if (customIntent != null) {
2261 activity.mIntent = customIntent;
2262 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002263 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002264 activity.mStartedActivity = false;
2265 int theme = r.activityInfo.getThemeResource();
2266 if (theme != 0) {
2267 activity.setTheme(theme);
2268 }
2269
2270 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002271 if (r.isPersistable()) {
2272 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2273 } else {
2274 mInstrumentation.callActivityOnCreate(activity, r.state);
2275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002276 if (!activity.mCalled) {
2277 throw new SuperNotCalledException(
2278 "Activity " + r.intent.getComponent().toShortString() +
2279 " did not call through to super.onCreate()");
2280 }
2281 r.activity = activity;
2282 r.stopped = true;
2283 if (!r.activity.mFinished) {
2284 activity.performStart();
2285 r.stopped = false;
2286 }
2287 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002288 if (r.isPersistable()) {
2289 if (r.state != null || r.persistentState != null) {
2290 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2291 r.persistentState);
2292 }
2293 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2295 }
2296 }
2297 if (!r.activity.mFinished) {
2298 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002299 if (r.isPersistable()) {
2300 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2301 r.persistentState);
2302 } else {
2303 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 if (!activity.mCalled) {
2306 throw new SuperNotCalledException(
2307 "Activity " + r.intent.getComponent().toShortString() +
2308 " did not call through to super.onPostCreate()");
2309 }
2310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 }
2312 r.paused = true;
2313
2314 mActivities.put(r.token, r);
2315
2316 } catch (SuperNotCalledException e) {
2317 throw e;
2318
2319 } catch (Exception e) {
2320 if (!mInstrumentation.onException(activity, e)) {
2321 throw new RuntimeException(
2322 "Unable to start activity " + component
2323 + ": " + e.toString(), e);
2324 }
2325 }
2326
2327 return activity;
2328 }
2329
Jeff Brownefd43bd2012-09-21 17:02:35 -07002330 private Context createBaseContextForActivity(ActivityClientRecord r,
2331 final Activity activity) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002332 ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002333 appContext.setOuterContext(activity);
Craig Mautnere0a38842013-12-16 16:14:02 -08002334 Context baseContext = appContext;
2335
2336 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2337 try {
2338 IActivityContainer container =
2339 ActivityManagerNative.getDefault().getEnclosingActivityContainer(r.token);
Craig Mautnerbd503a42014-01-10 10:16:43 -08002340 final int displayId =
2341 container == null ? Display.DEFAULT_DISPLAY : container.getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -08002342 if (displayId > Display.DEFAULT_DISPLAY) {
2343 Display display = dm.getRealDisplay(displayId, r.token);
2344 baseContext = appContext.createDisplayContext(display);
2345 }
2346 } catch (RemoteException e) {
2347 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002348
2349 // For debugging purposes, if the activity's package name contains the value of
2350 // the "debug.use-second-display" system property as a substring, then show
2351 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002352 String pkgName = SystemProperties.get("debug.second-display.pkg");
2353 if (pkgName != null && !pkgName.isEmpty()
2354 && r.packageInfo.mPackageName.contains(pkgName)) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002355 for (int displayId : dm.getDisplayIds()) {
2356 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002357 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002358 baseContext = appContext.createDisplayContext(display);
2359 break;
2360 }
2361 }
2362 }
2363 return baseContext;
2364 }
2365
Romain Guy65b345f2011-07-27 18:51:50 -07002366 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 // If we are getting ready to gc after going to the background, well
2368 // we are back active so skip it.
2369 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002370 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371
Jeff Hao1b012d32014-08-20 10:35:34 -07002372 if (r.profilerInfo != null) {
2373 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002374 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002375 }
2376
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002377 // Make sure we are running with the most recent config.
2378 handleConfigurationChanged(null, null);
2379
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002380 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002382
Craig Mautner233ceee2014-05-09 17:05:11 -07002383 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384
2385 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002386 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002387 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002388 handleResumeActivity(r.token, false, r.isForward,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002389 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002390
2391 if (!r.activity.mFinished && r.startsNotResumed) {
2392 // The activity manager actually wants this one to start out
2393 // paused, because it needs to be visible but isn't in the
2394 // foreground. We accomplish this by going through the
2395 // normal startup (because activities expect to go through
2396 // onResume() the first time they run, before their window
2397 // is displayed), and then pausing it. However, in this case
2398 // we do -not- need to do the full pause cycle (of freezing
2399 // and such) because the activity manager assumes it can just
2400 // retain the current state it has.
2401 try {
2402 r.activity.mCalled = false;
2403 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002404 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002405 // we need to be created again. But we only do this
2406 // for pre-Honeycomb apps, which always save their state
2407 // when pausing, so we can not have them save their state
2408 // when restarting from a paused state. For HC and later,
2409 // we want to (and can) let the state be saved as the normal
2410 // part of stopping the activity.
2411 if (r.isPreHoneycomb()) {
2412 r.state = oldState;
2413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414 if (!r.activity.mCalled) {
2415 throw new SuperNotCalledException(
2416 "Activity " + r.intent.getComponent().toShortString() +
2417 " did not call through to super.onPause()");
2418 }
2419
2420 } catch (SuperNotCalledException e) {
2421 throw e;
2422
2423 } catch (Exception e) {
2424 if (!mInstrumentation.onException(r.activity, e)) {
2425 throw new RuntimeException(
2426 "Unable to pause activity "
2427 + r.intent.getComponent().toShortString()
2428 + ": " + e.toString(), e);
2429 }
2430 }
2431 r.paused = true;
2432 }
2433 } else {
2434 // If there was an error, for any reason, tell the activity
2435 // manager to stop us.
2436 try {
2437 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07002438 .finishActivity(r.token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002440 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002441 }
2442 }
2443 }
2444
Romain Guy65b345f2011-07-27 18:51:50 -07002445 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002446 List<Intent> intents) {
2447 final int N = intents.size();
2448 for (int i=0; i<N; i++) {
2449 Intent intent = intents.get(i);
2450 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002451 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002452 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002453 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2454 }
2455 }
2456
2457 public final void performNewIntents(IBinder token,
2458 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002459 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002460 if (r != null) {
2461 final boolean resumed = !r.paused;
2462 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002463 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 mInstrumentation.callActivityOnPause(r.activity);
2465 }
2466 deliverNewIntents(r, intents);
2467 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002468 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002469 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002470 }
2471 }
2472 }
Bob Leee5408332009-09-04 18:31:17 -07002473
Romain Guy65b345f2011-07-27 18:51:50 -07002474 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002475 performNewIntents(data.token, data.intents);
2476 }
2477
Adam Skorydfc7fd72013-08-05 19:23:41 -07002478 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002479 Bundle data = new Bundle();
2480 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2481 if (r != null) {
2482 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2483 r.activity.onProvideAssistData(data);
2484 }
2485 if (data.isEmpty()) {
2486 data = null;
2487 }
2488 IActivityManager mgr = ActivityManagerNative.getDefault();
2489 try {
Adam Skory7140a252013-09-11 12:04:58 +01002490 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002491 } catch (RemoteException e) {
2492 }
2493 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002494
2495 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2496 ActivityClientRecord r = mActivities.get(token);
2497 if (r != null) {
2498 r.activity.onTranslucentConversionComplete(drawComplete);
2499 }
2500 }
2501
Craig Mautnereb8abf72014-07-02 15:04:09 -07002502 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2503 ActivityClientRecord r = mActivities.get(token);
2504 if (r != null) {
2505 r.activity.onNewActivityOptions(options);
2506 }
2507 }
2508
Jose Lima4b6c6692014-08-12 17:41:12 -07002509 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002510 ActivityClientRecord r = mActivities.get(token);
2511 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002512 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002513 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002514 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002515 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002516 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002517 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2518 if (!activity.mCalled) {
2519 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002520 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002521 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002522 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002523 }
2524 }
2525 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002526 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002527 } catch (RemoteException e) {
2528 }
2529 }
2530
Jose Lima4b6c6692014-08-12 17:41:12 -07002531 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002532 ActivityClientRecord r = mActivities.get(token);
2533 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002534 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002535 }
2536 }
2537
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002538 public void handleInstallProvider(ProviderInfo info) {
2539 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2540 }
2541
Craig Mautner8746a472014-07-24 15:12:54 -07002542 private void handleEnterAnimationComplete(IBinder token) {
2543 ActivityClientRecord r = mActivities.get(token);
2544 if (r != null) {
2545 r.activity.onEnterAnimationComplete();
2546 }
2547 }
2548
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002549 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2550
2551 /**
2552 * Return the Intent that's currently being handled by a
2553 * BroadcastReceiver on this thread, or null if none.
2554 * @hide
2555 */
2556 public static Intent getIntentBeingBroadcast() {
2557 return sCurrentBroadcastIntent.get();
2558 }
2559
Romain Guy65b345f2011-07-27 18:51:50 -07002560 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002561 // If we are getting ready to gc after going to the background, well
2562 // we are back active so skip it.
2563 unscheduleGcIdler();
2564
2565 String component = data.intent.getComponent().getClassName();
2566
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002567 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002568 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569
2570 IActivityManager mgr = ActivityManagerNative.getDefault();
2571
Romain Guy65b345f2011-07-27 18:51:50 -07002572 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 try {
2574 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2575 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002576 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002577 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2579 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002580 if (DEBUG_BROADCAST) Slog.i(TAG,
2581 "Finishing failed broadcast to " + data.intent.getComponent());
2582 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 throw new RuntimeException(
2584 "Unable to instantiate receiver " + component
2585 + ": " + e.toString(), e);
2586 }
2587
2588 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002589 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002590
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002591 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 TAG, "Performing receive of " + data.intent
2593 + ": app=" + app
2594 + ", appName=" + app.getPackageName()
2595 + ", pkg=" + packageInfo.getPackageName()
2596 + ", comp=" + data.intent.getComponent().toShortString()
2597 + ", dir=" + packageInfo.getAppDir());
2598
Dianne Hackborn21556372010-02-04 16:34:40 -08002599 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002600 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002601 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 receiver.onReceive(context.getReceiverRestrictedContext(),
2603 data.intent);
2604 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002605 if (DEBUG_BROADCAST) Slog.i(TAG,
2606 "Finishing failed broadcast to " + data.intent.getComponent());
2607 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608 if (!mInstrumentation.onException(receiver, e)) {
2609 throw new RuntimeException(
2610 "Unable to start receiver " + component
2611 + ": " + e.toString(), e);
2612 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002613 } finally {
2614 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 }
2616
Dianne Hackborne829fef2010-10-26 17:44:01 -07002617 if (receiver.getPendingResult() != null) {
2618 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 }
2620 }
2621
Christopher Tate181fafa2009-05-14 11:12:14 -07002622 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002623 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002624 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002625
Christopher Tate346acb12012-10-15 19:20:25 -07002626 // Sanity check the requested target package's uid against ours
2627 try {
2628 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2629 data.appInfo.packageName, 0, UserHandle.myUserId());
2630 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2631 Slog.w(TAG, "Asked to instantiate non-matching package "
2632 + data.appInfo.packageName);
2633 return;
2634 }
2635 } catch (RemoteException e) {
2636 Slog.e(TAG, "Can't reach package manager", e);
2637 return;
2638 }
2639
Christopher Tate181fafa2009-05-14 11:12:14 -07002640 // no longer idle; we have backup work to do
2641 unscheduleGcIdler();
2642
2643 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002644 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002645 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002646 if (packageName == null) {
2647 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2648 return;
2649 }
2650
Christopher Tate181fafa2009-05-14 11:12:14 -07002651 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07002652 // full backup operation but no app-supplied agent? use the default implementation
2653 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2654 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002655 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002656 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002657
Christopher Tate181fafa2009-05-14 11:12:14 -07002658 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002659 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07002660 BackupAgent agent = mBackupAgents.get(packageName);
2661 if (agent != null) {
2662 // reusing the existing instance
2663 if (DEBUG_BACKUP) {
2664 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07002665 }
Christopher Tate2e40d112014-07-15 12:37:38 -07002666 binder = agent.onBind();
2667 } else {
2668 try {
2669 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2670
2671 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2672 agent = (BackupAgent) cl.loadClass(classname).newInstance();
2673
2674 // set up the agent's context
2675 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2676 context.setOuterContext(agent);
2677 agent.attach(context);
2678
2679 agent.onCreate();
2680 binder = agent.onBind();
2681 mBackupAgents.put(packageName, agent);
2682 } catch (Exception e) {
2683 // If this is during restore, fail silently; otherwise go
2684 // ahead and let the user see the crash.
2685 Slog.e(TAG, "Agent threw during creation: " + e);
2686 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2687 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
2688 throw e;
2689 }
2690 // falling through with 'binder' still null
2691 }
Christopher Tated1475e02009-07-09 15:36:17 -07002692 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002693
2694 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002695 try {
2696 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2697 } catch (RemoteException e) {
2698 // nothing to do.
2699 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002700 } catch (Exception e) {
2701 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002702 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002703 }
2704 }
2705
2706 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002707 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002708 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002709
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002710 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002711 String packageName = packageInfo.mPackageName;
2712 BackupAgent agent = mBackupAgents.get(packageName);
2713 if (agent != null) {
2714 try {
2715 agent.onDestroy();
2716 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002717 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002718 e.printStackTrace();
2719 }
2720 mBackupAgents.remove(packageName);
2721 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002722 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002723 }
2724 }
2725
Romain Guy65b345f2011-07-27 18:51:50 -07002726 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 // If we are getting ready to gc after going to the background, well
2728 // we are back active so skip it.
2729 unscheduleGcIdler();
2730
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002731 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002732 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 Service service = null;
2734 try {
2735 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2736 service = (Service) cl.loadClass(data.info.name).newInstance();
2737 } catch (Exception e) {
2738 if (!mInstrumentation.onException(service, e)) {
2739 throw new RuntimeException(
2740 "Unable to instantiate service " + data.info.name
2741 + ": " + e.toString(), e);
2742 }
2743 }
2744
2745 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002746 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747
Jeff Browndefd4a62014-03-10 21:24:37 -07002748 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2749 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002751 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752 service.attach(context, this, data.info.name, data.token, app,
2753 ActivityManagerNative.getDefault());
2754 service.onCreate();
2755 mServices.put(data.token, service);
2756 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002757 ActivityManagerNative.getDefault().serviceDoneExecuting(
2758 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 } catch (RemoteException e) {
2760 // nothing to do.
2761 }
2762 } catch (Exception e) {
2763 if (!mInstrumentation.onException(service, e)) {
2764 throw new RuntimeException(
2765 "Unable to create service " + data.info.name
2766 + ": " + e.toString(), e);
2767 }
2768 }
2769 }
2770
Romain Guy65b345f2011-07-27 18:51:50 -07002771 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002773 if (DEBUG_SERVICE)
2774 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775 if (s != null) {
2776 try {
2777 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002778 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 try {
2780 if (!data.rebind) {
2781 IBinder binder = s.onBind(data.intent);
2782 ActivityManagerNative.getDefault().publishService(
2783 data.token, data.intent, binder);
2784 } else {
2785 s.onRebind(data.intent);
2786 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002787 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002789 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 } catch (RemoteException ex) {
2791 }
2792 } catch (Exception e) {
2793 if (!mInstrumentation.onException(s, e)) {
2794 throw new RuntimeException(
2795 "Unable to bind to service " + s
2796 + " with " + data.intent + ": " + e.toString(), e);
2797 }
2798 }
2799 }
2800 }
2801
Romain Guy65b345f2011-07-27 18:51:50 -07002802 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002803 Service s = mServices.get(data.token);
2804 if (s != null) {
2805 try {
2806 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002807 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002808 boolean doRebind = s.onUnbind(data.intent);
2809 try {
2810 if (doRebind) {
2811 ActivityManagerNative.getDefault().unbindFinished(
2812 data.token, data.intent, doRebind);
2813 } else {
2814 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002815 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816 }
2817 } catch (RemoteException ex) {
2818 }
2819 } catch (Exception e) {
2820 if (!mInstrumentation.onException(s, e)) {
2821 throw new RuntimeException(
2822 "Unable to unbind to service " + s
2823 + " with " + data.intent + ": " + e.toString(), e);
2824 }
2825 }
2826 }
2827 }
2828
Dianne Hackborn625ac272010-09-17 18:29:22 -07002829 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002830 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2831 try {
2832 Service s = mServices.get(info.token);
2833 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002834 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2835 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002836 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2837 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002839 } finally {
2840 IoUtils.closeQuietly(info.fd);
2841 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
2843 }
2844
Dianne Hackborn625ac272010-09-17 18:29:22 -07002845 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002846 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2847 try {
2848 ActivityClientRecord r = mActivities.get(info.token);
2849 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002850 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2851 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002852 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2853 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002854 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002855 } finally {
2856 IoUtils.closeQuietly(info.fd);
2857 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002858 }
2859 }
2860
Marco Nelissen18cb2872011-11-15 11:19:53 -08002861 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002862 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2863 try {
2864 ProviderClientRecord r = mLocalProviders.get(info.token);
2865 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002866 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2867 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002868 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2869 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002870 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002871 } finally {
2872 IoUtils.closeQuietly(info.fd);
2873 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002874 }
2875 }
2876
Romain Guy65b345f2011-07-27 18:51:50 -07002877 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 Service s = mServices.get(data.token);
2879 if (s != null) {
2880 try {
2881 if (data.args != null) {
2882 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002883 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002885 int res;
2886 if (!data.taskRemoved) {
2887 res = s.onStartCommand(data.args, data.flags, data.startId);
2888 } else {
2889 s.onTaskRemoved(data.args);
2890 res = Service.START_TASK_REMOVED_COMPLETE;
2891 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002892
2893 QueuedWork.waitToFinish();
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002896 ActivityManagerNative.getDefault().serviceDoneExecuting(
2897 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 } catch (RemoteException e) {
2899 // nothing to do.
2900 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002901 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002902 } catch (Exception e) {
2903 if (!mInstrumentation.onException(s, e)) {
2904 throw new RuntimeException(
2905 "Unable to start service " + s
2906 + " with " + data.args + ": " + e.toString(), e);
2907 }
2908 }
2909 }
2910 }
2911
Romain Guy65b345f2011-07-27 18:51:50 -07002912 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002913 Service s = mServices.remove(token);
2914 if (s != null) {
2915 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002916 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002917 s.onDestroy();
2918 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002919 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002921 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002922 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002923
2924 QueuedWork.waitToFinish();
2925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002926 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002927 ActivityManagerNative.getDefault().serviceDoneExecuting(
2928 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002929 } catch (RemoteException e) {
2930 // nothing to do.
2931 }
2932 } catch (Exception e) {
2933 if (!mInstrumentation.onException(s, e)) {
2934 throw new RuntimeException(
2935 "Unable to stop service " + s
2936 + ": " + e.toString(), e);
2937 }
2938 }
2939 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002940 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
2942
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002943 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002945 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002946 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 + " finished=" + r.activity.mFinished);
2948 if (r != null && !r.activity.mFinished) {
2949 if (clearHide) {
2950 r.hideForNow = false;
2951 r.activity.mStartedActivity = false;
2952 }
2953 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002954 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 if (r.pendingIntents != null) {
2956 deliverNewIntents(r, r.pendingIntents);
2957 r.pendingIntents = null;
2958 }
2959 if (r.pendingResults != null) {
2960 deliverResults(r, r.pendingResults);
2961 r.pendingResults = null;
2962 }
2963 r.activity.performResume();
2964
Bob Leee5408332009-09-04 18:31:17 -07002965 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002966 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 r.paused = false;
2969 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07002971 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 } catch (Exception e) {
2973 if (!mInstrumentation.onException(r.activity, e)) {
2974 throw new RuntimeException(
2975 "Unable to resume activity "
2976 + r.intent.getComponent().toShortString()
2977 + ": " + e.toString(), e);
2978 }
2979 }
2980 }
2981 return r;
2982 }
2983
Romain Guya998dff2012-03-23 18:58:36 -07002984 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002985 if (r.mPendingRemoveWindow != null) {
2986 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2987 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2988 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002989 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002990 r.activity.getClass().getName(), "Activity");
2991 }
2992 }
2993 r.mPendingRemoveWindow = null;
2994 r.mPendingRemoveWindowManager = null;
2995 }
2996
Craig Mautner233ceee2014-05-09 17:05:11 -07002997 final void handleResumeActivity(IBinder token,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002998 boolean clearHide, boolean isForward, boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 // If we are getting ready to gc after going to the background, well
3000 // we are back active so skip it.
3001 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003002 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003003
Adam Powellcfbe9be2013-11-06 14:58:58 -08003004 // TODO Push resumeArgs into the activity for consideration
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003005 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003006
3007 if (r != null) {
3008 final Activity a = r.activity;
3009
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003010 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 TAG, "Resume " + r + " started activity: " +
3012 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3013 + ", finished: " + a.mFinished);
3014
3015 final int forwardBit = isForward ?
3016 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 // If the window hasn't yet been added to the window manager,
3019 // and this guy didn't finish itself or start another activity,
3020 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003021 boolean willBeVisible = !a.mStartedActivity;
3022 if (!willBeVisible) {
3023 try {
3024 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3025 a.getActivityToken());
3026 } catch (RemoteException e) {
3027 }
3028 }
3029 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 r.window = r.activity.getWindow();
3031 View decor = r.window.getDecorView();
3032 decor.setVisibility(View.INVISIBLE);
3033 ViewManager wm = a.getWindowManager();
3034 WindowManager.LayoutParams l = r.window.getAttributes();
3035 a.mDecor = decor;
3036 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3037 l.softInputMode |= forwardBit;
3038 if (a.mVisibleFromClient) {
3039 a.mWindowAdded = true;
3040 wm.addView(decor, l);
3041 }
3042
3043 // If the window has already been added, but during resume
3044 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003045 // window visible.
3046 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003047 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048 TAG, "Launch " + r + " mStartedActivity set");
3049 r.hideForNow = true;
3050 }
3051
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003052 // Get rid of anything left hanging around.
3053 cleanUpPendingRemoveWindows(r);
3054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003055 // The window is now visible if it has been added, we are not
3056 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003057 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003058 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003059 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003060 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003061 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003063 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003064 r.newConfig = null;
3065 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003066 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 + isForward);
3068 WindowManager.LayoutParams l = r.window.getAttributes();
3069 if ((l.softInputMode
3070 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3071 != forwardBit) {
3072 l.softInputMode = (l.softInputMode
3073 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3074 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003075 if (r.activity.mVisibleFromClient) {
3076 ViewManager wm = a.getWindowManager();
3077 View decor = r.window.getDecorView();
3078 wm.updateViewLayout(decor, l);
3079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 }
3081 r.activity.mVisibleFromServer = true;
3082 mNumVisibleActivities++;
3083 if (r.activity.mVisibleFromClient) {
3084 r.activity.makeVisible();
3085 }
3086 }
3087
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003088 if (!r.onlyLocalRequest) {
3089 r.nextIdle = mNewActivities;
3090 mNewActivities = r;
3091 if (localLOGV) Slog.v(
3092 TAG, "Scheduling idle handler for " + r);
3093 Looper.myQueue().addIdleHandler(new Idler());
3094 }
3095 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003097 // Tell the activity manager we have resumed.
3098 if (reallyResume) {
3099 try {
3100 ActivityManagerNative.getDefault().activityResumed(token);
3101 } catch (RemoteException ex) {
3102 }
3103 }
3104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 } else {
3106 // If an exception was thrown when trying to resume, then
3107 // just end this activity.
3108 try {
3109 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07003110 .finishActivity(token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 } catch (RemoteException ex) {
3112 }
3113 }
3114 }
3115
3116 private int mThumbnailWidth = -1;
3117 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003118 private Bitmap mAvailThumbnailBitmap = null;
3119 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120
Romain Guy65b345f2011-07-27 18:51:50 -07003121 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003122 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003124 if (thumbnail == null) {
3125 int w = mThumbnailWidth;
3126 int h;
3127 if (w < 0) {
3128 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003129 int wId = com.android.internal.R.dimen.thumbnail_width;
3130 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003131 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3132 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003133 } else {
3134 h = mThumbnailHeight;
3135 }
3136
3137 // On platforms where we don't want thumbnails, set dims to (0,0)
3138 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003139 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3140 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003141 thumbnail.eraseColor(0);
3142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003145 if (thumbnail != null) {
3146 Canvas cv = mThumbnailCanvas;
3147 if (cv == null) {
3148 mThumbnailCanvas = cv = new Canvas();
3149 }
3150
3151 cv.setBitmap(thumbnail);
3152 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3153 mAvailThumbnailBitmap = thumbnail;
3154 thumbnail = null;
3155 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003156 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003159 } catch (Exception e) {
3160 if (!mInstrumentation.onException(r.activity, e)) {
3161 throw new RuntimeException(
3162 "Unable to create thumbnail of "
3163 + r.intent.getComponent().toShortString()
3164 + ": " + e.toString(), e);
3165 }
3166 thumbnail = null;
3167 }
3168
3169 return thumbnail;
3170 }
3171
Romain Guy65b345f2011-07-27 18:51:50 -07003172 private void handlePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003173 boolean userLeaving, int configChanges, boolean dontReport) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003174 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003176 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 if (userLeaving) {
3178 performUserLeavingActivity(r);
3179 }
Bob Leee5408332009-09-04 18:31:17 -07003180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003182 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003184 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003185 if (r.isPreHoneycomb()) {
3186 QueuedWork.waitToFinish();
3187 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003190 if (!dontReport) {
3191 try {
3192 ActivityManagerNative.getDefault().activityPaused(token);
3193 } catch (RemoteException ex) {
3194 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003196 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 }
3198 }
3199
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003200 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003201 mInstrumentation.callActivityOnUserLeaving(r.activity);
3202 }
3203
3204 final Bundle performPauseActivity(IBinder token, boolean finished,
3205 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003206 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003207 return r != null ? performPauseActivity(r, finished, saveState) : null;
3208 }
3209
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003210 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 boolean saveState) {
3212 if (r.paused) {
3213 if (r.activity.mFinished) {
3214 // If we are finishing, we won't call onResume() in certain cases.
3215 // So here we likewise don't want to call onPause() if the activity
3216 // isn't resumed.
3217 return null;
3218 }
3219 RuntimeException e = new RuntimeException(
3220 "Performing pause of activity that is not resumed: "
3221 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003222 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 if (finished) {
3225 r.activity.mFinished = true;
3226 }
3227 try {
3228 // Next have the activity save its current state and managed dialogs...
3229 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003230 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 }
3232 // Now we are idle.
3233 r.activity.mCalled = false;
3234 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003235 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3236 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 if (!r.activity.mCalled) {
3238 throw new SuperNotCalledException(
3239 "Activity " + r.intent.getComponent().toShortString() +
3240 " did not call through to super.onPause()");
3241 }
3242
3243 } catch (SuperNotCalledException e) {
3244 throw e;
3245
3246 } catch (Exception e) {
3247 if (!mInstrumentation.onException(r.activity, e)) {
3248 throw new RuntimeException(
3249 "Unable to pause activity "
3250 + r.intent.getComponent().toShortString()
3251 + ": " + e.toString(), e);
3252 }
3253 }
3254 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003255
3256 // Notify any outstanding on paused listeners
3257 ArrayList<OnActivityPausedListener> listeners;
3258 synchronized (mOnPauseListeners) {
3259 listeners = mOnPauseListeners.remove(r.activity);
3260 }
3261 int size = (listeners != null ? listeners.size() : 0);
3262 for (int i = 0; i < size; i++) {
3263 listeners.get(i).onPaused(r.activity);
3264 }
3265
Craig Mautnera0026042014-04-23 11:45:37 -07003266 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003267 }
3268
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003269 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003270 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003271 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003272 }
3273
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003274 private static class StopInfo implements Runnable {
3275 ActivityClientRecord activity;
3276 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003277 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003278 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003279
3280 @Override public void run() {
3281 // Tell activity manager we have been stopped.
3282 try {
3283 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3284 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003285 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003286 } catch (RemoteException ex) {
3287 }
3288 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003289 }
3290
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003291 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003292 public final IActivityManager.ContentProviderHolder holder;
3293 public final ProviderClientRecord client;
3294 public int stableCount;
3295 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003296
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003297 // When this is set, the stable and unstable ref counts are 0 and
3298 // we have a pending operation scheduled to remove the ref count
3299 // from the activity manager. On the activity manager we are still
3300 // holding an unstable ref, though it is not reflected in the counts
3301 // here.
3302 public boolean removePending;
3303
3304 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3305 ProviderClientRecord inClient, int sCount, int uCount) {
3306 holder = inHolder;
3307 client = inClient;
3308 stableCount = sCount;
3309 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310 }
3311 }
3312
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003313 /**
3314 * Core implementation of stopping an activity. Note this is a little
3315 * tricky because the server's meaning of stop is slightly different
3316 * than our client -- for the server, stop means to save state and give
3317 * it the result when it is done, but the window may still be visible.
3318 * For the client, we want to call onStop()/onStart() to indicate when
3319 * the activity's UI visibillity changes.
3320 */
Romain Guy65b345f2011-07-27 18:51:50 -07003321 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003322 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003323 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003324 if (r != null) {
3325 if (!keepShown && r.stopped) {
3326 if (r.activity.mFinished) {
3327 // If we are finishing, we won't call onResume() in certain
3328 // cases. So here we likewise don't want to call onStop()
3329 // if the activity isn't resumed.
3330 return;
3331 }
3332 RuntimeException e = new RuntimeException(
3333 "Performing stop of activity that is not resumed: "
3334 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003335 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003336 }
3337
3338 if (info != null) {
3339 try {
3340 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003341 // For now, don't create the thumbnail here; we are
3342 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 info.description = r.activity.onCreateDescription();
3344 } catch (Exception e) {
3345 if (!mInstrumentation.onException(r.activity, e)) {
3346 throw new RuntimeException(
3347 "Unable to save state of activity "
3348 + r.intent.getComponent().toShortString()
3349 + ": " + e.toString(), e);
3350 }
3351 }
3352 }
3353
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003354 // Next have the activity save its current state and managed dialogs...
3355 if (!r.activity.mFinished && saveState) {
3356 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003357 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003358 }
3359 }
3360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 if (!keepShown) {
3362 try {
3363 // Now we are idle.
3364 r.activity.performStop();
3365 } catch (Exception e) {
3366 if (!mInstrumentation.onException(r.activity, e)) {
3367 throw new RuntimeException(
3368 "Unable to stop activity "
3369 + r.intent.getComponent().toShortString()
3370 + ": " + e.toString(), e);
3371 }
3372 }
3373 r.stopped = true;
3374 }
3375
3376 r.paused = true;
3377 }
3378 }
3379
Romain Guy65b345f2011-07-27 18:51:50 -07003380 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 View v = r.activity.mDecor;
3382 if (v != null) {
3383 if (show) {
3384 if (!r.activity.mVisibleFromServer) {
3385 r.activity.mVisibleFromServer = true;
3386 mNumVisibleActivities++;
3387 if (r.activity.mVisibleFromClient) {
3388 r.activity.makeVisible();
3389 }
3390 }
3391 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003392 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003393 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003395 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003396 r.newConfig = null;
3397 }
3398 } else {
3399 if (r.activity.mVisibleFromServer) {
3400 r.activity.mVisibleFromServer = false;
3401 mNumVisibleActivities--;
3402 v.setVisibility(View.INVISIBLE);
3403 }
3404 }
3405 }
3406 }
3407
Romain Guy65b345f2011-07-27 18:51:50 -07003408 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003409 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003410 r.activity.mConfigChangeFlags |= configChanges;
3411
3412 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003413 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003415 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 TAG, "Finishing stop of " + r + ": show=" + show
3417 + " win=" + r.window);
3418
3419 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003420
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003421 // Make sure any pending writes are now committed.
3422 if (!r.isPreHoneycomb()) {
3423 QueuedWork.waitToFinish();
3424 }
3425
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003426 // Schedule the call to tell the activity manager we have
3427 // stopped. We don't do this immediately, because we want to
3428 // have a chance for any other pending work (in particular memory
3429 // trim requests) to complete before you tell the activity
3430 // manager to proceed and allow us to go fully into the background.
3431 info.activity = r;
3432 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003433 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003434 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003435 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436 }
3437
3438 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003439 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 if (r.stopped) {
3441 r.activity.performRestart();
3442 r.stopped = false;
3443 }
3444 }
3445
Romain Guy65b345f2011-07-27 18:51:50 -07003446 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003447 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003448
3449 if (r == null) {
3450 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3451 return;
3452 }
3453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003455 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 } else if (show && r.stopped) {
3457 // If we are getting ready to gc after going to the background, well
3458 // we are back active so skip it.
3459 unscheduleGcIdler();
3460
3461 r.activity.performRestart();
3462 r.stopped = false;
3463 }
3464 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003465 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003466 TAG, "Handle window " + r + " visibility: " + show);
3467 updateVisibility(r, show);
3468 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003469 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 }
3471
Romain Guy65b345f2011-07-27 18:51:50 -07003472 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003473 ActivityClientRecord r = mActivities.get(token);
3474
3475 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003476 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003477 return;
3478 }
3479
3480 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003481 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003482 try {
3483 // Now we are idle.
3484 r.activity.performStop();
3485 } catch (Exception e) {
3486 if (!mInstrumentation.onException(r.activity, e)) {
3487 throw new RuntimeException(
3488 "Unable to stop activity "
3489 + r.intent.getComponent().toShortString()
3490 + ": " + e.toString(), e);
3491 }
3492 }
3493 r.stopped = true;
3494 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003495
3496 // Make sure any pending writes are now committed.
3497 if (!r.isPreHoneycomb()) {
3498 QueuedWork.waitToFinish();
3499 }
3500
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003501 // Tell activity manager we slept.
3502 try {
3503 ActivityManagerNative.getDefault().activitySlept(r.token);
3504 } catch (RemoteException ex) {
3505 }
3506 } else {
3507 if (r.stopped && r.activity.mVisibleFromServer) {
3508 r.activity.performRestart();
3509 r.stopped = false;
3510 }
3511 }
3512 }
3513
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003514 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003515 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003516 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003517 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003518 onCoreSettingsChange();
3519 }
3520
3521 private void onCoreSettingsChange() {
3522 boolean debugViewAttributes =
3523 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3524 if (debugViewAttributes != View.mDebugViewAttributes) {
3525 View.mDebugViewAttributes = debugViewAttributes;
3526
3527 // request all activities to relaunch for the changes to take place
3528 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
3529 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, false);
3530 }
3531 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003532 }
3533
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003534 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3535 LoadedApk apk = peekPackageInfo(data.pkg, false);
3536 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003537 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003538 }
3539 apk = peekPackageInfo(data.pkg, true);
3540 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003541 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003542 }
3543 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003544 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003545 }
3546
Romain Guy65b345f2011-07-27 18:51:50 -07003547 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 final int N = results.size();
3549 for (int i=0; i<N; i++) {
3550 ResultInfo ri = results.get(i);
3551 try {
3552 if (ri.mData != null) {
3553 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003554 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003556 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003557 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003558 r.activity.dispatchActivityResult(ri.mResultWho,
3559 ri.mRequestCode, ri.mResultCode, ri.mData);
3560 } catch (Exception e) {
3561 if (!mInstrumentation.onException(r.activity, e)) {
3562 throw new RuntimeException(
3563 "Failure delivering result " + ri + " to activity "
3564 + r.intent.getComponent().toShortString()
3565 + ": " + e.toString(), e);
3566 }
3567 }
3568 }
3569 }
3570
Romain Guy65b345f2011-07-27 18:51:50 -07003571 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003572 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003573 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 if (r != null) {
3575 final boolean resumed = !r.paused;
3576 if (!r.activity.mFinished && r.activity.mDecor != null
3577 && r.hideForNow && resumed) {
3578 // We had hidden the activity because it started another
3579 // one... we have gotten a result back and we are not
3580 // paused, so make sure our window is visible.
3581 updateVisibility(r, true);
3582 }
3583 if (resumed) {
3584 try {
3585 // Now we are idle.
3586 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003587 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003588 mInstrumentation.callActivityOnPause(r.activity);
3589 if (!r.activity.mCalled) {
3590 throw new SuperNotCalledException(
3591 "Activity " + r.intent.getComponent().toShortString()
3592 + " did not call through to super.onPause()");
3593 }
3594 } catch (SuperNotCalledException e) {
3595 throw e;
3596 } catch (Exception e) {
3597 if (!mInstrumentation.onException(r.activity, e)) {
3598 throw new RuntimeException(
3599 "Unable to pause activity "
3600 + r.intent.getComponent().toShortString()
3601 + ": " + e.toString(), e);
3602 }
3603 }
3604 }
3605 deliverResults(r, res.results);
3606 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003607 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003608 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609 }
3610 }
3611 }
3612
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003613 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 return performDestroyActivity(token, finishing, 0, false);
3615 }
3616
Romain Guy65b345f2011-07-27 18:51:50 -07003617 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003619 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003620 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003621 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003623 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 r.activity.mConfigChangeFlags |= configChanges;
3625 if (finishing) {
3626 r.activity.mFinished = true;
3627 }
3628 if (!r.paused) {
3629 try {
3630 r.activity.mCalled = false;
3631 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003632 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 r.activity.getComponentName().getClassName());
3634 if (!r.activity.mCalled) {
3635 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003636 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 + " did not call through to super.onPause()");
3638 }
3639 } catch (SuperNotCalledException e) {
3640 throw e;
3641 } catch (Exception e) {
3642 if (!mInstrumentation.onException(r.activity, e)) {
3643 throw new RuntimeException(
3644 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003645 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003646 + ": " + e.toString(), e);
3647 }
3648 }
3649 r.paused = true;
3650 }
3651 if (!r.stopped) {
3652 try {
3653 r.activity.performStop();
3654 } catch (SuperNotCalledException e) {
3655 throw e;
3656 } catch (Exception e) {
3657 if (!mInstrumentation.onException(r.activity, e)) {
3658 throw new RuntimeException(
3659 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003660 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661 + ": " + e.toString(), e);
3662 }
3663 }
3664 r.stopped = true;
3665 }
3666 if (getNonConfigInstance) {
3667 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003668 r.lastNonConfigurationInstances
3669 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003670 } catch (Exception e) {
3671 if (!mInstrumentation.onException(r.activity, e)) {
3672 throw new RuntimeException(
3673 "Unable to retain activity "
3674 + r.intent.getComponent().toShortString()
3675 + ": " + e.toString(), e);
3676 }
3677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003678 }
3679 try {
3680 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003681 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003682 if (!r.activity.mCalled) {
3683 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003684 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 " did not call through to super.onDestroy()");
3686 }
3687 if (r.window != null) {
3688 r.window.closeAllPanels();
3689 }
3690 } catch (SuperNotCalledException e) {
3691 throw e;
3692 } catch (Exception e) {
3693 if (!mInstrumentation.onException(r.activity, e)) {
3694 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003695 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3696 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 }
3698 }
3699 }
3700 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003701 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003702 return r;
3703 }
3704
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003705 private static String safeToComponentShortString(Intent intent) {
3706 ComponentName component = intent.getComponent();
3707 return component == null ? "[Unknown]" : component.toShortString();
3708 }
3709
Romain Guy65b345f2011-07-27 18:51:50 -07003710 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003712 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 configChanges, getNonConfigInstance);
3714 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003715 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 WindowManager wm = r.activity.getWindowManager();
3717 View v = r.activity.mDecor;
3718 if (v != null) {
3719 if (r.activity.mVisibleFromServer) {
3720 mNumVisibleActivities--;
3721 }
3722 IBinder wtoken = v.getWindowToken();
3723 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003724 if (r.onlyLocalRequest) {
3725 // Hold off on removing this until the new activity's
3726 // window is being added.
3727 r.mPendingRemoveWindow = v;
3728 r.mPendingRemoveWindowManager = wm;
3729 } else {
3730 wm.removeViewImmediate(v);
3731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003732 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003733 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003734 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003735 r.activity.getClass().getName(), "Activity");
3736 }
3737 r.activity.mDecor = null;
3738 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003739 if (r.mPendingRemoveWindow == null) {
3740 // If we are delaying the removal of the activity window, then
3741 // we can't clean up all windows here. Note that we can't do
3742 // so later either, which means any windows that aren't closed
3743 // by the app will leak. Well we try to warning them a lot
3744 // about leaking windows, because that is a bug, so if they are
3745 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003746 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003747 r.activity.getClass().getName(), "Activity");
3748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003749
3750 // Mocked out contexts won't be participating in the normal
3751 // process lifecycle, but if we're running with a proper
3752 // ApplicationContext we need to have it tear down things
3753 // cleanly.
3754 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003755 if (c instanceof ContextImpl) {
3756 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 r.activity.getClass().getName(), "Activity");
3758 }
3759 }
3760 if (finishing) {
3761 try {
3762 ActivityManagerNative.getDefault().activityDestroyed(token);
3763 } catch (RemoteException ex) {
3764 // If the system process has died, it's game over for everyone.
3765 }
3766 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003767 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
3769
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003770 public final void requestRelaunchActivity(IBinder token,
3771 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3772 int configChanges, boolean notResumed, Configuration config,
3773 boolean fromServer) {
3774 ActivityClientRecord target = null;
3775
Craig Mautner88c05892013-06-28 09:47:45 -07003776 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003777 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3778 ActivityClientRecord r = mRelaunchingActivities.get(i);
3779 if (r.token == token) {
3780 target = r;
3781 if (pendingResults != null) {
3782 if (r.pendingResults != null) {
3783 r.pendingResults.addAll(pendingResults);
3784 } else {
3785 r.pendingResults = pendingResults;
3786 }
3787 }
3788 if (pendingNewIntents != null) {
3789 if (r.pendingIntents != null) {
3790 r.pendingIntents.addAll(pendingNewIntents);
3791 } else {
3792 r.pendingIntents = pendingNewIntents;
3793 }
3794 }
3795 break;
3796 }
3797 }
3798
3799 if (target == null) {
3800 target = new ActivityClientRecord();
3801 target.token = token;
3802 target.pendingResults = pendingResults;
3803 target.pendingIntents = pendingNewIntents;
3804 if (!fromServer) {
3805 ActivityClientRecord existing = mActivities.get(token);
3806 if (existing != null) {
3807 target.startsNotResumed = existing.paused;
3808 }
3809 target.onlyLocalRequest = true;
3810 }
3811 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003812 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003813 }
3814
3815 if (fromServer) {
3816 target.startsNotResumed = notResumed;
3817 target.onlyLocalRequest = false;
3818 }
3819 if (config != null) {
3820 target.createdConfig = config;
3821 }
3822 target.pendingConfigChanges |= configChanges;
3823 }
3824 }
3825
Romain Guy65b345f2011-07-27 18:51:50 -07003826 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003827 // If we are getting ready to gc after going to the background, well
3828 // we are back active so skip it.
3829 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003830 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831
3832 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003833 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003835 // First: make sure we have the most recent configuration and most
3836 // recent version of the activity, or skip it if some previous call
3837 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003838 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003839 int N = mRelaunchingActivities.size();
3840 IBinder token = tmp.token;
3841 tmp = null;
3842 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003843 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003844 if (r.token == token) {
3845 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003846 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003847 mRelaunchingActivities.remove(i);
3848 i--;
3849 N--;
3850 }
3851 }
Bob Leee5408332009-09-04 18:31:17 -07003852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003854 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003855 return;
3856 }
Bob Leee5408332009-09-04 18:31:17 -07003857
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003858 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3859 + tmp.token + " with configChanges=0x"
3860 + Integer.toHexString(configChanges));
3861
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 if (mPendingConfiguration != null) {
3863 changedConfig = mPendingConfiguration;
3864 mPendingConfiguration = null;
3865 }
3866 }
Bob Leee5408332009-09-04 18:31:17 -07003867
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003868 if (tmp.createdConfig != null) {
3869 // If the activity manager is passing us its current config,
3870 // assume that is really what we want regardless of what we
3871 // may have pending.
3872 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003873 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3874 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3875 if (changedConfig == null
3876 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3877 changedConfig = tmp.createdConfig;
3878 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003879 }
3880 }
3881
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003882 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003883 + tmp.token + ": changedConfig=" + changedConfig);
3884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003885 // If there was a pending configuration change, execute it first.
3886 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003887 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003888 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003889 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003890 }
Bob Leee5408332009-09-04 18:31:17 -07003891
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003892 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003893 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003894 if (r == null) {
3895 return;
3896 }
Bob Leee5408332009-09-04 18:31:17 -07003897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003898 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003899 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003900 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003901
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003902 r.activity.mChangingConfigurations = true;
3903
Dianne Hackborne2b04802010-12-09 09:24:55 -08003904 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003905 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003906 performPauseActivity(r.token, false, r.isPreHoneycomb());
3907 }
3908 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07003909 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 }
Bob Leee5408332009-09-04 18:31:17 -07003911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003913
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003914 r.activity = null;
3915 r.window = null;
3916 r.hideForNow = false;
3917 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003918 // Merge any pending results and pending intents; don't just replace them
3919 if (tmp.pendingResults != null) {
3920 if (r.pendingResults == null) {
3921 r.pendingResults = tmp.pendingResults;
3922 } else {
3923 r.pendingResults.addAll(tmp.pendingResults);
3924 }
3925 }
3926 if (tmp.pendingIntents != null) {
3927 if (r.pendingIntents == null) {
3928 r.pendingIntents = tmp.pendingIntents;
3929 } else {
3930 r.pendingIntents.addAll(tmp.pendingIntents);
3931 }
3932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003933 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003934
Christopher Tateb70f3df2009-04-07 16:07:59 -07003935 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003936 }
3937
Craig Mautnera0026042014-04-23 11:45:37 -07003938 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
3939 r.state = new Bundle();
3940 r.state.setAllowFds(false);
3941 if (r.isPersistable()) {
3942 r.persistentState = new PersistableBundle();
3943 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
3944 r.persistentState);
3945 } else {
3946 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
3947 }
3948 }
3949
Dianne Hackborn73c14162012-09-19 15:45:06 -07003950 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003952 ArrayList<ComponentCallbacks2> callbacks
3953 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003954
Craig Mautner88c05892013-06-28 09:47:45 -07003955 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003956 final int NAPP = mAllApplications.size();
3957 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003958 callbacks.add(mAllApplications.get(i));
3959 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003960 final int NACT = mActivities.size();
3961 for (int i=0; i<NACT; i++) {
3962 ActivityClientRecord ar = mActivities.valueAt(i);
3963 Activity a = ar.activity;
3964 if (a != null) {
3965 Configuration thisConfig = applyConfigCompatMainThread(
3966 mCurDefaultDisplayDpi, newConfig,
3967 ar.packageInfo.getCompatibilityInfo());
3968 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3969 // If the activity is currently resumed, its configuration
3970 // needs to change right now.
3971 callbacks.add(a);
3972 } else if (thisConfig != null) {
3973 // Otherwise, we will tell it about the change
3974 // the next time it is resumed or shown. Note that
3975 // the activity manager may, before then, decide the
3976 // activity needs to be destroyed to handle its new
3977 // configuration.
3978 if (DEBUG_CONFIGURATION) {
3979 Slog.v(TAG, "Setting activity "
3980 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003981 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003982 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003983 }
3984 }
3985 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003986 final int NSVC = mServices.size();
3987 for (int i=0; i<NSVC; i++) {
3988 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003989 }
3990 }
3991 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003992 final int NPRV = mLocalProviders.size();
3993 for (int i=0; i<NPRV; i++) {
3994 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003995 }
3996 }
Bob Leee5408332009-09-04 18:31:17 -07003997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003998 return callbacks;
3999 }
Bob Leee5408332009-09-04 18:31:17 -07004000
Romain Guya998dff2012-03-23 18:58:36 -07004001 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004003 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 // we check the runtime type and act accordingly.
4005 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4006 if (activity != null) {
4007 activity.mCalled = false;
4008 }
Bob Leee5408332009-09-04 18:31:17 -07004009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004010 boolean shouldChangeConfig = false;
4011 if ((activity == null) || (activity.mCurrentConfig == null)) {
4012 shouldChangeConfig = true;
4013 } else {
Bob Leee5408332009-09-04 18:31:17 -07004014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004015 // If the new config is the same as the config this Activity
4016 // is already running with then don't bother calling
4017 // onConfigurationChanged
4018 int diff = activity.mCurrentConfig.diff(config);
4019 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004020 // If this activity doesn't handle any of the config changes
4021 // then don't bother calling onConfigurationChanged as we're
4022 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07004023 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 shouldChangeConfig = true;
4025 }
4026 }
4027 }
Bob Leee5408332009-09-04 18:31:17 -07004028
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004029 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004030 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004031 if (shouldChangeConfig) {
4032 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07004033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 if (activity != null) {
4035 if (!activity.mCalled) {
4036 throw new SuperNotCalledException(
4037 "Activity " + activity.getLocalClassName() +
4038 " did not call through to super.onConfigurationChanged()");
4039 }
4040 activity.mConfigChangeFlags = 0;
4041 activity.mCurrentConfig = new Configuration(config);
4042 }
4043 }
4044 }
4045
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004046 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004047 synchronized (mResourcesManager) {
4048 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004049 }
4050 }
4051
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004052 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004053 Configuration config = mConfiguration;
4054 if (mCompatConfiguration == null) {
4055 mCompatConfiguration = new Configuration();
4056 }
4057 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004058 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004059 config = mCompatConfiguration;
4060 }
4061 return config;
4062 }
4063
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004064 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004065
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004066 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004067
Craig Mautner88c05892013-06-28 09:47:45 -07004068 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004069 if (mPendingConfiguration != null) {
4070 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4071 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004072 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004073 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004074 }
4075 mPendingConfiguration = null;
4076 }
4077
4078 if (config == null) {
4079 return;
4080 }
4081
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004082 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004083 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004084
4085 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004087 if (mConfiguration == null) {
4088 mConfiguration = new Configuration();
4089 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004090 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004091 return;
4092 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004093 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004095 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004096 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004097
4098 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4099
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004100 freeTextLayoutCachesIfNeeded(configDiff);
4101
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004102 if (callbacks != null) {
4103 final int N = callbacks.size();
4104 for (int i=0; i<N; i++) {
4105 performConfigurationChanged(callbacks.get(i), config);
4106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004107 }
4108 }
4109
Romain Guy46bfc482013-08-16 18:38:29 -07004110 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004111 if (configDiff != 0) {
4112 // Ask text layout engine to free its caches if there is a locale change
4113 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4114 if (hasLocaleConfigChange) {
4115 Canvas.freeTextLayoutCaches();
4116 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4117 }
4118 }
4119 }
4120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004122 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004123 if (r == null || r.activity == null) {
4124 return;
4125 }
Bob Leee5408332009-09-04 18:31:17 -07004126
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004127 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004128 + r.activityInfo.name);
4129
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004130 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004131
4132 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004133
4134 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004135 }
4136
Jeff Hao1b012d32014-08-20 10:35:34 -07004137 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004138 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004139 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004140 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004141 default:
4142 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004143 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004144 break;
4145 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004146 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004147 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004148 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004149 } finally {
4150 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004151 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004152 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004153 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004154 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004155 }
4156 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004157 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004158 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004159 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004160 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004161 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004162 }
4163 }
Bob Leee5408332009-09-04 18:31:17 -07004164
Romain Guya998dff2012-03-23 18:58:36 -07004165 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004166 if (managed) {
4167 try {
4168 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4169 } catch (IOException e) {
4170 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4171 + " -- can the process access this path?");
4172 } finally {
4173 try {
4174 dhd.fd.close();
4175 } catch (IOException e) {
4176 Slog.w(TAG, "Failure closing profile fd", e);
4177 }
4178 }
4179 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004180 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004181 }
4182 }
4183
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004184 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4185 boolean hasPkgInfo = false;
4186 if (packages != null) {
mark_chen89764e32014-12-12 15:38:48 +08004187 synchronized (mResourcesManager) {
4188 for (int i=packages.length-1; i>=0; i--) {
4189 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4190 if (!hasPkgInfo) {
4191 WeakReference<LoadedApk> ref;
4192 ref = mPackages.get(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004193 if (ref != null && ref.get() != null) {
4194 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004195 } else {
4196 ref = mResourcePackages.get(packages[i]);
4197 if (ref != null && ref.get() != null) {
4198 hasPkgInfo = true;
4199 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004200 }
4201 }
mark_chen89764e32014-12-12 15:38:48 +08004202 mPackages.remove(packages[i]);
4203 mResourcePackages.remove(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004204 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004205 }
4206 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004207 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004208 hasPkgInfo);
4209 }
4210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004211 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004212 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 final int N = callbacks.size();
4215 for (int i=0; i<N; i++) {
4216 callbacks.get(i).onLowMemory();
4217 }
4218
Chris Tatece229052009-03-25 16:44:52 -07004219 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4220 if (Process.myUid() != Process.SYSTEM_UID) {
4221 int sqliteReleased = SQLiteDatabase.releaseMemory();
4222 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4223 }
Bob Leee5408332009-09-04 18:31:17 -07004224
Mike Reedcaf0df12009-04-27 14:32:05 -04004225 // Ask graphics to free up as much as possible (font/image caches)
4226 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004228 // Ask text layout engine to free also as much as possible
4229 Canvas.freeTextLayoutCaches();
4230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004231 BinderInternal.forceGc("mem");
4232 }
4233
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004234 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004235 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004236
Dianne Hackborn73c14162012-09-19 15:45:06 -07004237 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004238
4239 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004240 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004241 callbacks.get(i).onTrimMemory(level);
4242 }
Romain Guy19f86e82012-04-23 15:19:07 -07004243
John Reckf47a5942014-06-30 16:20:04 -07004244 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004245 }
4246
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004247 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004248 if (Process.isIsolated()) {
4249 // Isolated processes aren't going to do UI.
4250 return;
4251 }
Romain Guya9582652011-11-10 14:20:10 -08004252 try {
4253 int uid = Process.myUid();
4254 String[] packages = getPackageManager().getPackagesForUid(uid);
4255
4256 // If there are several packages in this application we won't
4257 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004258 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004259 HardwareRenderer.setupDiskCache(cacheDir);
4260 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004261 }
4262 } catch (RemoteException e) {
4263 // Ignore
4264 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004265 }
4266
4267 private void updateDefaultDensity() {
4268 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4269 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4270 && !mDensityCompatMode) {
4271 Slog.i(TAG, "Switching default density from "
4272 + DisplayMetrics.DENSITY_DEVICE + " to "
4273 + mCurDefaultDisplayDpi);
4274 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4275 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4276 }
4277 }
4278
Romain Guy65b345f2011-07-27 18:51:50 -07004279 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 mBoundApplication = data;
4281 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004282 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004284 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004285 if (data.initProfilerInfo != null) {
4286 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4287 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4288 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4289 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4290 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004293 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004294 android.ddm.DdmHandleAppName.setAppName(data.processName,
4295 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004296
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004297 if (data.persistent) {
4298 // Persistent processes on low-memory devices do not get to
4299 // use hardware accelerated drawing, since this can add too much
4300 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004301 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004302 HardwareRenderer.disable(false);
4303 }
4304 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004305
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004306 if (mProfiler.profileFd != null) {
4307 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004308 }
4309
Joe Onoratod630f102011-03-17 18:42:26 -07004310 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4311 // implementation to use the pool executor. Normally, we use the
4312 // serialized executor as the default. This has to happen in the
4313 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004314 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004315 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4316 }
4317
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004318 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004320 /*
4321 * Before spawning a new process, reset the time zone to be the system time zone.
4322 * This needs to be done because the system time zone could have changed after the
4323 * the spawning of this process. Without doing this this process would have the incorrect
4324 * system time zone.
4325 */
4326 TimeZone.setDefault(null);
4327
4328 /*
4329 * Initialize the default locale in this process for the reasons we set the time zone.
4330 */
4331 Locale.setDefault(data.config.locale);
4332
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004333 /*
4334 * Update the system configuration since its preloaded and might not
4335 * reflect configuration changes. The configuration object passed
4336 * in AppBindData can be safely assumed to be up to date
4337 */
Craig Mautner88c05892013-06-28 09:47:45 -07004338 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004339 mCurDefaultDisplayDpi = data.config.densityDpi;
4340 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004341
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004342 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004343
Dianne Hackborndde331c2012-08-03 14:01:57 -07004344 /**
4345 * Switch this process to density compatibility mode if needed.
4346 */
4347 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4348 == 0) {
4349 mDensityCompatMode = true;
4350 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4351 }
4352 updateDefaultDensity();
4353
Jeff Browndefd4a62014-03-10 21:24:37 -07004354 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004355 if (!Process.isIsolated()) {
4356 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004357
Amith Yamasani92d57052012-08-23 10:07:52 -07004358 if (cacheDir != null) {
4359 // Provide a usable directory for temporary files
4360 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
Amith Yamasani92d57052012-08-23 10:07:52 -07004361 } else {
Pirama Arumuga Nainar51772b72015-02-10 12:41:42 -08004362 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property due to missing cache directory");
4363 }
4364
4365 // Use codeCacheDir to store generated/compiled graphics code
4366 final File codeCacheDir = appContext.getCodeCacheDir();
4367 if (codeCacheDir != null) {
4368 setupGraphicsSupport(data.info, codeCacheDir);
4369 } else {
4370 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Amith Yamasani92d57052012-08-23 10:07:52 -07004371 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004372 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00004373
4374
4375 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4376 DateFormat.set24HourTimePref(is24Hr);
4377
Jon Miranda836c0a82014-08-11 12:32:26 -07004378 View.mDebugViewAttributes =
4379 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4380
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004381 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004382 * For system applications on userdebug/eng builds, log stack
4383 * traces of disk and network access to dropbox for analysis.
4384 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004385 if ((data.appInfo.flags &
4386 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004387 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4388 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004389 }
4390
4391 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004392 * For apps targetting SDK Honeycomb or later, we don't allow
4393 * network usage on the main event loop / UI thread.
4394 *
4395 * Note to those grepping: this is what ultimately throws
4396 * NetworkOnMainThreadException ...
4397 */
4398 if (data.appInfo.targetSdkVersion > 9) {
4399 StrictMode.enableDeathOnNetwork();
4400 }
4401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4403 // XXX should have option to change the port.
4404 Debug.changeDebugPort(8100);
4405 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004406 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 + " is waiting for the debugger on port 8100...");
4408
4409 IActivityManager mgr = ActivityManagerNative.getDefault();
4410 try {
4411 mgr.showWaitingForDebugger(mAppThread, true);
4412 } catch (RemoteException ex) {
4413 }
4414
4415 Debug.waitForDebugger();
4416
4417 try {
4418 mgr.showWaitingForDebugger(mAppThread, false);
4419 } catch (RemoteException ex) {
4420 }
4421
4422 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004423 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004424 + " can be debugged on port 8100...");
4425 }
4426 }
4427
Siva Velusamy92a8b222012-03-09 16:24:04 -08004428 // Enable OpenGL tracing if required
4429 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004430 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004431 }
4432
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004433 // Allow application-generated systrace messages if we're debuggable.
4434 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4435 Trace.setAppTracingAllowed(appTracingAllowed);
4436
Robert Greenwalt434203a2010-10-11 16:00:27 -07004437 /**
4438 * Initialize the default http proxy in this process for the reasons we set the time zone.
4439 */
4440 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004441 if (b != null) {
4442 // In pre-boot mode (doing initial launch to collect password), not
4443 // all system is up. This includes the connectivity service, so don't
4444 // crash if we can't get it.
4445 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4446 try {
Jason Monk207900c2014-04-25 15:00:09 -04004447 ProxyInfo proxyInfo = service.getProxy();
4448 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004449 } catch (RemoteException e) {}
4450 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004453 InstrumentationInfo ii = null;
4454 try {
4455 ii = appContext.getPackageManager().
4456 getInstrumentationInfo(data.instrumentationName, 0);
4457 } catch (PackageManager.NameNotFoundException e) {
4458 }
4459 if (ii == null) {
4460 throw new RuntimeException(
4461 "Unable to find instrumentation info for: "
4462 + data.instrumentationName);
4463 }
4464
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004465 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004467 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
4468 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004469 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004470 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
4471 mInstrumentedLibDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004472
4473 ApplicationInfo instrApp = new ApplicationInfo();
4474 instrApp.packageName = ii.packageName;
4475 instrApp.sourceDir = ii.sourceDir;
4476 instrApp.publicSourceDir = ii.publicSourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004477 instrApp.splitSourceDirs = ii.splitSourceDirs;
4478 instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004479 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004480 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004481 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07004482 appContext.getClassLoader(), false, true, false);
Jeff Browndefd4a62014-03-10 21:24:37 -07004483 ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484
4485 try {
4486 java.lang.ClassLoader cl = instrContext.getClassLoader();
4487 mInstrumentation = (Instrumentation)
4488 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4489 } catch (Exception e) {
4490 throw new RuntimeException(
4491 "Unable to instantiate instrumentation "
4492 + data.instrumentationName + ": " + e.toString(), e);
4493 }
4494
4495 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004496 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4497 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004499 if (mProfiler.profileFile != null && !ii.handleProfiling
4500 && mProfiler.profileFd == null) {
4501 mProfiler.handlingProfiling = true;
4502 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004503 file.getParentFile().mkdirs();
4504 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4505 }
4506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507 } else {
4508 mInstrumentation = new Instrumentation();
4509 }
4510
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004511 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004512 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08004513 } else {
4514 // Small heap, clamp to the current growth limit and let the heap release
4515 // pages after the growth limit to the non growth limit capacity. b/18387825
4516 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004517 }
4518
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004519 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004520 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004521 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004522 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004524 // If the app is being launched for full backup or restore, bring it up in
4525 // a restricted environment with the base application class.
4526 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4527 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004528
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004529 // don't bring up providers in restricted mode; they may depend on the
4530 // app's custom Application class
4531 if (!data.restrictedBackupMode) {
4532 List<ProviderInfo> providers = data.providers;
4533 if (providers != null) {
4534 installContentProviders(app, providers);
4535 // For process that contains content providers, we want to
4536 // ensure that the JIT is enabled "at some point".
4537 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4538 }
4539 }
4540
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004541 // Do this after providers, since instrumentation tests generally start their
4542 // test thread at this point, and we don't want that racing.
4543 try {
4544 mInstrumentation.onCreate(data.instrumentationArgs);
4545 }
4546 catch (Exception e) {
4547 throw new RuntimeException(
4548 "Exception thrown in onCreate() of "
4549 + data.instrumentationName + ": " + e.toString(), e);
4550 }
4551
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004552 try {
4553 mInstrumentation.callApplicationOnCreate(app);
4554 } catch (Exception e) {
4555 if (!mInstrumentation.onException(app, e)) {
4556 throw new RuntimeException(
4557 "Unable to create application " + app.getClass().getName()
4558 + ": " + e.toString(), e);
4559 }
4560 }
4561 } finally {
4562 StrictMode.setThreadPolicy(savedPolicy);
4563 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004564 }
4565
4566 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4567 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004568 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4569 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004570 Debug.stopMethodTracing();
4571 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004572 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 // + ", app thr: " + mAppThread);
4574 try {
4575 am.finishInstrumentation(mAppThread, resultCode, results);
4576 } catch (RemoteException ex) {
4577 }
4578 }
4579
Romain Guy65b345f2011-07-27 18:51:50 -07004580 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 Context context, List<ProviderInfo> providers) {
4582 final ArrayList<IActivityManager.ContentProviderHolder> results =
4583 new ArrayList<IActivityManager.ContentProviderHolder>();
4584
Romain Guya998dff2012-03-23 18:58:36 -07004585 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004586 if (DEBUG_PROVIDER) {
4587 StringBuilder buf = new StringBuilder(128);
4588 buf.append("Pub ");
4589 buf.append(cpi.authority);
4590 buf.append(": ");
4591 buf.append(cpi.name);
4592 Log.i(TAG, buf.toString());
4593 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004594 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4595 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4596 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004597 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004599 }
4600 }
4601
4602 try {
4603 ActivityManagerNative.getDefault().publishContentProviders(
4604 getApplicationThread(), results);
4605 } catch (RemoteException ex) {
4606 }
4607 }
4608
Jeff Sharkey6d515712012-09-20 16:06:08 -07004609 public final IContentProvider acquireProvider(
4610 Context c, String auth, int userId, boolean stable) {
4611 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004612 if (provider != null) {
4613 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004614 }
4615
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004616 // There is a possible race here. Another thread may try to acquire
4617 // the same provider at the same time. When this happens, we want to ensure
4618 // that the first one wins.
4619 // Note that we cannot hold the lock while acquiring and installing the
4620 // provider since it might take a long time to run and it could also potentially
4621 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004622 IActivityManager.ContentProviderHolder holder = null;
4623 try {
4624 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004625 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004626 } catch (RemoteException ex) {
4627 }
4628 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004629 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 return null;
4631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004633 // Install provider will increment the reference count for us, and break
4634 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004635 holder = installProvider(c, holder, holder.info,
4636 true /*noisy*/, holder.noReleaseNeeded, stable);
4637 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004638 }
4639
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004640 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4641 if (stable) {
4642 prc.stableCount += 1;
4643 if (prc.stableCount == 1) {
4644 // We are acquiring a new stable reference on the provider.
4645 int unstableDelta;
4646 if (prc.removePending) {
4647 // We have a pending remove operation, which is holding the
4648 // last unstable reference. At this point we are converting
4649 // that unstable reference to our new stable reference.
4650 unstableDelta = -1;
4651 // Cancel the removal of the provider.
4652 if (DEBUG_PROVIDER) {
4653 Slog.v(TAG, "incProviderRef: stable "
4654 + "snatched provider from the jaws of death");
4655 }
4656 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004657 // There is a race! It fails to remove the message, which
4658 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004659 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4660 } else {
4661 unstableDelta = 0;
4662 }
4663 try {
4664 if (DEBUG_PROVIDER) {
4665 Slog.v(TAG, "incProviderRef Now stable - "
4666 + prc.holder.info.name + ": unstableDelta="
4667 + unstableDelta);
4668 }
4669 ActivityManagerNative.getDefault().refContentProvider(
4670 prc.holder.connection, 1, unstableDelta);
4671 } catch (RemoteException e) {
4672 //do nothing content provider object is dead any way
4673 }
4674 }
4675 } else {
4676 prc.unstableCount += 1;
4677 if (prc.unstableCount == 1) {
4678 // We are acquiring a new unstable reference on the provider.
4679 if (prc.removePending) {
4680 // Oh look, we actually have a remove pending for the
4681 // provider, which is still holding the last unstable
4682 // reference. We just need to cancel that to take new
4683 // ownership of the reference.
4684 if (DEBUG_PROVIDER) {
4685 Slog.v(TAG, "incProviderRef: unstable "
4686 + "snatched provider from the jaws of death");
4687 }
4688 prc.removePending = false;
4689 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4690 } else {
4691 // First unstable ref, increment our count in the
4692 // activity manager.
4693 try {
4694 if (DEBUG_PROVIDER) {
4695 Slog.v(TAG, "incProviderRef: Now unstable - "
4696 + prc.holder.info.name);
4697 }
4698 ActivityManagerNative.getDefault().refContentProvider(
4699 prc.holder.connection, 0, 1);
4700 } catch (RemoteException e) {
4701 //do nothing content provider object is dead any way
4702 }
4703 }
4704 }
4705 }
4706 }
4707
Jeff Sharkey6d515712012-09-20 16:06:08 -07004708 public final IContentProvider acquireExistingProvider(
4709 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004710 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004711 final ProviderKey key = new ProviderKey(auth, userId);
4712 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004713 if (pr == null) {
4714 return null;
4715 }
4716
4717 IContentProvider provider = pr.mProvider;
4718 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004719 if (!jBinder.isBinderAlive()) {
4720 // The hosting process of the provider has died; we can't
4721 // use this one.
4722 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4723 + ": existing object's process dead");
4724 handleUnstableProviderDiedLocked(jBinder, true);
4725 return null;
4726 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004727
4728 // Only increment the ref count if we have one. If we don't then the
4729 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004730 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004731 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004732 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004733 }
4734 return provider;
4735 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004736 }
4737
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004738 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4739 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004740 return false;
4741 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004744 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004745 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004746 if (prc == null) {
4747 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004749 }
4750
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004751 boolean lastRef = false;
4752 if (stable) {
4753 if (prc.stableCount == 0) {
4754 if (DEBUG_PROVIDER) Slog.v(TAG,
4755 "releaseProvider: stable ref count already 0, how?");
4756 return false;
4757 }
4758 prc.stableCount -= 1;
4759 if (prc.stableCount == 0) {
4760 // What we do at this point depends on whether there are
4761 // any unstable refs left: if there are, we just tell the
4762 // activity manager to decrement its stable count; if there
4763 // aren't, we need to enqueue this provider to be removed,
4764 // and convert to holding a single unstable ref while
4765 // doing so.
4766 lastRef = prc.unstableCount == 0;
4767 try {
4768 if (DEBUG_PROVIDER) {
4769 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4770 + lastRef + " - " + prc.holder.info.name);
4771 }
4772 ActivityManagerNative.getDefault().refContentProvider(
4773 prc.holder.connection, -1, lastRef ? 1 : 0);
4774 } catch (RemoteException e) {
4775 //do nothing content provider object is dead any way
4776 }
4777 }
4778 } else {
4779 if (prc.unstableCount == 0) {
4780 if (DEBUG_PROVIDER) Slog.v(TAG,
4781 "releaseProvider: unstable ref count already 0, how?");
4782 return false;
4783 }
4784 prc.unstableCount -= 1;
4785 if (prc.unstableCount == 0) {
4786 // If this is the last reference, we need to enqueue
4787 // this provider to be removed instead of telling the
4788 // activity manager to remove it at this point.
4789 lastRef = prc.stableCount == 0;
4790 if (!lastRef) {
4791 try {
4792 if (DEBUG_PROVIDER) {
4793 Slog.v(TAG, "releaseProvider: No longer unstable - "
4794 + prc.holder.info.name);
4795 }
4796 ActivityManagerNative.getDefault().refContentProvider(
4797 prc.holder.connection, 0, -1);
4798 } catch (RemoteException e) {
4799 //do nothing content provider object is dead any way
4800 }
4801 }
4802 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004803 }
4804
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004805 if (lastRef) {
4806 if (!prc.removePending) {
4807 // Schedule the actual remove asynchronously, since we don't know the context
4808 // this will be called in.
4809 // TODO: it would be nice to post a delayed message, so
4810 // if we come back and need the same provider quickly
4811 // we will still have it available.
4812 if (DEBUG_PROVIDER) {
4813 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4814 + prc.holder.info.name);
4815 }
4816 prc.removePending = true;
4817 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4818 mH.sendMessage(msg);
4819 } else {
4820 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4821 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004822 }
4823 return true;
4824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 }
4826
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004827 final void completeRemoveProvider(ProviderRefCount prc) {
4828 synchronized (mProviderMap) {
4829 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004830 // There was a race! Some other client managed to acquire
4831 // the provider before the removal was completed.
4832 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004833 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004834 + "provider still in use");
4835 return;
4836 }
4837
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004838 // More complicated race!! Some client managed to acquire the
4839 // provider and release it before the removal was completed.
4840 // Continue the removal, and abort the next remove message.
4841 prc.removePending = false;
4842
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004843 final IBinder jBinder = prc.holder.provider.asBinder();
4844 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4845 if (existingPrc == prc) {
4846 mProviderRefCountMap.remove(jBinder);
4847 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004848
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004849 for (int i=mProviderMap.size()-1; i>=0; i--) {
4850 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004851 IBinder myBinder = pr.mProvider.asBinder();
4852 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004853 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004854 }
4855 }
4856 }
4857
4858 try {
4859 if (DEBUG_PROVIDER) {
4860 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4861 + "removeContentProvider(" + prc.holder.info.name + ")");
4862 }
4863 ActivityManagerNative.getDefault().removeContentProvider(
4864 prc.holder.connection, false);
4865 } catch (RemoteException e) {
4866 //do nothing content provider object is dead any way
4867 }
4868 }
4869
4870 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004871 synchronized (mProviderMap) {
4872 handleUnstableProviderDiedLocked(provider, fromClient);
4873 }
4874 }
4875
4876 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4877 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4878 if (prc != null) {
4879 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4880 + provider + " " + prc.holder.info.name);
4881 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004882 for (int i=mProviderMap.size()-1; i>=0; i--) {
4883 ProviderClientRecord pr = mProviderMap.valueAt(i);
4884 if (pr != null && pr.mProvider.asBinder() == provider) {
4885 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4886 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004887 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004888 }
You Kimbc74de62013-10-01 00:13:26 +09004889
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004890 if (fromClient) {
4891 // We found out about this due to execution in our client
4892 // code. Tell the activity manager about it now, to ensure
4893 // that the next time we go to do anything with the provider
4894 // it knows it is dead (so we don't race with its death
4895 // notification).
4896 try {
4897 ActivityManagerNative.getDefault().unstableProviderDied(
4898 prc.holder.connection);
4899 } catch (RemoteException e) {
4900 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004901 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004902 }
4903 }
4904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004906 final void appNotRespondingViaProvider(IBinder provider) {
4907 synchronized (mProviderMap) {
4908 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4909 if (prc != null) {
4910 try {
4911 ActivityManagerNative.getDefault()
4912 .appNotRespondingViaProvider(prc.holder.connection);
4913 } catch (RemoteException e) {
4914 }
4915 }
4916 }
4917 }
4918
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004919 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004920 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
4921 final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
4922 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4923
4924 final ProviderClientRecord pcr = new ProviderClientRecord(
4925 auths, provider, localProvider, holder);
4926 for (String auth : auths) {
4927 final ProviderKey key = new ProviderKey(auth, userId);
4928 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004929 if (existing != null) {
4930 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004931 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004932 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004933 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 }
4935 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004936 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004937 }
4938
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004939 /**
4940 * Installs the provider.
4941 *
4942 * Providers that are local to the process or that come from the system server
4943 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4944 * Other remote providers are reference counted. The initial reference count
4945 * for all reference counted providers is one. Providers that are not reference
4946 * counted do not have a reference count (at all).
4947 *
4948 * This method detects when a provider has already been installed. When this happens,
4949 * it increments the reference count of the existing provider (if appropriate)
4950 * and returns the existing provider. This can happen due to concurrent
4951 * attempts to acquire the same provider.
4952 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004953 private IActivityManager.ContentProviderHolder installProvider(Context context,
4954 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4955 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004957 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004958 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004959 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004960 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004961 + info.name);
4962 }
4963 Context c = null;
4964 ApplicationInfo ai = info.applicationInfo;
4965 if (context.getPackageName().equals(ai.packageName)) {
4966 c = context;
4967 } else if (mInitialApplication != null &&
4968 mInitialApplication.getPackageName().equals(ai.packageName)) {
4969 c = mInitialApplication;
4970 } else {
4971 try {
4972 c = context.createPackageContext(ai.packageName,
4973 Context.CONTEXT_INCLUDE_CODE);
4974 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004975 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004976 }
4977 }
4978 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004979 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004980 ai.packageName +
4981 " while loading content provider " +
4982 info.name);
4983 return null;
4984 }
4985 try {
4986 final java.lang.ClassLoader cl = c.getClassLoader();
4987 localProvider = (ContentProvider)cl.
4988 loadClass(info.name).newInstance();
4989 provider = localProvider.getIContentProvider();
4990 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004991 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 info.name + " from sourceDir " +
4993 info.applicationInfo.sourceDir);
4994 return null;
4995 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004996 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 TAG, "Instantiating local provider " + info.name);
4998 // XXX Need to create the correct context for this provider.
4999 localProvider.attachInfo(c, info);
5000 } catch (java.lang.Exception e) {
5001 if (!mInstrumentation.onException(null, e)) {
5002 throw new RuntimeException(
5003 "Unable to get provider " + info.name
5004 + ": " + e.toString(), e);
5005 }
5006 return null;
5007 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005008 } else {
5009 provider = holder.provider;
5010 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005011 + info.name);
5012 }
5013
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005014 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005015
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005016 synchronized (mProviderMap) {
5017 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5018 + " / " + info.name);
5019 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005020 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005021 ComponentName cname = new ComponentName(info.packageName, info.name);
5022 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005023 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005024 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005025 Slog.v(TAG, "installProvider: lost the race, "
5026 + "using existing local provider");
5027 }
5028 provider = pr.mProvider;
5029 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005030 holder = new IActivityManager.ContentProviderHolder(info);
5031 holder.provider = provider;
5032 holder.noReleaseNeeded = true;
5033 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005034 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005035 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005036 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005037 retHolder = pr.mHolder;
5038 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005039 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5040 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005041 if (DEBUG_PROVIDER) {
5042 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005043 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005044 // We need to transfer our new reference to the existing
5045 // ref count, releasing the old one... but only if
5046 // release is needed (that is, it is not running in the
5047 // system process).
5048 if (!noReleaseNeeded) {
5049 incProviderRefLocked(prc, stable);
5050 try {
5051 ActivityManagerNative.getDefault().removeContentProvider(
5052 holder.connection, stable);
5053 } catch (RemoteException e) {
5054 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005055 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005056 }
5057 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005058 ProviderClientRecord client = installProviderAuthoritiesLocked(
5059 provider, localProvider, holder);
5060 if (noReleaseNeeded) {
5061 prc = new ProviderRefCount(holder, client, 1000, 1000);
5062 } else {
5063 prc = stable
5064 ? new ProviderRefCount(holder, client, 1, 0)
5065 : new ProviderRefCount(holder, client, 0, 1);
5066 }
5067 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005068 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005069 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005072
5073 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 }
5075
Romain Guy65b345f2011-07-27 18:51:50 -07005076 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005077 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005078 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005079 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005080 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005081 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005082 public void run() {
5083 ensureJitEnabled();
5084 }
5085 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005086 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5087 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005089 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005090 try {
5091 mgr.attachApplication(mAppThread);
5092 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005093 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005095 // Watch for getting close to heap limit.
5096 BinderInternal.addGcWatcher(new Runnable() {
5097 @Override public void run() {
5098 if (!mSomeActivitiesChanged) {
5099 return;
5100 }
5101 Runtime runtime = Runtime.getRuntime();
5102 long dalvikMax = runtime.maxMemory();
5103 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5104 if (dalvikUsed > ((3*dalvikMax)/4)) {
5105 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5106 + " total=" + (runtime.totalMemory()/1024)
5107 + " used=" + (dalvikUsed/1024));
5108 mSomeActivitiesChanged = false;
5109 try {
5110 mgr.releaseSomeActivities(mAppThread);
5111 } catch (RemoteException e) {
5112 }
5113 }
5114 }
5115 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 } else {
5117 // Don't set application object here -- if the system crashes,
5118 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005119 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005120 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 try {
5122 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005123 ContextImpl context = ContextImpl.createAppContext(
5124 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005125 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5126 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 } catch (Exception e) {
5128 throw new RuntimeException(
5129 "Unable to instantiate Application():" + e.toString(), e);
5130 }
5131 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005132
5133 // add dropbox logging to libcore
5134 DropBox.setReporter(new DropBoxReporter());
5135
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005136 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005137 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005138 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005139 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005140 // We need to apply this change to the resources
5141 // immediately, because upon returning the view
5142 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005143 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005144 // This actually changed the resources! Tell
5145 // everyone about it.
5146 if (mPendingConfiguration == null ||
5147 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5148 mPendingConfiguration = newConfig;
5149
Jeff Brown9ef09972013-10-15 20:49:59 -07005150 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005151 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005152 }
5153 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005154 }
Craig Mautner88c05892013-06-28 09:47:45 -07005155 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005156 public void onLowMemory() {
5157 }
Craig Mautner88c05892013-06-28 09:47:45 -07005158 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005159 public void onTrimMemory(int level) {
5160 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005161 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162 }
5163
Romain Guy5e9120d2012-01-30 12:17:22 -08005164 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005165 // The system process on low-memory devices do not get to use hardware
5166 // accelerated drawing, since this can add too much overhead to the
5167 // process.
5168 if (!ActivityManager.isHighEndGfx()) {
5169 HardwareRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005170 } else {
5171 HardwareRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 ActivityThread thread = new ActivityThread();
5174 thread.attach(true);
5175 return thread;
5176 }
5177
Jeff Brown10e89712011-07-08 18:52:57 -07005178 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005180 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 }
5182 }
5183
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005184 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005185 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005186 if (mCoreSettings != null) {
5187 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005188 }
Craig Mautner88c05892013-06-28 09:47:45 -07005189 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005190 }
5191 }
5192
Geremy Condra69689a72012-09-11 16:57:17 -07005193 private static class EventLoggingReporter implements EventLogger.Reporter {
5194 @Override
5195 public void report (int code, Object... list) {
5196 EventLog.writeEvent(code, list);
5197 }
5198 }
5199
Geremy Condrab7faaf42012-09-19 18:07:42 -07005200 private class DropBoxReporter implements DropBox.Reporter {
5201
5202 private DropBoxManager dropBox;
5203
5204 public DropBoxReporter() {
5205 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5206 }
5207
5208 @Override
5209 public void addData(String tag, byte[] data, int flags) {
5210 dropBox.addData(tag, data, flags);
5211 }
5212
5213 @Override
5214 public void addText(String tag, String data) {
5215 dropBox.addText(tag, data);
5216 }
5217 }
5218
Romain Guy65b345f2011-07-27 18:51:50 -07005219 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07005220 SamplingProfilerIntegration.start();
5221
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005222 // CloseGuard defaults to true and can be quite spammy. We
5223 // disable it here, but selectively enable it later (via
5224 // StrictMode) on debug builds, but using DropBox, not logs.
5225 CloseGuard.setEnabled(false);
5226
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005227 Environment.initForCurrentUser();
5228
Geremy Condra69689a72012-09-11 16:57:17 -07005229 // Set the reporter for event logging in libcore
5230 EventLogger.setReporter(new EventLoggingReporter());
5231
Kenny Root8b514752013-02-04 09:35:16 -08005232 Security.addProvider(new AndroidKeyStoreProvider());
5233
Robin Lee3d076af2014-04-25 14:57:49 +01005234 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5235 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5236 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 Process.setArgV0("<pre-initialized>");
5239
5240 Looper.prepareMainLooper();
5241
5242 ActivityThread thread = new ActivityThread();
5243 thread.attach(false);
5244
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005245 if (sMainThreadHandler == null) {
5246 sMainThreadHandler = thread.getHandler();
5247 }
5248
Romain Guy5e9120d2012-01-30 12:17:22 -08005249 AsyncTask.init();
5250
Dianne Hackborn287952c2010-09-22 22:34:31 -07005251 if (false) {
5252 Looper.myLooper().setMessageLogging(new
5253 LogPrinter(Log.DEBUG, "ActivityThread"));
5254 }
5255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005256 Looper.loop();
5257
Jeff Brown10e89712011-07-08 18:52:57 -07005258 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005259 }
5260}