blob: 561a029b2c76cce08ab4f0c8b4566d6857f85fd2 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120
Geremy Condrab7faaf42012-09-19 18:07:42 -0700121import libcore.io.DropBox;
Geremy Condra69689a72012-09-11 16:57:17 -0700122import libcore.io.EventLogger;
Jeff Sharkeye861b422012-03-01 20:59:22 -0800123import libcore.io.IoUtils;
Paul Jensene401d172014-09-12 10:47:39 -0400124import libcore.net.event.NetworkEventDispatcher;
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -0800125import dalvik.system.CloseGuard;
Jeff Hao1b012d32014-08-20 10:35:34 -0700126import dalvik.system.VMDebug;
Mathieu Chartier1e370902013-07-18 10:58:01 -0700127import dalvik.system.VMRuntime;
Bob Leee5408332009-09-04 18:31:17 -0700128
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700129final class RemoteServiceException extends AndroidRuntimeException {
130 public RemoteServiceException(String msg) {
131 super(msg);
132 }
133}
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135/**
136 * This manages the execution of the main thread in an
137 * application process, scheduling and executing activities,
138 * broadcasts, and other operations on it as the activity
139 * manager requests.
140 *
141 * {@hide}
142 */
143public final class ActivityThread {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700144 /** @hide */
145 public static final String TAG = "ActivityThread";
Jim Miller0b2a6d02010-07-13 18:01:29 -0700146 private static final android.graphics.Bitmap.Config THUMBNAIL_FORMAT = Bitmap.Config.RGB_565;
Joe Onorato43a17652011-04-06 19:22:23 -0700147 static final boolean localLOGV = false;
Dianne Hackborn287952c2010-09-22 22:34:31 -0700148 static final boolean DEBUG_MESSAGES = false;
Dianne Hackborne829fef2010-10-26 17:44:01 -0700149 /** @hide */
150 public static final boolean DEBUG_BROADCAST = false;
Chris Tate8a7dc172009-03-24 20:11:42 -0700151 private static final boolean DEBUG_RESULTS = false;
Dianne Hackborn755c8bf2012-05-07 15:06:09 -0700152 private static final boolean DEBUG_BACKUP = false;
Craig Mautner88c05892013-06-28 09:47:45 -0700153 public static final boolean DEBUG_CONFIGURATION = false;
Dianne Hackborna0c283e2012-02-09 10:47:01 -0800154 private static final boolean DEBUG_SERVICE = false;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -0700155 private static final boolean DEBUG_MEMORY_TRIM = false;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700156 private static final boolean DEBUG_PROVIDER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private static final long MIN_TIME_BETWEEN_GCS = 5*1000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 private static final int SQLITE_MEM_RELEASED_EVENT_LOG_TAG = 75003;
159 private static final int LOG_ON_PAUSE_CALLED = 30021;
160 private static final int LOG_ON_RESUME_CALLED = 30022;
161
Jeff Browndefd4a62014-03-10 21:24:37 -0700162 private ContextImpl mSystemContext;
Bob Leee5408332009-09-04 18:31:17 -0700163
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700164 static IPackageManager sPackageManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700166 final ApplicationThread mAppThread = new ApplicationThread();
167 final Looper mLooper = Looper.myLooper();
168 final H mH = new H();
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700169 final ArrayMap<IBinder, ActivityClientRecord> mActivities
170 = new ArrayMap<IBinder, ActivityClientRecord>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700171 // List of new activities (via ActivityRecord.nextIdle) that should
172 // be reported when next we idle.
173 ActivityClientRecord mNewActivities = null;
174 // Number of activities that are currently visible on-screen.
175 int mNumVisibleActivities = 0;
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700176 final ArrayMap<IBinder, Service> mServices
177 = new ArrayMap<IBinder, Service>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700178 AppBindData mBoundApplication;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700179 Profiler mProfiler;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700180 int mCurDefaultDisplayDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700181 boolean mDensityCompatMode;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700182 Configuration mConfiguration;
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700183 Configuration mCompatConfiguration;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700184 Application mInitialApplication;
185 final ArrayList<Application> mAllApplications
186 = new ArrayList<Application>();
187 // set of instantiated backup agents, keyed by package name
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700188 final ArrayMap<String, BackupAgent> mBackupAgents = new ArrayMap<String, BackupAgent>();
Jeff Sharkey66a017b2013-01-17 18:18:22 -0800189 /** Reference to singleton {@link ActivityThread} */
190 private static ActivityThread sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700191 Instrumentation mInstrumentation;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700192 String mInstrumentationPackageName = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700193 String mInstrumentationAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700194 String[] mInstrumentationSplitAppDirs = null;
195 String mInstrumentationLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700196 String mInstrumentedAppDir = null;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -0700197 String[] mInstrumentedSplitAppDirs = null;
198 String mInstrumentedLibDir = null;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700199 boolean mSystemThread = false;
200 boolean mJitEnabled = false;
Dianne Hackborn89ad4562014-08-24 16:45:38 -0700201 boolean mSomeActivitiesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700203 // These can be accessed by multiple threads; mPackages is the lock.
204 // XXX For now we keep around information about all packages we have
205 // seen, not removing entries from this map.
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800206 // NOTE: The activity and window managers need to call in to
Dianne Hackborn2f0b1752011-05-31 17:59:49 -0700207 // ActivityThread to do things like update resource configurations,
Dianne Hackborn1fbee792011-11-30 11:29:58 -0800208 // which means this lock gets held while the activity and window managers
209 // holds their own lock. Thus you MUST NEVER call back into the activity manager
210 // or window manager or anything that depends on them while holding this lock.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700211 final ArrayMap<String, WeakReference<LoadedApk>> mPackages
212 = new ArrayMap<String, WeakReference<LoadedApk>>();
213 final ArrayMap<String, WeakReference<LoadedApk>> mResourcePackages
214 = new ArrayMap<String, WeakReference<LoadedApk>>();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700215 final ArrayList<ActivityClientRecord> mRelaunchingActivities
216 = new ArrayList<ActivityClientRecord>();
217 Configuration mPendingConfiguration = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218
Craig Mautner88c05892013-06-28 09:47:45 -0700219 private final ResourcesManager mResourcesManager;
220
Jeff Sharkey6d515712012-09-20 16:06:08 -0700221 private static final class ProviderKey {
222 final String authority;
223 final int userId;
224
225 public ProviderKey(String authority, int userId) {
226 this.authority = authority;
227 this.userId = userId;
228 }
229
230 @Override
231 public boolean equals(Object o) {
232 if (o instanceof ProviderKey) {
233 final ProviderKey other = (ProviderKey) o;
Kenny Roote6585b32013-12-13 12:00:26 -0800234 return Objects.equals(authority, other.authority) && userId == other.userId;
Jeff Sharkey6d515712012-09-20 16:06:08 -0700235 }
236 return false;
237 }
238
239 @Override
240 public int hashCode() {
241 return ((authority != null) ? authority.hashCode() : 0) ^ userId;
242 }
243 }
244
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700245 // The lock of mProviderMap protects the following variables.
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700246 final ArrayMap<ProviderKey, ProviderClientRecord> mProviderMap
247 = new ArrayMap<ProviderKey, ProviderClientRecord>();
248 final ArrayMap<IBinder, ProviderRefCount> mProviderRefCountMap
249 = new ArrayMap<IBinder, ProviderRefCount>();
250 final ArrayMap<IBinder, ProviderClientRecord> mLocalProviders
251 = new ArrayMap<IBinder, ProviderClientRecord>();
252 final ArrayMap<ComponentName, ProviderClientRecord> mLocalProvidersByName
253 = new ArrayMap<ComponentName, ProviderClientRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Dianne Hackbornadd005c2013-07-17 18:43:12 -0700255 final ArrayMap<Activity, ArrayList<OnActivityPausedListener>> mOnPauseListeners
256 = new ArrayMap<Activity, ArrayList<OnActivityPausedListener>>();
Jeff Hamilton52d32032011-01-08 15:31:26 -0600257
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700258 final GcIdler mGcIdler = new GcIdler();
259 boolean mGcIdlerScheduled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -0700261 static Handler sMainThreadHandler; // set once in main()
262
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800263 Bundle mCoreSettings = null;
264
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400265 static final class ActivityClientRecord {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 IBinder token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700267 int ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 Intent intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700269 IVoiceInteractor voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -0700271 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 Activity activity;
273 Window window;
274 Activity parent;
275 String embeddedID;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700276 Activity.NonConfigurationInstances lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 boolean paused;
278 boolean stopped;
279 boolean hideForNow;
280 Configuration newConfig;
Dianne Hackborne88846e2009-09-30 21:34:25 -0700281 Configuration createdConfig;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700282 ActivityClientRecord nextIdle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283
Jeff Hao1b012d32014-08-20 10:35:34 -0700284 ProfilerInfo profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 ActivityInfo activityInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400287 CompatibilityInfo compatInfo;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700288 LoadedApk packageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
290 List<ResultInfo> pendingResults;
291 List<Intent> pendingIntents;
292
293 boolean startsNotResumed;
294 boolean isForward;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800295 int pendingConfigChanges;
296 boolean onlyLocalRequest;
297
298 View mPendingRemoveWindow;
299 WindowManager mPendingRemoveWindowManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700301 ActivityClientRecord() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 parent = null;
303 embeddedID = null;
304 paused = false;
305 stopped = false;
306 hideForNow = false;
307 nextIdle = null;
308 }
309
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800310 public boolean isPreHoneycomb() {
311 if (activity != null) {
312 return activity.getApplicationInfo().targetSdkVersion
313 < android.os.Build.VERSION_CODES.HONEYCOMB;
314 }
315 return false;
316 }
317
Craig Mautnera0026042014-04-23 11:45:37 -0700318 public boolean isPersistable() {
Craig Mautner43e52ed2014-06-16 17:18:52 -0700319 return activityInfo.persistableMode == ActivityInfo.PERSIST_ACROSS_REBOOTS;
Craig Mautnera0026042014-04-23 11:45:37 -0700320 }
321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 public String toString() {
Dianne Hackbornb61a0262012-05-14 17:19:18 -0700323 ComponentName componentName = intent != null ? intent.getComponent() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 return "ActivityRecord{"
325 + Integer.toHexString(System.identityHashCode(this))
326 + " token=" + token + " " + (componentName == null
327 ? "no component name" : componentName.toShortString())
328 + "}";
329 }
330 }
331
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700332 final class ProviderClientRecord {
333 final String[] mNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 final IContentProvider mProvider;
335 final ContentProvider mLocalProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700336 final IActivityManager.ContentProviderHolder mHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700338 ProviderClientRecord(String[] names, IContentProvider provider,
339 ContentProvider localProvider,
340 IActivityManager.ContentProviderHolder holder) {
341 mNames = names;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 mProvider = provider;
343 mLocalProvider = localProvider;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700344 mHolder = holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 }
346 }
347
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400348 static final class NewIntentData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 List<Intent> intents;
350 IBinder token;
351 public String toString() {
352 return "NewIntentData{intents=" + intents + " token=" + token + "}";
353 }
354 }
355
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400356 static final class ReceiverData extends BroadcastReceiver.PendingResult {
Dianne Hackborne829fef2010-10-26 17:44:01 -0700357 public ReceiverData(Intent intent, int resultCode, String resultData, Bundle resultExtras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700358 boolean ordered, boolean sticky, IBinder token, int sendingUser) {
359 super(resultCode, resultData, resultExtras, TYPE_COMPONENT, ordered, sticky,
riddle_hsu1f5ac4d2015-01-03 15:38:21 +0800360 token, sendingUser, intent.getFlags());
Dianne Hackborne829fef2010-10-26 17:44:01 -0700361 this.intent = intent;
362 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 Intent intent;
365 ActivityInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400366 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 public String toString() {
368 return "ReceiverData{intent=" + intent + " packageName=" +
Dianne Hackborne829fef2010-10-26 17:44:01 -0700369 info.packageName + " resultCode=" + getResultCode()
370 + " resultData=" + getResultData() + " resultExtras="
371 + getResultExtras(false) + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373 }
374
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400375 static final class CreateBackupAgentData {
Christopher Tate181fafa2009-05-14 11:12:14 -0700376 ApplicationInfo appInfo;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400377 CompatibilityInfo compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700378 int backupMode;
379 public String toString() {
380 return "CreateBackupAgentData{appInfo=" + appInfo
381 + " backupAgent=" + appInfo.backupAgentName
382 + " mode=" + backupMode + "}";
383 }
384 }
Bob Leee5408332009-09-04 18:31:17 -0700385
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400386 static final class CreateServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 IBinder token;
388 ServiceInfo info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400389 CompatibilityInfo compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 Intent intent;
391 public String toString() {
392 return "CreateServiceData{token=" + token + " className="
393 + info.name + " packageName=" + info.packageName
394 + " intent=" + intent + "}";
395 }
396 }
397
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400398 static final class BindServiceData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 IBinder token;
400 Intent intent;
401 boolean rebind;
402 public String toString() {
403 return "BindServiceData{token=" + token + " intent=" + intent + "}";
404 }
405 }
406
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400407 static final class ServiceArgsData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 IBinder token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700409 boolean taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 int startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700411 int flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 Intent args;
413 public String toString() {
414 return "ServiceArgsData{token=" + token + " startId=" + startId
415 + " args=" + args + "}";
416 }
417 }
418
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400419 static final class AppBindData {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700420 LoadedApk info;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 String processName;
422 ApplicationInfo appInfo;
423 List<ProviderInfo> providers;
424 ComponentName instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 Bundle instrumentationArgs;
426 IInstrumentationWatcher instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800427 IUiAutomationConnection instrumentationUiAutomationConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 int debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800429 boolean enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700430 boolean restrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700431 boolean persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 Configuration config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400433 CompatibilityInfo compatInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700434
435 /** Initial values for {@link Profiler}. */
Jeff Hao1b012d32014-08-20 10:35:34 -0700436 ProfilerInfo initProfilerInfo;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 public String toString() {
439 return "AppBindData{appInfo=" + appInfo + "}";
440 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700441 }
442
443 static final class Profiler {
444 String profileFile;
445 ParcelFileDescriptor profileFd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700446 int samplingInterval;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -0700447 boolean autoStopProfiler;
448 boolean profiling;
449 boolean handlingProfiling;
Jeff Hao1b012d32014-08-20 10:35:34 -0700450 public void setProfiler(ProfilerInfo profilerInfo) {
451 ParcelFileDescriptor fd = profilerInfo.profileFd;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700452 if (profiling) {
453 if (fd != null) {
454 try {
455 fd.close();
456 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700457 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700458 }
459 }
460 return;
461 }
462 if (profileFd != null) {
463 try {
464 profileFd.close();
465 } catch (IOException e) {
Romain Guya998dff2012-03-23 18:58:36 -0700466 // Ignore
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700467 }
468 }
Jeff Hao1b012d32014-08-20 10:35:34 -0700469 profileFile = profilerInfo.profileFile;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700470 profileFd = fd;
Jeff Hao1b012d32014-08-20 10:35:34 -0700471 samplingInterval = profilerInfo.samplingInterval;
472 autoStopProfiler = profilerInfo.autoStopProfiler;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700473 }
474 public void startProfiling() {
475 if (profileFd == null || profiling) {
476 return;
477 }
478 try {
Jeff Hao1b012d32014-08-20 10:35:34 -0700479 VMDebug.startMethodTracing(profileFile, profileFd.getFileDescriptor(),
480 8 * 1024 * 1024, 0, samplingInterval != 0, samplingInterval);
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700481 profiling = true;
482 } catch (RuntimeException e) {
483 Slog.w(TAG, "Profiling failed on path " + profileFile);
484 try {
485 profileFd.close();
486 profileFd = null;
487 } catch (IOException e2) {
488 Slog.w(TAG, "Failure closing profile fd", e2);
489 }
490 }
491 }
492 public void stopProfiling() {
493 if (profiling) {
494 profiling = false;
495 Debug.stopMethodTracing();
496 if (profileFd != null) {
497 try {
498 profileFd.close();
499 } catch (IOException e) {
500 }
501 }
502 profileFd = null;
503 profileFile = null;
504 }
505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 }
507
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400508 static final class DumpComponentInfo {
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700509 ParcelFileDescriptor fd;
Dianne Hackborn625ac272010-09-17 18:29:22 -0700510 IBinder token;
Dianne Hackborn30d71892010-12-11 10:37:55 -0800511 String prefix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512 String[] args;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 }
514
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400515 static final class ResultData {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 IBinder token;
517 List<ResultInfo> results;
518 public String toString() {
519 return "ResultData{token=" + token + " results" + results + "}";
520 }
521 }
522
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400523 static final class ContextCleanupInfo {
Dianne Hackborn21556372010-02-04 16:34:40 -0800524 ContextImpl context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 String what;
526 String who;
527 }
528
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400529 static final class DumpHeapData {
Andy McFadden824c5102010-07-09 16:26:57 -0700530 String path;
531 ParcelFileDescriptor fd;
532 }
533
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400534 static final class UpdateCompatibilityData {
535 String pkg;
536 CompatibilityInfo info;
537 }
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800538
Adam Skorydfc7fd72013-08-05 19:23:41 -0700539 static final class RequestAssistContextExtras {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -0800540 IBinder activityToken;
541 IBinder requestToken;
542 int requestType;
543 }
Anwar Ghuloum3a8ce1b2013-04-26 16:18:28 -0700544
Romain Guy65b345f2011-07-27 18:51:50 -0700545 private native void dumpGraphicsInfo(FileDescriptor fd);
Chet Haase9c1e23b2011-03-24 10:51:31 -0700546
Romain Guy65b345f2011-07-27 18:51:50 -0700547 private class ApplicationThread extends ApplicationThreadNative {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700548 private static final String ONE_COUNT_COLUMN = "%21s %8d";
549 private static final String TWO_COUNT_COLUMNS = "%21s %8d %21s %8d";
Vasu Nori3c7131f2010-09-21 14:36:57 -0700550 private static final String DB_INFO_FORMAT = " %8s %8s %14s %14s %s";
Bob Leee5408332009-09-04 18:31:17 -0700551
Dianne Hackborna413dc02013-07-12 12:02:55 -0700552 private int mLastProcessState = -1;
553
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700554 private void updatePendingConfiguration(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -0700555 synchronized (mResourcesManager) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700556 if (mPendingConfiguration == null ||
557 mPendingConfiguration.isOtherSeqNewer(config)) {
558 mPendingConfiguration = config;
559 }
560 }
561 }
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 public final void schedulePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700564 boolean userLeaving, int configChanges, boolean dontReport) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700565 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 finished ? H.PAUSE_ACTIVITY_FINISHING : H.PAUSE_ACTIVITY,
567 token,
Dianne Hackborna4e102e2014-09-04 22:52:27 -0700568 (userLeaving ? 1 : 0) | (dontReport ? 2 : 0),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 configChanges);
570 }
571
572 public final void scheduleStopActivity(IBinder token, boolean showWindow,
573 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700574 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 showWindow ? H.STOP_ACTIVITY_SHOW : H.STOP_ACTIVITY_HIDE,
576 token, 0, configChanges);
577 }
578
579 public final void scheduleWindowVisibility(IBinder token, boolean showWindow) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700580 sendMessage(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 showWindow ? H.SHOW_WINDOW : H.HIDE_WINDOW,
582 token);
583 }
584
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800585 public final void scheduleSleeping(IBinder token, boolean sleeping) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700586 sendMessage(H.SLEEPING, token, sleeping ? 1 : 0);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -0800587 }
588
Dianne Hackborna413dc02013-07-12 12:02:55 -0700589 public final void scheduleResumeActivity(IBinder token, int processState,
Adam Powellcfbe9be2013-11-06 14:58:58 -0800590 boolean isForward, Bundle resumeArgs) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700591 updateProcessState(processState, false);
Craig Mautner233ceee2014-05-09 17:05:11 -0700592 sendMessage(H.RESUME_ACTIVITY, token, isForward ? 1 : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 }
594
595 public final void scheduleSendResult(IBinder token, List<ResultInfo> results) {
596 ResultData res = new ResultData();
597 res.token = token;
598 res.results = results;
Jeff Brown9ef09972013-10-15 20:49:59 -0700599 sendMessage(H.SEND_RESULT, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
602 // we use token to identify this activity without having to send the
603 // activity itself back to the activity manager. (matters more with ipc)
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700604 public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700605 ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
Craig Mautnera0026042014-04-23 11:45:37 -0700606 IVoiceInteractor voiceInteractor, int procState, Bundle state,
607 PersistableBundle persistentState, List<ResultInfo> pendingResults,
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700608 List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
Jeff Hao1b012d32014-08-20 10:35:34 -0700609 ProfilerInfo profilerInfo) {
Dianne Hackborna413dc02013-07-12 12:02:55 -0700610
611 updateProcessState(procState, false);
612
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -0700613 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614
615 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -0700616 r.ident = ident;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 r.intent = intent;
Dianne Hackborn91097de2014-04-04 18:02:06 -0700618 r.voiceInteractor = voiceInteractor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 r.activityInfo = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400620 r.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 r.state = state;
Craig Mautnera0026042014-04-23 11:45:37 -0700622 r.persistentState = persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623
624 r.pendingResults = pendingResults;
625 r.pendingIntents = pendingNewIntents;
626
627 r.startsNotResumed = notResumed;
628 r.isForward = isForward;
629
Jeff Hao1b012d32014-08-20 10:35:34 -0700630 r.profilerInfo = profilerInfo;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -0700631
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700632 updatePendingConfiguration(curConfig);
633
Jeff Brown9ef09972013-10-15 20:49:59 -0700634 sendMessage(H.LAUNCH_ACTIVITY, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636
637 public final void scheduleRelaunchActivity(IBinder token,
638 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
Dianne Hackborn871ecdc2009-12-11 15:24:33 -0800639 int configChanges, boolean notResumed, Configuration config) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -0800640 requestRelaunchActivity(token, pendingResults, pendingNewIntents,
641 configChanges, notResumed, config, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 }
643
644 public final void scheduleNewIntent(List<Intent> intents, IBinder token) {
645 NewIntentData data = new NewIntentData();
646 data.intents = intents;
647 data.token = token;
648
Jeff Brown9ef09972013-10-15 20:49:59 -0700649 sendMessage(H.NEW_INTENT, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 }
651
652 public final void scheduleDestroyActivity(IBinder token, boolean finishing,
653 int configChanges) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700654 sendMessage(H.DESTROY_ACTIVITY, token, finishing ? 1 : 0,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 configChanges);
656 }
657
658 public final void scheduleReceiver(Intent intent, ActivityInfo info,
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400659 CompatibilityInfo compatInfo, int resultCode, String data, Bundle extras,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700660 boolean sync, int sendingUser, int processState) {
661 updateProcessState(processState, false);
Dianne Hackborne829fef2010-10-26 17:44:01 -0700662 ReceiverData r = new ReceiverData(intent, resultCode, data, extras,
Dianne Hackborn20e80982012-08-31 19:00:44 -0700663 sync, false, mAppThread.asBinder(), sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 r.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400665 r.compatInfo = compatInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700666 sendMessage(H.RECEIVER, r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 }
668
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400669 public final void scheduleCreateBackupAgent(ApplicationInfo app,
670 CompatibilityInfo compatInfo, int backupMode) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700671 CreateBackupAgentData d = new CreateBackupAgentData();
672 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400673 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700674 d.backupMode = backupMode;
675
Jeff Brown9ef09972013-10-15 20:49:59 -0700676 sendMessage(H.CREATE_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700677 }
678
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400679 public final void scheduleDestroyBackupAgent(ApplicationInfo app,
680 CompatibilityInfo compatInfo) {
Christopher Tate181fafa2009-05-14 11:12:14 -0700681 CreateBackupAgentData d = new CreateBackupAgentData();
682 d.appInfo = app;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400683 d.compatInfo = compatInfo;
Christopher Tate181fafa2009-05-14 11:12:14 -0700684
Jeff Brown9ef09972013-10-15 20:49:59 -0700685 sendMessage(H.DESTROY_BACKUP_AGENT, d);
Christopher Tate181fafa2009-05-14 11:12:14 -0700686 }
687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 public final void scheduleCreateService(IBinder token,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700689 ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
690 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 CreateServiceData s = new CreateServiceData();
692 s.token = token;
693 s.info = info;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400694 s.compatInfo = compatInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695
Jeff Brown9ef09972013-10-15 20:49:59 -0700696 sendMessage(H.CREATE_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
698
699 public final void scheduleBindService(IBinder token, Intent intent,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700700 boolean rebind, int processState) {
701 updateProcessState(processState, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 BindServiceData s = new BindServiceData();
703 s.token = token;
704 s.intent = intent;
705 s.rebind = rebind;
706
Amith Yamasani742a6712011-05-04 14:49:28 -0700707 if (DEBUG_SERVICE)
708 Slog.v(TAG, "scheduleBindService token=" + token + " intent=" + intent + " uid="
709 + Binder.getCallingUid() + " pid=" + Binder.getCallingPid());
Jeff Brown9ef09972013-10-15 20:49:59 -0700710 sendMessage(H.BIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
713 public final void scheduleUnbindService(IBinder token, Intent intent) {
714 BindServiceData s = new BindServiceData();
715 s.token = token;
716 s.intent = intent;
717
Jeff Brown9ef09972013-10-15 20:49:59 -0700718 sendMessage(H.UNBIND_SERVICE, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 }
720
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700721 public final void scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId,
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700722 int flags ,Intent args) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 ServiceArgsData s = new ServiceArgsData();
724 s.token = token;
Dianne Hackborn0c5001d2011-04-12 18:16:08 -0700725 s.taskRemoved = taskRemoved;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 s.startId = startId;
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -0700727 s.flags = flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 s.args = args;
729
Jeff Brown9ef09972013-10-15 20:49:59 -0700730 sendMessage(H.SERVICE_ARGS, s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 }
732
733 public final void scheduleStopService(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700734 sendMessage(H.STOP_SERVICE, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735 }
736
Jeff Hao1b012d32014-08-20 10:35:34 -0700737 public final void bindApplication(String processName, ApplicationInfo appInfo,
738 List<ProviderInfo> providers, ComponentName instrumentationName,
739 ProfilerInfo profilerInfo, Bundle instrumentationArgs,
740 IInstrumentationWatcher instrumentationWatcher,
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800741 IUiAutomationConnection instrumentationUiConnection, int debugMode,
742 boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,
743 Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,
744 Bundle coreSettings) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745
746 if (services != null) {
747 // Setup the service cache in the ServiceManager
748 ServiceManager.initServiceCache(services);
749 }
750
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -0800751 setCoreSettings(coreSettings);
752
Dave Allison0efbd9a2014-01-30 14:19:51 -0800753 /*
754 * Two possible indications that this package could be
755 * sharing its runtime with other packages:
756 *
757 * 1.) the sharedUserId attribute is set in the manifest,
758 * indicating a request to share a VM with other
759 * packages with the same sharedUserId.
760 *
761 * 2.) the application element of the manifest has an
762 * attribute specifying a non-default process name,
763 * indicating the desire to run in another packages VM.
764 *
765 * If sharing is enabled we do not have a unique application
766 * in a process and therefore cannot rely on the package
767 * name inside the runtime.
768 */
769 IPackageManager pm = getPackageManager();
770 android.content.pm.PackageInfo pi = null;
771 try {
772 pi = pm.getPackageInfo(appInfo.packageName, 0, UserHandle.myUserId());
773 } catch (RemoteException e) {
774 }
775 if (pi != null) {
776 boolean sharedUserIdSet = (pi.sharedUserId != null);
777 boolean processNameNotDefault =
778 (pi.applicationInfo != null &&
779 !appInfo.packageName.equals(pi.applicationInfo.processName));
780 boolean sharable = (sharedUserIdSet || processNameNotDefault);
781
782 // Tell the VMRuntime about the application, unless it is shared
783 // inside a process.
784 if (!sharable) {
785 VMRuntime.registerAppInfo(appInfo.packageName, appInfo.dataDir,
786 appInfo.processName);
787 }
788 }
Dave Allison24bafbc2013-11-13 17:15:50 -0800789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 AppBindData data = new AppBindData();
791 data.processName = processName;
792 data.appInfo = appInfo;
793 data.providers = providers;
794 data.instrumentationName = instrumentationName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 data.instrumentationArgs = instrumentationArgs;
796 data.instrumentationWatcher = instrumentationWatcher;
Svetoslav Ganov80943d82013-01-02 10:25:37 -0800797 data.instrumentationUiAutomationConnection = instrumentationUiConnection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 data.debugMode = debugMode;
Siva Velusamy92a8b222012-03-09 16:24:04 -0800799 data.enableOpenGlTrace = enableOpenGlTrace;
Christopher Tate181fafa2009-05-14 11:12:14 -0700800 data.restrictedBackupMode = isRestrictedBackupMode;
Dianne Hackborn5d927c22011-09-02 12:22:18 -0700801 data.persistent = persistent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 data.config = config;
Dianne Hackborne2515ee2011-04-27 18:52:56 -0400803 data.compatInfo = compatInfo;
Jeff Hao1b012d32014-08-20 10:35:34 -0700804 data.initProfilerInfo = profilerInfo;
Jeff Brown9ef09972013-10-15 20:49:59 -0700805 sendMessage(H.BIND_APPLICATION, data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 }
807
808 public final void scheduleExit() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700809 sendMessage(H.EXIT_APPLICATION, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 }
811
Christopher Tate5e1ab332009-09-01 20:32:49 -0700812 public final void scheduleSuicide() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700813 sendMessage(H.SUICIDE, null);
Christopher Tate5e1ab332009-09-01 20:32:49 -0700814 }
815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 public void scheduleConfigurationChanged(Configuration config) {
Dianne Hackborn58f42a52011-10-10 13:46:34 -0700817 updatePendingConfiguration(config);
Jeff Brown9ef09972013-10-15 20:49:59 -0700818 sendMessage(H.CONFIGURATION_CHANGED, config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820
821 public void updateTimeZone() {
822 TimeZone.setDefault(null);
823 }
824
Robert Greenwalt03595d02010-11-02 14:08:23 -0700825 public void clearDnsCache() {
826 // a non-standard API to get this to libcore
827 InetAddress.clearDnsCache();
Paul Jensene401d172014-09-12 10:47:39 -0400828 // Allow libcore to perform the necessary actions as it sees fit upon a network
829 // configuration change.
830 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
Robert Greenwalt03595d02010-11-02 14:08:23 -0700831 }
832
Jason Monk83520b92014-05-09 15:16:06 -0400833 public void setHttpProxy(String host, String port, String exclList, Uri pacFileUrl) {
Jason Monk602b2322013-07-03 17:04:33 -0400834 Proxy.setHttpProxySystemProperty(host, port, exclList, pacFileUrl);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700835 }
836
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 public void processInBackground() {
838 mH.removeMessages(H.GC_WHEN_IDLE);
839 mH.sendMessage(mH.obtainMessage(H.GC_WHEN_IDLE));
840 }
841
842 public void dumpService(FileDescriptor fd, IBinder servicetoken, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700843 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700844 try {
845 data.fd = ParcelFileDescriptor.dup(fd);
846 data.token = servicetoken;
847 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700848 sendMessage(H.DUMP_SERVICE, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700849 } catch (IOException e) {
850 Slog.w(TAG, "dumpService failed", e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 }
852 }
853
854 // This function exists to make sure all receiver dispatching is
855 // correctly ordered, since these are one-way calls and the binder driver
856 // applies transaction ordering per object for such calls.
857 public void scheduleRegisteredReceiver(IIntentReceiver receiver, Intent intent,
Dianne Hackborn68d881c2009-10-05 13:58:17 -0700858 int resultCode, String dataStr, Bundle extras, boolean ordered,
Dianne Hackborna413dc02013-07-12 12:02:55 -0700859 boolean sticky, int sendingUser, int processState) throws RemoteException {
860 updateProcessState(processState, false);
Dianne Hackborn20e80982012-08-31 19:00:44 -0700861 receiver.performReceive(intent, resultCode, dataStr, extras, ordered,
862 sticky, sendingUser);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 }
Bob Leee5408332009-09-04 18:31:17 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 public void scheduleLowMemory() {
Jeff Brown9ef09972013-10-15 20:49:59 -0700866 sendMessage(H.LOW_MEMORY, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
868
869 public void scheduleActivityConfigurationChanged(IBinder token) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700870 sendMessage(H.ACTIVITY_CONFIGURATION_CHANGED, token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 }
872
Jeff Hao1b012d32014-08-20 10:35:34 -0700873 public void profilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
874 sendMessage(H.PROFILER_CONTROL, profilerInfo, start ? 1 : 0, profileType);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -0800875 }
876
Andy McFadden824c5102010-07-09 16:26:57 -0700877 public void dumpHeap(boolean managed, String path, ParcelFileDescriptor fd) {
878 DumpHeapData dhd = new DumpHeapData();
879 dhd.path = path;
880 dhd.fd = fd;
Jeff Brown9ef09972013-10-15 20:49:59 -0700881 sendMessage(H.DUMP_HEAP, dhd, managed ? 1 : 0, 0, true /*async*/);
Andy McFadden824c5102010-07-09 16:26:57 -0700882 }
883
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700884 public void setSchedulingGroup(int group) {
885 // Note: do this immediately, since going into the foreground
886 // should happen regardless of what pending work we have to do
887 // and the activity manager will wait for us to report back that
888 // we are done before sending us to the background.
889 try {
890 Process.setProcessGroup(Process.myPid(), group);
891 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -0800892 Slog.w(TAG, "Failed setting process group to " + group, e);
Dianne Hackborn06de2ea2009-05-21 12:56:43 -0700893 }
894 }
Bob Leee5408332009-09-04 18:31:17 -0700895
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700896 public void dispatchPackageBroadcast(int cmd, String[] packages) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700897 sendMessage(H.DISPATCH_PACKAGE_BROADCAST, packages, cmd);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -0700898 }
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700899
900 public void scheduleCrash(String msg) {
Jeff Brown9ef09972013-10-15 20:49:59 -0700901 sendMessage(H.SCHEDULE_CRASH, msg);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700902 }
Dianne Hackborn625ac272010-09-17 18:29:22 -0700903
Dianne Hackborn30d71892010-12-11 10:37:55 -0800904 public void dumpActivity(FileDescriptor fd, IBinder activitytoken,
905 String prefix, String[] args) {
Dianne Hackborn625ac272010-09-17 18:29:22 -0700906 DumpComponentInfo data = new DumpComponentInfo();
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700907 try {
908 data.fd = ParcelFileDescriptor.dup(fd);
909 data.token = activitytoken;
910 data.prefix = prefix;
911 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700912 sendMessage(H.DUMP_ACTIVITY, data, 0, 0, true /*async*/);
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700913 } catch (IOException e) {
914 Slog.w(TAG, "dumpActivity failed", e);
Dianne Hackborn625ac272010-09-17 18:29:22 -0700915 }
916 }
Chet Haase9c1e23b2011-03-24 10:51:31 -0700917
Marco Nelissen18cb2872011-11-15 11:19:53 -0800918 public void dumpProvider(FileDescriptor fd, IBinder providertoken,
919 String[] args) {
920 DumpComponentInfo data = new DumpComponentInfo();
921 try {
922 data.fd = ParcelFileDescriptor.dup(fd);
923 data.token = providertoken;
924 data.args = args;
Jeff Brown9ef09972013-10-15 20:49:59 -0700925 sendMessage(H.DUMP_PROVIDER, data, 0, 0, true /*async*/);
Marco Nelissen18cb2872011-11-15 11:19:53 -0800926 } catch (IOException e) {
927 Slog.w(TAG, "dumpProvider failed", e);
928 }
929 }
930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 @Override
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700932 public void dumpMemInfo(FileDescriptor fd, Debug.MemoryInfo mem, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700933 boolean dumpFullInfo, boolean dumpDalvik, String[] args) {
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700934 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700935 PrintWriter pw = new FastPrintWriter(fout);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700936 try {
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700937 dumpMemInfo(pw, mem, checkin, dumpFullInfo, dumpDalvik);
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700938 } finally {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700939 pw.flush();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700940 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -0700941 }
942
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -0700943 private void dumpMemInfo(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
Dianne Hackborncbd9a522013-09-24 23:10:14 -0700944 boolean dumpFullInfo, boolean dumpDalvik) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 long nativeMax = Debug.getNativeHeapSize() / 1024;
946 long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024;
947 long nativeFree = Debug.getNativeHeapFreeSize() / 1024;
948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 Runtime runtime = Runtime.getRuntime();
950
951 long dalvikMax = runtime.totalMemory() / 1024;
952 long dalvikFree = runtime.freeMemory() / 1024;
953 long dalvikAllocated = dalvikMax - dalvikFree;
954 long viewInstanceCount = ViewDebug.getViewInstanceCount();
Romain Guy65b345f2011-07-27 18:51:50 -0700955 long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
Brian Carlstromc21550a2010-10-05 21:34:06 -0700956 long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
957 long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 int globalAssetCount = AssetManager.getGlobalAssetCount();
959 int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
960 int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
961 int binderProxyObjectCount = Debug.getBinderProxyObjectCount();
962 int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
Brian Carlstromc9d5b312010-10-05 22:23:41 -0700963 long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
Vasu Noric3849202010-03-09 10:47:25 -0800964 SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
Bob Leee5408332009-09-04 18:31:17 -0700965
Dianne Hackborne77187d2013-10-25 16:32:41 -0700966 dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, Process.myPid(),
967 (mBoundApplication != null) ? mBoundApplication.processName : "unknown",
968 nativeMax, nativeAllocated, nativeFree,
969 dalvikMax, dalvikAllocated, dalvikFree);
970
Dianne Hackbornb437e092011-08-05 17:50:29 -0700971 if (checkin) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 // NOTE: if you change anything significant below, also consider changing
973 // ACTIVITY_THREAD_CHECKIN_VERSION.
Anwar Ghuloum3c615062013-05-13 14:18:02 -0700974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 // Object counts
976 pw.print(viewInstanceCount); pw.print(',');
977 pw.print(viewRootInstanceCount); pw.print(',');
978 pw.print(appContextInstanceCount); pw.print(',');
979 pw.print(activityInstanceCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 pw.print(globalAssetCount); pw.print(',');
982 pw.print(globalAssetManagerCount); pw.print(',');
983 pw.print(binderLocalObjectCount); pw.print(',');
984 pw.print(binderProxyObjectCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 pw.print(binderDeathObjectCount); pw.print(',');
987 pw.print(openSslSocketCount); pw.print(',');
Bob Leee5408332009-09-04 18:31:17 -0700988
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 // SQL
Vasu Noric3849202010-03-09 10:47:25 -0800990 pw.print(stats.memoryUsed / 1024); pw.print(',');
Jeff Brown2a293b62012-01-19 14:02:22 -0800991 pw.print(stats.memoryUsed / 1024); pw.print(',');
992 pw.print(stats.pageCacheOverflow / 1024); pw.print(',');
Dianne Hackbornb437e092011-08-05 17:50:29 -0700993 pw.print(stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -0800994 for (int i = 0; i < stats.dbStats.size(); i++) {
995 DbStats dbStats = stats.dbStats.get(i);
Dianne Hackbornb437e092011-08-05 17:50:29 -0700996 pw.print(','); pw.print(dbStats.dbName);
997 pw.print(','); pw.print(dbStats.pageSize);
998 pw.print(','); pw.print(dbStats.dbSize);
999 pw.print(','); pw.print(dbStats.lookaside);
1000 pw.print(','); pw.print(dbStats.cache);
1001 pw.print(','); pw.print(dbStats.cache);
Vasu Noric3849202010-03-09 10:47:25 -08001002 }
Dianne Hackbornb437e092011-08-05 17:50:29 -07001003 pw.println();
Bob Leee5408332009-09-04 18:31:17 -07001004
Dianne Hackborn3bc8f78d2013-09-19 13:34:35 -07001005 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 }
Bob Leee5408332009-09-04 18:31:17 -07001007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 pw.println(" ");
1009 pw.println(" Objects");
Romain Guy65b345f2011-07-27 18:51:50 -07001010 printRow(pw, TWO_COUNT_COLUMNS, "Views:", viewInstanceCount, "ViewRootImpl:",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001011 viewRootInstanceCount);
1012
1013 printRow(pw, TWO_COUNT_COLUMNS, "AppContexts:", appContextInstanceCount,
1014 "Activities:", activityInstanceCount);
1015
1016 printRow(pw, TWO_COUNT_COLUMNS, "Assets:", globalAssetCount,
1017 "AssetManagers:", globalAssetManagerCount);
1018
1019 printRow(pw, TWO_COUNT_COLUMNS, "Local Binders:", binderLocalObjectCount,
1020 "Proxy Binders:", binderProxyObjectCount);
1021 printRow(pw, ONE_COUNT_COLUMN, "Death Recipients:", binderDeathObjectCount);
1022
1023 printRow(pw, ONE_COUNT_COLUMN, "OpenSSL Sockets:", openSslSocketCount);
Bob Leee5408332009-09-04 18:31:17 -07001024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 // SQLite mem info
1026 pw.println(" ");
1027 pw.println(" SQL");
Jeff Brown2a293b62012-01-19 14:02:22 -08001028 printRow(pw, ONE_COUNT_COLUMN, "MEMORY_USED:", stats.memoryUsed / 1024);
1029 printRow(pw, TWO_COUNT_COLUMNS, "PAGECACHE_OVERFLOW:",
1030 stats.pageCacheOverflow / 1024, "MALLOC_SIZE:", stats.largestMemAlloc / 1024);
Vasu Noric3849202010-03-09 10:47:25 -08001031 pw.println(" ");
1032 int N = stats.dbStats.size();
1033 if (N > 0) {
1034 pw.println(" DATABASES");
Vasu Nori3c7131f2010-09-21 14:36:57 -07001035 printRow(pw, " %8s %8s %14s %14s %s", "pgsz", "dbsz", "Lookaside(b)", "cache",
1036 "Dbname");
Vasu Noric3849202010-03-09 10:47:25 -08001037 for (int i = 0; i < N; i++) {
1038 DbStats dbStats = stats.dbStats.get(i);
Vasu Nori3c7131f2010-09-21 14:36:57 -07001039 printRow(pw, DB_INFO_FORMAT,
1040 (dbStats.pageSize > 0) ? String.valueOf(dbStats.pageSize) : " ",
1041 (dbStats.dbSize > 0) ? String.valueOf(dbStats.dbSize) : " ",
1042 (dbStats.lookaside > 0) ? String.valueOf(dbStats.lookaside) : " ",
1043 dbStats.cache, dbStats.dbName);
Vasu Noric3849202010-03-09 10:47:25 -08001044 }
1045 }
Bob Leee5408332009-09-04 18:31:17 -07001046
Dianne Hackborn82e1ee92009-08-11 18:56:41 -07001047 // Asset details.
1048 String assetAlloc = AssetManager.getAssetAllocations();
1049 if (assetAlloc != null) {
1050 pw.println(" ");
1051 pw.println(" Asset Allocations");
1052 pw.print(assetAlloc);
1053 }
Dianne Hackborn0e3328f2011-07-17 13:31:17 -07001054 }
1055
1056 @Override
1057 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
1058 dumpGraphicsInfo(fd);
Jeff Brown98365d72012-08-19 20:30:52 -07001059 WindowManagerGlobal.getInstance().dumpGfxInfo(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001062 private void dumpDatabaseInfo(FileDescriptor fd, String[] args) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07001063 PrintWriter pw = new FastPrintWriter(new FileOutputStream(fd));
Jeff Brown6754ba22011-12-14 20:20:01 -08001064 PrintWriterPrinter printer = new PrintWriterPrinter(pw);
1065 SQLiteDebug.dump(printer, args);
1066 pw.flush();
1067 }
1068
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001069 @Override
riddle_hsu1d6c40a2014-07-31 00:18:00 +08001070 public void dumpDbInfo(final FileDescriptor fd, final String[] args) {
1071 if (mSystemThread) {
1072 // Ensure this invocation is asynchronous to prevent
1073 // writer waiting due to buffer cannot be consumed.
1074 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
1075 @Override
1076 public void run() {
1077 dumpDatabaseInfo(fd, args);
1078 }
1079 });
1080 } else {
1081 dumpDatabaseInfo(fd, args);
1082 }
1083 }
1084
1085 @Override
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001086 public void unstableProviderDied(IBinder provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001087 sendMessage(H.UNSTABLE_PROVIDER_DIED, provider);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001088 }
1089
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001090 @Override
Adam Skorydfc7fd72013-08-05 19:23:41 -07001091 public void requestAssistContextExtras(IBinder activityToken, IBinder requestToken,
Adam Skory7140a252013-09-11 12:04:58 +01001092 int requestType) {
Adam Skorydfc7fd72013-08-05 19:23:41 -07001093 RequestAssistContextExtras cmd = new RequestAssistContextExtras();
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001094 cmd.activityToken = activityToken;
1095 cmd.requestToken = requestToken;
1096 cmd.requestType = requestType;
Jeff Brown9ef09972013-10-15 20:49:59 -07001097 sendMessage(H.REQUEST_ASSIST_CONTEXT_EXTRAS, cmd);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001098 }
1099
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08001100 public void setCoreSettings(Bundle coreSettings) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001101 sendMessage(H.SET_CORE_SETTINGS, coreSettings);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001102 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001103
1104 public void updatePackageCompatibilityInfo(String pkg, CompatibilityInfo info) {
1105 UpdateCompatibilityData ucd = new UpdateCompatibilityData();
1106 ucd.pkg = pkg;
1107 ucd.info = info;
Jeff Brown9ef09972013-10-15 20:49:59 -07001108 sendMessage(H.UPDATE_PACKAGE_COMPATIBILITY_INFO, ucd);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001109 }
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001110
1111 public void scheduleTrimMemory(int level) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001112 sendMessage(H.TRIM_MEMORY, null, level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001113 }
Marco Nelissen18cb2872011-11-15 11:19:53 -08001114
Craig Mautner5eda9b32013-07-02 11:58:16 -07001115 public void scheduleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001116 sendMessage(H.TRANSLUCENT_CONVERSION_COMPLETE, token, drawComplete ? 1 : 0);
Craig Mautner5eda9b32013-07-02 11:58:16 -07001117 }
Dianne Hackborna413dc02013-07-12 12:02:55 -07001118
Craig Mautnereb8abf72014-07-02 15:04:09 -07001119 public void scheduleOnNewActivityOptions(IBinder token, ActivityOptions options) {
1120 sendMessage(H.ON_NEW_ACTIVITY_OPTIONS,
1121 new Pair<IBinder, ActivityOptions>(token, options));
1122 }
1123
Dianne Hackborna413dc02013-07-12 12:02:55 -07001124 public void setProcessState(int state) {
1125 updateProcessState(state, true);
1126 }
1127
1128 public void updateProcessState(int processState, boolean fromIpc) {
1129 synchronized (this) {
1130 if (mLastProcessState != processState) {
1131 mLastProcessState = processState;
Mathieu Chartier1e370902013-07-18 10:58:01 -07001132 // Update Dalvik state based on ActivityManager.PROCESS_STATE_* constants.
1133 final int DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE = 0;
1134 final int DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE = 1;
1135 int dalvikProcessState = DALVIK_PROCESS_STATE_JANK_IMPERCEPTIBLE;
1136 // TODO: Tune this since things like gmail sync are important background but not jank perceptible.
1137 if (processState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND) {
1138 dalvikProcessState = DALVIK_PROCESS_STATE_JANK_PERCEPTIBLE;
1139 }
1140 VMRuntime.getRuntime().updateProcessState(dalvikProcessState);
Dianne Hackborna413dc02013-07-12 12:02:55 -07001141 if (false) {
1142 Slog.i(TAG, "******************* PROCESS STATE CHANGED TO: " + processState
1143 + (fromIpc ? " (from ipc": ""));
1144 }
1145 }
1146 }
1147 }
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001148
1149 @Override
1150 public void scheduleInstallProvider(ProviderInfo provider) {
Jeff Brown9ef09972013-10-15 20:49:59 -07001151 sendMessage(H.INSTALL_PROVIDER, provider);
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001152 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001153
1154 @Override
1155 public final void updateTimePrefs(boolean is24Hour) {
1156 DateFormat.set24HourTimePref(is24Hour);
1157 }
Craig Mautneree2e45a2014-06-27 12:10:03 -07001158
1159 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001160 public void scheduleCancelVisibleBehind(IBinder token) {
1161 sendMessage(H.CANCEL_VISIBLE_BEHIND, token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001162 }
1163
1164 @Override
Jose Lima4b6c6692014-08-12 17:41:12 -07001165 public void scheduleBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
1166 sendMessage(H.BACKGROUND_VISIBLE_BEHIND_CHANGED, token, visible ? 1 : 0);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001167 }
Craig Mautner8746a472014-07-24 15:12:54 -07001168
Jeff Sharkey605eb792014-11-04 13:34:06 -08001169 @Override
Craig Mautner8746a472014-07-24 15:12:54 -07001170 public void scheduleEnterAnimationComplete(IBinder token) {
1171 sendMessage(H.ENTER_ANIMATION_COMPLETE, token);
1172 }
Jeff Sharkey605eb792014-11-04 13:34:06 -08001173
1174 @Override
1175 public void notifyCleartextNetwork(byte[] firstPacket) {
1176 if (StrictMode.vmCleartextNetworkEnabled()) {
1177 StrictMode.onCleartextNetworkDetected(firstPacket);
1178 }
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 }
1181
Romain Guy65b345f2011-07-27 18:51:50 -07001182 private class H extends Handler {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 public static final int LAUNCH_ACTIVITY = 100;
1184 public static final int PAUSE_ACTIVITY = 101;
1185 public static final int PAUSE_ACTIVITY_FINISHING= 102;
1186 public static final int STOP_ACTIVITY_SHOW = 103;
1187 public static final int STOP_ACTIVITY_HIDE = 104;
1188 public static final int SHOW_WINDOW = 105;
1189 public static final int HIDE_WINDOW = 106;
1190 public static final int RESUME_ACTIVITY = 107;
1191 public static final int SEND_RESULT = 108;
Brian Carlstromed7e0072011-03-24 13:27:57 -07001192 public static final int DESTROY_ACTIVITY = 109;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 public static final int BIND_APPLICATION = 110;
1194 public static final int EXIT_APPLICATION = 111;
1195 public static final int NEW_INTENT = 112;
1196 public static final int RECEIVER = 113;
1197 public static final int CREATE_SERVICE = 114;
1198 public static final int SERVICE_ARGS = 115;
1199 public static final int STOP_SERVICE = 116;
Dianne Hackborn09233282014-04-30 11:33:59 -07001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 public static final int CONFIGURATION_CHANGED = 118;
1202 public static final int CLEAN_UP_CONTEXT = 119;
1203 public static final int GC_WHEN_IDLE = 120;
1204 public static final int BIND_SERVICE = 121;
1205 public static final int UNBIND_SERVICE = 122;
1206 public static final int DUMP_SERVICE = 123;
1207 public static final int LOW_MEMORY = 124;
1208 public static final int ACTIVITY_CONFIGURATION_CHANGED = 125;
1209 public static final int RELAUNCH_ACTIVITY = 126;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001210 public static final int PROFILER_CONTROL = 127;
Christopher Tate181fafa2009-05-14 11:12:14 -07001211 public static final int CREATE_BACKUP_AGENT = 128;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001212 public static final int DESTROY_BACKUP_AGENT = 129;
1213 public static final int SUICIDE = 130;
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001214 public static final int REMOVE_PROVIDER = 131;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001215 public static final int ENABLE_JIT = 132;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001216 public static final int DISPATCH_PACKAGE_BROADCAST = 133;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001217 public static final int SCHEDULE_CRASH = 134;
Andy McFadden824c5102010-07-09 16:26:57 -07001218 public static final int DUMP_HEAP = 135;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001219 public static final int DUMP_ACTIVITY = 136;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001220 public static final int SLEEPING = 137;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001221 public static final int SET_CORE_SETTINGS = 138;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001222 public static final int UPDATE_PACKAGE_COMPATIBILITY_INFO = 139;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001223 public static final int TRIM_MEMORY = 140;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001224 public static final int DUMP_PROVIDER = 141;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001225 public static final int UNSTABLE_PROVIDER_DIED = 142;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001226 public static final int REQUEST_ASSIST_CONTEXT_EXTRAS = 143;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001227 public static final int TRANSLUCENT_CONVERSION_COMPLETE = 144;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001228 public static final int INSTALL_PROVIDER = 145;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001229 public static final int ON_NEW_ACTIVITY_OPTIONS = 146;
Jose Lima4b6c6692014-08-12 17:41:12 -07001230 public static final int CANCEL_VISIBLE_BEHIND = 147;
1231 public static final int BACKGROUND_VISIBLE_BEHIND_CHANGED = 148;
Craig Mautner8746a472014-07-24 15:12:54 -07001232 public static final int ENTER_ANIMATION_COMPLETE = 149;
Narayan Kamathccb2a0862013-12-19 14:49:36 +00001233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 String codeToString(int code) {
Dianne Hackborn287952c2010-09-22 22:34:31 -07001235 if (DEBUG_MESSAGES) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 switch (code) {
1237 case LAUNCH_ACTIVITY: return "LAUNCH_ACTIVITY";
1238 case PAUSE_ACTIVITY: return "PAUSE_ACTIVITY";
1239 case PAUSE_ACTIVITY_FINISHING: return "PAUSE_ACTIVITY_FINISHING";
1240 case STOP_ACTIVITY_SHOW: return "STOP_ACTIVITY_SHOW";
1241 case STOP_ACTIVITY_HIDE: return "STOP_ACTIVITY_HIDE";
1242 case SHOW_WINDOW: return "SHOW_WINDOW";
1243 case HIDE_WINDOW: return "HIDE_WINDOW";
1244 case RESUME_ACTIVITY: return "RESUME_ACTIVITY";
1245 case SEND_RESULT: return "SEND_RESULT";
1246 case DESTROY_ACTIVITY: return "DESTROY_ACTIVITY";
1247 case BIND_APPLICATION: return "BIND_APPLICATION";
1248 case EXIT_APPLICATION: return "EXIT_APPLICATION";
1249 case NEW_INTENT: return "NEW_INTENT";
1250 case RECEIVER: return "RECEIVER";
1251 case CREATE_SERVICE: return "CREATE_SERVICE";
1252 case SERVICE_ARGS: return "SERVICE_ARGS";
1253 case STOP_SERVICE: return "STOP_SERVICE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 case CONFIGURATION_CHANGED: return "CONFIGURATION_CHANGED";
1255 case CLEAN_UP_CONTEXT: return "CLEAN_UP_CONTEXT";
1256 case GC_WHEN_IDLE: return "GC_WHEN_IDLE";
1257 case BIND_SERVICE: return "BIND_SERVICE";
1258 case UNBIND_SERVICE: return "UNBIND_SERVICE";
1259 case DUMP_SERVICE: return "DUMP_SERVICE";
1260 case LOW_MEMORY: return "LOW_MEMORY";
1261 case ACTIVITY_CONFIGURATION_CHANGED: return "ACTIVITY_CONFIGURATION_CHANGED";
1262 case RELAUNCH_ACTIVITY: return "RELAUNCH_ACTIVITY";
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001263 case PROFILER_CONTROL: return "PROFILER_CONTROL";
Christopher Tate181fafa2009-05-14 11:12:14 -07001264 case CREATE_BACKUP_AGENT: return "CREATE_BACKUP_AGENT";
1265 case DESTROY_BACKUP_AGENT: return "DESTROY_BACKUP_AGENT";
Christopher Tate5e1ab332009-09-01 20:32:49 -07001266 case SUICIDE: return "SUICIDE";
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001267 case REMOVE_PROVIDER: return "REMOVE_PROVIDER";
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001268 case ENABLE_JIT: return "ENABLE_JIT";
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001269 case DISPATCH_PACKAGE_BROADCAST: return "DISPATCH_PACKAGE_BROADCAST";
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001270 case SCHEDULE_CRASH: return "SCHEDULE_CRASH";
Andy McFadden824c5102010-07-09 16:26:57 -07001271 case DUMP_HEAP: return "DUMP_HEAP";
Dianne Hackborn625ac272010-09-17 18:29:22 -07001272 case DUMP_ACTIVITY: return "DUMP_ACTIVITY";
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001273 case SLEEPING: return "SLEEPING";
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001274 case SET_CORE_SETTINGS: return "SET_CORE_SETTINGS";
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001275 case UPDATE_PACKAGE_COMPATIBILITY_INFO: return "UPDATE_PACKAGE_COMPATIBILITY_INFO";
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001276 case TRIM_MEMORY: return "TRIM_MEMORY";
Marco Nelissen18cb2872011-11-15 11:19:53 -08001277 case DUMP_PROVIDER: return "DUMP_PROVIDER";
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001278 case UNSTABLE_PROVIDER_DIED: return "UNSTABLE_PROVIDER_DIED";
Adam Skorydfc7fd72013-08-05 19:23:41 -07001279 case REQUEST_ASSIST_CONTEXT_EXTRAS: return "REQUEST_ASSIST_CONTEXT_EXTRAS";
Craig Mautner5eda9b32013-07-02 11:58:16 -07001280 case TRANSLUCENT_CONVERSION_COMPLETE: return "TRANSLUCENT_CONVERSION_COMPLETE";
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001281 case INSTALL_PROVIDER: return "INSTALL_PROVIDER";
Craig Mautnereb8abf72014-07-02 15:04:09 -07001282 case ON_NEW_ACTIVITY_OPTIONS: return "ON_NEW_ACTIVITY_OPTIONS";
Jose Lima4b6c6692014-08-12 17:41:12 -07001283 case CANCEL_VISIBLE_BEHIND: return "CANCEL_VISIBLE_BEHIND";
1284 case BACKGROUND_VISIBLE_BEHIND_CHANGED: return "BACKGROUND_VISIBLE_BEHIND_CHANGED";
Craig Mautner8746a472014-07-24 15:12:54 -07001285 case ENTER_ANIMATION_COMPLETE: return "ENTER_ANIMATION_COMPLETE";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 }
1287 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001288 return Integer.toString(code);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 }
1290 public void handleMessage(Message msg) {
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001291 if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 switch (msg.what) {
1293 case LAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001294 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
Adam Powellcfbe9be2013-11-06 14:58:58 -08001295 final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296
1297 r.packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001298 r.activityInfo.applicationInfo, r.compatInfo);
Christopher Tateb70f3df2009-04-07 16:07:59 -07001299 handleLaunchActivity(r, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001300 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 } break;
1302 case RELAUNCH_ACTIVITY: {
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001303 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityRestart");
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001304 ActivityClientRecord r = (ActivityClientRecord)msg.obj;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08001305 handleRelaunchActivity(r);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001306 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 } break;
1308 case PAUSE_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001309 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001310 handlePauseActivity((IBinder)msg.obj, false, (msg.arg1&1) != 0, msg.arg2,
1311 (msg.arg1&2) != 0);
Bob Leee5408332009-09-04 18:31:17 -07001312 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001313 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 break;
1315 case PAUSE_ACTIVITY_FINISHING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001316 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
Dianne Hackborna4e102e2014-09-04 22:52:27 -07001317 handlePauseActivity((IBinder)msg.obj, true, (msg.arg1&1) != 0, msg.arg2,
1318 (msg.arg1&1) != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001319 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 break;
1321 case STOP_ACTIVITY_SHOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001322 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001323 handleStopActivity((IBinder)msg.obj, true, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001324 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 break;
1326 case STOP_ACTIVITY_HIDE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001327 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 handleStopActivity((IBinder)msg.obj, false, msg.arg2);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001329 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 break;
1331 case SHOW_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001332 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityShowWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 handleWindowVisibility((IBinder)msg.obj, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001334 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 break;
1336 case HIDE_WINDOW:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001337 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityHideWindow");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 handleWindowVisibility((IBinder)msg.obj, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001339 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 break;
1341 case RESUME_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001342 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
Craig Mautner233ceee2014-05-09 17:05:11 -07001343 handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001344 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 break;
1346 case SEND_RESULT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001347 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 handleSendResult((ResultData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001349 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 break;
1351 case DESTROY_ACTIVITY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001352 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 handleDestroyActivity((IBinder)msg.obj, msg.arg1 != 0,
1354 msg.arg2, false);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001355 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 break;
1357 case BIND_APPLICATION:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001358 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 AppBindData data = (AppBindData)msg.obj;
1360 handleBindApplication(data);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001361 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 break;
1363 case EXIT_APPLICATION:
1364 if (mInitialApplication != null) {
1365 mInitialApplication.onTerminate();
1366 }
1367 Looper.myLooper().quit();
1368 break;
1369 case NEW_INTENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001370 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityNewIntent");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001371 handleNewIntent((NewIntentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001372 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 break;
1374 case RECEIVER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001375 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveComp");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 handleReceiver((ReceiverData)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001377 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001378 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379 break;
1380 case CREATE_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001381 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceCreate");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 handleCreateService((CreateServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001383 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 break;
1385 case BIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001386 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceBind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 handleBindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001388 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001389 break;
1390 case UNBIND_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001391 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceUnbind");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 handleUnbindService((BindServiceData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001393 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001394 break;
1395 case SERVICE_ARGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001396 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStart");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 handleServiceArgs((ServiceArgsData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001398 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 break;
1400 case STOP_SERVICE:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001401 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "serviceStop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 handleStopService((IBinder)msg.obj);
Bob Leee5408332009-09-04 18:31:17 -07001403 maybeSnapshot();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001404 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001406 case CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001407 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001408 mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001409 handleConfigurationChanged((Configuration)msg.obj, null);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001410 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 break;
1412 case CLEAN_UP_CONTEXT:
1413 ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj;
1414 cci.context.performFinalCleanup(cci.who, cci.what);
1415 break;
1416 case GC_WHEN_IDLE:
1417 scheduleGcIdler();
1418 break;
1419 case DUMP_SERVICE:
Dianne Hackborn625ac272010-09-17 18:29:22 -07001420 handleDumpService((DumpComponentInfo)msg.obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001421 break;
1422 case LOW_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001423 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "lowMemory");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 handleLowMemory();
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001425 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 break;
1427 case ACTIVITY_CONFIGURATION_CHANGED:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001428 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 handleActivityConfigurationChanged((IBinder)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001430 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 break;
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001432 case PROFILER_CONTROL:
Jeff Hao1b012d32014-08-20 10:35:34 -07001433 handleProfilerControl(msg.arg1 != 0, (ProfilerInfo)msg.obj, msg.arg2);
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08001434 break;
Christopher Tate181fafa2009-05-14 11:12:14 -07001435 case CREATE_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001436 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupCreateAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001437 handleCreateBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001438 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001439 break;
1440 case DESTROY_BACKUP_AGENT:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001441 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "backupDestroyAgent");
Christopher Tate181fafa2009-05-14 11:12:14 -07001442 handleDestroyBackupAgent((CreateBackupAgentData)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001443 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate181fafa2009-05-14 11:12:14 -07001444 break;
Christopher Tate5e1ab332009-09-01 20:32:49 -07001445 case SUICIDE:
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07001446 Process.killProcess(Process.myPid());
1447 break;
1448 case REMOVE_PROVIDER:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001449 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "providerRemove");
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001450 completeRemoveProvider((ProviderRefCount)msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001451 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Christopher Tate5e1ab332009-09-01 20:32:49 -07001452 break;
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001453 case ENABLE_JIT:
1454 ensureJitEnabled();
1455 break;
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001456 case DISPATCH_PACKAGE_BROADCAST:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001457 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001458 handleDispatchPackageBroadcast(msg.arg1, (String[])msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001459 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001460 break;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001461 case SCHEDULE_CRASH:
1462 throw new RemoteServiceException((String)msg.obj);
Andy McFadden824c5102010-07-09 16:26:57 -07001463 case DUMP_HEAP:
1464 handleDumpHeap(msg.arg1 != 0, (DumpHeapData)msg.obj);
1465 break;
Dianne Hackborn625ac272010-09-17 18:29:22 -07001466 case DUMP_ACTIVITY:
1467 handleDumpActivity((DumpComponentInfo)msg.obj);
1468 break;
Marco Nelissen18cb2872011-11-15 11:19:53 -08001469 case DUMP_PROVIDER:
1470 handleDumpProvider((DumpComponentInfo)msg.obj);
1471 break;
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001472 case SLEEPING:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001473 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "sleeping");
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001474 handleSleeping((IBinder)msg.obj, msg.arg1 != 0);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001475 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08001476 break;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001477 case SET_CORE_SETTINGS:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001478 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "setCoreSettings");
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001479 handleSetCoreSettings((Bundle) msg.obj);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001480 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08001481 break;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001482 case UPDATE_PACKAGE_COMPATIBILITY_INFO:
1483 handleUpdatePackageCompatibilityInfo((UpdateCompatibilityData)msg.obj);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001484 break;
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001485 case TRIM_MEMORY:
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001486 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "trimMemory");
Dianne Hackbornce86ba82011-07-13 19:33:41 -07001487 handleTrimMemory(msg.arg1);
Dianne Hackborn1ded0b12012-04-26 14:14:50 -07001488 Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
Dianne Hackbornf0754f5b2011-07-21 16:02:07 -07001489 break;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001490 case UNSTABLE_PROVIDER_DIED:
1491 handleUnstableProviderDied((IBinder)msg.obj, false);
1492 break;
Adam Skorydfc7fd72013-08-05 19:23:41 -07001493 case REQUEST_ASSIST_CONTEXT_EXTRAS:
1494 handleRequestAssistContextExtras((RequestAssistContextExtras)msg.obj);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08001495 break;
Craig Mautner5eda9b32013-07-02 11:58:16 -07001496 case TRANSLUCENT_CONVERSION_COMPLETE:
1497 handleTranslucentConversionComplete((IBinder)msg.obj, msg.arg1 == 1);
1498 break;
Jeff Sharkeydd97f422013-10-08 17:01:30 -07001499 case INSTALL_PROVIDER:
1500 handleInstallProvider((ProviderInfo) msg.obj);
1501 break;
Craig Mautnereb8abf72014-07-02 15:04:09 -07001502 case ON_NEW_ACTIVITY_OPTIONS:
1503 Pair<IBinder, ActivityOptions> pair = (Pair<IBinder, ActivityOptions>) msg.obj;
1504 onNewActivityOptions(pair.first, pair.second);
Dake Gu7ef70b02014-07-08 18:37:12 -07001505 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001506 case CANCEL_VISIBLE_BEHIND:
1507 handleCancelVisibleBehind((IBinder) msg.obj);
Craig Mautneree2e45a2014-06-27 12:10:03 -07001508 break;
Jose Lima4b6c6692014-08-12 17:41:12 -07001509 case BACKGROUND_VISIBLE_BEHIND_CHANGED:
1510 handleOnBackgroundVisibleBehindChanged((IBinder) msg.obj, msg.arg1 > 0);
Craig Mautnereb8abf72014-07-02 15:04:09 -07001511 break;
Craig Mautner8746a472014-07-24 15:12:54 -07001512 case ENTER_ANIMATION_COMPLETE:
1513 handleEnterAnimationComplete((IBinder) msg.obj);
1514 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515 }
Dianne Hackborn755c8bf2012-05-07 15:06:09 -07001516 if (DEBUG_MESSAGES) Slog.v(TAG, "<<< done: " + codeToString(msg.what));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
Bob Leee5408332009-09-04 18:31:17 -07001518
Brian Carlstromed7e0072011-03-24 13:27:57 -07001519 private void maybeSnapshot() {
1520 if (mBoundApplication != null && SamplingProfilerIntegration.isEnabled()) {
Sen Hubde75702010-05-28 01:54:03 -07001521 // convert the *private* ActivityThread.PackageInfo to *public* known
1522 // android.content.pm.PackageInfo
1523 String packageName = mBoundApplication.info.mPackageName;
1524 android.content.pm.PackageInfo packageInfo = null;
1525 try {
1526 Context context = getSystemContext();
1527 if(context == null) {
1528 Log.e(TAG, "cannot get a valid context");
1529 return;
1530 }
1531 PackageManager pm = context.getPackageManager();
1532 if(pm == null) {
1533 Log.e(TAG, "cannot get a valid PackageManager");
1534 return;
1535 }
1536 packageInfo = pm.getPackageInfo(
1537 packageName, PackageManager.GET_ACTIVITIES);
1538 } catch (NameNotFoundException e) {
1539 Log.e(TAG, "cannot get package info for " + packageName, e);
1540 }
1541 SamplingProfilerIntegration.writeSnapshot(mBoundApplication.processName, packageInfo);
Bob Leee5408332009-09-04 18:31:17 -07001542 }
1543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Romain Guy65b345f2011-07-27 18:51:50 -07001546 private class Idler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001547 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 public final boolean queueIdle() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001549 ActivityClientRecord a = mNewActivities;
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001550 boolean stopProfiling = false;
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001551 if (mBoundApplication != null && mProfiler.profileFd != null
1552 && mProfiler.autoStopProfiler) {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001553 stopProfiling = true;
1554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 if (a != null) {
1556 mNewActivities = null;
1557 IActivityManager am = ActivityManagerNative.getDefault();
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001558 ActivityClientRecord prev;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001559 do {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001560 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 TAG, "Reporting idle of " + a +
1562 " finished=" +
Romain Guy65b345f2011-07-27 18:51:50 -07001563 (a.activity != null && a.activity.mFinished));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 if (a.activity != null && !a.activity.mFinished) {
1565 try {
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001566 am.activityIdle(a.token, a.createdConfig, stopProfiling);
Dianne Hackborne88846e2009-09-30 21:34:25 -07001567 a.createdConfig = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001568 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07001569 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570 }
1571 }
1572 prev = a;
1573 a = a.nextIdle;
1574 prev.nextIdle = null;
1575 } while (a != null);
1576 }
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001577 if (stopProfiling) {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001578 mProfiler.stopProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07001579 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001580 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 return false;
1582 }
1583 }
1584
1585 final class GcIdler implements MessageQueue.IdleHandler {
Craig Mautner48d0d182013-06-11 07:53:06 -07001586 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 public final boolean queueIdle() {
1588 doGcIfNeeded();
1589 return false;
1590 }
1591 }
1592
Romain Guy65b345f2011-07-27 18:51:50 -07001593 public static ActivityThread currentActivityThread() {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08001594 return sCurrentActivityThread;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
Romain Guy65b345f2011-07-27 18:51:50 -07001597 public static String currentPackageName() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001598 ActivityThread am = currentActivityThread();
1599 return (am != null && am.mBoundApplication != null)
Dianne Hackbornb57a50b2013-04-11 17:29:32 -07001600 ? am.mBoundApplication.appInfo.packageName : null;
1601 }
1602
1603 public static String currentProcessName() {
1604 ActivityThread am = currentActivityThread();
1605 return (am != null && am.mBoundApplication != null)
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001606 ? am.mBoundApplication.processName : null;
1607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608
Romain Guy65b345f2011-07-27 18:51:50 -07001609 public static Application currentApplication() {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001610 ActivityThread am = currentActivityThread();
1611 return am != null ? am.mInitialApplication : null;
1612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001614 public static IPackageManager getPackageManager() {
1615 if (sPackageManager != null) {
1616 //Slog.v("PackageManager", "returning cur default = " + sPackageManager);
1617 return sPackageManager;
1618 }
1619 IBinder b = ServiceManager.getService("package");
1620 //Slog.v("PackageManager", "default service binder = " + b);
1621 sPackageManager = IPackageManager.Stub.asInterface(b);
1622 //Slog.v("PackageManager", "default service = " + sPackageManager);
1623 return sPackageManager;
1624 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625
Romain Guy65b345f2011-07-27 18:51:50 -07001626 private Configuration mMainThreadConfig = new Configuration();
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001627 Configuration applyConfigCompatMainThread(int displayDensity, Configuration config,
1628 CompatibilityInfo compat) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001629 if (config == null) {
1630 return null;
1631 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001632 if (!compat.supportsScreen()) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001633 mMainThreadConfig.setTo(config);
1634 config = mMainThreadConfig;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07001635 compat.applyToConfiguration(displayDensity, config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07001636 }
1637 return config;
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001640 /**
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001641 * Creates the top level resources for the given package.
1642 */
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001643 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
1644 String[] libDirs, int displayId, Configuration overrideConfiguration,
Dianne Hackborn756220b2012-08-14 16:45:30 -07001645 LoadedApk pkgInfo) {
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07001646 return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
1647 displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null);
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001648 }
1649
1650 final Handler getHandler() {
1651 return mH;
1652 }
1653
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001654 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1655 int flags) {
Amith Yamasani98edc952012-09-25 14:09:27 -07001656 return getPackageInfo(packageName, compatInfo, flags, UserHandle.myUserId());
1657 }
1658
1659 public final LoadedApk getPackageInfo(String packageName, CompatibilityInfo compatInfo,
1660 int flags, int userId) {
Craig Mautner88c05892013-06-28 09:47:45 -07001661 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001662 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 if ((flags&Context.CONTEXT_INCLUDE_CODE) != 0) {
1664 ref = mPackages.get(packageName);
1665 } else {
1666 ref = mResourcePackages.get(packageName);
1667 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001668 LoadedApk packageInfo = ref != null ? ref.get() : null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001669 //Slog.i(TAG, "getPackageInfo " + packageName + ": " + packageInfo);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07001670 //if (packageInfo != null) Slog.i(TAG, "isUptoDate " + packageInfo.mResDir
1671 // + ": " + packageInfo.mResources.getAssets().isUpToDate());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001672 if (packageInfo != null && (packageInfo.mResources == null
1673 || packageInfo.mResources.getAssets().isUpToDate())) {
1674 if (packageInfo.isSecurityViolation()
1675 && (flags&Context.CONTEXT_IGNORE_SECURITY) == 0) {
1676 throw new SecurityException(
1677 "Requesting code from " + packageName
1678 + " to be run in process "
1679 + mBoundApplication.processName
1680 + "/" + mBoundApplication.appInfo.uid);
1681 }
1682 return packageInfo;
1683 }
1684 }
1685
1686 ApplicationInfo ai = null;
1687 try {
1688 ai = getPackageManager().getApplicationInfo(packageName,
Amith Yamasani98edc952012-09-25 14:09:27 -07001689 PackageManager.GET_SHARED_LIBRARY_FILES, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 } catch (RemoteException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07001691 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001692 }
1693
1694 if (ai != null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001695 return getPackageInfo(ai, compatInfo, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
1698 return null;
1699 }
1700
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001701 public final LoadedApk getPackageInfo(ApplicationInfo ai, CompatibilityInfo compatInfo,
1702 int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 boolean includeCode = (flags&Context.CONTEXT_INCLUDE_CODE) != 0;
1704 boolean securityViolation = includeCode && ai.uid != 0
1705 && ai.uid != Process.SYSTEM_UID && (mBoundApplication != null
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001706 ? !UserHandle.isSameApp(ai.uid, mBoundApplication.appInfo.uid)
Amith Yamasani742a6712011-05-04 14:49:28 -07001707 : true);
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001708 boolean registerPackage = includeCode && (flags&Context.CONTEXT_REGISTER_PACKAGE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 if ((flags&(Context.CONTEXT_INCLUDE_CODE
1710 |Context.CONTEXT_IGNORE_SECURITY))
1711 == Context.CONTEXT_INCLUDE_CODE) {
1712 if (securityViolation) {
1713 String msg = "Requesting code from " + ai.packageName
1714 + " (with uid " + ai.uid + ")";
1715 if (mBoundApplication != null) {
1716 msg = msg + " to be run in process "
1717 + mBoundApplication.processName + " (with uid "
1718 + mBoundApplication.appInfo.uid + ")";
1719 }
1720 throw new SecurityException(msg);
1721 }
1722 }
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001723 return getPackageInfo(ai, compatInfo, null, securityViolation, includeCode,
1724 registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 }
1726
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001727 public final LoadedApk getPackageInfoNoCheck(ApplicationInfo ai,
1728 CompatibilityInfo compatInfo) {
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001729 return getPackageInfo(ai, compatInfo, null, false, true, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 }
1731
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001732 public final LoadedApk peekPackageInfo(String packageName, boolean includeCode) {
Craig Mautner88c05892013-06-28 09:47:45 -07001733 synchronized (mResourcesManager) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04001734 WeakReference<LoadedApk> ref;
1735 if (includeCode) {
1736 ref = mPackages.get(packageName);
1737 } else {
1738 ref = mResourcePackages.get(packageName);
1739 }
1740 return ref != null ? ref.get() : null;
1741 }
1742 }
1743
Romain Guy65b345f2011-07-27 18:51:50 -07001744 private LoadedApk getPackageInfo(ApplicationInfo aInfo, CompatibilityInfo compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001745 ClassLoader baseLoader, boolean securityViolation, boolean includeCode,
1746 boolean registerPackage) {
Craig Mautner88c05892013-06-28 09:47:45 -07001747 synchronized (mResourcesManager) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001748 WeakReference<LoadedApk> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 if (includeCode) {
1750 ref = mPackages.get(aInfo.packageName);
1751 } else {
1752 ref = mResourcePackages.get(aInfo.packageName);
1753 }
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001754 LoadedApk packageInfo = ref != null ? ref.get() : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 if (packageInfo == null || (packageInfo.mResources != null
1756 && !packageInfo.mResources.getAssets().isUpToDate())) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001757 if (localLOGV) Slog.v(TAG, (includeCode ? "Loading code package "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 : "Loading resource-only package ") + aInfo.packageName
1759 + " (in " + (mBoundApplication != null
1760 ? mBoundApplication.processName : null)
1761 + ")");
1762 packageInfo =
Jeff Browndefd4a62014-03-10 21:24:37 -07001763 new LoadedApk(this, aInfo, compatInfo, baseLoader,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 securityViolation, includeCode &&
Dianne Hackbornfee756f2014-07-16 17:31:10 -07001765 (aInfo.flags&ApplicationInfo.FLAG_HAS_CODE) != 0, registerPackage);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 if (includeCode) {
1767 mPackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001768 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 } else {
1770 mResourcePackages.put(aInfo.packageName,
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07001771 new WeakReference<LoadedApk>(packageInfo));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 }
1773 }
1774 return packageInfo;
1775 }
1776 }
1777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 ActivityThread() {
Craig Mautner88c05892013-06-28 09:47:45 -07001779 mResourcesManager = ResourcesManager.getInstance();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 }
1781
1782 public ApplicationThread getApplicationThread()
1783 {
1784 return mAppThread;
1785 }
1786
1787 public Instrumentation getInstrumentation()
1788 {
1789 return mInstrumentation;
1790 }
1791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 public boolean isProfiling() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001793 return mProfiler != null && mProfiler.profileFile != null
1794 && mProfiler.profileFd == null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 }
1796
1797 public String getProfileFilePath() {
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001798 return mProfiler.profileFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 }
1800
1801 public Looper getLooper() {
1802 return mLooper;
1803 }
1804
1805 public Application getApplication() {
1806 return mInitialApplication;
1807 }
Bob Leee5408332009-09-04 18:31:17 -07001808
Dianne Hackbornd97c7ad2009-06-19 11:37:35 -07001809 public String getProcessName() {
1810 return mBoundApplication.processName;
1811 }
Bob Leee5408332009-09-04 18:31:17 -07001812
Dianne Hackborn21556372010-02-04 16:34:40 -08001813 public ContextImpl getSystemContext() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 synchronized (this) {
1815 if (mSystemContext == null) {
Jeff Browndefd4a62014-03-10 21:24:37 -07001816 mSystemContext = ContextImpl.createSystemContext(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
Jeff Browndefd4a62014-03-10 21:24:37 -07001818 return mSystemContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
1821
Narayan Kamath29564cd2014-08-07 10:57:40 +01001822 public void installSystemApplicationInfo(ApplicationInfo info, ClassLoader classLoader) {
Mike Cleron432b7132009-09-24 15:28:29 -07001823 synchronized (this) {
Narayan Kamath29564cd2014-08-07 10:57:40 +01001824 getSystemContext().installSystemApplicationInfo(info, classLoader);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001825
Narayan Kamath3480ab42014-08-15 18:11:04 +01001826 // The code package for "android" in the system server needs
1827 // to be the system context's package.
1828 mPackages.put("android", new WeakReference<LoadedApk>(getSystemContext().mPackageInfo));
1829
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07001830 // give ourselves a default profiler
1831 mProfiler = new Profiler();
Mike Cleron432b7132009-09-24 15:28:29 -07001832 }
1833 }
1834
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001835 void ensureJitEnabled() {
1836 if (!mJitEnabled) {
1837 mJitEnabled = true;
1838 dalvik.system.VMRuntime.getRuntime().startJitCompilation();
1839 }
1840 }
Craig Mautner48d0d182013-06-11 07:53:06 -07001841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 void scheduleGcIdler() {
1843 if (!mGcIdlerScheduled) {
1844 mGcIdlerScheduled = true;
1845 Looper.myQueue().addIdleHandler(mGcIdler);
1846 }
1847 mH.removeMessages(H.GC_WHEN_IDLE);
1848 }
1849
1850 void unscheduleGcIdler() {
1851 if (mGcIdlerScheduled) {
1852 mGcIdlerScheduled = false;
1853 Looper.myQueue().removeIdleHandler(mGcIdler);
1854 }
1855 mH.removeMessages(H.GC_WHEN_IDLE);
1856 }
1857
1858 void doGcIfNeeded() {
1859 mGcIdlerScheduled = false;
1860 final long now = SystemClock.uptimeMillis();
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001861 //Slog.i(TAG, "**** WE MIGHT WANT TO GC: then=" + Binder.getLastGcTime()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 // + "m now=" + now);
1863 if ((BinderInternal.getLastGcTime()+MIN_TIME_BETWEEN_GCS) < now) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07001864 //Slog.i(TAG, "**** WE DO, WE DO WANT TO GC!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 BinderInternal.forceGc("bg");
1866 }
1867 }
1868
Dianne Hackborne77187d2013-10-25 16:32:41 -07001869 private static final String HEAP_FULL_COLUMN
1870 = "%13s %8s %8s %8s %8s %8s %8s %8s %8s %8s %8s";
1871 private static final String HEAP_COLUMN
1872 = "%13s %8s %8s %8s %8s %8s %8s %8s";
1873
1874 // Formatting for checkin service - update version if row format changes
1875 private static final int ACTIVITY_THREAD_CHECKIN_VERSION = 3;
1876
1877 static void printRow(PrintWriter pw, String format, Object...objs) {
1878 pw.println(String.format(format, objs));
1879 }
1880
1881 public static void dumpMemInfoTable(PrintWriter pw, Debug.MemoryInfo memInfo, boolean checkin,
1882 boolean dumpFullInfo, boolean dumpDalvik, int pid, String processName,
1883 long nativeMax, long nativeAllocated, long nativeFree,
1884 long dalvikMax, long dalvikAllocated, long dalvikFree) {
1885
1886 // For checkin, we print one long comma-separated list of values
1887 if (checkin) {
1888 // NOTE: if you change anything significant below, also consider changing
1889 // ACTIVITY_THREAD_CHECKIN_VERSION.
1890
1891 // Header
1892 pw.print(ACTIVITY_THREAD_CHECKIN_VERSION); pw.print(',');
1893 pw.print(pid); pw.print(',');
1894 pw.print(processName); pw.print(',');
1895
1896 // Heap info - max
1897 pw.print(nativeMax); pw.print(',');
1898 pw.print(dalvikMax); pw.print(',');
1899 pw.print("N/A,");
1900 pw.print(nativeMax + dalvikMax); pw.print(',');
1901
1902 // Heap info - allocated
1903 pw.print(nativeAllocated); pw.print(',');
1904 pw.print(dalvikAllocated); pw.print(',');
1905 pw.print("N/A,");
1906 pw.print(nativeAllocated + dalvikAllocated); pw.print(',');
1907
1908 // Heap info - free
1909 pw.print(nativeFree); pw.print(',');
1910 pw.print(dalvikFree); pw.print(',');
1911 pw.print("N/A,");
1912 pw.print(nativeFree + dalvikFree); pw.print(',');
1913
1914 // Heap info - proportional set size
1915 pw.print(memInfo.nativePss); pw.print(',');
1916 pw.print(memInfo.dalvikPss); pw.print(',');
1917 pw.print(memInfo.otherPss); pw.print(',');
1918 pw.print(memInfo.getTotalPss()); pw.print(',');
1919
1920 // Heap info - swappable set size
1921 pw.print(memInfo.nativeSwappablePss); pw.print(',');
1922 pw.print(memInfo.dalvikSwappablePss); pw.print(',');
1923 pw.print(memInfo.otherSwappablePss); pw.print(',');
1924 pw.print(memInfo.getTotalSwappablePss()); pw.print(',');
1925
1926 // Heap info - shared dirty
1927 pw.print(memInfo.nativeSharedDirty); pw.print(',');
1928 pw.print(memInfo.dalvikSharedDirty); pw.print(',');
1929 pw.print(memInfo.otherSharedDirty); pw.print(',');
1930 pw.print(memInfo.getTotalSharedDirty()); pw.print(',');
1931
1932 // Heap info - shared clean
1933 pw.print(memInfo.nativeSharedClean); pw.print(',');
1934 pw.print(memInfo.dalvikSharedClean); pw.print(',');
1935 pw.print(memInfo.otherSharedClean); pw.print(',');
1936 pw.print(memInfo.getTotalSharedClean()); pw.print(',');
1937
1938 // Heap info - private Dirty
1939 pw.print(memInfo.nativePrivateDirty); pw.print(',');
1940 pw.print(memInfo.dalvikPrivateDirty); pw.print(',');
1941 pw.print(memInfo.otherPrivateDirty); pw.print(',');
1942 pw.print(memInfo.getTotalPrivateDirty()); pw.print(',');
1943
1944 // Heap info - private Clean
1945 pw.print(memInfo.nativePrivateClean); pw.print(',');
1946 pw.print(memInfo.dalvikPrivateClean); pw.print(',');
1947 pw.print(memInfo.otherPrivateClean); pw.print(',');
1948 pw.print(memInfo.getTotalPrivateClean()); pw.print(',');
1949
1950 // Heap info - other areas
1951 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
1952 pw.print(Debug.MemoryInfo.getOtherLabel(i)); pw.print(',');
1953 pw.print(memInfo.getOtherPss(i)); pw.print(',');
1954 pw.print(memInfo.getOtherSwappablePss(i)); pw.print(',');
1955 pw.print(memInfo.getOtherSharedDirty(i)); pw.print(',');
1956 pw.print(memInfo.getOtherSharedClean(i)); pw.print(',');
1957 pw.print(memInfo.getOtherPrivateDirty(i)); pw.print(',');
1958 pw.print(memInfo.getOtherPrivateClean(i)); pw.print(',');
1959 }
1960 return;
1961 }
1962
1963 // otherwise, show human-readable format
1964 if (dumpFullInfo) {
1965 printRow(pw, HEAP_FULL_COLUMN, "", "Pss", "Pss", "Shared", "Private",
1966 "Shared", "Private", "Swapped", "Heap", "Heap", "Heap");
1967 printRow(pw, HEAP_FULL_COLUMN, "", "Total", "Clean", "Dirty", "Dirty",
1968 "Clean", "Clean", "Dirty", "Size", "Alloc", "Free");
1969 printRow(pw, HEAP_FULL_COLUMN, "", "------", "------", "------", "------",
1970 "------", "------", "------", "------", "------", "------");
1971 printRow(pw, HEAP_FULL_COLUMN, "Native Heap", memInfo.nativePss,
1972 memInfo.nativeSwappablePss, memInfo.nativeSharedDirty,
1973 memInfo.nativePrivateDirty, memInfo.nativeSharedClean,
1974 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1975 nativeMax, nativeAllocated, nativeFree);
1976 printRow(pw, HEAP_FULL_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1977 memInfo.dalvikSwappablePss, memInfo.dalvikSharedDirty,
1978 memInfo.dalvikPrivateDirty, memInfo.dalvikSharedClean,
1979 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1980 dalvikMax, dalvikAllocated, dalvikFree);
1981 } else {
1982 printRow(pw, HEAP_COLUMN, "", "Pss", "Private",
1983 "Private", "Swapped", "Heap", "Heap", "Heap");
1984 printRow(pw, HEAP_COLUMN, "", "Total", "Dirty",
1985 "Clean", "Dirty", "Size", "Alloc", "Free");
1986 printRow(pw, HEAP_COLUMN, "", "------", "------", "------",
1987 "------", "------", "------", "------", "------");
1988 printRow(pw, HEAP_COLUMN, "Native Heap", memInfo.nativePss,
1989 memInfo.nativePrivateDirty,
1990 memInfo.nativePrivateClean, memInfo.nativeSwappedOut,
1991 nativeMax, nativeAllocated, nativeFree);
1992 printRow(pw, HEAP_COLUMN, "Dalvik Heap", memInfo.dalvikPss,
1993 memInfo.dalvikPrivateDirty,
1994 memInfo.dalvikPrivateClean, memInfo.dalvikSwappedOut,
1995 dalvikMax, dalvikAllocated, dalvikFree);
1996 }
1997
1998 int otherPss = memInfo.otherPss;
1999 int otherSwappablePss = memInfo.otherSwappablePss;
2000 int otherSharedDirty = memInfo.otherSharedDirty;
2001 int otherPrivateDirty = memInfo.otherPrivateDirty;
2002 int otherSharedClean = memInfo.otherSharedClean;
2003 int otherPrivateClean = memInfo.otherPrivateClean;
2004 int otherSwappedOut = memInfo.otherSwappedOut;
2005
2006 for (int i=0; i<Debug.MemoryInfo.NUM_OTHER_STATS; i++) {
2007 final int myPss = memInfo.getOtherPss(i);
2008 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2009 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2010 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2011 final int mySharedClean = memInfo.getOtherSharedClean(i);
2012 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2013 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2014 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2015 || mySharedClean != 0 || myPrivateClean != 0 || mySwappedOut != 0) {
2016 if (dumpFullInfo) {
2017 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2018 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2019 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2020 } else {
2021 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2022 myPss, myPrivateDirty,
2023 myPrivateClean, mySwappedOut, "", "", "");
2024 }
2025 otherPss -= myPss;
2026 otherSwappablePss -= mySwappablePss;
2027 otherSharedDirty -= mySharedDirty;
2028 otherPrivateDirty -= myPrivateDirty;
2029 otherSharedClean -= mySharedClean;
2030 otherPrivateClean -= myPrivateClean;
2031 otherSwappedOut -= mySwappedOut;
2032 }
2033 }
2034
2035 if (dumpFullInfo) {
2036 printRow(pw, HEAP_FULL_COLUMN, "Unknown", otherPss, otherSwappablePss,
2037 otherSharedDirty, otherPrivateDirty, otherSharedClean, otherPrivateClean,
2038 otherSwappedOut, "", "", "");
2039 printRow(pw, HEAP_FULL_COLUMN, "TOTAL", memInfo.getTotalPss(),
2040 memInfo.getTotalSwappablePss(),
2041 memInfo.getTotalSharedDirty(), memInfo.getTotalPrivateDirty(),
2042 memInfo.getTotalSharedClean(), memInfo.getTotalPrivateClean(),
2043 memInfo.getTotalSwappedOut(), nativeMax+dalvikMax,
2044 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2045 } else {
2046 printRow(pw, HEAP_COLUMN, "Unknown", otherPss,
2047 otherPrivateDirty, otherPrivateClean, otherSwappedOut,
2048 "", "", "");
2049 printRow(pw, HEAP_COLUMN, "TOTAL", memInfo.getTotalPss(),
2050 memInfo.getTotalPrivateDirty(),
2051 memInfo.getTotalPrivateClean(),
2052 memInfo.getTotalSwappedOut(),
2053 nativeMax+dalvikMax,
2054 nativeAllocated+dalvikAllocated, nativeFree+dalvikFree);
2055 }
2056
2057 if (dumpDalvik) {
2058 pw.println(" ");
2059 pw.println(" Dalvik Details");
2060
2061 for (int i=Debug.MemoryInfo.NUM_OTHER_STATS;
2062 i<Debug.MemoryInfo.NUM_OTHER_STATS + Debug.MemoryInfo.NUM_DVK_STATS; i++) {
2063 final int myPss = memInfo.getOtherPss(i);
2064 final int mySwappablePss = memInfo.getOtherSwappablePss(i);
2065 final int mySharedDirty = memInfo.getOtherSharedDirty(i);
2066 final int myPrivateDirty = memInfo.getOtherPrivateDirty(i);
2067 final int mySharedClean = memInfo.getOtherSharedClean(i);
2068 final int myPrivateClean = memInfo.getOtherPrivateClean(i);
2069 final int mySwappedOut = memInfo.getOtherSwappedOut(i);
2070 if (myPss != 0 || mySharedDirty != 0 || myPrivateDirty != 0
2071 || mySharedClean != 0 || myPrivateClean != 0) {
2072 if (dumpFullInfo) {
2073 printRow(pw, HEAP_FULL_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2074 myPss, mySwappablePss, mySharedDirty, myPrivateDirty,
2075 mySharedClean, myPrivateClean, mySwappedOut, "", "", "");
2076 } else {
2077 printRow(pw, HEAP_COLUMN, Debug.MemoryInfo.getOtherLabel(i),
2078 myPss, myPrivateDirty,
2079 myPrivateClean, mySwappedOut, "", "", "");
2080 }
2081 }
2082 }
2083 }
2084 }
2085
Jeff Hamilton52d32032011-01-08 15:31:26 -06002086 public void registerOnActivityPausedListener(Activity activity,
2087 OnActivityPausedListener listener) {
2088 synchronized (mOnPauseListeners) {
2089 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2090 if (list == null) {
2091 list = new ArrayList<OnActivityPausedListener>();
2092 mOnPauseListeners.put(activity, list);
2093 }
2094 list.add(listener);
2095 }
2096 }
2097
Jeff Hamiltonce3224c2011-01-17 11:05:03 -08002098 public void unregisterOnActivityPausedListener(Activity activity,
2099 OnActivityPausedListener listener) {
2100 synchronized (mOnPauseListeners) {
2101 ArrayList<OnActivityPausedListener> list = mOnPauseListeners.get(activity);
2102 if (list != null) {
2103 list.remove(listener);
2104 }
2105 }
2106 }
2107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 public final ActivityInfo resolveActivityInfo(Intent intent) {
2109 ActivityInfo aInfo = intent.resolveActivityInfo(
2110 mInitialApplication.getPackageManager(), PackageManager.GET_SHARED_LIBRARY_FILES);
2111 if (aInfo == null) {
2112 // Throw an exception.
2113 Instrumentation.checkStartActivityResult(
Dianne Hackborna4972e92012-03-14 10:38:05 -07002114 ActivityManager.START_CLASS_NOT_FOUND, intent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
2116 return aInfo;
2117 }
Bob Leee5408332009-09-04 18:31:17 -07002118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 public final Activity startActivityNow(Activity parent, String id,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002120 Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002121 Activity.NonConfigurationInstances lastNonConfigurationInstances) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002122 ActivityClientRecord r = new ActivityClientRecord();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 r.token = token;
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002124 r.ident = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 r.intent = intent;
2126 r.state = state;
2127 r.parent = parent;
2128 r.embeddedID = id;
2129 r.activityInfo = activityInfo;
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002130 r.lastNonConfigurationInstances = lastNonConfigurationInstances;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 if (localLOGV) {
2132 ComponentName compname = intent.getComponent();
2133 String name;
2134 if (compname != null) {
2135 name = compname.toShortString();
2136 } else {
2137 name = "(Intent " + intent + ").getComponent() returned null";
2138 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002139 Slog.v(TAG, "Performing launch: action=" + intent.getAction()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 + ", comp=" + name
2141 + ", token=" + token);
2142 }
Craig Mautner233ceee2014-05-09 17:05:11 -07002143 return performLaunchActivity(r, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144 }
2145
2146 public final Activity getActivity(IBinder token) {
2147 return mActivities.get(token).activity;
2148 }
2149
2150 public final void sendActivityResult(
2151 IBinder token, String id, int requestCode,
2152 int resultCode, Intent data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002153 if (DEBUG_RESULTS) Slog.v(TAG, "sendActivityResult: id=" + id
Chris Tate8a7dc172009-03-24 20:11:42 -07002154 + " req=" + requestCode + " res=" + resultCode + " data=" + data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 ArrayList<ResultInfo> list = new ArrayList<ResultInfo>();
2156 list.add(new ResultInfo(id, requestCode, resultCode, data));
2157 mAppThread.scheduleSendResult(token, list);
2158 }
2159
Jeff Brown9ef09972013-10-15 20:49:59 -07002160 private void sendMessage(int what, Object obj) {
2161 sendMessage(what, obj, 0, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163
Jeff Brown9ef09972013-10-15 20:49:59 -07002164 private void sendMessage(int what, Object obj, int arg1) {
2165 sendMessage(what, obj, arg1, 0, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 }
2167
Jeff Brown9ef09972013-10-15 20:49:59 -07002168 private void sendMessage(int what, Object obj, int arg1, int arg2) {
2169 sendMessage(what, obj, arg1, arg2, false);
2170 }
2171
2172 private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
2173 if (DEBUG_MESSAGES) Slog.v(
2174 TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
2175 + ": " + arg1 + " / " + obj);
2176 Message msg = Message.obtain();
2177 msg.what = what;
2178 msg.obj = obj;
2179 msg.arg1 = arg1;
2180 msg.arg2 = arg2;
2181 if (async) {
2182 msg.setAsynchronous(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 }
Jeff Brown9ef09972013-10-15 20:49:59 -07002184 mH.sendMessage(msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 }
2186
Dianne Hackborn21556372010-02-04 16:34:40 -08002187 final void scheduleContextCleanup(ContextImpl context, String who,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 String what) {
2189 ContextCleanupInfo cci = new ContextCleanupInfo();
2190 cci.context = context;
2191 cci.who = who;
2192 cci.what = what;
Jeff Brown9ef09972013-10-15 20:49:59 -07002193 sendMessage(H.CLEAN_UP_CONTEXT, cci);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195
Craig Mautner233ceee2014-05-09 17:05:11 -07002196 private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002197 // System.out.println("##### [" + System.currentTimeMillis() + "] ActivityThread.performLaunchActivity(" + r + ")");
2198
2199 ActivityInfo aInfo = r.activityInfo;
2200 if (r.packageInfo == null) {
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002201 r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 Context.CONTEXT_INCLUDE_CODE);
2203 }
Bob Leee5408332009-09-04 18:31:17 -07002204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 ComponentName component = r.intent.getComponent();
2206 if (component == null) {
2207 component = r.intent.resolveActivity(
2208 mInitialApplication.getPackageManager());
2209 r.intent.setComponent(component);
2210 }
2211
2212 if (r.activityInfo.targetActivity != null) {
2213 component = new ComponentName(r.activityInfo.packageName,
2214 r.activityInfo.targetActivity);
2215 }
2216
2217 Activity activity = null;
2218 try {
2219 java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
2220 activity = mInstrumentation.newActivity(
2221 cl, component.getClassName(), r.intent);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08002222 StrictMode.incrementExpectedActivityCount(activity.getClass());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 r.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002224 r.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 if (r.state != null) {
2226 r.state.setClassLoader(cl);
2227 }
2228 } catch (Exception e) {
2229 if (!mInstrumentation.onException(activity, e)) {
2230 throw new RuntimeException(
2231 "Unable to instantiate activity " + component
2232 + ": " + e.toString(), e);
2233 }
2234 }
2235
2236 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002237 Application app = r.packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002238
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002239 if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
2240 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 TAG, r + ": app=" + app
2242 + ", appName=" + app.getPackageName()
2243 + ", pkg=" + r.packageInfo.getPackageName()
2244 + ", comp=" + r.intent.getComponent().toShortString()
2245 + ", dir=" + r.packageInfo.getAppDir());
2246
2247 if (activity != null) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002248 Context appContext = createBaseContextForActivity(r, activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002249 CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Dianne Hackborn5fd21692011-06-07 14:09:47 -07002250 Configuration config = new Configuration(mCompatConfiguration);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002251 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07002252 + r.activityInfo.name + " with config " + config);
Dianne Hackbornb06ea702009-07-13 13:07:51 -07002253 activity.attach(appContext, this, getInstrumentation(), r.token,
2254 r.ident, app, r.intent, r.activityInfo, title, r.parent,
Craig Mautner233ceee2014-05-09 17:05:11 -07002255 r.embeddedID, r.lastNonConfigurationInstances, config,
Dianne Hackborn91097de2014-04-04 18:02:06 -07002256 r.voiceInteractor);
Bob Leee5408332009-09-04 18:31:17 -07002257
Christopher Tateb70f3df2009-04-07 16:07:59 -07002258 if (customIntent != null) {
2259 activity.mIntent = customIntent;
2260 }
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07002261 r.lastNonConfigurationInstances = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 activity.mStartedActivity = false;
2263 int theme = r.activityInfo.getThemeResource();
2264 if (theme != 0) {
2265 activity.setTheme(theme);
2266 }
2267
2268 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002269 if (r.isPersistable()) {
2270 mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
2271 } else {
2272 mInstrumentation.callActivityOnCreate(activity, r.state);
2273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 if (!activity.mCalled) {
2275 throw new SuperNotCalledException(
2276 "Activity " + r.intent.getComponent().toShortString() +
2277 " did not call through to super.onCreate()");
2278 }
2279 r.activity = activity;
2280 r.stopped = true;
2281 if (!r.activity.mFinished) {
2282 activity.performStart();
2283 r.stopped = false;
2284 }
2285 if (!r.activity.mFinished) {
Craig Mautnera0026042014-04-23 11:45:37 -07002286 if (r.isPersistable()) {
2287 if (r.state != null || r.persistentState != null) {
2288 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,
2289 r.persistentState);
2290 }
2291 } else if (r.state != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
2293 }
2294 }
2295 if (!r.activity.mFinished) {
2296 activity.mCalled = false;
Craig Mautnera0026042014-04-23 11:45:37 -07002297 if (r.isPersistable()) {
2298 mInstrumentation.callActivityOnPostCreate(activity, r.state,
2299 r.persistentState);
2300 } else {
2301 mInstrumentation.callActivityOnPostCreate(activity, r.state);
2302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 if (!activity.mCalled) {
2304 throw new SuperNotCalledException(
2305 "Activity " + r.intent.getComponent().toShortString() +
2306 " did not call through to super.onPostCreate()");
2307 }
2308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 }
2310 r.paused = true;
2311
2312 mActivities.put(r.token, r);
2313
2314 } catch (SuperNotCalledException e) {
2315 throw e;
2316
2317 } catch (Exception e) {
2318 if (!mInstrumentation.onException(activity, e)) {
2319 throw new RuntimeException(
2320 "Unable to start activity " + component
2321 + ": " + e.toString(), e);
2322 }
2323 }
2324
2325 return activity;
2326 }
2327
Jeff Brownefd43bd2012-09-21 17:02:35 -07002328 private Context createBaseContextForActivity(ActivityClientRecord r,
2329 final Activity activity) {
Jeff Browndefd4a62014-03-10 21:24:37 -07002330 ContextImpl appContext = ContextImpl.createActivityContext(this, r.packageInfo, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002331 appContext.setOuterContext(activity);
Craig Mautnere0a38842013-12-16 16:14:02 -08002332 Context baseContext = appContext;
2333
2334 final DisplayManagerGlobal dm = DisplayManagerGlobal.getInstance();
2335 try {
2336 IActivityContainer container =
2337 ActivityManagerNative.getDefault().getEnclosingActivityContainer(r.token);
Craig Mautnerbd503a42014-01-10 10:16:43 -08002338 final int displayId =
2339 container == null ? Display.DEFAULT_DISPLAY : container.getDisplayId();
Craig Mautnere0a38842013-12-16 16:14:02 -08002340 if (displayId > Display.DEFAULT_DISPLAY) {
2341 Display display = dm.getRealDisplay(displayId, r.token);
2342 baseContext = appContext.createDisplayContext(display);
2343 }
2344 } catch (RemoteException e) {
2345 }
Jeff Brownefd43bd2012-09-21 17:02:35 -07002346
2347 // For debugging purposes, if the activity's package name contains the value of
2348 // the "debug.use-second-display" system property as a substring, then show
2349 // its content on a secondary display if there is one.
Jeff Brownefd43bd2012-09-21 17:02:35 -07002350 String pkgName = SystemProperties.get("debug.second-display.pkg");
2351 if (pkgName != null && !pkgName.isEmpty()
2352 && r.packageInfo.mPackageName.contains(pkgName)) {
Jeff Brownefd43bd2012-09-21 17:02:35 -07002353 for (int displayId : dm.getDisplayIds()) {
2354 if (displayId != Display.DEFAULT_DISPLAY) {
Craig Mautner48d0d182013-06-11 07:53:06 -07002355 Display display = dm.getRealDisplay(displayId, r.token);
Jeff Brownefd43bd2012-09-21 17:02:35 -07002356 baseContext = appContext.createDisplayContext(display);
2357 break;
2358 }
2359 }
2360 }
2361 return baseContext;
2362 }
2363
Romain Guy65b345f2011-07-27 18:51:50 -07002364 private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 // If we are getting ready to gc after going to the background, well
2366 // we are back active so skip it.
2367 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002368 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369
Jeff Hao1b012d32014-08-20 10:35:34 -07002370 if (r.profilerInfo != null) {
2371 mProfiler.setProfiler(r.profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07002372 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07002373 }
2374
Dianne Hackborn58f42a52011-10-10 13:46:34 -07002375 // Make sure we are running with the most recent config.
2376 handleConfigurationChanged(null, null);
2377
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002378 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 TAG, "Handling launch of " + r);
Adam Powellcfbe9be2013-11-06 14:58:58 -08002380
Craig Mautner233ceee2014-05-09 17:05:11 -07002381 Activity a = performLaunchActivity(r, customIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382
2383 if (a != null) {
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08002384 r.createdConfig = new Configuration(mConfiguration);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002385 Bundle oldState = r.state;
Craig Mautner233ceee2014-05-09 17:05:11 -07002386 handleResumeActivity(r.token, false, r.isForward,
Dianne Hackbornad9b32112012-09-17 15:35:01 -07002387 !r.activity.mFinished && !r.startsNotResumed);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388
2389 if (!r.activity.mFinished && r.startsNotResumed) {
2390 // The activity manager actually wants this one to start out
2391 // paused, because it needs to be visible but isn't in the
2392 // foreground. We accomplish this by going through the
2393 // normal startup (because activities expect to go through
2394 // onResume() the first time they run, before their window
2395 // is displayed), and then pausing it. However, in this case
2396 // we do -not- need to do the full pause cycle (of freezing
2397 // and such) because the activity manager assumes it can just
2398 // retain the current state it has.
2399 try {
2400 r.activity.mCalled = false;
2401 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -08002402 // We need to keep around the original state, in case
Dianne Hackborn03fcc332012-05-15 12:49:40 -07002403 // we need to be created again. But we only do this
2404 // for pre-Honeycomb apps, which always save their state
2405 // when pausing, so we can not have them save their state
2406 // when restarting from a paused state. For HC and later,
2407 // we want to (and can) let the state be saved as the normal
2408 // part of stopping the activity.
2409 if (r.isPreHoneycomb()) {
2410 r.state = oldState;
2411 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (!r.activity.mCalled) {
2413 throw new SuperNotCalledException(
2414 "Activity " + r.intent.getComponent().toShortString() +
2415 " did not call through to super.onPause()");
2416 }
2417
2418 } catch (SuperNotCalledException e) {
2419 throw e;
2420
2421 } catch (Exception e) {
2422 if (!mInstrumentation.onException(r.activity, e)) {
2423 throw new RuntimeException(
2424 "Unable to pause activity "
2425 + r.intent.getComponent().toShortString()
2426 + ": " + e.toString(), e);
2427 }
2428 }
2429 r.paused = true;
2430 }
2431 } else {
2432 // If there was an error, for any reason, tell the activity
2433 // manager to stop us.
2434 try {
2435 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07002436 .finishActivity(r.token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07002438 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002439 }
2440 }
2441 }
2442
Romain Guy65b345f2011-07-27 18:51:50 -07002443 private void deliverNewIntents(ActivityClientRecord r,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002444 List<Intent> intents) {
2445 final int N = intents.size();
2446 for (int i=0; i<N; i++) {
2447 Intent intent = intents.get(i);
2448 intent.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002449 intent.prepareToEnterProcess();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002450 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 mInstrumentation.callActivityOnNewIntent(r.activity, intent);
2452 }
2453 }
2454
2455 public final void performNewIntents(IBinder token,
2456 List<Intent> intents) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002457 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 if (r != null) {
2459 final boolean resumed = !r.paused;
2460 if (resumed) {
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002461 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002462 mInstrumentation.callActivityOnPause(r.activity);
2463 }
2464 deliverNewIntents(r, intents);
2465 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07002466 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07002467 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 }
2469 }
2470 }
Bob Leee5408332009-09-04 18:31:17 -07002471
Romain Guy65b345f2011-07-27 18:51:50 -07002472 private void handleNewIntent(NewIntentData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002473 performNewIntents(data.token, data.intents);
2474 }
2475
Adam Skorydfc7fd72013-08-05 19:23:41 -07002476 public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002477 Bundle data = new Bundle();
2478 ActivityClientRecord r = mActivities.get(cmd.activityToken);
2479 if (r != null) {
2480 r.activity.getApplication().dispatchOnProvideAssistData(r.activity, data);
2481 r.activity.onProvideAssistData(data);
2482 }
2483 if (data.isEmpty()) {
2484 data = null;
2485 }
2486 IActivityManager mgr = ActivityManagerNative.getDefault();
2487 try {
Adam Skory7140a252013-09-11 12:04:58 +01002488 mgr.reportAssistContextExtras(cmd.requestToken, data);
Dianne Hackbornf9c5e0f2013-01-23 14:39:13 -08002489 } catch (RemoteException e) {
2490 }
2491 }
Craig Mautner5eda9b32013-07-02 11:58:16 -07002492
2493 public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
2494 ActivityClientRecord r = mActivities.get(token);
2495 if (r != null) {
2496 r.activity.onTranslucentConversionComplete(drawComplete);
2497 }
2498 }
2499
Craig Mautnereb8abf72014-07-02 15:04:09 -07002500 public void onNewActivityOptions(IBinder token, ActivityOptions options) {
2501 ActivityClientRecord r = mActivities.get(token);
2502 if (r != null) {
2503 r.activity.onNewActivityOptions(options);
2504 }
2505 }
2506
Jose Lima4b6c6692014-08-12 17:41:12 -07002507 public void handleCancelVisibleBehind(IBinder token) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002508 ActivityClientRecord r = mActivities.get(token);
2509 if (r != null) {
Dianne Hackborn89ad4562014-08-24 16:45:38 -07002510 mSomeActivitiesChanged = true;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002511 final Activity activity = r.activity;
Jose Lima4b6c6692014-08-12 17:41:12 -07002512 if (activity.mVisibleBehind) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002513 activity.mCalled = false;
Jose Limafcf70832014-08-27 23:09:05 -07002514 activity.onVisibleBehindCanceled();
Craig Mautneree2e45a2014-06-27 12:10:03 -07002515 // Tick, tick, tick. The activity has 500 msec to return or it will be destroyed.
2516 if (!activity.mCalled) {
2517 throw new SuperNotCalledException("Activity " + activity.getLocalClassName() +
Jose Limafcf70832014-08-27 23:09:05 -07002518 " did not call through to super.onVisibleBehindCanceled()");
Craig Mautneree2e45a2014-06-27 12:10:03 -07002519 }
Jose Lima4b6c6692014-08-12 17:41:12 -07002520 activity.mVisibleBehind = false;
Craig Mautneree2e45a2014-06-27 12:10:03 -07002521 }
2522 }
2523 try {
Jose Lima4b6c6692014-08-12 17:41:12 -07002524 ActivityManagerNative.getDefault().backgroundResourcesReleased(token);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002525 } catch (RemoteException e) {
2526 }
2527 }
2528
Jose Lima4b6c6692014-08-12 17:41:12 -07002529 public void handleOnBackgroundVisibleBehindChanged(IBinder token, boolean visible) {
Craig Mautneree2e45a2014-06-27 12:10:03 -07002530 ActivityClientRecord r = mActivities.get(token);
2531 if (r != null) {
Jose Lima4b6c6692014-08-12 17:41:12 -07002532 r.activity.onBackgroundVisibleBehindChanged(visible);
Craig Mautneree2e45a2014-06-27 12:10:03 -07002533 }
2534 }
2535
Jeff Sharkeydd97f422013-10-08 17:01:30 -07002536 public void handleInstallProvider(ProviderInfo info) {
2537 installContentProviders(mInitialApplication, Lists.newArrayList(info));
2538 }
2539
Craig Mautner8746a472014-07-24 15:12:54 -07002540 private void handleEnterAnimationComplete(IBinder token) {
2541 ActivityClientRecord r = mActivities.get(token);
2542 if (r != null) {
2543 r.activity.onEnterAnimationComplete();
2544 }
2545 }
2546
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002547 private static final ThreadLocal<Intent> sCurrentBroadcastIntent = new ThreadLocal<Intent>();
2548
2549 /**
2550 * Return the Intent that's currently being handled by a
2551 * BroadcastReceiver on this thread, or null if none.
2552 * @hide
2553 */
2554 public static Intent getIntentBeingBroadcast() {
2555 return sCurrentBroadcastIntent.get();
2556 }
2557
Romain Guy65b345f2011-07-27 18:51:50 -07002558 private void handleReceiver(ReceiverData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002559 // If we are getting ready to gc after going to the background, well
2560 // we are back active so skip it.
2561 unscheduleGcIdler();
2562
2563 String component = data.intent.getComponent().getClassName();
2564
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002565 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002566 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002567
2568 IActivityManager mgr = ActivityManagerNative.getDefault();
2569
Romain Guy65b345f2011-07-27 18:51:50 -07002570 BroadcastReceiver receiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002571 try {
2572 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2573 data.intent.setExtrasClassLoader(cl);
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002574 data.intent.prepareToEnterProcess();
Dianne Hackborne829fef2010-10-26 17:44:01 -07002575 data.setExtrasClassLoader(cl);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 receiver = (BroadcastReceiver)cl.loadClass(component).newInstance();
2577 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002578 if (DEBUG_BROADCAST) Slog.i(TAG,
2579 "Finishing failed broadcast to " + data.intent.getComponent());
2580 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 throw new RuntimeException(
2582 "Unable to instantiate receiver " + component
2583 + ": " + e.toString(), e);
2584 }
2585
2586 try {
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002587 Application app = packageInfo.makeApplication(false, mInstrumentation);
Bob Leee5408332009-09-04 18:31:17 -07002588
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002589 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 TAG, "Performing receive of " + data.intent
2591 + ": app=" + app
2592 + ", appName=" + app.getPackageName()
2593 + ", pkg=" + packageInfo.getPackageName()
2594 + ", comp=" + data.intent.getComponent().toShortString()
2595 + ", dir=" + packageInfo.getAppDir());
2596
Dianne Hackborn21556372010-02-04 16:34:40 -08002597 ContextImpl context = (ContextImpl)app.getBaseContext();
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002598 sCurrentBroadcastIntent.set(data.intent);
Dianne Hackborne829fef2010-10-26 17:44:01 -07002599 receiver.setPendingResult(data);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 receiver.onReceive(context.getReceiverRestrictedContext(),
2601 data.intent);
2602 } catch (Exception e) {
Dianne Hackborne829fef2010-10-26 17:44:01 -07002603 if (DEBUG_BROADCAST) Slog.i(TAG,
2604 "Finishing failed broadcast to " + data.intent.getComponent());
2605 data.sendFinished(mgr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002606 if (!mInstrumentation.onException(receiver, e)) {
2607 throw new RuntimeException(
2608 "Unable to start receiver " + component
2609 + ": " + e.toString(), e);
2610 }
Brad Fitzpatrickbfb19192010-10-29 15:25:44 -07002611 } finally {
2612 sCurrentBroadcastIntent.set(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002613 }
2614
Dianne Hackborne829fef2010-10-26 17:44:01 -07002615 if (receiver.getPendingResult() != null) {
2616 data.finish();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 }
2618 }
2619
Christopher Tate181fafa2009-05-14 11:12:14 -07002620 // Instantiate a BackupAgent and tell it that it's alive
Romain Guy65b345f2011-07-27 18:51:50 -07002621 private void handleCreateBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002622 if (DEBUG_BACKUP) Slog.v(TAG, "handleCreateBackupAgent: " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002623
Christopher Tate346acb12012-10-15 19:20:25 -07002624 // Sanity check the requested target package's uid against ours
2625 try {
2626 PackageInfo requestedPackage = getPackageManager().getPackageInfo(
2627 data.appInfo.packageName, 0, UserHandle.myUserId());
2628 if (requestedPackage.applicationInfo.uid != Process.myUid()) {
2629 Slog.w(TAG, "Asked to instantiate non-matching package "
2630 + data.appInfo.packageName);
2631 return;
2632 }
2633 } catch (RemoteException e) {
2634 Slog.e(TAG, "Can't reach package manager", e);
2635 return;
2636 }
2637
Christopher Tate181fafa2009-05-14 11:12:14 -07002638 // no longer idle; we have backup work to do
2639 unscheduleGcIdler();
2640
2641 // instantiate the BackupAgent class named in the manifest
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002642 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002643 String packageName = packageInfo.mPackageName;
Christopher Tate346acb12012-10-15 19:20:25 -07002644 if (packageName == null) {
2645 Slog.d(TAG, "Asked to create backup agent for nonexistent package");
2646 return;
2647 }
2648
Christopher Tate181fafa2009-05-14 11:12:14 -07002649 String classname = data.appInfo.backupAgentName;
Christopher Tate79ec80d2011-06-24 14:58:49 -07002650 // full backup operation but no app-supplied agent? use the default implementation
2651 if (classname == null && (data.backupMode == IApplicationThread.BACKUP_MODE_FULL
2652 || data.backupMode == IApplicationThread.BACKUP_MODE_RESTORE_FULL)) {
Christopher Tate4a627c72011-04-01 14:43:32 -07002653 classname = "android.app.backup.FullBackupAgent";
Christopher Tate181fafa2009-05-14 11:12:14 -07002654 }
Christopher Tate4a627c72011-04-01 14:43:32 -07002655
Christopher Tate181fafa2009-05-14 11:12:14 -07002656 try {
Christopher Tated1475e02009-07-09 15:36:17 -07002657 IBinder binder = null;
Christopher Tate2e40d112014-07-15 12:37:38 -07002658 BackupAgent agent = mBackupAgents.get(packageName);
2659 if (agent != null) {
2660 // reusing the existing instance
2661 if (DEBUG_BACKUP) {
2662 Slog.v(TAG, "Reusing existing agent instance");
Christopher Tated1475e02009-07-09 15:36:17 -07002663 }
Christopher Tate2e40d112014-07-15 12:37:38 -07002664 binder = agent.onBind();
2665 } else {
2666 try {
2667 if (DEBUG_BACKUP) Slog.v(TAG, "Initializing agent class " + classname);
2668
2669 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2670 agent = (BackupAgent) cl.loadClass(classname).newInstance();
2671
2672 // set up the agent's context
2673 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2674 context.setOuterContext(agent);
2675 agent.attach(context);
2676
2677 agent.onCreate();
2678 binder = agent.onBind();
2679 mBackupAgents.put(packageName, agent);
2680 } catch (Exception e) {
2681 // If this is during restore, fail silently; otherwise go
2682 // ahead and let the user see the crash.
2683 Slog.e(TAG, "Agent threw during creation: " + e);
2684 if (data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE
2685 && data.backupMode != IApplicationThread.BACKUP_MODE_RESTORE_FULL) {
2686 throw e;
2687 }
2688 // falling through with 'binder' still null
2689 }
Christopher Tated1475e02009-07-09 15:36:17 -07002690 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002691
2692 // tell the OS that we're live now
Christopher Tate181fafa2009-05-14 11:12:14 -07002693 try {
2694 ActivityManagerNative.getDefault().backupAgentCreated(packageName, binder);
2695 } catch (RemoteException e) {
2696 // nothing to do.
2697 }
Christopher Tate181fafa2009-05-14 11:12:14 -07002698 } catch (Exception e) {
2699 throw new RuntimeException("Unable to create BackupAgent "
Christopher Tate4a627c72011-04-01 14:43:32 -07002700 + classname + ": " + e.toString(), e);
Christopher Tate181fafa2009-05-14 11:12:14 -07002701 }
2702 }
2703
2704 // Tear down a BackupAgent
Romain Guy65b345f2011-07-27 18:51:50 -07002705 private void handleDestroyBackupAgent(CreateBackupAgentData data) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002706 if (DEBUG_BACKUP) Slog.v(TAG, "handleDestroyBackupAgent: " + data);
Bob Leee5408332009-09-04 18:31:17 -07002707
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002708 LoadedApk packageInfo = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002709 String packageName = packageInfo.mPackageName;
2710 BackupAgent agent = mBackupAgents.get(packageName);
2711 if (agent != null) {
2712 try {
2713 agent.onDestroy();
2714 } catch (Exception e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002715 Slog.w(TAG, "Exception thrown in onDestroy by backup agent of " + data.appInfo);
Christopher Tate181fafa2009-05-14 11:12:14 -07002716 e.printStackTrace();
2717 }
2718 mBackupAgents.remove(packageName);
2719 } else {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08002720 Slog.w(TAG, "Attempt to destroy unknown backup agent " + data);
Christopher Tate181fafa2009-05-14 11:12:14 -07002721 }
2722 }
2723
Romain Guy65b345f2011-07-27 18:51:50 -07002724 private void handleCreateService(CreateServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725 // If we are getting ready to gc after going to the background, well
2726 // we are back active so skip it.
2727 unscheduleGcIdler();
2728
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002729 LoadedApk packageInfo = getPackageInfoNoCheck(
Dianne Hackborne2515ee2011-04-27 18:52:56 -04002730 data.info.applicationInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 Service service = null;
2732 try {
2733 java.lang.ClassLoader cl = packageInfo.getClassLoader();
2734 service = (Service) cl.loadClass(data.info.name).newInstance();
2735 } catch (Exception e) {
2736 if (!mInstrumentation.onException(service, e)) {
2737 throw new RuntimeException(
2738 "Unable to instantiate service " + data.info.name
2739 + ": " + e.toString(), e);
2740 }
2741 }
2742
2743 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002744 if (localLOGV) Slog.v(TAG, "Creating service " + data.info.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745
Jeff Browndefd4a62014-03-10 21:24:37 -07002746 ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
2747 context.setOuterContext(service);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748
Dianne Hackborn0be1f782009-11-09 12:30:12 -08002749 Application app = packageInfo.makeApplication(false, mInstrumentation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 service.attach(context, this, data.info.name, data.token, app,
2751 ActivityManagerNative.getDefault());
2752 service.onCreate();
2753 mServices.put(data.token, service);
2754 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002755 ActivityManagerNative.getDefault().serviceDoneExecuting(
2756 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 } catch (RemoteException e) {
2758 // nothing to do.
2759 }
2760 } catch (Exception e) {
2761 if (!mInstrumentation.onException(service, e)) {
2762 throw new RuntimeException(
2763 "Unable to create service " + data.info.name
2764 + ": " + e.toString(), e);
2765 }
2766 }
2767 }
2768
Romain Guy65b345f2011-07-27 18:51:50 -07002769 private void handleBindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770 Service s = mServices.get(data.token);
Amith Yamasani742a6712011-05-04 14:49:28 -07002771 if (DEBUG_SERVICE)
2772 Slog.v(TAG, "handleBindService s=" + s + " rebind=" + data.rebind);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002773 if (s != null) {
2774 try {
2775 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002776 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 try {
2778 if (!data.rebind) {
2779 IBinder binder = s.onBind(data.intent);
2780 ActivityManagerNative.getDefault().publishService(
2781 data.token, data.intent, binder);
2782 } else {
2783 s.onRebind(data.intent);
2784 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002785 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002787 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 } catch (RemoteException ex) {
2789 }
2790 } catch (Exception e) {
2791 if (!mInstrumentation.onException(s, e)) {
2792 throw new RuntimeException(
2793 "Unable to bind to service " + s
2794 + " with " + data.intent + ": " + e.toString(), e);
2795 }
2796 }
2797 }
2798 }
2799
Romain Guy65b345f2011-07-27 18:51:50 -07002800 private void handleUnbindService(BindServiceData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 Service s = mServices.get(data.token);
2802 if (s != null) {
2803 try {
2804 data.intent.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002805 data.intent.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 boolean doRebind = s.onUnbind(data.intent);
2807 try {
2808 if (doRebind) {
2809 ActivityManagerNative.getDefault().unbindFinished(
2810 data.token, data.intent, doRebind);
2811 } else {
2812 ActivityManagerNative.getDefault().serviceDoneExecuting(
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002813 data.token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 }
2815 } catch (RemoteException ex) {
2816 }
2817 } catch (Exception e) {
2818 if (!mInstrumentation.onException(s, e)) {
2819 throw new RuntimeException(
2820 "Unable to unbind to service " + s
2821 + " with " + data.intent + ": " + e.toString(), e);
2822 }
2823 }
2824 }
2825 }
2826
Dianne Hackborn625ac272010-09-17 18:29:22 -07002827 private void handleDumpService(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002828 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2829 try {
2830 Service s = mServices.get(info.token);
2831 if (s != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002832 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2833 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002834 s.dump(info.fd.getFileDescriptor(), pw, info.args);
2835 pw.flush();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002837 } finally {
2838 IoUtils.closeQuietly(info.fd);
2839 StrictMode.setThreadPolicy(oldPolicy);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002840 }
2841 }
2842
Dianne Hackborn625ac272010-09-17 18:29:22 -07002843 private void handleDumpActivity(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002844 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2845 try {
2846 ActivityClientRecord r = mActivities.get(info.token);
2847 if (r != null && r.activity != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002848 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2849 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002850 r.activity.dump(info.prefix, info.fd.getFileDescriptor(), pw, info.args);
2851 pw.flush();
Dianne Hackborn625ac272010-09-17 18:29:22 -07002852 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002853 } finally {
2854 IoUtils.closeQuietly(info.fd);
2855 StrictMode.setThreadPolicy(oldPolicy);
Dianne Hackborn625ac272010-09-17 18:29:22 -07002856 }
2857 }
2858
Marco Nelissen18cb2872011-11-15 11:19:53 -08002859 private void handleDumpProvider(DumpComponentInfo info) {
Jeff Sharkeye861b422012-03-01 20:59:22 -08002860 final StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
2861 try {
2862 ProviderClientRecord r = mLocalProviders.get(info.token);
2863 if (r != null && r.mLocalProvider != null) {
Dianne Hackborn8c841092013-06-24 13:46:13 -07002864 PrintWriter pw = new FastPrintWriter(new FileOutputStream(
2865 info.fd.getFileDescriptor()));
Jeff Sharkeye861b422012-03-01 20:59:22 -08002866 r.mLocalProvider.dump(info.fd.getFileDescriptor(), pw, info.args);
2867 pw.flush();
Marco Nelissen18cb2872011-11-15 11:19:53 -08002868 }
Jeff Sharkeye861b422012-03-01 20:59:22 -08002869 } finally {
2870 IoUtils.closeQuietly(info.fd);
2871 StrictMode.setThreadPolicy(oldPolicy);
Marco Nelissen18cb2872011-11-15 11:19:53 -08002872 }
2873 }
2874
Romain Guy65b345f2011-07-27 18:51:50 -07002875 private void handleServiceArgs(ServiceArgsData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 Service s = mServices.get(data.token);
2877 if (s != null) {
2878 try {
2879 if (data.args != null) {
2880 data.args.setExtrasClassLoader(s.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01002881 data.args.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002882 }
Dianne Hackborn0c5001d2011-04-12 18:16:08 -07002883 int res;
2884 if (!data.taskRemoved) {
2885 res = s.onStartCommand(data.args, data.flags, data.startId);
2886 } else {
2887 s.onTaskRemoved(data.args);
2888 res = Service.START_TASK_REMOVED_COMPLETE;
2889 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002890
2891 QueuedWork.waitToFinish();
2892
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002893 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002894 ActivityManagerNative.getDefault().serviceDoneExecuting(
2895 data.token, 1, data.startId, res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 } catch (RemoteException e) {
2897 // nothing to do.
2898 }
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08002899 ensureJitEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 } catch (Exception e) {
2901 if (!mInstrumentation.onException(s, e)) {
2902 throw new RuntimeException(
2903 "Unable to start service " + s
2904 + " with " + data.args + ": " + e.toString(), e);
2905 }
2906 }
2907 }
2908 }
2909
Romain Guy65b345f2011-07-27 18:51:50 -07002910 private void handleStopService(IBinder token) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 Service s = mServices.remove(token);
2912 if (s != null) {
2913 try {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002914 if (localLOGV) Slog.v(TAG, "Destroying service " + s);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 s.onDestroy();
2916 Context context = s.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08002917 if (context instanceof ContextImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 final String who = s.getClassName();
Dianne Hackborn21556372010-02-04 16:34:40 -08002919 ((ContextImpl) context).scheduleFinalCleanup(who, "Service");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002920 }
Brad Fitzpatrick333b8cb2010-08-26 12:04:57 -07002921
2922 QueuedWork.waitToFinish();
2923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002924 try {
Dianne Hackbornf6f9f2d2009-08-21 16:26:03 -07002925 ActivityManagerNative.getDefault().serviceDoneExecuting(
2926 token, 0, 0, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002927 } catch (RemoteException e) {
2928 // nothing to do.
2929 }
2930 } catch (Exception e) {
2931 if (!mInstrumentation.onException(s, e)) {
2932 throw new RuntimeException(
2933 "Unable to stop service " + s
2934 + ": " + e.toString(), e);
2935 }
2936 }
2937 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002938 //Slog.i(TAG, "Running services: " + mServices);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 }
2940
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002941 public final ActivityClientRecord performResumeActivity(IBinder token,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 boolean clearHide) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07002943 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07002944 if (localLOGV) Slog.v(TAG, "Performing resume of " + r
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 + " finished=" + r.activity.mFinished);
2946 if (r != null && !r.activity.mFinished) {
2947 if (clearHide) {
2948 r.hideForNow = false;
2949 r.activity.mStartedActivity = false;
2950 }
2951 try {
Dianne Hackborn689586d2012-10-01 18:23:04 -07002952 r.activity.mFragments.noteStateNotSaved();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002953 if (r.pendingIntents != null) {
2954 deliverNewIntents(r, r.pendingIntents);
2955 r.pendingIntents = null;
2956 }
2957 if (r.pendingResults != null) {
2958 deliverResults(r, r.pendingResults);
2959 r.pendingResults = null;
2960 }
2961 r.activity.performResume();
2962
Bob Leee5408332009-09-04 18:31:17 -07002963 EventLog.writeEvent(LOG_ON_RESUME_CALLED,
Dianne Hackbornb12e1352012-09-26 11:39:20 -07002964 UserHandle.myUserId(), r.activity.getComponentName().getClassName());
Bob Leee5408332009-09-04 18:31:17 -07002965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002966 r.paused = false;
2967 r.stopped = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 r.state = null;
Craig Mautnera0026042014-04-23 11:45:37 -07002969 r.persistentState = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 } catch (Exception e) {
2971 if (!mInstrumentation.onException(r.activity, e)) {
2972 throw new RuntimeException(
2973 "Unable to resume activity "
2974 + r.intent.getComponent().toShortString()
2975 + ": " + e.toString(), e);
2976 }
2977 }
2978 }
2979 return r;
2980 }
2981
Romain Guya998dff2012-03-23 18:58:36 -07002982 static final void cleanUpPendingRemoveWindows(ActivityClientRecord r) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002983 if (r.mPendingRemoveWindow != null) {
2984 r.mPendingRemoveWindowManager.removeViewImmediate(r.mPendingRemoveWindow);
2985 IBinder wtoken = r.mPendingRemoveWindow.getWindowToken();
2986 if (wtoken != null) {
Jeff Brown98365d72012-08-19 20:30:52 -07002987 WindowManagerGlobal.getInstance().closeAll(wtoken,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08002988 r.activity.getClass().getName(), "Activity");
2989 }
2990 }
2991 r.mPendingRemoveWindow = null;
2992 r.mPendingRemoveWindowManager = null;
2993 }
2994
Craig Mautner233ceee2014-05-09 17:05:11 -07002995 final void handleResumeActivity(IBinder token,
Adam Powellcfbe9be2013-11-06 14:58:58 -08002996 boolean clearHide, boolean isForward, boolean reallyResume) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 // If we are getting ready to gc after going to the background, well
2998 // we are back active so skip it.
2999 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003000 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001
Adam Powellcfbe9be2013-11-06 14:58:58 -08003002 // TODO Push resumeArgs into the activity for consideration
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003003 ActivityClientRecord r = performResumeActivity(token, clearHide);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004
3005 if (r != null) {
3006 final Activity a = r.activity;
3007
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003008 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003009 TAG, "Resume " + r + " started activity: " +
3010 a.mStartedActivity + ", hideForNow: " + r.hideForNow
3011 + ", finished: " + a.mFinished);
3012
3013 final int forwardBit = isForward ?
3014 WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;
Bob Leee5408332009-09-04 18:31:17 -07003015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 // If the window hasn't yet been added to the window manager,
3017 // and this guy didn't finish itself or start another activity,
3018 // then go ahead and add the window.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003019 boolean willBeVisible = !a.mStartedActivity;
3020 if (!willBeVisible) {
3021 try {
3022 willBeVisible = ActivityManagerNative.getDefault().willActivityBeVisible(
3023 a.getActivityToken());
3024 } catch (RemoteException e) {
3025 }
3026 }
3027 if (r.window == null && !a.mFinished && willBeVisible) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 r.window = r.activity.getWindow();
3029 View decor = r.window.getDecorView();
3030 decor.setVisibility(View.INVISIBLE);
3031 ViewManager wm = a.getWindowManager();
3032 WindowManager.LayoutParams l = r.window.getAttributes();
3033 a.mDecor = decor;
3034 l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
3035 l.softInputMode |= forwardBit;
3036 if (a.mVisibleFromClient) {
3037 a.mWindowAdded = true;
3038 wm.addView(decor, l);
3039 }
3040
3041 // If the window has already been added, but during resume
3042 // we started another activity, then don't yet make the
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003043 // window visible.
3044 } else if (!willBeVisible) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003045 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003046 TAG, "Launch " + r + " mStartedActivity set");
3047 r.hideForNow = true;
3048 }
3049
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003050 // Get rid of anything left hanging around.
3051 cleanUpPendingRemoveWindows(r);
3052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003053 // The window is now visible if it has been added, we are not
3054 // simply finishing, and we are not starting another activity.
Dianne Hackborn061d58a2010-03-12 15:07:06 -08003055 if (!r.activity.mFinished && willBeVisible
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003056 && r.activity.mDecor != null && !r.hideForNow) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003058 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Resuming activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003059 + r.activityInfo.name + " with newConfig " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003061 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 r.newConfig = null;
3063 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003064 if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003065 + isForward);
3066 WindowManager.LayoutParams l = r.window.getAttributes();
3067 if ((l.softInputMode
3068 & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)
3069 != forwardBit) {
3070 l.softInputMode = (l.softInputMode
3071 & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))
3072 | forwardBit;
Dianne Hackbornc1e605e2009-09-25 17:18:15 -07003073 if (r.activity.mVisibleFromClient) {
3074 ViewManager wm = a.getWindowManager();
3075 View decor = r.window.getDecorView();
3076 wm.updateViewLayout(decor, l);
3077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
3079 r.activity.mVisibleFromServer = true;
3080 mNumVisibleActivities++;
3081 if (r.activity.mVisibleFromClient) {
3082 r.activity.makeVisible();
3083 }
3084 }
3085
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003086 if (!r.onlyLocalRequest) {
3087 r.nextIdle = mNewActivities;
3088 mNewActivities = r;
3089 if (localLOGV) Slog.v(
3090 TAG, "Scheduling idle handler for " + r);
3091 Looper.myQueue().addIdleHandler(new Idler());
3092 }
3093 r.onlyLocalRequest = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003095 // Tell the activity manager we have resumed.
3096 if (reallyResume) {
3097 try {
3098 ActivityManagerNative.getDefault().activityResumed(token);
3099 } catch (RemoteException ex) {
3100 }
3101 }
3102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003103 } else {
3104 // If an exception was thrown when trying to resume, then
3105 // just end this activity.
3106 try {
3107 ActivityManagerNative.getDefault()
Winson Chung3b3f4642014-04-22 10:08:18 -07003108 .finishActivity(token, Activity.RESULT_CANCELED, null, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 } catch (RemoteException ex) {
3110 }
3111 }
3112 }
3113
3114 private int mThumbnailWidth = -1;
3115 private int mThumbnailHeight = -1;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003116 private Bitmap mAvailThumbnailBitmap = null;
3117 private Canvas mThumbnailCanvas = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003118
Romain Guy65b345f2011-07-27 18:51:50 -07003119 private Bitmap createThumbnailBitmap(ActivityClientRecord r) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003120 Bitmap thumbnail = mAvailThumbnailBitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003121 try {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003122 if (thumbnail == null) {
3123 int w = mThumbnailWidth;
3124 int h;
3125 if (w < 0) {
3126 Resources res = r.activity.getResources();
Winson Chung6784f1c2014-05-22 11:22:41 -07003127 int wId = com.android.internal.R.dimen.thumbnail_width;
3128 int hId = com.android.internal.R.dimen.thumbnail_height;
Winson Chungfd086222014-05-13 15:49:42 -07003129 mThumbnailWidth = w = res.getDimensionPixelSize(wId);
3130 mThumbnailHeight = h = res.getDimensionPixelSize(hId);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003131 } else {
3132 h = mThumbnailHeight;
3133 }
3134
3135 // On platforms where we don't want thumbnails, set dims to (0,0)
3136 if ((w > 0) && (h > 0)) {
Dianne Hackborndde331c2012-08-03 14:01:57 -07003137 thumbnail = Bitmap.createBitmap(r.activity.getResources().getDisplayMetrics(),
3138 w, h, THUMBNAIL_FORMAT);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003139 thumbnail.eraseColor(0);
3140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003141 }
3142
Dianne Hackbornfb3806d2010-12-09 13:14:12 -08003143 if (thumbnail != null) {
3144 Canvas cv = mThumbnailCanvas;
3145 if (cv == null) {
3146 mThumbnailCanvas = cv = new Canvas();
3147 }
3148
3149 cv.setBitmap(thumbnail);
3150 if (!r.activity.onCreateThumbnail(thumbnail, cv)) {
3151 mAvailThumbnailBitmap = thumbnail;
3152 thumbnail = null;
3153 }
Dianne Hackborn6311d0a2011-08-02 16:37:58 -07003154 cv.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003155 }
Jim Miller0b2a6d02010-07-13 18:01:29 -07003156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003157 } catch (Exception e) {
3158 if (!mInstrumentation.onException(r.activity, e)) {
3159 throw new RuntimeException(
3160 "Unable to create thumbnail of "
3161 + r.intent.getComponent().toShortString()
3162 + ": " + e.toString(), e);
3163 }
3164 thumbnail = null;
3165 }
3166
3167 return thumbnail;
3168 }
3169
Romain Guy65b345f2011-07-27 18:51:50 -07003170 private void handlePauseActivity(IBinder token, boolean finished,
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003171 boolean userLeaving, int configChanges, boolean dontReport) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003172 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 if (r != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003174 //Slog.v(TAG, "userLeaving=" + userLeaving + " handling pause of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003175 if (userLeaving) {
3176 performUserLeavingActivity(r);
3177 }
Bob Leee5408332009-09-04 18:31:17 -07003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003180 performPauseActivity(token, finished, r.isPreHoneycomb());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003181
Dianne Hackbornaa93bcd2010-10-27 13:57:00 -07003182 // Make sure any pending writes are now committed.
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003183 if (r.isPreHoneycomb()) {
3184 QueuedWork.waitToFinish();
3185 }
Dianne Hackbornad9b32112012-09-17 15:35:01 -07003186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003187 // Tell the activity manager we have paused.
Dianne Hackborna4e102e2014-09-04 22:52:27 -07003188 if (!dontReport) {
3189 try {
3190 ActivityManagerNative.getDefault().activityPaused(token);
3191 } catch (RemoteException ex) {
3192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003194 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003195 }
3196 }
3197
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003198 final void performUserLeavingActivity(ActivityClientRecord r) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003199 mInstrumentation.callActivityOnUserLeaving(r.activity);
3200 }
3201
3202 final Bundle performPauseActivity(IBinder token, boolean finished,
3203 boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003204 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003205 return r != null ? performPauseActivity(r, finished, saveState) : null;
3206 }
3207
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003208 final Bundle performPauseActivity(ActivityClientRecord r, boolean finished,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 boolean saveState) {
3210 if (r.paused) {
3211 if (r.activity.mFinished) {
3212 // If we are finishing, we won't call onResume() in certain cases.
3213 // So here we likewise don't want to call onPause() if the activity
3214 // isn't resumed.
3215 return null;
3216 }
3217 RuntimeException e = new RuntimeException(
3218 "Performing pause of activity that is not resumed: "
3219 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003220 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003222 if (finished) {
3223 r.activity.mFinished = true;
3224 }
3225 try {
3226 // Next have the activity save its current state and managed dialogs...
3227 if (!r.activity.mFinished && saveState) {
Craig Mautnera0026042014-04-23 11:45:37 -07003228 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 }
3230 // Now we are idle.
3231 r.activity.mCalled = false;
3232 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003233 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
3234 r.activity.getComponentName().getClassName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 if (!r.activity.mCalled) {
3236 throw new SuperNotCalledException(
3237 "Activity " + r.intent.getComponent().toShortString() +
3238 " did not call through to super.onPause()");
3239 }
3240
3241 } catch (SuperNotCalledException e) {
3242 throw e;
3243
3244 } catch (Exception e) {
3245 if (!mInstrumentation.onException(r.activity, e)) {
3246 throw new RuntimeException(
3247 "Unable to pause activity "
3248 + r.intent.getComponent().toShortString()
3249 + ": " + e.toString(), e);
3250 }
3251 }
3252 r.paused = true;
Jeff Hamilton52d32032011-01-08 15:31:26 -06003253
3254 // Notify any outstanding on paused listeners
3255 ArrayList<OnActivityPausedListener> listeners;
3256 synchronized (mOnPauseListeners) {
3257 listeners = mOnPauseListeners.remove(r.activity);
3258 }
3259 int size = (listeners != null ? listeners.size() : 0);
3260 for (int i = 0; i < size; i++) {
3261 listeners.get(i).onPaused(r.activity);
3262 }
3263
Craig Mautnera0026042014-04-23 11:45:37 -07003264 return !r.activity.mFinished && saveState ? r.state : null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003267 final void performStopActivity(IBinder token, boolean saveState) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003268 ActivityClientRecord r = mActivities.get(token);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003269 performStopActivityInner(r, null, false, saveState);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003270 }
3271
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003272 private static class StopInfo implements Runnable {
3273 ActivityClientRecord activity;
3274 Bundle state;
Craig Mautnera0026042014-04-23 11:45:37 -07003275 PersistableBundle persistentState;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 CharSequence description;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003277
3278 @Override public void run() {
3279 // Tell activity manager we have been stopped.
3280 try {
3281 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
3282 ActivityManagerNative.getDefault().activityStopped(
Craig Mautnera0026042014-04-23 11:45:37 -07003283 activity.token, state, persistentState, description);
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003284 } catch (RemoteException ex) {
3285 }
3286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003287 }
3288
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003289 private static final class ProviderRefCount {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003290 public final IActivityManager.ContentProviderHolder holder;
3291 public final ProviderClientRecord client;
3292 public int stableCount;
3293 public int unstableCount;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08003294
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07003295 // When this is set, the stable and unstable ref counts are 0 and
3296 // we have a pending operation scheduled to remove the ref count
3297 // from the activity manager. On the activity manager we are still
3298 // holding an unstable ref, though it is not reflected in the counts
3299 // here.
3300 public boolean removePending;
3301
3302 ProviderRefCount(IActivityManager.ContentProviderHolder inHolder,
3303 ProviderClientRecord inClient, int sCount, int uCount) {
3304 holder = inHolder;
3305 client = inClient;
3306 stableCount = sCount;
3307 unstableCount = uCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003308 }
3309 }
3310
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003311 /**
3312 * Core implementation of stopping an activity. Note this is a little
3313 * tricky because the server's meaning of stop is slightly different
3314 * than our client -- for the server, stop means to save state and give
3315 * it the result when it is done, but the window may still be visible.
3316 * For the client, we want to call onStop()/onStart() to indicate when
3317 * the activity's UI visibillity changes.
3318 */
Romain Guy65b345f2011-07-27 18:51:50 -07003319 private void performStopActivityInner(ActivityClientRecord r,
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003320 StopInfo info, boolean keepShown, boolean saveState) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003321 if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 if (r != null) {
3323 if (!keepShown && r.stopped) {
3324 if (r.activity.mFinished) {
3325 // If we are finishing, we won't call onResume() in certain
3326 // cases. So here we likewise don't want to call onStop()
3327 // if the activity isn't resumed.
3328 return;
3329 }
3330 RuntimeException e = new RuntimeException(
3331 "Performing stop of activity that is not resumed: "
3332 + r.intent.getComponent().toShortString());
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08003333 Slog.e(TAG, e.getMessage(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003334 }
3335
3336 if (info != null) {
3337 try {
3338 // First create a thumbnail for the activity...
Dianne Hackborn0500b3c2011-11-01 15:28:43 -07003339 // For now, don't create the thumbnail here; we are
3340 // doing that by doing a screen snapshot.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 info.description = r.activity.onCreateDescription();
3342 } catch (Exception e) {
3343 if (!mInstrumentation.onException(r.activity, e)) {
3344 throw new RuntimeException(
3345 "Unable to save state of activity "
3346 + r.intent.getComponent().toShortString()
3347 + ": " + e.toString(), e);
3348 }
3349 }
3350 }
3351
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003352 // Next have the activity save its current state and managed dialogs...
3353 if (!r.activity.mFinished && saveState) {
3354 if (r.state == null) {
Craig Mautnera0026042014-04-23 11:45:37 -07003355 callCallActivityOnSaveInstanceState(r);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003356 }
3357 }
3358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003359 if (!keepShown) {
3360 try {
3361 // Now we are idle.
3362 r.activity.performStop();
3363 } catch (Exception e) {
3364 if (!mInstrumentation.onException(r.activity, e)) {
3365 throw new RuntimeException(
3366 "Unable to stop activity "
3367 + r.intent.getComponent().toShortString()
3368 + ": " + e.toString(), e);
3369 }
3370 }
3371 r.stopped = true;
3372 }
3373
3374 r.paused = true;
3375 }
3376 }
3377
Romain Guy65b345f2011-07-27 18:51:50 -07003378 private void updateVisibility(ActivityClientRecord r, boolean show) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003379 View v = r.activity.mDecor;
3380 if (v != null) {
3381 if (show) {
3382 if (!r.activity.mVisibleFromServer) {
3383 r.activity.mVisibleFromServer = true;
3384 mNumVisibleActivities++;
3385 if (r.activity.mVisibleFromClient) {
3386 r.activity.makeVisible();
3387 }
3388 }
3389 if (r.newConfig != null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003390 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Updating activity vis "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003391 + r.activityInfo.name + " with new config " + r.newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 performConfigurationChanged(r.activity, r.newConfig);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07003393 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(r.newConfig));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 r.newConfig = null;
3395 }
3396 } else {
3397 if (r.activity.mVisibleFromServer) {
3398 r.activity.mVisibleFromServer = false;
3399 mNumVisibleActivities--;
3400 v.setVisibility(View.INVISIBLE);
3401 }
3402 }
3403 }
3404 }
3405
Romain Guy65b345f2011-07-27 18:51:50 -07003406 private void handleStopActivity(IBinder token, boolean show, int configChanges) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003407 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408 r.activity.mConfigChangeFlags |= configChanges;
3409
3410 StopInfo info = new StopInfo();
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003411 performStopActivityInner(r, info, show, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003412
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003413 if (localLOGV) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003414 TAG, "Finishing stop of " + r + ": show=" + show
3415 + " win=" + r.window);
3416
3417 updateVisibility(r, show);
Bob Leee5408332009-09-04 18:31:17 -07003418
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003419 // Make sure any pending writes are now committed.
3420 if (!r.isPreHoneycomb()) {
3421 QueuedWork.waitToFinish();
3422 }
3423
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003424 // Schedule the call to tell the activity manager we have
3425 // stopped. We don't do this immediately, because we want to
3426 // have a chance for any other pending work (in particular memory
3427 // trim requests) to complete before you tell the activity
3428 // manager to proceed and allow us to go fully into the background.
3429 info.activity = r;
3430 info.state = r.state;
Craig Mautnera0026042014-04-23 11:45:37 -07003431 info.persistentState = r.persistentState;
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07003432 mH.post(info);
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003433 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 }
3435
3436 final void performRestartActivity(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003437 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 if (r.stopped) {
3439 r.activity.performRestart();
3440 r.stopped = false;
3441 }
3442 }
3443
Romain Guy65b345f2011-07-27 18:51:50 -07003444 private void handleWindowVisibility(IBinder token, boolean show) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003445 ActivityClientRecord r = mActivities.get(token);
Dianne Hackbornbfddc0f2010-12-14 11:28:01 -08003446
3447 if (r == null) {
3448 Log.w(TAG, "handleWindowVisibility: no activity for token " + token);
3449 return;
3450 }
3451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003452 if (!show && !r.stopped) {
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003453 performStopActivityInner(r, null, show, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003454 } else if (show && r.stopped) {
3455 // If we are getting ready to gc after going to the background, well
3456 // we are back active so skip it.
3457 unscheduleGcIdler();
3458
3459 r.activity.performRestart();
3460 r.stopped = false;
3461 }
3462 if (r.activity.mDecor != null) {
Joe Onorato43a17652011-04-06 19:22:23 -07003463 if (false) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 TAG, "Handle window " + r + " visibility: " + show);
3465 updateVisibility(r, show);
3466 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003467 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003468 }
3469
Romain Guy65b345f2011-07-27 18:51:50 -07003470 private void handleSleeping(IBinder token, boolean sleeping) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003471 ActivityClientRecord r = mActivities.get(token);
3472
3473 if (r == null) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003474 Log.w(TAG, "handleSleeping: no activity for token " + token);
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003475 return;
3476 }
3477
3478 if (sleeping) {
Dianne Hackborn842e04b2011-01-22 13:00:12 -08003479 if (!r.stopped && !r.isPreHoneycomb()) {
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003480 try {
3481 // Now we are idle.
3482 r.activity.performStop();
3483 } catch (Exception e) {
3484 if (!mInstrumentation.onException(r.activity, e)) {
3485 throw new RuntimeException(
3486 "Unable to stop activity "
3487 + r.intent.getComponent().toShortString()
3488 + ": " + e.toString(), e);
3489 }
3490 }
3491 r.stopped = true;
3492 }
Dianne Hackborn5d9d03a2011-01-24 13:15:09 -08003493
3494 // Make sure any pending writes are now committed.
3495 if (!r.isPreHoneycomb()) {
3496 QueuedWork.waitToFinish();
3497 }
3498
Dianne Hackborn4eba96b2011-01-21 13:34:36 -08003499 // Tell activity manager we slept.
3500 try {
3501 ActivityManagerNative.getDefault().activitySlept(r.token);
3502 } catch (RemoteException ex) {
3503 }
3504 } else {
3505 if (r.stopped && r.activity.mVisibleFromServer) {
3506 r.activity.performRestart();
3507 r.stopped = false;
3508 }
3509 }
3510 }
3511
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003512 private void handleSetCoreSettings(Bundle coreSettings) {
Craig Mautner88c05892013-06-28 09:47:45 -07003513 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08003514 mCoreSettings = coreSettings;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003515 }
Jon Miranda836c0a82014-08-11 12:32:26 -07003516 onCoreSettingsChange();
3517 }
3518
3519 private void onCoreSettingsChange() {
3520 boolean debugViewAttributes =
3521 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
3522 if (debugViewAttributes != View.mDebugViewAttributes) {
3523 View.mDebugViewAttributes = debugViewAttributes;
3524
3525 // request all activities to relaunch for the changes to take place
3526 for (Map.Entry<IBinder, ActivityClientRecord> entry : mActivities.entrySet()) {
3527 requestRelaunchActivity(entry.getKey(), null, null, 0, false, null, false);
3528 }
3529 }
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08003530 }
3531
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003532 private void handleUpdatePackageCompatibilityInfo(UpdateCompatibilityData data) {
3533 LoadedApk apk = peekPackageInfo(data.pkg, false);
3534 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003535 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003536 }
3537 apk = peekPackageInfo(data.pkg, true);
3538 if (apk != null) {
Craig Mautner48d0d182013-06-11 07:53:06 -07003539 apk.setCompatibilityInfo(data.info);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003540 }
3541 handleConfigurationChanged(mConfiguration, data.info);
Jeff Brown98365d72012-08-19 20:30:52 -07003542 WindowManagerGlobal.getInstance().reportNewConfiguration(mConfiguration);
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003543 }
3544
Romain Guy65b345f2011-07-27 18:51:50 -07003545 private void deliverResults(ActivityClientRecord r, List<ResultInfo> results) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003546 final int N = results.size();
3547 for (int i=0; i<N; i++) {
3548 ResultInfo ri = results.get(i);
3549 try {
3550 if (ri.mData != null) {
3551 ri.mData.setExtrasClassLoader(r.activity.getClassLoader());
Nicolas Prevotd1c99b12014-07-04 16:56:17 +01003552 ri.mData.prepareToEnterProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003553 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003554 if (DEBUG_RESULTS) Slog.v(TAG,
Chris Tate8a7dc172009-03-24 20:11:42 -07003555 "Delivering result to activity " + r + " : " + ri);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003556 r.activity.dispatchActivityResult(ri.mResultWho,
3557 ri.mRequestCode, ri.mResultCode, ri.mData);
3558 } catch (Exception e) {
3559 if (!mInstrumentation.onException(r.activity, e)) {
3560 throw new RuntimeException(
3561 "Failure delivering result " + ri + " to activity "
3562 + r.intent.getComponent().toShortString()
3563 + ": " + e.toString(), e);
3564 }
3565 }
3566 }
3567 }
3568
Romain Guy65b345f2011-07-27 18:51:50 -07003569 private void handleSendResult(ResultData res) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003570 ActivityClientRecord r = mActivities.get(res.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003571 if (DEBUG_RESULTS) Slog.v(TAG, "Handling send result to " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003572 if (r != null) {
3573 final boolean resumed = !r.paused;
3574 if (!r.activity.mFinished && r.activity.mDecor != null
3575 && r.hideForNow && resumed) {
3576 // We had hidden the activity because it started another
3577 // one... we have gotten a result back and we are not
3578 // paused, so make sure our window is visible.
3579 updateVisibility(r, true);
3580 }
3581 if (resumed) {
3582 try {
3583 // Now we are idle.
3584 r.activity.mCalled = false;
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003585 r.activity.mTemporaryPause = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003586 mInstrumentation.callActivityOnPause(r.activity);
3587 if (!r.activity.mCalled) {
3588 throw new SuperNotCalledException(
3589 "Activity " + r.intent.getComponent().toShortString()
3590 + " did not call through to super.onPause()");
3591 }
3592 } catch (SuperNotCalledException e) {
3593 throw e;
3594 } catch (Exception e) {
3595 if (!mInstrumentation.onException(r.activity, e)) {
3596 throw new RuntimeException(
3597 "Unable to pause activity "
3598 + r.intent.getComponent().toShortString()
3599 + ": " + e.toString(), e);
3600 }
3601 }
3602 }
3603 deliverResults(r, res.results);
3604 if (resumed) {
Dianne Hackbornb46ed762011-06-02 18:33:15 -07003605 r.activity.performResume();
Dianne Hackbornfb3cffe2010-10-25 17:08:56 -07003606 r.activity.mTemporaryPause = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003607 }
3608 }
3609 }
3610
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003611 public final ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 return performDestroyActivity(token, finishing, 0, false);
3613 }
3614
Romain Guy65b345f2011-07-27 18:51:50 -07003615 private ActivityClientRecord performDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003616 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003617 ActivityClientRecord r = mActivities.get(token);
Craig Mautner88c05892013-06-28 09:47:45 -07003618 Class<? extends Activity> activityClass = null;
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003619 if (localLOGV) Slog.v(TAG, "Performing finish of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 if (r != null) {
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003621 activityClass = r.activity.getClass();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003622 r.activity.mConfigChangeFlags |= configChanges;
3623 if (finishing) {
3624 r.activity.mFinished = true;
3625 }
3626 if (!r.paused) {
3627 try {
3628 r.activity.mCalled = false;
3629 mInstrumentation.callActivityOnPause(r.activity);
Dianne Hackbornb12e1352012-09-26 11:39:20 -07003630 EventLog.writeEvent(LOG_ON_PAUSE_CALLED, UserHandle.myUserId(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 r.activity.getComponentName().getClassName());
3632 if (!r.activity.mCalled) {
3633 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003634 "Activity " + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003635 + " did not call through to super.onPause()");
3636 }
3637 } catch (SuperNotCalledException e) {
3638 throw e;
3639 } catch (Exception e) {
3640 if (!mInstrumentation.onException(r.activity, e)) {
3641 throw new RuntimeException(
3642 "Unable to pause activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003643 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 + ": " + e.toString(), e);
3645 }
3646 }
3647 r.paused = true;
3648 }
3649 if (!r.stopped) {
3650 try {
3651 r.activity.performStop();
3652 } catch (SuperNotCalledException e) {
3653 throw e;
3654 } catch (Exception e) {
3655 if (!mInstrumentation.onException(r.activity, e)) {
3656 throw new RuntimeException(
3657 "Unable to stop activity "
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003658 + safeToComponentShortString(r.intent)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003659 + ": " + e.toString(), e);
3660 }
3661 }
3662 r.stopped = true;
3663 }
3664 if (getNonConfigInstance) {
3665 try {
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003666 r.lastNonConfigurationInstances
3667 = r.activity.retainNonConfigurationInstances();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003668 } catch (Exception e) {
3669 if (!mInstrumentation.onException(r.activity, e)) {
3670 throw new RuntimeException(
3671 "Unable to retain activity "
3672 + r.intent.getComponent().toShortString()
3673 + ": " + e.toString(), e);
3674 }
3675 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 }
3677 try {
3678 r.activity.mCalled = false;
Dianne Hackborn2dedce62010-04-15 14:45:25 -07003679 mInstrumentation.callActivityOnDestroy(r.activity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003680 if (!r.activity.mCalled) {
3681 throw new SuperNotCalledException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003682 "Activity " + safeToComponentShortString(r.intent) +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 " did not call through to super.onDestroy()");
3684 }
3685 if (r.window != null) {
3686 r.window.closeAllPanels();
3687 }
3688 } catch (SuperNotCalledException e) {
3689 throw e;
3690 } catch (Exception e) {
3691 if (!mInstrumentation.onException(r.activity, e)) {
3692 throw new RuntimeException(
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003693 "Unable to destroy activity " + safeToComponentShortString(r.intent)
3694 + ": " + e.toString(), e);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003695 }
3696 }
3697 }
3698 mActivities.remove(token);
Brad Fitzpatrick5f8b5c12011-01-20 15:12:08 -08003699 StrictMode.decrementExpectedActivityCount(activityClass);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003700 return r;
3701 }
3702
Mitsuru Oshimad9aef732009-06-16 20:20:50 -07003703 private static String safeToComponentShortString(Intent intent) {
3704 ComponentName component = intent.getComponent();
3705 return component == null ? "[Unknown]" : component.toShortString();
3706 }
3707
Romain Guy65b345f2011-07-27 18:51:50 -07003708 private void handleDestroyActivity(IBinder token, boolean finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 int configChanges, boolean getNonConfigInstance) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003710 ActivityClientRecord r = performDestroyActivity(token, finishing,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003711 configChanges, getNonConfigInstance);
3712 if (r != null) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003713 cleanUpPendingRemoveWindows(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003714 WindowManager wm = r.activity.getWindowManager();
3715 View v = r.activity.mDecor;
3716 if (v != null) {
3717 if (r.activity.mVisibleFromServer) {
3718 mNumVisibleActivities--;
3719 }
3720 IBinder wtoken = v.getWindowToken();
3721 if (r.activity.mWindowAdded) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003722 if (r.onlyLocalRequest) {
3723 // Hold off on removing this until the new activity's
3724 // window is being added.
3725 r.mPendingRemoveWindow = v;
3726 r.mPendingRemoveWindowManager = wm;
3727 } else {
3728 wm.removeViewImmediate(v);
3729 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003731 if (wtoken != null && r.mPendingRemoveWindow == null) {
Jeff Brown98365d72012-08-19 20:30:52 -07003732 WindowManagerGlobal.getInstance().closeAll(wtoken,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003733 r.activity.getClass().getName(), "Activity");
3734 }
3735 r.activity.mDecor = null;
3736 }
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003737 if (r.mPendingRemoveWindow == null) {
3738 // If we are delaying the removal of the activity window, then
3739 // we can't clean up all windows here. Note that we can't do
3740 // so later either, which means any windows that aren't closed
3741 // by the app will leak. Well we try to warning them a lot
3742 // about leaking windows, because that is a bug, so if they are
3743 // using this recreate facility then they get to live with leaks.
Jeff Brown98365d72012-08-19 20:30:52 -07003744 WindowManagerGlobal.getInstance().closeAll(token,
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003745 r.activity.getClass().getName(), "Activity");
3746 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003747
3748 // Mocked out contexts won't be participating in the normal
3749 // process lifecycle, but if we're running with a proper
3750 // ApplicationContext we need to have it tear down things
3751 // cleanly.
3752 Context c = r.activity.getBaseContext();
Dianne Hackborn21556372010-02-04 16:34:40 -08003753 if (c instanceof ContextImpl) {
3754 ((ContextImpl) c).scheduleFinalCleanup(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003755 r.activity.getClass().getName(), "Activity");
3756 }
3757 }
3758 if (finishing) {
3759 try {
3760 ActivityManagerNative.getDefault().activityDestroyed(token);
3761 } catch (RemoteException ex) {
3762 // If the system process has died, it's game over for everyone.
3763 }
3764 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003765 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003766 }
3767
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003768 public final void requestRelaunchActivity(IBinder token,
3769 List<ResultInfo> pendingResults, List<Intent> pendingNewIntents,
3770 int configChanges, boolean notResumed, Configuration config,
3771 boolean fromServer) {
3772 ActivityClientRecord target = null;
3773
Craig Mautner88c05892013-06-28 09:47:45 -07003774 synchronized (mResourcesManager) {
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003775 for (int i=0; i<mRelaunchingActivities.size(); i++) {
3776 ActivityClientRecord r = mRelaunchingActivities.get(i);
3777 if (r.token == token) {
3778 target = r;
3779 if (pendingResults != null) {
3780 if (r.pendingResults != null) {
3781 r.pendingResults.addAll(pendingResults);
3782 } else {
3783 r.pendingResults = pendingResults;
3784 }
3785 }
3786 if (pendingNewIntents != null) {
3787 if (r.pendingIntents != null) {
3788 r.pendingIntents.addAll(pendingNewIntents);
3789 } else {
3790 r.pendingIntents = pendingNewIntents;
3791 }
3792 }
3793 break;
3794 }
3795 }
3796
3797 if (target == null) {
3798 target = new ActivityClientRecord();
3799 target.token = token;
3800 target.pendingResults = pendingResults;
3801 target.pendingIntents = pendingNewIntents;
3802 if (!fromServer) {
3803 ActivityClientRecord existing = mActivities.get(token);
3804 if (existing != null) {
3805 target.startsNotResumed = existing.paused;
3806 }
3807 target.onlyLocalRequest = true;
3808 }
3809 mRelaunchingActivities.add(target);
Jeff Brown9ef09972013-10-15 20:49:59 -07003810 sendMessage(H.RELAUNCH_ACTIVITY, target);
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003811 }
3812
3813 if (fromServer) {
3814 target.startsNotResumed = notResumed;
3815 target.onlyLocalRequest = false;
3816 }
3817 if (config != null) {
3818 target.createdConfig = config;
3819 }
3820 target.pendingConfigChanges |= configChanges;
3821 }
3822 }
3823
Romain Guy65b345f2011-07-27 18:51:50 -07003824 private void handleRelaunchActivity(ActivityClientRecord tmp) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003825 // If we are getting ready to gc after going to the background, well
3826 // we are back active so skip it.
3827 unscheduleGcIdler();
Dianne Hackborn89ad4562014-08-24 16:45:38 -07003828 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003829
3830 Configuration changedConfig = null;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003831 int configChanges = 0;
Bob Leee5408332009-09-04 18:31:17 -07003832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 // First: make sure we have the most recent configuration and most
3834 // recent version of the activity, or skip it if some previous call
3835 // had taken a more recent version.
Craig Mautner88c05892013-06-28 09:47:45 -07003836 synchronized (mResourcesManager) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003837 int N = mRelaunchingActivities.size();
3838 IBinder token = tmp.token;
3839 tmp = null;
3840 for (int i=0; i<N; i++) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003841 ActivityClientRecord r = mRelaunchingActivities.get(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003842 if (r.token == token) {
3843 tmp = r;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003844 configChanges |= tmp.pendingConfigChanges;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003845 mRelaunchingActivities.remove(i);
3846 i--;
3847 N--;
3848 }
3849 }
Bob Leee5408332009-09-04 18:31:17 -07003850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 if (tmp == null) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003852 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Abort, activity not relaunching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003853 return;
3854 }
Bob Leee5408332009-09-04 18:31:17 -07003855
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003856 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
3857 + tmp.token + " with configChanges=0x"
3858 + Integer.toHexString(configChanges));
3859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003860 if (mPendingConfiguration != null) {
3861 changedConfig = mPendingConfiguration;
3862 mPendingConfiguration = null;
3863 }
3864 }
Bob Leee5408332009-09-04 18:31:17 -07003865
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003866 if (tmp.createdConfig != null) {
3867 // If the activity manager is passing us its current config,
3868 // assume that is really what we want regardless of what we
3869 // may have pending.
3870 if (mConfiguration == null
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003871 || (tmp.createdConfig.isOtherSeqNewer(mConfiguration)
3872 && mConfiguration.diff(tmp.createdConfig) != 0)) {
3873 if (changedConfig == null
3874 || tmp.createdConfig.isOtherSeqNewer(changedConfig)) {
3875 changedConfig = tmp.createdConfig;
3876 }
Dianne Hackborn871ecdc2009-12-11 15:24:33 -08003877 }
3878 }
3879
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003880 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Relaunching activity "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07003881 + tmp.token + ": changedConfig=" + changedConfig);
3882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003883 // If there was a pending configuration change, execute it first.
3884 if (changedConfig != null) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -07003885 mCurDefaultDisplayDpi = changedConfig.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07003886 updateDefaultDensity();
Dianne Hackborne2515ee2011-04-27 18:52:56 -04003887 handleConfigurationChanged(changedConfig, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003888 }
Bob Leee5408332009-09-04 18:31:17 -07003889
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07003890 ActivityClientRecord r = mActivities.get(tmp.token);
Dianne Hackborn399cccb2010-04-13 22:57:49 -07003891 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handling relaunch of " + r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 if (r == null) {
3893 return;
3894 }
Bob Leee5408332009-09-04 18:31:17 -07003895
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003896 r.activity.mConfigChangeFlags |= configChanges;
Dianne Hackborn30c9bd82010-12-01 16:07:40 -08003897 r.onlyLocalRequest = tmp.onlyLocalRequest;
Christopher Tateb70f3df2009-04-07 16:07:59 -07003898 Intent currentIntent = r.activity.mIntent;
Bob Leee5408332009-09-04 18:31:17 -07003899
Dianne Hackborn0aae2d42010-12-07 23:51:29 -08003900 r.activity.mChangingConfigurations = true;
3901
Dianne Hackborne2b04802010-12-09 09:24:55 -08003902 // Need to ensure state is saved.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003903 if (!r.paused) {
Dianne Hackborne2b04802010-12-09 09:24:55 -08003904 performPauseActivity(r.token, false, r.isPreHoneycomb());
3905 }
3906 if (r.state == null && !r.stopped && !r.isPreHoneycomb()) {
Craig Mautnera0026042014-04-23 11:45:37 -07003907 callCallActivityOnSaveInstanceState(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003908 }
Bob Leee5408332009-09-04 18:31:17 -07003909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 handleDestroyActivity(r.token, false, configChanges, true);
Bob Leee5408332009-09-04 18:31:17 -07003911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 r.activity = null;
3913 r.window = null;
3914 r.hideForNow = false;
3915 r.nextIdle = null;
The Android Open Source Project10592532009-03-18 17:39:46 -07003916 // Merge any pending results and pending intents; don't just replace them
3917 if (tmp.pendingResults != null) {
3918 if (r.pendingResults == null) {
3919 r.pendingResults = tmp.pendingResults;
3920 } else {
3921 r.pendingResults.addAll(tmp.pendingResults);
3922 }
3923 }
3924 if (tmp.pendingIntents != null) {
3925 if (r.pendingIntents == null) {
3926 r.pendingIntents = tmp.pendingIntents;
3927 } else {
3928 r.pendingIntents.addAll(tmp.pendingIntents);
3929 }
3930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003931 r.startsNotResumed = tmp.startsNotResumed;
Bob Leee5408332009-09-04 18:31:17 -07003932
Christopher Tateb70f3df2009-04-07 16:07:59 -07003933 handleLaunchActivity(r, currentIntent);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003934 }
3935
Craig Mautnera0026042014-04-23 11:45:37 -07003936 private void callCallActivityOnSaveInstanceState(ActivityClientRecord r) {
3937 r.state = new Bundle();
3938 r.state.setAllowFds(false);
3939 if (r.isPersistable()) {
3940 r.persistentState = new PersistableBundle();
3941 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state,
3942 r.persistentState);
3943 } else {
3944 mInstrumentation.callActivityOnSaveInstanceState(r.activity, r.state);
3945 }
3946 }
3947
Dianne Hackborn73c14162012-09-19 15:45:06 -07003948 ArrayList<ComponentCallbacks2> collectComponentCallbacks(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 boolean allActivities, Configuration newConfig) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07003950 ArrayList<ComponentCallbacks2> callbacks
3951 = new ArrayList<ComponentCallbacks2>();
Bob Leee5408332009-09-04 18:31:17 -07003952
Craig Mautner88c05892013-06-28 09:47:45 -07003953 synchronized (mResourcesManager) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003954 final int NAPP = mAllApplications.size();
3955 for (int i=0; i<NAPP; i++) {
Dianne Hackborn73c14162012-09-19 15:45:06 -07003956 callbacks.add(mAllApplications.get(i));
3957 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003958 final int NACT = mActivities.size();
3959 for (int i=0; i<NACT; i++) {
3960 ActivityClientRecord ar = mActivities.valueAt(i);
3961 Activity a = ar.activity;
3962 if (a != null) {
3963 Configuration thisConfig = applyConfigCompatMainThread(
3964 mCurDefaultDisplayDpi, newConfig,
3965 ar.packageInfo.getCompatibilityInfo());
3966 if (!ar.activity.mFinished && (allActivities || !ar.paused)) {
3967 // If the activity is currently resumed, its configuration
3968 // needs to change right now.
3969 callbacks.add(a);
3970 } else if (thisConfig != null) {
3971 // Otherwise, we will tell it about the change
3972 // the next time it is resumed or shown. Note that
3973 // the activity manager may, before then, decide the
3974 // activity needs to be destroyed to handle its new
3975 // configuration.
3976 if (DEBUG_CONFIGURATION) {
3977 Slog.v(TAG, "Setting activity "
3978 + ar.activityInfo.name + " newConfig=" + thisConfig);
Romain Guya998dff2012-03-23 18:58:36 -07003979 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003980 ar.newConfig = thisConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 }
3982 }
3983 }
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003984 final int NSVC = mServices.size();
3985 for (int i=0; i<NSVC; i++) {
3986 callbacks.add(mServices.valueAt(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003987 }
3988 }
3989 synchronized (mProviderMap) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07003990 final int NPRV = mLocalProviders.size();
3991 for (int i=0; i<NPRV; i++) {
3992 callbacks.add(mLocalProviders.valueAt(i).mLocalProvider);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003993 }
3994 }
Bob Leee5408332009-09-04 18:31:17 -07003995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003996 return callbacks;
3997 }
Bob Leee5408332009-09-04 18:31:17 -07003998
Romain Guya998dff2012-03-23 18:58:36 -07003999 private static void performConfigurationChanged(ComponentCallbacks2 cb, Configuration config) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004000 // Only for Activity objects, check that they actually call up to their
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004001 // superclass implementation. ComponentCallbacks2 is an interface, so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004002 // we check the runtime type and act accordingly.
4003 Activity activity = (cb instanceof Activity) ? (Activity) cb : null;
4004 if (activity != null) {
4005 activity.mCalled = false;
4006 }
Bob Leee5408332009-09-04 18:31:17 -07004007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 boolean shouldChangeConfig = false;
4009 if ((activity == null) || (activity.mCurrentConfig == null)) {
4010 shouldChangeConfig = true;
4011 } else {
Bob Leee5408332009-09-04 18:31:17 -07004012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004013 // If the new config is the same as the config this Activity
4014 // is already running with then don't bother calling
4015 // onConfigurationChanged
4016 int diff = activity.mCurrentConfig.diff(config);
4017 if (diff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004018 // If this activity doesn't handle any of the config changes
4019 // then don't bother calling onConfigurationChanged as we're
4020 // going to destroy it.
Dianne Hackborne6676352011-06-01 16:51:20 -07004021 if ((~activity.mActivityInfo.getRealConfigChanged() & diff) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004022 shouldChangeConfig = true;
4023 }
4024 }
4025 }
Bob Leee5408332009-09-04 18:31:17 -07004026
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004027 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Config callback " + cb
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004028 + ": shouldChangeConfig=" + shouldChangeConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004029 if (shouldChangeConfig) {
4030 cb.onConfigurationChanged(config);
Bob Leee5408332009-09-04 18:31:17 -07004031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 if (activity != null) {
4033 if (!activity.mCalled) {
4034 throw new SuperNotCalledException(
4035 "Activity " + activity.getLocalClassName() +
4036 " did not call through to super.onConfigurationChanged()");
4037 }
4038 activity.mConfigChangeFlags = 0;
4039 activity.mCurrentConfig = new Configuration(config);
4040 }
4041 }
4042 }
4043
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004044 public final void applyConfigurationToResources(Configuration config) {
Craig Mautner88c05892013-06-28 09:47:45 -07004045 synchronized (mResourcesManager) {
4046 mResourcesManager.applyConfigurationToResourcesLocked(config, null);
Dianne Hackborn2f0b1752011-05-31 17:59:49 -07004047 }
4048 }
4049
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004050 final Configuration applyCompatConfiguration(int displayDensity) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004051 Configuration config = mConfiguration;
4052 if (mCompatConfiguration == null) {
4053 mCompatConfiguration = new Configuration();
4054 }
4055 mCompatConfiguration.setTo(mConfiguration);
Craig Mautner88c05892013-06-28 09:47:45 -07004056 if (mResourcesManager.applyCompatConfiguration(displayDensity, mCompatConfiguration)) {
Dianne Hackborn836e2622011-10-04 18:32:39 -07004057 config = mCompatConfiguration;
4058 }
4059 return config;
4060 }
4061
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004062 final void handleConfigurationChanged(Configuration config, CompatibilityInfo compat) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004063
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004064 int configDiff = 0;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004065
Craig Mautner88c05892013-06-28 09:47:45 -07004066 synchronized (mResourcesManager) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004067 if (mPendingConfiguration != null) {
4068 if (!mPendingConfiguration.isOtherSeqNewer(config)) {
4069 config = mPendingConfiguration;
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004070 mCurDefaultDisplayDpi = config.densityDpi;
Dianne Hackborndde331c2012-08-03 14:01:57 -07004071 updateDefaultDensity();
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004072 }
4073 mPendingConfiguration = null;
4074 }
4075
4076 if (config == null) {
4077 return;
4078 }
4079
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004080 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle configuration changed: "
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004081 + config);
Craig Mautner88c05892013-06-28 09:47:45 -07004082
4083 mResourcesManager.applyConfigurationToResourcesLocked(config, compat);
4084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004085 if (mConfiguration == null) {
4086 mConfiguration = new Configuration();
4087 }
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004088 if (!mConfiguration.isOtherSeqNewer(config) && compat == null) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004089 return;
4090 }
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004091 configDiff = mConfiguration.diff(config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004092 mConfiguration.updateFrom(config);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004093 config = applyCompatConfiguration(mCurDefaultDisplayDpi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004094 }
Dianne Hackborn73c14162012-09-19 15:45:06 -07004095
4096 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(false, config);
4097
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004098 freeTextLayoutCachesIfNeeded(configDiff);
4099
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004100 if (callbacks != null) {
4101 final int N = callbacks.size();
4102 for (int i=0; i<N; i++) {
4103 performConfigurationChanged(callbacks.get(i), config);
4104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004105 }
4106 }
4107
Romain Guy46bfc482013-08-16 18:38:29 -07004108 static void freeTextLayoutCachesIfNeeded(int configDiff) {
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004109 if (configDiff != 0) {
4110 // Ask text layout engine to free its caches if there is a locale change
4111 boolean hasLocaleConfigChange = ((configDiff & ActivityInfo.CONFIG_LOCALE) != 0);
4112 if (hasLocaleConfigChange) {
4113 Canvas.freeTextLayoutCaches();
4114 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Cleared TextLayout Caches");
4115 }
4116 }
4117 }
4118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004119 final void handleActivityConfigurationChanged(IBinder token) {
Dianne Hackborn01e4cfc2010-06-24 15:07:24 -07004120 ActivityClientRecord r = mActivities.get(token);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004121 if (r == null || r.activity == null) {
4122 return;
4123 }
Bob Leee5408332009-09-04 18:31:17 -07004124
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004125 if (DEBUG_CONFIGURATION) Slog.v(TAG, "Handle activity config changed: "
Dianne Hackborndc6b6352009-09-30 14:20:09 -07004126 + r.activityInfo.name);
4127
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004128 performConfigurationChanged(r.activity, mCompatConfiguration);
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004129
4130 freeTextLayoutCachesIfNeeded(r.activity.mCurrentConfig.diff(mCompatConfiguration));
Dianne Hackborn89ad4562014-08-24 16:45:38 -07004131
4132 mSomeActivitiesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004133 }
4134
Jeff Hao1b012d32014-08-20 10:35:34 -07004135 final void handleProfilerControl(boolean start, ProfilerInfo profilerInfo, int profileType) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004136 if (start) {
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004137 try {
Romain Guy7eabe552011-07-21 14:56:34 -07004138 switch (profileType) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004139 default:
4140 mProfiler.setProfiler(profilerInfo);
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004141 mProfiler.startProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004142 break;
4143 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004144 } catch (RuntimeException e) {
Jeff Hao1b012d32014-08-20 10:35:34 -07004145 Slog.w(TAG, "Profiling failed on path " + profilerInfo.profileFile
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004146 + " -- can the process access this path?");
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004147 } finally {
4148 try {
Jeff Hao1b012d32014-08-20 10:35:34 -07004149 profilerInfo.profileFd.close();
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004150 } catch (IOException e) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004151 Slog.w(TAG, "Failure closing profile fd", e);
Dianne Hackborn9c8dd552009-06-23 19:22:52 -07004152 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004153 }
4154 } else {
Romain Guy7eabe552011-07-21 14:56:34 -07004155 switch (profileType) {
Romain Guy7eabe552011-07-21 14:56:34 -07004156 default:
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004157 mProfiler.stopProfiling();
Romain Guy7eabe552011-07-21 14:56:34 -07004158 break;
Romain Guy7eabe552011-07-21 14:56:34 -07004159 }
The Android Open Source Projectf5b4b982009-03-05 20:00:43 -08004160 }
4161 }
Bob Leee5408332009-09-04 18:31:17 -07004162
Romain Guya998dff2012-03-23 18:58:36 -07004163 static final void handleDumpHeap(boolean managed, DumpHeapData dhd) {
Andy McFadden824c5102010-07-09 16:26:57 -07004164 if (managed) {
4165 try {
4166 Debug.dumpHprofData(dhd.path, dhd.fd.getFileDescriptor());
4167 } catch (IOException e) {
4168 Slog.w(TAG, "Managed heap dump failed on path " + dhd.path
4169 + " -- can the process access this path?");
4170 } finally {
4171 try {
4172 dhd.fd.close();
4173 } catch (IOException e) {
4174 Slog.w(TAG, "Failure closing profile fd", e);
4175 }
4176 }
4177 } else {
Andy McFadden06a6b552010-07-13 16:28:09 -07004178 Debug.dumpNativeHeap(dhd.fd.getFileDescriptor());
Andy McFadden824c5102010-07-09 16:26:57 -07004179 }
4180 }
4181
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004182 final void handleDispatchPackageBroadcast(int cmd, String[] packages) {
4183 boolean hasPkgInfo = false;
4184 if (packages != null) {
mark_chen89764e32014-12-12 15:38:48 +08004185 synchronized (mResourcesManager) {
4186 for (int i=packages.length-1; i>=0; i--) {
4187 //Slog.i(TAG, "Cleaning old package: " + packages[i]);
4188 if (!hasPkgInfo) {
4189 WeakReference<LoadedApk> ref;
4190 ref = mPackages.get(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004191 if (ref != null && ref.get() != null) {
4192 hasPkgInfo = true;
mark_chen89764e32014-12-12 15:38:48 +08004193 } else {
4194 ref = mResourcePackages.get(packages[i]);
4195 if (ref != null && ref.get() != null) {
4196 hasPkgInfo = true;
4197 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004198 }
4199 }
mark_chen89764e32014-12-12 15:38:48 +08004200 mPackages.remove(packages[i]);
4201 mResourcePackages.remove(packages[i]);
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004202 }
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004203 }
4204 }
Brad Fitzpatrick390dae12010-11-10 08:27:28 -08004205 ApplicationPackageManager.handlePackageBroadcast(cmd, packages,
Dianne Hackborn4416c3d2010-05-04 17:22:49 -07004206 hasPkgInfo);
4207 }
4208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004209 final void handleLowMemory() {
Dianne Hackborn73c14162012-09-19 15:45:06 -07004210 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Bob Leee5408332009-09-04 18:31:17 -07004211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004212 final int N = callbacks.size();
4213 for (int i=0; i<N; i++) {
4214 callbacks.get(i).onLowMemory();
4215 }
4216
Chris Tatece229052009-03-25 16:44:52 -07004217 // Ask SQLite to free up as much memory as it can, mostly from its page caches.
4218 if (Process.myUid() != Process.SYSTEM_UID) {
4219 int sqliteReleased = SQLiteDatabase.releaseMemory();
4220 EventLog.writeEvent(SQLITE_MEM_RELEASED_EVENT_LOG_TAG, sqliteReleased);
4221 }
Bob Leee5408332009-09-04 18:31:17 -07004222
Mike Reedcaf0df12009-04-27 14:32:05 -04004223 // Ask graphics to free up as much as possible (font/image caches)
4224 Canvas.freeCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004225
Fabrice Di Meglio30ca5cd2012-05-07 17:45:44 -07004226 // Ask text layout engine to free also as much as possible
4227 Canvas.freeTextLayoutCaches();
4228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004229 BinderInternal.forceGc("mem");
4230 }
4231
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004232 final void handleTrimMemory(int level) {
Dianne Hackborn162bc0e2012-04-09 14:06:16 -07004233 if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Trimming memory to level: " + level);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004234
Dianne Hackborn73c14162012-09-19 15:45:06 -07004235 ArrayList<ComponentCallbacks2> callbacks = collectComponentCallbacks(true, null);
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004236
4237 final int N = callbacks.size();
Romain Guya998dff2012-03-23 18:58:36 -07004238 for (int i = 0; i < N; i++) {
Dianne Hackbornc68c9132011-07-29 01:25:18 -07004239 callbacks.get(i).onTrimMemory(level);
4240 }
Romain Guy19f86e82012-04-23 15:19:07 -07004241
John Reckf47a5942014-06-30 16:20:04 -07004242 WindowManagerGlobal.getInstance().trimMemory(level);
Dianne Hackbornce86ba82011-07-13 19:33:41 -07004243 }
4244
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004245 private void setupGraphicsSupport(LoadedApk info, File cacheDir) {
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004246 if (Process.isIsolated()) {
4247 // Isolated processes aren't going to do UI.
4248 return;
4249 }
Romain Guya9582652011-11-10 14:20:10 -08004250 try {
4251 int uid = Process.myUid();
4252 String[] packages = getPackageManager().getPackagesForUid(uid);
4253
4254 // If there are several packages in this application we won't
4255 // initialize the graphics disk caches
Dianne Hackborna0c283e2012-02-09 10:47:01 -08004256 if (packages != null && packages.length == 1) {
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004257 HardwareRenderer.setupDiskCache(cacheDir);
4258 RenderScript.setupDiskCache(cacheDir);
Romain Guya9582652011-11-10 14:20:10 -08004259 }
4260 } catch (RemoteException e) {
4261 // Ignore
4262 }
Dianne Hackborndde331c2012-08-03 14:01:57 -07004263 }
4264
4265 private void updateDefaultDensity() {
4266 if (mCurDefaultDisplayDpi != Configuration.DENSITY_DPI_UNDEFINED
4267 && mCurDefaultDisplayDpi != DisplayMetrics.DENSITY_DEVICE
4268 && !mDensityCompatMode) {
4269 Slog.i(TAG, "Switching default density from "
4270 + DisplayMetrics.DENSITY_DEVICE + " to "
4271 + mCurDefaultDisplayDpi);
4272 DisplayMetrics.DENSITY_DEVICE = mCurDefaultDisplayDpi;
4273 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4274 }
4275 }
4276
Romain Guy65b345f2011-07-27 18:51:50 -07004277 private void handleBindApplication(AppBindData data) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004278 mBoundApplication = data;
4279 mConfiguration = new Configuration(data.config);
Dianne Hackborn5fd21692011-06-07 14:09:47 -07004280 mCompatConfiguration = new Configuration(data.config);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004281
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004282 mProfiler = new Profiler();
Jeff Hao1b012d32014-08-20 10:35:34 -07004283 if (data.initProfilerInfo != null) {
4284 mProfiler.profileFile = data.initProfilerInfo.profileFile;
4285 mProfiler.profileFd = data.initProfilerInfo.profileFd;
4286 mProfiler.samplingInterval = data.initProfilerInfo.samplingInterval;
4287 mProfiler.autoStopProfiler = data.initProfilerInfo.autoStopProfiler;
4288 }
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004290 // send up app name; do this *before* waiting for debugger
Christopher Tate8ee038d2009-11-06 11:30:20 -08004291 Process.setArgV0(data.processName);
Siva Velusamyd693dfa2012-09-10 14:36:58 -07004292 android.ddm.DdmHandleAppName.setAppName(data.processName,
4293 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004294
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004295 if (data.persistent) {
4296 // Persistent processes on low-memory devices do not get to
4297 // use hardware accelerated drawing, since this can add too much
4298 // overhead to the process.
Jeff Brown98365d72012-08-19 20:30:52 -07004299 if (!ActivityManager.isHighEndGfx()) {
Dianne Hackborn5d927c22011-09-02 12:22:18 -07004300 HardwareRenderer.disable(false);
4301 }
4302 }
Jeff Hao1b012d32014-08-20 10:35:34 -07004303
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004304 if (mProfiler.profileFd != null) {
4305 mProfiler.startProfiling();
Dianne Hackborn62f20ec2011-08-15 17:40:28 -07004306 }
4307
Joe Onoratod630f102011-03-17 18:42:26 -07004308 // If the app is Honeycomb MR1 or earlier, switch its AsyncTask
4309 // implementation to use the pool executor. Normally, we use the
4310 // serialized executor as the default. This has to happen in the
4311 // main thread so the main looper is set right.
Dianne Hackborn81e92762011-10-09 16:00:21 -07004312 if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
Joe Onoratod630f102011-03-17 18:42:26 -07004313 AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
4314 }
4315
Dianne Hackborn7895bc22014-09-05 15:09:03 -07004316 Message.updateCheckRecycle(data.appInfo.targetSdkVersion);
4317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004318 /*
4319 * Before spawning a new process, reset the time zone to be the system time zone.
4320 * This needs to be done because the system time zone could have changed after the
4321 * the spawning of this process. Without doing this this process would have the incorrect
4322 * system time zone.
4323 */
4324 TimeZone.setDefault(null);
4325
4326 /*
4327 * Initialize the default locale in this process for the reasons we set the time zone.
4328 */
4329 Locale.setDefault(data.config.locale);
4330
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004331 /*
4332 * Update the system configuration since its preloaded and might not
4333 * reflect configuration changes. The configuration object passed
4334 * in AppBindData can be safely assumed to be up to date
4335 */
Craig Mautner88c05892013-06-28 09:47:45 -07004336 mResourcesManager.applyConfigurationToResourcesLocked(data.config, data.compatInfo);
Dianne Hackborn908aecc2012-07-31 16:37:34 -07004337 mCurDefaultDisplayDpi = data.config.densityDpi;
4338 applyCompatConfiguration(mCurDefaultDisplayDpi);
Suchi Amalapurapuc9843292009-06-24 17:02:25 -07004339
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004340 data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004341
Dianne Hackborndde331c2012-08-03 14:01:57 -07004342 /**
4343 * Switch this process to density compatibility mode if needed.
4344 */
4345 if ((data.appInfo.flags&ApplicationInfo.FLAG_SUPPORTS_SCREEN_DENSITIES)
4346 == 0) {
4347 mDensityCompatMode = true;
4348 Bitmap.setDefaultDensity(DisplayMetrics.DENSITY_DEFAULT);
4349 }
4350 updateDefaultDensity();
4351
Jeff Browndefd4a62014-03-10 21:24:37 -07004352 final ContextImpl appContext = ContextImpl.createAppContext(this, data.info);
Amith Yamasani11de39a2012-08-17 18:00:52 -07004353 if (!Process.isIsolated()) {
4354 final File cacheDir = appContext.getCacheDir();
Jeff Sharkeye1d330a2012-05-02 13:46:21 -07004355
Amith Yamasani92d57052012-08-23 10:07:52 -07004356 if (cacheDir != null) {
4357 // Provide a usable directory for temporary files
4358 System.setProperty("java.io.tmpdir", cacheDir.getAbsolutePath());
Amith Yamasani92d57052012-08-23 10:07:52 -07004359 } else {
Pirama Arumuga Nainar51772b72015-02-10 12:41:42 -08004360 Log.v(TAG, "Unable to initialize \"java.io.tmpdir\" property due to missing cache directory");
4361 }
4362
4363 // Use codeCacheDir to store generated/compiled graphics code
4364 final File codeCacheDir = appContext.getCodeCacheDir();
4365 if (codeCacheDir != null) {
4366 setupGraphicsSupport(data.info, codeCacheDir);
4367 } else {
4368 Log.e(TAG, "Unable to setupGraphicsSupport due to missing code-cache directory");
Amith Yamasani92d57052012-08-23 10:07:52 -07004369 }
Amith Yamasani11de39a2012-08-17 18:00:52 -07004370 }
Narayan Kamathccb2a0862013-12-19 14:49:36 +00004371
4372
4373 final boolean is24Hr = "24".equals(mCoreSettings.getString(Settings.System.TIME_12_24));
4374 DateFormat.set24HourTimePref(is24Hr);
4375
Jon Miranda836c0a82014-08-11 12:32:26 -07004376 View.mDebugViewAttributes =
4377 mCoreSettings.getInt(Settings.Global.DEBUG_VIEW_ATTRIBUTES, 0) != 0;
4378
Dianne Hackborn96e240f2009-07-26 17:42:30 -07004379 /**
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004380 * For system applications on userdebug/eng builds, log stack
4381 * traces of disk and network access to dropbox for analysis.
4382 */
Brad Fitzpatrickad13b982010-07-14 12:35:53 -07004383 if ((data.appInfo.flags &
4384 (ApplicationInfo.FLAG_SYSTEM |
Brad Fitzpatrick50d66f92010-09-13 21:29:05 -07004385 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
4386 StrictMode.conditionallyEnableDebugLogging();
Brad Fitzpatrick438d0592010-06-10 12:19:19 -07004387 }
4388
4389 /**
Brad Fitzpatrickb6e18412010-10-28 14:50:05 -07004390 * For apps targetting SDK Honeycomb or later, we don't allow
4391 * network usage on the main event loop / UI thread.
4392 *
4393 * Note to those grepping: this is what ultimately throws
4394 * NetworkOnMainThreadException ...
4395 */
4396 if (data.appInfo.targetSdkVersion > 9) {
4397 StrictMode.enableDeathOnNetwork();
4398 }
4399
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004400 if (data.debugMode != IApplicationThread.DEBUG_OFF) {
4401 // XXX should have option to change the port.
4402 Debug.changeDebugPort(8100);
4403 if (data.debugMode == IApplicationThread.DEBUG_WAIT) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004404 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 + " is waiting for the debugger on port 8100...");
4406
4407 IActivityManager mgr = ActivityManagerNative.getDefault();
4408 try {
4409 mgr.showWaitingForDebugger(mAppThread, true);
4410 } catch (RemoteException ex) {
4411 }
4412
4413 Debug.waitForDebugger();
4414
4415 try {
4416 mgr.showWaitingForDebugger(mAppThread, false);
4417 } catch (RemoteException ex) {
4418 }
4419
4420 } else {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004421 Slog.w(TAG, "Application " + data.info.getPackageName()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004422 + " can be debugged on port 8100...");
4423 }
4424 }
4425
Siva Velusamy92a8b222012-03-09 16:24:04 -08004426 // Enable OpenGL tracing if required
4427 if (data.enableOpenGlTrace) {
Siva Velusamy0c1761b2012-12-14 17:09:06 -08004428 GLUtils.setTracingLevel(1);
Siva Velusamy92a8b222012-03-09 16:24:04 -08004429 }
4430
Jamie Gennisf9c7d6b2013-03-25 14:18:25 -07004431 // Allow application-generated systrace messages if we're debuggable.
4432 boolean appTracingAllowed = (data.appInfo.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0;
4433 Trace.setAppTracingAllowed(appTracingAllowed);
4434
Robert Greenwalt434203a2010-10-11 16:00:27 -07004435 /**
4436 * Initialize the default http proxy in this process for the reasons we set the time zone.
4437 */
4438 IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004439 if (b != null) {
4440 // In pre-boot mode (doing initial launch to collect password), not
4441 // all system is up. This includes the connectivity service, so don't
4442 // crash if we can't get it.
4443 IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
4444 try {
Jason Monk207900c2014-04-25 15:00:09 -04004445 ProxyInfo proxyInfo = service.getProxy();
4446 Proxy.setHttpProxySystemProperty(proxyInfo);
Dianne Hackbornd4d32c52011-11-08 17:33:59 -08004447 } catch (RemoteException e) {}
4448 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07004449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 if (data.instrumentationName != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004451 InstrumentationInfo ii = null;
4452 try {
4453 ii = appContext.getPackageManager().
4454 getInstrumentationInfo(data.instrumentationName, 0);
4455 } catch (PackageManager.NameNotFoundException e) {
4456 }
4457 if (ii == null) {
4458 throw new RuntimeException(
4459 "Unable to find instrumentation info for: "
4460 + data.instrumentationName);
4461 }
4462
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004463 mInstrumentationPackageName = ii.packageName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 mInstrumentationAppDir = ii.sourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004465 mInstrumentationSplitAppDirs = ii.splitSourceDirs;
4466 mInstrumentationLibDir = ii.nativeLibraryDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004467 mInstrumentedAppDir = data.info.getAppDir();
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004468 mInstrumentedSplitAppDirs = data.info.getSplitAppDirs();
4469 mInstrumentedLibDir = data.info.getLibDir();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004470
4471 ApplicationInfo instrApp = new ApplicationInfo();
4472 instrApp.packageName = ii.packageName;
4473 instrApp.sourceDir = ii.sourceDir;
4474 instrApp.publicSourceDir = ii.publicSourceDir;
Jeff Sharkey8a4c9722014-06-16 13:48:42 -07004475 instrApp.splitSourceDirs = ii.splitSourceDirs;
4476 instrApp.splitPublicSourceDirs = ii.splitPublicSourceDirs;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 instrApp.dataDir = ii.dataDir;
Kenny Root85387d72010-08-26 10:13:11 -07004478 instrApp.nativeLibraryDir = ii.nativeLibraryDir;
Dianne Hackborne2515ee2011-04-27 18:52:56 -04004479 LoadedApk pi = getPackageInfo(instrApp, data.compatInfo,
Dianne Hackbornfee756f2014-07-16 17:31:10 -07004480 appContext.getClassLoader(), false, true, false);
Jeff Browndefd4a62014-03-10 21:24:37 -07004481 ContextImpl instrContext = ContextImpl.createAppContext(this, pi);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482
4483 try {
4484 java.lang.ClassLoader cl = instrContext.getClassLoader();
4485 mInstrumentation = (Instrumentation)
4486 cl.loadClass(data.instrumentationName.getClassName()).newInstance();
4487 } catch (Exception e) {
4488 throw new RuntimeException(
4489 "Unable to instantiate instrumentation "
4490 + data.instrumentationName + ": " + e.toString(), e);
4491 }
4492
4493 mInstrumentation.init(this, instrContext, appContext,
Svetoslav Ganov80943d82013-01-02 10:25:37 -08004494 new ComponentName(ii.packageName, ii.name), data.instrumentationWatcher,
4495 data.instrumentationUiAutomationConnection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004496
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004497 if (mProfiler.profileFile != null && !ii.handleProfiling
4498 && mProfiler.profileFd == null) {
4499 mProfiler.handlingProfiling = true;
4500 File file = new File(mProfiler.profileFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 file.getParentFile().mkdirs();
4502 Debug.startMethodTracing(file.toString(), 8 * 1024 * 1024);
4503 }
4504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 } else {
4506 mInstrumentation = new Instrumentation();
4507 }
4508
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004509 if ((data.appInfo.flags&ApplicationInfo.FLAG_LARGE_HEAP) != 0) {
Dianne Hackbornde398512011-01-18 18:45:21 -08004510 dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
Mathieu Chartier24cee072015-01-08 14:42:20 -08004511 } else {
4512 // Small heap, clamp to the current growth limit and let the heap release
4513 // pages after the growth limit to the non growth limit capacity. b/18387825
4514 dalvik.system.VMRuntime.getRuntime().clampGrowthLimit();
Dianne Hackborn3b81bc12011-01-15 11:50:52 -08004515 }
4516
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004517 // Allow disk access during application and provider setup. This could
Jeff Sharkey7c501672012-02-28 12:08:37 -08004518 // block processing ordered broadcasts, but later processing would
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004519 // probably end up doing the same disk access.
Jeff Sharkey7c501672012-02-28 12:08:37 -08004520 final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 try {
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004522 // If the app is being launched for full backup or restore, bring it up in
4523 // a restricted environment with the base application class.
4524 Application app = data.info.makeApplication(data.restrictedBackupMode, null);
4525 mInitialApplication = app;
Jeff Sharkey7c501672012-02-28 12:08:37 -08004526
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004527 // don't bring up providers in restricted mode; they may depend on the
4528 // app's custom Application class
4529 if (!data.restrictedBackupMode) {
4530 List<ProviderInfo> providers = data.providers;
4531 if (providers != null) {
4532 installContentProviders(app, providers);
4533 // For process that contains content providers, we want to
4534 // ensure that the JIT is enabled "at some point".
4535 mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000);
4536 }
4537 }
4538
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004539 // Do this after providers, since instrumentation tests generally start their
4540 // test thread at this point, and we don't want that racing.
4541 try {
4542 mInstrumentation.onCreate(data.instrumentationArgs);
4543 }
4544 catch (Exception e) {
4545 throw new RuntimeException(
4546 "Exception thrown in onCreate() of "
4547 + data.instrumentationName + ": " + e.toString(), e);
4548 }
4549
Jeff Sharkey6bff62c2012-02-29 12:26:36 -08004550 try {
4551 mInstrumentation.callApplicationOnCreate(app);
4552 } catch (Exception e) {
4553 if (!mInstrumentation.onException(app, e)) {
4554 throw new RuntimeException(
4555 "Unable to create application " + app.getClass().getName()
4556 + ": " + e.toString(), e);
4557 }
4558 }
4559 } finally {
4560 StrictMode.setThreadPolicy(savedPolicy);
4561 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004562 }
4563
4564 /*package*/ final void finishInstrumentation(int resultCode, Bundle results) {
4565 IActivityManager am = ActivityManagerNative.getDefault();
Jeff Sharkey3ad9d002011-09-15 15:51:54 -07004566 if (mProfiler.profileFile != null && mProfiler.handlingProfiling
4567 && mProfiler.profileFd == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 Debug.stopMethodTracing();
4569 }
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004570 //Slog.i(TAG, "am: " + ActivityManagerNative.getDefault()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004571 // + ", app thr: " + mAppThread);
4572 try {
4573 am.finishInstrumentation(mAppThread, resultCode, results);
4574 } catch (RemoteException ex) {
4575 }
4576 }
4577
Romain Guy65b345f2011-07-27 18:51:50 -07004578 private void installContentProviders(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004579 Context context, List<ProviderInfo> providers) {
4580 final ArrayList<IActivityManager.ContentProviderHolder> results =
4581 new ArrayList<IActivityManager.ContentProviderHolder>();
4582
Romain Guya998dff2012-03-23 18:58:36 -07004583 for (ProviderInfo cpi : providers) {
Dianne Hackborn40e9f292012-11-27 19:12:23 -08004584 if (DEBUG_PROVIDER) {
4585 StringBuilder buf = new StringBuilder(128);
4586 buf.append("Pub ");
4587 buf.append(cpi.authority);
4588 buf.append(": ");
4589 buf.append(cpi.name);
4590 Log.i(TAG, buf.toString());
4591 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004592 IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi,
4593 false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
4594 if (cph != null) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004595 cph.noReleaseNeeded = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 results.add(cph);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 }
4598 }
4599
4600 try {
4601 ActivityManagerNative.getDefault().publishContentProviders(
4602 getApplicationThread(), results);
4603 } catch (RemoteException ex) {
4604 }
4605 }
4606
Jeff Sharkey6d515712012-09-20 16:06:08 -07004607 public final IContentProvider acquireProvider(
4608 Context c, String auth, int userId, boolean stable) {
4609 final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004610 if (provider != null) {
4611 return provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 }
4613
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004614 // There is a possible race here. Another thread may try to acquire
4615 // the same provider at the same time. When this happens, we want to ensure
4616 // that the first one wins.
4617 // Note that we cannot hold the lock while acquiring and installing the
4618 // provider since it might take a long time to run and it could also potentially
4619 // be re-entrant in the case where the provider is in the same process.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004620 IActivityManager.ContentProviderHolder holder = null;
4621 try {
4622 holder = ActivityManagerNative.getDefault().getContentProvider(
Jeff Sharkey6d515712012-09-20 16:06:08 -07004623 getApplicationThread(), auth, userId, stable);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 } catch (RemoteException ex) {
4625 }
4626 if (holder == null) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004627 Slog.e(TAG, "Failed to find provider info for " + auth);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 return null;
4629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004631 // Install provider will increment the reference count for us, and break
4632 // any ties in the race.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004633 holder = installProvider(c, holder, holder.info,
4634 true /*noisy*/, holder.noReleaseNeeded, stable);
4635 return holder.provider;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 }
4637
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004638 private final void incProviderRefLocked(ProviderRefCount prc, boolean stable) {
4639 if (stable) {
4640 prc.stableCount += 1;
4641 if (prc.stableCount == 1) {
4642 // We are acquiring a new stable reference on the provider.
4643 int unstableDelta;
4644 if (prc.removePending) {
4645 // We have a pending remove operation, which is holding the
4646 // last unstable reference. At this point we are converting
4647 // that unstable reference to our new stable reference.
4648 unstableDelta = -1;
4649 // Cancel the removal of the provider.
4650 if (DEBUG_PROVIDER) {
4651 Slog.v(TAG, "incProviderRef: stable "
4652 + "snatched provider from the jaws of death");
4653 }
4654 prc.removePending = false;
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004655 // There is a race! It fails to remove the message, which
4656 // will be handled in completeRemoveProvider().
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004657 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4658 } else {
4659 unstableDelta = 0;
4660 }
4661 try {
4662 if (DEBUG_PROVIDER) {
4663 Slog.v(TAG, "incProviderRef Now stable - "
4664 + prc.holder.info.name + ": unstableDelta="
4665 + unstableDelta);
4666 }
4667 ActivityManagerNative.getDefault().refContentProvider(
4668 prc.holder.connection, 1, unstableDelta);
4669 } catch (RemoteException e) {
4670 //do nothing content provider object is dead any way
4671 }
4672 }
4673 } else {
4674 prc.unstableCount += 1;
4675 if (prc.unstableCount == 1) {
4676 // We are acquiring a new unstable reference on the provider.
4677 if (prc.removePending) {
4678 // Oh look, we actually have a remove pending for the
4679 // provider, which is still holding the last unstable
4680 // reference. We just need to cancel that to take new
4681 // ownership of the reference.
4682 if (DEBUG_PROVIDER) {
4683 Slog.v(TAG, "incProviderRef: unstable "
4684 + "snatched provider from the jaws of death");
4685 }
4686 prc.removePending = false;
4687 mH.removeMessages(H.REMOVE_PROVIDER, prc);
4688 } else {
4689 // First unstable ref, increment our count in the
4690 // activity manager.
4691 try {
4692 if (DEBUG_PROVIDER) {
4693 Slog.v(TAG, "incProviderRef: Now unstable - "
4694 + prc.holder.info.name);
4695 }
4696 ActivityManagerNative.getDefault().refContentProvider(
4697 prc.holder.connection, 0, 1);
4698 } catch (RemoteException e) {
4699 //do nothing content provider object is dead any way
4700 }
4701 }
4702 }
4703 }
4704 }
4705
Jeff Sharkey6d515712012-09-20 16:06:08 -07004706 public final IContentProvider acquireExistingProvider(
4707 Context c, String auth, int userId, boolean stable) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004708 synchronized (mProviderMap) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004709 final ProviderKey key = new ProviderKey(auth, userId);
4710 final ProviderClientRecord pr = mProviderMap.get(key);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004711 if (pr == null) {
4712 return null;
4713 }
4714
4715 IContentProvider provider = pr.mProvider;
4716 IBinder jBinder = provider.asBinder();
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004717 if (!jBinder.isBinderAlive()) {
4718 // The hosting process of the provider has died; we can't
4719 // use this one.
4720 Log.i(TAG, "Acquiring provider " + auth + " for user " + userId
4721 + ": existing object's process dead");
4722 handleUnstableProviderDiedLocked(jBinder, true);
4723 return null;
4724 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004725
4726 // Only increment the ref count if we have one. If we don't then the
4727 // provider is not reference counted and never needs to be released.
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004728 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004729 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004730 incProviderRefLocked(prc, stable);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004731 }
4732 return provider;
4733 }
Dianne Hackborncca1f0e2010-09-26 18:34:53 -07004734 }
4735
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004736 public final boolean releaseProvider(IContentProvider provider, boolean stable) {
4737 if (provider == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 return false;
4739 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004741 IBinder jBinder = provider.asBinder();
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004742 synchronized (mProviderMap) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004743 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004744 if (prc == null) {
4745 // The provider has no ref count, no release is needed.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004746 return false;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004747 }
4748
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004749 boolean lastRef = false;
4750 if (stable) {
4751 if (prc.stableCount == 0) {
4752 if (DEBUG_PROVIDER) Slog.v(TAG,
4753 "releaseProvider: stable ref count already 0, how?");
4754 return false;
4755 }
4756 prc.stableCount -= 1;
4757 if (prc.stableCount == 0) {
4758 // What we do at this point depends on whether there are
4759 // any unstable refs left: if there are, we just tell the
4760 // activity manager to decrement its stable count; if there
4761 // aren't, we need to enqueue this provider to be removed,
4762 // and convert to holding a single unstable ref while
4763 // doing so.
4764 lastRef = prc.unstableCount == 0;
4765 try {
4766 if (DEBUG_PROVIDER) {
4767 Slog.v(TAG, "releaseProvider: No longer stable w/lastRef="
4768 + lastRef + " - " + prc.holder.info.name);
4769 }
4770 ActivityManagerNative.getDefault().refContentProvider(
4771 prc.holder.connection, -1, lastRef ? 1 : 0);
4772 } catch (RemoteException e) {
4773 //do nothing content provider object is dead any way
4774 }
4775 }
4776 } else {
4777 if (prc.unstableCount == 0) {
4778 if (DEBUG_PROVIDER) Slog.v(TAG,
4779 "releaseProvider: unstable ref count already 0, how?");
4780 return false;
4781 }
4782 prc.unstableCount -= 1;
4783 if (prc.unstableCount == 0) {
4784 // If this is the last reference, we need to enqueue
4785 // this provider to be removed instead of telling the
4786 // activity manager to remove it at this point.
4787 lastRef = prc.stableCount == 0;
4788 if (!lastRef) {
4789 try {
4790 if (DEBUG_PROVIDER) {
4791 Slog.v(TAG, "releaseProvider: No longer unstable - "
4792 + prc.holder.info.name);
4793 }
4794 ActivityManagerNative.getDefault().refContentProvider(
4795 prc.holder.connection, 0, -1);
4796 } catch (RemoteException e) {
4797 //do nothing content provider object is dead any way
4798 }
4799 }
4800 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004801 }
4802
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004803 if (lastRef) {
4804 if (!prc.removePending) {
4805 // Schedule the actual remove asynchronously, since we don't know the context
4806 // this will be called in.
4807 // TODO: it would be nice to post a delayed message, so
4808 // if we come back and need the same provider quickly
4809 // we will still have it available.
4810 if (DEBUG_PROVIDER) {
4811 Slog.v(TAG, "releaseProvider: Enqueueing pending removal - "
4812 + prc.holder.info.name);
4813 }
4814 prc.removePending = true;
4815 Message msg = mH.obtainMessage(H.REMOVE_PROVIDER, prc);
4816 mH.sendMessage(msg);
4817 } else {
4818 Slog.w(TAG, "Duplicate remove pending of provider " + prc.holder.info.name);
4819 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004820 }
4821 return true;
4822 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 }
4824
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004825 final void completeRemoveProvider(ProviderRefCount prc) {
4826 synchronized (mProviderMap) {
4827 if (!prc.removePending) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004828 // There was a race! Some other client managed to acquire
4829 // the provider before the removal was completed.
4830 // Abort the removal. We will do it later.
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004831 if (DEBUG_PROVIDER) Slog.v(TAG, "completeRemoveProvider: lost the race, "
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004832 + "provider still in use");
4833 return;
4834 }
4835
Guobin Zhang9e3e52662013-03-21 13:57:11 +08004836 // More complicated race!! Some client managed to acquire the
4837 // provider and release it before the removal was completed.
4838 // Continue the removal, and abort the next remove message.
4839 prc.removePending = false;
4840
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004841 final IBinder jBinder = prc.holder.provider.asBinder();
4842 ProviderRefCount existingPrc = mProviderRefCountMap.get(jBinder);
4843 if (existingPrc == prc) {
4844 mProviderRefCountMap.remove(jBinder);
4845 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004846
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004847 for (int i=mProviderMap.size()-1; i>=0; i--) {
4848 ProviderClientRecord pr = mProviderMap.valueAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004849 IBinder myBinder = pr.mProvider.asBinder();
4850 if (myBinder == jBinder) {
Dianne Hackbornadd005c2013-07-17 18:43:12 -07004851 mProviderMap.removeAt(i);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004852 }
4853 }
4854 }
4855
4856 try {
4857 if (DEBUG_PROVIDER) {
4858 Slog.v(TAG, "removeProvider: Invoking ActivityManagerNative."
4859 + "removeContentProvider(" + prc.holder.info.name + ")");
4860 }
4861 ActivityManagerNative.getDefault().removeContentProvider(
4862 prc.holder.connection, false);
4863 } catch (RemoteException e) {
4864 //do nothing content provider object is dead any way
4865 }
4866 }
4867
4868 final void handleUnstableProviderDied(IBinder provider, boolean fromClient) {
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004869 synchronized (mProviderMap) {
4870 handleUnstableProviderDiedLocked(provider, fromClient);
4871 }
4872 }
4873
4874 final void handleUnstableProviderDiedLocked(IBinder provider, boolean fromClient) {
4875 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4876 if (prc != null) {
4877 if (DEBUG_PROVIDER) Slog.v(TAG, "Cleaning up dead provider "
4878 + provider + " " + prc.holder.info.name);
4879 mProviderRefCountMap.remove(provider);
You Kimbc74de62013-10-01 00:13:26 +09004880 for (int i=mProviderMap.size()-1; i>=0; i--) {
4881 ProviderClientRecord pr = mProviderMap.valueAt(i);
4882 if (pr != null && pr.mProvider.asBinder() == provider) {
4883 Slog.i(TAG, "Removing dead content provider:" + pr.mProvider.toString());
4884 mProviderMap.removeAt(i);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004885 }
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004886 }
You Kimbc74de62013-10-01 00:13:26 +09004887
Dianne Hackbornc428aae2012-10-03 16:38:22 -07004888 if (fromClient) {
4889 // We found out about this due to execution in our client
4890 // code. Tell the activity manager about it now, to ensure
4891 // that the next time we go to do anything with the provider
4892 // it knows it is dead (so we don't race with its death
4893 // notification).
4894 try {
4895 ActivityManagerNative.getDefault().unstableProviderDied(
4896 prc.holder.connection);
4897 } catch (RemoteException e) {
4898 //do nothing content provider object is dead any way
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004899 }
Dianne Hackborn9bfb7072009-09-22 11:37:40 -07004900 }
4901 }
4902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004903
Jeff Sharkey7aa76012013-09-30 14:26:27 -07004904 final void appNotRespondingViaProvider(IBinder provider) {
4905 synchronized (mProviderMap) {
4906 ProviderRefCount prc = mProviderRefCountMap.get(provider);
4907 if (prc != null) {
4908 try {
4909 ActivityManagerNative.getDefault()
4910 .appNotRespondingViaProvider(prc.holder.connection);
4911 } catch (RemoteException e) {
4912 }
4913 }
4914 }
4915 }
4916
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004917 private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
Jeff Sharkey6d515712012-09-20 16:06:08 -07004918 ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
Andreas Gampe18e99c12015-03-06 15:29:06 -08004919 final String auths[] = holder.info.authority.split(";");
Jeff Sharkey6d515712012-09-20 16:06:08 -07004920 final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);
4921
4922 final ProviderClientRecord pcr = new ProviderClientRecord(
4923 auths, provider, localProvider, holder);
4924 for (String auth : auths) {
4925 final ProviderKey key = new ProviderKey(auth, userId);
4926 final ProviderClientRecord existing = mProviderMap.get(key);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004927 if (existing != null) {
4928 Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
Jeff Sharkey6d515712012-09-20 16:06:08 -07004929 + " already published as " + auth);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004930 } else {
Jeff Sharkey6d515712012-09-20 16:06:08 -07004931 mProviderMap.put(key, pcr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004932 }
4933 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004934 return pcr;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 }
4936
Jeff Brownddaa9ac2011-11-11 20:16:14 -08004937 /**
4938 * Installs the provider.
4939 *
4940 * Providers that are local to the process or that come from the system server
4941 * may be installed permanently which is indicated by setting noReleaseNeeded to true.
4942 * Other remote providers are reference counted. The initial reference count
4943 * for all reference counted providers is one. Providers that are not reference
4944 * counted do not have a reference count (at all).
4945 *
4946 * This method detects when a provider has already been installed. When this happens,
4947 * it increments the reference count of the existing provider (if appropriate)
4948 * and returns the existing provider. This can happen due to concurrent
4949 * attempts to acquire the same provider.
4950 */
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004951 private IActivityManager.ContentProviderHolder installProvider(Context context,
4952 IActivityManager.ContentProviderHolder holder, ProviderInfo info,
4953 boolean noisy, boolean noReleaseNeeded, boolean stable) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954 ContentProvider localProvider = null;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004955 IContentProvider provider;
Dianne Hackborn5f48fca2012-05-30 11:06:31 -07004956 if (holder == null || holder.provider == null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004957 if (DEBUG_PROVIDER || noisy) {
Dianne Hackborn399cccb2010-04-13 22:57:49 -07004958 Slog.d(TAG, "Loading provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004959 + info.name);
4960 }
4961 Context c = null;
4962 ApplicationInfo ai = info.applicationInfo;
4963 if (context.getPackageName().equals(ai.packageName)) {
4964 c = context;
4965 } else if (mInitialApplication != null &&
4966 mInitialApplication.getPackageName().equals(ai.packageName)) {
4967 c = mInitialApplication;
4968 } else {
4969 try {
4970 c = context.createPackageContext(ai.packageName,
4971 Context.CONTEXT_INCLUDE_CODE);
4972 } catch (PackageManager.NameNotFoundException e) {
Romain Guy65b345f2011-07-27 18:51:50 -07004973 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974 }
4975 }
4976 if (c == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004977 Slog.w(TAG, "Unable to get context for package " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004978 ai.packageName +
4979 " while loading content provider " +
4980 info.name);
4981 return null;
4982 }
4983 try {
4984 final java.lang.ClassLoader cl = c.getClassLoader();
4985 localProvider = (ContentProvider)cl.
4986 loadClass(info.name).newInstance();
4987 provider = localProvider.getIContentProvider();
4988 if (provider == null) {
Dianne Hackbornc9421ba2010-03-11 22:23:46 -08004989 Slog.e(TAG, "Failed to instantiate class " +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004990 info.name + " from sourceDir " +
4991 info.applicationInfo.sourceDir);
4992 return null;
4993 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07004994 if (DEBUG_PROVIDER) Slog.v(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 TAG, "Instantiating local provider " + info.name);
4996 // XXX Need to create the correct context for this provider.
4997 localProvider.attachInfo(c, info);
4998 } catch (java.lang.Exception e) {
4999 if (!mInstrumentation.onException(null, e)) {
5000 throw new RuntimeException(
5001 "Unable to get provider " + info.name
5002 + ": " + e.toString(), e);
5003 }
5004 return null;
5005 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005006 } else {
5007 provider = holder.provider;
5008 if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 + info.name);
5010 }
5011
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005012 IActivityManager.ContentProviderHolder retHolder;
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005013
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005014 synchronized (mProviderMap) {
5015 if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
5016 + " / " + info.name);
5017 IBinder jBinder = provider.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 if (localProvider != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005019 ComponentName cname = new ComponentName(info.packageName, info.name);
5020 ProviderClientRecord pr = mLocalProvidersByName.get(cname);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005021 if (pr != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005022 if (DEBUG_PROVIDER) {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005023 Slog.v(TAG, "installProvider: lost the race, "
5024 + "using existing local provider");
5025 }
5026 provider = pr.mProvider;
5027 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005028 holder = new IActivityManager.ContentProviderHolder(info);
5029 holder.provider = provider;
5030 holder.noReleaseNeeded = true;
5031 pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005032 mLocalProviders.put(jBinder, pr);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005033 mLocalProvidersByName.put(cname, pr);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005034 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005035 retHolder = pr.mHolder;
5036 } else {
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005037 ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
5038 if (prc != null) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005039 if (DEBUG_PROVIDER) {
5040 Slog.v(TAG, "installProvider: lost the race, updating ref count");
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005041 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005042 // We need to transfer our new reference to the existing
5043 // ref count, releasing the old one... but only if
5044 // release is needed (that is, it is not running in the
5045 // system process).
5046 if (!noReleaseNeeded) {
5047 incProviderRefLocked(prc, stable);
5048 try {
5049 ActivityManagerNative.getDefault().removeContentProvider(
5050 holder.connection, stable);
5051 } catch (RemoteException e) {
5052 //do nothing content provider object is dead any way
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005053 }
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005054 }
5055 } else {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005056 ProviderClientRecord client = installProviderAuthoritiesLocked(
5057 provider, localProvider, holder);
5058 if (noReleaseNeeded) {
5059 prc = new ProviderRefCount(holder, client, 1000, 1000);
5060 } else {
5061 prc = stable
5062 ? new ProviderRefCount(holder, client, 1, 0)
5063 : new ProviderRefCount(holder, client, 0, 1);
5064 }
5065 mProviderRefCountMap.put(jBinder, prc);
Jeff Brownddaa9ac2011-11-11 20:16:14 -08005066 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005067 retHolder = prc.holder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 }
5069 }
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07005070
5071 return retHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 }
5073
Romain Guy65b345f2011-07-27 18:51:50 -07005074 private void attach(boolean system) {
Jeff Sharkey66a017b2013-01-17 18:18:22 -08005075 sCurrentActivityThread = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 mSystemThread = system;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005077 if (!system) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005078 ViewRootImpl.addFirstDrawHandler(new Runnable() {
Craig Mautner88c05892013-06-28 09:47:45 -07005079 @Override
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08005080 public void run() {
5081 ensureJitEnabled();
5082 }
5083 });
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005084 android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
5085 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 RuntimeInit.setApplicationObject(mAppThread.asBinder());
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005087 final IActivityManager mgr = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005088 try {
5089 mgr.attachApplication(mAppThread);
5090 } catch (RemoteException ex) {
Romain Guy65b345f2011-07-27 18:51:50 -07005091 // Ignore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005093 // Watch for getting close to heap limit.
5094 BinderInternal.addGcWatcher(new Runnable() {
5095 @Override public void run() {
5096 if (!mSomeActivitiesChanged) {
5097 return;
5098 }
5099 Runtime runtime = Runtime.getRuntime();
5100 long dalvikMax = runtime.maxMemory();
5101 long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
5102 if (dalvikUsed > ((3*dalvikMax)/4)) {
5103 if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
5104 + " total=" + (runtime.totalMemory()/1024)
5105 + " used=" + (dalvikUsed/1024));
5106 mSomeActivitiesChanged = false;
5107 try {
5108 mgr.releaseSomeActivities(mAppThread);
5109 } catch (RemoteException e) {
5110 }
5111 }
5112 }
5113 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 } else {
5115 // Don't set application object here -- if the system crashes,
5116 // we can't display an alert, we just want to die die die.
Siva Velusamyd693dfa2012-09-10 14:36:58 -07005117 android.ddm.DdmHandleAppName.setAppName("system_process",
Dianne Hackborn89ad4562014-08-24 16:45:38 -07005118 UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005119 try {
5120 mInstrumentation = new Instrumentation();
Jeff Browndefd4a62014-03-10 21:24:37 -07005121 ContextImpl context = ContextImpl.createAppContext(
5122 this, getSystemContext().mPackageInfo);
Jeff Brown7fc8e352014-09-16 18:06:47 -07005123 mInitialApplication = context.mPackageInfo.makeApplication(true, null);
5124 mInitialApplication.onCreate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005125 } catch (Exception e) {
5126 throw new RuntimeException(
5127 "Unable to instantiate Application():" + e.toString(), e);
5128 }
5129 }
Geremy Condrab7faaf42012-09-19 18:07:42 -07005130
5131 // add dropbox logging to libcore
5132 DropBox.setReporter(new DropBoxReporter());
5133
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005134 ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
Craig Mautner88c05892013-06-28 09:47:45 -07005135 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005136 public void onConfigurationChanged(Configuration newConfig) {
Craig Mautner88c05892013-06-28 09:47:45 -07005137 synchronized (mResourcesManager) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005138 // We need to apply this change to the resources
5139 // immediately, because upon returning the view
5140 // hierarchy will be informed about it.
Craig Mautner88c05892013-06-28 09:47:45 -07005141 if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
Dianne Hackbornae078162010-03-18 11:29:37 -07005142 // This actually changed the resources! Tell
5143 // everyone about it.
5144 if (mPendingConfiguration == null ||
5145 mPendingConfiguration.isOtherSeqNewer(newConfig)) {
5146 mPendingConfiguration = newConfig;
5147
Jeff Brown9ef09972013-10-15 20:49:59 -07005148 sendMessage(H.CONFIGURATION_CHANGED, newConfig);
Dianne Hackbornae078162010-03-18 11:29:37 -07005149 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005150 }
5151 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005152 }
Craig Mautner88c05892013-06-28 09:47:45 -07005153 @Override
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005154 public void onLowMemory() {
5155 }
Craig Mautner88c05892013-06-28 09:47:45 -07005156 @Override
Dianne Hackbornc68c9132011-07-29 01:25:18 -07005157 public void onTrimMemory(int level) {
5158 }
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005159 });
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005160 }
5161
Romain Guy5e9120d2012-01-30 12:17:22 -08005162 public static ActivityThread systemMain() {
Alan Viverette5e1565e2014-07-29 16:14:25 -07005163 // The system process on low-memory devices do not get to use hardware
5164 // accelerated drawing, since this can add too much overhead to the
5165 // process.
5166 if (!ActivityManager.isHighEndGfx()) {
5167 HardwareRenderer.disable(true);
John Reck73840ea2014-09-22 07:39:18 -07005168 } else {
5169 HardwareRenderer.enableForegroundTrimming();
Alan Viverette5e1565e2014-07-29 16:14:25 -07005170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005171 ActivityThread thread = new ActivityThread();
5172 thread.attach(true);
5173 return thread;
5174 }
5175
Jeff Brown10e89712011-07-08 18:52:57 -07005176 public final void installSystemProviders(List<ProviderInfo> providers) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005177 if (providers != null) {
Jeff Brown10e89712011-07-08 18:52:57 -07005178 installContentProviders(mInitialApplication, providers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
5180 }
5181
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005182 public int getIntCoreSetting(String key, int defaultValue) {
Craig Mautner88c05892013-06-28 09:47:45 -07005183 synchronized (mResourcesManager) {
Svetoslav Ganov9aa597e2011-03-03 18:17:41 -08005184 if (mCoreSettings != null) {
5185 return mCoreSettings.getInt(key, defaultValue);
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005186 }
Craig Mautner88c05892013-06-28 09:47:45 -07005187 return defaultValue;
Svetoslav Ganov54d068e2011-03-02 12:58:40 -08005188 }
5189 }
5190
Geremy Condra69689a72012-09-11 16:57:17 -07005191 private static class EventLoggingReporter implements EventLogger.Reporter {
5192 @Override
5193 public void report (int code, Object... list) {
5194 EventLog.writeEvent(code, list);
5195 }
5196 }
5197
Geremy Condrab7faaf42012-09-19 18:07:42 -07005198 private class DropBoxReporter implements DropBox.Reporter {
5199
5200 private DropBoxManager dropBox;
5201
5202 public DropBoxReporter() {
5203 dropBox = (DropBoxManager) getSystemContext().getSystemService(Context.DROPBOX_SERVICE);
5204 }
5205
5206 @Override
5207 public void addData(String tag, byte[] data, int flags) {
5208 dropBox.addData(tag, data, flags);
5209 }
5210
5211 @Override
5212 public void addText(String tag, String data) {
5213 dropBox.addText(tag, data);
5214 }
5215 }
5216
Romain Guy65b345f2011-07-27 18:51:50 -07005217 public static void main(String[] args) {
Bob Leee5408332009-09-04 18:31:17 -07005218 SamplingProfilerIntegration.start();
5219
Brad Fitzpatrick4d9e6d22010-11-15 08:49:51 -08005220 // CloseGuard defaults to true and can be quite spammy. We
5221 // disable it here, but selectively enable it later (via
5222 // StrictMode) on debug builds, but using DropBox, not logs.
5223 CloseGuard.setEnabled(false);
5224
Jeff Sharkeyb049e212012-09-07 23:16:01 -07005225 Environment.initForCurrentUser();
5226
Geremy Condra69689a72012-09-11 16:57:17 -07005227 // Set the reporter for event logging in libcore
5228 EventLogger.setReporter(new EventLoggingReporter());
5229
Kenny Root8b514752013-02-04 09:35:16 -08005230 Security.addProvider(new AndroidKeyStoreProvider());
5231
Robin Lee3d076af2014-04-25 14:57:49 +01005232 // Make sure TrustedCertificateStore looks in the right place for CA certificates
5233 final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
5234 TrustedCertificateStore.setDefaultUserDirectory(configDir);
5235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 Process.setArgV0("<pre-initialized>");
5237
5238 Looper.prepareMainLooper();
5239
5240 ActivityThread thread = new ActivityThread();
5241 thread.attach(false);
5242
Vairavan Srinivasan7335cfd2012-08-18 18:36:03 -07005243 if (sMainThreadHandler == null) {
5244 sMainThreadHandler = thread.getHandler();
5245 }
5246
Romain Guy5e9120d2012-01-30 12:17:22 -08005247 AsyncTask.init();
5248
Dianne Hackborn287952c2010-09-22 22:34:31 -07005249 if (false) {
5250 Looper.myLooper().setMessageLogging(new
5251 LogPrinter(Log.DEBUG, "ActivityThread"));
5252 }
5253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005254 Looper.loop();
5255
Jeff Brown10e89712011-07-08 18:52:57 -07005256 throw new RuntimeException("Main thread loop unexpectedly exited");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005257 }
5258}